texteditoractionhandler.cpp 24.3 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "texteditoractionhandler.h"
31

con's avatar
con committed
32
#include "basetexteditor.h"
33
#include "displaysettings.h"
con's avatar
con committed
34
#include "linenumberfilter.h"
35
36
#include "texteditorconstants.h"
#include "texteditorplugin.h"
con's avatar
con committed
37

con's avatar
con committed
38
#include <locator/locatormanager.h>
39
#include <coreplugin/icore.h>
con's avatar
con committed
40
#include <coreplugin/coreconstants.h>
41
#include <coreplugin/actionmanager/actionmanager.h>
42
#include <coreplugin/actionmanager/actioncontainer.h>
43
#include <utils/hostosinfo.h>
hjk's avatar
hjk committed
44
#include <utils/qtcassert.h>
con's avatar
con committed
45

hjk's avatar
hjk committed
46
#include <QDebug>
47
#include <QAction>
con's avatar
con committed
48
49
50
51

using namespace TextEditor;
using namespace TextEditor::Internal;

52
TextEditorActionHandler::TextEditorActionHandler(const char *context,
53
                                                 uint optionalActions)
54
  : QObject(Core::ICore::instance()),
55
56
57
58
59
    m_undoAction(0),
    m_redoAction(0),
    m_copyAction(0),
    m_cutAction(0),
    m_pasteAction(0),
60
    m_circularPasteAction(0),
61
    m_switchUtf8bomAction(0),
62
63
64
65
66
67
68
69
    m_selectAllAction(0),
    m_gotoAction(0),
    m_printAction(0),
    m_formatAction(0),
    m_visualizeWhitespaceAction(0),
    m_cleanWhitespaceAction(0),
    m_textWrappingAction(0),
    m_unCommentSelectionAction(0),
mae's avatar
mae committed
70
71
72
    m_unfoldAllAction(0),
    m_foldAction(0),
    m_unfoldAction(0),
73
    m_cutLineAction(0),
74
    m_copyLineAction(0),
75
    m_deleteLineAction(0),
76
77
78
79
    m_deleteEndOfWordAction(0),
    m_deleteEndOfWordCamelCaseAction(0),
    m_deleteStartOfWordAction(0),
    m_deleteStartOfWordCamelCaseAction(0),
80
81
82
    m_selectEncodingAction(0),
    m_increaseFontSizeAction(0),
    m_decreaseFontSizeAction(0),
mae's avatar
mae committed
83
    m_resetFontSizeAction(0),
84
85
86
87
88
89
90
91
92
93
    m_gotoBlockStartAction(0),
    m_gotoBlockEndAction(0),
    m_gotoBlockStartWithSelectionAction(0),
    m_gotoBlockEndWithSelectionAction(0),
    m_selectBlockUpAction(0),
    m_selectBlockDownAction(0),
    m_moveLineUpAction(0),
    m_moveLineDownAction(0),
    m_copyLineUpAction(0),
    m_copyLineDownAction(0),
94
    m_joinLinesAction(0),
95
96
97
98
    m_insertLineAboveAction(0),
    m_insertLineBelowAction(0),
    m_upperCaseSelectionAction(0),
    m_lowerCaseSelectionAction(0),
99
100
    m_indentAction(0),
    m_unindentAction(0),
101
    m_followSymbolAction(0),
102
    m_followSymbolInNextSplitAction(0),
103
    m_jumpToFileAction(0),
104
    m_jumpToFileInNextSplitAction(0),
con's avatar
con committed
105
106
    m_optionalActions(optionalActions),
    m_currentEditor(0),
107
    m_contextId(context),
con's avatar
con committed
108
109
    m_initialized(false)
{
hjk's avatar
hjk committed
110
    connect(Core::ICore::editorManager(), SIGNAL(currentEditorChanged(Core::IEditor*)),
111
        this, SLOT(updateCurrentEditor(Core::IEditor*)));
con's avatar
con committed
112
113
}

114
115
116
117
TextEditorActionHandler::~TextEditorActionHandler()
{
}

118
void TextEditorActionHandler::setupActions(BaseTextEditorWidget *editor)
con's avatar
con committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
{
    initializeActions();
    editor->setActionHack(this);
    QObject::connect(editor, SIGNAL(undoAvailable(bool)), this, SLOT(updateUndoAction()));
    QObject::connect(editor, SIGNAL(redoAvailable(bool)), this, SLOT(updateRedoAction()));
    QObject::connect(editor, SIGNAL(copyAvailable(bool)), this, SLOT(updateCopyAction()));
}


void TextEditorActionHandler::initializeActions()
{
    if (!m_initialized) {
        createActions();
        m_initialized = true;
    }
}

void TextEditorActionHandler::createActions()
{
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
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
    using namespace Core::Constants;
    using namespace TextEditor::Constants;

    m_undoAction = registerAction(UNDO,
            SLOT(undoAction()), true, tr("&Undo"));
    m_redoAction = registerAction(REDO,
            SLOT(redoAction()), true, tr("&Redo"));
    m_copyAction = registerAction(COPY,
            SLOT(copyAction()), true);
    m_cutAction = registerAction(CUT,
            SLOT(cutAction()), true);
    m_pasteAction = registerAction(PASTE,
            SLOT(pasteAction()), true);
    m_selectAllAction = registerAction(SELECTALL,
            SLOT(selectAllAction()), true);
    m_gotoAction = registerAction(GOTO,
            SLOT(gotoAction()));
    m_printAction = registerAction(PRINT,
            SLOT(printAction()));
    m_cutLineAction = registerAction(CUT_LINE,
            SLOT(cutLine()), true, tr("Cut &Line"),
            QKeySequence(tr("Shift+Del")));
    m_copyLineAction = registerAction(COPY_LINE,
            SLOT(copyLine()), false, tr("Copy &Line"),
            QKeySequence(tr("Ctrl+Ins")));
    m_deleteLineAction = registerAction(DELETE_LINE,
            SLOT(deleteLine()), true, tr("Delete &Line"));
    m_deleteEndOfWordAction = registerAction(DELETE_END_OF_WORD,
            SLOT(deleteEndOfWord()), true, tr("Delete Word from Cursor On"));
    m_deleteEndOfWordCamelCaseAction = registerAction(DELETE_END_OF_WORD_CAMEL_CASE,
            SLOT(deleteEndOfWordCamelCase()), true, tr("Delete Word Camel Case from Cursor On"));
    m_deleteStartOfWordAction = registerAction(DELETE_START_OF_WORD,
            SLOT(deleteStartOfWord()), true, tr("Delete Word up to Cursor"));
    m_deleteStartOfWordCamelCaseAction = registerAction(DELETE_START_OF_WORD_CAMEL_CASE,
            SLOT(deleteStartOfWordCamelCase()), true, tr("Delete Word Camel Case up to Cursor"));
    m_gotoBlockStartWithSelectionAction = registerAction(GOTO_BLOCK_START_WITH_SELECTION,
            SLOT(gotoBlockStartWithSelection()), true, tr("Go to Block Start with Selection"),
            QKeySequence(tr("Ctrl+{")));
    m_gotoBlockEndWithSelectionAction = registerAction(GOTO_BLOCK_END_WITH_SELECTION,
            SLOT(gotoBlockEndWithSelection()), true, tr("Go to Block End with Selection"),
            QKeySequence(tr("Ctrl+}")));
    m_moveLineUpAction = registerAction(MOVE_LINE_UP,
            SLOT(moveLineUp()), true, tr("Move Line Up"),
            QKeySequence(tr("Ctrl+Shift+Up")));
    m_moveLineDownAction = registerAction(MOVE_LINE_DOWN,
            SLOT(moveLineDown()), true, tr("Move Line Down"),
            QKeySequence(tr("Ctrl+Shift+Down")));
    m_copyLineUpAction = registerAction(COPY_LINE_UP,
            SLOT(copyLineUp()), true, tr("Copy Line Up"),
            QKeySequence(tr("Ctrl+Alt+Up")));
    m_copyLineDownAction = registerAction(COPY_LINE_DOWN,
            SLOT(copyLineDown()), true, tr("Copy Line Down"),
            QKeySequence(tr("Ctrl+Alt+Down")));
    m_joinLinesAction = registerAction(JOIN_LINES,
            SLOT(joinLines()), true, tr("Join Lines"),
            QKeySequence(tr("Ctrl+J")));
    m_insertLineAboveAction = registerAction(INSERT_LINE_ABOVE,
            SLOT(insertLineAbove()), true, tr("Insert Line Above Current Line"),
            QKeySequence(tr("Ctrl+Shift+Return")));
    m_insertLineBelowAction = registerAction(INSERT_LINE_BELOW,
            SLOT(insertLineBelow()), true, tr("Insert Line Below Current Line"),
            QKeySequence(tr("Ctrl+Return")));
    m_upperCaseSelectionAction = registerAction(UPPERCASE_SELECTION,
            SLOT(uppercaseSelection()), true, tr("Uppercase Selection"),
            QKeySequence(Core::UseMacShortcuts ? tr("Meta+Shift+U") : tr("Alt+Shift+U")));
    m_lowerCaseSelectionAction = registerAction(LOWERCASE_SELECTION,
            SLOT(lowercaseSelection()), true, tr("Lowercase Selection"),
            QKeySequence(Core::UseMacShortcuts ? tr("Meta+U") : tr("Alt+U")));
    m_switchUtf8bomAction = registerAction(SWITCH_UTF8BOM,
            SLOT(switchUtf8bomAction()), true);
    m_indentAction = registerAction(INDENT,
            SLOT(indent()), true, tr("Indent"));
    m_unindentAction = registerAction(UNINDENT,
            SLOT(unindent()), true, tr("Unindent"));
    m_followSymbolAction = registerAction(FOLLOW_SYMBOL_UNDER_CURSOR,
            SLOT(openLinkUnderCursor()), true, tr("Follow Symbol Under Cursor"),
            QKeySequence(Qt::Key_F2));
    m_followSymbolInNextSplitAction = registerAction(FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT,
            SLOT(openLinkUnderCursorInNextSplit()), true, tr("Follow Symbol Under Cursor in Next Split"),
            QKeySequence(Utils::HostOsInfo::isMacHost() ? tr("Meta+E, F2") : tr("Ctrl+E, F2")));
    m_jumpToFileAction = registerAction(JUMP_TO_FILE_UNDER_CURSOR,
            SLOT(openLinkUnderCursor()), true, tr("Jump To File Under Cursor"),
            QKeySequence(Qt::Key_F2));
    m_jumpToFileInNextSplitAction = registerAction(JUMP_TO_FILE_UNDER_CURSOR_IN_NEXT_SPLIT,
            SLOT(openLinkUnderCursorInNextSplit()), true,
            QKeySequence(Utils::HostOsInfo::isMacHost() ? tr("Meta+E, F2") : tr("Ctrl+E, F2")));

    // register "Edit" Menu Actions
    Core::ActionContainer *editMenu = Core::ActionManager::actionContainer(M_EDIT);
    m_selectEncodingAction = registerAction(SELECT_ENCODING,
            SLOT(selectEncoding()), false, tr("Select Encoding..."),
            QKeySequence(), G_EDIT_OTHER, editMenu);
    m_circularPasteAction = registerAction(CIRCULAR_PASTE,
            SLOT(circularPasteAction()), false, tr("Paste from Clipboard History"),
            QKeySequence(tr("Ctrl+Shift+V")), G_EDIT_COPYPASTE, editMenu);

    // register "Edit -> Advanced" Menu Actions
    Core::ActionContainer *advancedEditMenu = Core::ActionManager::actionContainer(M_EDIT_ADVANCED);
    m_formatAction = registerAction(AUTO_INDENT_SELECTION,
            SLOT(formatAction()), true, tr("Auto-&indent Selection"),
            QKeySequence(tr("Ctrl+I")),
            G_EDIT_FORMAT, advancedEditMenu);
    m_rewrapParagraphAction = registerAction(REWRAP_PARAGRAPH,
            SLOT(rewrapParagraphAction()), true, tr("&Rewrap Paragraph"),
            QKeySequence(Core::UseMacShortcuts ? tr("Meta+E, R") : tr("Ctrl+E, R")),
            G_EDIT_FORMAT, advancedEditMenu);
    m_visualizeWhitespaceAction = registerAction(VISUALIZE_WHITESPACE,
            SLOT(setVisualizeWhitespace(bool)), false, tr("&Visualize Whitespace"),
            QKeySequence(Core::UseMacShortcuts ? tr("Meta+E, Meta+V") : tr("Ctrl+E, Ctrl+V")),
            G_EDIT_FORMAT, advancedEditMenu);
    m_cleanWhitespaceAction = registerAction(CLEAN_WHITESPACE,
            SLOT(setTextWrapping(bool)), true, tr("Clean Whitespace"),
            QKeySequence(),
            G_EDIT_FORMAT, advancedEditMenu);;
    m_textWrappingAction = registerAction(TEXT_WRAPPING,
            SLOT(setTextWrapping(bool)), false, tr("Enable Text &Wrapping"),
            QKeySequence(Core::UseMacShortcuts ? tr("Meta+E, Meta+W") : tr("Ctrl+E, Ctrl+W")),
            G_EDIT_FORMAT, advancedEditMenu);
    m_textWrappingAction->setCheckable(true);
    m_unCommentSelectionAction = registerAction(UN_COMMENT_SELECTION,
            SLOT(unCommentSelection()), true, tr("Toggle Comment &Selection"),
            QKeySequence(tr("Ctrl+/")),
            G_EDIT_FORMAT, advancedEditMenu);
    m_foldAction = registerAction(FOLD,
            SLOT(fold()), true, tr("Fold"),
            QKeySequence(tr("Ctrl+<")),
            G_EDIT_COLLAPSING, advancedEditMenu);
    m_unfoldAction = registerAction(UNFOLD,
            SLOT(unfold()), true, tr("Unfold"),
            QKeySequence(tr("Ctrl+>")),
            G_EDIT_COLLAPSING, advancedEditMenu);
    m_unfoldAllAction = registerAction(UNFOLD_ALL,
            SLOT(unfoldAll()), true, tr("Toggle &Fold All"),
            QKeySequence(),
            G_EDIT_COLLAPSING, advancedEditMenu);
    m_increaseFontSizeAction = registerAction(INCREASE_FONT_SIZE,
            SLOT(increaseFontSize()), false, tr("Increase Font Size"),
            QKeySequence(tr("Ctrl++")),
            G_EDIT_FONT, advancedEditMenu);
    m_decreaseFontSizeAction = registerAction(DECREASE_FONT_SIZE,
            SLOT(decreaseFontSize()), false, tr("Decrease Font Size"),
            QKeySequence(tr("Ctrl+-")),
            G_EDIT_FONT, advancedEditMenu);
    m_resetFontSizeAction = registerAction(RESET_FONT_SIZE,
            SLOT(resetFontSize()), false, tr("Reset Font Size"),
            QKeySequence(Core::UseMacShortcuts ? tr("Meta+0") : tr("Ctrl+0")),
            G_EDIT_FONT, advancedEditMenu);
    m_gotoBlockStartAction = registerAction(GOTO_BLOCK_START,
            SLOT(gotoBlockStart()), true, tr("Go to Block Start"),
            QKeySequence(tr("Ctrl+[")),
            G_EDIT_BLOCKS, advancedEditMenu);
    m_gotoBlockEndAction = registerAction(GOTO_BLOCK_END,
            SLOT(gotoBlockEnd()), true, tr("Go to Block End"),
            QKeySequence(tr("Ctrl+]")),
            G_EDIT_BLOCKS, advancedEditMenu);
    m_selectBlockUpAction = registerAction(SELECT_BLOCK_UP,
            SLOT(selectBlockUp()), true, tr("Select Block Up"),
            QKeySequence(tr("Ctrl+U")),
            G_EDIT_BLOCKS, advancedEditMenu);
    m_selectBlockDownAction = registerAction(SELECT_BLOCK_DOWN,
            SLOT(selectBlockDown()), true, tr("Select Block Down"),
            QKeySequence(),
            G_EDIT_BLOCKS, advancedEditMenu);

    // register GOTO Actions
    registerAction(GOTO_LINE_START,
            SLOT(gotoLineStart()), true, tr("Go to Line Start"));
    registerAction(GOTO_LINE_END,
            SLOT(gotoLineEnd()), true, tr("Go to Line End"));
    registerAction(GOTO_NEXT_LINE,
            SLOT(gotoNextLine()), true, tr("Go to Next Line"));
    registerAction(GOTO_PREVIOUS_LINE,
            SLOT(gotoPreviousLine()), true, tr("Go to Previous Line"));
    registerAction(GOTO_PREVIOUS_CHARACTER,
            SLOT(gotoPreviousCharacter()), true, tr("Go to Previous Character"));
    registerAction(GOTO_NEXT_CHARACTER,
            SLOT(gotoNextCharacter()), true, tr("Go to Next Character"));
    registerAction(GOTO_PREVIOUS_WORD,
            SLOT(gotoPreviousWord()), true, tr("Go to Previous Word"));
    registerAction(GOTO_NEXT_WORD,
            SLOT(gotoNextWord()), true, tr("Go to Next Word"));
    registerAction(GOTO_PREVIOUS_WORD_CAMEL_CASE,
            SLOT(gotoPreviousWordCamelCase()), false, tr("Go to Previous Word Camel Case"));
    registerAction(GOTO_NEXT_WORD_CAMEL_CASE,
            SLOT(gotoNextWordCamelCase()), false, tr("Go to Next Word Camel Case"));

    // register GOTO actions with selection
    registerAction(GOTO_LINE_START_WITH_SELECTION,
            SLOT(gotoLineStartWithSelection()), true, tr("Go to Line Start with Selection"));
    registerAction(GOTO_LINE_END_WITH_SELECTION,
            SLOT(gotoLineEndWithSelection()), true, tr("Go to Line End with Selection"));
    registerAction(GOTO_NEXT_LINE_WITH_SELECTION,
            SLOT(gotoNextLineWithSelection()), true, tr("Go to Next Line with Selection"));
    registerAction(GOTO_PREVIOUS_LINE_WITH_SELECTION,
            SLOT(gotoPreviousLineWithSelection()), true, tr("Go to Previous Line with Selection"));
    registerAction(GOTO_PREVIOUS_CHARACTER_WITH_SELECTION,
            SLOT(gotoPreviousCharacterWithSelection()), true, tr("Go to Previous Character with Selection"));
    registerAction(GOTO_NEXT_CHARACTER_WITH_SELECTION,
            SLOT(gotoNextCharacterWithSelection()), true, tr("Go to Next Character with Selection"));
    registerAction(GOTO_PREVIOUS_WORD_WITH_SELECTION,
            SLOT(gotoPreviousWordWithSelection()), true, tr("Go to Previous Word with Selection"));
    registerAction(GOTO_NEXT_WORD_WITH_SELECTION,
            SLOT(gotoNextWordWithSelection()), true, tr("Go to Next Word with Selection"));
    registerAction(GOTO_PREVIOUS_WORD_CAMEL_CASE_WITH_SELECTION,
            SLOT(gotoPreviousWordCamelCaseWithSelection()), false, tr("Go to Previous Word Camel Case with Selection"));
    registerAction(GOTO_NEXT_WORD_CAMEL_CASE_WITH_SELECTION,
            SLOT(gotoNextWordCamelCaseWithSelection()), false, tr("Go to Next Word Camel Case with Selection"));

    // Collect all modifying actions so we can check for them inside a readonly file
    // and disable them
348
349
350
351
352
353
354
    m_modifyingActions << m_pasteAction;
    m_modifyingActions << m_formatAction;
    m_modifyingActions << m_rewrapParagraphAction;
    m_modifyingActions << m_cleanWhitespaceAction;
    m_modifyingActions << m_unCommentSelectionAction;
    m_modifyingActions << m_cutLineAction;
    m_modifyingActions << m_deleteLineAction;
355
356
357
358
    m_modifyingActions << m_deleteEndOfWordAction;
    m_modifyingActions << m_deleteEndOfWordCamelCaseAction;
    m_modifyingActions << m_deleteStartOfWordAction;
    m_modifyingActions << m_deleteStartOfWordCamelCaseAction;
359
360
361
362
363
364
365
366
367
    m_modifyingActions << m_moveLineUpAction;
    m_modifyingActions << m_moveLineDownAction;
    m_modifyingActions << m_copyLineUpAction;
    m_modifyingActions << m_copyLineDownAction;
    m_modifyingActions << m_joinLinesAction;
    m_modifyingActions << m_insertLineAboveAction;
    m_modifyingActions << m_insertLineBelowAction;
    m_modifyingActions << m_upperCaseSelectionAction;
    m_modifyingActions << m_lowerCaseSelectionAction;
368
    m_modifyingActions << m_circularPasteAction;
369
    m_modifyingActions << m_switchUtf8bomAction;
370
371
    m_modifyingActions << m_indentAction;
    m_modifyingActions << m_unindentAction;
con's avatar
con committed
372
373
}

