mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-13 15:45:42 +08:00
refs #139 , improved version of CKeyboardLineEdit widget
* CKeyboardLineEdit supports CKeyboardKey to set all details directly to object, sets native scan code, and native virtual code * CKeyboardKeyListModel::setData supports specialized line edit and combo box * fixed some wrong overrides
This commit is contained in:
@@ -45,14 +45,28 @@ namespace BlackGui
|
||||
{
|
||||
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;
|
||||
int pi = this->indexToPropertyIndex(index);
|
||||
if (pi == CKeyboardKey::IndexModifier1 || pi == CKeyboardKey::IndexModifier2 || pi == CKeyboardKey::IndexModifier1AsString || pi == CKeyboardKey::IndexModifier2AsString)
|
||||
{
|
||||
if (index.row() >= this->m_container.size()) return true;
|
||||
CKeyboardKey key = this->m_container[index.row()];
|
||||
key.setPropertyByIndex(value, pi);
|
||||
key.cleanup();
|
||||
this->m_container[index.row()] = key;
|
||||
return true;
|
||||
}
|
||||
else if (pi == CKeyboardKey::IndexKey || pi == CKeyboardKey::IndexKeyAsString || pi == CKeyboardKey::IndexKeyAsStringRepresentation)
|
||||
{
|
||||
Q_ASSERT(value.canConvert<CKeyboardKey>());
|
||||
if (index.row() >= this->m_container.size()) return true;
|
||||
CKeyboardKey key = this->m_container[index.row()];
|
||||
key.setPropertyByIndex(value, CKeyboardKey::IndexKeyObject);
|
||||
key.cleanup();
|
||||
this->m_container[index.row()] = key;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return CListModelBase::setData(index, value, role);
|
||||
}
|
||||
|
||||
QWidget *CKeyboardKeyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
@@ -62,7 +76,7 @@ namespace BlackGui
|
||||
|
||||
if (index.row() < model->rowCount())
|
||||
{
|
||||
int pi = model->columnToPropertyIndex(index.column());
|
||||
int pi = model->indexToPropertyIndex(index);
|
||||
if (pi == CKeyboardKey::IndexModifier1 || pi == CKeyboardKey::IndexModifier2 || pi == CKeyboardKey::IndexModifier1AsString || pi == CKeyboardKey::IndexModifier2AsString)
|
||||
{
|
||||
CKeyboardKey key = model->at(index);
|
||||
@@ -75,7 +89,7 @@ namespace BlackGui
|
||||
else if (pi == CKeyboardKey::IndexKey || pi == CKeyboardKey::IndexKeyAsString || pi == CKeyboardKey::IndexKeyAsStringRepresentation)
|
||||
{
|
||||
CKeyboardKey key = model->at(index);
|
||||
CKeyboardLineEdit *edit = new CKeyboardLineEdit(parent);
|
||||
CKeyboardLineEdit *edit = new CKeyboardLineEdit(key, parent);
|
||||
edit->setText(key.getKeyAsString());
|
||||
return edit;
|
||||
}
|
||||
@@ -88,12 +102,18 @@ namespace BlackGui
|
||||
QItemDelegate::setEditorData(editor, index);
|
||||
}
|
||||
|
||||
void CKeyboardKeyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)
|
||||
void CKeyboardKeyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
||||
{
|
||||
QItemDelegate::setModelData(editor, model, index);
|
||||
// with our own line edit we have a special treatment
|
||||
// otherwise (comboboxes) standard handling via QItemDelegate
|
||||
CKeyboardLineEdit *lineEdit = qobject_cast<CKeyboardLineEdit *>(editor);
|
||||
if (lineEdit)
|
||||
model->setData(index, lineEdit->getKey().toQVariant() , Qt::EditRole);
|
||||
else
|
||||
QItemDelegate::setModelData(editor, model, index);
|
||||
}
|
||||
|
||||
void CKeyboardKeyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index)
|
||||
void CKeyboardKeyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
QItemDelegate::updateEditorGeometry(editor, option, index);
|
||||
}
|
||||
@@ -113,6 +133,11 @@ namespace BlackGui
|
||||
|
||||
void CKeyboardLineEdit::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
const Qt::Key k = static_cast<Qt::Key>(event->key());
|
||||
if (k == Qt::Key_Shift || k == Qt::Key_Control || k == Qt::Key_Meta || k == Qt::Key_Alt || k == Qt::Key_CapsLock || k == Qt::Key_NumLock || k == Qt::Key_ScrollLock) return;
|
||||
this->m_key.setKey(event->key());
|
||||
this->m_key.setNativeScanCode(event->nativeScanCode());
|
||||
this->m_key.setNativeVirtualKey(event->nativeVirtualKey());
|
||||
this->setText(CKeyboardKey::toStringRepresentation(event->key()));
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace BlackGui
|
||||
void initAsHotkeyList() { this->m_container.initAsHotkeyList(); }
|
||||
|
||||
//! \copydoc QAbstractTableModel::setData
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||
};
|
||||
|
||||
/*!
|
||||
@@ -43,26 +43,25 @@ namespace BlackGui
|
||||
|
||||
public:
|
||||
//! \brief Constructor
|
||||
explicit CKeyboardKeyItemDelegate(QObject *parent = nullptr) : QItemDelegate(parent) {}
|
||||
explicit CKeyboardKeyItemDelegate(QObject *parent = nullptr) :
|
||||
QItemDelegate(parent) {}
|
||||
|
||||
//! \copydoc QItemDelegate::createEditor
|
||||
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||
|
||||
//! \copydoc QItemDelegate::setEditorData
|
||||
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
|
||||
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override;
|
||||
|
||||
//! \copydoc QItemDelegate::setModelData
|
||||
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index);
|
||||
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
|
||||
|
||||
//! \copydoc QItemDelegate::updateEditorGeometry
|
||||
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index);
|
||||
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||
|
||||
//! \brief correspondig model
|
||||
const CKeyboardKeyListModel *model() const;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
* \brief Special edit widget for key sequence
|
||||
*/
|
||||
@@ -72,12 +71,18 @@ namespace BlackGui
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
CKeyboardLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) { }
|
||||
CKeyboardLineEdit(BlackMisc::Hardware::CKeyboardKey &key, QWidget *parent = nullptr) :
|
||||
QLineEdit(parent), m_key(key) { }
|
||||
|
||||
//! \brief get key
|
||||
BlackMisc::Hardware::CKeyboardKey getKey() const { return this->m_key; }
|
||||
|
||||
protected:
|
||||
//! \brief Overriding and handling key press
|
||||
virtual void keyPressEvent(QKeyEvent *event) override;
|
||||
|
||||
private:
|
||||
BlackMisc::Hardware::CKeyboardKey m_key;
|
||||
};
|
||||
}
|
||||
#endif // guard
|
||||
|
||||
Reference in New Issue
Block a user