command.cpp 9.61 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
33
#include <QtCore/QDebug>
#include <QtGui/QAction>
#include <QtGui/QShortcut>

con's avatar
con committed
34
#include "command_p.h"
con's avatar
con committed
35
36

/*!
con's avatar
con committed
37
    \class Core::Command
con's avatar
con committed
38
39
    \mainclass

40
    \brief The class Command represents an action like a menu item, tool button, or shortcut.
con's avatar
con committed
41
42
43
44

*/

/*!
con's avatar
con committed
45
    \enum Command::CommandType
con's avatar
con committed
46
47
48
*/

/*!
con's avatar
con committed
49
    \enum Command::CommandAttribute
con's avatar
con committed
50
51
52
*/

/*!
con's avatar
con committed
53
    \fn virtual void Command::setDefaultKeySequence(const QKeySequence &key)
con's avatar
con committed
54
55
56
*/

/*!
con's avatar
con committed
57
    \fn virtual int Command::id() const
con's avatar
con committed
58
59
60
*/

/*!
con's avatar
con committed
61
    \fn virtual CommandType Command::type() const
con's avatar
con committed
62
63
64
*/

/*!
con's avatar
con committed
65
    \fn virtual QAction *Command::action() const
con's avatar
con committed
66
67
68
*/

/*!
con's avatar
con committed
69
    \fn virtual QShortcut *Command::shortcut() const
con's avatar
con committed
70
71
72
*/

/*!
con's avatar
con committed
73
    \fn virtual void Command::setAttribute(CommandAttribute attr)
con's avatar
con committed
74
75
76
*/

/*!
con's avatar
con committed
77
    \fn virtual void Command::removeAttribute(CommandAttribute attr)
con's avatar
con committed
78
79
80
*/

/*!
con's avatar
con committed
81
    \fn virtual bool Command::hasAttribute(CommandAttribute attr) const
con's avatar
con committed
82
83
84
*/

/*!
con's avatar
con committed
85
    \fn virtual bool Command::isActive() const
con's avatar
con committed
86
87
88
*/

/*!
con's avatar
con committed
89
    \fn virtual Command::~Command()
con's avatar
con committed
90
91
92
93
94
*/

using namespace Core::Internal;

/*!
con's avatar
con committed
95
96
97
    \class CommandPrivate
    \inheaderfile command_p.h
    \internal
con's avatar
con committed
98
99
*/

con's avatar
con committed
100
CommandPrivate::CommandPrivate(CommandType type, int id)
con's avatar
con committed
101
102
103
104
    : m_type(type), m_id(id)
{
}

con's avatar
con committed
105
void CommandPrivate::setStateFlags(int state)
con's avatar
con committed
106
107
108
109
{
    m_type |= (state & CS_Mask);
}

con's avatar
con committed
110
int CommandPrivate::stateFlags() const
con's avatar
con committed
111
112
113
114
{
    return (m_type & CS_Mask);
}

con's avatar
con committed
115
void CommandPrivate::setDefaultKeySequence(const QKeySequence &key)
con's avatar
con committed
116
117
118
119
{
    m_defaultKey = key;
}

con's avatar
con committed
120
QKeySequence CommandPrivate::defaultKeySequence() const
con's avatar
con committed
121
122
123
124
{
    return m_defaultKey;
}

con's avatar
con committed
125
void CommandPrivate::setDefaultText(const QString &text)
con's avatar
con committed
126
127
128
129
{
    m_defaultText = text;
}

con's avatar
con committed
130
QString CommandPrivate::defaultText() const
con's avatar
con committed
131
132
133
134
{
    return m_defaultText;
}

con's avatar
con committed
135
int CommandPrivate::id() const
con's avatar
con committed
136
137
138
139
{
    return m_id;
}

con's avatar
con committed
140
CommandPrivate::CommandType CommandPrivate::type() const
con's avatar
con committed
141
142
143
144
{
    return (CommandType)(m_type & CT_Mask);
}

con's avatar
con committed
145
QAction *CommandPrivate::action() const
con's avatar
con committed
146
147
148
149
{
    return 0;
}

con's avatar
con committed
150
QShortcut *CommandPrivate::shortcut() const
con's avatar
con committed
151
152
153
154
{
    return 0;
}

con's avatar
con committed
155
void CommandPrivate::setAttribute(CommandAttribute attr)
con's avatar
con committed
156
157
158
159
{
    m_type |= attr;
}

con's avatar
con committed
160
void CommandPrivate::removeAttribute(CommandAttribute attr)
con's avatar
con committed
161
162
163
164
{
    m_type &= ~attr;
}

con's avatar
con committed
165
bool CommandPrivate::hasAttribute(CommandAttribute attr) const
con's avatar
con committed
166
167
168
169
{
    return (m_type & attr);
}

con's avatar
con committed
170
QString CommandPrivate::stringWithAppendedShortcut(const QString &str) const
con's avatar
con committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
{
    return QString("%1 <span style=\"color: gray; font-size: small\">%2</span>").arg(str).arg(
            keySequence().toString(QKeySequence::NativeText));
}

// ---------- Shortcut ------------

/*!
    \class Shortcut
*/

/*!
    ...
*/
Shortcut::Shortcut(int id)
con's avatar
con committed
186
    : CommandPrivate(CT_Shortcut, id), m_shortcut(0)
con's avatar
con committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
{

}

/*!
    ...
*/
QString Shortcut::name() const
{
    if (!m_shortcut)
        return QString();

    return m_shortcut->whatsThis();
}

/*!
    ...
*/
void Shortcut::setShortcut(QShortcut *shortcut)
{
    m_shortcut = shortcut;
}

/*!
    ...
*/
QShortcut *Shortcut::shortcut() const
{
    return m_shortcut;
}

/*!
    ...
*/
void Shortcut::setContext(const QList<int> &context)
{
    m_context = context;
}

/*!
    ...
*/
QList<int> Shortcut::context() const
{
    return m_context;
}

/*!
    ...
*/
void Shortcut::setDefaultKeySequence(const QKeySequence &key)
{
    setKeySequence(key);
con's avatar
con committed
240
    CommandPrivate::setDefaultKeySequence(key);
con's avatar
con committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
}

void Shortcut::setKeySequence(const QKeySequence &key)
{
    m_shortcut->setKey(key);
    emit keySequenceChanged();
}

QKeySequence Shortcut::keySequence() const
{
    return m_shortcut->key();
}

void Shortcut::setDefaultText(const QString &text)
{
    m_defaultText = text;
}

QString Shortcut::defaultText() const
{
    return m_defaultText;
}

/*!
    ...
*/
bool Shortcut::setCurrentContext(const QList<int> &context)
{
    foreach (int ctxt, m_context) {
        if (context.contains(ctxt)) {
            m_shortcut->setEnabled(true);
            return true;
        }
    }
    m_shortcut->setEnabled(false);
    return false;
}

/*!
    ...
*/
bool Shortcut::isActive() const
{
    return m_shortcut->isEnabled();
}

// ---------- Action ------------

/*!
    \class Action
*/

/*!
    ...
*/
Action::Action(CommandType type, int id)
con's avatar
con committed
297
    : CommandPrivate(type, id), m_action(0)
con's avatar
con committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
{

}

/*!
    ...
*/
QString Action::name() const
{
    if (!m_action)
        return QString();

    return m_action->text();
}

/*!
    ...
*/
void Action::setAction(QAction *action)
{
    m_action = action;
    if (m_action) {
        m_action->setParent(this);
        m_toolTip = m_action->toolTip();
    }
}

/*!
    ...
*/
QAction *Action::action() const
{
    return m_action;
}

/*!
    ...
*/
void Action::setLocations(const QList<CommandLocation> &locations)
{
    m_locations = locations;
}

/*!
    ...
*/
QList<CommandLocation> Action::locations() const
{
    return m_locations;
}

/*!
    ...
*/
void Action::setDefaultKeySequence(const QKeySequence &key)
{
    setKeySequence(key);
con's avatar
con committed
355
    CommandPrivate::setDefaultKeySequence(key);
con's avatar
con committed
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
}

