refs #454 Improve hotkey settings component

This commit is contained in:
Roland Winklmeier
2015-08-14 20:30:35 +02:00
committed by Mathew Sutcliffe
parent 6644c73703
commit 5a82e2e6bf
17 changed files with 1257 additions and 128 deletions

View File

@@ -15,7 +15,7 @@
#include "blackcore/context_network.h"
#include "blackcore/context_settings.h"
#include "blackcore/context_audio.h"
#include "blackmisc/hardware/keyboardkeylist.h"
#include "blackmisc/input/keyboardkeylist.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/settingsblackmiscclasses.h"
#include <QColorDialog>
@@ -29,7 +29,7 @@ using namespace BlackMisc::Audio;
using namespace BlackMisc::PhysicalQuantities;
using namespace BlackMisc::Geo;
using namespace BlackMisc::Settings;
using namespace BlackMisc::Hardware;
using namespace BlackMisc::Input;
namespace BlackGui
{
@@ -80,7 +80,6 @@ namespace BlackGui
//! \todo Settings are loaded twice, this here is for init but each component also consumes the signal changed slot
this->ui->comp_AudioSetup->reloadSettings();
this->ui->comp_SettingsServersComponent->reloadSettings();
this->ui->comp_SettingsHotkeysComponent->reloadSettings();
}
/*

View File

@@ -9,13 +9,13 @@
#include "settingshotkeycomponent.h"
#include "ui_settingshotkeycomponent.h"
#include "blackcore/context_settings.h"
#include "blackmisc/settingutilities.h"
#include "blackmisc/variant.h"
#include "blackgui/hotkeydialog.h"
#include "blackcore/context_application.h"
#include <QMessageBox>
using namespace BlackCore;
using namespace BlackMisc;
using namespace BlackMisc::Settings;
using namespace BlackMisc::Input;
using namespace BlackGui::Models;
namespace BlackGui
{
@@ -27,48 +27,111 @@ namespace BlackGui
ui(new Ui::CSettingsHotkeyComponent)
{
ui->setupUi(this);
ui->tv_hotkeys->setModel(&m_model);
connect(ui->pb_addHotkey, &QPushButton::clicked, this, &CSettingsHotkeyComponent::ps_addEntry);
connect(ui->pb_editHotkey, &QPushButton::clicked, this, &CSettingsHotkeyComponent::ps_editEntry);
connect(ui->pb_removeHotkey, &QPushButton::clicked, this, &CSettingsHotkeyComponent::ps_removeEntry);
ui->tv_hotkeys->selectRow(0);
}
CSettingsHotkeyComponent::~CSettingsHotkeyComponent() { }
void CSettingsHotkeyComponent::runtimeHasBeenSet()
CSettingsHotkeyComponent::~CSettingsHotkeyComponent()
{
Q_ASSERT_X(this->getIContextSettings(), Q_FUNC_INFO, "Missing settings");
this->connect(this->getIContextSettings(), &IContextSettings::changedSettings, this, &CSettingsHotkeyComponent::ps_changedSettings);
// Settings hotkeys
this->connect(this->ui->pb_SettingsCancel, &QPushButton::clicked, this, &CSettingsHotkeyComponent::reloadSettings);
this->connect(this->ui->pb_SettingsSave, &QPushButton::clicked, this, &CSettingsHotkeyComponent::ps_saveHotkeys);
this->connect(this->ui->pb_SettingsRemove, &QPushButton::clicked, this, &CSettingsHotkeyComponent::ps_clearHotkey);
}
void CSettingsHotkeyComponent::reloadSettings()
void CSettingsHotkeyComponent::ps_addEntry()
{
// update hot keys
this->ui->tvp_SettingsMiscHotkeys->updateContainer(this->getIContextSettings()->getHotkeys());
BlackMisc::CIdentifierList registeredApps;
if (getIContextApplication()) registeredApps = getIContextApplication()->getRegisteredApplications();
// add local application
registeredApps.push_back(CIdentifier());
auto selectedActionHotkey = CHotkeyDialog::getActionHotkey(CActionHotkey(), registeredApps, this);
if (selectedActionHotkey.isValid() && checkAndConfirmConflicts(selectedActionHotkey))
{
addHotkeytoSettings(selectedActionHotkey);
int position = m_model.rowCount();
m_model.insertRows(position, 1, QModelIndex());
QModelIndex index = m_model.index(position, 0, QModelIndex());
m_model.setData(index, QVariant::fromValue(selectedActionHotkey), CActionHotkeyListModel::ActionHotkeyRole);
}
}
void CSettingsHotkeyComponent::ps_changedSettings(uint typeValue)
void CSettingsHotkeyComponent::ps_editEntry()
{
IContextSettings::SettingsType type = static_cast<IContextSettings::SettingsType>(typeValue);
this->reloadSettings();
Q_UNUSED(type);
auto index = ui->tv_hotkeys->selectionModel()->currentIndex();
if (!index.isValid()) return;
const auto model = ui->tv_hotkeys->model();
const QModelIndex indexHotkey = model->index(index.row(), 0, QModelIndex());
Q_ASSERT(indexHotkey.data(CActionHotkeyListModel::ActionHotkeyRole).canConvert<CActionHotkey>());
CActionHotkey actionHotkey = indexHotkey.data(CActionHotkeyListModel::ActionHotkeyRole).value<CActionHotkey>();
BlackMisc::CIdentifierList registeredApps;
if (getIContextApplication()) registeredApps = getIContextApplication()->getRegisteredApplications();
// add local application
registeredApps.push_back(CIdentifier());
auto selectedActionHotkey = CHotkeyDialog::getActionHotkey(actionHotkey, registeredApps, this);
if (selectedActionHotkey.isValid() && checkAndConfirmConflicts(selectedActionHotkey, { actionHotkey }))
{
updateHotkeyInSettings(actionHotkey, selectedActionHotkey);
m_model.setData(indexHotkey, QVariant::fromValue(selectedActionHotkey), CActionHotkeyListModel::ActionHotkeyRole);
}
}
void CSettingsHotkeyComponent::ps_saveHotkeys()
void CSettingsHotkeyComponent::ps_removeEntry()
{
const QString path = CSettingUtilities::appendPaths(IContextSettings::PathRoot(), IContextSettings::PathHotkeys());
this->getIContextSettings()->value(path, CSettingUtilities::CmdUpdate(), CVariant::from(this->ui->tvp_SettingsMiscHotkeys->derivedModel()->getContainer()));
QModelIndexList indexes = ui->tv_hotkeys->selectionModel()->selectedRows();
for (const auto &index : indexes)
{
CActionHotkey actionHotkey = index.data(CActionHotkeyListModel::ActionHotkeyRole).value<CActionHotkey>();
removeHotkeyFromSettings(actionHotkey);
m_model.removeRows(index.row(), 1, QModelIndex());
}
}
void CSettingsHotkeyComponent::ps_clearHotkey()
void CSettingsHotkeyComponent::addHotkeytoSettings(const CActionHotkey &actionHotkey)
{
QModelIndex i = this->ui->tvp_SettingsMiscHotkeys->currentIndex();
if (i.row() < 0 || i.row() >= this->ui->tvp_SettingsMiscHotkeys->rowCount()) return;
CSettingKeyboardHotkey hotkey = this->ui->tvp_SettingsMiscHotkeys->at(i);
CSettingKeyboardHotkey defaultHotkey;
defaultHotkey.setFunction(hotkey.getFunction());
this->ui->tvp_SettingsMiscHotkeys->derivedModel()->update(i, defaultHotkey);
CActionHotkeyList actionHotkeyList(m_actionHotkeys.get());
actionHotkeyList.push_back(actionHotkey);
m_actionHotkeys.set(actionHotkeyList);
}
void CSettingsHotkeyComponent::updateHotkeyInSettings(const CActionHotkey &oldValue, const CActionHotkey &newValue)
{
CActionHotkeyList actionHotkeyList(m_actionHotkeys.get());
actionHotkeyList.replace(oldValue, newValue);
m_actionHotkeys.set(actionHotkeyList);
}
void CSettingsHotkeyComponent::removeHotkeyFromSettings(const CActionHotkey &actionHotkey)
{
CActionHotkeyList actionHotkeyList(m_actionHotkeys.get());
actionHotkeyList.remove(actionHotkey);
m_actionHotkeys.set(actionHotkeyList);
}
bool CSettingsHotkeyComponent::checkAndConfirmConflicts(const CActionHotkey &actionHotkey, const CActionHotkeyList &ignore)
{
auto configuredHotkeys = m_actionHotkeys.get();
CActionHotkeyList conflicts = configuredHotkeys.findSupersetsOf(actionHotkey);
conflicts.push_back(configuredHotkeys.findSubsetsOf(actionHotkey));
conflicts.removeIfIn(ignore);
if (!conflicts.isEmpty())
{
QString message = QString("The selected combination conflicts with the following %1 combinations(s):\n\n").arg(conflicts.size());
for (const auto &conflict : conflicts)
{
message += conflict.getCombination().toQString();
message += "\n";
}
message += "\n Do you want to use it anway?";
auto reply = QMessageBox::warning(this, "SettingsHotkeyComponent",
message,
QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if (reply == QMessageBox::No) { return false; }
}
return true;
}
} // ns

View File

@@ -12,18 +12,22 @@
#ifndef BLACKGUI_COMPONENTS_SETTINGSHOTKEYCOMPONENT_H
#define BLACKGUI_COMPONENTS_SETTINGSHOTKEYCOMPONENT_H
#include "blackgui/blackguiexport.h"
#include "blackgui/models/actionhotkeylistmodel.h"
#include "blackgui/components/enableforruntime.h"
#include "blackcore/settings/application.h"
#include <QFrame>
namespace Ui { class CSettingsHotkeyComponent; }
namespace Ui {
class CSettingsHotkeyComponent;
}
namespace BlackGui
{
namespace Components
{
//! Define hotkeys
//! Configure hotkeys
class BLACKGUI_EXPORT CSettingsHotkeyComponent :
public QFrame,
public CEnableForRuntime
@@ -32,31 +36,27 @@ namespace BlackGui
public:
//! Constructor
explicit CSettingsHotkeyComponent(QWidget *parent = nullptr);
CSettingsHotkeyComponent(QWidget *parent = nullptr);
//! Destructor
~CSettingsHotkeyComponent();
//! Reload settings
void reloadSettings();
protected:
//! \copydoc CRuntimeBasedComponent::runtimeHasBeenSet
virtual void runtimeHasBeenSet() override;
private slots:
//! Settings have been changed
void ps_changedSettings(uint typeValue);
//! Save the Hotkeys
void ps_saveHotkeys();
//! Clear single hotkey
void ps_clearHotkey();
void ps_addEntry();
void ps_editEntry();
void ps_removeEntry();
private:
void addHotkeytoSettings(const BlackMisc::Input::CActionHotkey &actionHotkey);
void updateHotkeyInSettings(const BlackMisc::Input::CActionHotkey &oldValue, const BlackMisc::Input::CActionHotkey &newValue);
void removeHotkeyFromSettings(const BlackMisc::Input::CActionHotkey &actionHotkey);
bool checkAndConfirmConflicts(const BlackMisc::Input::CActionHotkey &actionHotkey, const BlackMisc::Input::CActionHotkeyList &ignore = {});
QScopedPointer<Ui::CSettingsHotkeyComponent> ui;
BlackGui::Models::CActionHotkeyListModel m_model;
BlackCore::CSetting<BlackCore::Settings::Application::ActionHotkeys> m_actionHotkeys { this };
void ps_hotkeySlot(bool keyDown);
};
} // ns

View File

@@ -7,11 +7,11 @@
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<height>193</height>
</rect>
</property>
<property name="windowTitle">
<string>Frame</string>
<string>sample_hotkeys</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
@@ -19,102 +19,88 @@
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="vl_SettingsHotkeyComponent">
<property name="spacing">
<number>3</number>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
<number>1</number>
</property>
<property name="topMargin">
<number>0</number>
<number>1</number>
</property>
<property name="rightMargin">
<number>0</number>
<number>1</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="BlackGui::Views::CKeyboardKeyView" name="tvp_SettingsMiscHotkeys">
<property name="cornerButtonEnabled">
<bool>true</bool>
<item row="1" column="1">
<widget class="QPushButton" name="pb_addHotkey">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="pb_editHotkey">
<property name="text">
<string>Edit</string>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QPushButton" name="pb_removeHotkey">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0" colspan="5">
<widget class="QTableView" name="tv_hotkeys">
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderStretchLastSection">
<bool>false</bool>
</attribute>
</widget>
</item>
<item>
<widget class="QFrame" name="fr_SettingsHotkeyComponent">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="hl_SettingsMisc">
<item>
<widget class="QPushButton" name="pb_SettingsSave">
<property name="text">
<string>Save</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pb_SettingsRemove">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pb_SettingsCancel">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="vs_SettingsHotkeysWorkaround">
<item row="3" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
<enum>QSizePolicy::MinimumExpanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>BlackGui::Views::CKeyboardKeyView</class>
<extends>QTableView</extends>
<header>blackgui/views/keyboardkeyview.h</header>
</customwidget>
</customwidgets>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,251 @@
#include "hotkeydialog.h"
#include "ui_hotkeydialog.h"
#include "blackgui/stylesheetutility.h"
#include "blackmisc/input/hotkeycombination.h"
#include "blackmisc/icon.h"
#include "blackmisc/logmessage.h"
#include <QIcon>
#include <QMessageBox>
using namespace BlackMisc;
using namespace BlackMisc::Input;
namespace BlackGui
{
CKeySelectionBox::CKeySelectionBox(QWidget *parent) :
QComboBox(parent)
{
connect(this, static_cast<void(CKeySelectionBox::*)(int)>(&CKeySelectionBox::currentIndexChanged), this, &CKeySelectionBox::ps_updateSelectedIndex);
}
void CKeySelectionBox::setSelectedIndex(int index)
{
m_oldIndex = index;
setCurrentIndex(m_oldIndex);
}
void CKeySelectionBox::ps_updateSelectedIndex(int index)
{
emit keySelectionChanged(m_oldIndex, index);
m_oldIndex = index;
}
CHotkeyDialog::CHotkeyDialog(const BlackMisc::Input::CActionHotkey &actionHotkey, QWidget *parent) :
QDialog(parent),
ui(new Ui::CHotkeyDialog),
m_actionHotkey(actionHotkey),
m_actionModel(this)
{
m_inputManager = BlackCore::CInputManager::instance();
ui->setupUi(this);
ui->advancedFrame->hide();
ui->tv_actions->setModel(&m_actionModel);
ui->pb_advancedMode->setIcon(BlackMisc::CIcons::arrowMediumSouth16());
selectAction();
if (!actionHotkey.getCombination().isEmpty()) { ui->pb_selectedHotkey->setText(actionHotkey.getCombination().toQString()); }
connect(ui->pb_advancedMode, &QPushButton::clicked, this, &CHotkeyDialog::ps_advancedModeChanged);
connect(ui->pb_selectedHotkey, &QPushButton::clicked, this, &CHotkeyDialog::ps_selectHotkey);
connect(ui->pb_accept, &QPushButton::clicked, this, &CHotkeyDialog::ps_accept);
connect(ui->pb_cancel, &QPushButton::clicked, this, &CHotkeyDialog::reject);
connect(m_inputManager, &BlackCore::CInputManager::combinationSelectionChanged, this, &CHotkeyDialog::ps_combinationSelectionChanged);
connect(m_inputManager, &BlackCore::CInputManager::combinationSelectionFinished, this, &CHotkeyDialog::ps_combinationSelectionFinished);
connect(ui->tv_actions->selectionModel(), &QItemSelectionModel::selectionChanged, this, &CHotkeyDialog::ps_changeSelectedAction);
initStyleSheet();
}
CHotkeyDialog::~CHotkeyDialog()
{
}
void CHotkeyDialog::setRegisteredApplications(const BlackMisc::CIdentifierList &applications)
{
for (const auto & app : applications)
{
ui->cb_identifier->addItem(app.getMachineName(), QVariant::fromValue(app));
}
}
void CHotkeyDialog::initStyleSheet()
{
const QString s = CStyleSheetUtility::instance().styles(
{
CStyleSheetUtility::fileNameFonts(),
CStyleSheetUtility::fileNameStandardWidget()
}
);
setStyleSheet(s);
}
CActionHotkey CHotkeyDialog::getActionHotkey(const CActionHotkey &initial, const CIdentifierList &applications, QWidget *parent)
{
CHotkeyDialog editDialog(initial, parent);
editDialog.setWindowModality(Qt::WindowModal);
// add local application
editDialog.setRegisteredApplications(applications);
if (editDialog.exec()) { return editDialog.getSelectedActionHotkey(); }
else { return {}; }
}
void CHotkeyDialog::ps_advancedModeChanged()
{
if (m_actionHotkey.getCombination().isEmpty()) return;
if (!ui->advancedFrame->isVisible())
{
setupAdvancedFrame();
ui->advancedFrame->show();
ui->pb_advancedMode->setIcon(BlackMisc::CIcons::arrowMediumNorth16());
}
else
{
ui->pb_advancedMode->setIcon(BlackMisc::CIcons::arrowMediumSouth16());
ui->advancedFrame->hide();
ui->gb_hotkey->resize(0, 0);
}
}
void CHotkeyDialog::ps_selectHotkey()
{
ui->pb_selectedHotkey->setText("Press any key/button...");
m_inputManager->startCapture();
}
void CHotkeyDialog::ps_combinationSelectionChanged(const BlackMisc::Input::CHotkeyCombination &combination)
{
ui->pb_selectedHotkey->setText(combination.toFormattedQString());
}
void CHotkeyDialog::ps_combinationSelectionFinished(const BlackMisc::Input::CHotkeyCombination &combination)
{
m_actionHotkey.setCombination(combination);
synchronize();
}
void CHotkeyDialog::ps_changeSelectedAction(const QItemSelection &selected, const QItemSelection &deselected)
{
Q_UNUSED(deselected);
const auto index = selected.indexes().first();
m_actionHotkey.setAction(index.data(Models::CActionModel::ActionRole).toString());
}
void CHotkeyDialog::ps_accept()
{
if (m_actionHotkey.getApplicableMachine().getMachineName().isEmpty())
{
CLogMessage().validationWarning("Missing %1") << ui->gb_machine->title();
return;
}
if (m_actionHotkey.getCombination().isEmpty())
{
CLogMessage().validationWarning("Missing %1") << ui->gb_hotkey->title();
return;
}
if (m_actionHotkey.getAction().isEmpty())
{
CLogMessage().validationWarning("Missing %1") << ui->gb_action->title();
return;
}
QDialog::accept();
}
void CHotkeyDialog::synchronize()
{
synchronizeSimpleSelection();
synchronizeAdvancedSelection();
}
void CHotkeyDialog::synchronizeSimpleSelection()
{
ui->pb_selectedHotkey->setText(m_actionHotkey.getCombination().toFormattedQString());
}
void CHotkeyDialog::synchronizeAdvancedSelection()
{
if (ui->advancedFrame->isVisible()) { setupAdvancedFrame(); }
}
void CHotkeyDialog::setupAdvancedFrame()
{
clearAdvancedFrame();
auto allSupportedKeys = CKeyboardKeyList::allSupportedKeys();
QStringList splittedKeys = m_actionHotkey.getCombination().toQString().split('+', QString::SkipEmptyParts);
for (const auto &splittedKey : splittedKeys)
{
if (splittedKey == "+") continue;
int currentIndex = -1;
CKeySelectionBox *ksb = new CKeySelectionBox(ui->advancedFrame);
for (const auto &supportedKey : allSupportedKeys)
{
QString supportedKeyAsString = supportedKey.toQString();
ksb->addItem(supportedKeyAsString, QVariant::fromValue(supportedKey));
if (supportedKeyAsString == splittedKey)
{
currentIndex = ksb->count() - 1;
}
}
ksb->setSelectedIndex(currentIndex);
ui->advancedFrame->layout()->addWidget(ksb);
int position = ui->advancedFrame->layout()->count() - 1;
ksb->setProperty("position", position);
connect(ksb, &CKeySelectionBox::keySelectionChanged, this, &CHotkeyDialog::advancedKeyChanged);
}
}
void CHotkeyDialog::clearAdvancedFrame()
{
QLayout *layout = ui->advancedFrame->layout();
QLayoutItem *child;
while ((child = layout->takeAt(0)) != 0)
{
if (child->widget()) child->widget()->deleteLater();
delete child;
}
}
void CHotkeyDialog::advancedKeyChanged(int oldIndex, int newIndex)
{
CKeySelectionBox* ksb = qobject_cast<CKeySelectionBox *>(sender());
Q_ASSERT(ksb);
CKeyboardKey oldKey = ksb->itemData(oldIndex).value<CKeyboardKey>();
CKeyboardKey newKey = ksb->itemData(newIndex).value<CKeyboardKey>();
auto combination = m_actionHotkey.getCombination();
combination.replaceKey(oldKey, newKey);
m_actionHotkey.setCombination(combination);
synchronize();
}
void CHotkeyDialog::selectAction()
{
if (m_actionHotkey.getAction().isEmpty()) return;
const auto tokens = m_actionHotkey.getAction().split("/", QString::SkipEmptyParts);
QModelIndex parentIndex = QModelIndex();
for (const auto &token : tokens)
{
QModelIndex startIndex = m_actionModel.index(0, 0, parentIndex);
auto indexList = m_actionModel.match(startIndex, Qt::DisplayRole, QVariant::fromValue(token));
if (indexList.isEmpty()) return;
parentIndex = indexList.first();
ui->tv_actions->expand(parentIndex);
}
QItemSelectionModel * selectionModel = ui->tv_actions->selectionModel();
selectionModel->select(parentIndex, QItemSelectionModel::Select);
}
}

108
src/blackgui/hotkeydialog.h Normal file
View File

@@ -0,0 +1,108 @@
/* Copyright (C) 2014
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_HOTKEYDIALOG_H
#define BLACKGUI_HOTKEYDIALOG_H
#include "blackgui/models/actionmodel.h"
#include "blackcore/input_manager.h"
#include "blackmisc/input/hotkeycombination.h"
#include "blackmisc/input/actionhotkeylist.h"
#include "blackmisc/identifierlist.h"
#include <QDialog>
#include <QComboBox>
namespace Ui
{
class CHotkeyDialog;
}
namespace BlackGui
{
/*!
* Combobox for selecting keyboard keys
*/
class CKeySelectionBox : public QComboBox
{
Q_OBJECT
public:
//! Constructor
CKeySelectionBox(QWidget *parent = nullptr);
//! Set key with index as selected
void setSelectedIndex(int index);
signals:
//! User has changed the selection
void keySelectionChanged(int oldIndex, int newIndex);
private slots:
void ps_updateSelectedIndex(int index);
private:
int m_oldIndex;
};
/*!
* Hotkey dialog
*/
class CHotkeyDialog : public QDialog
{
Q_OBJECT
public:
//! Constructor
CHotkeyDialog(const BlackMisc::Input::CActionHotkey &actionHotkey, QWidget *parent = nullptr);
//! Destructor
~CHotkeyDialog();
//! Get hotkey selected by user
BlackMisc::Input::CActionHotkey getSelectedActionHotkey() const { return m_actionHotkey; }
//! Set registered applications
void setRegisteredApplications(const BlackMisc::CIdentifierList &applications);
//! Init style sheet
void initStyleSheet();
//! getHotkey runs the hotkey dialog and returns the result
static BlackMisc::Input::CActionHotkey getActionHotkey(const BlackMisc::Input::CActionHotkey &initial, const BlackMisc::CIdentifierList &applications,
QWidget *parent = nullptr);
private:
void ps_advancedModeChanged();
void ps_selectHotkey();
void ps_combinationSelectionChanged(const BlackMisc::Input::CHotkeyCombination &combination);
void ps_combinationSelectionFinished(const BlackMisc::Input::CHotkeyCombination &combination);
void ps_changeSelectedAction(const QItemSelection &selected, const QItemSelection &deselected);
void ps_accept();
void synchronize();
void synchronizeSimpleSelection();
void synchronizeAdvancedSelection();
void setupAdvancedFrame();
void clearAdvancedFrame();
void advancedKeyChanged(int oldIndex, int newIndex);
void selectAction();
QScopedPointer<Ui::CHotkeyDialog> ui;
BlackMisc::Input::CActionHotkey m_actionHotkey;
BlackGui::Models::CActionModel m_actionModel;
BlackCore::CInputManager *m_inputManager;
};
}
#endif

