feat(WideBar): Allow disabling alt shortcuts

Signed-off-by: leo78913 <leo3758@riseup.net>
This commit is contained in:
leo78913 2023-01-08 22:47:38 -03:00
parent f3acf35aea
commit b2de01b076
4 changed files with 30 additions and 13 deletions

View File

@ -174,6 +174,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
instanceToolbarSetting = APPLICATION->settings()->getSetting(setting_name); instanceToolbarSetting = APPLICATION->settings()->getSetting(setting_name);
ui->instanceToolBar->setVisibilityState(instanceToolbarSetting->get().toByteArray()); ui->instanceToolBar->setVisibilityState(instanceToolbarSetting->get().toByteArray());
} }
// set the menu for the folders and help tool buttons // set the menu for the folders and help tool buttons

View File

@ -106,6 +106,9 @@
<property name="floatable"> <property name="floatable">
<bool>false</bool> <bool>false</bool>
</property> </property>
<property name="useDefaultAction" stdset="0">
<bool>true</bool>
</property>
<attribute name="toolBarArea"> <attribute name="toolBarArea">
<enum>RightToolBarArea</enum> <enum>RightToolBarArea</enum>
</attribute> </attribute>

View File

@ -7,15 +7,20 @@
class ActionButton : public QToolButton { class ActionButton : public QToolButton {
Q_OBJECT Q_OBJECT
public: 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); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
setToolButtonStyle(Qt::ToolButtonTextBesideIcon); setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
// workaround for breeze and breeze forks // workaround for breeze and breeze forks
setProperty("_kde_toolButton_alignment", Qt::AlignLeft); 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(action, &QAction::changed, this, &ActionButton::actionChanged);
connect(this, &ActionButton::clicked, action, &QAction::trigger);
actionChanged(); actionChanged();
}; };
@ -23,21 +28,24 @@ class ActionButton : public QToolButton {
void actionChanged() void actionChanged()
{ {
setEnabled(m_action->isEnabled()); setEnabled(m_action->isEnabled());
setChecked(m_action->isChecked()); // better pop up mode
setMenu(m_action->menu()); if (m_action->menu()) {
if (menu()) {
setPopupMode(QToolButton::MenuButtonPopup); setPopupMode(QToolButton::MenuButtonPopup);
} }
setCheckable(m_action->isCheckable()); if (!m_use_default_action) {
setText(m_action->text()); setChecked(m_action->isChecked());
setIcon(m_action->icon()); setCheckable(m_action->isCheckable());
setToolTip(m_action->toolTip()); setText(m_action->text());
setHidden(!m_action->isVisible()); setIcon(m_action->icon());
setToolTip(m_action->toolTip());
setHidden(!m_action->isVisible());
}
setFocusPolicy(Qt::NoFocus); setFocusPolicy(Qt::NoFocus);
} }
private: private:
QAction* m_action; QAction* m_action;
bool m_use_default_action;
}; };
WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent) WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent)
@ -61,7 +69,7 @@ WideBar::WideBar(QWidget* parent) : QToolBar(parent)
void WideBar::addAction(QAction* action) void WideBar::addAction(QAction* action)
{ {
BarEntry entry; 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.menu_action = action;
entry.type = BarEntry::Type::Action; entry.type = BarEntry::Type::Action;
@ -93,7 +101,7 @@ void WideBar::insertActionBefore(QAction* before, QAction* action)
return; return;
BarEntry entry; 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.menu_action = action;
entry.type = BarEntry::Type::Action; entry.type = BarEntry::Type::Action;
@ -109,7 +117,7 @@ void WideBar::insertActionAfter(QAction* after, QAction* action)
return; return;
BarEntry entry; 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.menu_action = action;
entry.type = BarEntry::Type::Action; entry.type = BarEntry::Type::Action;

View File

@ -9,6 +9,9 @@
class WideBar : public QToolBar { class WideBar : public QToolBar {
Q_OBJECT 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: public:
explicit WideBar(const QString& title, QWidget* parent = nullptr); explicit WideBar(const QString& title, QWidget* parent = nullptr);
@ -49,6 +52,8 @@ class WideBar : public QToolBar {
private: private:
QList<BarEntry> m_entries; QList<BarEntry> m_entries;
bool m_use_default_action = false;
// Menu to toggle visibility from buttons in the bar // Menu to toggle visibility from buttons in the bar
std::unique_ptr<QMenu> m_bar_menu = nullptr; std::unique_ptr<QMenu> m_bar_menu = nullptr;
enum class MenuState { Fresh, Dirty } m_menu_state = MenuState::Dirty; enum class MenuState { Fresh, Dirty } m_menu_state = MenuState::Dirty;