⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 q3accel.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/******************************************************************************** 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 "q3accel.h"#include "q3signal.h"#include "qapplication.h"#include "qwidget.h"#include "q3ptrlist.h"#include "qwhatsthis.h"#include "qpointer.h"#include "qstatusbar.h"#include "qdockwidget.h"#include "qevent.h"#include "qkeysequence.h"#include "private/qapplication_p.h"using namespace Qt;/*!    \class Q3Accel    \brief The Q3Accel class handles keyboard accelerator and shortcut keys.    \compat    A keyboard accelerator triggers an action when a certain key    combination is pressed. The accelerator handles all keyboard    activity for all the children of one top-level widget, so it is    not affected by the keyboard focus.    In most cases, you will not need to use this class directly. Use    the QAction class to create actions with accelerators that can be    used in both menus and toolbars. If you're only interested in    menus use Q3MenuData::insertItem() or Q3MenuData::setAccel() to make    accelerators for operations that are also available on menus. Many    widgets automatically generate accelerators, such as QAbstractButton,    QGroupBox, QLabel (with QLabel::setBuddy()), QMenuBar, and QTabBar.    Example:    \code        QPushButton p("&Exit", parent); // automatic shortcut Alt+E        Q3PopupMenu *fileMenu = new fileMenu(parent);        fileMenu->insertItem("Undo", parent, SLOT(undo()),                             Qt::CTRL + Qt::Key_Z);    \endcode    A Q3Accel contains a list of accelerator items that can be    manipulated using insertItem(), removeItem(), clear(), key() and    findKey().    Each accelerator item consists of an identifier and a \l    QKeySequence. A single key sequence consists of a keyboard code    combined with modifiers (Qt::SHIFT, Qt::CTRL, Qt::ALT, or    Qt::UNICODE_ACCEL). For example, Qt::CTRL + Qt::Key_P could be a shortcut    for printing a document. As an alternative, use Qt::UNICODE_ACCEL with the    unicode code point of the character. For example, Qt::UNICODE_ACCEL    + 'A' gives the same accelerator as Qt::Key_A.    When an accelerator key is pressed, the accelerator sends out the    signal activated() with a number that identifies this particular    accelerator item. Accelerator items can also be individually    connected, so that two different keys will activate two different    slots (see connectItem() and disconnectItem()).    The activated() signal is \e not emitted when two or more    accelerators match the same key.  Instead, the first matching    accelerator sends out the activatedAmbiguously() signal. By    pressing the key multiple times, users can navigate between all    matching accelerators. Some standard controls like QPushButton and    QCheckBox connect the activatedAmbiguously() signal to the    harmless setFocus() slot, whereas activated() is connected to a    slot invoking the button's action. Most controls, like QLabel and    QTabBar, treat activated() and activatedAmbiguously() as    equivalent.    Use setEnabled() to enable or disable all the items in an    accelerator, or setItemEnabled() to enable or disable individual    items. An item is active only when both the Q3Accel and the item    itself are enabled.    The function setWhatsThis() specifies a help text that appears    when the user presses an accelerator key in What's This mode.    The accelerator will be deleted when \e parent is deleted,    and will consume relevant key events until then.    Please note that the accelerator    \code        accelerator->insertItem(QKeySequence("M"));    \endcode    can be triggered with both the 'M' key, and with Shift+M,    unless a second accelerator is defined for the Shift+M    combination.    Example:    \code        Q3Accel *a = new Q3Accel(myWindow);        a->connectItem(a->insertItem(Qt::CTRL + Qt::Key_P),                       myWindow, SLOT(printDoc()));    \endcode    \sa QKeyEvent QWidget::keyPressEvent()    QAbstractButton::setAccel() QLabel::setBuddy() QKeySequence*/struct Q3AccelItem {                                // internal accelerator item    Q3AccelItem(const QKeySequence &k, int i)        { key=k; id=i; enabled=true; signal=0; }   ~Q3AccelItem()               { delete signal; }    int id;    QKeySequence key;    bool enabled;    Q3Signal *signal;    QString whatsthis;};typedef Q3PtrList<Q3AccelItem> Q3AccelList; // internal accelerator listclass Q3AccelPrivate {public:    Q3AccelPrivate(Q3Accel* p);    ~Q3AccelPrivate();    Q3AccelList aitems;    bool enabled;    QPointer<QWidget> watch;    bool ignorewhatsthis;    Q3Accel* parent;    void activate(Q3AccelItem* item);    void activateAmbiguously(Q3AccelItem* item);};class Q3AccelManager {public:    static Q3AccelManager* self() { return self_ptr ? self_ptr : new Q3AccelManager; }    void registerAccel(Q3AccelPrivate* a) { accels.append(a); }    void unregisterAccel(Q3AccelPrivate* a) { accels.removeRef(a); if (accels.isEmpty()) delete this; }    bool tryAccelEvent(QWidget* w, QKeyEvent* e);    bool dispatchAccelEvent(QWidget* w, QKeyEvent* e);    bool tryComposeUnicode(QWidget* w, QKeyEvent* e);private:    Q3AccelManager()        : currentState(QKeySequence::NoMatch), clash(-1), metaComposeUnicode(false),composedUnicode(0)        { setFuncPtr(); self_ptr = this; }    ~Q3AccelManager() { self_ptr = 0; }    void setFuncPtr();    bool correctSubWindow(QWidget *w, Q3AccelPrivate* d);    QKeySequence::SequenceMatch match(QKeyEvent* e, Q3AccelItem* item, QKeySequence& temp);    int translateModifiers(ButtonState state);    Q3PtrList<Q3AccelPrivate> accels;    static Q3AccelManager* self_ptr;    QKeySequence::SequenceMatch currentState;    QKeySequence intermediate;    int clash;    bool metaComposeUnicode;    int composedUnicode;};Q3AccelManager* Q3AccelManager::self_ptr = 0;bool Q_COMPAT_EXPORT qt_tryAccelEvent(QWidget* w, QKeyEvent*  e){    return Q3AccelManager::self()->tryAccelEvent(w, e);}bool Q_COMPAT_EXPORT qt_dispatchAccelEvent(QWidget* w, QKeyEvent*  e){    return Q3AccelManager::self()->dispatchAccelEvent(w, e);}bool Q_COMPAT_EXPORT qt_tryComposeUnicode(QWidget* w, QKeyEvent*  e){    return Q3AccelManager::self()->tryComposeUnicode(w, e);}void Q3AccelManager::setFuncPtr() {    if (qApp->d_func()->qt_compat_used)        return;    QApplicationPrivate *data = static_cast<QApplicationPrivate*>(qApp->d_ptr);    data->qt_tryAccelEvent = qt_tryAccelEvent;    data->qt_tryComposeUnicode = qt_tryComposeUnicode;    data->qt_dispatchAccelEvent = qt_dispatchAccelEvent;    data->qt_compat_used = true;}#ifdef Q_WS_MACstatic bool qt_accel_no_shortcuts = true;#elsestatic bool qt_accel_no_shortcuts = false;#endifvoid Q_COMPAT_EXPORT qt_set_accel_auto_shortcuts(bool b) { qt_accel_no_shortcuts = b; }/*    \internal    Returns true if the accel is in the current subwindow, else false.*/bool Q3AccelManager::correctSubWindow(QWidget* w, Q3AccelPrivate* d) {#if !defined (Q_OS_MACX)     if (!d->watch || !d->watch->isVisible() || !d->watch->isEnabled())#else    if (!d->watch || (!d->watch->isVisible() && !d->watch->inherits("QMenuBar")) || !d->watch->isEnabled())#endif        return false;    QWidget* tlw = w->window();    QWidget* wtlw = d->watch->window();    /* if we live in a floating dock window, keep our parent's     * accelerators working */#ifndef QT_NO_MAINWINDOW    if ((tlw->windowType() == Qt::Dialog) && tlw->parentWidget() && ::qobject_cast<QDockWidget*>(tlw))        return tlw->parentWidget()->window() == wtlw;    if (wtlw  != tlw)        return false;#endif    /* if we live in a MDI subwindow, ignore the event if we are       not the active document window */    QWidget* sw = d->watch;    while (sw && sw->windowType() != Qt::SubWindow)        sw = sw->parentWidget(true);    if (sw)  { // we are in a subwindow indeed        QWidget* fw = w;        while (fw && fw != sw)            fw = fw->parentWidget(true);        if (fw != sw) // focus widget not in our subwindow            return false;    }    return true;}inline int Q3AccelManager::translateModifiers(ButtonState state){    int result = 0;    if (state & ShiftButton)        result |= SHIFT;    if (state & ControlButton)        result |= CTRL;    if (state & MetaButton)        result |= META;    if (state & AltButton)        result |= ALT;    return result;}/*    \internal    Matches the current intermediate key sequence + the latest    keyevent, with and AccelItem. Returns Identical,    PartialMatch or NoMatch, and fills \a temp with the    resulting key sequence.*/QKeySequence::SequenceMatch Q3AccelManager::match(QKeyEvent *e, Q3AccelItem* item, QKeySequence& temp){    QKeySequence::SequenceMatch result = QKeySequence::NoMatch;    int index = intermediate.count();    temp = intermediate;    int modifier = translateModifiers(e->state());    if (e->key() && e->key() != Key_unknown) {        int key = e->key()  | modifier;        if (e->key() == Key_BackTab) {            /*            In QApplication, we map shift+tab to shift+backtab.            This code here reverts the mapping in a way that keeps            backtab and shift+tab accelerators working, in that            order, meaning backtab has priority.*/            key &= ~SHIFT;            temp.setKey(key, index);            if (QKeySequence::NoMatch != (result = temp.matches(item->key)))                return result;            if (e->state() & ShiftButton)                key |= SHIFT;            key = Key_Tab | (key & MODIFIER_MASK);            temp.setKey(key, index);            if (QKeySequence::NoMatch != (result = temp.matches(item->key)))                return result;        } else {            temp.setKey(key, index);            if (QKeySequence::NoMatch != (result = temp.matches(item->key)))                return result;        }        if (key == Key_BackTab) {            if (e->state() & ShiftButton)                key |= SHIFT;            temp.setKey(key, index);            if (QKeySequence::NoMatch != (result = temp.matches(item->key)))                return result;        }    }    if (!e->text().isEmpty()) {        temp.setKey((int)e->text()[0].unicode() | UNICODE_ACCEL | modifier, index);        result = temp.matches(item->key);    }    return result;}bool Q3AccelManager::tryAccelEvent(QWidget* w, QKeyEvent* e)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -