diff options
Diffstat (limited to 'launcher/ui/widgets')
-rw-r--r-- | launcher/ui/widgets/JavaSettingsWidget.cpp | 42 | ||||
-rw-r--r-- | launcher/ui/widgets/JavaSettingsWidget.h | 9 | ||||
-rw-r--r-- | launcher/ui/widgets/PageContainer.cpp | 5 | ||||
-rw-r--r-- | launcher/ui/widgets/PageContainer.h | 1 | ||||
-rw-r--r-- | launcher/ui/widgets/WideBar.cpp | 250 | ||||
-rw-r--r-- | launcher/ui/widgets/WideBar.h | 30 |
6 files changed, 253 insertions, 84 deletions
diff --git a/launcher/ui/widgets/JavaSettingsWidget.cpp b/launcher/ui/widgets/JavaSettingsWidget.cpp index c7c4dbbd..15994319 100644 --- a/launcher/ui/widgets/JavaSettingsWidget.cpp +++ b/launcher/ui/widgets/JavaSettingsWidget.cpp @@ -71,6 +71,7 @@ void JavaSettingsWidget::setupUi() m_memoryGroupBox->setObjectName(QStringLiteral("memoryGroupBox")); m_gridLayout_2 = new QGridLayout(m_memoryGroupBox); m_gridLayout_2->setObjectName(QStringLiteral("gridLayout_2")); + m_gridLayout_2->setColumnStretch(0, 1); m_labelMinMem = new QLabel(m_memoryGroupBox); m_labelMinMem->setObjectName(QStringLiteral("labelMinMem")); @@ -80,7 +81,7 @@ void JavaSettingsWidget::setupUi() m_minMemSpinBox->setObjectName(QStringLiteral("minMemSpinBox")); m_minMemSpinBox->setSuffix(QStringLiteral(" MiB")); m_minMemSpinBox->setMinimum(128); - m_minMemSpinBox->setMaximum(m_availableMemory); + m_minMemSpinBox->setMaximum(1048576); m_minMemSpinBox->setSingleStep(128); m_labelMinMem->setBuddy(m_minMemSpinBox); m_gridLayout_2->addWidget(m_minMemSpinBox, 0, 1, 1, 1); @@ -93,11 +94,15 @@ void JavaSettingsWidget::setupUi() m_maxMemSpinBox->setObjectName(QStringLiteral("maxMemSpinBox")); m_maxMemSpinBox->setSuffix(QStringLiteral(" MiB")); m_maxMemSpinBox->setMinimum(128); - m_maxMemSpinBox->setMaximum(m_availableMemory); + m_maxMemSpinBox->setMaximum(1048576); m_maxMemSpinBox->setSingleStep(128); m_labelMaxMem->setBuddy(m_maxMemSpinBox); m_gridLayout_2->addWidget(m_maxMemSpinBox, 1, 1, 1, 1); + m_labelMaxMemIcon = new QLabel(m_memoryGroupBox); + m_labelMaxMemIcon->setObjectName(QStringLiteral("labelMaxMemIcon")); + m_gridLayout_2->addWidget(m_labelMaxMemIcon, 1, 2, 1, 1); + m_labelPermGen = new QLabel(m_memoryGroupBox); m_labelPermGen->setObjectName(QStringLiteral("labelPermGen")); m_labelPermGen->setText(QStringLiteral("PermGen:")); @@ -108,7 +113,7 @@ void JavaSettingsWidget::setupUi() m_permGenSpinBox->setObjectName(QStringLiteral("permGenSpinBox")); m_permGenSpinBox->setSuffix(QStringLiteral(" MiB")); m_permGenSpinBox->setMinimum(64); - m_permGenSpinBox->setMaximum(m_availableMemory); + m_permGenSpinBox->setMaximum(1048576); m_permGenSpinBox->setSingleStep(8); m_gridLayout_2->addWidget(m_permGenSpinBox, 2, 1, 1, 1); m_permGenSpinBox->setVisible(false); @@ -130,6 +135,7 @@ void JavaSettingsWidget::initialize() m_minMemSpinBox->setValue(observedMinMemory); m_maxMemSpinBox->setValue(observedMaxMemory); m_permGenSpinBox->setValue(observedPermGenMemory); + updateThresholds(); } void JavaSettingsWidget::refresh() @@ -210,9 +216,9 @@ int JavaSettingsWidget::permGenSize() const void JavaSettingsWidget::memoryValueChanged(int) { bool actuallyChanged = false; - int min = m_minMemSpinBox->value(); - int max = m_maxMemSpinBox->value(); - int permgen = m_permGenSpinBox->value(); + unsigned int min = m_minMemSpinBox->value(); + unsigned int max = m_maxMemSpinBox->value(); + unsigned int permgen = m_permGenSpinBox->value(); QObject *obj = sender(); if (obj == m_minMemSpinBox && min != observedMinMemory) { @@ -242,6 +248,7 @@ void JavaSettingsWidget::memoryValueChanged(int) if(actuallyChanged) { checkJavaPathOnEdit(m_javaPathTextBox->text()); + updateThresholds(); } } @@ -435,3 +442,26 @@ void JavaSettingsWidget::retranslate() m_permGenSpinBox->setToolTip(tr("The amount of memory available to store loaded Java classes.")); m_javaBrowseBtn->setText(tr("Browse")); } + +void JavaSettingsWidget::updateThresholds() +{ + QString iconName; + + if (observedMaxMemory >= m_availableMemory) { + iconName = "status-bad"; + m_labelMaxMemIcon->setToolTip(tr("Your maximum memory allocation exceeds your system memory capacity.")); + } else if (observedMaxMemory > (m_availableMemory * 0.9)) { + iconName = "status-yellow"; + m_labelMaxMemIcon->setToolTip(tr("Your maximum memory allocation approaches your system memory capacity.")); + } else { + iconName = "status-good"; + m_labelMaxMemIcon->setToolTip(""); + } + + { + auto height = m_labelMaxMemIcon->fontInfo().pixelSize(); + QIcon icon = APPLICATION->getThemedIcon(iconName); + QPixmap pix = icon.pixmap(height, height); + m_labelMaxMemIcon->setPixmap(pix); + } +} diff --git a/launcher/ui/widgets/JavaSettingsWidget.h b/launcher/ui/widgets/JavaSettingsWidget.h index 5344e2cd..e4b7c712 100644 --- a/launcher/ui/widgets/JavaSettingsWidget.h +++ b/launcher/ui/widgets/JavaSettingsWidget.h @@ -56,6 +56,8 @@ public: int maxHeapSize() const; QString javaPath() const; + void updateThresholds(); + protected slots: void memoryValueChanged(int); @@ -85,6 +87,7 @@ private: /* data */ QSpinBox *m_maxMemSpinBox = nullptr; QLabel *m_labelMinMem = nullptr; QLabel *m_labelMaxMem = nullptr; + QLabel *m_labelMaxMemIcon = nullptr; QSpinBox *m_minMemSpinBox = nullptr; QLabel *m_labelPermGen = nullptr; QSpinBox *m_permGenSpinBox = nullptr; @@ -92,9 +95,9 @@ private: /* data */ QIcon yellowIcon; QIcon badIcon; - int observedMinMemory = 0; - int observedMaxMemory = 0; - int observedPermGenMemory = 0; + unsigned int observedMinMemory = 0; + unsigned int observedMaxMemory = 0; + unsigned int observedPermGenMemory = 0; QString queuedCheck; uint64_t m_availableMemory = 0ull; shared_qobject_ptr<JavaChecker> m_checker; diff --git a/launcher/ui/widgets/PageContainer.cpp b/launcher/ui/widgets/PageContainer.cpp index 8d606820..0a06a351 100644 --- a/launcher/ui/widgets/PageContainer.cpp +++ b/launcher/ui/widgets/PageContainer.cpp @@ -130,6 +130,11 @@ bool PageContainer::selectPage(QString pageId) return false; } +BasePage* PageContainer::getPage(QString pageId) +{ + return m_model->findPageEntryById(pageId); +} + void PageContainer::refreshContainer() { m_proxyModel->invalidate(); diff --git a/launcher/ui/widgets/PageContainer.h b/launcher/ui/widgets/PageContainer.h index 80d87a9b..97e294dc 100644 --- a/launcher/ui/widgets/PageContainer.h +++ b/launcher/ui/widgets/PageContainer.h @@ -79,6 +79,7 @@ public: } virtual bool selectPage(QString pageId) override; + BasePage* getPage(QString pageId) override; void refreshContainer() override; virtual void setParentContainer(BasePageContainer * container) diff --git a/launcher/ui/widgets/WideBar.cpp b/launcher/ui/widgets/WideBar.cpp index 79f1e0c9..428be563 100644 --- a/launcher/ui/widgets/WideBar.cpp +++ b/launcher/ui/widgets/WideBar.cpp @@ -1,19 +1,24 @@ #include "WideBar.h" + +#include <QContextMenuEvent> +#include <QCryptographicHash> #include <QToolButton> -#include <QMenu> -class ActionButton : public QToolButton -{ +class ActionButton : public QToolButton { Q_OBJECT -public: - ActionButton(QAction * action, QWidget * parent = 0) : QToolButton(parent), m_action(action) { + public: + ActionButton(QAction* action, QWidget* parent = nullptr) : QToolButton(parent), m_action(action) + { setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + connect(action, &QAction::changed, this, &ActionButton::actionChanged); connect(this, &ActionButton::clicked, action, &QAction::trigger); + actionChanged(); }; -private slots: - void actionChanged() { + public slots: + void actionChanged() + { setEnabled(m_action->isEnabled()); setChecked(m_action->isChecked()); setCheckable(m_action->isCheckable()); @@ -23,137 +28,242 @@ private slots: setHidden(!m_action->isVisible()); setFocusPolicy(Qt::NoFocus); } -private: - QAction * m_action; -}; + private: + QAction* m_action; +}; WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent) { setFloatable(false); setMovable(false); + + setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu); + connect(this, &QToolBar::customContextMenuRequested, this, &WideBar::showVisibilityMenu); } WideBar::WideBar(QWidget* parent) : QToolBar(parent) { setFloatable(false); setMovable(false); -} - -struct WideBar::BarEntry { - enum Type { - None, - Action, - Separator, - Spacer - } type = None; - QAction *qAction = nullptr; - QAction *wideAction = nullptr; -}; - -WideBar::~WideBar() -{ - for(auto *iter: m_entries) { - delete iter; - } + setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu); + connect(this, &QToolBar::customContextMenuRequested, this, &WideBar::showVisibilityMenu); } void WideBar::addAction(QAction* action) { - auto entry = new BarEntry(); - entry->qAction = addWidget(new ActionButton(action, this)); - entry->wideAction = action; - entry->type = BarEntry::Action; + BarEntry entry; + entry.bar_action = addWidget(new ActionButton(action, this)); + entry.menu_action = action; + entry.type = BarEntry::Type::Action; + m_entries.push_back(entry); + + m_menu_state = MenuState::Dirty; } void WideBar::addSeparator() { - auto entry = new BarEntry(); - entry->qAction = QToolBar::addSeparator(); - entry->type = BarEntry::Separator; + BarEntry entry; + entry.bar_action = QToolBar::addSeparator(); + entry.type = BarEntry::Type::Separator; + m_entries.push_back(entry); } -auto WideBar::getMatching(QAction* act) -> QList<BarEntry*>::iterator +auto WideBar::getMatching(QAction* act) -> QList<BarEntry>::iterator { - auto iter = std::find_if(m_entries.begin(), m_entries.end(), [act](BarEntry * entry) { - return entry->wideAction == act; - }); - + auto iter = std::find_if(m_entries.begin(), m_entries.end(), [act](BarEntry const& entry) { return entry.menu_action == act; }); + return iter; } -void WideBar::insertActionBefore(QAction* before, QAction* action){ +void WideBar::insertActionBefore(QAction* before, QAction* action) +{ auto iter = getMatching(before); - if(iter == m_entries.end()) + if (iter == m_entries.end()) return; - auto entry = new BarEntry(); - entry->qAction = insertWidget((*iter)->qAction, new ActionButton(action, this)); - entry->wideAction = action; - entry->type = BarEntry::Action; + BarEntry entry; + entry.bar_action = insertWidget(iter->bar_action, new ActionButton(action, this)); + entry.menu_action = action; + entry.type = BarEntry::Type::Action; + m_entries.insert(iter, entry); + + m_menu_state = MenuState::Dirty; } -void WideBar::insertActionAfter(QAction* after, QAction* action){ +void WideBar::insertActionAfter(QAction* after, QAction* action) +{ auto iter = getMatching(after); - if(iter == m_entries.end()) + if (iter == m_entries.end()) return; - auto entry = new BarEntry(); - entry->qAction = insertWidget((*(iter+1))->qAction, new ActionButton(action, this)); - entry->wideAction = action; - entry->type = BarEntry::Action; + BarEntry entry; + entry.bar_action = insertWidget((iter + 1)->bar_action, new ActionButton(action, this)); + entry.menu_action = action; + entry.type = BarEntry::Type::Action; + m_entries.insert(iter + 1, entry); + + m_menu_state = MenuState::Dirty; } void WideBar::insertSpacer(QAction* action) { auto iter = getMatching(action); - if(iter == m_entries.end()) + if (iter == m_entries.end()) return; - QWidget* spacer = new QWidget(); + auto* spacer = new QWidget(); spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - auto entry = new BarEntry(); - entry->qAction = insertWidget((*iter)->qAction, spacer); - entry->type = BarEntry::Spacer; + BarEntry entry; + entry.bar_action = insertWidget(iter->bar_action, spacer); + entry.type = BarEntry::Type::Spacer; m_entries.insert(iter, entry); } void WideBar::insertSeparator(QAction* before) { auto iter = getMatching(before); - if(iter == m_entries.end()) + if (iter == m_entries.end()) return; - auto entry = new BarEntry(); - entry->qAction = QToolBar::insertSeparator(before); - entry->type = BarEntry::Separator; + BarEntry entry; + entry.bar_action = QToolBar::insertSeparator(before); + entry.type = BarEntry::Type::Separator; + m_entries.insert(iter, entry); } -QMenu * WideBar::createContextMenu(QWidget *parent, const QString & title) +QMenu* WideBar::createContextMenu(QWidget* parent, const QString& title) { - QMenu *contextMenu = new QMenu(title, parent); - for(auto & item: m_entries) { - switch(item->type) { + auto* contextMenu = new QMenu(title, parent); + for (auto& item : m_entries) { + switch (item.type) { default: - case BarEntry::None: + case BarEntry::Type::None: break; - case BarEntry::Separator: - case BarEntry::Spacer: + case BarEntry::Type::Separator: + case BarEntry::Type::Spacer: contextMenu->addSeparator(); break; - case BarEntry::Action: - contextMenu->addAction(item->wideAction); + case BarEntry::Type::Action: + contextMenu->addAction(item.menu_action); break; } } return contextMenu; } +static void copyAction(QAction* from, QAction* to) +{ + Q_ASSERT(from); + Q_ASSERT(to); + + to->setText(from->text()); + to->setIcon(from->icon()); + to->setToolTip(from->toolTip()); +} + +void WideBar::showVisibilityMenu(QPoint const& position) +{ + if (!m_bar_menu) + m_bar_menu = std::make_unique<QMenu>(this); + + if (m_menu_state == MenuState::Dirty) { + for (auto* old_action : m_bar_menu->actions()) + old_action->deleteLater(); + + m_bar_menu->clear(); + + for (auto& entry : m_entries) { + if (entry.type != BarEntry::Type::Action) + continue; + + auto act = new QAction(); + copyAction(entry.menu_action, act); + + act->setCheckable(true); + act->setChecked(entry.bar_action->isVisible()); + + connect(act, &QAction::toggled, entry.bar_action, [this, &entry](bool toggled){ + entry.bar_action->setVisible(toggled); + + // NOTE: This is needed so that disabled actions get reflected on the button when it is made visible. + static_cast<ActionButton*>(widgetForAction(entry.bar_action))->actionChanged(); + }); + + m_bar_menu->addAction(act); + } + + m_menu_state = MenuState::Fresh; + } + + m_bar_menu->popup(mapToGlobal(position)); +} + +[[nodiscard]] QByteArray WideBar::getVisibilityState() const +{ + QByteArray state; + + for (auto const& entry : m_entries) { + if (entry.type != BarEntry::Type::Action) + continue; + + state.append(entry.bar_action->isVisible() ? '1' : '0'); + } + + state.append(','); + state.append(getHash()); + + return state; +} + +void WideBar::setVisibilityState(QByteArray&& state) +{ + auto split = state.split(','); + + auto bits = split.first(); + auto hash = split.last(); + + // If the actions changed, we better not try to load the old one to avoid unwanted hiding + if (!checkHash(hash)) + return; + + qsizetype i = 0; + for (auto& entry : m_entries) { + if (entry.type != BarEntry::Type::Action) + continue; + if (i == bits.size()) + break; + + entry.bar_action->setVisible(bits.at(i++) == '1'); + + // NOTE: This is needed so that disabled actions get reflected on the button when it is made visible. + static_cast<ActionButton*>(widgetForAction(entry.bar_action))->actionChanged(); + } +} + +QByteArray WideBar::getHash() const +{ + QCryptographicHash hash(QCryptographicHash::Sha1); + for (auto const& entry : m_entries) { + if (entry.type != BarEntry::Type::Action) + continue; + hash.addData(entry.menu_action->text().toLatin1()); + } + + return hash.result().toBase64(); +} + +bool WideBar::checkHash(QByteArray const& old_hash) const +{ + return old_hash == getHash(); +} + + #include "WideBar.moc" diff --git a/launcher/ui/widgets/WideBar.h b/launcher/ui/widgets/WideBar.h index 8ff62ef2..a0a7896c 100644 --- a/launcher/ui/widgets/WideBar.h +++ b/launcher/ui/widgets/WideBar.h @@ -2,9 +2,10 @@ #include <QAction> #include <QMap> +#include <QMenu> #include <QToolBar> -class QMenu; +#include <memory> class WideBar : public QToolBar { Q_OBJECT @@ -12,7 +13,7 @@ class WideBar : public QToolBar { public: explicit WideBar(const QString& title, QWidget* parent = nullptr); explicit WideBar(QWidget* parent = nullptr); - virtual ~WideBar(); + ~WideBar() override = default; void addAction(QAction* action); void addSeparator(); @@ -23,12 +24,31 @@ class WideBar : public QToolBar { void insertActionAfter(QAction* after, QAction* action); QMenu* createContextMenu(QWidget* parent = nullptr, const QString& title = QString()); + void showVisibilityMenu(const QPoint&); + + // Ideally we would use a QBitArray for this, but it doesn't support string conversion, + // so using it in settings is very messy. + + [[nodiscard]] QByteArray getVisibilityState() const; + void setVisibilityState(QByteArray&&); private: - struct BarEntry; + struct BarEntry { + enum class Type { None, Action, Separator, Spacer } type = Type::None; + QAction* bar_action = nullptr; + QAction* menu_action = nullptr; + }; - auto getMatching(QAction* act) -> QList<BarEntry*>::iterator; + auto getMatching(QAction* act) -> QList<BarEntry>::iterator; + + /** Used to distinguish between versions of the WideBar with different actions */ + [[nodiscard]] QByteArray getHash() const; + [[nodiscard]] bool checkHash(QByteArray const&) const; private: - QList<BarEntry*> m_entries; + QList<BarEntry> m_entries; + + // Menu to toggle visibility from buttons in the bar + std::unique_ptr<QMenu> m_bar_menu = nullptr; + enum class MenuState { Fresh, Dirty } m_menu_state = MenuState::Dirty; }; |