| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2013~2020 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as |
| 6 |
* published by the Free Software Foundation; either version 2 of the |
| 7 |
* License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; see the file COPYING. If not, |
| 16 |
* see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* this is forked from kdelibs/kdeui/kkeysequencewidget.cpp */ |
| 20 |
|
| 21 |
/* |
| 22 |
Original Copyright header |
| 23 |
SPDX-FileCopyrightText: 1998 Mark Donohoe <[email protected]> |
| 24 |
SPDX-FileCopyrightText: 2001 Ellis Whitehead <[email protected]> |
| 25 |
SPDX-FileCopyrightText: 2007 Andreas Hartmetz <[email protected]> |
| 26 |
|
| 27 |
This library is free software; you can redistribute it and/or |
| 28 |
modify it under the terms of the GNU Library General Public |
| 29 |
License as published by the Free Software Foundation; either |
| 30 |
version 2 of the License, or (at your option) any later version. |
| 31 |
|
| 32 |
This library is distributed in the hope that it will be useful, |
| 33 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 34 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 35 |
Library General Public License for more details. |
| 36 |
|
| 37 |
You should have received a copy of the GNU Library General Public License |
| 38 |
along with this library; see the file COPYING.LIB. If not, write to |
| 39 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 40 |
Boston, MA 02110-1301, USA. |
| 41 |
*/ |
| 42 |
|
| 43 |
#include "fcitxqtkeysequencewidget.h" |
| 44 |
#include "fcitxqtkeysequencewidget_p.h" |
| 45 |
|
| 46 |
#include "qtkeytrans.h" |
| 47 |
#include <QApplication> |
| 48 |
#include <QHBoxLayout> |
| 49 |
#include <QHash> |
| 50 |
#include <QKeyEvent> |
| 51 |
#include <QLoggingCategory> |
| 52 |
#include <QMessageBox> |
| 53 |
#include <QTimer> |
| 54 |
#include <QToolButton> |
| 55 |
#include <fcitx-utils/i18n.h> |
| 56 |
#include <fcitx-utils/key.h> |
| 57 |
|
| 58 |
Q_LOGGING_CATEGORY(fcitx5qtKeysequenceWidget, "fcitx5.qt.keysequencewidget") |
| 59 |
|
| 60 |
namespace fcitx { |
| 61 |
|
| 62 |
namespace { |
| 63 |
|
| 64 |
bool isX11LikePlatform() { |
| 65 |
return QGuiApplication::platformName() == "xcb" || |
| 66 |
QGuiApplication::platformName().startsWith("wayland"); |
| 67 |
} |
| 68 |
|
| 69 |
bool keyQtToFcitx(int keyQt, const QString &text, FcitxQtModifierSide side, |
| 70 |
Key &outkey) { |
| 71 |
int key = keyQt & (~Qt::KeyboardModifierMask); |
| 72 |
int state = keyQt & Qt::KeyboardModifierMask; |
| 73 |
int sym; |
| 74 |
unsigned int states; |
| 75 |
if (!keyQtToSym(key, Qt::KeyboardModifiers(state), text, sym, states)) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
if (side == MS_Right) { |
| 79 |
switch (sym) { |
| 80 |
case FcitxKey_Control_L: |
| 81 |
sym = FcitxKey_Control_R; |
| 82 |
break; |
| 83 |
case FcitxKey_Alt_L: |
| 84 |
sym = FcitxKey_Alt_R; |
| 85 |
break; |
| 86 |
case FcitxKey_Shift_L: |
| 87 |
sym = FcitxKey_Shift_R; |
| 88 |
break; |
| 89 |
case FcitxKey_Super_L: |
| 90 |
sym = FcitxKey_Super_R; |
| 91 |
break; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
outkey = Key(static_cast<KeySym>(sym), KeyStates(states)); |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
} // namespace |
| 100 |
|
| 101 |
class FcitxQtKeySequenceWidgetPrivate { |
| 102 |
public: |
| 103 |
FcitxQtKeySequenceWidgetPrivate(FcitxQtKeySequenceWidget *q); |
| 104 |
|
| 105 |
void init(); |
| 106 |
|
| 107 |
static bool isOkWhenModifierless(int keyQt); |
| 108 |
|
| 109 |
void updateShortcutDisplay(); |
| 110 |
void startRecording(); |
| 111 |
|
| 112 |
void controlModifierlessTimout() { |
| 113 |
if (keySequence_.size() != 0 && !modifierKeys_) { |
| 114 |
// No modifier key pressed currently. Start the timout |
| 115 |
modifierlessTimeout_.start(600); |
| 116 |
} else { |
| 117 |
// A modifier is pressed. Stop the timeout |
| 118 |
modifierlessTimeout_.stop(); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
void cancelRecording() { |
| 123 |
keySequence_ = oldKeySequence_; |
| 124 |
doneRecording(); |
| 125 |
} |
| 126 |
|
| 127 |
// private slot |
| 128 |
void doneRecording(); |
| 129 |
|
| 130 |
// members |
| 131 |
FcitxQtKeySequenceWidget *const q; |
| 132 |
QHBoxLayout *layout_ = nullptr; |
| 133 |
FcitxQtKeySequenceButton *keyButton_ = nullptr; |
| 134 |
QToolButton *clearButton_ = nullptr; |
| 135 |
QAction *keyCodeModeAction_ = nullptr; |
| 136 |
|
| 137 |
QList<Key> keySequence_; |
| 138 |
QList<Key> oldKeySequence_; |
| 139 |
QTimer modifierlessTimeout_; |
| 140 |
bool allowModifierless_ = false; |
| 141 |
KeyStates modifierKeys_; |
| 142 |
unsigned int qtModifierKeys_ = 0; |
| 143 |
bool isRecording_ = false; |
| 144 |
bool multiKeyShortcutsAllowed_ = false; |
| 145 |
bool allowModifierOnly_ = false; |
| 146 |
bool modifierAllowed_ = true; |
| 147 |
bool keycodeAllowed_ = true; |
| 148 |
}; |
| 149 |
|
| 150 |
FcitxQtKeySequenceWidgetPrivate::FcitxQtKeySequenceWidgetPrivate( |
| 151 |
FcitxQtKeySequenceWidget *q) |
| 152 |
: q(q) {} |
| 153 |
|
| 154 |
FcitxQtKeySequenceWidget::FcitxQtKeySequenceWidget(QWidget *parent) |
| 155 |
: QWidget(parent), d(new FcitxQtKeySequenceWidgetPrivate(this)) { |
| 156 |
d->init(); |
| 157 |
setFocusProxy(d->keyButton_); |
| 158 |
connect(d->keyButton_, &QPushButton::clicked, this, |
| 159 |
&FcitxQtKeySequenceWidget::captureKeySequence); |
| 160 |
connect(d->clearButton_, &QPushButton::clicked, this, |
| 161 |
&FcitxQtKeySequenceWidget::clearKeySequence); |
| 162 |
connect(&d->modifierlessTimeout_, &QTimer::timeout, this, |
| 163 |
[this]() { d->doneRecording(); }); |
| 164 |
d->updateShortcutDisplay(); |
| 165 |
} |
| 166 |
|
| 167 |
void FcitxQtKeySequenceWidgetPrivate::init() { |
| 168 |
layout_ = new QHBoxLayout(q); |
| 169 |
layout_->setContentsMargins(0, 0, 0, 0); |
| 170 |
|
| 171 |
keyButton_ = new FcitxQtKeySequenceButton(this, q); |
| 172 |
keyButton_->setFocusPolicy(Qt::StrongFocus); |
| 173 |
keyButton_->setIcon(QIcon::fromTheme("configure")); |
| 174 |
layout_->addWidget(keyButton_); |
| 175 |
|
| 176 |
clearButton_ = new QToolButton(q); |
| 177 |
layout_->addWidget(clearButton_); |
| 178 |
|
| 179 |
keyCodeModeAction_ = new QAction(_("Key code mode")); |
| 180 |
keyCodeModeAction_->setCheckable(true); |
| 181 |
keyCodeModeAction_->setEnabled(isX11LikePlatform()); |
| 182 |
q->setContextMenuPolicy(Qt::ActionsContextMenu); |
| 183 |
q->addAction(keyCodeModeAction_); |
| 184 |
|
| 185 |
if (qApp->isLeftToRight()) |
| 186 |
clearButton_->setIcon(QIcon::fromTheme("edit-clear-locationbar-rtl")); |
| 187 |
else |
| 188 |
clearButton_->setIcon(QIcon::fromTheme("edit-clear-locationbar-ltr")); |
| 189 |
|
| 190 |
q->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); |
| 191 |
} |
| 192 |
|
| 193 |
FcitxQtKeySequenceWidget::~FcitxQtKeySequenceWidget() { delete d; } |
| 194 |
|
| 195 |
bool FcitxQtKeySequenceWidget::multiKeyShortcutsAllowed() const { |
| 196 |
return d->multiKeyShortcutsAllowed_; |
| 197 |
} |
| 198 |
|
| 199 |
void FcitxQtKeySequenceWidget::setMultiKeyShortcutsAllowed(bool allowed) { |
| 200 |
d->multiKeyShortcutsAllowed_ = allowed; |
| 201 |
} |
| 202 |
|
| 203 |
bool FcitxQtKeySequenceWidget::isModifierAllowed() const { |
| 204 |
return d->modifierAllowed_; |
| 205 |
} |
| 206 |
|
| 207 |
void FcitxQtKeySequenceWidget::setModifierAllowed(bool allowed) { |
| 208 |
d->modifierAllowed_ = allowed; |
| 209 |
} |
| 210 |
|
| 211 |
bool FcitxQtKeySequenceWidget::isKeycodeAllowed() const { |
| 212 |
return d->keycodeAllowed_; |
| 213 |
} |
| 214 |
|
| 215 |
void FcitxQtKeySequenceWidget::setKeycodeAllowed(bool allowed) { |
| 216 |
if (d->keycodeAllowed_ == allowed) { |
| 217 |
return; |
| 218 |
} |
| 219 |
d->keycodeAllowed_ = allowed; |
| 220 |
if (allowed) { |
| 221 |
d->keyCodeModeAction_->setChecked(false); |
| 222 |
addAction(d->keyCodeModeAction_); |
| 223 |
} else { |
| 224 |
removeAction(d->keyCodeModeAction_); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
void FcitxQtKeySequenceWidget::setModifierlessAllowed(bool allow) { |
| 229 |
d->allowModifierless_ = allow; |
| 230 |
} |
| 231 |
|
| 232 |
bool FcitxQtKeySequenceWidget::isModifierlessAllowed() { |
| 233 |
return d->allowModifierless_; |
| 234 |
} |
| 235 |
|
| 236 |
bool FcitxQtKeySequenceWidget::isModifierOnlyAllowed() { |
| 237 |
return d->allowModifierOnly_; |
| 238 |
} |
| 239 |
|
| 240 |
bool FcitxQtKeySequenceWidget::isModifierlessAllowed() const { |
| 241 |
return d->allowModifierless_; |
| 242 |
} |
| 243 |
|
| 244 |
bool FcitxQtKeySequenceWidget::isModifierOnlyAllowed() const { |
| 245 |
return d->allowModifierOnly_; |
| 246 |
} |
| 247 |
|
| 248 |
void FcitxQtKeySequenceWidget::setModifierOnlyAllowed(bool allow) { |
| 249 |
d->allowModifierOnly_ = allow; |
| 250 |
} |
| 251 |
|
| 252 |
void FcitxQtKeySequenceWidget::setClearButtonShown(bool show) { |
| 253 |
d->clearButton_->setVisible(show); |
| 254 |
} |
| 255 |
|
| 256 |
bool FcitxQtKeySequenceWidget::isClearButtonVisible() const { |
| 257 |
return d->clearButton_->isVisible(); |
| 258 |
} |
| 259 |
|
| 260 |
// slot |
| 261 |
void FcitxQtKeySequenceWidget::captureKeySequence() { d->startRecording(); } |
| 262 |
|
| 263 |
const QList<Key> &FcitxQtKeySequenceWidget::keySequence() const { |
| 264 |
return d->keySequence_; |
| 265 |
} |
| 266 |
|
| 267 |
// slot |
| 268 |
void FcitxQtKeySequenceWidget::setKeySequence(const QList<Key> &seq) { |
| 269 |
// oldKeySequence holds the key sequence before recording started, if |
| 270 |
// setKeySequence() |
| 271 |
// is called while not recording then set oldKeySequence to the existing |
| 272 |
// sequence so |
| 273 |
// that the keySequenceChanged() signal is emitted if the new and previous |
| 274 |
// key |
| 275 |
// sequences are different |
| 276 |
if (!d->isRecording_) { |
| 277 |
d->oldKeySequence_ = d->keySequence_; |
| 278 |
} |
| 279 |
|
| 280 |
d->keySequence_ = QList<Key>(); |
| 281 |
for (auto key : seq) { |
| 282 |
d->keySequence_ << key; |
| 283 |
} |
| 284 |
d->doneRecording(); |
| 285 |
} |
| 286 |
|
| 287 |
// slot |
| 288 |
void FcitxQtKeySequenceWidget::clearKeySequence() { |
| 289 |
setKeySequence(QList<Key>()); |
| 290 |
} |
| 291 |
|
| 292 |
void FcitxQtKeySequenceWidgetPrivate::startRecording() { |
| 293 |
modifierKeys_ = 0; |
| 294 |
oldKeySequence_ = keySequence_; |
| 295 |
keySequence_ = QList<Key>(); |
| 296 |
isRecording_ = true; |
| 297 |
keyButton_->grabKeyboard(); |
| 298 |
|
| 299 |
if (!QWidget::keyboardGrabber()) { |
| 300 |
qWarning() << "Failed to grab the keyboard! Most likely qt's nograb " |
| 301 |
"option is active"; |
| 302 |
} |
| 303 |
|
| 304 |
keyButton_->setDown(true); |
| 305 |
updateShortcutDisplay(); |
| 306 |
} |
| 307 |
|
| 308 |
void FcitxQtKeySequenceWidgetPrivate::doneRecording() { |
| 309 |
modifierlessTimeout_.stop(); |
| 310 |
isRecording_ = false; |
| 311 |
keyButton_->releaseKeyboard(); |
| 312 |
keyButton_->setDown(false); |
| 313 |
|
| 314 |
if (keySequence_ == oldKeySequence_ && !allowModifierOnly_) { |
| 315 |
// The sequence hasn't changed |
| 316 |
updateShortcutDisplay(); |
| 317 |
return; |
| 318 |
} |
| 319 |
|
| 320 |
Q_EMIT q->keySequenceChanged(keySequence_); |
| 321 |
|
| 322 |
updateShortcutDisplay(); |
| 323 |
} |
| 324 |
|
| 325 |
void FcitxQtKeySequenceWidgetPrivate::updateShortcutDisplay() { |
| 326 |
QString s = QString::fromUtf8( |
| 327 |
Key::keyListToString(keySequence_, KeyStringFormat::Localized).c_str()); |
| 328 |
s.replace('&', QLatin1String("&&")); |
| 329 |
|
| 330 |
if (isRecording_) { |
| 331 |
if (modifierKeys_) { |
| 332 |
if (!s.isEmpty()) |
| 333 |
s.append(","); |
| 334 |
if (modifierKeys_ & KeyState::Super) |
| 335 |
s += "Super+"; |
| 336 |
if (modifierKeys_ & KeyState::Ctrl) |
| 337 |
s += "Control+"; |
| 338 |
if (modifierKeys_ & KeyState::Alt) |
| 339 |
s += "Alt+"; |
| 340 |
if (modifierKeys_ & KeyState::Shift) |
| 341 |
s += "Shift+"; |
| 342 |
if (modifierKeys_ & KeyState::Hyper) |
| 343 |
s += "Hyper+"; |
| 344 |
|
| 345 |
} else if (keySequence_.size() == 0) { |
| 346 |
s = "..."; |
| 347 |
} |
| 348 |
// make it clear that input is still going on |
| 349 |
s.append(" ..."); |
| 350 |
} |
| 351 |
|
| 352 |
if (s.isEmpty()) { |
| 353 |
s = _("Empty"); |
| 354 |
} |
| 355 |
|
| 356 |
s.prepend(' '); |
| 357 |
s.append(' '); |
| 358 |
keyButton_->setText(s); |
| 359 |
} |
| 360 |
|
| 361 |
FcitxQtKeySequenceButton::~FcitxQtKeySequenceButton() {} |
| 362 |
|
| 363 |
// prevent Qt from special casing Tab and Backtab |
| 364 |
bool FcitxQtKeySequenceButton::event(QEvent *e) { |
| 365 |
if (d->isRecording_ && e->type() == QEvent::KeyPress) { |
| 366 |
keyPressEvent(static_cast<QKeyEvent *>(e)); |
| 367 |
return true; |
| 368 |
} |
| 369 |
|
| 370 |
// The shortcut 'alt+c' ( or any other dialog local action shortcut ) |
| 371 |
// ended the recording and triggered the action associated with the |
| 372 |
// action. In case of 'alt+c' ending the dialog. It seems that those |
| 373 |
// ShortcutOverride events get sent even if grabKeyboard() is active. |
| 374 |
if (d->isRecording_ && e->type() == QEvent::ShortcutOverride) { |
| 375 |
e->accept(); |
| 376 |
return true; |
| 377 |
} |
| 378 |
|
| 379 |
return QPushButton::event(e); |
| 380 |
} |
| 381 |
|
| 382 |
void FcitxQtKeySequenceButton::keyPressEvent(QKeyEvent *e) { |
| 383 |
int keyQt = e->key(); |
| 384 |
if (keyQt == -1) { |
| 385 |
// Qt sometimes returns garbage keycodes, I observed -1, if it doesn't |
| 386 |
// know a key. We cannot do anything useful with those (several keys |
| 387 |
// have -1, indistinguishable) and QKeySequence.toString() will also |
| 388 |
// yield a garbage string. |
| 389 |
QMessageBox::warning( |
| 390 |
this, _("The key you just pressed is not supported by Qt."), |
| 391 |
_("Unsupported Key")); |
| 392 |
return d->cancelRecording(); |
| 393 |
} |
| 394 |
|
| 395 |
// Same as Key::normalize(); |
| 396 |
unsigned int newQtModifiers = |
| 397 |
e->modifiers() & (Qt::META | Qt::ALT | Qt::CTRL | Qt::SHIFT); |
| 398 |
KeyStates newModifiers; |
| 399 |
if (isX11LikePlatform()) { |
| 400 |
newModifiers = KeyStates(e->nativeModifiers()) & |
| 401 |
KeyStates{KeyState::Ctrl_Alt_Shift, KeyState::Hyper, |
| 402 |
KeyState::Super}; |
| 403 |
newModifiers |= |
| 404 |
Key::keySymToStates(static_cast<KeySym>(e->nativeVirtualKey())); |
| 405 |
} else { |
| 406 |
if (newQtModifiers & Qt::META) { |
| 407 |
newModifiers |= KeyState::Super; |
| 408 |
} |
| 409 |
if (newQtModifiers & Qt::ALT) { |
| 410 |
newModifiers |= KeyState::Alt; |
| 411 |
} |
| 412 |
if (newQtModifiers & Qt::CTRL) { |
| 413 |
newModifiers |= KeyState::Ctrl; |
| 414 |
} |
| 415 |
if (newQtModifiers & Qt::SHIFT) { |
| 416 |
newModifiers |= KeyState::Shift; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
// don't have the return or space key appear as first key of the sequence |
| 421 |
// when they |
| 422 |
// were pressed to start editing - catch and them and imitate their effect |
| 423 |
if (!d->isRecording_ && |
| 424 |
((keyQt == Qt::Key_Return || keyQt == Qt::Key_Space))) { |
| 425 |
d->startRecording(); |
| 426 |
d->modifierKeys_ = newModifiers; |
| 427 |
d->qtModifierKeys_ = newQtModifiers; |
| 428 |
d->updateShortcutDisplay(); |
| 429 |
return; |
| 430 |
} |
| 431 |
|
| 432 |
// We get events even if recording isn't active. |
| 433 |
if (!d->isRecording_) |
| 434 |
return QPushButton::keyPressEvent(e); |
| 435 |
|
| 436 |
e->accept(); |
| 437 |
d->modifierKeys_ = newModifiers; |
| 438 |
d->qtModifierKeys_ = newQtModifiers; |
| 439 |
|
| 440 |
switch (keyQt) { |
| 441 |
case Qt::Key_AltGr: // or else we get unicode salad |
| 442 |
return; |
| 443 |
case Qt::Key_Shift: |
| 444 |
case Qt::Key_Control: |
| 445 |
case Qt::Key_Alt: |
| 446 |
case Qt::Key_Super_L: |
| 447 |
case Qt::Key_Super_R: |
| 448 |
case Qt::Key_Hyper_L: |
| 449 |
case Qt::Key_Hyper_R: |
| 450 |
case Qt::Key_Meta: |
| 451 |
case Qt::Key_Menu: // unused (yes, but why?) |
| 452 |
d->controlModifierlessTimout(); |
| 453 |
d->updateShortcutDisplay(); |
| 454 |
break; |
| 455 |
default: |
| 456 |
// We now have a valid key press. |
| 457 |
if (keyQt) { |
| 458 |
if ((keyQt == Qt::Key_Backtab) && |
| 459 |
d->modifierKeys_.test(KeyState::Shift)) { |
| 460 |
keyQt = Qt::Key_Tab | d->qtModifierKeys_; |
| 461 |
} else { |
| 462 |
keyQt |= d->qtModifierKeys_; |
| 463 |
} |
| 464 |
|
| 465 |
Key key; |
| 466 |
if (d->keyCodeModeAction_->isChecked()) { |
| 467 |
key = Key::fromKeyCode(e->nativeScanCode(), d->modifierKeys_); |
| 468 |
} else { |
| 469 |
if (isX11LikePlatform()) { |
| 470 |
key = Key(static_cast<KeySym>(e->nativeVirtualKey()), |
| 471 |
KeyStates(e->nativeModifiers())) |
| 472 |
.normalize(); |
| 473 |
} else { |
| 474 |
if (!keyQtToFcitx(keyQt, e->text(), MS_Unknown, key)) { |
| 475 |
qCDebug(fcitx5qtKeysequenceWidget) |
| 476 |
<< "FcitxQtKeySequenceButton::keyPressEvent() " |
| 477 |
"Failed to " |
| 478 |
"convert Qt key to fcitx: " |
| 479 |
<< e; |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
// Check the first key. |
| 485 |
if (d->keySequence_.size() == 0) { |
| 486 |
if (!d->allowModifierless_ && key.states() == 0) { |
| 487 |
return; |
| 488 |
} |
| 489 |
if (!d->modifierAllowed_ && key.states() != 0) { |
| 490 |
return; |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
if (key.isValid()) { |
| 495 |
d->keySequence_ << key; |
| 496 |
} |
| 497 |
|
| 498 |
if ((!d->multiKeyShortcutsAllowed_) || |
| 499 |
(d->keySequence_.size() >= 4)) { |
| 500 |
d->doneRecording(); |
| 501 |
return; |
| 502 |
} |
| 503 |
d->controlModifierlessTimout(); |
| 504 |
d->updateShortcutDisplay(); |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
void FcitxQtKeySequenceButton::keyReleaseEvent(QKeyEvent *e) { |
| 510 |
if (e->key() == -1) { |
| 511 |
// ignore garbage, see keyPressEvent() |
| 512 |
return; |
| 513 |
} |
| 514 |
|
| 515 |
if (!d->isRecording_) |
| 516 |
return QPushButton::keyReleaseEvent(e); |
| 517 |
|
| 518 |
e->accept(); |
| 519 |
|
| 520 |
if (!d->multiKeyShortcutsAllowed_ && d->allowModifierOnly_ && |
| 521 |
(e->key() == Qt::Key_Shift || e->key() == Qt::Key_Control || |
| 522 |
e->key() == Qt::Key_Meta || e->key() == Qt::Key_Alt)) { |
| 523 |
auto side = MS_Unknown; |
| 524 |
|
| 525 |
if (isX11LikePlatform()) { |
| 526 |
|
| 527 |
if (e->nativeVirtualKey() == FcitxKey_Control_L || |
| 528 |
e->nativeVirtualKey() == FcitxKey_Alt_L || |
| 529 |
e->nativeVirtualKey() == FcitxKey_Shift_L || |
| 530 |
e->nativeVirtualKey() == FcitxKey_Super_L) { |
| 531 |
side = MS_Left; |
| 532 |
} |
| 533 |
if (e->nativeVirtualKey() == FcitxKey_Control_R || |
| 534 |
e->nativeVirtualKey() == FcitxKey_Alt_R || |
| 535 |
e->nativeVirtualKey() == FcitxKey_Shift_R || |
| 536 |
e->nativeVirtualKey() == FcitxKey_Super_R) { |
| 537 |
side = MS_Right; |
| 538 |
} |
| 539 |
} |
| 540 |
int keyQt = e->key() | d->qtModifierKeys_; |
| 541 |
Key key; |
| 542 |
if (keyQtToFcitx(keyQt, e->text(), side, key)) { |
| 543 |
if (d->keyCodeModeAction_->isChecked()) { |
| 544 |
key = Key::fromKeyCode(e->nativeScanCode(), key.states()); |
| 545 |
} |
| 546 |
d->keySequence_ = QList<Key>({key}); |
| 547 |
} |
| 548 |
d->doneRecording(); |
| 549 |
return; |
| 550 |
} |
| 551 |
|
| 552 |
unsigned int newQtModifiers = |
| 553 |
e->modifiers() & (Qt::META | Qt::ALT | Qt::CTRL | Qt::SHIFT); |
| 554 |
KeyStates newModifiers; |
| 555 |
if (isX11LikePlatform()) { |
| 556 |
newModifiers = KeyStates(e->nativeModifiers()) & |
| 557 |
KeyStates{KeyState::Ctrl_Alt_Shift, KeyState::Hyper, |
| 558 |
KeyState::Super}; |
| 559 |
newModifiers &= |
| 560 |
~Key::keySymToStates(static_cast<KeySym>(e->nativeVirtualKey())); |
| 561 |
} else { |
| 562 |
if (newQtModifiers & Qt::META) { |
| 563 |
newModifiers |= KeyState::Super; |
| 564 |
} |
| 565 |
if (newQtModifiers & Qt::ALT) { |
| 566 |
newModifiers |= KeyState::Alt; |
| 567 |
} |
| 568 |
if (newQtModifiers & Qt::CTRL) { |
| 569 |
newModifiers |= KeyState::Ctrl; |
| 570 |
} |
| 571 |
if (newQtModifiers & Qt::SHIFT) { |
| 572 |
newModifiers |= KeyState::Shift; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
// if a modifier that belongs to the shortcut was released... |
| 577 |
if ((newModifiers & d->modifierKeys_) < d->modifierKeys_) { |
| 578 |
d->modifierKeys_ = newModifiers; |
| 579 |
d->controlModifierlessTimout(); |
| 580 |
d->updateShortcutDisplay(); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
// static |
| 585 |
bool FcitxQtKeySequenceWidgetPrivate::isOkWhenModifierless(int keyQt) { |
| 586 |
// this whole function is a hack, but especially the first line of code |
| 587 |
if (QKeySequence(keyQt).toString().length() == 1) |
| 588 |
return false; |
| 589 |
|
| 590 |
switch (keyQt) { |
| 591 |
case Qt::Key_Return: |
| 592 |
case Qt::Key_Space: |
| 593 |
case Qt::Key_Tab: |
| 594 |
case Qt::Key_Backtab: // does this ever happen? |
| 595 |
case Qt::Key_Backspace: |
| 596 |
case Qt::Key_Delete: |
| 597 |
return false; |
| 598 |
default: |
| 599 |
return true; |
| 600 |
} |
| 601 |
} |
| 602 |
} // namespace fcitx |
| 603 |
|
| 604 |
#include "moc_fcitxqtkeysequencewidget.cpp" |