texteditoractionhandler.cpp 24.6 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

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
43
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
con's avatar
con committed
44
#include <coreplugin/editormanager/editormanager.h>
hjk's avatar
hjk committed
45
#include <utils/qtcassert.h>
con's avatar
con committed
46
47
48
49
50
51
52
53
54

#include <QtCore/QSet>
#include <QtCore/QtDebug>
#include <QtGui/QAction>
#include <QtGui/QTextCursor>

using namespace TextEditor;
using namespace TextEditor::Internal;

55
TextEditorActionHandler::TextEditorActionHandler(const char *context,
56
                                                 uint optionalActions)
57
  : QObject(Core::ICore::instance()),
58
59
60
61
62
63
64
65
66
67
68
69
70
    m_undoAction(0),
    m_redoAction(0),
    m_copyAction(0),
    m_cutAction(0),
    m_pasteAction(0),
    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
71
72
73
    m_unfoldAllAction(0),
    m_foldAction(0),
    m_unfoldAction(0),
74
75
76
77
78
    m_cutLineAction(0),
    m_deleteLineAction(0),
    m_selectEncodingAction(0),
    m_increaseFontSizeAction(0),
    m_decreaseFontSizeAction(0),
mae's avatar
mae committed
79
    m_resetFontSizeAction(0),
80
81
82
83
84
85
86
87
88
89
    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),
90
    m_joinLinesAction(0),
con's avatar
con committed
91
92
    m_optionalActions(optionalActions),
    m_currentEditor(0),
93
    m_contextId(context),
con's avatar
con committed
94
95
    m_initialized(false)
{
96
    connect(Core::ICore::instance()->editorManager(), SIGNAL(currentEditorChanged(Core::IEditor*)),
97
        this, SLOT(updateCurrentEditor(Core::IEditor*)));
con's avatar
con committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
}

