hottestwidget.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "hottestwidget.h"
  2. #include "ui_hottestwidget.h"
  3. //#define TEST_MAPPING
  4. HotTestWidget::HotTestWidget(QWidget *parent) :
  5. QWidget(parent),
  6. ui(new Ui::HotTestWidget),
  7. hotkey_1(new QHotkey(this)),
  8. hotkey_2(new QHotkey(this)),
  9. hotkey_3(new QHotkey(this)),
  10. hotkey_4(new QHotkey(NULL)),
  11. hotkey_5(new QHotkey(NULL)),
  12. thread4(new QThread(this)),
  13. thread5(new QThread(this)),
  14. testHotkeys(),
  15. nativeHotkey(new QHotkey(this))
  16. {
  17. ui->setupUi(this);
  18. this->thread4->start();
  19. this->thread5->start();
  20. #ifdef TEST_MAPPING
  21. //shortcut mapping override
  22. QHotkey::addGlobalMapping(QKeySequence("X"), QHotkey::NativeShortcut());// add invalid mapping to test if the overwrite works for all platforms
  23. #endif
  24. //1
  25. connect(this->ui->hotkeyCheckbox_1, &QCheckBox::toggled,
  26. this->hotkey_1, &QHotkey::setRegistered);
  27. connect(this->ui->hotkeySequenceEdit_1, &QKeySequenceEdit::keySequenceChanged,
  28. this, &HotTestWidget::setShortcut_1);
  29. connect(this->hotkey_1, &QHotkey::activated,
  30. this, &HotTestWidget::increase_1);
  31. //2
  32. connect(this->ui->hotkeyCheckbox_2, &QCheckBox::toggled,
  33. this->hotkey_2, &QHotkey::setRegistered);
  34. connect(this->ui->hotkeySequenceEdit_2, &QKeySequenceEdit::keySequenceChanged,
  35. this, &HotTestWidget::setShortcut_2);
  36. connect(this->hotkey_2, &QHotkey::activated,
  37. this, &HotTestWidget::increase_2);
  38. //3
  39. connect(this->ui->hotkeyCheckbox_3, &QCheckBox::toggled,
  40. this->hotkey_3, &QHotkey::setRegistered);
  41. connect(this->ui->hotkeySequenceEdit_3, &QKeySequenceEdit::keySequenceChanged,
  42. this, &HotTestWidget::setShortcut_3);
  43. connect(this->hotkey_3, &QHotkey::activated,
  44. this, &HotTestWidget::increase_3);
  45. //4
  46. connect(this->ui->hotkeyCheckbox_4, &QCheckBox::toggled,
  47. this->hotkey_4, &QHotkey::setRegistered);
  48. connect(this->ui->hotkeySequenceEdit_4, &QKeySequenceEdit::keySequenceChanged,
  49. this, &HotTestWidget::setShortcut_4);
  50. connect(this->hotkey_4, &QHotkey::activated,
  51. this, &HotTestWidget::increase_4);
  52. //5
  53. connect(this->ui->hotkeyCheckbox_5, &QCheckBox::toggled,
  54. this->hotkey_5, &QHotkey::setRegistered);
  55. connect(this->ui->hotkeySequenceEdit_5, &QKeySequenceEdit::keySequenceChanged,
  56. this, &HotTestWidget::setShortcut_5);
  57. connect(this->hotkey_5, &QHotkey::activated,
  58. this, &HotTestWidget::increase_5);
  59. //test connections
  60. this->testHotkeys += new QHotkey(Qt::Key_F, Qt::NoModifier, false, this);
  61. connect(this->testHotkeys.last(), &QHotkey::activated,
  62. this->ui->hotkeyFCheckBox, &QCheckBox::toggle);
  63. this->testHotkeys += new QHotkey(Qt::Key_F12, Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier, false, this);
  64. connect(this->testHotkeys.last(), &QHotkey::activated,
  65. this->ui->hotkeyCtrlAltMetaF12CheckBox, &QCheckBox::toggle);
  66. this->testHotkeys += new QHotkey(Qt::Key_Cancel, Qt::ControlModifier | Qt::ShiftModifier, false, this);
  67. connect(this->testHotkeys.last(), &QHotkey::activated,
  68. this->ui->hotkeyCtrlShiftCancelCheckBox, &QCheckBox::toggle);
  69. this->testHotkeys += new QHotkey(Qt::Key_Delete, Qt::MetaModifier, false, this);
  70. connect(this->testHotkeys.last(), &QHotkey::activated,
  71. this->ui->hotkeyMetaDelCheckBox, &QCheckBox::toggle);
  72. this->testHotkeys += new QHotkey(Qt::Key_NumLock, Qt::NoModifier, false, this);
  73. connect(this->testHotkeys.last(), &QHotkey::activated,
  74. this->ui->hotkeyNumlockCheckBox, &QCheckBox::toggle);
  75. this->testHotkeys += new QHotkey(Qt::Key_5, Qt::ControlModifier, false, this);
  76. connect(this->testHotkeys.last(), &QHotkey::activated,
  77. this->ui->hotkeyCtrl5CheckBox, &QCheckBox::toggle);
  78. this->testHotkeys += new QHotkey(Qt::Key_Tab, Qt::ShiftModifier, false, this);
  79. connect(this->testHotkeys.last(), &QHotkey::activated,
  80. this->ui->hotkeyShiftTabCheckBox, &QCheckBox::toggle);
  81. this->testHotkeys += new QHotkey(Qt::Key_Comma, Qt::ShiftModifier, false, this);
  82. connect(this->testHotkeys.last(), &QHotkey::activated,
  83. this->ui->hotkeyShiftCheckBox, &QCheckBox::toggle);
  84. this->testHotkeys += new QHotkey(Qt::Key_Semicolon, Qt::ShiftModifier, false, this);
  85. connect(this->testHotkeys.last(), &QHotkey::activated,
  86. this->ui->hotkeyShiftCheckBox_2, &QCheckBox::toggle);
  87. this->testHotkeys += new QHotkey(Qt::Key_K, Qt::ShiftModifier | Qt::AltModifier, false, this);
  88. connect(this->testHotkeys.last(), &QHotkey::activated,
  89. this->ui->hotkeyShiftAltKCheckBox, &QCheckBox::toggle);
  90. this->testHotkeys += new QHotkey(Qt::Key_K, Qt::ShiftModifier | Qt::AltModifier, false, this);
  91. connect(this->testHotkeys.last(), &QHotkey::activated,
  92. this->ui->hotkeyShiftAltKCheckBox_2, &QCheckBox::toggle);
  93. //native
  94. connect(this->nativeHotkey, &QHotkey::activated,
  95. this, &HotTestWidget::increase_native);
  96. }
  97. HotTestWidget::~HotTestWidget()
  98. {
  99. this->thread4->quit();
  100. this->thread4->wait();
  101. this->thread5->quit();
  102. this->thread5->wait();
  103. delete this->hotkey_4;
  104. delete this->hotkey_5;
  105. delete ui;
  106. }
  107. void HotTestWidget::setShortcut_1(const QKeySequence &sequence)
  108. {
  109. this->hotkey_1->setShortcut(sequence, false);
  110. }
  111. void HotTestWidget::setShortcut_2(const QKeySequence &sequence)
  112. {
  113. this->hotkey_2->setShortcut(sequence, false);
  114. }
  115. void HotTestWidget::setShortcut_3(const QKeySequence &sequence)
  116. {
  117. this->hotkey_3->setShortcut(sequence, false);
  118. }
  119. void HotTestWidget::setShortcut_4(const QKeySequence &sequence)
  120. {
  121. this->hotkey_4->setShortcut(sequence, false);
  122. }
  123. void HotTestWidget::setShortcut_5(const QKeySequence &sequence)
  124. {
  125. this->hotkey_5->setShortcut(sequence, false);
  126. }
  127. void HotTestWidget::increase_1()
  128. {
  129. this->ui->hotkeyCount_1->display(this->ui->hotkeyCount_1->intValue() + 1);
  130. }
  131. void HotTestWidget::increase_2()
  132. {
  133. this->ui->hotkeyCount_2->display(this->ui->hotkeyCount_2->intValue() + 1);
  134. }
  135. void HotTestWidget::increase_3()
  136. {
  137. this->ui->hotkeyCount_3->display(this->ui->hotkeyCount_3->intValue() + 1);
  138. }
  139. void HotTestWidget::increase_4()
  140. {
  141. this->ui->hotkeyCount_4->display(this->ui->hotkeyCount_4->intValue() + 1);
  142. }
  143. void HotTestWidget::increase_5()
  144. {
  145. this->ui->hotkeyCount_5->display(this->ui->hotkeyCount_5->intValue() + 1);
  146. }
  147. void HotTestWidget::on_resetButton_1_clicked()
  148. {
  149. this->ui->hotkeyCount_1->display(0);
  150. }
  151. void HotTestWidget::on_resetButton_2_clicked()
  152. {
  153. this->ui->hotkeyCount_2->display(0);
  154. }
  155. void HotTestWidget::on_resetButton_3_clicked()
  156. {
  157. this->ui->hotkeyCount_3->display(0);
  158. }
  159. void HotTestWidget::on_resetButton_4_clicked()
  160. {
  161. this->ui->hotkeyCount_4->display(0);
  162. }
  163. void HotTestWidget::on_resetButton_5_clicked()
  164. {
  165. this->ui->hotkeyCount_5->display(0);
  166. }
  167. void HotTestWidget::on_groupBox_toggled(bool checked)
  168. {
  169. for(QHotkey *hotkey : this->testHotkeys)
  170. hotkey->setRegistered(checked);
  171. }
  172. void HotTestWidget::on_threadEnableCheckBox_clicked()
  173. {
  174. this->ui->threadEnableCheckBox->setEnabled(false);
  175. this->ui->hotkeyCheckbox_1->setChecked(false);
  176. this->ui->hotkeyCheckbox_2->setChecked(false);
  177. this->ui->hotkeyCheckbox_3->setChecked(false);
  178. this->ui->hotkeyCheckbox_4->setChecked(false);
  179. this->ui->hotkeyCheckbox_5->setChecked(false);
  180. QApplication::processEvents();
  181. Q_ASSERT(!this->hotkey_4->isRegistered());
  182. Q_ASSERT(!this->hotkey_5->isRegistered());
  183. this->hotkey_4->moveToThread(this->thread4);
  184. this->hotkey_5->moveToThread(this->thread5);
  185. QApplication::processEvents();
  186. Q_ASSERT(this->hotkey_4->thread() == this->thread4);
  187. Q_ASSERT(this->hotkey_5->thread() == this->thread5);
  188. connect(this->thread4, &QThread::finished, this, [this](){
  189. this->hotkey_4->moveToThread(qApp->thread());
  190. });
  191. connect(this->thread5, &QThread::finished, this, [this](){
  192. this->hotkey_5->moveToThread(qApp->thread());
  193. });
  194. this->ui->tabWidget->setCurrentIndex(0);
  195. }
  196. void HotTestWidget::on_registeredCheckBox_toggled(bool checked)
  197. {
  198. if(checked) {
  199. this->nativeHotkey->setNativeShortcut({
  200. (quint32)this->ui->nativeKeySpinBox->value(),
  201. (quint32)this->ui->nativeModifiersSpinBox->value()
  202. }, true);
  203. } else
  204. this->nativeHotkey->setRegistered(false);
  205. }
  206. void HotTestWidget::increase_native()
  207. {
  208. this->ui->nativeCount->display(this->ui->nativeCount->intValue() + 1);
  209. }