BibleTime
btshortcutsdialog.cpp
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #include "btshortcutsdialog.h"
14 
15 #include <QDialogButtonBox>
16 #include <QFlags>
17 #include <QFrame>
18 #include <QGridLayout>
19 #include <QKeyEvent>
20 #include <QLabel>
21 #include <QRadioButton>
22 #include <QVBoxLayout>
23 #include <memory>
24 #include "../../util/btconnect.h"
25 #include "../messagedialog.h"
26 
27 
28 // *************** BtShortcutsDialog ***************************************************************************
29 // A dialog to allow the user to input a shortcut for a primary and alternate key
30 
31 BtShortcutsDialog::BtShortcutsDialog(QWidget * parent, Qt::WindowFlags f)
32  : QDialog(parent, f)
33 {
34  setMinimumWidth(350);
35 
36  auto * const vLayout = new QVBoxLayout(this);
37 
38  {
39  auto gridLayout = std::make_unique<QGridLayout>();
40 
41  m_primaryButton = new QRadioButton(this);
42  m_primaryButton->setChecked(true);
43  gridLayout->addWidget(m_primaryButton, 0, 0);
44 
45  m_alternateButton = new QRadioButton(this);
46  gridLayout->addWidget(m_alternateButton, 1, 0);
47 
48  m_primaryLabel = new QLabel(this);
49  m_primaryLabel->setMinimumWidth(100);
50  m_primaryLabel->setFrameShape(QFrame::Panel);
51  gridLayout->addWidget(m_primaryLabel, 0, 1);
52 
53  m_alternateLabel = new QLabel(this);
54  m_alternateLabel->setMinimumWidth(100);
55  m_alternateLabel->setFrameShape(QFrame::Panel);
56  gridLayout->addWidget(m_alternateLabel, 1, 1);
57 
58  vLayout->addLayout(gridLayout.get());
59  gridLayout.release();
60  }
61 
62  auto * const buttons = new QDialogButtonBox(QDialogButtonBox::Ok
63  | QDialogButtonBox::Cancel,
64  this);
66  BT_CONNECT(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
67  BT_CONNECT(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
68  vLayout->addWidget(buttons);
69 
70  setLayout(vLayout);
71 
72  retranslateUi();
73 }
74 
75 // get new primary key from dialog
77  return m_primaryLabel->text();
78 }
79 
80 // set the initial value of the primary key
81 void BtShortcutsDialog::setFirstKeys(const QString& keys) {
82  m_primaryLabel->setText(keys);
83 }
84 
85 // get new second keys from dialog
87  return m_alternateLabel->text();
88 }
89 
90 // set the initial value of the second keys
91 void BtShortcutsDialog::setSecondKeys(const QString& keys) {
92  m_alternateLabel->setText(keys);
93 }
94 
95 // get key from users input, put into primary or alternate label for display to user
96 void BtShortcutsDialog::keyReleaseEvent(QKeyEvent* event) {
97  int key = event->key();
98  if ( (key == Qt::Key_Shift) || (key == Qt::Key_Control) || (key == Qt::Key_Meta) || (key == Qt::Key_Alt) )
99  return;
100 
101  QKeySequence keys(key);
102  QString keyStr = keys.toString();
103  if ( (event->modifiers() & Qt::AltModifier) == Qt::AltModifier)
104  keyStr = QStringLiteral("Alt+") + keyStr;
105  if ( (event->modifiers() & Qt::ShiftModifier) == Qt::ShiftModifier)
106  keyStr = QStringLiteral("Shift+") + keyStr;
107  if ( (event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier)
108  keyStr = QStringLiteral("Ctrl+") + keyStr;
109 
110  keyChangeRequest(keyStr);
111 }
112 
113 // complete the keyChangeRequest
114 void BtShortcutsDialog::changeSelectedShortcut(QKeySequence const & keys) {
115  if (!m_primaryButton->isChecked() || !m_alternateButton->isChecked())
116  return;
117 
118  auto const keysString(keys.toString());
119  if (m_primaryButton->isChecked())
120  m_primaryLabel->setText(keysString);
121 
122  if (m_alternateButton->isChecked())
123  m_alternateLabel->setText(keysString);
124 }
125 
127  setWindowTitle(tr("Configure shortcuts"));
128  auto dialogTooltip = tr("Select first or second shortcut and type the "
129  "shortcut with keyboard");
130 
131  m_primaryButton->setText(tr("First shortcut"));
132  m_primaryButton->setToolTip(dialogTooltip);
133 
134  m_alternateButton->setText(tr("Second shortcut"));
135  m_alternateButton->setToolTip(dialogTooltip);
136 
137  m_primaryLabel->setToolTip(dialogTooltip);
138 
139  m_alternateLabel->setToolTip(dialogTooltip);
140 }
141 
#define BT_CONNECT(...)
Definition: btconnect.h:20
void keyReleaseEvent(QKeyEvent *event) override
QRadioButton * m_alternateButton
BtShortcutsDialog(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
void changeSelectedShortcut(QKeySequence const &keys)
QRadioButton * m_primaryButton
void keyChangeRequest(QKeySequence const &keys)
void setFirstKeys(const QString &keys)
void setSecondKeys(const QString &keys)
void prepareDialogBox(QDialogButtonBox *box)