374
375
376
377
378
379
380
QAction *TextEditorActionHandler::registerAction(const Core::Id &id,
                                                 const char *slot,
                                                 bool scriptable,
                                                 const QString &title,
                                                 const QKeySequence &keySequence,
                                                 const char *menueGroup,
                                                 Core::ActionContainer *container)
con's avatar
con committed
381
382
{
    QAction *result = new QAction(title, this);
383
384
385
    Core::Command *command = Core::ActionManager::registerAction(result, id, m_contextId, scriptable);
    if (!keySequence.isEmpty())
        command->setKeySequence(keySequence);
con's avatar
con committed
386

387
388
    if (container && menueGroup)
        container->addAction(command, menueGroup);
con's avatar
con committed
389

390
391
    connect(result, SIGNAL(triggered(bool)), this, slot);
    return result;
con's avatar
con committed
392
393
394
395
}

TextEditorActionHandler::UpdateMode TextEditorActionHandler::updateMode() const
{
396
    Q_ASSERT(m_currentEditor != 0);
397
    return m_currentEditor->isReadOnly() ? ReadOnlyMode : WriteMode;
con's avatar
con committed
398
399
400
401
}

void TextEditorActionHandler::updateActions()
{
402
403
    if (!m_currentEditor || !m_initialized)
        return;
con's avatar
con committed
404
405
406
407
408
    updateActions(updateMode());
}

