OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / macros / texteditormacrohandler.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Nicolas Arnaud-Cormos.
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "texteditormacrohandler.h"
34 #include "macroevent.h"
35 #include "macro.h"
36
37 #include <texteditor/itexteditor.h>
38 #include <texteditor/texteditorconstants.h>
39
40 #include <coreplugin/icore.h>
41 #include <coreplugin/editormanager/editormanager.h>
42 #include <coreplugin/editormanager/ieditor.h>
43 #include <coreplugin/actionmanager/actionmanager.h>
44 #include <coreplugin/actionmanager/command.h>
45 #include <coreplugin/uniqueidmanager.h>
46
47 #include <QtGui/QWidget>
48 #include <QtGui/QKeyEvent>
49 #include <QtGui/QApplication>
50 #include <QtGui/QShortcut>
51
52 using namespace Macros;
53 using namespace Macros::Internal;
54
55 static const char KEYEVENTNAME[] = "TextEditorKey";
56 static quint8 TEXT = 0;
57 static quint8 TYPE = 1;
58 static quint8 MODIFIERS = 2;
59 static quint8 KEY = 3;
60 static quint8 AUTOREP = 4;
61 static quint8 COUNT = 5;
62
63
64 TextEditorMacroHandler::TextEditorMacroHandler():
65     IMacroHandler()
66 {
67     const Core::EditorManager *editorManager = Core::EditorManager::instance();
68     connect(editorManager, SIGNAL(currentEditorChanged(Core::IEditor*)),
69             this, SLOT(changeEditor(Core::IEditor*)));
70     connect(editorManager, SIGNAL(editorAboutToClose(Core::IEditor*)),
71             this, SLOT(closeEditor(Core::IEditor*)));
72 }
73
74 void TextEditorMacroHandler::startRecording(Macros::Macro *macro)
75 {
76     IMacroHandler::startRecording(macro);
77     if (isRecording() && m_currentEditor && m_currentEditor->widget())
78         m_currentEditor->widget()->installEventFilter(this);
79
80     // Block completion
81     Core::ActionManager *am = Core::ICore::instance()->actionManager();
82     am->command(TextEditor::Constants::COMPLETE_THIS)->shortcut()->blockSignals(true);
83 }
84
85 void TextEditorMacroHandler::endRecordingMacro(Macros::Macro *macro)
86 {
87     if (m_currentEditor && m_currentEditor->widget())
88         m_currentEditor->widget()->removeEventFilter(this);
89     IMacroHandler::endRecordingMacro(macro);
90
91     // Unblock completion
92     Core::ActionManager *am = Core::ICore::instance()->actionManager();
93     am->command(TextEditor::Constants::COMPLETE_THIS)->shortcut()->blockSignals(false);
94 }
95
96 bool TextEditorMacroHandler::canExecuteEvent(const MacroEvent &macroEvent)
97 {
98     return (macroEvent.id() == KEYEVENTNAME);
99 }
100
101 bool TextEditorMacroHandler::executeEvent(const MacroEvent &macroEvent)
102 {
103     if (!m_currentEditor)
104         return false;
105
106     QKeyEvent keyEvent((QEvent::Type)macroEvent.value(TYPE).toInt(),
107                        macroEvent.value(KEY).toInt(),
108                        (Qt::KeyboardModifiers)macroEvent.value(MODIFIERS).toInt(),
109                        macroEvent.value(TEXT).toString(),
110                        macroEvent.value(AUTOREP).toBool(),
111                        macroEvent.value(COUNT).toInt());
112     QApplication::instance()->sendEvent(m_currentEditor->widget(), &keyEvent);
113     return true;
114 }
115
116 bool TextEditorMacroHandler::eventFilter(QObject *watched, QEvent *event)
117 {
118     Q_UNUSED(watched)
119
120     if (!isRecording())
121         return false;
122
123     if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
124         QKeyEvent *keyEvent = dynamic_cast<QKeyEvent *>(event);
125         MacroEvent e;
126         e.setId(KEYEVENTNAME);
127         e.setValue(TEXT, keyEvent->text());
128         e.setValue(TYPE, keyEvent->type());
129         e.setValue(MODIFIERS, (int)keyEvent->modifiers());
130         e.setValue(KEY, keyEvent->key());
131         e.setValue(AUTOREP, keyEvent->isAutoRepeat());
132         e.setValue(COUNT, keyEvent->count());
133         addMacroEvent(e);
134     }
135     return false;
136 }
137
138 void TextEditorMacroHandler::changeEditor(Core::IEditor *editor)
139 {
140     if (isRecording() && m_currentEditor && m_currentEditor->widget())
141         m_currentEditor->widget()->removeEventFilter(this);
142
143     m_currentEditor = qobject_cast<TextEditor::ITextEditor *>(editor);
144     if (isRecording() && m_currentEditor && m_currentEditor->widget())
145         m_currentEditor->widget()->installEventFilter(this);
146 }
147
148 void TextEditorMacroHandler::closeEditor(Core::IEditor *editor)
149 {
150     Q_UNUSED(editor);
151     if (isRecording() && m_currentEditor && m_currentEditor->widget())
152         m_currentEditor->widget()->removeEventFilter(this);
153     m_currentEditor = 0;
154 }