void TextEditorActionHandler::setupActions(BaseTextEditor *editor)
{
    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()
{
    m_undoAction      = registerNewAction(QLatin1String(Core::Constants::UNDO),      this, SLOT(undoAction()),
                                          tr("&Undo"));
    m_redoAction      = registerNewAction(QLatin1String(Core::Constants::REDO),      this, SLOT(redoAction()),
                                          tr("&Redo"));
    m_copyAction      = registerNewAction(QLatin1String(Core::Constants::COPY),      this, SLOT(copyAction()));
    m_cutAction       = registerNewAction(QLatin1String(Core::Constants::CUT),       this, SLOT(cutAction()));
    m_pasteAction     = registerNewAction(QLatin1String(Core::Constants::PASTE),     this, SLOT(pasteAction()));
    m_selectAllAction = registerNewAction(QLatin1String(Core::Constants::SELECTALL), this, SLOT(selectAllAction()));
    m_gotoAction      = registerNewAction(QLatin1String(Core::Constants::GOTO),      this, SLOT(gotoAction()));
    m_printAction     = registerNewAction(QLatin1String(Core::Constants::PRINT),     this, SLOT(printAction()));

131
    Core::ActionManager *am = Core::ICore::instance()->actionManager();
con's avatar
con committed
132

133
134
    Core::ActionContainer *medit = am->actionContainer(Core::Constants::M_EDIT);
    Core::ActionContainer *advancedMenu = am->actionContainer(Core::Constants::M_EDIT_ADVANCED);
con's avatar
con committed
135
136

    m_selectEncodingAction = new QAction(tr("Select Encoding..."), this);
con's avatar
con committed
137
    Core::Command *command = am->registerAction(m_selectEncodingAction, Constants::SELECT_ENCODING, m_contextId);
con's avatar
con committed
138
139
140
141
142
143
144
    connect(m_selectEncodingAction, SIGNAL(triggered()), this, SLOT(selectEncoding()));
    medit->addAction(command, Core::Constants::G_EDIT_OTHER);


    m_formatAction = new QAction(tr("Auto-&indent Selection"), this);
    command = am->registerAction(m_formatAction, TextEditor::Constants::AUTO_INDENT_SELECTION, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+I")));
145
    advancedMenu->addAction(command, Core::Constants::G_EDIT_FORMAT);
con's avatar
con committed
146
147
    connect(m_formatAction, SIGNAL(triggered(bool)), this, SLOT(formatAction()));

con's avatar
con committed
148
149
150
151
152
153
#ifdef Q_WS_MAC
    QString modifier = tr("Meta");
#else
    QString modifier = tr("Ctrl");
#endif

154
155
    m_rewrapParagraphAction = new QAction(tr("&Rewrap Paragraph"), this);
    command = am->registerAction(m_rewrapParagraphAction, TextEditor::Constants::REWRAP_PARAGRAPH, m_contextId);
con's avatar
con committed
156
    command->setDefaultKeySequence(QKeySequence(tr("%1+E, R").arg(modifier)));
157
158
159
    advancedMenu->addAction(command, Core::Constants::G_EDIT_FORMAT);
    connect(m_rewrapParagraphAction, SIGNAL(triggered(bool)), this, SLOT(rewrapParagraphAction()));

con's avatar
con committed
160

161
    m_visualizeWhitespaceAction = new QAction(tr("&Visualize Whitespace"), this);
con's avatar
con committed
162
163
    m_visualizeWhitespaceAction->setCheckable(true);
    command = am->registerAction(m_visualizeWhitespaceAction,
164
                                 TextEditor::Constants::VISUALIZE_WHITESPACE, m_contextId);
con's avatar
con committed
165
    command->setDefaultKeySequence(QKeySequence(tr("%1+E, %2+V").arg(modifier, modifier)));
166
    advancedMenu->addAction(command, Core::Constants::G_EDIT_FORMAT);
con's avatar
con committed
167
168
    connect(m_visualizeWhitespaceAction, SIGNAL(triggered(bool)), this, SLOT(setVisualizeWhitespace(bool)));

169
170
171
172
    m_cleanWhitespaceAction = new QAction(tr("Clean Whitespace"), this);
    command = am->registerAction(m_cleanWhitespaceAction,
                                 TextEditor::Constants::CLEAN_WHITESPACE, m_contextId);

173
    advancedMenu->addAction(command, Core::Constants::G_EDIT_FORMAT);
174
175
    connect(m_cleanWhitespaceAction, SIGNAL(triggered()), this, SLOT(cleanWhitespace()));

con's avatar
con committed
176
177
    m_textWrappingAction = new QAction(tr("Enable Text &Wrapping"), this);
    m_textWrappingAction->setCheckable(true);
178
    command = am->registerAction(m_textWrappingAction, TextEditor::Constants::TEXT_WRAPPING, m_contextId);
con's avatar
con committed
179
    command->setDefaultKeySequence(QKeySequence(tr("%1+E, %2+W").arg(modifier, modifier)));
180
    advancedMenu->addAction(command, Core::Constants::G_EDIT_FORMAT);
con's avatar
con committed
181
182
183
184
185
186
187
    connect(m_textWrappingAction, SIGNAL(triggered(bool)), this, SLOT(setTextWrapping(bool)));


    m_unCommentSelectionAction = new QAction(tr("(Un)Comment &Selection"), this);
    command = am->registerAction(m_unCommentSelectionAction, Constants::UN_COMMENT_SELECTION, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+/")));
    connect(m_unCommentSelectionAction, SIGNAL(triggered()), this, SLOT(unCommentSelection()));
188
    advancedMenu->addAction(command, Core::Constants::G_EDIT_FORMAT);
con's avatar
con committed
189

190
191
192
193
194
    m_cutLineAction = new QAction(tr("Cut &Line"), this);
    command = am->registerAction(m_cutLineAction, Constants::CUT_LINE, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Shift+Del")));
    connect(m_cutLineAction, SIGNAL(triggered()), this, SLOT(cutLine()));

con's avatar
con committed
195
196
197
198
    m_deleteLineAction = new QAction(tr("Delete &Line"), this);
    command = am->registerAction(m_deleteLineAction, Constants::DELETE_LINE, m_contextId);
    connect(m_deleteLineAction, SIGNAL(triggered()), this, SLOT(deleteLine()));

mae's avatar
mae committed
199
200
    m_foldAction = new QAction(tr("Fold"), this);
    command = am->registerAction(m_foldAction, Constants::FOLD, m_contextId);
con's avatar
con committed
201
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+<")));
mae's avatar
mae committed
202
    connect(m_foldAction, SIGNAL(triggered()), this, SLOT(fold()));
203
    advancedMenu->addAction(command, Core::Constants::G_EDIT_COLLAPSING);
con's avatar
con committed
204

mae's avatar
mae committed
205
206
    m_unfoldAction = new QAction(tr("Unfold"), this);
    command = am->registerAction(m_unfoldAction, Constants::UNFOLD, m_contextId);
con's avatar
con committed
207
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+>")));
mae's avatar
mae committed
208
    connect(m_unfoldAction, SIGNAL(triggered()), this, SLOT(unfold()));
209
    advancedMenu->addAction(command, Core::Constants::G_EDIT_COLLAPSING);
con's avatar
con committed
210

mae's avatar
mae committed
211
    m_unfoldAllAction = new QAction(tr("(Un)&Fold All"), this);
mae's avatar
mae committed
212
213
    command = am->registerAction(m_unfoldAllAction, Constants::UNFOLD_ALL, m_contextId);
    connect(m_unfoldAllAction, SIGNAL(triggered()), this, SLOT(unfoldAll()));
214
    advancedMenu->addAction(command, Core::Constants::G_EDIT_COLLAPSING);
con's avatar
con committed
215
216
217
218
219

    m_increaseFontSizeAction = new QAction(tr("Increase Font Size"), this);
    command = am->registerAction(m_increaseFontSizeAction, Constants::INCREASE_FONT_SIZE, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl++")));
    connect(m_increaseFontSizeAction, SIGNAL(triggered()), this, SLOT(increaseFontSize()));
220
    advancedMenu->addAction(command, Core::Constants::G_EDIT_FONT);
221

con's avatar
con committed
222
223
224
225
    m_decreaseFontSizeAction = new QAction(tr("Decrease Font Size"), this);
    command = am->registerAction(m_decreaseFontSizeAction, Constants::DECREASE_FONT_SIZE, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+-")));
    connect(m_decreaseFontSizeAction, SIGNAL(triggered()), this, SLOT(decreaseFontSize()));
226
    advancedMenu->addAction(command, Core::Constants::G_EDIT_FONT);
227

mae's avatar
mae committed
228
229
    m_resetFontSizeAction = new QAction(tr("Reset Font Size"), this);
    command = am->registerAction(m_resetFontSizeAction, Constants::RESET_FONT_SIZE, m_contextId);
230
#ifndef Q_WS_MAC
mae's avatar
mae committed
231
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+0")));
232
#endif
mae's avatar
mae committed
233
234
235
    connect(m_resetFontSizeAction, SIGNAL(triggered()), this, SLOT(resetFontSize()));
    advancedMenu->addAction(command, Core::Constants::G_EDIT_FONT);

con's avatar
con committed
236
    m_gotoBlockStartAction = new QAction(tr("Go to Block Start"), this);
237
238
239
    command = am->registerAction(m_gotoBlockStartAction, Constants::GOTO_BLOCK_START, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+[")));
    connect(m_gotoBlockStartAction, SIGNAL(triggered()), this, SLOT(gotoBlockStart()));
mae's avatar
mae committed
240
    advancedMenu->addAction(command, Core::Constants::G_EDIT_BLOCKS);
241

con's avatar
con committed
242
    m_gotoBlockEndAction = new QAction(tr("Go to Block End"), this);
243
244
245
    command = am->registerAction(m_gotoBlockEndAction, Constants::GOTO_BLOCK_END, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+]")));
    connect(m_gotoBlockEndAction, SIGNAL(triggered()), this, SLOT(gotoBlockEnd()));
mae's avatar
mae committed
246
    advancedMenu->addAction(command, Core::Constants::G_EDIT_BLOCKS);
247

con's avatar
con committed
248
    m_gotoBlockStartWithSelectionAction = new QAction(tr("Go to Block Start With Selection"), this);
249
250
251
252
    command = am->registerAction(m_gotoBlockStartWithSelectionAction, Constants::GOTO_BLOCK_START_WITH_SELECTION, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+{")));
    connect(m_gotoBlockStartWithSelectionAction, SIGNAL(triggered()), this, SLOT(gotoBlockStartWithSelection()));

con's avatar
con committed
253
    m_gotoBlockEndWithSelectionAction = new QAction(tr("Go to Block End With Selection"), this);
254
255
256
257
    command = am->registerAction(m_gotoBlockEndWithSelectionAction, Constants::GOTO_BLOCK_END_WITH_SELECTION, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+}")));
    connect(m_gotoBlockEndWithSelectionAction, SIGNAL(triggered()), this, SLOT(gotoBlockEndWithSelection()));

hjk's avatar
hjk committed
258
    m_selectBlockUpAction = new QAction(tr("Select Block Up"), this);
259
260
261
    command = am->registerAction(m_selectBlockUpAction, Constants::SELECT_BLOCK_UP, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+U")));
    connect(m_selectBlockUpAction, SIGNAL(triggered()), this, SLOT(selectBlockUp()));
mae's avatar
mae committed
262
    advancedMenu->addAction(command, Core::Constants::G_EDIT_BLOCKS);
263

hjk's avatar
hjk committed
264
    m_selectBlockDownAction = new QAction(tr("Select Block Down"), this);
265
266
    command = am->registerAction(m_selectBlockDownAction, Constants::SELECT_BLOCK_DOWN, m_contextId);
    connect(m_selectBlockDownAction, SIGNAL(triggered()), this, SLOT(selectBlockDown()));
mae's avatar
mae committed
267
    advancedMenu->addAction(command, Core::Constants::G_EDIT_BLOCKS);
268

hjk's avatar
hjk committed
269
    m_moveLineUpAction = new QAction(tr("Move Line Up"), this);
270
271
272
273
    command = am->registerAction(m_moveLineUpAction, Constants::MOVE_LINE_UP, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+Up")));
    connect(m_moveLineUpAction, SIGNAL(triggered()), this, SLOT(moveLineUp()));

hjk's avatar
hjk committed
274
    m_moveLineDownAction = new QAction(tr("Move Line Down"), this);
275
276
277
    command = am->registerAction(m_moveLineDownAction, Constants::MOVE_LINE_DOWN, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+Down")));
    connect(m_moveLineDownAction, SIGNAL(triggered()), this, SLOT(moveLineDown()));
278

hjk's avatar
hjk committed
279
    m_copyLineUpAction = new QAction(tr("Copy Line Up"), this);
280
281
282
283
    command = am->registerAction(m_copyLineUpAction, Constants::COPY_LINE_UP, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+Up")));
    connect(m_copyLineUpAction, SIGNAL(triggered()), this, SLOT(copyLineUp()));

hjk's avatar
hjk committed
284
    m_copyLineDownAction = new QAction(tr("Copy Line Down"), this);
285
286
287
    command = am->registerAction(m_copyLineDownAction, Constants::COPY_LINE_DOWN, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+Down")));
    connect(m_copyLineDownAction, SIGNAL(triggered()), this, SLOT(copyLineDown()));
288

hjk's avatar
hjk committed
289
    m_joinLinesAction = new QAction(tr("Join Lines"), this);
290
    command = am->registerAction(m_joinLinesAction, Constants::JOIN_LINES, m_contextId);
291
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+J")));
292
    connect(m_joinLinesAction, SIGNAL(triggered()), this, SLOT(joinLines()));
293
294
295
296
297
298
299
300

    m_insertLineAboveAction = new QAction(tr("Insert Line Above Current Line"), this);
    command = am->registerAction(m_insertLineAboveAction, Constants::INSERT_LINE_ABOVE, m_contextId);
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+Return")));
    connect(m_insertLineAboveAction, SIGNAL(triggered()), this, SLOT(insertLineAbove()));

    m_insertLineBelowAction = new QAction(tr("Insert Line Below Current Line"), this);
    command = am->registerAction(m_insertLineBelowAction, Constants::INSERT_LINE_BELOW, m_contextId);
301
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Return")));
302
    connect(m_insertLineBelowAction, SIGNAL(triggered()), this, SLOT(insertLineBelow()));
mae's avatar
mae committed
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

    QAction *a = 0;
    a = new QAction(tr("Goto Line Start"), this);
    command = am->registerAction(a, Constants::GOTO_LINE_START, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoLineStart()));
    a = new QAction(tr("Goto Line End"), this);
    command = am->registerAction(a, Constants::GOTO_LINE_END, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoLineEnd()));
    a = new QAction(tr("Goto Next Line"), this);
    command = am->registerAction(a, Constants::GOTO_NEXT_LINE, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoNextLine()));
    a = new QAction(tr("Goto Previous Line"), this);
    command = am->registerAction(a, Constants::GOTO_PREVIOUS_LINE, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoPreviousLine()));
    a = new QAction(tr("Goto Previous Character"), this);
    command = am->registerAction(a, Constants::GOTO_PREVIOUS_CHARACTER, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoPreviousCharacter()));
    a = new QAction(tr("Goto Next Character"), this);
    command = am->registerAction(a, Constants::GOTO_NEXT_CHARACTER, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoNextCharacter()));
    a = new QAction(tr("Goto Previous Word"), this);
    command = am->registerAction(a, Constants::GOTO_PREVIOUS_WORD, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoPreviousWord()));
    a = new QAction(tr("Goto Next Word"), this);
    command = am->registerAction(a, Constants::GOTO_NEXT_WORD, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoNextWord()));

    a = new QAction(tr("Goto Line Start With Selection"), this);
    command = am->registerAction(a, Constants::GOTO_LINE_START_WITH_SELECTION, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoLineStartWithSelection()));
    a = new QAction(tr("Goto Line End With Selection"), this);
    command = am->registerAction(a, Constants::GOTO_LINE_END_WITH_SELECTION, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoLineEndWithSelection()));
    a = new QAction(tr("Goto Next Line With Selection"), this);
    command = am->registerAction(a, Constants::GOTO_NEXT_LINE_WITH_SELECTION, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoNextLineWithSelection()));
    a = new QAction(tr("Goto Previous Line With Selection"), this);
    command = am->registerAction(a, Constants::GOTO_PREVIOUS_LINE_WITH_SELECTION, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoPreviousLineWithSelection()));
    a = new QAction(tr("Goto Previous Character With Selection"), this);
    command = am->registerAction(a, Constants::GOTO_PREVIOUS_CHARACTER_WITH_SELECTION, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoPreviousCharacterWithSelection()));
    a = new QAction(tr("Goto Next Character With Selection"), this);
    command = am->registerAction(a, Constants::GOTO_NEXT_CHARACTER_WITH_SELECTION, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoNextCharacterWithSelection()));
    a = new QAction(tr("Goto Previous Word With Selection"), this);
    command = am->registerAction(a, Constants::GOTO_PREVIOUS_WORD_WITH_SELECTION, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoPreviousWordWithSelection()));
    a = new QAction(tr("Goto Next Word With Selection"), this);
    command = am->registerAction(a, Constants::GOTO_NEXT_WORD_WITH_SELECTION, m_contextId);
    connect(a, SIGNAL(triggered()), this, SLOT(gotoNextWordWithSelection()));

con's avatar
con committed
355
356
357
358
359
360
361
362
363
364
365
366
367
}