View File

@@ -0,0 +1,167 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CHotkeyDialog</class>
<widget class="QDialog" name="CHotkeyDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>269</width>
<height>384</height>
</rect>
</property>
<property name="windowTitle">
<string>Edit hotkey</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0" colspan="3">
<widget class="QGroupBox" name="gb_machine">
<property name="title">
<string>Machine</string>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<widget class="QComboBox" name="cb_identifier"/>
</item>
</layout>
</widget>
</item>
<item row="1" column="0" colspan="3">
<widget class="QGroupBox" name="gb_hotkey">
<property name="title">
<string>Combination</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="3">
<widget class="QPushButton" name="pb_advancedMode">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>21</width>
<height>21</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>21</width>
<height>21</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QRadioButton" name="rb_press">
<property name="text">
<string>Press</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="0" colspan="3">
<widget class="QPushButton" name="pb_selectedHotkey">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>[Select]</string>
</property>
</widget>
</item>
<item row="2" column="2" colspan="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<widget class="QRadioButton" name="rb_release">
<property name="text">
<string>Release</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="4">
<widget class="QFrame" name="advancedFrame">
<property name="enabled">
<bool>true</bool>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout"/>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="0" colspan="3">
<widget class="QGroupBox" name="gb_action">
<property name="title">
<string>Action</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QTreeView" name="tv_actions">
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="1">
<widget class="QPushButton" name="pb_accept">
<property name="text">
<string>Ok</string>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QPushButton" name="pb_cancel">
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,136 @@
/* Copyright (C) 2013
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "actionhotkeylistmodel.h"
#include "blackmisc/blackmiscfreefunctions.h"
using namespace BlackMisc;
using namespace BlackMisc::Input;
namespace BlackGui
{
namespace Models
{
CActionHotkeyListModel::CActionHotkeyListModel(QObject *parent) :
QAbstractTableModel(parent)
{
}
int CActionHotkeyListModel::rowCount(const QModelIndex & /** parent **/) const
{
return m_actionHotkeys.size();
}
int CActionHotkeyListModel::columnCount(const QModelIndex & /** parent **/) const
{
return 3;
}
QVariant CActionHotkeyListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) { return QVariant(); }
if (index.row() >= m_actionHotkeys.size() || index.row() < 0) { return QVariant(); }
if (role == Qt::DisplayRole)
{
if (index.column() == 0)
{
BlackMisc::CIdentifier identifier = m_actionHotkeys[index.row()].getApplicableMachine();
return identifier.getMachineName();
}
if (index.column() == 1)
{
CHotkeyCombination combination = m_actionHotkeys[index.row()].getCombination();
return combination.toQString();
}
if (index.column() == 2)
{
return m_actionHotkeys[index.row()].getAction();
}
}
else if (role == ActionHotkeyRole)
{
auto hotkey = m_actionHotkeys[index.row()];
return QVariant::fromValue(hotkey);
}
return {};
}
QVariant CActionHotkeyListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal) {
switch (section)
{
case 0:
return QStringLiteral("Machine");
case 1:
return QStringLiteral("Combination");
case 2:
return QStringLiteral("Action");
}
}
}
return {};
}
bool CActionHotkeyListModel::insertRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row)
{
m_actionHotkeys.push_back(BlackMisc::Input::CActionHotkey());
}
endInsertRows();
return true;
}
bool CActionHotkeyListModel::removeRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position + rows - 1);
Q_ASSERT(position + rows - 1 < m_actionHotkeys.size());
for (int row = 0; row < rows; ++row)
{
auto toRemove = m_actionHotkeys[position + row];
m_actionHotkeys.remove(toRemove);
}
endRemoveRows();
return true;
}
bool CActionHotkeyListModel::setData(const QModelIndex &index, const QVariant &var, int role)
{
if (index.isValid() && role == ActionHotkeyRole)
{
m_actionHotkeys[index.row()] = var.value<BlackMisc::Input::CActionHotkey>();
emit dataChanged(index, index);
return true;
}
return false;
}
void CActionHotkeyListModel::clear()
{
beginResetModel();
m_actionHotkeys.clear();
endResetModel();
}
}
} // namespace

