File: editSingleShortcut.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (225 lines) | stat: -rw-r--r-- 4,891 bytes parent folder | download | duplicates (4)
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/VIEW/DIALOGS/editSingleShortcut.h>
#include <BALL/VIEW/KERNEL/shortcutRegistry.h>
#include <BALL/VIEW/KERNEL/common.h>
#include <BALL/VIEW/KERNEL/mainControl.h>
#include <BALL/SYSTEM/path.h>
#include <QtWidgets/QDialog>

namespace BALL
{
	namespace VIEW
	{

		EditSingleShortcut::EditSingleShortcut(QWidget* parent, const char* name, Qt::WindowFlags)
			: QDialog(parent),
				Ui_EditSingleShortcutData(),
				is_recording_(false),
			  modifiers_(0),
				key_(0)
		{
			// apply the dialogs layout
			setupUi(this);

			setObjectName(name);
			setAutoFillBackground(true);

			// signals and slots connections
			new_shortcut_label->setText("Please type key sequence!");

			connect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
			connect( buttonBox, SIGNAL( rejected() ), this, SLOT( reject() ) );

			connect(customize_button, SIGNAL(toggled(bool)), this, SLOT(modeChanged_(bool)));
			connect(none_button,      SIGNAL(toggled(bool)), this, SLOT(modeChanged_(bool)));

			setShortcutText("Please insert your shortcut now.");
		}

		void EditSingleShortcut::accept()
		{
			stopRecording_();
			QDialog::accept();
		}

		void EditSingleShortcut::reject()
		{
			stopRecording_();
			QDialog::reject();
		}

		void EditSingleShortcut::setup(const QString& seq)
		{
			new_sequence_ = QKeySequence(seq);

			updateText_();

			setErrorText("");
			key_ = 0;
			modifiers_ = 0;

			if(!new_sequence_.isEmpty()) {
				customize_button->setChecked(true);
				none_button->setChecked(false);
			} else {
				customize_button->setChecked(false);
				none_button->setChecked(true);
			}

			changeMode_(new_sequence_ != QKeySequence());
		}

		void EditSingleShortcut::modeChanged_(bool toggled)
		{
			if(toggled) {
				changeMode_(customize_button->isChecked());
			}
		}

		void EditSingleShortcut::changeMode_(bool mode)
		{
			if(mode) {
				if(new_sequence_.isEmpty()) {
					setShortcutText("Please insert your shortcut.");
				}
				startRecording_();
			} else {
				key_ = 0;
				modifiers_ = 0;
				setShortcutText("None");
				stopRecording_();
				new_sequence_ = QKeySequence();
			}
		}

		void EditSingleShortcut::setShortcutText(QString new_keysequence)
		{
			new_shortcut_label->setText(new_keysequence);
		}

		void EditSingleShortcut::keyPressEvent(QKeyEvent* evt)
		{
			if(!is_recording_) {
				return;
			}

			// Qt delivered a garbage keycode; Ignore it!
			if (evt->key() == -1)
			{
				return;
			}

			// If AltGr is pressed there is a problem with unicode
			// So just bail out...

			evt->accept();

			switch (evt->key())
			{
				case Qt::Key_AltGr: //or else we get unicode salad
					return;
				case Qt::Key_Shift:
				case Qt::Key_Control:
				case Qt::Key_Alt:
				case Qt::Key_Meta:
				case Qt::Key_Menu: //unused (yes, but why?)
					break;
				default:
					key_ = evt->key();
			}

			if ((key_ == 0) || (key_ == Qt::Key_unknown))
			{
				// Filter the interesting modifiers
				modifiers_ = evt->modifiers() & (Qt::ControlModifier | Qt::AltModifier |
																				 Qt::ShiftModifier | Qt::MetaModifier);
			}

			new_sequence_ = QKeySequence(key_ | modifiers_);

			updateText_();
		}

		void EditSingleShortcut::keyReleaseEvent(QKeyEvent* evt)
		{
			if(!is_recording_) {
				return;
			}

			// Qt delivered a garbage keycode; Ignore it!
			if (evt->key() == -1) {
				return;
			}

			switch (evt->key()) {
				case Qt::Key_AltGr: //or else we get unicode salad
					return;
				case Qt::Key_Shift:
				case Qt::Key_Control:
				case Qt::Key_Alt:
				case Qt::Key_Meta:
				case Qt::Key_Menu: //unused (yes, but why?)
					break;
				default:
					key_ = evt->key();
			}

			if ((key_ == 0) || (key_ == Qt::Key_unknown))
			{
				// Filter the interesting modifiers
				modifiers_ = evt->modifiers() & (Qt::ControlModifier | Qt::AltModifier |
																				 Qt::ShiftModifier | Qt::MetaModifier);
			}

			new_sequence_ = QKeySequence(key_ | modifiers_);
			evt->accept();

			updateText_();
		}

		void EditSingleShortcut::updateText_()
		{
			QString s = new_sequence_.toString(QKeySequence::NativeText);
			//"Escape" the ampersand character
			s.replace('&', QLatin1String("&&"));

			if (ShortcutRegistry::getInstance(0)->hasKey(new_sequence_))
			{
				setErrorText("Shortcut already in use.");
			}
			else
			{
				setErrorText("");
			}

			setShortcutText(s);
		}

		void EditSingleShortcut::setErrorText(QString error)
		{
			//make it nice and red
			error_label->setText("<span style=\" color:#ff0000;\">" + error + "</span>");
		}

		void EditSingleShortcut::startRecording_()
		{
			if(!is_recording_) {
				grabKeyboard();
				is_recording_ = true;
			}
		}

		void EditSingleShortcut::stopRecording_()
		{
			if(is_recording_) {
				releaseKeyboard();
				is_recording_ = false;
			}
		}

	} //namespace VIEW
} //namespace BALL