aboutsummaryrefslogtreecommitdiff
path: root/gui/widgets
diff options
context:
space:
mode:
Diffstat (limited to 'gui/widgets')
-rw-r--r--gui/widgets/IconLabel.cpp30
-rw-r--r--gui/widgets/IconLabel.h26
-rw-r--r--gui/widgets/LineSeparator.cpp37
-rw-r--r--gui/widgets/LineSeparator.h18
-rw-r--r--gui/widgets/ServerStatus.cpp115
-rw-r--r--gui/widgets/ServerStatus.h34
6 files changed, 260 insertions, 0 deletions
diff --git a/gui/widgets/IconLabel.cpp b/gui/widgets/IconLabel.cpp
new file mode 100644
index 00000000..1bfe8dc9
--- /dev/null
+++ b/gui/widgets/IconLabel.cpp
@@ -0,0 +1,30 @@
+#include "IconLabel.h"
+
+#include <QStyle>
+#include <QStyleOption>
+#include <QLayout>
+#include <QPainter>
+#include <QRect>
+
+IconLabel::IconLabel(QWidget *parent, QIcon icon, QSize size)
+ : QWidget(parent), m_icon(icon), m_size(size)
+{
+ setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
+}
+
+QSize IconLabel::sizeHint() const
+{
+ return m_size;
+}
+
+void IconLabel::setIcon(QIcon icon)
+{
+ m_icon = icon;
+ update();
+}
+
+void IconLabel::paintEvent(QPaintEvent *)
+{
+ QPainter p(this);
+ m_icon.paint(&p, contentsRect());
+}
diff --git a/gui/widgets/IconLabel.h b/gui/widgets/IconLabel.h
new file mode 100644
index 00000000..a2f1eef3
--- /dev/null
+++ b/gui/widgets/IconLabel.h
@@ -0,0 +1,26 @@
+#pragma once
+#include <QWidget>
+#include <QIcon>
+
+class QStyleOption;
+
+/**
+ * This is a trivial widget that paints a QIcon of the specified size.
+ */
+class IconLabel : public QWidget
+{
+ Q_OBJECT
+
+public:
+ /// Create a line separator. orientation is the orientation of the line.
+ explicit IconLabel(QWidget *parent, QIcon icon, QSize size);
+
+ virtual QSize sizeHint() const;
+ virtual void paintEvent(QPaintEvent *);
+
+ void setIcon(QIcon icon);
+
+private:
+ QSize m_size;
+ QIcon m_icon;
+};
diff --git a/gui/widgets/LineSeparator.cpp b/gui/widgets/LineSeparator.cpp
new file mode 100644
index 00000000..f4ee173d
--- /dev/null
+++ b/gui/widgets/LineSeparator.cpp
@@ -0,0 +1,37 @@
+#include "LineSeparator.h"
+
+#include <QStyle>
+#include <QStyleOption>
+#include <QLayout>
+#include <QPainter>
+
+void LineSeparator::initStyleOption(QStyleOption *option) const
+{
+ option->initFrom(this);
+ // in a horizontal layout, the line is vertical (and vice versa)
+ if (m_orientation == Qt::Vertical)
+ option->state |= QStyle::State_Horizontal;
+}
+
+LineSeparator::LineSeparator(QWidget *parent, Qt::Orientation orientation)
+ : QWidget(parent), m_orientation(orientation)
+{
+ setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
+}
+
+QSize LineSeparator::sizeHint() const
+{
+ QStyleOption opt;
+ initStyleOption(&opt);
+ const int extent =
+ style()->pixelMetric(QStyle::PM_ToolBarSeparatorExtent, &opt, parentWidget());
+ return QSize(extent, extent);
+}
+
+void LineSeparator::paintEvent(QPaintEvent *)
+{
+ QPainter p(this);
+ QStyleOption opt;
+ initStyleOption(&opt);
+ style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, &p, parentWidget());
+}
diff --git a/gui/widgets/LineSeparator.h b/gui/widgets/LineSeparator.h
new file mode 100644
index 00000000..376f2056
--- /dev/null
+++ b/gui/widgets/LineSeparator.h
@@ -0,0 +1,18 @@
+#pragma once
+#include <QWidget>
+
+class QStyleOption;
+
+class LineSeparator : public QWidget
+{
+ Q_OBJECT
+
+public:
+ /// Create a line separator. orientation is the orientation of the line.
+ explicit LineSeparator(QWidget *parent, Qt::Orientation orientation = Qt::Vertical);
+ QSize sizeHint() const;
+ void paintEvent(QPaintEvent *);
+ void initStyleOption(QStyleOption *option) const;
+private:
+ Qt::Orientation m_orientation = Qt::Vertical;
+};
diff --git a/gui/widgets/ServerStatus.cpp b/gui/widgets/ServerStatus.cpp
new file mode 100644
index 00000000..e540a301
--- /dev/null
+++ b/gui/widgets/ServerStatus.cpp
@@ -0,0 +1,115 @@
+#include "ServerStatus.h"
+#include "LineSeparator.h"
+#include "IconLabel.h"
+#include "logic/status/StatusChecker.h"
+
+#include "MultiMC.h"
+
+#include <QHBoxLayout>
+#include <QFrame>
+#include <QLabel>
+#include <QMap>
+#include <QToolButton>
+#include <QAction>
+
+ServerStatus::ServerStatus(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f)
+{
+ layout = new QHBoxLayout(this);
+ layout->setContentsMargins(0, 0, 0, 0);
+ goodIcon = QIcon::fromTheme("status-good");
+ badIcon = QIcon::fromTheme("status-bad");
+
+ addStatus("minecraft.net", tr("Web"));
+ addLine();
+ addStatus("account.mojang.com", tr("Account"));
+ addLine();
+ addStatus("skins.minecraft.net", tr("Skins"));
+ addLine();
+ addStatus("authserver.mojang.com", tr("Auth"));
+ addLine();
+ addStatus("sessionserver.mojang.com", tr("Session"));
+
+ m_statusRefresh = new QToolButton(this);
+ m_statusRefresh->setCheckable(true);
+ m_statusRefresh->setToolButtonStyle(Qt::ToolButtonIconOnly);
+ m_statusRefresh->setIcon(QIcon::fromTheme("refresh"));
+ layout->addWidget(m_statusRefresh);
+
+ setLayout(layout);
+
+ // Start status checker
+ {
+ auto reloader = MMC->statusChecker().get();
+ connect(reloader, &StatusChecker::statusChanged, this, &ServerStatus::StatusChanged);
+ connect(reloader, &StatusChecker::statusLoading, this, &ServerStatus::StatusReloading);
+ connect(m_statusRefresh, &QAbstractButton::clicked, this, &ServerStatus::reloadStatus);
+ MMC->statusChecker()->startTimer(60000);
+ reloadStatus();
+ }
+}
+
+ServerStatus::~ServerStatus()
+{
+}
+
+void ServerStatus::reloadStatus()
+{
+ MMC->statusChecker()->reloadStatus();
+}
+
+void ServerStatus::addLine()
+{
+ layout->addWidget(new LineSeparator(this));
+}
+
+void ServerStatus::addStatus(QString key, QString name)
+{
+ {
+ auto label = new IconLabel(this, badIcon, QSize(16, 16));
+ label->setToolTip(key);
+ serverLabels[key] = label;
+ layout->addWidget(label);
+ }
+ {
+ auto label = new QLabel(this);
+ label->setText(name);
+ label->setToolTip(key);
+ layout->addWidget(label);
+ }
+}
+
+void ServerStatus::setStatus(QString key, bool value)
+{
+ if (!serverLabels.contains(key))
+ return;
+ IconLabel *label = serverLabels[key];
+ label->setIcon(value ? goodIcon : badIcon);
+}
+
+void ServerStatus::StatusChanged(const QMap<QString, QString> statusEntries)
+{
+ auto convertStatus = [&](QString status)->bool
+ {
+ if (status == "green")
+ return true;
+ else if (status == "yellow")
+ return false;
+ else if (status == "red")
+ return false;
+ return false;
+ }
+ ;
+ auto iter = statusEntries.begin();
+ while (iter != statusEntries.end())
+ {
+ QString key = iter.key();
+ bool value = convertStatus(iter.value());
+ setStatus(key, value);
+ iter++;
+ }
+}
+
+void ServerStatus::StatusReloading(bool is_reloading)
+{
+ m_statusRefresh->setChecked(is_reloading);
+}
diff --git a/gui/widgets/ServerStatus.h b/gui/widgets/ServerStatus.h
new file mode 100644
index 00000000..2244031b
--- /dev/null
+++ b/gui/widgets/ServerStatus.h
@@ -0,0 +1,34 @@
+#pragma once
+#include <QString>
+#include <QWidget>
+#include <QMap>
+#include <QIcon>
+#include <memory>
+
+class IconLabel;
+class QToolButton;
+class QHBoxLayout;
+
+class ServerStatus: public QWidget
+{
+ Q_OBJECT
+public:
+ explicit ServerStatus(QWidget *parent = nullptr, Qt::WindowFlags f = 0);
+ virtual ~ServerStatus();
+ ;
+public slots:
+ void reloadStatus();
+ void StatusChanged(const QMap<QString, QString> statuses);
+ void StatusReloading(bool is_reloading);
+
+private: /* methods */
+ void addLine();
+ void addStatus(QString key, QString name);
+ void setStatus(QString key, bool value);
+private: /* data */
+ QHBoxLayout * layout = nullptr;
+ QToolButton *m_statusRefresh = nullptr;
+ QMap<QString, IconLabel *> serverLabels;
+ QIcon goodIcon;
+ QIcon badIcon;
+};