command.cpp 14.3 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
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
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

30 31 32
#include "command_p.h"

#include "icore.h"
33
#include "icontext.h"
34 35
#include "uniqueidmanager.h"

con's avatar
con committed
36
#include <QtCore/QDebug>
37 38
#include <QtCore/QTextStream>

con's avatar
con committed
39 40
#include <QtGui/QAction>
#include <QtGui/QShortcut>
mae's avatar
mae committed
41
#include <QtGui/QMainWindow>
con's avatar
con committed
42 43

/*!
con's avatar
con committed
44
    \class Core::Command
con's avatar
con committed
45 46
    \mainclass

47
    \brief The class Command represents an action like a menu item, tool button, or shortcut.
con's avatar
con committed
48 49 50 51
    You don't create Command objects directly, instead use \l{ActionManager::registerAction()}
    to register an action and retrieve a Command. The Command object represents the user visible
    action and its properties. If multiple actions are registered with the same ID (but
    different contexts) the returned Command is the shared one between these actions.
con's avatar
con committed
52

con's avatar
con committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66
    A Command has two basic properties: A default shortcut and a default text. The default
    shortcut is a key sequence that the user can use to trigger the active action that
    the Command represents. The default text is e.g. used for representing the Command
    in the keyboard shortcut preference pane. If the default text is empty, the text
    of the visible action is used.

    The user visible action is updated to represent the state of the active action (if any).
    For performance reasons only the enabled and visible state are considered by default though.
    You can tell a Command to also update the actions icon and text by setting the
    corresponding \l{Command::CommandAttribute}{attribute}.

    If there is no active action, the default behavior of the visible action is to be disabled.
    You can change that behavior to make the visible action hide instead via the Command's
    \l{Command::CommandAttribute}{attributes}.
con's avatar
con committed
67 68 69
*/

/*!
con's avatar
con committed
70
    \enum Command::CommandAttribute
con's avatar
con committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84
    Defines how the user visible action is updated when the active action changes.
    The default is to update the enabled and visible state, and to disable the
    user visible action when there is no active action.
    \omitvalue CA_Mask
    \value CA_UpdateText
        Also update the actions text.
    \value CA_UpdateIcon
        Also update the actions icon.
    \value CA_Hide
        When there is no active action, hide the user "visible" action, instead of just
        disabling it.
    \value CA_NonConfigureable
        Flag to indicate that the keyboard shortcut of this Command should not be
        configurable by the user.
con's avatar
con committed
85 86 87
*/

/*!
con's avatar
con committed
88 89 90 91
    \fn void Command::setDefaultKeySequence(const QKeySequence &key)
    Set the default keyboard shortcut that can be used to activate this command to \a key.
    This is used if the user didn't customize the shortcut, or resets the shortcut
    to the default one.
con's avatar
con committed
92 93 94
*/

/*!
con's avatar
con committed
95 96 97
    \fn void Command::defaultKeySequence() const
    Returns the default keyboard shortcut that can be used to activate this command.
    \sa setDefaultKeySequence()
con's avatar
con committed
98 99 100
*/

/*!
con's avatar
con committed
101 102 103
    \fn void Command::keySequenceChanged()
    Sent when the keyboard shortcut assigned to this Command changes, e.g.
    when the user sets it in the keyboard shortcut settings dialog.
con's avatar
con committed
104 105 106
*/

/*!
con's avatar
con committed
107 108 109
    \fn QKeySequence Command::keySequence() const
    Returns the current keyboard shortcut assigned to this Command.
    \sa defaultKeySequence()
con's avatar
con committed
110 111 112
*/

/*!
con's avatar
con committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    \fn void Command::setKeySequence(const QKeySequence &key)
    \internal
*/

/*!
    \fn void Command::setDefaultText(const QString &text)
    Set the \a text that is used to represent the Command in the
    keyboard shortcut settings dialog. If you don't set this,
    the current text from the user visible action is taken (which
    is ok in many cases).
*/

/*!
    \fn QString Command::defaultText() const
    Returns the text that is used to present this Command to the user.
    \sa setDefaultText()
*/

/*!
    \fn int Command::id() const
    \internal
*/

/*!
    \fn QString Command::stringWithAppendedShortcut(const QString &string) const
    Returns the \a string with an appended representation of the keyboard shortcut
    that is currently assigned to this Command.
*/

/*!
    \fn QAction *Command::action() const
    Returns the user visible action for this Command.
    If the Command represents a shortcut, it returns null.
    Use this action to put it on e.g. tool buttons. The action
    automatically forwards trigger and toggle signals to the
    action that is currently active for this Command.
    It also shows the current keyboard shortcut in its
    tool tip (in addition to the tool tip of the active action)
    and gets disabled/hidden when there is
    no active action for the current context.
con's avatar
con committed
153 154 155
*/

/*!
con's avatar
con committed
156 157 158
    \fn QShortcut *Command::shortcut() const
    Returns the shortcut for this Command.
    If the Command represents an action, it returns null.
con's avatar
con committed
159 160 161
*/

/*!
con's avatar
con committed
162 163 164 165 166
    \fn void Command::setAttribute(CommandAttribute attribute)
    Add the \a attribute to the attributes of this Command.
    \sa CommandAttribute
    \sa removeAttribute()
    \sa hasAttribute()
con's avatar
con committed
167 168 169
*/

/*!
con's avatar
con committed
170 171 172 173
    \fn void Command::removeAttribute(CommandAttribute attribute)
    Remove the \a attribute from the attributes of this Command.
    \sa CommandAttribute
    \sa setAttribute()
con's avatar
con committed
174 175 176
*/

/*!
con's avatar
con committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    \fn bool Command::hasAttribute(CommandAttribute attribute) const
    Returns if the Command has the \a attribute set.
    \sa CommandAttribute
    \sa removeAttribute()
    \sa setAttribute()
*/

/*!
    \fn bool Command::isActive() const
    Returns if the Command has an active action/shortcut for the current
    context.
*/

/*!
    \fn Command::~Command()
    \internal
con's avatar
con committed
193 194 195 196 197
*/

using namespace Core::Internal;

/*!
con's avatar
con committed
198 199
    \class CommandPrivate
    \internal
con's avatar
con committed
200 201
*/

con's avatar
con committed
202 203
CommandPrivate::CommandPrivate(int id)
    : m_attributes(0), m_id(id)
con's avatar
con committed
204 205 206
{
}

con's avatar
con committed
207
void CommandPrivate::setDefaultKeySequence(const QKeySequence &key)
con's avatar
con committed
208 209 210 211
{
    m_defaultKey = key;
}

con's avatar
con committed
212
QKeySequence CommandPrivate::defaultKeySequence() const
con's avatar
con committed
213 214 215 216
{
    return m_defaultKey;
}

con's avatar
con committed
217
void CommandPrivate::setDefaultText(const QString &text)
con's avatar
con committed
218 219 220 221
{
    m_defaultText = text;
}

con's avatar
con committed
222
QString CommandPrivate::defaultText() const
con's avatar
con committed
223 224 225 226
{
    return m_defaultText;
}

con's avatar
con committed
227
int CommandPrivate::id() const
con's avatar
con committed
228 229 230 231
{
    return m_id;
}

con's avatar
con committed
232
QAction *CommandPrivate::action() const
con's avatar
con committed
233 234 235 236
{
    return 0;
}

con's avatar
con committed
237
QShortcut *CommandPrivate::shortcut() const
con's avatar
con committed
238 239 240 241
{
    return 0;
}

242
Core::Context CommandPrivate::context() const
243 244 245 246
{
    return m_context;
}

con's avatar
con committed
247
void CommandPrivate::setAttribute(CommandAttribute attr)
con's avatar
con committed
248
{
con's avatar
con committed
249
    m_attributes |= attr;
con's avatar
con committed
250 251
}

con's avatar
con committed
252
void CommandPrivate::removeAttribute(CommandAttribute attr)
con's avatar
con committed
253
{
con's avatar
con committed
254
    m_attributes &= ~attr;
con's avatar
con committed
255 256
}

con's avatar
con committed
257
bool CommandPrivate::hasAttribute(CommandAttribute attr) const
con's avatar
con committed
258
{
con's avatar
con committed
259
    return (m_attributes & attr);
con's avatar
con committed
260 261
}

con's avatar
con committed
262
QString CommandPrivate::stringWithAppendedShortcut(const QString &str) const
con's avatar
con committed
263 264 265 266 267 268 269 270 271
{
    return QString("%1 <span style=\"color: gray; font-size: small\">%2</span>").arg(str).arg(
            keySequence().toString(QKeySequence::NativeText));
}

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

/*!
    \class Shortcut
con's avatar
con committed
272
    \internal
con's avatar
con committed
273 274 275
*/

Shortcut::Shortcut(int id)
con's avatar
con committed
276
    : CommandPrivate(id), m_shortcut(0)
con's avatar
con committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
{

}

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;
}

299
void Shortcut::setContext(const Core::Context &context)
con's avatar
con committed
300 301 302 303
{
    m_context = context;
}

304
Core::Context Shortcut::context() const
con's avatar
con committed
305 306 307 308 309 310
{
    return m_context;
}

void Shortcut::setDefaultKeySequence(const QKeySequence &key)
{
311 312
    if (m_shortcut->key().isEmpty())
        setKeySequence(key);
con's avatar
con committed
313
    CommandPrivate::setDefaultKeySequence(key);
con's avatar
con committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
}

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;
}

337
bool Shortcut::setCurrentContext(const Context &context)
con's avatar
con committed
338 339 340
{
    foreach (int ctxt, m_context) {
        if (context.contains(ctxt)) {
con's avatar
con committed
341 342 343 344
            if (!m_shortcut->isEnabled()) {
                m_shortcut->setEnabled(true);
                emit activeStateChanged();
            }
con's avatar
con committed
345 346 347
            return true;
        }
    }
con's avatar
con committed
348 349 350 351
    if (m_shortcut->isEnabled()) {
        m_shortcut->setEnabled(false);
        emit activeStateChanged();
    }
con's avatar
con committed
352 353 354 355 356 357 358 359 360 361 362
    return false;
}

bool Shortcut::isActive() const
{
    return m_shortcut->isEnabled();
}

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

/*!
con's avatar
con committed
363 364
  \class Action
  \internal
con's avatar
con committed
365
*/
con's avatar
con committed
366
Action::Action(int id)
367 368 369 370 371
    : CommandPrivate(id),
    m_action(0),
    m_currentAction(0),
    m_active(false),
    m_contextInitialized(false)
con's avatar
con committed
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
{

}

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)
{
410
    if (m_action->shortcut().isEmpty())
411
        setKeySequence(key);
con's avatar
con committed
412
    CommandPrivate::setDefaultKeySequence(key);
con's avatar
con committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
}

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();
}

435
bool Action::setCurrentContext(const Core::Context &context)
con's avatar
con committed
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
{
    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();
        return true;
    }
mae's avatar
mae committed
466
    // no active/delegate action, "visible" action is not enabled/visible
con's avatar
con committed
467 468 469
    if (hasAttribute(CA_Hide))
        m_action->setVisible(false);
    m_action->setEnabled(false);
con's avatar
con committed
470
    setActive(false);
con's avatar
con committed
471 472 473
    return false;
}

474 475 476 477 478 479 480 481 482 483 484 485 486 487
static inline QString msgActionWarning(QAction *newAction, int k, QAction *oldAction)
{
    QString msg;
    QTextStream str(&msg);
    str << "addOverrideAction " << newAction->objectName() << '/' << newAction->text()
         << ": Action ";
    if (oldAction)
        str << oldAction->objectName() << '/' << oldAction->text();
    str << " is already registered for context " << k << ' '
        << Core::ICore::instance()->uniqueIDManager()->stringForUniqueIdentifier(k)
        << '.';
    return msg;
}

488
void Action::addOverrideAction(QAction *action, const Core::Context &context)
con's avatar
con committed
489 490 491 492 493 494 495
{
    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))
496
                qWarning("%s", qPrintable(msgActionWarning(action, k, m_contextActionMap.value(k, 0))));
con's avatar
con committed
497 498 499 500 501
            m_contextActionMap.insert(k, action);
        }
    }
}

502
void Action::actionChanged()
con's avatar
con committed
503 504 505 506
{
    if (hasAttribute(CA_UpdateIcon)) {
        m_action->setIcon(m_currentAction->icon());
        m_action->setIconText(m_currentAction->iconText());
507 508 509
#ifndef Q_WS_MAC
        m_action->setIconVisibleInMenu(m_currentAction->isIconVisibleInMenu());
#endif
con's avatar
con committed
510 511 512 513 514 515 516 517 518
    }
    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());
    }

con's avatar
con committed
519
    m_action->setCheckable(m_currentAction->isCheckable());
con's avatar
con committed
520 521 522 523 524 525
    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());
con's avatar
con committed
526
    setActive(m_action->isEnabled() && m_action->isVisible() && !m_action->isSeparator());
con's avatar
con committed
527 528
}

529
bool Action::isActive() const
con's avatar
con committed
530 531 532
{
    return m_active;
}
con's avatar
con committed
533 534 535 536 537 538 539 540

void Action::setActive(bool state)
{
    if (state != m_active) {
        m_active = state;
        emit activeStateChanged();
    }
}