Merge pull request #724 from leo78913/dot-ui-mainwindow
Closes https://github.com/PrismLauncher/PrismLauncher/issues/594 Closes https://github.com/PrismLauncher/PrismLauncher/issues/69 Closes https://github.com/PrismLauncher/PrismLauncher/issues/473
This commit is contained in:
@ -7,12 +7,20 @@
|
||||
class ActionButton : public QToolButton {
|
||||
Q_OBJECT
|
||||
public:
|
||||
ActionButton(QAction* action, QWidget* parent = nullptr) : QToolButton(parent), m_action(action)
|
||||
ActionButton(QAction* action, QWidget* parent = nullptr, bool use_default_action = false) : QToolButton(parent),
|
||||
m_action(action), m_use_default_action(use_default_action)
|
||||
{
|
||||
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
||||
setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
||||
// workaround for breeze and breeze forks
|
||||
setProperty("_kde_toolButton_alignment", Qt::AlignLeft);
|
||||
|
||||
if (m_use_default_action) {
|
||||
setDefaultAction(action);
|
||||
} else {
|
||||
connect(this, &ActionButton::clicked, action, &QAction::trigger);
|
||||
}
|
||||
connect(action, &QAction::changed, this, &ActionButton::actionChanged);
|
||||
connect(this, &ActionButton::clicked, action, &QAction::trigger);
|
||||
|
||||
actionChanged();
|
||||
};
|
||||
@ -20,17 +28,24 @@ class ActionButton : public QToolButton {
|
||||
void actionChanged()
|
||||
{
|
||||
setEnabled(m_action->isEnabled());
|
||||
setChecked(m_action->isChecked());
|
||||
setCheckable(m_action->isCheckable());
|
||||
setText(m_action->text());
|
||||
setIcon(m_action->icon());
|
||||
setToolTip(m_action->toolTip());
|
||||
setHidden(!m_action->isVisible());
|
||||
// better pop up mode
|
||||
if (m_action->menu()) {
|
||||
setPopupMode(QToolButton::MenuButtonPopup);
|
||||
}
|
||||
if (!m_use_default_action) {
|
||||
setChecked(m_action->isChecked());
|
||||
setCheckable(m_action->isCheckable());
|
||||
setText(m_action->text());
|
||||
setIcon(m_action->icon());
|
||||
setToolTip(m_action->toolTip());
|
||||
setHidden(!m_action->isVisible());
|
||||
}
|
||||
setFocusPolicy(Qt::NoFocus);
|
||||
}
|
||||
|
||||
private:
|
||||
QAction* m_action;
|
||||
bool m_use_default_action;
|
||||
};
|
||||
|
||||
WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent)
|
||||
@ -54,7 +69,7 @@ WideBar::WideBar(QWidget* parent) : QToolBar(parent)
|
||||
void WideBar::addAction(QAction* action)
|
||||
{
|
||||
BarEntry entry;
|
||||
entry.bar_action = addWidget(new ActionButton(action, this));
|
||||
entry.bar_action = addWidget(new ActionButton(action, this, m_use_default_action));
|
||||
entry.menu_action = action;
|
||||
entry.type = BarEntry::Type::Action;
|
||||
|
||||
@ -86,7 +101,7 @@ void WideBar::insertActionBefore(QAction* before, QAction* action)
|
||||
return;
|
||||
|
||||
BarEntry entry;
|
||||
entry.bar_action = insertWidget(iter->bar_action, new ActionButton(action, this));
|
||||
entry.bar_action = insertWidget(iter->bar_action, new ActionButton(action, this, m_use_default_action));
|
||||
entry.menu_action = action;
|
||||
entry.type = BarEntry::Type::Action;
|
||||
|
||||
@ -102,7 +117,7 @@ void WideBar::insertActionAfter(QAction* after, QAction* action)
|
||||
return;
|
||||
|
||||
BarEntry entry;
|
||||
entry.bar_action = insertWidget((iter + 1)->bar_action, new ActionButton(action, this));
|
||||
entry.bar_action = insertWidget((iter + 1)->bar_action, new ActionButton(action, this, m_use_default_action));
|
||||
entry.menu_action = action;
|
||||
entry.type = BarEntry::Type::Action;
|
||||
|
||||
@ -111,6 +126,15 @@ void WideBar::insertActionAfter(QAction* after, QAction* action)
|
||||
m_menu_state = MenuState::Dirty;
|
||||
}
|
||||
|
||||
void WideBar::insertWidgetBefore(QAction* before, QWidget* widget)
|
||||
{
|
||||
auto iter = getMatching(before);
|
||||
if (iter == m_entries.end())
|
||||
return;
|
||||
|
||||
insertWidget(iter->bar_action, widget);
|
||||
}
|
||||
|
||||
void WideBar::insertSpacer(QAction* action)
|
||||
{
|
||||
auto iter = getMatching(action);
|
||||
@ -133,7 +157,7 @@ void WideBar::insertSeparator(QAction* before)
|
||||
return;
|
||||
|
||||
BarEntry entry;
|
||||
entry.bar_action = QToolBar::insertSeparator(before);
|
||||
entry.bar_action = QToolBar::insertSeparator(iter->bar_action);
|
||||
entry.type = BarEntry::Type::Separator;
|
||||
|
||||
m_entries.insert(iter, entry);
|
||||
@ -180,6 +204,10 @@ void WideBar::showVisibilityMenu(QPoint const& position)
|
||||
|
||||
m_bar_menu->clear();
|
||||
|
||||
m_bar_menu->addActions(m_context_menu_actions);
|
||||
|
||||
m_bar_menu->addSeparator()->setText(tr("Customize toolbar actions"));
|
||||
|
||||
for (auto& entry : m_entries) {
|
||||
if (entry.type != BarEntry::Type::Action)
|
||||
continue;
|
||||
@ -206,6 +234,10 @@ void WideBar::showVisibilityMenu(QPoint const& position)
|
||||
m_bar_menu->popup(mapToGlobal(position));
|
||||
}
|
||||
|
||||
void WideBar::addContextMenuAction(QAction* action) {
|
||||
m_context_menu_actions.append(action);
|
||||
}
|
||||
|
||||
[[nodiscard]] QByteArray WideBar::getVisibilityState() const
|
||||
{
|
||||
QByteArray state;
|
||||
|
@ -9,6 +9,9 @@
|
||||
|
||||
class WideBar : public QToolBar {
|
||||
Q_OBJECT
|
||||
// Why: so we can enable / disable alt shortcuts in toolbuttons
|
||||
// with toolbuttons using setDefaultAction, theres no alt shortcuts
|
||||
Q_PROPERTY(bool useDefaultAction MEMBER m_use_default_action)
|
||||
|
||||
public:
|
||||
explicit WideBar(const QString& title, QWidget* parent = nullptr);
|
||||
@ -22,10 +25,13 @@ class WideBar : public QToolBar {
|
||||
void insertSeparator(QAction* before);
|
||||
void insertActionBefore(QAction* before, QAction* action);
|
||||
void insertActionAfter(QAction* after, QAction* action);
|
||||
void insertWidgetBefore(QAction* before, QWidget* widget);
|
||||
|
||||
QMenu* createContextMenu(QWidget* parent = nullptr, const QString& title = QString());
|
||||
void showVisibilityMenu(const QPoint&);
|
||||
|
||||
void addContextMenuAction(QAction* action);
|
||||
|
||||
// Ideally we would use a QBitArray for this, but it doesn't support string conversion,
|
||||
// so using it in settings is very messy.
|
||||
|
||||
@ -48,6 +54,10 @@ class WideBar : public QToolBar {
|
||||
private:
|
||||
QList<BarEntry> m_entries;
|
||||
|
||||
QList<QAction*> m_context_menu_actions;
|
||||
|
||||
bool m_use_default_action = false;
|
||||
|
||||
// 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;
|
||||
|
Reference in New Issue
Block a user