diff options
Diffstat (limited to 'src/frontend/settingsdialogs/btshortcutseditor.cpp')
-rw-r--r-- | src/frontend/settingsdialogs/btshortcutseditor.cpp | 277 |
1 files changed, 138 insertions, 139 deletions
diff --git a/src/frontend/settingsdialogs/btshortcutseditor.cpp b/src/frontend/settingsdialogs/btshortcutseditor.cpp index 2e943eb..3811f7d 100644 --- a/src/frontend/settingsdialogs/btshortcutseditor.cpp +++ b/src/frontend/settingsdialogs/btshortcutseditor.cpp @@ -2,7 +2,7 @@ * * This file is part of BibleTime's source code, http://www.bibletime.info/. * -* Copyright 1999-2014 by the BibleTime developers. +* Copyright 1999-2016 by the BibleTime developers. * The BibleTime source code is licensed under the GNU General Public License version 2.0. * **********/ @@ -22,6 +22,7 @@ #include <QVBoxLayout> #include "frontend/displaywindow/btactioncollection.h" #include "frontend/settingsdialogs/btshortcutsdialog.h" +#include "util/btconnect.h" // *************** BtShortcutsEditorItem ******************************************************************* @@ -48,7 +49,7 @@ class BtShortcutsEditorItem : public QTableWidgetItem { }; BtShortcutsEditorItem::BtShortcutsEditorItem(QAction* action) - : m_action(action), m_newFirstHotkey(0), m_newSecondHotkey(0) { + : m_action(action), m_newFirstHotkey(nullptr), m_newSecondHotkey(nullptr) { QList<QKeySequence> list = m_action->shortcuts(); if (list.count() > 0) m_newFirstHotkey = new QKeySequence(list.at(0)); @@ -74,13 +75,13 @@ void BtShortcutsEditorItem::setDefaultKeys(QKeySequence keys) { } void BtShortcutsEditorItem::setFirstHotkey(QKeySequence keys) { - if (m_newFirstHotkey == 0) + if (m_newFirstHotkey == nullptr) m_newFirstHotkey = new QKeySequence(); *m_newFirstHotkey = keys; } void BtShortcutsEditorItem::setSecondHotkey(const QString& keys) { - if (m_newSecondHotkey == 0) + if (m_newSecondHotkey == nullptr) m_newSecondHotkey = new QKeySequence(); *m_newSecondHotkey = QKeySequence(keys); } @@ -88,22 +89,22 @@ void BtShortcutsEditorItem::setSecondHotkey(const QString& keys) { // Deletes hotkey information void BtShortcutsEditorItem::deleteHotkeys() { delete m_newFirstHotkey; - m_newFirstHotkey = 0; + m_newFirstHotkey = nullptr; delete m_newSecondHotkey; - m_newSecondHotkey = 0; + m_newSecondHotkey = nullptr; } // Moves the hotkey information into the QAction variable void BtShortcutsEditorItem::commitChanges() { QString actionName = text(); QList<QKeySequence> list; - if ( (m_newFirstHotkey != 0) && (*m_newFirstHotkey != QKeySequence()) ) { + if ( (m_newFirstHotkey != nullptr) && (*m_newFirstHotkey != QKeySequence()) ) { list << *m_newFirstHotkey; } - if ( (m_newSecondHotkey != 0) && (*m_newSecondHotkey != QKeySequence()) ) + if ( (m_newSecondHotkey != nullptr) && (*m_newSecondHotkey != QKeySequence()) ) list << *m_newSecondHotkey; - if (m_action != 0) + if (m_action != nullptr) m_action->setShortcuts(list); } @@ -111,29 +112,139 @@ void BtShortcutsEditorItem::commitChanges() { // ******************* BtShortcutsEditor ******************************************************* BtShortcutsEditor::BtShortcutsEditor(BtActionCollection* collection, QWidget* parent) - : QWidget(parent), m_dlg(new BtShortcutsDialog(this)), m_table(0), m_shortcutChooser(0), m_noneButton(0), m_defaultButton(0), - m_customButton(0), m_defaultLabelValue(0), m_currentRow(-1) { - init(); - addCollection(collection); - bool ok = connect(m_dlg, SIGNAL(keyChangeRequest(const QString&)), this, SLOT(makeKeyChangeRequest(const QString&)) ); - Q_ASSERT(ok); -} - -BtShortcutsEditor::BtShortcutsEditor(QWidget* parent) - : QWidget(parent), m_table(0) { - init(); -} + : QWidget(parent), m_dlg(new BtShortcutsDialog(this)), m_table(nullptr), m_shortcutChooser(nullptr), m_noneButton(nullptr), m_defaultButton(nullptr), + m_customButton(nullptr), m_defaultLabelValue(nullptr), m_currentRow(-1) { -// initialize this widget -void BtShortcutsEditor::init() { - QVBoxLayout* vBox = new QVBoxLayout(this); + QVBoxLayout * const vBox = new QVBoxLayout(this); setLayout(vBox); - m_table = createShortcutsTable(); + // Create the action and shortcuts table: + m_table = new QTableWidget{this}; + m_table->setColumnCount(3); + m_table->setAlternatingRowColors(true); + m_table->setSelectionBehavior(QAbstractItemView::SelectRows); + m_table->setHorizontalHeaderLabels({tr("Action\nname"), + tr("First\nshortcut"), + tr("Second\nshortcut")}); + m_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); + m_table->horizontalHeader()->resizeSection(0, 180); + m_table->horizontalHeader()->resizeSection(1, 100); + m_table->horizontalHeader()->setStretchLastSection(true); + m_table->verticalHeader()->setVisible(false); + m_table->setShowGrid(false); + BT_CONNECT(m_table, &QTableWidget::cellClicked, + this, &BtShortcutsEditor::changeRow); vBox->addWidget(m_table); - m_shortcutChooser = createShortcutChooser(); + // Create the area below the table where the shortcuts are edited: + m_shortcutChooser = + new QGroupBox(tr("Shortcut for selected action name"), this); + m_shortcutChooser->setFlat(false); + { + QVBoxLayout * const vLayout = new QVBoxLayout(m_shortcutChooser); + { + QHBoxLayout * const hLayout = new QHBoxLayout(); + vLayout->addLayout(hLayout); + + m_noneButton = new QRadioButton(tr("None"), m_shortcutChooser); + hLayout->addWidget(m_noneButton); + BT_CONNECT(m_noneButton, &QRadioButton::clicked, + this, &BtShortcutsEditor::noneButtonClicked); + + m_defaultButton = + new QRadioButton(tr("Default"), m_shortcutChooser); + hLayout->addWidget(m_defaultButton); + BT_CONNECT( + m_defaultButton, &QRadioButton::clicked, + this, &BtShortcutsEditor::defaultButtonClicked); + + m_customButton = new QRadioButton(tr("Custom"), m_shortcutChooser); + hLayout->addWidget(m_customButton); + BT_CONNECT(m_customButton, &QRadioButton::clicked, + this, &BtShortcutsEditor::customButtonClicked); + + m_customPushButton = new QPushButton(m_shortcutChooser); + m_customPushButton->setMinimumWidth(140); + hLayout->addWidget(m_customPushButton); + + hLayout->addItem(new QSpacerItem(1, + 1, + QSizePolicy::Expanding, + QSizePolicy::Minimum)); + } + + QHBoxLayout * const hLayout = new QHBoxLayout(); + vLayout->addLayout(hLayout); + + hLayout->addWidget(new QLabel(tr("Default key:"), m_shortcutChooser)); + + m_defaultLabelValue = new QLabel(m_shortcutChooser); + hLayout->addWidget(m_defaultLabelValue); + + hLayout->addItem(new QSpacerItem(1, + 1, + QSizePolicy::Expanding, + QSizePolicy::Minimum)); + } vBox->addWidget(m_shortcutChooser); + + collection->foreachQAction([this](QAction & action, + QKeySequence const & defaultKeys) + { + int const count = m_table->rowCount(); + m_table->insertRow(count); + + { + BtShortcutsEditorItem * const item = + new BtShortcutsEditorItem{&action}; + try { + /// \todo Remove this & hack and use Qt properties instead: + item->setText(action.text().replace(QRegExp("&(.)"), "\\1")); + item->setIcon(action.icon()); + item->setDefaultKeys(defaultKeys); + item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); + m_table->setItem(count, 0, item); + } catch (...) { + delete item; + throw; + } + } + + QList<QKeySequence> keys = action.shortcuts(); + + { + QTableWidgetItem * const item = new QTableWidgetItem; + try { + item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); + item->setToolTip(tr("Select to change key")); + if (keys.count() > 0) + item->setText(keys[0].toString()); + m_table->setItem(count, 1, item); + } catch (...) { + delete item; + throw; + } + } + + { + QTableWidgetItem * const item = new QTableWidgetItem; + try { + item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); + item->setToolTip(tr("Select to change key")); + if (keys.count() > 1) + item->setText(keys[1].toString()); + m_table->setItem(count, 2, item); + } catch (...) { + delete item; + throw; + } + } + }); + m_table->sortItems(0); + m_table->selectRow(0); + changeRow(0, 0); + BT_CONNECT(m_dlg, &BtShortcutsDialog::keyChangeRequest, + this, &BtShortcutsEditor::makeKeyChangeRequest); } // get the shortcut editor item from the zeroth column of the table @@ -148,77 +259,11 @@ void BtShortcutsEditor::commitChanges() { int rows = m_table->rowCount(); for (int row = 0; row < rows; row++) { BtShortcutsEditorItem* btItem = getShortcutsEditor(row); - if (btItem != 0) + if (btItem != nullptr) btItem->commitChanges(); } } -// puts actions and shortcut keys into QTableWidget -void BtShortcutsEditor::addCollection(BtActionCollection* collection, const QString& title) { - Q_UNUSED(title); /// \todo Is this correct? - - foreach (QAction *action, collection->actions()) { - /// \todo Is the following check really necessary? - if (action) { - int count = m_table->rowCount(); - m_table->insertRow(count); - - QString name = action->text().remove('&'); - QList<QKeySequence> keys = action->shortcuts(); - QIcon icon = action->icon(); - QKeySequence defaultKeys = collection->getDefaultShortcut(action); - - BtShortcutsEditorItem* item = new BtShortcutsEditorItem(action); - item->setText(name); - item->setIcon(icon); - item->setDefaultKeys(defaultKeys); - item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); - m_table->setItem(count, 0, item); - - QTableWidgetItem* item1 = new QTableWidgetItem(); - item1->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); - item1->setToolTip(tr("Select to change key")); - if (keys.count() > 0) - item1->setText(keys[0].toString()); - m_table->setItem(count, 1, item1); - - QTableWidgetItem* item2 = new QTableWidgetItem(); - item2->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); - item2->setToolTip(tr("Select to change key")); - if (keys.count() > 1) - item2->setText(keys[1].toString()); - m_table->setItem(count, 2, item2); - } - } - m_table->sortItems(0); - m_table->selectRow(0); - changeRow(0, 0); -} - -// create the action and shortcuts table -QTableWidget* BtShortcutsEditor::createShortcutsTable() { - QTableWidget* table = new QTableWidget(this); - table->setColumnCount(3); - table->setAlternatingRowColors(true); - table->setSelectionBehavior(QAbstractItemView::SelectRows); - QStringList headerList; - headerList << tr("Action\nname") << tr("First\nshortcut") << tr("Second\nshortcut"); - table->setHorizontalHeaderLabels(headerList); -#if QT_VERSION < 0x050000 - table->horizontalHeader()->setResizeMode(QHeaderView::Interactive); -#else - table->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); -#endif - table->horizontalHeader()->resizeSection(0, 180); - table->horizontalHeader()->resizeSection(1, 100); - table->horizontalHeader()->setStretchLastSection(true); - table->verticalHeader()->setVisible(false); - table->setShowGrid(false); - bool ok = connect(table, SIGNAL(cellClicked(int, int)), this, SLOT(changeRow(int, int))); - Q_ASSERT(ok); - return table; -} - // called when a different action name row is selected void BtShortcutsEditor::changeRow(int row, int column) { Q_UNUSED(column); /// \todo Is this correct? @@ -248,52 +293,6 @@ void BtShortcutsEditor::changeRow(int row, int column) { m_customButton->setChecked(true); } -// create the area below the table where the shortcuts are edited -QWidget* BtShortcutsEditor::createShortcutChooser() { - QGroupBox* box = new QGroupBox(tr("Shortcut for selected action name"), this); - box->setFlat(false); - QVBoxLayout* vLayout = new QVBoxLayout(box); - QHBoxLayout* hLayout = new QHBoxLayout(); - vLayout->addLayout(hLayout); - - m_noneButton = new QRadioButton(tr("None"), box); - hLayout->addWidget(m_noneButton); - bool ok = connect(m_noneButton, SIGNAL(clicked(bool)), this, SLOT(noneButtonClicked(bool))); - Q_ASSERT(ok); - - m_defaultButton = new QRadioButton(tr("Default"), box); - hLayout->addWidget(m_defaultButton); - ok = connect(m_defaultButton, SIGNAL(clicked(bool)), this, SLOT(defaultButtonClicked(bool))); - Q_ASSERT(ok); - - m_customButton = new QRadioButton(tr("Custom"), box); - hLayout->addWidget(m_customButton); - ok = connect(m_customButton, SIGNAL(clicked(bool)), this, SLOT(customButtonClicked(bool))); - Q_ASSERT(ok); - - m_customPushButton = new QPushButton(box); - m_customPushButton->setMinimumWidth(140); - hLayout->addWidget(m_customPushButton); - - QSpacerItem* spacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum); - hLayout->addItem(spacer); - - QHBoxLayout* hLayout2 = new QHBoxLayout(); - vLayout->addLayout(hLayout2); - - QLabel* defaultLabel = new QLabel(tr("Default key:"), box); - hLayout2->addWidget(defaultLabel); - - m_defaultLabelValue = new QLabel(box); - hLayout2->addWidget(m_defaultLabelValue); - - QSpacerItem* spacer2 = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum); - hLayout2->addItem(spacer2); - - return box; -} - - // called when the none radio button is clicked void BtShortcutsEditor::noneButtonClicked(bool checked) { Q_UNUSED(checked); /// \todo Is this correct? |