📄 q3action.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "q3action.h"#ifndef QT_NO_ACTION#include "qevent.h"#include "q3toolbar.h"#include "qlist.h"#include "q3popupmenu.h"#include "q3accel.h"#include "qtoolbutton.h"#include "qcombobox.h"#include "qtooltip.h"#include "qwhatsthis.h"#include "qstatusbar.h"#include "qaction.h"/*! \class Q3Action q3action.h \brief The Q3Action class provides an abstract user interface action that can appear both in menus and tool bars. \compat In GUI applications many commands can be invoked via a menu option, a toolbar button and a keyboard accelerator. Since the same action must be performed regardless of how the action was invoked, and since the menu and toolbar should be kept in sync, it is useful to represent a command as an \e action. An action can be added to a menu and a toolbar and will automatically keep them in sync. For example, if the user presses a Bold toolbar button the Bold menu item will automatically be checked. A Q3Action may contain an icon, a menu text, an accelerator, a status text, a "What's This?" text and a tool tip. Most of these can be set in the constructor. They can also be set independently with setIconSet(), setText(), setMenuText(), setToolTip(), setStatusTip(), setWhatsThis() and setAccel(). An action may be a toggle action e.g. a Bold toolbar button, or a command action, e.g. 'Open File' to invoke an open file dialog. Toggle actions emit the toggled() signal when their state changes. Both command and toggle actions emit the activated() signal when they are invoked. Use setToggleAction() to set an action's toggled status. To see if an action is a toggle action use isToggleAction(). A toggle action may be "on", isOn() returns true, or "off", isOn() returns false. Actions are added to widgets (menus or toolbars) using addTo(), and removed using removeFrom(). Note that when using Q3ToolBar and Q3PopupMenu, their actions must be Q3Actions. Once a Q3Action has been created it should be added to the relevant menu and toolbar and then connected to the slot which will perform the action. We recommend that actions are created as children of the window that they are used in. In most cases actions will be children of the application's main window. To prevent recursion, don't create an action as a child of a widget that the action is later added to.*/class Q3ActionPrivate{public: Q3ActionPrivate(Q3Action *act); ~Q3ActionPrivate(); QIcon *icon; QString text; QString menutext; QString tooltip; QString statustip; QString whatsthis;#ifndef QT_NO_ACCEL QKeySequence key; Q3Accel* accel; int accelid;#endif uint enabled : 1; uint visible : 1; uint toggleaction : 1; uint on : 1; uint forceDisabled : 1; uint forceInvisible : 1; Q3ActionGroupPrivate* d_group; Q3Action *action; struct MenuItem { MenuItem():popup(0),id(0){} Q3PopupMenu* popup; int id; }; // ComboItem is only necessary for actions that are // in dropdown/exclusive actiongroups. The actiongroup // will clean this up struct ComboItem { ComboItem():combo(0), id(0) {} QComboBox *combo; int id; }; //just bindings to the Qt4.0 widgets struct Action4Item { Action4Item():widget(0){} QWidget* widget; static QAction *action; }; QList<Action4Item *> action4items; QList<MenuItem *> menuitems; QList<QToolButton *> toolbuttons; QList<ComboItem *> comboitems; enum Update { Icons = 1, Visibility = 2, State = 4, EverythingElse = 8 }; void update(uint upd = EverythingElse); QString menuText() const; QString toolTip() const; QString statusTip() const;};QAction *Q3ActionPrivate::Action4Item::action = 0;Q3ActionPrivate::Q3ActionPrivate(Q3Action *act) : icon(0),#ifndef QT_NO_ACCEL key(0), accel(0), accelid(0),#endif enabled(true), visible(true), toggleaction(false), on(false), forceDisabled(false), forceInvisible(false) , d_group(0), action(act){}Q3ActionPrivate::~Q3ActionPrivate(){ QList<QToolButton*>::Iterator ittb(toolbuttons.begin()); QToolButton *tb; while (ittb != toolbuttons.end()) { tb = *ittb; ++ittb; delete tb; } QList<Q3ActionPrivate::MenuItem*>::Iterator itmi(menuitems.begin()); Q3ActionPrivate::MenuItem* mi; while (itmi != menuitems.end()) { mi = *itmi; ++itmi; Q3PopupMenu* menu = mi->popup; if (menu->findItem(mi->id)) menu->removeItem(mi->id); } qDeleteAll(menuitems); QList<Q3ActionPrivate::Action4Item*>::Iterator itmi4(action4items.begin()); Q3ActionPrivate::Action4Item* mi4; while (itmi4 != action4items.end()) { mi4 = *itmi4; ++itmi4; mi4->widget->removeAction(mi4->action); } delete Q3ActionPrivate::Action4Item::action; Q3ActionPrivate::Action4Item::action = 0; qDeleteAll(action4items); QList<Q3ActionPrivate::ComboItem*>::Iterator itci(comboitems.begin()); Q3ActionPrivate::ComboItem* ci; while (itci != comboitems.end()) { ci = *itci; ++itci; QComboBox* combo = ci->combo; combo->clear(); Q3ActionGroup *group = ::qobject_cast<Q3ActionGroup*>(action->parent()); if (group) { QObjectList siblings = group->queryList("Q3Action"); for (int i = 0; i < siblings.size(); ++i) { Q3Action *sib = ::qobject_cast<Q3Action*>(siblings.at(i)); sib->removeFrom(combo); } for (int i = 0; i < siblings.size(); ++i) { Q3Action *sib = ::qobject_cast<Q3Action*>(siblings.at(i)); if (sib == action) continue; sib->addTo(combo); } } } qDeleteAll(comboitems);#ifndef QT_NO_ACCEL delete accel;#endif delete icon;}class Q3ActionGroupPrivate{public: uint exclusive: 1; uint dropdown: 1; QList<Q3Action*> actions; Q3Action* selected; Q3Action* separatorAction; struct MenuItem { MenuItem():popup(0),id(0){} Q3PopupMenu* popup; int id; }; struct Action4Item { Action4Item():widget(0){} QWidget* widget; static QAction *action; }; QList<Action4Item *> action4items; QList<QComboBox*> comboboxes; QList<QToolButton*> menubuttons; QList<MenuItem*> menuitems; QList<Q3PopupMenu*> popupmenus; void update(const Q3ActionGroup *);};QAction *Q3ActionGroupPrivate::Action4Item::action = 0;void Q3ActionPrivate::update(uint upd){ for (QList<MenuItem*>::Iterator it(menuitems.begin()); it != menuitems.end(); ++it) { MenuItem* mi = *it; QString t = menuText();#ifndef QT_NO_ACCEL if (key) t += QLatin1Char('\t') + (QString)QKeySequence(key);#endif if (upd & State) { mi->popup->setItemEnabled(mi->id, enabled); if (toggleaction) mi->popup->setItemChecked(mi->id, on); } if (upd & Visibility) mi->popup->setItemVisible(mi->id, visible); if (upd & Icons) if (icon) mi->popup->changeItem(mi->id, *icon, t); else mi->popup->changeItem(mi->id, QIcon(), t); if (upd & EverythingElse) { mi->popup->changeItem(mi->id, t); if (!whatsthis.isEmpty()) mi->popup->setWhatsThis(mi->id, whatsthis); if (toggleaction) { mi->popup->setCheckable(true); mi->popup->setItemChecked(mi->id, on); } } } if(QAction *act = Action4Item::action) { if (upd & Visibility) act->setVisible(visible); if (upd & Icons) { if (icon) act->setIcon(*icon); else act->setIcon(QIcon()); } if (upd & EverythingElse) { QString text = action->menuText();#ifndef QT_NO_ACCEL if (key) text += QLatin1Char('\t') + (QString)QKeySequence(key);#endif act->setText(text); act->setToolTip(statusTip()); act->setWhatsThis(whatsthis); } } for (QList<QToolButton*>::Iterator it2(toolbuttons.begin()); it2 != toolbuttons.end(); ++it2) { QToolButton* btn = *it2; if (upd & State) { btn->setEnabled(enabled); if (toggleaction) btn->setOn(on); } if (upd & Visibility) visible ? btn->show() : btn->hide(); if (upd & Icons) { if (icon) btn->setIconSet(*icon); else btn->setIconSet(QIcon()); } if (upd & EverythingElse) { btn->setToggleButton(toggleaction); if (!text.isEmpty()) btn->setTextLabel(text, false);#ifndef QT_NO_TOOLTIP btn->setToolTip(toolTip());#endif#ifndef QT_NO_STATUSTIP btn->setStatusTip(statusTip());#endif#ifndef QT_NO_WHATSTHIS QWhatsThis::remove(btn); if (!whatsthis.isEmpty()) QWhatsThis::add(btn, whatsthis);#endif } }#ifndef QT_NO_ACCEL if (accel) { accel->setEnabled(enabled && visible); if (!whatsthis.isEmpty()) accel->setWhatsThis(accelid, whatsthis); }#endif // Only used by actiongroup for (QList<ComboItem*>::Iterator it3(comboitems.begin()); it3 != comboitems.end(); ++it3) { ComboItem *ci = *it3; if (!ci->combo) return; if (ci->id == -1) { ci->id = ci->combo->count(); if (icon) ci->combo->insertItem(icon->pixmap(), text); else ci->combo->insertItem(text); } else { if (icon) ci->combo->changeItem(icon->pixmap(), text, ci->id); else ci->combo->changeItem(text, ci->id); } }}QString Q3ActionPrivate::menuText() const{ if (menutext.isNull()) { QString t(text); t.replace(QLatin1Char('&'), QLatin1String("&&")); return t; } return menutext;}QString Q3ActionPrivate::toolTip() const{ if (tooltip.isNull()) {#ifndef QT_NO_ACCEL if (accel) return text + QLatin1String(" (") + (QString)QKeySequence(accel->key(accelid)) + QLatin1Char(')');#endif return text; } return tooltip;}QString Q3ActionPrivate::statusTip() const{ if (statustip.isNull()) return toolTip(); return statustip;}/* internal: guesses a descriptive text from a menu text */static QString qt_stripMenuText(QString s){ s.remove(QLatin1String("...")); s.remove(QLatin1Char('&')); return s.trimmed();};/*! Constructs an action called \a name with parent \a parent. If \a parent is a Q3ActionGroup, the new action inserts itself into \a parent. For accelerators and status tips to work, \a parent must either be a widget, or an action group whose parent is a widget. \warning To prevent recursion, don't create an action as a child of a widget that the action is later added to.*/Q3Action::Q3Action(QObject* parent, const char* name) : QObject(parent, name){ d = new Q3ActionPrivate(this); init();}/*! Constructs an action called \a name with parent \a parent. If \a toggle is true the action will be a toggle action, otherwise it will be a command action. If \a parent is a Q3ActionGroup, the new action inserts itself into \a parent.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -