shortcutmanager.cpp 15 KB
Newer Older
Nikolai Kosjar's avatar
Nikolai Kosjar committed
1
2
/****************************************************************************
**
3
4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Nikolai Kosjar's avatar
Nikolai Kosjar committed
5
6
7
8
9
10
11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12
13
14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Nikolai Kosjar's avatar
Nikolai Kosjar committed
15
**
16
17
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
18
19
20
21
22
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
Nikolai Kosjar's avatar
Nikolai Kosjar committed
23
24
25
**
****************************************************************************/

Marco Bubke's avatar
Marco Bubke committed
26
27
#include "shortcutmanager.h"

28
29
#include "designersettings.h"

Marco Bubke's avatar
Marco Bubke committed
30
31
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
32
#include <coreplugin/actionmanager/command.h>
Marco Bubke's avatar
Marco Bubke committed
33
#include <coreplugin/icore.h>
34
#include <coreplugin/idocument.h>
35
#include <coreplugin/editormanager/documentmodel.h>
36
#include <coreplugin/editormanager/editormanager.h>
Marco Bubke's avatar
Marco Bubke committed
37
#include <coreplugin/coreconstants.h>
38
39
40
#include <qmljseditor/qmljseditorconstants.h>
#include <qmljseditor/qmljseditordocument.h>

Marco Bubke's avatar
Marco Bubke committed
41
#include <utils/hostosinfo.h>
42
43
44
#include <utils/qtcassert.h>

#include <qmljs/qmljsreformatter.h>
Marco Bubke's avatar
Marco Bubke committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include "qmldesignerconstants.h"
#include "qmldesignerplugin.h"
#include "designmodewidget.h"