bool TextEditorActionHandler::supportsAction(const QString & /*id */) const
{
    return true;
}

QAction *TextEditorActionHandler::registerNewAction(const QString &id, const QString &title)
{
    if (!supportsAction(id))
        return 0;

    QAction *result = new QAction(title, this);
368
    Core::ICore::instance()->actionManager()->registerAction(result, id, m_contextId);
con's avatar
con committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
    return result;
}

QAction *TextEditorActionHandler::registerNewAction(const QString &id,
                                                    QObject *receiver,
                                                    const char *slot,
                                                    const QString &title)
{
    QAction *rc = registerNewAction(id, title);
    if (!rc)
        return 0;

    connect(rc, SIGNAL(triggered()), receiver, slot);
    return rc;
}

TextEditorActionHandler::UpdateMode TextEditorActionHandler::updateMode() const
{
387
    Q_ASSERT(m_currentEditor != 0);
con's avatar
con committed
388
389
390
391
392
    return m_currentEditor->file()->isReadOnly() ? ReadOnlyMode : WriteMode;
}

void TextEditorActionHandler::updateActions()
{
393
394
    if (!m_currentEditor || !m_initialized)
        return;
con's avatar
con committed
395
396
397
398
399
    updateActions(updateMode());
}

void TextEditorActionHandler::updateActions(UpdateMode um)
{
400
401
402
403
404
405
406
    m_pasteAction->setEnabled(um != ReadOnlyMode);
    m_formatAction->setEnabled((m_optionalActions & Format) && um != ReadOnlyMode);
    m_unCommentSelectionAction->setEnabled((m_optionalActions & UnCommentSelection) && um != ReadOnlyMode);
    m_moveLineUpAction->setEnabled(um != ReadOnlyMode);
    m_moveLineDownAction->setEnabled(um != ReadOnlyMode);

    m_formatAction->setEnabled((m_optionalActions & Format));
mae's avatar
mae committed
407
    m_unfoldAllAction->setEnabled((m_optionalActions & UnCollapseAll));
408
    m_visualizeWhitespaceAction->setChecked(m_currentEditor->displaySettings().m_visualizeWhitespace);
con's avatar
con committed
409
    if (m_textWrappingAction) {
410
        m_textWrappingAction->setChecked(m_currentEditor->displaySettings().m_textWrapping);
con's avatar
con committed
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
    }

    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()
{
432
    const bool hasCopyableText = m_currentEditor && m_currentEditor->textCursor().hasSelection();
con's avatar
con committed
433
434
    if (m_cutAction)
        m_cutAction->setEnabled(hasCopyableText && updateMode() == WriteMode);
435
    if (m_copyAction) {
con's avatar
con committed
436
        m_copyAction->setEnabled(hasCopyableText);
437
    }
con's avatar
con committed
438
439
440
441
}

void TextEditorActionHandler::gotoAction()
{
con's avatar
con committed
442
    Locator::LocatorManager *locatorManager = Locator::LocatorManager::instance();
con's avatar
con committed
443
    QTC_ASSERT(locatorManager, return);
444
445
446
447
448
    QString locatorString = TextEditorPlugin::instance()->lineNumberFilter()->shortcutString();
    locatorString += QLatin1Char(' ');
    const int selectionStart = locatorString.size();
    locatorString += tr("<line number>");
    locatorManager->show(locatorString, selectionStart, locatorString.size() - selectionStart);
con's avatar
con committed
449
450
451
452
453
}

void TextEditorActionHandler::printAction()
{
    if (m_currentEditor)
454
        m_currentEditor->print(Core::ICore::instance()->printer());
con's avatar
con committed
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
}

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

475
476
477
478
479
480
481
482
483
484
485
#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 ();\
}

