mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-05 17:35:34 +08:00
refs #454 Improve hotkey settings component
This commit is contained in:
committed by
Mathew Sutcliffe
parent
6644c73703
commit
5a82e2e6bf
@@ -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();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user