#include "mainwindow.h" #include "qglobalshortcut.h" #include #include #include #include #include #include #include #include #include #include #include #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), mEdit{}, mHide{}, mFull{} { auto menu = this->menuBar(); auto preference = menu->addAction("Preference"); connect(preference, &QAction::triggered, [this] { this->showEditor(); }); preference->setShortcut(QKeySequence::fromString("Ctrl+P")); auto quit = menu->addAction("Quit"); connect(quit, &QAction::triggered, [this] { qApp->quit(); }); quit->setShortcut(QKeySequence::fromString("Ctrl+Q")); initEditor(); } MainWindow::~MainWindow() { delete mEdit; } void MainWindow::showEditor() { mEdit->show(); } void MainWindow::initEditor() { mEdit = new QWidget{}; QLabel* hide = new QLabel{}; hide->setText("hide/show"); mHide = new KeyEdit{mEdit}; QLabel* full = new QLabel{}; full->setText("fullscreen/restore"); mFull = new KeyEdit{mEdit}; QPushButton* acc = new QPushButton{}; acc->setText("Ok"); QPushButton* rej = new QPushButton{}; rej->setText("Cancel"); QGridLayout* g = new QGridLayout{}; g->addWidget(hide, 0, 0); g->addWidget(mHide, 0, 1); g->addWidget(full, 1, 0); g->addWidget(mFull, 1, 1); g->addWidget(acc, 2, 0); g->addWidget(rej, 2, 1); connect(acc, &QPushButton::clicked, mEdit, [this] { mEdit->hide(); }); connect(rej, &QPushButton::clicked, mEdit, [this] { mHide->clear(); mFull->clear(); mEdit->hide(); }); connect(mHide, &KeyEdit::actived, [this] { if(this->isHidden()) { this->activateWindow(); this->raise(); this->show(); } else { this->hide(); } }); connect(mHide, &KeyEdit::error, [this](QString msg) { QMessageBox::critical(this, "Error", msg); mHide->clear(); }); connect(mFull, &KeyEdit::actived, [this] { if(this->isFullScreen()) { this->showNormal(); } else { this->showFullScreen(); } }); connect(mFull, &KeyEdit::error, [this](QString msg) { QMessageBox::critical(this, "Error", msg); }); mEdit->setLayout(g); mEdit->resize(mEdit->sizeHint()); mEdit->hide(); #ifdef Q_OS_WIN // test conflit with QQ mHide->setKey(QKeySequence::fromString("Ctrl+Alt+A")); #else // test conflit with search mHide->setKey(QKeySequence::fromString("Alt+Space")); #endif } KeyEdit::KeyEdit(QWidget *parent) : QKeySequenceEdit{parent} { mGhk = new QGlobalShortcut{this}; connect(mGhk, &QGlobalShortcut::activated, [this] { this->actived(mGhk->key()); }); connect(mGhk, &QGlobalShortcut::error, this, &KeyEdit::error); connect(this, &KeyEdit::editingFinished, [this] { mGhk->setKey(this->keySequence()); }); } KeyEdit::~KeyEdit() { delete mGhk; } void KeyEdit::clear() { QKeySequenceEdit::clear(); mGhk->unsetKey(); } void KeyEdit::setKey(QKeySequence key) { QKeySequenceEdit::setKeySequence(key); mGhk->setKey(key); }