Merge pull request #486 from flowln/wide_bar_customization

This commit is contained in:
flow 2022-12-11 04:16:45 -08:00 committed by GitHub
commit d95625ed88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 286 additions and 77 deletions

View File

@ -67,11 +67,21 @@ void ExternalResourcesPage::ShowContextMenu(const QPoint& pos)
void ExternalResourcesPage::openedImpl() void ExternalResourcesPage::openedImpl()
{ {
m_model->startWatching(); m_model->startWatching();
auto const setting_name = QString("WideBarVisibility_%1").arg(id());
if (!APPLICATION->settings()->contains(setting_name))
m_wide_bar_setting = APPLICATION->settings()->registerSetting(setting_name);
else
m_wide_bar_setting = APPLICATION->settings()->getSetting(setting_name);
ui->actionsToolbar->setVisibilityState(m_wide_bar_setting->get().toByteArray());
} }
void ExternalResourcesPage::closedImpl() void ExternalResourcesPage::closedImpl()
{ {
m_model->stopWatching(); m_model->stopWatching();
m_wide_bar_setting->set(ui->actionsToolbar->getVisibilityState());
} }
void ExternalResourcesPage::retranslate() void ExternalResourcesPage::retranslate()

View File

@ -4,6 +4,7 @@
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include "Application.h" #include "Application.h"
#include "settings/Setting.h"
#include "minecraft/MinecraftInstance.h" #include "minecraft/MinecraftInstance.h"
#include "ui/pages/BasePage.h" #include "ui/pages/BasePage.h"
@ -70,4 +71,6 @@ class ExternalResourcesPage : public QMainWindow, public BasePage {
QString m_viewFilter; QString m_viewFilter;
bool m_controlsEnabled = true; bool m_controlsEnabled = true;
std::shared_ptr<Setting> m_wide_bar_setting = nullptr;
}; };

View File

@ -537,6 +537,19 @@ void ScreenshotsPage::openedImpl()
ui->listView->setModel(nullptr); ui->listView->setModel(nullptr);
} }
} }
auto const setting_name = QString("WideBarVisibility_%1").arg(id());
if (!APPLICATION->settings()->contains(setting_name))
m_wide_bar_setting = APPLICATION->settings()->registerSetting(setting_name);
else
m_wide_bar_setting = APPLICATION->settings()->getSetting(setting_name);
ui->toolBar->setVisibilityState(m_wide_bar_setting->get().toByteArray());
}
void ScreenshotsPage::closedImpl()
{
m_wide_bar_setting->set(ui->toolBar->getVisibilityState());
} }
#include "ScreenshotsPage.moc" #include "ScreenshotsPage.moc"

View File

@ -40,6 +40,8 @@
#include "ui/pages/BasePage.h" #include "ui/pages/BasePage.h"
#include <Application.h> #include <Application.h>
#include "settings/Setting.h"
class QFileSystemModel; class QFileSystemModel;
class QIdentityProxyModel; class QIdentityProxyModel;
namespace Ui namespace Ui
@ -59,7 +61,8 @@ public:
explicit ScreenshotsPage(QString path, QWidget *parent = 0); explicit ScreenshotsPage(QString path, QWidget *parent = 0);
virtual ~ScreenshotsPage(); virtual ~ScreenshotsPage();
virtual void openedImpl() override; void openedImpl() override;
void closedImpl() override;
enum enum
{ {
@ -110,4 +113,6 @@ private:
QString m_folder; QString m_folder;
bool m_valid = false; bool m_valid = false;
bool m_uploadActive = false; bool m_uploadActive = false;
std::shared_ptr<Setting> m_wide_bar_setting = nullptr;
}; };

View File

@ -765,11 +765,21 @@ void ServersPage::updateState()
void ServersPage::openedImpl() void ServersPage::openedImpl()
{ {
m_model->observe(); m_model->observe();
auto const setting_name = QString("WideBarVisibility_%1").arg(id());
if (!APPLICATION->settings()->contains(setting_name))
m_wide_bar_setting = APPLICATION->settings()->registerSetting(setting_name);
else
m_wide_bar_setting = APPLICATION->settings()->getSetting(setting_name);
ui->toolBar->setVisibilityState(m_wide_bar_setting->get().toByteArray());
} }
void ServersPage::closedImpl() void ServersPage::closedImpl()
{ {
m_model->unobserve(); m_model->unobserve();
m_wide_bar_setting->set(ui->toolBar->getVisibilityState());
} }
void ServersPage::on_actionAdd_triggered() void ServersPage::on_actionAdd_triggered()