void TextEditorActionHandler::updateActions(UpdateMode um)
{
409
410
    foreach (QAction *a, m_modifyingActions)
        a->setEnabled(um != ReadOnlyMode);
411
412
    m_formatAction->setEnabled((m_optionalActions & Format) && um != ReadOnlyMode);
    m_unCommentSelectionAction->setEnabled((m_optionalActions & UnCommentSelection) && um != ReadOnlyMode);
413
    m_followSymbolAction->setEnabled(m_optionalActions & FollowSymbolUnderCursor);
414
    m_followSymbolInNextSplitAction->setEnabled(m_optionalActions & FollowSymbolUnderCursor);
415
    m_jumpToFileAction->setEnabled(m_optionalActions & JumpToFileUnderCursor);
416
    m_jumpToFileInNextSplitAction->setEnabled(m_optionalActions & JumpToFileUnderCursor);
417

mae's avatar
mae committed
418
    m_unfoldAllAction->setEnabled((m_optionalActions & UnCollapseAll));
419
    m_visualizeWhitespaceAction->setChecked(m_currentEditor->displaySettings().m_visualizeWhitespace);
420
    if (m_textWrappingAction)
421
        m_textWrappingAction->setChecked(m_currentEditor->displaySettings().m_textWrapping);
con's avatar
con committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

    updateRedoAction();
    updateUndoAction();
    updateCopyAction();
}