486
487
488
489
490
491
492

FUNCTION2(undoAction, undo)
FUNCTION2(redoAction, redo)
FUNCTION2(copyAction, copy)
FUNCTION2(cutAction, cut)
FUNCTION2(pasteAction, paste)
FUNCTION2(formatAction, format)
493
FUNCTION2(rewrapParagraphAction, rewrapParagraph)
494
495
FUNCTION2(selectAllAction, selectAll)
FUNCTION(cleanWhitespace)
496
FUNCTION(unCommentSelection)
497
FUNCTION(cutLine)
498
FUNCTION(deleteLine)
mae's avatar
mae committed
499
500
501
FUNCTION(unfoldAll)
FUNCTION(fold)
FUNCTION(unfold)
502
503
FUNCTION2(increaseFontSize, zoomIn)
FUNCTION2(decreaseFontSize, zoomOut)
mae's avatar
mae committed
504
FUNCTION2(resetFontSize, zoomReset)
505
506
507
508
509
FUNCTION(selectEncoding)
FUNCTION(gotoBlockStart)
FUNCTION(gotoBlockEnd)
FUNCTION(gotoBlockStartWithSelection)
FUNCTION(gotoBlockEndWithSelection)
510
511
FUNCTION(selectBlockUp)
FUNCTION(selectBlockDown)
512
513
FUNCTION(moveLineUp)
FUNCTION(moveLineDown)
514
515
FUNCTION(copyLineUp)
FUNCTION(copyLineDown)
516
FUNCTION(joinLines)
517
518
FUNCTION(insertLineAbove)
FUNCTION(insertLineBelow)
con's avatar
con committed
519

mae's avatar
mae committed
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
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)
FUNCTION(gotoNextWord)
FUNCTION(gotoNextWordWithSelection)


538
void TextEditorActionHandler::updateCurrentEditor(Core::IEditor *editor)
con's avatar
con committed
539
{
540
541
542
543
544
545
546
547
548
549
550
    m_currentEditor = 0;

    if (!editor)
        return;

    BaseTextEditor *baseEditor = qobject_cast<BaseTextEditor *>(editor->widget());

    if (baseEditor && baseEditor->actionHack() == this) {
        m_currentEditor = baseEditor;
        updateActions();
    }
con's avatar
con committed
551
552
553
554
555
556
}

const QPointer<BaseTextEditor> &TextEditorActionHandler::currentEditor() const
{
    return m_currentEditor;
}