mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-15 01:05:35 +08:00
refs #139 list for keyboard keys and the corresponding models for the GUI
This commit is contained in:
119
src/blackgui/keyboardkeylistmodel.cpp
Normal file
119
src/blackgui/keyboardkeylistmodel.cpp
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
#include "keyboardkeylistmodel.h"
|
||||||
|
#include "blackmisc/blackmiscfreefunctions.h"
|
||||||
|
#include <QMetaProperty>
|
||||||
|
#include <QComboBox>
|
||||||
|
#include <QTableView>
|
||||||
|
#include <QEvent>
|
||||||
|
#include <QKeyEvent>
|
||||||
|
|
||||||
|
|
||||||
|
using namespace BlackMisc::Hardware;
|
||||||
|
|
||||||
|
namespace BlackGui
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
CKeyboardKeyListModel::CKeyboardKeyListModel(QObject *parent) :
|
||||||
|
CListModelBase<BlackMisc::Hardware::CKeyboardKey, BlackMisc::Hardware::CKeyboardKeyList>("ViewKeyboardKeyList", parent)
|
||||||
|
{
|
||||||
|
this->m_columns.addColumn(CColumn("key", CKeyboardKey::IndexKeyAsStringRepresentation, true));
|
||||||
|
this->m_columns.addColumn(CColumn("modifier 1", CKeyboardKey::IndexModifier1AsString, true));
|
||||||
|
this->m_columns.addColumn(CColumn("modifier 2", CKeyboardKey::IndexModifier2AsString, true));
|
||||||
|
this->m_columns.addColumn(CColumn("function", CKeyboardKey::IndexFunctionAsString));
|
||||||
|
|
||||||
|
this->setSortColumnByPropertyIndex(CKeyboardKey::IndexFunctionAsString);
|
||||||
|
this->m_sortOrder = Qt::AscendingOrder;
|
||||||
|
|
||||||
|
// force strings for translation in resource files
|
||||||
|
(void)QT_TRANSLATE_NOOP("ViewKeyboardKeyList", "modifier 1");
|
||||||
|
(void)QT_TRANSLATE_NOOP("ViewKeyboardKeyList", "modifier 2");
|
||||||
|
(void)QT_TRANSLATE_NOOP("ViewKeyboardKeyList", "function");
|
||||||
|
(void)QT_TRANSLATE_NOOP("ViewKeyboardKeyList", "key");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Display icons
|
||||||
|
*/
|
||||||
|
QVariant CKeyboardKeyListModel::data(const QModelIndex &modelIndex, int role) const
|
||||||
|
{
|
||||||
|
// shortcut, fast check
|
||||||
|
return CListModelBase::data(modelIndex, role);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CKeyboardKeyListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||||
|
{
|
||||||
|
if (role == Qt::EditRole)
|
||||||
|
{
|
||||||
|
if (index.row() >= this->m_container.size()) return true;
|
||||||
|
CKeyboardKey key = this->m_container[index.row()];
|
||||||
|
int propertyIndex = this->columnToPropertyIndex(index.column());
|
||||||
|
key.setPropertyByIndex(value, propertyIndex);
|
||||||
|
key.cleanup();
|
||||||
|
this->m_container[index.row()] = key;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
QWidget *CKeyboardKeyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
const CKeyboardKeyListModel *model = this->model();
|
||||||
|
Q_ASSERT(model);
|
||||||
|
|
||||||
|
if (index.row() < model->rowCount())
|
||||||
|
{
|
||||||
|
int pi = model->columnToPropertyIndex(index.column());
|
||||||
|
if (pi == CKeyboardKey::IndexModifier1 || pi == CKeyboardKey::IndexModifier2 || pi == CKeyboardKey::IndexModifier1AsString || pi == CKeyboardKey::IndexModifier2AsString)
|
||||||
|
{
|
||||||
|
CKeyboardKey key = model->at(index);
|
||||||
|
QString v = (pi == CKeyboardKey::IndexModifier1 || pi == CKeyboardKey::IndexModifier1AsString) ? key.getModifier1AsString() : key.getModifier2AsString();
|
||||||
|
QComboBox *edit = new QComboBox(parent);
|
||||||
|
edit->addItems(CKeyboardKey::modifiers());
|
||||||
|
edit->setCurrentText(v);
|
||||||
|
return edit;
|
||||||
|
}
|
||||||
|
else if (pi == CKeyboardKey::IndexKey || pi == CKeyboardKey::IndexKeyAsString || pi == CKeyboardKey::IndexKeyAsStringRepresentation)
|
||||||
|
{
|
||||||
|
CKeyboardKey key = model->at(index);
|
||||||
|
CKeyboardLineEdit *edit = new CKeyboardLineEdit(parent);
|
||||||
|
edit->setText(key.getKeyAsString());
|
||||||
|
return edit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return QItemDelegate::createEditor(parent, option, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CKeyboardKeyItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
QItemDelegate::setEditorData(editor, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CKeyboardKeyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)
|
||||||
|
{
|
||||||
|
QItemDelegate::setModelData(editor, model, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CKeyboardKeyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index)
|
||||||
|
{
|
||||||
|
QItemDelegate::updateEditorGeometry(editor, option, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
const CKeyboardKeyListModel *CKeyboardKeyItemDelegate::model() const
|
||||||
|
{
|
||||||
|
if (this->parent())
|
||||||
|
{
|
||||||
|
QTableView *tableView = dynamic_cast<QTableView *>(this->parent());
|
||||||
|
if (tableView)
|
||||||
|
{
|
||||||
|
return dynamic_cast<CKeyboardKeyListModel *>(tableView->model());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CKeyboardLineEdit::keyPressEvent(QKeyEvent *event)
|
||||||
|
{
|
||||||
|
this->setText(CKeyboardKey::toStringRepresentation(event->key()));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
83
src/blackgui/keyboardkeylistmodel.h
Normal file
83
src/blackgui/keyboardkeylistmodel.h
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
#ifndef BLACKGUI_KEYBOARDKEYLISTMODEL_H
|
||||||
|
#define BLACKGUI_KEYBOARDKEYLISTMODEL_H
|
||||||
|
|
||||||
|
#include "blackmisc/hwkeyboardkeylist.h"
|
||||||
|
#include "blackgui/listmodelbase.h"
|
||||||
|
#include <QAbstractItemModel>
|
||||||
|
#include <QDBusConnection>
|
||||||
|
#include <QItemDelegate>
|
||||||
|
#include <QLineEdit>
|
||||||
|
|
||||||
|
namespace BlackGui
|
||||||
|
{
|
||||||
|
/*!
|
||||||
|
* \brief Keyboard key list model
|
||||||
|
*/
|
||||||
|
class CKeyboardKeyListModel : public CListModelBase<BlackMisc::Hardware::CKeyboardKey, BlackMisc::Hardware::CKeyboardKeyList>
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
//! \brief Constructor
|
||||||
|
explicit CKeyboardKeyListModel(QObject *parent = nullptr);
|
||||||
|
|
||||||
|
//! \brief Destructor
|
||||||
|
virtual ~CKeyboardKeyListModel() {}
|
||||||
|
|
||||||
|
//! \copydoc CListModelBase::data
|
||||||
|
QVariant data(const QModelIndex &modelIndex, int role = Qt::DisplayRole) const override;
|
||||||
|
|
||||||
|
//! \copydoc BlackMisc::Hardware::CKeyboardKeyList::initAsHotkeyList
|
||||||
|
void initAsHotkeyList() { this->m_container.initAsHotkeyList(); }
|
||||||
|
|
||||||
|
//! \copydoc QAbstractTableModel::setData
|
||||||
|
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Special edit delegate for key sequence
|
||||||
|
*/
|
||||||
|
class CKeyboardKeyItemDelegate : public QItemDelegate
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
//! \brief Constructor
|
||||||
|
explicit CKeyboardKeyItemDelegate(QObject *parent = nullptr) : QItemDelegate(parent) {}
|
||||||
|
|
||||||
|
//! \copydoc QItemDelegate::createEditor
|
||||||
|
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||||
|
|
||||||
|
//! \copydoc QItemDelegate::setEditorData
|
||||||
|
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
|
||||||
|
|
||||||
|
//! \copydoc QItemDelegate::setModelData
|
||||||
|
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index);
|
||||||
|
|
||||||
|
//! \copydoc QItemDelegate::updateEditorGeometry
|
||||||
|
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index);
|
||||||
|
|
||||||
|
//! \brief correspondig model
|
||||||
|
const CKeyboardKeyListModel *model() const;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Special edit widget for key sequence
|
||||||
|
*/
|
||||||
|
class CKeyboardLineEdit : public QLineEdit
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
//! Constructor
|
||||||
|
CKeyboardLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) { }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
//! \brief Overriding and handling key press
|
||||||
|
virtual void keyPressEvent(QKeyEvent *event) override;
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif // guard
|
||||||
49
src/blackmisc/hwkeyboardkeylist.cpp
Normal file
49
src/blackmisc/hwkeyboardkeylist.cpp
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
/* Copyright (C) 2013 VATSIM Community / authors
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
#include "hwkeyboardkeylist.h"
|
||||||
|
#include "predicates.h"
|
||||||
|
|
||||||
|
namespace BlackMisc
|
||||||
|
{
|
||||||
|
namespace Hardware
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
CKeyboardKeyList::CKeyboardKeyList() { }
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Construct from base class object
|
||||||
|
*/
|
||||||
|
CKeyboardKeyList::CKeyboardKeyList(const CSequence<CKeyboardKey> &baseClass) :
|
||||||
|
CSequence<CKeyboardKey>(baseClass)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Register metadata
|
||||||
|
*/
|
||||||
|
void CKeyboardKeyList::registerMetadata()
|
||||||
|
{
|
||||||
|
qRegisterMetaType<BlackMisc::CSequence<CKeyboardKey>>();
|
||||||
|
qDBusRegisterMetaType<BlackMisc::CSequence<CKeyboardKey>>();
|
||||||
|
qRegisterMetaType<BlackMisc::CCollection<CKeyboardKey>>();
|
||||||
|
qDBusRegisterMetaType<BlackMisc::CCollection<CKeyboardKey>>();
|
||||||
|
qRegisterMetaType<CKeyboardKeyList>();
|
||||||
|
qDBusRegisterMetaType<CKeyboardKeyList>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CKeyboardKeyList::initAsHotkeyList()
|
||||||
|
{
|
||||||
|
this->clear();
|
||||||
|
this->push_back(CKeyboardKey(QChar::Null, CKeyboardKey::ModifierNone, CKeyboardKey::ModifierNone, CKeyboardKey::HotkeyPtt));
|
||||||
|
this->push_back(CKeyboardKey(QChar::Null, CKeyboardKey::ModifierNone, CKeyboardKey::ModifierNone, CKeyboardKey::HotkeyOpacity50));
|
||||||
|
this->push_back(CKeyboardKey(QChar::Null, CKeyboardKey::ModifierNone, CKeyboardKey::ModifierNone, CKeyboardKey::HotkeyOpacity100));
|
||||||
|
this->push_back(CKeyboardKey(QChar::Null, CKeyboardKey::ModifierNone, CKeyboardKey::ModifierNone, CKeyboardKey::HotkeyToggleCom1));
|
||||||
|
this->push_back(CKeyboardKey(QChar::Null, CKeyboardKey::ModifierNone, CKeyboardKey::ModifierNone, CKeyboardKey::HotkeyToggleCom2));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace
|
||||||
54
src/blackmisc/hwkeyboardkeylist.h
Normal file
54
src/blackmisc/hwkeyboardkeylist.h
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
/* Copyright (C) 2013 VATSIM Community / authors
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\file
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BLACKMISC_KEYBOARDKEYLIST_H
|
||||||
|
#define BLACKMISC_KEYBOARDKEYLIST_H
|
||||||
|
|
||||||
|
#include "hwkeyboardkey.h"
|
||||||
|
#include "collection.h"
|
||||||
|
#include "sequence.h"
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
namespace BlackMisc
|
||||||
|
{
|
||||||
|
namespace Hardware
|
||||||
|
{
|
||||||
|
/*!
|
||||||
|
* Value object encapsulating a list of keyboard keys.
|
||||||
|
*/
|
||||||
|
class CKeyboardKeyList : public CSequence<CKeyboardKey>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
//! Default constructor
|
||||||
|
CKeyboardKeyList();
|
||||||
|
|
||||||
|
//! \brief Construct from a base class object.
|
||||||
|
CKeyboardKeyList(const CSequence<CKeyboardKey> &baseClass);
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::toQVariant
|
||||||
|
virtual QVariant toQVariant() const override
|
||||||
|
{
|
||||||
|
return QVariant::fromValue(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! \brief Register metadata
|
||||||
|
static void registerMetadata();
|
||||||
|
|
||||||
|
//! \brief Fill the list with hotkey
|
||||||
|
void initAsHotkeyList();
|
||||||
|
};
|
||||||
|
|
||||||
|
} //namespace
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
Q_DECLARE_METATYPE(BlackMisc::Hardware::CKeyboardKeyList)
|
||||||
|
Q_DECLARE_METATYPE(BlackMisc::CCollection<BlackMisc::Hardware::CKeyboardKey>)
|
||||||
|
Q_DECLARE_METATYPE(BlackMisc::CSequence<BlackMisc::Hardware::CKeyboardKey>)
|
||||||
|
|
||||||
|
#endif //guard
|
||||||
Reference in New Issue
Block a user