mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-20 05:23:12 -07:00
ServerItemWidget Styling
This commit is contained in:
parent
43926a6f3f
commit
a7ff2fef3f
10 changed files with 92 additions and 23 deletions
|
@ -41,6 +41,7 @@ class DiscoveryManager : public QObject
|
||||||
friend class DiscoveryManagerPrivate;
|
friend class DiscoveryManagerPrivate;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
ChiakiLog log;
|
||||||
ChiakiDiscoveryService service;
|
ChiakiDiscoveryService service;
|
||||||
QList<DiscoveryHost> hosts;
|
QList<DiscoveryHost> hosts;
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,12 @@
|
||||||
class DynamicGridWidget;
|
class DynamicGridWidget;
|
||||||
class ServerItemWidget;
|
class ServerItemWidget;
|
||||||
|
|
||||||
|
struct DisplayServer
|
||||||
|
{
|
||||||
|
DiscoveryHost discovery_host;
|
||||||
|
bool discovered;
|
||||||
|
};
|
||||||
|
|
||||||
class MainWindow : public QMainWindow
|
class MainWindow : public QMainWindow
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -35,17 +41,21 @@ class MainWindow : public QMainWindow
|
||||||
|
|
||||||
DiscoveryManager discovery_manager;
|
DiscoveryManager discovery_manager;
|
||||||
|
|
||||||
public:
|
QList<DisplayServer> display_servers;
|
||||||
explicit MainWindow(QWidget *parent = nullptr);
|
|
||||||
|
|
||||||
public slots:
|
private slots:
|
||||||
void ServerItemWidgetSelected();
|
void ServerItemWidgetSelected();
|
||||||
void ServerItemWidgetTriggered();
|
void ServerItemWidgetTriggered();
|
||||||
|
|
||||||
void RunDiscovery();
|
void RunDiscovery();
|
||||||
void ShowSettings();
|
void ShowSettings();
|
||||||
|
|
||||||
void DiscoveryHostsUpdated();
|
void UpdateDisplayServers();
|
||||||
|
void UpdateServerWidgets();
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit MainWindow(QWidget *parent = nullptr);
|
||||||
|
~MainWindow() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //CHIAKI_MAINWINDOW_H
|
#endif //CHIAKI_MAINWINDOW_H
|
||||||
|
|
|
@ -33,7 +33,7 @@ class ServerIconWidget : public QWidget
|
||||||
void paintEvent(QPaintEvent *event) override;
|
void paintEvent(QPaintEvent *event) override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ServerIconWidget(QWidget *parent = nullptr) : QWidget(parent) {}
|
explicit ServerIconWidget(QWidget *parent = nullptr);
|
||||||
|
|
||||||
void SetState(ChiakiDiscoveryHostState state) { this->state = state; update(); }
|
void SetState(ChiakiDiscoveryHostState state) { this->state = state; update(); }
|
||||||
};
|
};
|
||||||
|
|
|
@ -18,11 +18,12 @@
|
||||||
#ifndef CHIAKI_SERVERITEMWIDGET_H
|
#ifndef CHIAKI_SERVERITEMWIDGET_H
|
||||||
#define CHIAKI_SERVERITEMWIDGET_H
|
#define CHIAKI_SERVERITEMWIDGET_H
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QFrame>
|
||||||
|
|
||||||
class ServerIconWidget;
|
class ServerIconWidget;
|
||||||
|
class DisplayServer;
|
||||||
|
|
||||||
class ServerItemWidget : public QWidget
|
class ServerItemWidget : public QFrame
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
@ -41,6 +42,8 @@ class ServerItemWidget : public QWidget
|
||||||
bool IsSelected() { return selected; }
|
bool IsSelected() { return selected; }
|
||||||
void SetSelected(bool selected);
|
void SetSelected(bool selected);
|
||||||
|
|
||||||
|
void Update(const DisplayServer &display_server);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void Selected();
|
void Selected();
|
||||||
void Triggered();
|
void Triggered();
|
||||||
|
|
|
@ -29,6 +29,8 @@ static void DiscoveryServiceHostsCallback(ChiakiDiscoveryHost *hosts, size_t hos
|
||||||
|
|
||||||
DiscoveryManager::DiscoveryManager(QObject *parent) : QObject(parent)
|
DiscoveryManager::DiscoveryManager(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
|
chiaki_log_init(&log, CHIAKI_LOG_ALL & ~CHIAKI_LOG_VERBOSE, chiaki_log_cb_print, nullptr);
|
||||||
|
|
||||||
ChiakiDiscoveryServiceOptions options;
|
ChiakiDiscoveryServiceOptions options;
|
||||||
options.ping_ms = PING_MS;
|
options.ping_ms = PING_MS;
|
||||||
options.hosts_max = HOSTS_MAX;
|
options.hosts_max = HOSTS_MAX;
|
||||||
|
@ -43,7 +45,7 @@ DiscoveryManager::DiscoveryManager(QObject *parent) : QObject(parent)
|
||||||
options.send_addr = reinterpret_cast<sockaddr *>(&addr);
|
options.send_addr = reinterpret_cast<sockaddr *>(&addr);
|
||||||
options.send_addr_size = sizeof(addr);
|
options.send_addr_size = sizeof(addr);
|
||||||
|
|
||||||
ChiakiErrorCode err = chiaki_discovery_service_init(&service, &options, nullptr /* TODO */);
|
ChiakiErrorCode err = chiaki_discovery_service_init(&service, &options, &log);
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
throw std::exception();
|
throw std::exception();
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,18 +64,15 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
|
||||||
scroll_content_layout->addStretch(0);
|
scroll_content_layout->addStretch(0);
|
||||||
grid_widget->setContentsMargins(0, 0, 0, 0);
|
grid_widget->setContentsMargins(0, 0, 0, 0);
|
||||||
|
|
||||||
for(int i=0; i<10; i++)
|
|
||||||
{
|
|
||||||
auto w = new ServerItemWidget(grid_widget);
|
|
||||||
connect(w, &ServerItemWidget::Selected, this, &MainWindow::ServerItemWidgetSelected);
|
|
||||||
connect(w, &ServerItemWidget::Triggered, this, &MainWindow::ServerItemWidgetTriggered);
|
|
||||||
server_item_widgets.append(w);
|
|
||||||
grid_widget->AddWidget(w);
|
|
||||||
}
|
|
||||||
|
|
||||||
resize(800, 600);
|
resize(800, 600);
|
||||||
|
|
||||||
connect(&discovery_manager, &DiscoveryManager::HostsUpdated, this, &MainWindow::DiscoveryHostsUpdated);
|
connect(&discovery_manager, &DiscoveryManager::HostsUpdated, this, &MainWindow::UpdateDisplayServers);
|
||||||
|
|
||||||
|
UpdateDisplayServers();
|
||||||
|
}
|
||||||
|
|
||||||
|
MainWindow::~MainWindow()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::ServerItemWidgetSelected()
|
void MainWindow::ServerItemWidgetSelected()
|
||||||
|
@ -111,7 +108,37 @@ void MainWindow::ShowSettings()
|
||||||
qDebug() << "TODO: ShowSettings()";
|
qDebug() << "TODO: ShowSettings()";
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::DiscoveryHostsUpdated()
|
void MainWindow::UpdateDisplayServers()
|
||||||
{
|
{
|
||||||
qDebug() << "updated hosts" << discovery_manager.GetHosts().count();
|
display_servers.clear();
|
||||||
|
|
||||||
|
for(const auto &host : discovery_manager.GetHosts())
|
||||||
|
{
|
||||||
|
DisplayServer server;
|
||||||
|
server.discovered = true;
|
||||||
|
server.discovery_host = host;
|
||||||
|
display_servers.append(server);
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateServerWidgets();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::UpdateServerWidgets()
|
||||||
|
{
|
||||||
|
// remove excessive widgets
|
||||||
|
while(server_item_widgets.count() > display_servers.count())
|
||||||
|
delete server_item_widgets.takeLast();
|
||||||
|
|
||||||
|
// add new widgets as necessary
|
||||||
|
while(server_item_widgets.count() < display_servers.count())
|
||||||
|
{
|
||||||
|
auto widget = new ServerItemWidget(grid_widget);
|
||||||
|
connect(widget, &ServerItemWidget::Selected, this, &MainWindow::ServerItemWidgetSelected);
|
||||||
|
connect(widget, &ServerItemWidget::Triggered, this, &MainWindow::ServerItemWidgetTriggered);
|
||||||
|
server_item_widgets.append(widget);
|
||||||
|
grid_widget->AddWidget(widget);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(size_t i=0; i<server_item_widgets.count(); i++)
|
||||||
|
server_item_widgets[i]->Update(display_servers[i]);
|
||||||
}
|
}
|
|
@ -19,6 +19,10 @@
|
||||||
|
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
|
||||||
|
ServerIconWidget::ServerIconWidget(QWidget *parent) : QWidget(parent)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void ServerIconWidget::paintEvent(QPaintEvent *event)
|
void ServerIconWidget::paintEvent(QPaintEvent *event)
|
||||||
{
|
{
|
||||||
static const float icon_aspect = 100.0f / 17.0f;
|
static const float icon_aspect = 100.0f / 17.0f;
|
||||||
|
|
|
@ -17,15 +17,18 @@
|
||||||
|
|
||||||
#include <serveritemwidget.h>
|
#include <serveritemwidget.h>
|
||||||
#include <servericonwidget.h>
|
#include <servericonwidget.h>
|
||||||
|
#include <mainwindow.h>
|
||||||
|
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
#include <QStyle>
|
#include <QStyle>
|
||||||
|
|
||||||
ServerItemWidget::ServerItemWidget(QWidget *parent) : QWidget(parent)
|
ServerItemWidget::ServerItemWidget(QWidget *parent) : QFrame(parent)
|
||||||
{
|
{
|
||||||
|
setFrameStyle(QFrame::Panel | QFrame::Raised);
|
||||||
|
|
||||||
auto layout = new QVBoxLayout(this);
|
auto layout = new QVBoxLayout(this);
|
||||||
setLayout(layout);
|
this->setLayout(layout);
|
||||||
|
|
||||||
auto label = new QLabel("Server", this);
|
auto label = new QLabel("Server", this);
|
||||||
layout->addWidget(label);
|
layout->addWidget(label);
|
||||||
|
@ -33,6 +36,9 @@ ServerItemWidget::ServerItemWidget(QWidget *parent) : QWidget(parent)
|
||||||
icon_widget = new ServerIconWidget(this);
|
icon_widget = new ServerIconWidget(this);
|
||||||
layout->addWidget(icon_widget);
|
layout->addWidget(icon_widget);
|
||||||
|
|
||||||
|
auto label2 = new QLabel("Server2", this);
|
||||||
|
layout->addWidget(label2);
|
||||||
|
|
||||||
this->selected = true;
|
this->selected = true;
|
||||||
SetSelected(false);
|
SetSelected(false);
|
||||||
|
|
||||||
|
@ -54,5 +60,17 @@ void ServerItemWidget::SetSelected(bool selected)
|
||||||
if(this->selected == selected)
|
if(this->selected == selected)
|
||||||
return;
|
return;
|
||||||
this->selected = selected;
|
this->selected = selected;
|
||||||
setStyleSheet(selected ? "background-color: palette(highlight);" : "");
|
setStyleSheet(selected ? "background-color: palette(highlight); color: palette(highlighted-text);" : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerItemWidget::Update(const DisplayServer &display_server)
|
||||||
|
{
|
||||||
|
if(display_server.discovered)
|
||||||
|
{
|
||||||
|
icon_widget->SetState(display_server.discovery_host.state);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
icon_widget->SetState(CHIAKI_DISCOVERY_HOST_STATE_UNKNOWN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -198,6 +198,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_start(ChiakiDiscoveryThrea
|
||||||
|
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_stop(ChiakiDiscoveryThread *thread)
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_stop(ChiakiDiscoveryThread *thread)
|
||||||
{
|
{
|
||||||
|
chiaki_stop_pipe_stop(&thread->stop_pipe);
|
||||||
ChiakiErrorCode err = chiaki_thread_join(&thread->thread, NULL);
|
ChiakiErrorCode err = chiaki_thread_join(&thread->thread, NULL);
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
return err;
|
return err;
|
||||||
|
|
|
@ -28,6 +28,7 @@ static void discovery_service_report_state(ChiakiDiscoveryService *service);
|
||||||
|
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_service_init(ChiakiDiscoveryService *service, ChiakiDiscoveryServiceOptions *options, ChiakiLog *log)
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_service_init(ChiakiDiscoveryService *service, ChiakiDiscoveryServiceOptions *options, ChiakiLog *log)
|
||||||
{
|
{
|
||||||
|
service->log = log;
|
||||||
service->options = *options;
|
service->options = *options;
|
||||||
service->ping_index = 0;
|
service->ping_index = 0;
|
||||||
|
|
||||||
|
@ -222,6 +223,8 @@ static void discovery_service_host_received(ChiakiDiscoveryHost *host, void *use
|
||||||
goto r2con;
|
goto r2con;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CHIAKI_LOGI(service->log, "Discovery Service detected new host with id %s", host->host_id);
|
||||||
|
|
||||||
change = true;
|
change = true;
|
||||||
index = service->hosts_count++;
|
index = service->hosts_count++;
|
||||||
memset(&service->hosts[index], 0, sizeof(ChiakiDiscoveryHost));
|
memset(&service->hosts[index], 0, sizeof(ChiakiDiscoveryHost));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue