summaryrefslogtreecommitdiffstats
path: root/src/kile/editorkeysequencemanager.cpp
blob: 213b959ba7003b3911410a1ce35342b7bd5a23cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
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
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
/**************************************************************************
*   Copyright (C) 2006 by Michel Ludwig (michel.ludwig@kdemail.net)       *
***************************************************************************/

/**************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************/

#include "editorkeysequencemanager.h"

#include "kileinfo.h"
#include "kilejscript.h"

#include <kate/document.h>
#include <klocale.h>

#define MAX(a,b) (a >= b ? a : b)

namespace KileEditorKeySequence {

	Manager::Manager(KileInfo* kileInfo, TQObject *tqparent, const char *name) : TQObject(tqparent, name), m_kileInfo(kileInfo) {
	}

	Manager::~Manager() {
	}

	void Manager::addAction(const TQString& seq, Action *action) {
		if(seq.isEmpty()) {
			return;
		}
		if(m_actionMap.tqfind(seq) == m_actionMap.end()) {
			m_actionMap[seq] = action;
			m_watchedKeySequencesList.push_back(seq);
			emit watchedKeySequencesChanged();
		}
	}


	void Manager::removeKeySequence(const TQString& seq) {
		if(seq.isEmpty()) {
			return;
		}
		TQMap<TQString, Action*>::iterator it = m_actionMap.tqfind(seq);
		if(it != m_actionMap.end()) {
			delete (it.data());
			m_actionMap.remove(it);
			m_watchedKeySequencesList.remove(seq);
			emit watchedKeySequencesChanged();
		}
	}

	void Manager::removeKeySequence(const TQStringList& l) {
		bool changed = false;
		for(TQStringList::const_iterator i = l.begin(); i != l.end(); ++i) {
			if((*i).isEmpty()) {
				continue;
			}
			TQMap<TQString, Action*>::iterator it = m_actionMap.tqfind(*i);
			if(it != m_actionMap.end()) {
				delete (it.data());
				m_actionMap.remove(it);
				m_watchedKeySequencesList.remove(*i);
				changed = true;
			}
		}
		if(changed) {
			emit watchedKeySequencesChanged();
		}
	}

	void Manager::addActionMap(const TQMap<TQString, Action*>& map) {
		bool changed = false;
		for(TQMap<TQString, Action*>::const_iterator i = map.begin(); i != map.end(); ++i) {
			if(i.key().isEmpty()) {
				continue;
			}
			if(m_actionMap[i.key()] != i.data()) {
				m_actionMap[i.key()] = i.data();
				changed = true;
			}
		}
		if(changed) {
			emit watchedKeySequencesChanged();
		}
	}

	TQString Manager::getKeySequence(const Action* a) {
		for(TQMap<TQString, Action*>::const_iterator i = m_actionMap.begin(); i != m_actionMap.end(); ++i) {
			if(i.data() == a) {
				return i.key();
			}
		}
		return TQString();
	}

	Action* Manager::getAction(const TQString& seq) {
		TQMap<TQString, Action*>::iterator i = m_actionMap.tqfind(seq);
		return (i == m_actionMap.end()) ? 0L : (*i);
	}

	void Manager::setEditorKeySequence(const TQString& /* seq */, Action* /* action */) {
	}

	void Manager::keySequenceTyped(const TQString& seq) {
		m_actionMap[seq]->execute();
	}

	void Manager::clear() {
		m_watchedKeySequencesList.clear();
		m_actionMap.clear();
		emit watchedKeySequencesChanged();
	}


	const TQStringList& Manager::getWatchedKeySequences() {
		return m_watchedKeySequencesList;
	}

	bool Manager::isSequenceAssigned(const TQString& seq) const {
		for(TQValueList<TQString>::const_iterator i = m_watchedKeySequencesList.begin(); i != m_watchedKeySequencesList.end(); ++i) {
			if((*i).startsWith(seq)) {
				return true;
			}
		}
		return false;
	}