namespace QmlDesigner {

ShortCutManager::ShortCutManager()
    : QObject(),
    m_revertToSavedAction(0),
    m_saveAction(0),
    m_saveAsAction(0),
    m_closeCurrentEditorAction(0),
    m_closeAllEditorsAction(0),
    m_closeOtherEditorsAction(0),
    m_undoAction(tr("&Undo"), 0),
    m_redoAction(tr("&Redo"), 0),
    m_deleteAction(tr("Delete"), tr("Delete \"%1\""), Utils::ParameterAction::EnabledWithParameter),
    m_cutAction(tr("Cu&t"), tr("Cut \"%1\""), Utils::ParameterAction::EnabledWithParameter),
    m_copyAction(tr("&Copy"), tr("Copy \"%1\""), Utils::ParameterAction::EnabledWithParameter),
    m_pasteAction(tr("&Paste"), tr("Paste \"%1\""), Utils::ParameterAction::EnabledWithParameter),
    m_selectAllAction(tr("Select &All"), tr("Select All \"%1\""), Utils::ParameterAction::EnabledWithParameter),
68
    m_hideSidebarsAction(tr("Toggle Sidebars"), 0),
Marco Bubke's avatar
Marco Bubke committed
69
70
71
72
73
74
75
76
    m_restoreDefaultViewAction(tr("&Restore Default View"), 0),
    m_toggleLeftSidebarAction(tr("Toggle &Left Sidebar"), 0),
    m_toggleRightSidebarAction(tr("Toggle &Right Sidebar"), 0),
    m_goIntoComponentAction (tr("&Go into Component"), 0)
{

}

77
78
79
void ShortCutManager::registerActions(const Core::Context &qmlDesignerMainContext,
                                      const Core::Context &qmlDesignerFormEditorContext,
                                      const Core::Context &qmlDesignerNavigatorContext)
Marco Bubke's avatar
Marco Bubke committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{
    Core::ActionContainer *editMenu = Core::ActionManager::actionContainer(Core::Constants::M_EDIT);

    connect(&m_undoAction, SIGNAL(triggered()), this, SLOT(undo()));

    connect(&m_redoAction, SIGNAL(triggered()), this, SLOT(redo()));

    connect(&m_deleteAction, SIGNAL(triggered()), this, SLOT(deleteSelected()));

    connect(&m_cutAction, SIGNAL(triggered()), this, SLOT(cutSelected()));

    connect(&m_copyAction, SIGNAL(triggered()), this, SLOT(copySelected()));

    connect(&m_pasteAction, SIGNAL(triggered()), this, SLOT(paste()));

    connect(&m_selectAllAction, SIGNAL(triggered()), this, SLOT(selectAll()));

    connect(&m_hideSidebarsAction, SIGNAL(triggered()), this, SLOT(toggleSidebars()));

    connect(&m_restoreDefaultViewAction,
            SIGNAL(triggered()),
            QmlDesignerPlugin::instance()->mainWidget(),
            SLOT(restoreDefaultView()));

    connect(&m_goIntoComponentAction, SIGNAL(triggered()), SLOT(goIntoComponent()));

    connect(&m_toggleLeftSidebarAction,
            SIGNAL(triggered()),
            QmlDesignerPlugin::instance()->mainWidget(),
            SLOT(toggleLeftSidebar()));

    connect(&m_toggleRightSidebarAction,
            SIGNAL(triggered()),
            QmlDesignerPlugin::instance()->mainWidget(),
            SLOT(toggleRightSidebar()));

    // Revert to saved
117
    Core::EditorManager *em = Core::EditorManager::instance();
Marco Bubke's avatar
Marco Bubke committed
118
    Core::ActionManager::registerAction(&m_revertToSavedAction,Core::Constants::REVERTTOSAVED, qmlDesignerMainContext);
119
    connect(&m_revertToSavedAction, SIGNAL(triggered()), em, SLOT(revertToSaved()));
Marco Bubke's avatar
Marco Bubke committed
120
121
122

    //Save
    Core::ActionManager::registerAction(&m_saveAction, Core::Constants::SAVE, qmlDesignerMainContext);
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    connect(&m_saveAction, &QAction::triggered, em, [em] {
        DesignerSettings settings = QmlDesignerPlugin::instance()->settings();
        /* Reformat document if we have a .ui.qml file */
        if (settings.value(DesignerSettingsKey::REFORMAT_UI_QML_FILES).toBool()
                && em->currentDocument()->filePath().toString().endsWith(".ui.qml"))
            if (QmlJSEditor::QmlJSEditorDocument *document
                    = qobject_cast<QmlJSEditor::QmlJSEditorDocument *>(em->currentDocument())) {
                const QString &newText = QmlJS::reformat(document->semanticInfo().document);
                QTextCursor tc(document->document());
                tc.movePosition(QTextCursor::Start);
                tc.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
                tc.insertText(newText);
            }
        em->saveDocument();
    });


Marco Bubke's avatar
Marco Bubke committed
140
141
142

    //Save As
    Core::ActionManager::registerAction(&m_saveAsAction, Core::Constants::SAVEAS, qmlDesignerMainContext);
143
    connect(&m_saveAsAction, SIGNAL(triggered()), em, SLOT(saveDocumentAs()));
Marco Bubke's avatar
Marco Bubke committed
144
145
146

    //Close Editor
    Core::ActionManager::registerAction(&m_closeCurrentEditorAction, Core::Constants::CLOSE, qmlDesignerMainContext);
147
    connect(&m_closeCurrentEditorAction, SIGNAL(triggered()), em, SLOT(slotCloseCurrentEditorOrDocument()));
Marco Bubke's avatar
Marco Bubke committed
148
149
150

    //Close All
    Core::ActionManager::registerAction(&m_closeAllEditorsAction, Core::Constants::CLOSEALL, qmlDesignerMainContext);
151
    connect(&m_closeAllEditorsAction, SIGNAL(triggered()), em, SLOT(closeAllEditors()));
Marco Bubke's avatar
Marco Bubke committed
152
153
154

    //Close All Others Action
    Core::ActionManager::registerAction(&m_closeOtherEditorsAction, Core::Constants::CLOSEOTHERS, qmlDesignerMainContext);
155
    connect(&m_closeOtherEditorsAction, SIGNAL(triggered()), em, SLOT(closeOtherDocuments()));
Marco Bubke's avatar
Marco Bubke committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169

    // Undo / Redo
    Core::ActionManager::registerAction(&m_undoAction, Core::Constants::UNDO, qmlDesignerMainContext);
    Core::ActionManager::registerAction(&m_redoAction, Core::Constants::REDO, qmlDesignerMainContext);

    Core::Command *command;

    //GoIntoComponent
    command = Core::ActionManager::registerAction(&m_goIntoComponentAction,
                                                  Constants::GO_INTO_COMPONENT, qmlDesignerMainContext);
    command->setDefaultKeySequence(QKeySequence(Qt::Key_F2));

    //Edit Menu

170
    Core::ActionManager::registerAction(&m_deleteAction, QmlDesigner::Constants::C_BACKSPACE, qmlDesignerFormEditorContext);
171
172
173
174
175
176
    command = Core::ActionManager::registerAction(&m_deleteAction, QmlDesigner::Constants::C_BACKSPACE, qmlDesignerNavigatorContext);
    command->setDefaultKeySequence(QKeySequence(Qt::Key_Backspace));
    command->setAttribute(Core::Command::CA_Hide); // don't show delete in other modes
    if (Utils::HostOsInfo::isMacHost())
        editMenu->addAction(command, Core::Constants::G_EDIT_COPYPASTE);

177
    Core::ActionManager::registerAction(&m_deleteAction, QmlDesigner::Constants::C_DELETE, qmlDesignerFormEditorContext);
178
    command = Core::ActionManager::registerAction(&m_deleteAction, QmlDesigner::Constants::C_DELETE, qmlDesignerNavigatorContext);
Marco Bubke's avatar
Marco Bubke committed
179
180
    command->setDefaultKeySequence(QKeySequence::Delete);
    command->setAttribute(Core::Command::CA_Hide); // don't show delete in other modes
181
182
    if (!Utils::HostOsInfo::isMacHost())
        editMenu->addAction(command, Core::Constants::G_EDIT_COPYPASTE);
Marco Bubke's avatar
Marco Bubke committed
183

184
    Core::ActionManager::registerAction(&m_cutAction, Core::Constants::CUT, qmlDesignerFormEditorContext);
Marco Bubke's avatar
Marco Bubke committed
185
186
187
188
    command = Core::ActionManager::registerAction(&m_cutAction, Core::Constants::CUT, qmlDesignerNavigatorContext);
    command->setDefaultKeySequence(QKeySequence::Cut);
    editMenu->addAction(command, Core::Constants::G_EDIT_COPYPASTE);

189
    Core::ActionManager::registerAction(&m_copyAction, Core::Constants::COPY, qmlDesignerFormEditorContext);
Marco Bubke's avatar
Marco Bubke committed
190
191
192
193
    command = Core::ActionManager::registerAction(&m_copyAction,  Core::Constants::COPY, qmlDesignerNavigatorContext);
    command->setDefaultKeySequence(QKeySequence::Copy);
    editMenu->addAction(command, Core::Constants::G_EDIT_COPYPASTE);

194
    Core::ActionManager::registerAction(&m_pasteAction,  Core::Constants::PASTE, qmlDesignerFormEditorContext);
Marco Bubke's avatar
Marco Bubke committed
195
196
197
198
    command = Core::ActionManager::registerAction(&m_pasteAction,  Core::Constants::PASTE, qmlDesignerNavigatorContext);
    command->setDefaultKeySequence(QKeySequence::Paste);
    editMenu->addAction(command, Core::Constants::G_EDIT_COPYPASTE);

199
    Core::ActionManager::registerAction(&m_selectAllAction, Core::Constants::SELECTALL, qmlDesignerFormEditorContext);
Marco Bubke's avatar
Marco Bubke committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    command = Core::ActionManager::registerAction(&m_selectAllAction, Core::Constants::SELECTALL, qmlDesignerNavigatorContext);

    command->setDefaultKeySequence(QKeySequence::SelectAll);
    editMenu->addAction(command, Core::Constants::G_EDIT_SELECTALL);

    Core::ActionContainer *viewsMenu = Core::ActionManager::actionContainer(Core::Constants::M_WINDOW_VIEWS);

    command = Core::ActionManager::registerAction(&m_toggleLeftSidebarAction,  Constants::TOGGLE_LEFT_SIDEBAR, qmlDesignerMainContext);
    command->setAttribute(Core::Command::CA_Hide);
    command->setDefaultKeySequence(QKeySequence("Ctrl+Alt+0"));
    viewsMenu->addAction(command);

    command = Core::ActionManager::registerAction(&m_toggleRightSidebarAction, Constants::TOGGLE_RIGHT_SIDEBAR, qmlDesignerMainContext);
    command->setAttribute(Core::Command::CA_Hide);
    command->setDefaultKeySequence(QKeySequence("Ctrl+Alt+Shift+0"));
    viewsMenu->addAction(command);

    command = Core::ActionManager::registerAction(&m_restoreDefaultViewAction,  Constants::RESTORE_DEFAULT_VIEW, qmlDesignerMainContext);
    command->setAttribute(Core::Command::CA_Hide);
    viewsMenu->addAction(command);

221
    Core::ActionManager::registerAction(&m_hideSidebarsAction, Core::Constants::TOGGLE_SIDEBAR, qmlDesignerMainContext);
Marco Bubke's avatar
Marco Bubke committed
222
223
}

224
void ShortCutManager::updateActions(Core::IEditor* currentEditor)
Marco Bubke's avatar
Marco Bubke committed
225
{
226
    int openedCount = Core::DocumentModel::entryCount();
Marco Bubke's avatar
Marco Bubke committed
227

228
229
230
231
232
233
234
235
    Core::IDocument *document = 0;
    if (currentEditor)
        document = currentEditor->document();
    m_saveAction.setEnabled(document && document->isModified());
    m_saveAsAction.setEnabled(document && document->isSaveAsAllowed());
    m_revertToSavedAction.setEnabled(document
                                      && !document->filePath().isEmpty()
                                      && document->isModified());
Marco Bubke's avatar
Marco Bubke committed
236
237

    QString quotedName;
238
    if (currentEditor && document)
239
        quotedName = '"' + document->displayName() + '"';
Marco Bubke's avatar
Marco Bubke committed
240
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
297
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
355
356
357
358
359
360

    m_saveAsAction.setText(tr("Save %1 As...").arg(quotedName));
    m_saveAction.setText(tr("&Save %1").arg(quotedName));
    m_revertToSavedAction.setText(tr("Revert %1 to Saved").arg(quotedName));

    m_closeCurrentEditorAction.setEnabled(currentEditor != 0);
    m_closeCurrentEditorAction.setText(tr("Close %1").arg(quotedName));
    m_closeAllEditorsAction.setEnabled(openedCount > 0);
    m_closeOtherEditorsAction.setEnabled(openedCount > 1);
    m_closeOtherEditorsAction.setText((openedCount > 1 ? tr("Close All Except %1").arg(quotedName) : tr("Close Others")));
}

void ShortCutManager::undo()
{
    if (currentDesignDocument())
        currentDesignDocument()->undo();
}

void ShortCutManager::redo()
{
    if (currentDesignDocument())
        currentDesignDocument()->redo();
}

void ShortCutManager::deleteSelected()
{
    if (currentDesignDocument())
        currentDesignDocument()->deleteSelected();
}

void ShortCutManager::cutSelected()
{
    if (currentDesignDocument())
        currentDesignDocument()->cutSelected();
}

void ShortCutManager::copySelected()
{
    if (currentDesignDocument())
        currentDesignDocument()->copySelected();
}

void ShortCutManager::paste()
{
    if (currentDesignDocument())
        currentDesignDocument()->paste();
}

void ShortCutManager::selectAll()
{
    if (currentDesignDocument())
        currentDesignDocument()->selectAll();
}

void ShortCutManager::toggleSidebars()
{
    QmlDesignerPlugin::instance()->mainWidget()->toggleSidebars();
}

void ShortCutManager::toggleLeftSidebar()
{
    QmlDesignerPlugin::instance()->mainWidget()->toggleLeftSidebar();
}

void ShortCutManager::toggleRightSidebar()
{
     QmlDesignerPlugin::instance()->mainWidget()->toggleRightSidebar();
}

void ShortCutManager::connectUndoActions(DesignDocument *designDocument)
{
    if (designDocument) {
        connect(designDocument, SIGNAL(undoAvailable(bool)), this, SLOT(undoAvailable(bool)));
        connect(designDocument, SIGNAL(redoAvailable(bool)), this, SLOT(redoAvailable(bool)));
    }
}

void ShortCutManager::disconnectUndoActions(DesignDocument *designDocument)
{
    if (currentDesignDocument()) {
        disconnect(designDocument, SIGNAL(undoAvailable(bool)), this, SLOT(undoAvailable(bool)));
        disconnect(designDocument, SIGNAL(redoAvailable(bool)), this, SLOT(redoAvailable(bool)));
    }
}

void ShortCutManager::updateUndoActions(DesignDocument *designDocument)
{
    if (designDocument) {
        m_undoAction.setEnabled(designDocument->isUndoAvailable());
        m_redoAction.setEnabled(designDocument->isRedoAvailable());
    } else {
        m_undoAction.setEnabled(false);
        m_redoAction.setEnabled(false);
    }
}

DesignDocument *ShortCutManager::currentDesignDocument() const
{
    return QmlDesignerPlugin::instance()->currentDesignDocument();
}

void ShortCutManager::undoAvailable(bool isAvailable)
{
    DesignDocument *documentController = qobject_cast<DesignDocument*>(sender());
    if (currentDesignDocument() &&
        currentDesignDocument() == documentController) {
        m_undoAction.setEnabled(isAvailable);
    }
}

void ShortCutManager::redoAvailable(bool isAvailable)
{
    DesignDocument *documentController = qobject_cast<DesignDocument*>(sender());
    if (currentDesignDocument() &&
        currentDesignDocument() == documentController) {
        m_redoAction.setEnabled(isAvailable);
    }
}

void ShortCutManager::goIntoComponent()
{
361
362
363
364
365
366
    if (currentDesignDocument()
            && currentDesignDocument()->currentModel()
            && currentDesignDocument()->rewriterView()
            && currentDesignDocument()->rewriterView()->hasSingleSelectedModelNode()) {
        DocumentManager::goIntoComponent(currentDesignDocument()->rewriterView()->singleSelectedModelNode());
    }
Marco Bubke's avatar
Marco Bubke committed
367
368
369
}

} // namespace QmlDesigner