View File

@@ -0,0 +1,70 @@
/* Copyright (C) 2013
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_ACTIONHOTKEYLISTMODEL_H
#define BLACKGUI_ACTIONHOTKEYLISTMODEL_H
#include "blackgui/blackguiexport.h"
#include "blackmisc/input/actionhotkeylist.h"
#include <QAbstractTableModel>
namespace BlackGui
{
namespace Models
{
//! Hotkey list model
class BLACKGUI_EXPORT CActionHotkeyListModel : public QAbstractTableModel
{
Q_OBJECT
public:
//! Item role
enum ItemRole
{
ActionHotkeyRole = Qt::UserRole
};
//! Constructor
CActionHotkeyListModel(QObject *parent = nullptr);
//! Destructor
virtual ~CActionHotkeyListModel() {}
//! \copydoc QAbstractTableModel::rowCount
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
//! \copydoc QAbstractTableModel::columCount
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
//! \copydoc QAbstractTableModel::data
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
//! \copydoc QAbstractTableModel::setData
bool setData(const QModelIndex &index, const QVariant &var, int role) override;
//! \copydoc QAbstractTableModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
//! \copydoc QAbstractTableModel::insertRows
bool insertRows(int position, int rows, const QModelIndex &index) override;
//! \copydoc QAbstractTableModel::removeRows
bool removeRows(int position, int rows, const QModelIndex &index) override;
//! Clear model
void clear();
private:
BlackMisc::Input::CActionHotkeyList m_actionHotkeys;
};
}
}
#endif // guard

View File

@@ -0,0 +1,71 @@
#include "actionitem.h"
namespace BlackGui
{
namespace Models
{
ActionItem::ActionItem(const QString &action, const QString &name, ActionItem *parent) :
m_action(action), m_actionName(name), m_parentItem(parent)
{
}
ActionItem::~ActionItem()
{
qDeleteAll(m_childItems);
}
void ActionItem::appendChild(ActionItem *item)
{
m_childItems.append(item);
}
ActionItem *ActionItem::findChildByName(const QString &name)
{
for (auto child : m_childItems)
{
if (child->getActionName() == name) return child;
}
return nullptr;
}
ActionItem *ActionItem::getChildByRow(int row)
{
return m_childItems.value(row);
}
int ActionItem::getChildCount() const
{
return m_childItems.count();
}
int ActionItem::getColumnCount() const
{
return 1;
}
QString ActionItem::getAction() const
{
return m_action;
}
QString ActionItem::getActionName() const
{
return m_actionName;
}
ActionItem *ActionItem::getParentItem()
{
return m_parentItem;
}
int ActionItem::getRow() const
{
if (m_parentItem) { return m_parentItem->m_childItems.indexOf(const_cast<ActionItem *>(this)); }
return 0;
}
}
}

View File

@@ -0,0 +1,70 @@
/* Copyright (C) 2015
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_ACTIONITEM_H
#define BLACKGUI_ACTIONITEM_H
#include <QString>
#include <QVariant>
namespace BlackGui
{
namespace Models
{
//! One single action item in a tree
class ActionItem
{
public:
//! Constructor
ActionItem(const QString &action, const QString &name, ActionItem *parentItem = nullptr);
//! Destructor
~ActionItem();
//! Append a new child
void appendChild(ActionItem *child);
//! Find child by its name
ActionItem *findChildByName(const QString &name);
//! Get child by row
ActionItem *getChildByRow(int row);
//! Number of childs
int getChildCount() const;
//! Number of columns
int getColumnCount() const;
//! Returns the stored action
QString getAction() const;
//! Get action name
QString getActionName() const;
//! Get row of this item
int getRow() const;
//! Get parent item
ActionItem *getParentItem();
private:
QList<ActionItem *> m_childItems;
QString m_action;
QString m_actionName;
ActionItem *m_parentItem;
};
}
}
#endif // guard

View File

@@ -0,0 +1,103 @@
#include "actionmodel.h"
#include "actionitem.h"
#include "blackcore/input_manager.h"
namespace BlackGui
{
namespace Models
{
CActionModel::CActionModel(QObject *parent) :
QAbstractItemModel(parent),
m_rootItem(new ActionItem(QString(), QString()))
{
setupModelData();
}
CActionModel::~CActionModel()
{
}
int CActionModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid()) { return static_cast<ActionItem *>(parent.internalPointer())->getColumnCount(); }
else { return m_rootItem->getColumnCount(); }
}
QVariant CActionModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) { return QVariant(); }
ActionItem *item = static_cast<ActionItem *>(index.internalPointer());
if (role == Qt::DisplayRole) { return item->getActionName(); }
if (role == ActionRole) { return item->getAction(); }
return {};
}
Qt::ItemFlags CActionModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) { return 0; }
return QAbstractItemModel::flags(index);
}
QModelIndex CActionModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent)) { return QModelIndex(); }
ActionItem *parentItem;
if (!parent.isValid()) { parentItem = m_rootItem.data(); }
else { parentItem = static_cast<ActionItem *>(parent.internalPointer()); }
ActionItem *childItem = parentItem->getChildByRow(row);
if (childItem) { return createIndex(row, column, childItem); }
else { return {}; }
}
QModelIndex CActionModel::parent(const QModelIndex &index) const
{
if (!index.isValid()) { return {}; }
ActionItem *childItem = static_cast<ActionItem *>(index.internalPointer());
ActionItem *parentItem = childItem->getParentItem();
if (parentItem == m_rootItem.data()) { return {}; }
return createIndex(parentItem->getRow(), 0, parentItem);
}
int CActionModel::rowCount(const QModelIndex &parent) const
{
ActionItem *parentItem;
if (parent.column() > 0) { return 0; }
if (!parent.isValid()) { parentItem = m_rootItem.data(); }
else { parentItem = static_cast<ActionItem *>(parent.internalPointer()); }
return parentItem->getChildCount();
}
void CActionModel::setupModelData()
{
m_rootItem.reset(new ActionItem(QString(), QString()));
for (const auto &actionPath : BlackCore::CInputManager::instance()->allAvailableActions())
{
const auto tokens = actionPath.split("/", QString::SkipEmptyParts);
ActionItem *parentItem = m_rootItem.data();
for (const auto &token : tokens)
{
ActionItem *child = parentItem->findChildByName(token);
if (child == nullptr)
{
child = new ActionItem(actionPath, token, parentItem);
parentItem->appendChild(child);
}
Q_ASSERT(child);
parentItem = child;
}
}
}
}
}

View File

@@ -0,0 +1,74 @@
/* Copyright (C) 2015
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_ACTIONMODEL_H
#define BLACKGUI_ACTIONMODEL_H
#include "blackcore/actionbind.h"
#include <QAbstractItemModel>
#include <QScopedPointer>
namespace BlackGui
{
namespace Models
{
class ActionItem;
/*!
* Action tree model
*/
class CActionModel : public QAbstractItemModel
{
Q_OBJECT
public:
//! User roles
enum ItemRole
{
ActionRole = Qt::UserRole
};
//! Constructor
CActionModel(QObject *parent = nullptr);
//! Destructor
~CActionModel();
//! \copydoc QAbstractItemModel::data
QVariant data(const QModelIndex &index, int role) const override;
//! \copydoc QAbstractItemModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override;
//! \copydoc QAbstractItemModel::index
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
//! \copydoc QAbstractItemModel::parent
QModelIndex parent(const QModelIndex &index) const override;
//! \copydoc QAbstractItemModel::rowCount
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
//! \copydoc QAbstractItemModel::columnCount
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
private:
void setupModelData();
QScopedPointer<ActionItem> m_rootItem;
};
}
}
#endif // guard