	TQPair<int, TQString> Manager::checkSequence(const TQString& seq, const TQString& skip) {
		for(TQValueList<TQString>::iterator i = m_watchedKeySequencesList.begin(); i != m_watchedKeySequencesList.end(); ++i) {
			if((*i) == skip) {
				continue;
			}
			if((*i).startsWith(seq)) {
				return (*i == seq) ? tqMakePair<int, TQString>(1, seq) : tqMakePair<int, TQString>(2, *i);
			}
 			if(!(*i).isEmpty() && seq.startsWith(*i)) {
				return tqMakePair<int, TQString>(3, *i);
			}
		}
		return tqMakePair<int, TQString>(0, TQString());
	}

Recorder::Recorder(Kate::View *view, Manager *manager) : TQObject(view), m_manager(manager), m_view(view) {
	connect(m_manager, TQT_SIGNAL(watchedKeySequencesChanged()), this, TQT_SLOT(reloadWatchedKeySequences()));
	connect(this, TQT_SIGNAL(detectedTypedKeySequence(const TQString&)), m_manager, TQT_SLOT(keySequenceTyped(const TQString&)));
	m_view->cursorPositionReal(&m_oldLine, &m_oldCol);
	reloadWatchedKeySequences();
}

Recorder::~Recorder() {
}

bool Recorder::eventFilter(TQObject* /* o */, TQEvent *e) {
	if (e->type() == TQEvent::KeyPress) {
		TQKeyEvent *keyEvent = (TQKeyEvent*)(e);
		uint curLine, curCol;
		m_view->cursorPositionReal(&curLine, &curCol);
		if(curLine != m_oldLine || m_oldCol+1 != curCol) {
			m_typedSequence = TQString();
			m_oldLine = curLine;
			m_oldCol = curCol;
		}
		else {
			++m_oldCol;
		}
		m_typedSequence += keyEvent->text();
		if(m_typedSequence.length() == m_maxSequenceLength+1) {
			m_typedSequence = m_typedSequence.mid(1, m_typedSequence.length() - 1);
		}
		return seekForKeySequence(m_typedSequence);
	}
	return false;
}

	bool Recorder::seekForKeySequence(const TQString& s) {
		for(uint i = 0; i < s.length(); ++i) {
			TQString toCheck = s.right(s.length() - i);
			if(m_watchedKeySequencesList.tqcontains(toCheck) > 0) {
 				m_view->getDoc()->removeText(m_oldLine, m_oldCol-(s.length() - i - 1), m_oldLine, m_oldCol);
				m_typedSequence = TQString(); // clean m_typedSequence to avoid wrong action triggering if one presses keys without printable character
				emit detectedTypedKeySequence(toCheck);
				return true;
			}
		}
		return false;
	}

	void Recorder::reloadWatchedKeySequences() {
		m_watchedKeySequencesList = m_manager->getWatchedKeySequences();
		m_maxSequenceLength = 0;
		for(TQStringList::iterator i = m_watchedKeySequencesList.begin(); i != m_watchedKeySequencesList.end(); ++i) {
			m_maxSequenceLength = MAX(m_maxSequenceLength, (*i).length());
		}
		if(m_maxSequenceLength < m_typedSequence.length()) {
			m_typedSequence = m_typedSequence.right(m_maxSequenceLength);
		}
	}

	Action::Action() {
	}

	Action::~Action() {
	}

	TQString Action::getDescription() const {
		return TQString();
	}

	ExecuteJScriptAction::ExecuteJScriptAction(KileJScript::JScript *jScript, KileJScript::Manager *jScriptManager) : m_jScript(jScript), m_jScriptManager(jScriptManager){
	}

	ExecuteJScriptAction::~ExecuteJScriptAction() {
	}

	void ExecuteJScriptAction::execute() {
		m_jScriptManager->executeJScript(m_jScript);
	}

	TQString ExecuteJScriptAction::getDescription() const {
		return i18n("Script execution of %1").tqarg(m_jScript->getFileName());
	}


}

#include "editorkeysequencemanager.moc"