void Action::setKeySequence(const QKeySequence &key)
{
    m_action->setShortcut(key);
    updateToolTipWithKeySequence();
    emit keySequenceChanged();
}

void Action::updateToolTipWithKeySequence()
{
    if (m_action->shortcut().isEmpty())
        m_action->setToolTip(m_toolTip);
    else
        m_action->setToolTip(stringWithAppendedShortcut(m_toolTip));
}

QKeySequence Action::keySequence() const
{
    return m_action->shortcut();
}

// ---------- OverrideableAction ------------

/*!
    \class OverrideableAction
*/

/*!
    ...
*/
OverrideableAction::OverrideableAction(int id)
    : Action(CT_OverridableAction, id), m_currentAction(0), m_active(false),
    m_contextInitialized(false)
{
}

/*!
    ...
*/
void OverrideableAction::setAction(QAction *action)
{
    Action::setAction(action);
}

/*!
    ...
*/
bool OverrideableAction::setCurrentContext(const QList<int> &context)
{
    m_context = context;

    QAction *oldAction = m_currentAction;
    m_currentAction = 0;
    for (int i = 0; i < m_context.size(); ++i) {
        if (QAction *a = m_contextActionMap.value(m_context.at(i), 0)) {
            m_currentAction = a;
            break;
        }
    }

    if (m_currentAction == oldAction && m_contextInitialized)
        return true;
    m_contextInitialized = true;

    if (oldAction) {
        disconnect(oldAction, SIGNAL(changed()), this, SLOT(actionChanged()));
        disconnect(m_action, SIGNAL(triggered(bool)), oldAction, SIGNAL(triggered(bool)));
        disconnect(m_action, SIGNAL(toggled(bool)), oldAction, SLOT(setChecked(bool)));
    }
    if (m_currentAction) {
        connect(m_currentAction, SIGNAL(changed()), this, SLOT(actionChanged()));
        // we want to avoid the toggling semantic on slot trigger(), so we just connect the signals
        connect(m_action, SIGNAL(triggered(bool)), m_currentAction, SIGNAL(triggered(bool)));
        // we need to update the checked state, so we connect to setChecked slot, which also fires a toggled signal
        connect(m_action, SIGNAL(toggled(bool)), m_currentAction, SLOT(setChecked(bool)));
        actionChanged();
        m_active = true;
        return true;
    }
    if (hasAttribute(CA_Hide))
        m_action->setVisible(false);
    m_action->setEnabled(false);
    m_active = false;
    return false;
}

/*!
    ...
*/
void OverrideableAction::addOverrideAction(QAction *action, const QList<int> &context)
{
    if (context.isEmpty()) {
        m_contextActionMap.insert(0, action);
    } else {
        for (int i=0; i<context.size(); ++i) {
            int k = context.at(i);
            if (m_contextActionMap.contains(k))
                qWarning() << QString("addOverrideAction: action already registered for context when registering '%1'").arg(action->text());
            m_contextActionMap.insert(k, action);
        }
    }
}

/*!
    ...
*/
void OverrideableAction::actionChanged()
{
    if (hasAttribute(CA_UpdateIcon)) {
        m_action->setIcon(m_currentAction->icon());
        m_action->setIconText(m_currentAction->iconText());
    }
    if (hasAttribute(CA_UpdateText)) {
        m_action->setText(m_currentAction->text());
        m_toolTip = m_currentAction->toolTip();
        updateToolTipWithKeySequence();
        m_action->setStatusTip(m_currentAction->statusTip());
        m_action->setWhatsThis(m_currentAction->whatsThis());
    }

    bool block = m_action->blockSignals(true);
    m_action->setChecked(m_currentAction->isChecked());
    m_action->blockSignals(block);

    m_action->setEnabled(m_currentAction->isEnabled());
    m_action->setVisible(m_currentAction->isVisible());
}

/*!
    ...
*/
bool OverrideableAction::isActive() const
{
    return m_active;
}