| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: qkeysequence.cpp 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Implementation of QKeySequence class
|
|---|
| 5 | **
|
|---|
| 6 | ** Created : 0108007
|
|---|
| 7 | **
|
|---|
| 8 | ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
|
|---|
| 9 | **
|
|---|
| 10 | ** This file is part of the kernel module of the Qt GUI Toolkit.
|
|---|
| 11 | **
|
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License
|
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file.
|
|---|
| 15 | **
|
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the
|
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 19 | ** packaging of this file.
|
|---|
| 20 | **
|
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License
|
|---|
| 23 | ** Agreement provided with the Software.
|
|---|
| 24 | **
|
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 27 | **
|
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
|---|
| 29 | ** information about Qt Commercial License Agreements.
|
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
|---|
| 32 | **
|
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
|---|
| 34 | ** not clear to you.
|
|---|
| 35 | **
|
|---|
| 36 | **********************************************************************/
|
|---|
| 37 |
|
|---|
| 38 | #include "qkeysequence.h"
|
|---|
| 39 |
|
|---|
| 40 | #ifndef QT_NO_ACCEL
|
|---|
| 41 |
|
|---|
| 42 | #include "qaccel.h"
|
|---|
| 43 | #include "qshared.h"
|
|---|
| 44 | #include "qvaluelist.h"
|
|---|
| 45 | #ifndef QT_NO_REGEXP
|
|---|
| 46 | # include "qregexp.h"
|
|---|
| 47 | #endif
|
|---|
| 48 |
|
|---|
| 49 | #ifdef Q_WS_MAC
|
|---|
| 50 | #define QMAC_CTRL (QString(QChar(0x2318)))
|
|---|
| 51 | #define QMAC_META (QString(QChar(0x2303)))
|
|---|
| 52 | #define QMAC_ALT (QString(QChar(0x2325)))
|
|---|
| 53 | #define QMAC_SHIFT (QString(QChar(0x21E7)))
|
|---|
| 54 | #endif
|
|---|
| 55 |
|
|---|
| 56 | /*!
|
|---|
| 57 | \class QKeySequence qkeysequence.h
|
|---|
| 58 | \brief The QKeySequence class encapsulates a key sequence as used
|
|---|
| 59 | by accelerators.
|
|---|
| 60 |
|
|---|
| 61 | \ingroup misc
|
|---|
| 62 |
|
|---|
| 63 | A key sequence consists of up to four keyboard codes, each
|
|---|
| 64 | optionally combined with modifiers, e.g. \c SHIFT, \c CTRL, \c
|
|---|
| 65 | ALT, \c META, or \c UNICODE_ACCEL. For example, \c{CTRL + Key_P}
|
|---|
| 66 | might be a sequence used as a shortcut for printing a document.
|
|---|
| 67 | The key codes are listed in \c{qnamespace.h}. As an alternative,
|
|---|
| 68 | use \c UNICODE_ACCEL with the unicode code point of the character.
|
|---|
| 69 | For example, \c{UNICODE_ACCEL + 'A'} gives the same key sequence
|
|---|
| 70 | as \c Key_A.
|
|---|
| 71 |
|
|---|
| 72 | Key sequences can be constructed either from an integer key code,
|
|---|
| 73 | or from a human readable translatable string such as
|
|---|
| 74 | "Ctrl+X,Alt+Space". A key sequence can be cast to a QString to
|
|---|
| 75 | obtain a human readable translated version of the sequence.
|
|---|
| 76 | Translations are done in the "QAccel" context.
|
|---|
| 77 |
|
|---|
| 78 | \sa QAccel
|
|---|
| 79 | */
|
|---|
| 80 |
|
|---|
| 81 | /*!
|
|---|
| 82 | \enum Qt::SequenceMatch
|
|---|
| 83 |
|
|---|
| 84 | \value NoMatch Sequences have nothing in common
|
|---|
| 85 | \value PartialMatch Sequences match partially, but are not complete
|
|---|
| 86 | \value Identical Sequences do not differ
|
|---|
| 87 | */
|
|---|
| 88 |
|
|---|
| 89 | static struct {
|
|---|
| 90 | int key;
|
|---|
| 91 | const char* name;
|
|---|
| 92 | } keyname[] = {
|
|---|
| 93 | { Qt::Key_Space, QT_TRANSLATE_NOOP( "QAccel", "Space" ) },
|
|---|
| 94 | { Qt::Key_Escape, QT_TRANSLATE_NOOP( "QAccel", "Esc" ) },
|
|---|
| 95 | { Qt::Key_Tab, QT_TRANSLATE_NOOP( "QAccel", "Tab" ) },
|
|---|
| 96 | { Qt::Key_Backtab, QT_TRANSLATE_NOOP( "QAccel", "Backtab" ) },
|
|---|
| 97 | { Qt::Key_Backspace, QT_TRANSLATE_NOOP( "QAccel", "Backspace" ) },
|
|---|
| 98 | { Qt::Key_Return, QT_TRANSLATE_NOOP( "QAccel", "Return" ) },
|
|---|
| 99 | { Qt::Key_Enter, QT_TRANSLATE_NOOP( "QAccel", "Enter" ) },
|
|---|
| 100 | { Qt::Key_Insert, QT_TRANSLATE_NOOP( "QAccel", "Ins" ) },
|
|---|
| 101 | { Qt::Key_Delete, QT_TRANSLATE_NOOP( "QAccel", "Del" ) },
|
|---|
| 102 | { Qt::Key_Pause, QT_TRANSLATE_NOOP( "QAccel", "Pause" ) },
|
|---|
| 103 | { Qt::Key_Print, QT_TRANSLATE_NOOP( "QAccel", "Print" ) },
|
|---|
| 104 | { Qt::Key_SysReq, QT_TRANSLATE_NOOP( "QAccel", "SysReq" ) },
|
|---|
| 105 | { Qt::Key_Home, QT_TRANSLATE_NOOP( "QAccel", "Home" ) },
|
|---|
| 106 | { Qt::Key_End, QT_TRANSLATE_NOOP( "QAccel", "End" ) },
|
|---|
| 107 | { Qt::Key_Left, QT_TRANSLATE_NOOP( "QAccel", "Left" ) },
|
|---|
| 108 | { Qt::Key_Up, QT_TRANSLATE_NOOP( "QAccel", "Up" ) },
|
|---|
| 109 | { Qt::Key_Right, QT_TRANSLATE_NOOP( "QAccel", "Right" ) },
|
|---|
| 110 | { Qt::Key_Down, QT_TRANSLATE_NOOP( "QAccel", "Down" ) },
|
|---|
| 111 | { Qt::Key_Prior, QT_TRANSLATE_NOOP( "QAccel", "PgUp" ) },
|
|---|
| 112 | { Qt::Key_Next, QT_TRANSLATE_NOOP( "QAccel", "PgDown" ) },
|
|---|
| 113 | { Qt::Key_CapsLock, QT_TRANSLATE_NOOP( "QAccel", "CapsLock" ) },
|
|---|
| 114 | { Qt::Key_NumLock, QT_TRANSLATE_NOOP( "QAccel", "NumLock" ) },
|
|---|
| 115 | { Qt::Key_ScrollLock, QT_TRANSLATE_NOOP( "QAccel", "ScrollLock" ) },
|
|---|
| 116 | { Qt::Key_Menu, QT_TRANSLATE_NOOP( "QAccel", "Menu" ) },
|
|---|
| 117 | { Qt::Key_Help, QT_TRANSLATE_NOOP( "QAccel", "Help" ) },
|
|---|
| 118 |
|
|---|
| 119 | // Multimedia keys
|
|---|
| 120 | { Qt::Key_Back, QT_TRANSLATE_NOOP( "QAccel", "Back" ) },
|
|---|
| 121 | { Qt::Key_Forward, QT_TRANSLATE_NOOP( "QAccel", "Forward" ) },
|
|---|
| 122 | { Qt::Key_Stop, QT_TRANSLATE_NOOP( "QAccel", "Stop" ) },
|
|---|
| 123 | { Qt::Key_Refresh, QT_TRANSLATE_NOOP( "QAccel", "Refresh" ) },
|
|---|
| 124 | { Qt::Key_VolumeDown, QT_TRANSLATE_NOOP( "QAccel", "Volume Down" ) },
|
|---|
| 125 | { Qt::Key_VolumeMute, QT_TRANSLATE_NOOP( "QAccel", "Volume Mute" ) },
|
|---|
| 126 | { Qt::Key_VolumeUp, QT_TRANSLATE_NOOP( "QAccel", "Volume Up" ) },
|
|---|
| 127 | { Qt::Key_BassBoost, QT_TRANSLATE_NOOP( "QAccel", "Bass Boost" ) },
|
|---|
| 128 | { Qt::Key_BassUp, QT_TRANSLATE_NOOP( "QAccel", "Bass Up" ) },
|
|---|
| 129 | { Qt::Key_BassDown, QT_TRANSLATE_NOOP( "QAccel", "Bass Down" ) },
|
|---|
| 130 | { Qt::Key_TrebleUp, QT_TRANSLATE_NOOP( "QAccel", "Treble Up" ) },
|
|---|
| 131 | { Qt::Key_TrebleDown, QT_TRANSLATE_NOOP( "QAccel", "Treble Down" ) },
|
|---|
| 132 | { Qt::Key_MediaPlay, QT_TRANSLATE_NOOP( "QAccel", "Media Play" ) },
|
|---|
| 133 | { Qt::Key_MediaStop, QT_TRANSLATE_NOOP( "QAccel", "Media Stop" ) },
|
|---|
| 134 | { Qt::Key_MediaPrev, QT_TRANSLATE_NOOP( "QAccel", "Media Previous" ) },
|
|---|
| 135 | { Qt::Key_MediaNext, QT_TRANSLATE_NOOP( "QAccel", "Media Next" ) },
|
|---|
| 136 | { Qt::Key_MediaRecord, QT_TRANSLATE_NOOP( "QAccel", "Media Record" ) },
|
|---|
| 137 | { Qt::Key_HomePage, QT_TRANSLATE_NOOP( "QAccel", "Home" ) },
|
|---|
| 138 | { Qt::Key_Favorites, QT_TRANSLATE_NOOP( "QAccel", "Favorites" ) },
|
|---|
| 139 | { Qt::Key_Search, QT_TRANSLATE_NOOP( "QAccel", "Search" ) },
|
|---|
| 140 | { Qt::Key_Standby, QT_TRANSLATE_NOOP( "QAccel", "Standby" ) },
|
|---|
| 141 | { Qt::Key_OpenUrl, QT_TRANSLATE_NOOP( "QAccel", "Open URL" ) },
|
|---|
| 142 | { Qt::Key_LaunchMail, QT_TRANSLATE_NOOP( "QAccel", "Launch Mail" ) },
|
|---|
| 143 | { Qt::Key_LaunchMedia, QT_TRANSLATE_NOOP( "QAccel", "Launch Media" ) },
|
|---|
| 144 | { Qt::Key_Launch0, QT_TRANSLATE_NOOP( "QAccel", "Launch (0)" ) },
|
|---|
| 145 | { Qt::Key_Launch1, QT_TRANSLATE_NOOP( "QAccel", "Launch (1)" ) },
|
|---|
| 146 | { Qt::Key_Launch2, QT_TRANSLATE_NOOP( "QAccel", "Launch (2)" ) },
|
|---|
| 147 | { Qt::Key_Launch3, QT_TRANSLATE_NOOP( "QAccel", "Launch (3)" ) },
|
|---|
| 148 | { Qt::Key_Launch4, QT_TRANSLATE_NOOP( "QAccel", "Launch (4)" ) },
|
|---|
| 149 | { Qt::Key_Launch5, QT_TRANSLATE_NOOP( "QAccel", "Launch (5)" ) },
|
|---|
| 150 | { Qt::Key_Launch6, QT_TRANSLATE_NOOP( "QAccel", "Launch (6)" ) },
|
|---|
| 151 | { Qt::Key_Launch7, QT_TRANSLATE_NOOP( "QAccel", "Launch (7)" ) },
|
|---|
| 152 | { Qt::Key_Launch8, QT_TRANSLATE_NOOP( "QAccel", "Launch (8)" ) },
|
|---|
| 153 | { Qt::Key_Launch9, QT_TRANSLATE_NOOP( "QAccel", "Launch (9)" ) },
|
|---|
| 154 | { Qt::Key_LaunchA, QT_TRANSLATE_NOOP( "QAccel", "Launch (A)" ) },
|
|---|
| 155 | { Qt::Key_LaunchB, QT_TRANSLATE_NOOP( "QAccel", "Launch (B)" ) },
|
|---|
| 156 | { Qt::Key_LaunchC, QT_TRANSLATE_NOOP( "QAccel", "Launch (C)" ) },
|
|---|
| 157 | { Qt::Key_LaunchD, QT_TRANSLATE_NOOP( "QAccel", "Launch (D)" ) },
|
|---|
| 158 | { Qt::Key_LaunchE, QT_TRANSLATE_NOOP( "QAccel", "Launch (E)" ) },
|
|---|
| 159 | { Qt::Key_LaunchF, QT_TRANSLATE_NOOP( "QAccel", "Launch (F)" ) },
|
|---|
| 160 |
|
|---|
| 161 | // --------------------------------------------------------------
|
|---|
| 162 | // More consistent namings
|
|---|
| 163 | { Qt::Key_Print, QT_TRANSLATE_NOOP( "QAccel", "Print Screen" ) },
|
|---|
| 164 | { Qt::Key_Prior, QT_TRANSLATE_NOOP( "QAccel", "Page Up" ) },
|
|---|
| 165 | { Qt::Key_Next, QT_TRANSLATE_NOOP( "QAccel", "Page Down" ) },
|
|---|
| 166 | { Qt::Key_CapsLock, QT_TRANSLATE_NOOP( "QAccel", "Caps Lock" ) },
|
|---|
| 167 | { Qt::Key_NumLock, QT_TRANSLATE_NOOP( "QAccel", "Num Lock" ) },
|
|---|
| 168 | { Qt::Key_NumLock, QT_TRANSLATE_NOOP( "QAccel", "Number Lock" ) },
|
|---|
| 169 | { Qt::Key_ScrollLock, QT_TRANSLATE_NOOP( "QAccel", "Scroll Lock" ) },
|
|---|
| 170 | { Qt::Key_Insert, QT_TRANSLATE_NOOP( "QAccel", "Insert" ) },
|
|---|
| 171 | { Qt::Key_Delete, QT_TRANSLATE_NOOP( "QAccel", "Delete" ) },
|
|---|
| 172 | { Qt::Key_Escape, QT_TRANSLATE_NOOP( "QAccel", "Escape" ) },
|
|---|
| 173 | { Qt::Key_SysReq, QT_TRANSLATE_NOOP( "QAccel", "System Request" ) },
|
|---|
| 174 |
|
|---|
| 175 | { 0, 0 }
|
|---|
| 176 | };
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 | class QKeySequencePrivate : public QShared
|
|---|
| 180 | {
|
|---|
| 181 | public:
|
|---|
| 182 | inline QKeySequencePrivate()
|
|---|
| 183 | {
|
|---|
| 184 | key[0] = key[1] = key[2] = key[3] = 0;
|
|---|
| 185 | }
|
|---|
| 186 | inline QKeySequencePrivate( QKeySequencePrivate *copy )
|
|---|
| 187 | {
|
|---|
| 188 | key[0] = copy->key[0];
|
|---|
| 189 | key[1] = copy->key[1];
|
|---|
| 190 | key[2] = copy->key[2];
|
|---|
| 191 | key[3] = copy->key[3];
|
|---|
| 192 | }
|
|---|
| 193 | int key[4];
|
|---|
| 194 | };
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 | /*!
|
|---|
| 198 | Constructs an empty key sequence.
|
|---|
| 199 | */
|
|---|
| 200 | QKeySequence::QKeySequence()
|
|---|
| 201 | {
|
|---|
| 202 | d = new QKeySequencePrivate();
|
|---|
| 203 | Q_CHECK_PTR( d );
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /*!
|
|---|
| 207 | Creates a key sequence from the string \a key. For example
|
|---|
| 208 | "Ctrl+O" gives CTRL+UNICODE_ACCEL+'O'. The strings "Ctrl",
|
|---|
| 209 | "Shift", "Alt" and "Meta" are recognized, as well as their
|
|---|
| 210 | translated equivalents in the "QAccel" context (using
|
|---|
| 211 | QObject::tr()).
|
|---|
| 212 |
|
|---|
| 213 | Multiple key codes (up to four) may be entered by separating them
|
|---|
| 214 | with commas, e.g. "Alt+X,Ctrl+S,Q".
|
|---|
| 215 |
|
|---|
| 216 | This contructor is typically used with \link QObject::tr() tr
|
|---|
| 217 | \endlink(), so that accelerator keys can be replaced in
|
|---|
| 218 | translations:
|
|---|
| 219 |
|
|---|
| 220 | \code
|
|---|
| 221 | QPopupMenu *file = new QPopupMenu( this );
|
|---|
| 222 | file->insertItem( tr("&Open..."), this, SLOT(open()),
|
|---|
| 223 | QKeySequence( tr("Ctrl+O", "File|Open") ) );
|
|---|
| 224 | \endcode
|
|---|
| 225 |
|
|---|
| 226 | Note the \c "File|Open" translator comment. It is by no means
|
|---|
| 227 | necessary, but it provides some context for the human translator.
|
|---|
| 228 | */
|
|---|
| 229 | QKeySequence::QKeySequence( const QString& key )
|
|---|
| 230 | {
|
|---|
| 231 | d = new QKeySequencePrivate();
|
|---|
| 232 | Q_CHECK_PTR( d );
|
|---|
| 233 | assign( key );
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | // ### BCI: Merge with constructor below for 4.0
|
|---|
| 238 | /*!
|
|---|
| 239 | Constructs a key sequence that has a single \a key.
|
|---|
| 240 |
|
|---|
| 241 | The key codes are listed in \c{qnamespace.h} and can be
|
|---|
| 242 | combined with modifiers, e.g. with \c SHIFT, \c CTRL, \c
|
|---|
| 243 | ALT, \c META or \c UNICODE_ACCEL.
|
|---|
| 244 | */
|
|---|
| 245 | QKeySequence::QKeySequence( int key )
|
|---|
| 246 | {
|
|---|
| 247 | d = new QKeySequencePrivate();
|
|---|
| 248 | Q_CHECK_PTR( d );
|
|---|
| 249 | d->key[0] = key;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | /*!
|
|---|
| 253 | Constructs a key sequence with up to 4 keys \a k1, \a k2,
|
|---|
| 254 | \a k3 and \a k4.
|
|---|
| 255 |
|
|---|
| 256 | The key codes are listed in \c{qnamespace.h} and can be
|
|---|
| 257 | combined with modifiers, e.g. with \c SHIFT, \c CTRL, \c
|
|---|
| 258 | ALT, \c META or \c UNICODE_ACCEL.
|
|---|
| 259 | */
|
|---|
| 260 | QKeySequence::QKeySequence( int k1, int k2, int k3, int k4 )
|
|---|
| 261 | {
|
|---|
| 262 | d = new QKeySequencePrivate();
|
|---|
| 263 | Q_CHECK_PTR( d );
|
|---|
| 264 | d->key[0] = k1;
|
|---|
| 265 | d->key[1] = k2;
|
|---|
| 266 | d->key[2] = k3;
|
|---|
| 267 | d->key[3] = k4;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | /*!
|
|---|
| 271 | Copy constructor. Makes a copy of \a keysequence.
|
|---|
| 272 | */
|
|---|
| 273 | QKeySequence::QKeySequence( const QKeySequence& keysequence )
|
|---|
| 274 | : d( keysequence.d )
|
|---|
| 275 | {
|
|---|
| 276 | d->ref();
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 | /*!
|
|---|
| 281 | Destroys the key sequence.
|
|---|
| 282 | */
|
|---|
| 283 | QKeySequence::~QKeySequence()
|
|---|
| 284 | {
|
|---|
| 285 | if ( d->deref() )
|
|---|
| 286 | delete d;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /*!
|
|---|
| 290 | \internal
|
|---|
| 291 | KeySequences should never be modified, but rather just created.
|
|---|
| 292 | Internally though we do need to modify to keep pace in event
|
|---|
| 293 | delivery.
|
|---|
| 294 | */
|
|---|
| 295 |
|
|---|
| 296 | void QKeySequence::setKey( int key, int index )
|
|---|
| 297 | {
|
|---|
| 298 | #ifdef QT_CHECK_STATE
|
|---|
| 299 | if ( 0 > index && 4 < index ) {
|
|---|
| 300 | qWarning( "QKeySequence::setKey: index %u out of range", index );
|
|---|
| 301 | return;
|
|---|
| 302 | }
|
|---|
| 303 | #endif // QT_CHECK_STATE
|
|---|
| 304 |
|
|---|
| 305 | if ( 1 < d->count ) {
|
|---|
| 306 | QKeySequencePrivate *newd = new QKeySequencePrivate( d );
|
|---|
| 307 | d->deref();
|
|---|
| 308 | d = newd;
|
|---|
| 309 | }
|
|---|
| 310 | d->key[index] = key;
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | /*!
|
|---|
| 314 | Returns the number of keys in the key sequence.
|
|---|
| 315 | The maximum is 4.
|
|---|
| 316 | */
|
|---|
| 317 | uint QKeySequence::count() const
|
|---|
| 318 | {
|
|---|
| 319 | if ( ! d->key[0] )
|
|---|
| 320 | return 0;
|
|---|
| 321 | if ( ! d->key[1] )
|
|---|
| 322 | return 1;
|
|---|
| 323 | if ( ! d->key[2] )
|
|---|
| 324 | return 2;
|
|---|
| 325 | if ( ! d->key[3] )
|
|---|
| 326 | return 3;
|
|---|
| 327 | return 4;
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 | /*!
|
|---|
| 332 | Returns TRUE if the key sequence is empty; otherwise returns
|
|---|
| 333 | FALSE.
|
|---|
| 334 | */
|
|---|
| 335 | bool QKeySequence::isEmpty() const
|
|---|
| 336 | {
|
|---|
| 337 | return !d->key[0];
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 | /*!
|
|---|
| 342 | Adds the string \a keyseq to the key sequence. \a keyseq may
|
|---|
| 343 | contain up to four key codes, provided they are seperated by a
|
|---|
| 344 | comma, e.g. "Alt+X,Ctrl+S,Z"). Returns the number of key codes
|
|---|
| 345 | added.
|
|---|
| 346 | */
|
|---|
| 347 | int QKeySequence::assign( QString keyseq )
|
|---|
| 348 | {
|
|---|
| 349 | QString part;
|
|---|
| 350 | int n = 0;
|
|---|
| 351 | int p = 0, diff = 0;
|
|---|
| 352 |
|
|---|
| 353 | // Run through the whole string, but stop
|
|---|
| 354 | // if we have 4 keys before the end.
|
|---|
| 355 | while ( keyseq.length() && n < 4 ) {
|
|---|
| 356 | // We MUST use something to seperate each sequence, and space
|
|---|
| 357 | // does not cut it, since some of the key names have space
|
|---|
| 358 | // in them.. (Let's hope no one translate with a comma in it:)
|
|---|
| 359 | p = keyseq.find( ',' );
|
|---|
| 360 | if ( -1 != p ) {
|
|---|
| 361 | if ( ',' == keyseq[p+1] ) // e.g. 'Ctrl+,, Shift+,,'
|
|---|
| 362 | p++;
|
|---|
| 363 | if ( ' ' == keyseq[p+1] ) { // Space after comma
|
|---|
| 364 | diff = 1;
|
|---|
| 365 | p++;
|
|---|
| 366 | } else {
|
|---|
| 367 | diff = 0;
|
|---|
| 368 | }
|
|---|
| 369 | }
|
|---|
| 370 | part = keyseq.left( -1==p?keyseq.length():p-diff );
|
|---|
| 371 | keyseq = keyseq.right( -1==p?0:keyseq.length() - ( p + 1 ) );
|
|---|
| 372 | d->key[n] = decodeString( part );
|
|---|
| 373 | n++;
|
|---|
| 374 | }
|
|---|
| 375 | return n;
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | struct ModifKeyName {
|
|---|
| 379 | ModifKeyName() { }
|
|---|
| 380 | ModifKeyName(int q, QString n) : qt_key(q), name(n) { }
|
|---|
| 381 | int qt_key;
|
|---|
| 382 | QString name;
|
|---|
| 383 | };
|
|---|
| 384 |
|
|---|
| 385 | /*!
|
|---|
| 386 | Constructs a single key from the string \str.
|
|---|
| 387 | */
|
|---|
| 388 | int QKeySequence::decodeString( const QString& str )
|
|---|
| 389 | {
|
|---|
| 390 | int ret = 0;
|
|---|
| 391 | QString accel = str;
|
|---|
| 392 |
|
|---|
| 393 | QValueList<ModifKeyName> modifs;
|
|---|
| 394 | #ifdef QMAC_CTRL
|
|---|
| 395 | modifs << ModifKeyName( CTRL, QMAC_CTRL );
|
|---|
| 396 | #endif
|
|---|
| 397 | #ifdef QMAC_ALT
|
|---|
| 398 | modifs << ModifKeyName( ALT, QMAC_ALT );
|
|---|
| 399 | #endif
|
|---|
| 400 | #ifdef QMAC_META
|
|---|
| 401 | modifs << ModifKeyName( META, QMAC_META );
|
|---|
| 402 | #endif
|
|---|
| 403 | #ifdef QMAC_SHIFT
|
|---|
| 404 | modifs << ModifKeyName( SHIFT, QMAC_SHIFT );
|
|---|
| 405 | #endif
|
|---|
| 406 | modifs << ModifKeyName( CTRL, "ctrl+" ) << ModifKeyName( CTRL, QAccel::tr("Ctrl").lower().append('+') );
|
|---|
| 407 | modifs << ModifKeyName( SHIFT, "shift+" ) << ModifKeyName( SHIFT, QAccel::tr("Shift").lower().append('+') );
|
|---|
| 408 | modifs << ModifKeyName( ALT, "alt+" ) << ModifKeyName( ALT, QAccel::tr("Alt").lower().append('+') );
|
|---|
| 409 | modifs << ModifKeyName( META, "meta+" ) << ModifKeyName( ALT, QAccel::tr("Meta").lower().append('+') );
|
|---|
| 410 | QString sl = accel.lower();
|
|---|
| 411 | for( QValueList<ModifKeyName>::iterator it = modifs.begin(); it != modifs.end(); ++it ) {
|
|---|
| 412 | if ( sl.contains( (*it).name ) ) {
|
|---|
| 413 | ret |= (*it).qt_key;
|
|---|
| 414 | #ifndef QT_NO_REGEXP
|
|---|
| 415 | accel.remove( QRegExp(QRegExp::escape((*it).name), FALSE) );
|
|---|
| 416 | #else
|
|---|
| 417 | accel.remove( (*it).name );
|
|---|
| 418 | #endif
|
|---|
| 419 | sl = accel.lower();
|
|---|
| 420 | }
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | int p = accel.findRev( '+', str.length() - 2 ); // -2 so that Ctrl++ works
|
|---|
| 424 | if( p > 0 )
|
|---|
| 425 | accel = accel.mid( p + 1 );
|
|---|
| 426 |
|
|---|
| 427 | int fnum = 0;
|
|---|
| 428 | if ( accel.length() == 1 ) {
|
|---|
| 429 | char ltr = accel[0].upper().latin1();
|
|---|
| 430 | // We can only upper A-Z without problems.
|
|---|
| 431 | if ( ltr < (char)Key_A || ltr > (char)Key_Z )
|
|---|
| 432 | ret |= accel[0].unicode();
|
|---|
| 433 | else
|
|---|
| 434 | ret |= accel[0].upper().unicode();
|
|---|
| 435 | ret |= UNICODE_ACCEL;
|
|---|
| 436 | } else if ( accel[0] == 'F' && (fnum = accel.mid(1).toInt()) ) {
|
|---|
| 437 | ret |= Key_F1 + fnum - 1;
|
|---|
| 438 | } else {
|
|---|
| 439 | // Check through translation table for the correct key name
|
|---|
| 440 | // ...or fall back on english table.
|
|---|
| 441 | bool found = FALSE;
|
|---|
| 442 | for ( int tran = 0; tran < 2; tran++ ) {
|
|---|
| 443 | for ( int i = 0; keyname[i].name; i++ ) {
|
|---|
| 444 | if ( tran ? accel == QAccel::tr(keyname[i].name)
|
|---|
| 445 | : accel == keyname[i].name ) {
|
|---|
| 446 | ret |= keyname[i].key;
|
|---|
| 447 | found = TRUE;
|
|---|
| 448 | break;
|
|---|
| 449 | }
|
|---|
| 450 | }
|
|---|
| 451 | if(found)
|
|---|
| 452 | break;
|
|---|
| 453 | }
|
|---|
| 454 | }
|
|---|
| 455 | return ret;
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 | /*!
|
|---|
| 460 | Creates an accelerator string for \a key. For example,
|
|---|
| 461 | CTRL+Key_O gives "Ctrl+O". The strings, "Ctrl", "Shift", etc. are
|
|---|
| 462 | translated (using QObject::tr()) in the "QAccel" context.
|
|---|
| 463 | */
|
|---|
| 464 | QString QKeySequence::encodeString( int key )
|
|---|
| 465 | {
|
|---|
| 466 | QString s;
|
|---|
| 467 | #if defined(Q_OS_MAC) && !defined(QWS)
|
|---|
| 468 | // On MAC the order is Meta, Alt, Shift, Control.
|
|---|
| 469 | if ( (key & META) == META )
|
|---|
| 470 | s += QMAC_META;
|
|---|
| 471 | if ( (key & ALT) == ALT )
|
|---|
| 472 | s += QMAC_ALT;
|
|---|
| 473 | if ( (key & SHIFT) == SHIFT )
|
|---|
| 474 | s += QMAC_SHIFT;
|
|---|
| 475 | if ( (key & CTRL) == CTRL )
|
|---|
| 476 | s += QMAC_CTRL;
|
|---|
| 477 | #else
|
|---|
| 478 | // On other systems the order is Meta, Control, Alt, Shift
|
|---|
| 479 | if ( (key & META) == META )
|
|---|
| 480 | s += QAccel::tr( "Meta" );
|
|---|
| 481 | if ( (key & CTRL) == CTRL ) {
|
|---|
| 482 | if ( !s.isEmpty() )
|
|---|
| 483 | s += QAccel::tr( "+" );
|
|---|
| 484 | s += QAccel::tr( "Ctrl" );
|
|---|
| 485 | }
|
|---|
| 486 | if ( (key & ALT) == ALT ) {
|
|---|
| 487 | if ( !s.isEmpty() )
|
|---|
| 488 | s += QAccel::tr( "+" );
|
|---|
| 489 | s += QAccel::tr( "Alt" );
|
|---|
| 490 | }
|
|---|
| 491 | if ( (key & SHIFT) == SHIFT ) {
|
|---|
| 492 | if ( !s.isEmpty() )
|
|---|
| 493 | s += QAccel::tr( "+" );
|
|---|
| 494 | s += QAccel::tr( "Shift" );
|
|---|
| 495 | }
|
|---|
| 496 | #endif
|
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 | key &= ~(SHIFT | CTRL | ALT | META );
|
|---|
| 500 | QString p;
|
|---|
| 501 |
|
|---|
| 502 | if ( (key & UNICODE_ACCEL) == UNICODE_ACCEL ) {
|
|---|
| 503 | // Note: This character should NOT be upper()'ed, since
|
|---|
| 504 | // the encoded string should indicate EXACTLY what the
|
|---|
| 505 | // key represents! Hence a 'Ctrl+Shift+c' is posible to
|
|---|
| 506 | // represent, but is clearly impossible to trigger...
|
|---|
| 507 | p = QChar(key & 0xffff);
|
|---|
| 508 | } else if ( key >= Key_F1 && key <= Key_F35 ) {
|
|---|
| 509 | p = QAccel::tr( "F%1" ).arg(key - Key_F1 + 1);
|
|---|
| 510 | } else if ( key > Key_Space && key <= Key_AsciiTilde ) {
|
|---|
| 511 | p.sprintf( "%c", key );
|
|---|
| 512 | } else {
|
|---|
| 513 | int i=0;
|
|---|
| 514 | while (keyname[i].name) {
|
|---|
| 515 | if ( key == keyname[i].key ) {
|
|---|
| 516 | p = QAccel::tr(keyname[i].name);
|
|---|
| 517 | break;
|
|---|
| 518 | }
|
|---|
| 519 | ++i;
|
|---|
| 520 | }
|
|---|
| 521 | // If we can't find the actual translatable keyname,
|
|---|
| 522 | // fall back on the unicode representation of it...
|
|---|
| 523 | // Or else characters like Key_aring may not get displayed
|
|---|
| 524 | // ( Really depends on you locale )
|
|---|
| 525 | if ( !keyname[i].name )
|
|---|
| 526 | // Note: This character should NOT be upper()'ed, see above!
|
|---|
| 527 | p = QChar(key & 0xffff);
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | #ifndef Q_OS_MAC
|
|---|
| 531 | if ( !s.isEmpty() )
|
|---|
| 532 | s += QAccel::tr( "+" );
|
|---|
| 533 | #endif
|
|---|
| 534 |
|
|---|
| 535 | s += p;
|
|---|
| 536 | return s;
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | /*!
|
|---|
| 540 | Matches the sequence with \a seq. Returns \c Qt::Identical if
|
|---|
| 541 | successful, \c Qt::PartialMatch for matching but incomplete \a seq,
|
|---|
| 542 | and \c Qt::NoMatch if the sequences have nothing in common.
|
|---|
| 543 | Returns \c Qt::NoMatch if \a seq is shorter.
|
|---|
| 544 | */
|
|---|
| 545 | Qt::SequenceMatch QKeySequence::matches( const QKeySequence& seq ) const
|
|---|
| 546 | {
|
|---|
| 547 | uint userN = count(),
|
|---|
| 548 | seqN = seq.count();
|
|---|
| 549 |
|
|---|
| 550 | if ( userN > seqN )
|
|---|
| 551 | return NoMatch;
|
|---|
| 552 |
|
|---|
| 553 | // If equal in length, we have a potential Identical sequence,
|
|---|
| 554 | // else we already know it can only be partial.
|
|---|
| 555 | SequenceMatch match = ( userN == seqN ? Identical : PartialMatch );
|
|---|
| 556 |
|
|---|
| 557 | for ( uint i = 0; i < userN; i++ ) {
|
|---|
| 558 | int userKey = (*this)[i],
|
|---|
| 559 | sequenceKey = seq[i];
|
|---|
| 560 |
|
|---|
| 561 | if ( (userKey & ~Qt::UNICODE_ACCEL) !=
|
|---|
| 562 | (sequenceKey & ~Qt::UNICODE_ACCEL) )
|
|---|
| 563 | return NoMatch;
|
|---|
| 564 | }
|
|---|
| 565 | return match;
|
|---|
| 566 | }
|
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 | /*!
|
|---|
| 570 | Creates an accelerator string for the key sequence.
|
|---|
| 571 | For instance CTRL+Key_O gives "Ctrl+O". If the key sequence has
|
|---|
| 572 | multiple key codes they are returned comma-separated, e.g.
|
|---|
| 573 | "Alt+X, Ctrl+Y, Z". The strings, "Ctrl", "Shift", etc. are
|
|---|
| 574 | translated (using QObject::tr()) in the "QAccel" scope. If the key
|
|---|
| 575 | sequence has no keys, QString::null is returned.
|
|---|
| 576 |
|
|---|
| 577 | On Mac OS X, the string returned resembles the sequence that is shown in
|
|---|
| 578 | the menubar.
|
|---|
| 579 | */
|
|---|
| 580 | QKeySequence::operator QString() const
|
|---|
| 581 | {
|
|---|
| 582 | int end = count();
|
|---|
| 583 | if ( !end ) return QString::null;
|
|---|
| 584 |
|
|---|
| 585 | QString complete;
|
|---|
| 586 | int i = 0;
|
|---|
| 587 | while ( i < end ) {
|
|---|
| 588 | complete += encodeString( d->key[i] );
|
|---|
| 589 | i++;
|
|---|
| 590 | if ( i != end)
|
|---|
| 591 | complete += ", ";
|
|---|
| 592 | }
|
|---|
| 593 | return complete;
|
|---|
| 594 | }
|
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 | /*!
|
|---|
| 598 | \obsolete
|
|---|
| 599 | For backward compatibility: returns the first keycode
|
|---|
| 600 | as integer. If the key sequence is empty, 0 is returned.
|
|---|
| 601 | */
|
|---|
| 602 | QKeySequence::operator int () const
|
|---|
| 603 | {
|
|---|
| 604 | if ( 1 <= count() )
|
|---|
| 605 | return d->key[0];
|
|---|
| 606 | return 0;
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 | /*!
|
|---|
| 611 | Returns a reference to the element at position \a index in the key
|
|---|
| 612 | sequence. This can only be used to read an element.
|
|---|
| 613 | */
|
|---|
| 614 | int QKeySequence::operator[]( uint index ) const
|
|---|
| 615 | {
|
|---|
| 616 | #ifdef QT_CHECK_STATE
|
|---|
| 617 | if ( index > 4 ) {
|
|---|
| 618 | qWarning( "QKeySequence::operator[]: index %u out of range", index );
|
|---|
| 619 | return 0;
|
|---|
| 620 | }
|
|---|
| 621 | #endif // QT_CHECK_STATE
|
|---|
| 622 | return d->key[index];
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 |
|
|---|
| 626 | /*!
|
|---|
| 627 | Assignment operator. Assigns \a keysequence to this
|
|---|
| 628 | object.
|
|---|
| 629 | */
|
|---|
| 630 | QKeySequence &QKeySequence::operator=( const QKeySequence & keysequence )
|
|---|
| 631 | {
|
|---|
| 632 | keysequence.d->ref();
|
|---|
| 633 | if ( d->deref() )
|
|---|
| 634 | delete d;
|
|---|
| 635 | d = keysequence.d;
|
|---|
| 636 | return *this;
|
|---|
| 637 | }
|
|---|
| 638 |
|
|---|
| 639 |
|
|---|
| 640 | /*!
|
|---|
| 641 | Returns TRUE if \a keysequence is equal to this key
|
|---|
| 642 | sequence; otherwise returns FALSE.
|
|---|
| 643 | */
|
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 | bool QKeySequence::operator==( const QKeySequence& keysequence ) const
|
|---|
| 647 | {
|
|---|
| 648 | return ( (d->key[0]&~UNICODE_ACCEL) == (keysequence.d->key[0]&~UNICODE_ACCEL) &&
|
|---|
| 649 | (d->key[1]&~UNICODE_ACCEL) == (keysequence.d->key[1]&~UNICODE_ACCEL) &&
|
|---|
| 650 | (d->key[2]&~UNICODE_ACCEL) == (keysequence.d->key[2]&~UNICODE_ACCEL) &&
|
|---|
| 651 | (d->key[3]&~UNICODE_ACCEL) == (keysequence.d->key[3]&~UNICODE_ACCEL) );
|
|---|
| 652 | }
|
|---|
| 653 |
|
|---|
| 654 |
|
|---|
| 655 | /*!
|
|---|
| 656 | Returns TRUE if \a keysequence is not equal to this key sequence;
|
|---|
| 657 | otherwise returns FALSE.
|
|---|
| 658 | */
|
|---|
| 659 | bool QKeySequence::operator!= ( const QKeySequence& keysequence ) const
|
|---|
| 660 | {
|
|---|
| 661 | QKeySequence *that = (QKeySequence*)this;
|
|---|
| 662 | return !( (*that) == keysequence );
|
|---|
| 663 | }
|
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 | /*****************************************************************************
|
|---|
| 667 | QKeySequence stream functions
|
|---|
| 668 | *****************************************************************************/
|
|---|
| 669 | #if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO)
|
|---|
| 670 | /*!
|
|---|
| 671 | \relates QKeySequence
|
|---|
| 672 |
|
|---|
| 673 | Writes the key sequence \a keysequence to the stream \a s.
|
|---|
| 674 |
|
|---|
| 675 | \sa \link datastreamformat.html Format of the QDataStream operators \endlink
|
|---|
| 676 | */
|
|---|
| 677 | QDataStream &operator<<( QDataStream &s, const QKeySequence &keysequence )
|
|---|
| 678 | {
|
|---|
| 679 | QValueList<int> list;
|
|---|
| 680 | list += keysequence.d->key[0];
|
|---|
| 681 | list += keysequence.d->key[1];
|
|---|
| 682 | list += keysequence.d->key[2];
|
|---|
| 683 | list += keysequence.d->key[3];
|
|---|
| 684 | s << list;
|
|---|
| 685 |
|
|---|
| 686 | return s;
|
|---|
| 687 | }
|
|---|
| 688 |
|
|---|
| 689 |
|
|---|
| 690 | /*!
|
|---|
| 691 | \relates QKeySequence
|
|---|
| 692 |
|
|---|
| 693 | Reads a key sequence from the stream \a s into the key sequence \a
|
|---|
| 694 | keysequence.
|
|---|
| 695 |
|
|---|
| 696 | \sa \link datastreamformat.html Format of the QDataStream operators \endlink
|
|---|
| 697 | */
|
|---|
| 698 | QDataStream &operator>>( QDataStream &s, QKeySequence &keysequence )
|
|---|
| 699 | {
|
|---|
| 700 | QValueList<int> list;
|
|---|
| 701 | s >> list;
|
|---|
| 702 |
|
|---|
| 703 | #ifdef QT_CHECK_STATE
|
|---|
| 704 | if ( 1 != list.count() && 4 != list.count() ) {
|
|---|
| 705 | qWarning( "Invalid QKeySequence data in the datastream." );
|
|---|
| 706 | return s;
|
|---|
| 707 | }
|
|---|
| 708 | #endif
|
|---|
| 709 |
|
|---|
| 710 | if ( 1 == list.count() ) {
|
|---|
| 711 | keysequence.d->key[0] = *list.at( 0 );
|
|---|
| 712 | keysequence.d->key[1] =
|
|---|
| 713 | keysequence.d->key[2] =
|
|---|
| 714 | keysequence.d->key[3] = 0;
|
|---|
| 715 | } else {
|
|---|
| 716 | keysequence.d->key[0] = *list.at( 0 );
|
|---|
| 717 | keysequence.d->key[1] = *list.at( 1 );
|
|---|
| 718 | keysequence.d->key[2] = *list.at( 2 );
|
|---|
| 719 | keysequence.d->key[3] = *list.at( 3 );
|
|---|
| 720 | }
|
|---|
| 721 | return s;
|
|---|
| 722 | }
|
|---|
| 723 |
|
|---|
| 724 | #endif //QT_NO_DATASTREAM
|
|---|
| 725 |
|
|---|
| 726 | #endif //QT_NO_ACCEL
|
|---|