From 5f6c0594f07df57af2574be0420a68f84b703b87 Mon Sep 17 00:00:00 2001 From: Jens Bache-Wiig Date: Fri, 17 Jul 2009 13:36:08 +0200 Subject: Add priority property to QAction We need this to support the behavior in Gtk+ where, when Qt::ToolButtonTextBesideIcon is used, only text labels for important actions are shown. It will also enable us to prioritize actions in the future when for instance collapsing a toolbar. Task-number: 258290 Reviewed-by: thierry --- demos/textedit/textedit.cpp | 14 +++++++++++++ src/corelib/global/qnamespace.h | 2 +- src/gui/kernel/qaction.cpp | 27 +++++++++++++++++++++++++- src/gui/kernel/qaction.h | 8 ++++++++ src/gui/kernel/qaction_p.h | 1 + src/gui/styles/qstyle.cpp | 2 +- src/gui/widgets/qtoolbutton.cpp | 10 ++++++++-- tests/auto/qaction/tst_qaction.cpp | 4 ++++ tests/auto/qtoolbutton/tst_qtoolbutton.cpp | 27 ++++++++++++++++++++++++++ tools/assistant/tools/assistant/mainwindow.cpp | 8 +++++++- 10 files changed, 97 insertions(+), 6 deletions(-) diff --git a/demos/textedit/textedit.cpp b/demos/textedit/textedit.cpp index 75a13a5..7f8e1fe 100644 --- a/demos/textedit/textedit.cpp +++ b/demos/textedit/textedit.cpp @@ -159,6 +159,7 @@ void TextEdit::setupFileActions() QAction *a; a = new QAction(QIcon(rsrcPath + "/filenew.png"), tr("&New"), this); + a->setPriority(QAction::LowPriority); a->setShortcut(QKeySequence::New); connect(a, SIGNAL(triggered()), this, SLOT(fileNew())); tb->addAction(a); @@ -180,6 +181,7 @@ void TextEdit::setupFileActions() menu->addAction(a); a = new QAction(tr("Save &As..."), this); + a->setPriority(QAction::LowPriority); connect(a, SIGNAL(triggered()), this, SLOT(fileSaveAs())); menu->addAction(a); menu->addSeparator(); @@ -196,6 +198,7 @@ void TextEdit::setupFileActions() menu->addAction(a); a = new QAction(QIcon(rsrcPath + "/exportpdf.png"), tr("&Export PDF..."), this); + a->setPriority(QAction::LowPriority); a->setShortcut(Qt::CTRL + Qt::Key_D); connect(a, SIGNAL(triggered()), this, SLOT(filePrintPdf())); tb->addAction(a); @@ -225,19 +228,23 @@ void TextEdit::setupEditActions() tb->addAction(a); menu->addAction(a); a = actionRedo = new QAction(QIcon(rsrcPath + "/editredo.png"), tr("&Redo"), this); + a->setPriority(QAction::LowPriority); a->setShortcut(QKeySequence::Redo); tb->addAction(a); menu->addAction(a); menu->addSeparator(); a = actionCut = new QAction(QIcon(rsrcPath + "/editcut.png"), tr("Cu&t"), this); + a->setPriority(QAction::LowPriority); a->setShortcut(QKeySequence::Cut); tb->addAction(a); menu->addAction(a); a = actionCopy = new QAction(QIcon(rsrcPath + "/editcopy.png"), tr("&Copy"), this); + a->setPriority(QAction::LowPriority); a->setShortcut(QKeySequence::Copy); tb->addAction(a); menu->addAction(a); a = actionPaste = new QAction(QIcon(rsrcPath + "/editpaste.png"), tr("&Paste"), this); + a->setPriority(QAction::LowPriority); a->setShortcut(QKeySequence::Paste); tb->addAction(a); menu->addAction(a); @@ -254,6 +261,7 @@ void TextEdit::setupTextActions() menuBar()->addMenu(menu); actionTextBold = new QAction(QIcon(rsrcPath + "/textbold.png"), tr("&Bold"), this); + actionTextBold->setPriority(QAction::LowPriority); actionTextBold->setShortcut(Qt::CTRL + Qt::Key_B); QFont bold; bold.setBold(true); @@ -264,6 +272,7 @@ void TextEdit::setupTextActions() actionTextBold->setCheckable(true); actionTextItalic = new QAction(QIcon(rsrcPath + "/textitalic.png"), tr("&Italic"), this); + actionTextItalic->setPriority(QAction::LowPriority); actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I); QFont italic; italic.setItalic(true); @@ -274,6 +283,7 @@ void TextEdit::setupTextActions() actionTextItalic->setCheckable(true); actionTextUnderline = new QAction(QIcon(rsrcPath + "/textunder.png"), tr("&Underline"), this); + actionTextUnderline->setPriority(QAction::LowPriority); actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U); QFont underline; underline.setUnderline(true); @@ -302,12 +312,16 @@ void TextEdit::setupTextActions() actionAlignLeft->setShortcut(Qt::CTRL + Qt::Key_L); actionAlignLeft->setCheckable(true); + actionAlignLeft->setPriority(QAction::LowPriority); actionAlignCenter->setShortcut(Qt::CTRL + Qt::Key_E); actionAlignCenter->setCheckable(true); + actionAlignCenter->setPriority(QAction::LowPriority); actionAlignRight->setShortcut(Qt::CTRL + Qt::Key_R); actionAlignRight->setCheckable(true); + actionAlignRight->setPriority(QAction::LowPriority); actionAlignJustify->setShortcut(Qt::CTRL + Qt::Key_J); actionAlignJustify->setCheckable(true); + actionAlignJustify->setPriority(QAction::LowPriority); tb->addActions(grp->actions()); menu->addActions(grp->actions()); diff --git a/src/corelib/global/qnamespace.h b/src/corelib/global/qnamespace.h index 077e4ef..7770fd6 100644 --- a/src/corelib/global/qnamespace.h +++ b/src/corelib/global/qnamespace.h @@ -1411,7 +1411,7 @@ public: ToolButtonTextOnly, ToolButtonTextBesideIcon, ToolButtonTextUnderIcon, - ToolButtonSystemDefault + ToolButtonFollowStyle }; enum LayoutDirection { diff --git a/src/gui/kernel/qaction.cpp b/src/gui/kernel/qaction.cpp index 4ee17f4..09ba6cc 100644 --- a/src/gui/kernel/qaction.cpp +++ b/src/gui/kernel/qaction.cpp @@ -81,7 +81,7 @@ static QString qt_strippedText(QString s) QActionPrivate::QActionPrivate() : group(0), enabled(1), forceDisabled(0), visible(1), forceInvisible(0), checkable(0), checked(0), separator(0), fontSet(false), - menuRole(QAction::TextHeuristicRole), iconVisibleInMenu(-1) + menuRole(QAction::TextHeuristicRole), priority(QAction::NormalPriority), iconVisibleInMenu(-1) { #ifdef QT3_SUPPORT static int qt_static_action_id = -1; @@ -909,6 +909,31 @@ QString QAction::whatsThis() const return d->whatsthis; } +/*! + \property QAction::priority + \since 4.6 + + \brief tells collapsible layouts how the action should be prioritized + + This property can be set to indicate that an action should be prioritied + in a layout. For instance when toolbars have the Qt::ToolButtonTextBesideIcon + mode is set, lower priority actions will hide text labels to preserve space. +*/ +void QAction::setPriority(Priority priority) +{ + Q_D(QAction); + if (d->priority == priority) + return; + + d->priority = priority; + d->sendDataChanged(); +} + +QAction::Priority QAction::priority() const +{ + Q_D(const QAction); + return d->priority; +} /*! \property QAction::checkable diff --git a/src/gui/kernel/qaction.h b/src/gui/kernel/qaction.h index 6920ec5..133fab4 100644 --- a/src/gui/kernel/qaction.h +++ b/src/gui/kernel/qaction.h @@ -67,6 +67,7 @@ class Q_GUI_EXPORT QAction : public QObject Q_DECLARE_PRIVATE(QAction) Q_ENUMS(MenuRole) + Q_ENUMS(Priority) Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable) Q_PROPERTY(bool checked READ isChecked WRITE setChecked DESIGNABLE isCheckable NOTIFY toggled) Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled) @@ -85,10 +86,14 @@ class Q_GUI_EXPORT QAction : public QObject Q_PROPERTY(bool visible READ isVisible WRITE setVisible) Q_PROPERTY(MenuRole menuRole READ menuRole WRITE setMenuRole) Q_PROPERTY(bool iconVisibleInMenu READ isIconVisibleInMenu WRITE setIconVisibleInMenu) + Q_PROPERTY(Priority priority READ priority WRITE setPriority) public: enum MenuRole { NoRole, TextHeuristicRole, ApplicationSpecificRole, AboutQtRole, AboutRole, PreferencesRole, QuitRole }; + enum Priority { LowPriority = 0, + NormalPriority = 128, + HighPriority = 256}; explicit QAction(QObject* parent); QAction(const QString &text, QObject* parent); QAction(const QIcon &icon, const QString &text, QObject* parent); @@ -123,6 +128,9 @@ public: void setWhatsThis(const QString &what); QString whatsThis() const; + void setPriority(Priority priority); + Priority priority() const; + #ifndef QT_NO_MENU QMenu *menu() const; void setMenu(QMenu *menu); diff --git a/src/gui/kernel/qaction_p.h b/src/gui/kernel/qaction_p.h index bae9bbf..4745ed1 100644 --- a/src/gui/kernel/qaction_p.h +++ b/src/gui/kernel/qaction_p.h @@ -102,6 +102,7 @@ public: uint separator : 1; uint fontSet : 1; QAction::MenuRole menuRole; + QAction::Priority priority; int iconVisibleInMenu : 3; // Only has values -1, 0, and 1 QList widgets; #ifndef QT_NO_GRAPHICSVIEW diff --git a/src/gui/styles/qstyle.cpp b/src/gui/styles/qstyle.cpp index bccd766..d47c610 100644 --- a/src/gui/styles/qstyle.cpp +++ b/src/gui/styles/qstyle.cpp @@ -1865,7 +1865,7 @@ void QStyle::drawItemPixmap(QPainter *painter, const QRect &rect, int alignment, \value SH_DockWidget_ButtonsHaveFrame Determines if dockwidget buttons should have frames. Default is true. - \value SH_ToolButtonStyle Determines the default system style for tool buttons that uses Qt::ToolButtonSystemDefault. + \value SH_ToolButtonStyle Determines the default system style for tool buttons that uses Qt::ToolButtonFollowStyle. \omitvalue SH_UnderlineAccelerator diff --git a/src/gui/widgets/qtoolbutton.cpp b/src/gui/widgets/qtoolbutton.cpp index 5d0a98a..3901245 100644 --- a/src/gui/widgets/qtoolbutton.cpp +++ b/src/gui/widgets/qtoolbutton.cpp @@ -378,11 +378,17 @@ void QToolButton::initStyleOption(QStyleOptionToolButton *option) const if (d->hasMenu()) option->features |= QStyleOptionToolButton::HasMenu; #endif - if (d->toolButtonStyle == Qt::ToolButtonSystemDefault) { + if (d->toolButtonStyle == Qt::ToolButtonFollowStyle) { option->toolButtonStyle = Qt::ToolButtonStyle(style()->styleHint(QStyle::SH_ToolButtonStyle, option, this)); } else option->toolButtonStyle = d->toolButtonStyle; + if (option->toolButtonStyle == Qt::ToolButtonTextBesideIcon) { + // If the action is not prioritized, remove the text label to save space + if (d->defaultAction && d->defaultAction->priority() < QAction::NormalPriority) + option->toolButtonStyle = Qt::ToolButtonIconOnly; + } + if (d->icon.isNull() && d->arrowType == Qt::NoArrow && !forceNoText) { if (!d->text.isEmpty()) option->toolButtonStyle = Qt::ToolButtonTextOnly; @@ -482,7 +488,7 @@ QSize QToolButton::minimumSizeHint() const If you want your toolbars to depend on system settings, as is possible in GNOME and KDE desktop environments you should - use the ToolButtonSystemDefault. + use the ToolButtonFollowStyle. QToolButton automatically connects this slot to the relevant signal in the QMainWindow in which is resides. diff --git a/tests/auto/qaction/tst_qaction.cpp b/tests/auto/qaction/tst_qaction.cpp index 452ca58..3c71baf 100644 --- a/tests/auto/qaction/tst_qaction.cpp +++ b/tests/auto/qaction/tst_qaction.cpp @@ -105,6 +105,10 @@ void tst_QAction::getSetCheck() obj1.setMenu((QMenu *)0); QCOMPARE((QMenu *)0, obj1.menu()); delete var2; + + QCOMPARE(obj1.priority(), QAction::NormalPriority); + obj1.setPriority(QAction::LowPriority); + QCOMPARE(obj1.priority(), QAction::LowPriority); } class MyWidget : public QWidget diff --git a/tests/auto/qtoolbutton/tst_qtoolbutton.cpp b/tests/auto/qtoolbutton/tst_qtoolbutton.cpp index 9e342ad..4176507 100644 --- a/tests/auto/qtoolbutton/tst_qtoolbutton.cpp +++ b/tests/auto/qtoolbutton/tst_qtoolbutton.cpp @@ -64,6 +64,7 @@ public: private slots: void getSetCheck(); void triggered(); + void collapseTextOnPriority(); void task230994_iconSize(); void task176137_autoRepeatOfAction(); @@ -160,6 +161,32 @@ void tst_QToolButton::triggered() delete menu; } +void tst_QToolButton::collapseTextOnPriority() +{ + class MyToolButton : public QToolButton + { + friend class tst_QToolButton; + public: + void initStyleOption(QStyleOptionToolButton *option) + { + QToolButton::initStyleOption(option); + } + }; + + MyToolButton button; + button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon); + QAction action(button.style()->standardIcon(QStyle::SP_ArrowBack), "test", 0); + button.setDefaultAction(&action); + + QStyleOptionToolButton option; + button.initStyleOption(&option); + QVERIFY(option.toolButtonStyle == Qt::ToolButtonTextBesideIcon); + action.setPriority(QAction::LowPriority); + button.initStyleOption(&option); + QVERIFY(option.toolButtonStyle == Qt::ToolButtonIconOnly); +} + + void tst_QToolButton::task230994_iconSize() { //we check that the iconsize returned bu initStyleOption is valid diff --git a/tools/assistant/tools/assistant/mainwindow.cpp b/tools/assistant/tools/assistant/mainwindow.cpp index ae3f7bc..7926020 100644 --- a/tools/assistant/tools/assistant/mainwindow.cpp +++ b/tools/assistant/tools/assistant/mainwindow.cpp @@ -93,7 +93,7 @@ MainWindow::MainWindow(CmdLineParser *cmdLine, QWidget *parent) , m_qtDocInstaller(0) , m_connectedInitSignals(false) { - setToolButtonStyle(Qt::ToolButtonSystemDefault); + setToolButtonStyle(Qt::ToolButtonFollowStyle); if (usesDefaultCollection()) { MainWindow::collectionFileDirectory(true); @@ -431,6 +431,7 @@ void MainWindow::setupActions() SLOT(printPreview())); m_printAction = menu->addAction(tr("&Print..."), m_centralWidget, SLOT(print())); + m_printAction->setPriority(QAction::LowPriority); m_printAction->setIcon(QIcon(resourcePath + QLatin1String("/print.png"))); m_printAction->setShortcut(QKeySequence::Print); @@ -450,6 +451,7 @@ void MainWindow::setupActions() menu = menuBar()->addMenu(tr("&Edit")); m_copyAction = menu->addAction(tr("&Copy selected Text"), m_centralWidget, SLOT(copySelection())); + m_copyAction->setPriority(QAction::LowPriority); m_copyAction->setIconText("&Copy"); m_copyAction->setIcon(QIcon(resourcePath + QLatin1String("/editcopy.png"))); m_copyAction->setShortcuts(QKeySequence::Copy); @@ -476,16 +478,19 @@ void MainWindow::setupActions() m_viewMenu = menuBar()->addMenu(tr("&View")); m_zoomInAction = m_viewMenu->addAction(tr("Zoom &in"), m_centralWidget, SLOT(zoomIn())); + m_zoomInAction->setPriority(QAction::LowPriority); m_zoomInAction->setIcon(QIcon(resourcePath + QLatin1String("/zoomin.png"))); m_zoomInAction->setShortcut(QKeySequence::ZoomIn); m_zoomOutAction = m_viewMenu->addAction(tr("Zoom &out"), m_centralWidget, SLOT(zoomOut())); + m_zoomOutAction->setPriority(QAction::LowPriority); m_zoomOutAction->setIcon(QIcon(resourcePath + QLatin1String("/zoomout.png"))); m_zoomOutAction->setShortcut(QKeySequence::ZoomOut); m_resetZoomAction = m_viewMenu->addAction(tr("Normal &Size"), m_centralWidget, SLOT(resetZoom())); + m_resetZoomAction->setPriority(QAction::LowPriority); m_resetZoomAction->setIcon(QIcon(resourcePath + QLatin1String("/resetzoom.png"))); m_resetZoomAction->setShortcut(tr("Ctrl+0")); @@ -511,6 +516,7 @@ void MainWindow::setupActions() m_backAction->setIcon(QIcon(resourcePath + QLatin1String("/previous.png"))); m_nextAction = menu->addAction(tr("&Forward"), m_centralWidget, SLOT(forward())); + m_nextAction->setPriority(QAction::LowPriority); m_nextAction->setEnabled(false); m_nextAction->setShortcuts(QKeySequence::Forward); m_nextAction->setIcon(QIcon(resourcePath + QLatin1String("/next.png"))); -- cgit v0.12