void TextEditorActionHandler::updateRedoAction()
{
    if (m_redoAction)
        m_redoAction->setEnabled(m_currentEditor && m_currentEditor->document()->isRedoAvailable());
}

void TextEditorActionHandler::updateUndoAction()
{
    if (m_undoAction)
        m_undoAction->setEnabled(m_currentEditor && m_currentEditor->document()->isUndoAvailable());
}

void TextEditorActionHandler::updateCopyAction()
{
442
    const bool hasCopyableText = m_currentEditor && m_currentEditor->textCursor().hasSelection();
con's avatar
con committed
443
444
    if (m_cutAction)
        m_cutAction->setEnabled(hasCopyableText && updateMode() == WriteMode);
445
    if (m_copyAction)
con's avatar
con committed
446
447
448
449
450
        m_copyAction->setEnabled(hasCopyableText);
}

void TextEditorActionHandler::gotoAction()
{
con's avatar
con committed
451
    Locator::LocatorManager *locatorManager = Locator::LocatorManager::instance();
con's avatar
con committed
452
    QTC_ASSERT(locatorManager, return);
453
454
455
    QString locatorString = TextEditorPlugin::instance()->lineNumberFilter()->shortcutString();
    locatorString += QLatin1Char(' ');
    const int selectionStart = locatorString.size();
456
    locatorString += tr("<line>:<column>");
457
    locatorManager->show(locatorString, selectionStart, locatorString.size() - selectionStart);
con's avatar
con committed
458
459
460
461
462
}

