Make ServerIconWidget Paint State

This commit is contained in:
Florian Märkl 2019-08-13 20:32:29 +02:00
commit 446ddd0071
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
3 changed files with 54 additions and 10 deletions

View file

@ -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())

View file

@ -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);
}

View file

@ -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)