View File

@@ -20,6 +20,7 @@
#include "blackmisc/network/textmessagelist.h"
#include "blackmisc/network/aircraftmappinglist.h"
#include "blackmisc/setkeyboardhotkeylist.h"
#include "blackmisc/input/actionhotkeylist.h"
#include "blackmisc/simulation/simulatedaircraftlist.h"
#include "blackmisc/simulation/aircraftmodellist.h"
#include "blackmisc/simulation/distributorlist.h"

View File

@@ -25,6 +25,6 @@
#include "blackgui/models/liverylistmodel.h"
#include "blackgui/models/distributorlistmodel.h"
#include "blackgui/models/keyboardkeylistmodel.h"
#include "blackgui/models/actionhotkeylistmodel.h"
#endif // guard

View File

@@ -29,6 +29,24 @@ QMainWindow::separator:hover {
background: transparent;
}
/** Main window **/
QDialog {
background-image: url(:/textures/icons/textures/texture-outer.jpg);
background-color: darkslategray;
}
/** separator between info areas and rest **/
/** this hides them **/
QDialog::separator {
background: transparent;
width: 0px; /* when vertical */
height: 0px; /* when horizontal */
}
QDialog::separator:hover {
background: transparent;
}
/**
Required when dock widget is floating
1) background-image not working on QDockWidget, so I use direct children for that

View File

@@ -139,3 +139,15 @@ QAbstractScrollArea #pg_StatusPageCons ole { background-color: black; }
padding: 3px;
border-radius: 5px;
}
#gb_hotkey {
background-image: url(:/textures/icons/textures/texture-inner.jpg);
}
#gb_action {
background-image: url(:/textures/icons/textures/texture-inner.jpg);
}
#gb_machine {
background-image: url(:/textures/icons/textures/texture-inner.jpg);
}