void TextEditorActionHandler::printAction()
{
    if (m_currentEditor)
hjk's avatar
hjk committed
463
        m_currentEditor->print(Core::ICore::printer());
con's avatar
con committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
}

void TextEditorActionHandler::setVisualizeWhitespace(bool checked)
{
    if (m_currentEditor) {
        DisplaySettings ds = m_currentEditor->displaySettings();
        ds.m_visualizeWhitespace = checked;
        m_currentEditor->setDisplaySettings(ds);
    }
}

void TextEditorActionHandler::setTextWrapping(bool checked)
{
    if (m_currentEditor) {
        DisplaySettings ds = m_currentEditor->displaySettings();
        ds.m_textWrapping = checked;
        m_currentEditor->setDisplaySettings(ds);
    }
}

484
485
486
487
488
489
490
491
492
493
494
#define FUNCTION(funcname) void TextEditorActionHandler::funcname ()\
{\
    if (m_currentEditor)\
        m_currentEditor->funcname ();\
}
#define FUNCTION2(funcname, funcname2) void TextEditorActionHandler::funcname ()\
{\
    if (m_currentEditor)\
        m_currentEditor->funcname2 ();\
}

495
496
497
498
499
500

FUNCTION2(undoAction, undo)
FUNCTION2(redoAction, redo)
FUNCTION2(copyAction, copy)
FUNCTION2(cutAction, cut)
FUNCTION2(pasteAction, paste)
501
FUNCTION2(circularPasteAction, circularPaste)
502
FUNCTION2(switchUtf8bomAction, switchUtf8bom)
503
FUNCTION2(formatAction, format)
504
FUNCTION2(rewrapParagraphAction, rewrapParagraph)
505
506
FUNCTION2(selectAllAction, selectAll)
FUNCTION(cleanWhitespace)
507
FUNCTION(unCommentSelection)
508
FUNCTION(cutLine)
509
FUNCTION(copyLine)
510
FUNCTION(deleteLine)
511
512
513
514
FUNCTION(deleteEndOfWord)
FUNCTION(deleteEndOfWordCamelCase)
FUNCTION(deleteStartOfWord)
FUNCTION(deleteStartOfWordCamelCase)
mae's avatar
mae committed
515
516
517
FUNCTION(unfoldAll)
FUNCTION(fold)
FUNCTION(unfold)
518
519
FUNCTION2(increaseFontSize, zoomIn)
FUNCTION2(decreaseFontSize, zoomOut)
mae's avatar
mae committed
520
FUNCTION2(resetFontSize, zoomReset)
521
522
523
524
525
FUNCTION(selectEncoding)
FUNCTION(gotoBlockStart)
FUNCTION(gotoBlockEnd)
FUNCTION(gotoBlockStartWithSelection)
FUNCTION(gotoBlockEndWithSelection)
526
527
FUNCTION(selectBlockUp)
FUNCTION(selectBlockDown)
528
529
FUNCTION(moveLineUp)
FUNCTION(moveLineDown)
530
531
FUNCTION(copyLineUp)
FUNCTION(copyLineDown)
532
FUNCTION(joinLines)
533
534
FUNCTION(uppercaseSelection)
FUNCTION(lowercaseSelection)
535
536
FUNCTION(insertLineAbove)
FUNCTION(insertLineBelow)
537
538
FUNCTION(indent)
FUNCTION(unindent)
539
FUNCTION(openLinkUnderCursor)
540
FUNCTION(openLinkUnderCursorInNextSplit)
con's avatar
con committed
541

