From 446ddd00710253ae5ebf4438a1934a5c52a55f97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Tue, 13 Aug 2019 20:32:29 +0200 Subject: [PATCH] Make ServerIconWidget Paint State --- gui/src/mainwindow.cpp | 6 +++- gui/src/servericonwidget.cpp | 56 ++++++++++++++++++++++++++++++------ lib/src/discoveryservice.c | 2 +- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/gui/src/mainwindow.cpp b/gui/src/mainwindow.cpp index 3306799..f1cde41 100644 --- a/gui/src/mainwindow.cpp +++ b/gui/src/mainwindow.cpp @@ -127,7 +127,11 @@ void MainWindow::UpdateServerWidgets() { // remove excessive widgets while(server_item_widgets.count() > display_servers.count()) - delete server_item_widgets.takeLast(); + { + auto widget = server_item_widgets.takeLast(); + grid_widget->RemoveWidget(widget); + delete widget; + } // add new widgets as necessary while(server_item_widgets.count() < display_servers.count()) diff --git a/gui/src/servericonwidget.cpp b/gui/src/servericonwidget.cpp index e6dd621..e9d4e08 100644 --- a/gui/src/servericonwidget.cpp +++ b/gui/src/servericonwidget.cpp @@ -26,28 +26,68 @@ ServerIconWidget::ServerIconWidget(QWidget *parent) : QWidget(parent) void ServerIconWidget::paintEvent(QPaintEvent *event) { static const float icon_aspect = 100.0f / 17.0f; + static const float coolness = 0.585913713f; if(width() == 0 || height() == 0) return; QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); - QRect icon_rect; + QRectF icon_rect; float widget_aspect = (float)width() / (float)height(); if(widget_aspect > icon_aspect) { icon_rect.setHeight(height()); - icon_rect.setWidth((int)((float)height() * icon_aspect)); - icon_rect.moveTop(0); - icon_rect.moveLeft((width() - icon_rect.width()) / 2); + icon_rect.setWidth((float)height() * icon_aspect); + icon_rect.moveTop(0.0f); + icon_rect.moveLeft(((float)width() - icon_rect.width()) * 0.5f); } else { icon_rect.setWidth(width()); - icon_rect.setHeight((int)((float)width() / icon_aspect)); - icon_rect.moveLeft(0); - icon_rect.moveTop((height() - icon_rect.height()) / 2); + icon_rect.setHeight((float)width() / icon_aspect); + icon_rect.moveLeft(0.0f); + icon_rect.moveTop(((float)height() - icon_rect.height()) * 0.5f); } - painter.fillRect(icon_rect, Qt::red); + + auto XForY = [&icon_rect](float y, bool right) + { + float r = (icon_rect.height() - y) * coolness; + if(right) + r += icon_rect.width() - icon_rect.height() * coolness; + return r; + }; + + auto SectionPath = [&XForY, &icon_rect](float y0, float y1) + { + QPainterPath path; + path.moveTo(XForY(y0, false), y0); + path.lineTo(XForY(y1, false), y1); + path.lineTo(XForY(y1, true), y1); + path.lineTo(XForY(y0, true), y0); + path.translate(icon_rect.topLeft()); + return path; + }; + + auto color = palette().color(QPalette::Text); + + QColor bar_color; + switch(state) + { + case CHIAKI_DISCOVERY_HOST_STATE_STANDBY: + bar_color = QColor(255, 174, 47); + break; + case CHIAKI_DISCOVERY_HOST_STATE_READY: + bar_color = QColor(0, 167, 255); + break; + default: + break; + } + + if(bar_color.isValid()) + painter.fillPath(SectionPath(icon_rect.height() * 0.41f - 1.0f, icon_rect.height() * 0.59f + 1.0f), bar_color); + painter.fillPath(SectionPath(0, icon_rect.height() * 0.41f), color); + painter.fillPath(SectionPath(icon_rect.height() * 0.59f, icon_rect.height()), color); } \ No newline at end of file diff --git a/lib/src/discoveryservice.c b/lib/src/discoveryservice.c index b86afe5..0dacc49 100644 --- a/lib/src/discoveryservice.c +++ b/lib/src/discoveryservice.c @@ -182,7 +182,7 @@ static void discovery_service_drop_old_hosts(ChiakiDiscoveryService *service) } if(change) - discovery_service_drop_old_hosts(service); + discovery_service_report_state(service); } static void discovery_service_host_received(ChiakiDiscoveryHost *host, void *user)