View File

@ -42,6 +42,8 @@
#include "ui/pages/BasePage.h" #include "ui/pages/BasePage.h"
#include <Application.h> #include <Application.h>
#include "settings/Setting.h"
namespace Ui namespace Ui
{ {
class ServersPage; class ServersPage;
@ -112,5 +114,7 @@ private: // data
Ui::ServersPage *ui = nullptr; Ui::ServersPage *ui = nullptr;
ServersModel * m_model = nullptr; ServersModel * m_model = nullptr;
InstancePtr m_inst = nullptr; InstancePtr m_inst = nullptr;
std::shared_ptr<Setting> m_wide_bar_setting = nullptr;
}; };

View File

@ -126,6 +126,21 @@ void VersionPage::retranslate()
ui->retranslateUi(this); ui->retranslateUi(this);
} }
void VersionPage::openedImpl()
{
auto const setting_name = QString("WideBarVisibility_%1").arg(id());
if (!APPLICATION->settings()->contains(setting_name))
m_wide_bar_setting = APPLICATION->settings()->registerSetting(setting_name);
else
m_wide_bar_setting = APPLICATION->settings()->getSetting(setting_name);
ui->toolBar->setVisibilityState(m_wide_bar_setting->get().toByteArray());
}
void VersionPage::closedImpl()
{
m_wide_bar_setting->set(ui->toolBar->getVisibilityState());
}
QMenu * VersionPage::createPopupMenu() QMenu * VersionPage::createPopupMenu()
{ {
QMenu* filteredMenu = QMainWindow::createPopupMenu(); QMenu* filteredMenu = QMainWindow::createPopupMenu();

View File

@ -70,6 +70,9 @@ public:
virtual bool shouldDisplay() const override; virtual bool shouldDisplay() const override;
void retranslate() override; void retranslate() override;
void openedImpl() override;
void closedImpl() override;
private slots: private slots:
void on_actionChange_version_triggered(); void on_actionChange_version_triggered();
void on_actionInstall_Forge_triggered(); void on_actionInstall_Forge_triggered();
@ -116,6 +119,8 @@ private:
int currentIdx = 0; int currentIdx = 0;
bool controlsEnabled = false; bool controlsEnabled = false;
std::shared_ptr<Setting> m_wide_bar_setting = nullptr;
public slots: public slots:
void versionCurrent(const QModelIndex &current, const QModelIndex &previous); void versionCurrent(const QModelIndex &current, const QModelIndex &previous);

View File

@ -113,11 +113,21 @@ WorldListPage::WorldListPage(BaseInstance *inst, std::shared_ptr<WorldList> worl
void WorldListPage::openedImpl() void WorldListPage::openedImpl()
{ {
m_worlds->startWatching(); m_worlds->startWatching();
auto const setting_name = QString("WideBarVisibility_%1").arg(id());
if (!APPLICATION->settings()->contains(setting_name))
m_wide_bar_setting = APPLICATION->settings()->registerSetting(setting_name);
else
m_wide_bar_setting = APPLICATION->settings()->getSetting(setting_name);
ui->toolBar->setVisibilityState(m_wide_bar_setting->get().toByteArray());
} }
void WorldListPage::closedImpl() void WorldListPage::closedImpl()
{ {
m_worlds->stopWatching(); m_worlds->stopWatching();
m_wide_bar_setting->set(ui->toolBar->getVisibilityState());
} }
WorldListPage::~WorldListPage() WorldListPage::~WorldListPage()

View File

@ -42,6 +42,8 @@
#include <Application.h> #include <Application.h>
#include <LoggedProcess.h> #include <LoggedProcess.h>
#include "settings/Setting.h"
class WorldList; class WorldList;
namespace Ui namespace Ui
{ {
@ -102,6 +104,8 @@ private:
unique_qobject_ptr<LoggedProcess> m_mceditProcess; unique_qobject_ptr<LoggedProcess> m_mceditProcess;
bool m_mceditStarting = false; bool m_mceditStarting = false;
std::shared_ptr<Setting> m_wide_bar_setting = nullptr;
private slots: private slots:
void on_actionCopy_Seed_triggered(); void on_actionCopy_Seed_triggered();
void on_actionMCEdit_triggered(); void on_actionMCEdit_triggered();

View File

@ -1,19 +1,24 @@
#include "WideBar.h" #include "WideBar.h"
#include <QToolButton>
#include <QMenu>
class ActionButton : public QToolButton #include <QContextMenuEvent>
{ #include <QCryptographicHash>
#include <QToolButton>
class ActionButton : public QToolButton {
Q_OBJECT Q_OBJECT
public: public:
ActionButton(QAction * action, QWidget * parent = 0) : QToolButton(parent), m_action(action) { ActionButton(QAction* action, QWidget* parent = nullptr) : QToolButton(parent), m_action(action)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
connect(action, &QAction::changed, this, &ActionButton::actionChanged); connect(action, &QAction::changed, this, &ActionButton::actionChanged);
connect(this, &ActionButton::clicked, action, &QAction::trigger); connect(this, &ActionButton::clicked, action, &QAction::trigger);
actionChanged(); actionChanged();
}; };
private slots: public slots:
void actionChanged() { void actionChanged()
{
setEnabled(m_action->isEnabled()); setEnabled(m_action->isEnabled());
setChecked(m_action->isChecked()); setChecked(m_action->isChecked());
setCheckable(m_action->isCheckable()); setCheckable(m_action->isCheckable());
@ -23,137 +28,242 @@ private slots:
setHidden(!m_action->isVisible()); setHidden(!m_action->isVisible());
setFocusPolicy(Qt::NoFocus); setFocusPolicy(Qt::NoFocus);
} }
private:
QAction * m_action;
};
private:
QAction* m_action;
};
WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent) WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent)
{ {
setFloatable(false); setFloatable(false);
setMovable(false); setMovable(false);
setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
connect(this, &QToolBar::customContextMenuRequested, this, &WideBar::showVisibilityMenu);
} }
WideBar::WideBar(QWidget* parent) : QToolBar(parent) WideBar::WideBar(QWidget* parent) : QToolBar(parent)
{ {
setFloatable(false); setFloatable(false);
setMovable(false); setMovable(false);
}
struct WideBar::BarEntry { setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
enum Type { connect(this, &QToolBar::customContextMenuRequested, this, &WideBar::showVisibilityMenu);
None,
Action,
Separator,
Spacer
} type = None;
QAction *qAction = nullptr;
QAction *wideAction = nullptr;
};
WideBar::~WideBar()
{
for(auto *iter: m_entries) {
delete iter;
}
} }
void WideBar::addAction(QAction* action) void WideBar::addAction(QAction* action)
{ {
auto entry = new BarEntry(); BarEntry entry;
entry->qAction = addWidget(new ActionButton(action, this)); entry.bar_action = addWidget(new ActionButton(action, this));
entry->wideAction = action; entry.menu_action = action;
entry->type = BarEntry::Action; entry.type = BarEntry::Type::Action;
m_entries.push_back(entry); m_entries.push_back(entry);
m_menu_state = MenuState::Dirty;
} }
void WideBar::addSeparator() void WideBar::addSeparator()
{ {
auto entry = new BarEntry(); BarEntry entry;
entry->qAction = QToolBar::addSeparator(); entry.bar_action = QToolBar::addSeparator();
entry->type = BarEntry::Separator; entry.type = BarEntry::Type::Separator;
m_entries.push_back(entry); 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) { auto iter = std::find_if(m_entries.begin(), m_entries.end(), [act](BarEntry const& entry) { return entry.menu_action == act; });
return entry->wideAction == act;
});
return iter; return iter;
} }
void WideBar::insertActionBefore(QAction* before, QAction* action){ void WideBar::insertActionBefore(QAction* before, QAction* action)
{
auto iter = getMatching(before); auto iter = getMatching(before);
if(iter == m_entries.end()) if (iter == m_entries.end())
return; return;
auto entry = new BarEntry(); BarEntry entry;
entry->qAction = insertWidget((*iter)->qAction, new ActionButton(action, this)); entry.bar_action = insertWidget(iter->bar_action, new ActionButton(action, this));
entry->wideAction = action; entry.menu_action = action;
entry->type = BarEntry::Action; entry.type = BarEntry::Type::Action;
m_entries.insert(iter, entry); 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); auto iter = getMatching(after);
if(iter == m_entries.end()) if (iter == m_entries.end())
return; return;
auto entry = new BarEntry(); BarEntry entry;
entry->qAction = insertWidget((*(iter+1))->qAction, new ActionButton(action, this)); entry.bar_action = insertWidget((iter + 1)->bar_action, new ActionButton(action, this));
entry->wideAction = action; entry.menu_action = action;
entry->type = BarEntry::Action; entry.type = BarEntry::Type::Action;
m_entries.insert(iter + 1, entry); m_entries.insert(iter + 1, entry);
m_menu_state = MenuState::Dirty;
} }
void WideBar::insertSpacer(QAction* action) void WideBar::insertSpacer(QAction* action)
{ {
auto iter = getMatching(action); auto iter = getMatching(action);
if(iter == m_entries.end()) if (iter == m_entries.end())
return; return;
QWidget* spacer = new QWidget(); auto* spacer = new QWidget();
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
auto entry = new BarEntry(); BarEntry entry;
entry->qAction = insertWidget((*iter)->qAction, spacer); entry.bar_action = insertWidget(iter->bar_action, spacer);
entry->type = BarEntry::Spacer; entry.type = BarEntry::Type::Spacer;
m_entries.insert(iter, entry); m_entries.insert(iter, entry);
} }
void WideBar::insertSeparator(QAction* before) void WideBar::insertSeparator(QAction* before)
{ {
auto iter = getMatching(before); auto iter = getMatching(before);
if(iter == m_entries.end()) if (iter == m_entries.end())
return; return;
auto entry = new BarEntry(); BarEntry entry;
entry->qAction = QToolBar::insertSeparator(before); entry.bar_action = QToolBar::insertSeparator(before);
entry->type = BarEntry::Separator; entry.type = BarEntry::Type::Separator;
m_entries.insert(iter, entry); 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); auto* contextMenu = new QMenu(title, parent);
for(auto & item: m_entries) { for (auto& item : m_entries) {
switch(item->type) { switch (item.type) {
default: default:
case BarEntry::None: case BarEntry::Type::None:
break; break;
case BarEntry::Separator: case BarEntry::Type::Separator:
case BarEntry::Spacer: case BarEntry::Type::Spacer:
contextMenu->addSeparator(); contextMenu->addSeparator();
break; break;
case BarEntry::Action: case BarEntry::Type::Action:
contextMenu->addAction(item->wideAction); contextMenu->addAction(item.menu_action);
break; break;
} }
} }
return contextMenu; 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" #include "WideBar.moc"

View File

@ -2,9 +2,10 @@
#include <QAction> #include <QAction>
#include <QMap> #include <QMap>
#include <QMenu>
#include <QToolBar> #include <QToolBar>
class QMenu; #include <memory>
class WideBar : public QToolBar { class WideBar : public QToolBar {
Q_OBJECT Q_OBJECT
@ -12,7 +13,7 @@ class WideBar : public QToolBar {
public: public:
explicit WideBar(const QString& title, QWidget* parent = nullptr); explicit WideBar(const QString& title, QWidget* parent = nullptr);
explicit WideBar(QWidget* parent = nullptr); explicit WideBar(QWidget* parent = nullptr);
virtual ~WideBar(); ~WideBar() override = default;
void addAction(QAction* action); void addAction(QAction* action);
void addSeparator(); void addSeparator();
@ -23,12 +24,31 @@ class WideBar : public QToolBar {
void insertActionAfter(QAction* after, QAction* action); void insertActionAfter(QAction* after, QAction* action);
QMenu* createContextMenu(QWidget* parent = nullptr, const QString& title = QString()); 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: 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: 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;
}; };