mae's avatar
mae committed
542
543
544
545
546
547
548
549
550
551
552
553
554
555
FUNCTION(gotoLineStart)
FUNCTION(gotoLineStartWithSelection)
FUNCTION(gotoLineEnd)
FUNCTION(gotoLineEndWithSelection)
FUNCTION(gotoNextLine)
FUNCTION(gotoNextLineWithSelection)
FUNCTION(gotoPreviousLine)
FUNCTION(gotoPreviousLineWithSelection)
FUNCTION(gotoPreviousCharacter)
FUNCTION(gotoPreviousCharacterWithSelection)
FUNCTION(gotoNextCharacter)
FUNCTION(gotoNextCharacterWithSelection)
FUNCTION(gotoPreviousWord)
FUNCTION(gotoPreviousWordWithSelection)
mae's avatar
mae committed
556
557
FUNCTION(gotoPreviousWordCamelCase)
FUNCTION(gotoPreviousWordCamelCaseWithSelection)
mae's avatar
mae committed
558
559
FUNCTION(gotoNextWord)
FUNCTION(gotoNextWordWithSelection)
mae's avatar
mae committed
560
561
FUNCTION(gotoNextWordCamelCase)
FUNCTION(gotoNextWordCamelCaseWithSelection)
mae's avatar
mae committed
562
563


564
void TextEditorActionHandler::updateCurrentEditor(Core::IEditor *editor)
con's avatar
con committed
565
{
566
567
568
569
570
    m_currentEditor = 0;

    if (!editor)
        return;

571
    BaseTextEditorWidget *baseEditor = qobject_cast<BaseTextEditorWidget *>(editor->widget());
572
573
574
575
576

    if (baseEditor && baseEditor->actionHack() == this) {
        m_currentEditor = baseEditor;
        updateActions();
    }
con's avatar
con committed
577
578
}

579
const QPointer<BaseTextEditorWidget> &TextEditorActionHandler::currentEditor() const
con's avatar
con committed
580
581
582
{
    return m_currentEditor;
}