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

@@ -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