mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-30 11:55:35 +08:00
refs #139, changed list model to support editable columns
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "blackmisc/avaircraftlist.h"
|
||||
#include "blackmisc/nwserverlist.h"
|
||||
#include "blackmisc/nwuserlist.h"
|
||||
#include "blackmisc/hwkeyboardkeylist.h"
|
||||
#include "blackmisc/blackmiscfreefunctions.h"
|
||||
|
||||
namespace BlackGui
|
||||
@@ -17,8 +18,8 @@ namespace BlackGui
|
||||
/*
|
||||
* Column count
|
||||
*/
|
||||
template <typename ObjectType, typename ListType>
|
||||
int CListModelBase<ObjectType, ListType>::columnCount(const QModelIndex & /** modelIndex **/) const
|
||||
template <typename ObjectType, typename ContainerType>
|
||||
int CListModelBase<ObjectType, ContainerType>::columnCount(const QModelIndex & /** modelIndex **/) const
|
||||
{
|
||||
int c = this->m_columns.size();
|
||||
return c;
|
||||
@@ -27,17 +28,17 @@ namespace BlackGui
|
||||
/*
|
||||
* Row count
|
||||
*/
|
||||
template <typename ObjectType, typename ListType>
|
||||
int CListModelBase<ObjectType, ListType>::rowCount(const QModelIndex & /** parent */) const
|
||||
template <typename ObjectType, typename ContainerType>
|
||||
int CListModelBase<ObjectType, ContainerType>::rowCount(const QModelIndex & /** parent */) const
|
||||
{
|
||||
return this->m_list.size();
|
||||
return this->m_container.size();
|
||||
}
|
||||
|
||||
/*
|
||||
* Column to property index
|
||||
*/
|
||||
template <typename ObjectType, typename ListType>
|
||||
int CListModelBase<ObjectType, ListType>::columnToPropertyIndex(int column) const
|
||||
template <typename ObjectType, typename ContainerType>
|
||||
int CListModelBase<ObjectType, ContainerType>::columnToPropertyIndex(int column) const
|
||||
{
|
||||
return this->m_columns.columnToPropertyIndex(column);
|
||||
}
|
||||
@@ -45,15 +46,14 @@ namespace BlackGui
|
||||
/*
|
||||
* Header data
|
||||
*/
|
||||
template <typename ObjectType, typename ListType> QVariant
|
||||
CListModelBase<ObjectType, ListType>::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
template <typename ObjectType, typename ContainerType> QVariant
|
||||
CListModelBase<ObjectType, ContainerType>::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
|
||||
{
|
||||
if (section < 0 || section >= this->m_columns.size()) return QVariant();
|
||||
QString col = this->m_columns.columnToName(section);
|
||||
col = QCoreApplication::translate(this->m_columns.getTranslationContext(), col.toUtf8().constData());
|
||||
return QVariant(col);
|
||||
QString header = this->m_columns.at(section).getColumnName(false);
|
||||
return QVariant(header);
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
@@ -61,11 +61,11 @@ namespace BlackGui
|
||||
/*
|
||||
* Data
|
||||
*/
|
||||
template <typename ObjectType, typename ListType>
|
||||
QVariant CListModelBase<ObjectType, ListType>::data(const QModelIndex &index, int role) const
|
||||
template <typename ObjectType, typename ContainerType>
|
||||
QVariant CListModelBase<ObjectType, ContainerType>::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
// checks
|
||||
if (index.row() < 0 || index.row() >= this->m_list.size() ||
|
||||
if (index.row() < 0 || index.row() >= this->m_container.size() ||
|
||||
index.column() < 0 || index.column() >= this->columnCount(index))
|
||||
{
|
||||
return QVariant();
|
||||
@@ -73,7 +73,7 @@ namespace BlackGui
|
||||
|
||||
if (role == Qt::DisplayRole)
|
||||
{
|
||||
ObjectType obj = this->m_list[index.row()];
|
||||
ObjectType obj = this->m_container[index.row()];
|
||||
int propertyIndex = this->columnToPropertyIndex(index.column());
|
||||
QString propertyString = obj.propertyByIndexAsString(propertyIndex, true);
|
||||
return QVariant::fromValue(propertyString);
|
||||
@@ -88,74 +88,89 @@ namespace BlackGui
|
||||
/*
|
||||
* Update
|
||||
*/
|
||||
template <typename ObjectType, typename ListType>
|
||||
int CListModelBase<ObjectType, ListType>::update(const ListType &list)
|
||||
template <typename ObjectType, typename ContainerType>
|
||||
int CListModelBase<ObjectType, ContainerType>::update(const ContainerType &list)
|
||||
{
|
||||
ListType copyList = (list.size() > 1 && this->hasValidSortColumn() ?
|
||||
ContainerType copyList = (list.size() > 1 && this->hasValidSortColumn() ?
|
||||
this->sortListByColumn(list, this->m_sortedColumn, this->m_sortOrder) :
|
||||
list);
|
||||
this->beginResetModel();
|
||||
this->m_list.clear();
|
||||
this->m_container.clear();
|
||||
foreach(ObjectType object, copyList)
|
||||
{
|
||||
this->m_list.push_back(object);
|
||||
this->m_container.push_back(object);
|
||||
}
|
||||
this->endResetModel();
|
||||
return this->m_list.size();
|
||||
return this->m_container.size();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Update
|
||||
*/
|
||||
template <typename ObjectType, typename ContainerType>
|
||||
void CListModelBase<ObjectType, ContainerType>::update(const QModelIndex &index, const ObjectType &object)
|
||||
{
|
||||
if (index.row() >= this->m_container.size()) return;
|
||||
this->m_container[index.row()] = object;
|
||||
|
||||
QModelIndex i1 = index.sibling(index.row(), 0);
|
||||
QModelIndex i2 = index.sibling(index.row(), this->columnCount(index) - 1);
|
||||
emit this->dataChanged(i1, i2);
|
||||
}
|
||||
|
||||
/*
|
||||
* Push back
|
||||
*/
|
||||
template <typename ObjectType, typename ListType>
|
||||
void CListModelBase<ObjectType, ListType>::push_back(const ObjectType &object)
|
||||
template <typename ObjectType, typename ContainerType>
|
||||
void CListModelBase<ObjectType, ContainerType>::push_back(const ObjectType &object)
|
||||
{
|
||||
beginInsertRows(QModelIndex(), this->m_list.size(), this->m_list.size());
|
||||
this->m_list.push_back(object);
|
||||
beginInsertRows(QModelIndex(), this->m_container.size(), this->m_container.size());
|
||||
this->m_container.push_back(object);
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
/*
|
||||
* Push back
|
||||
*/
|
||||
template <typename ObjectType, typename ListType>
|
||||
void CListModelBase<ObjectType, ListType>::insert(const ObjectType &object)
|
||||
template <typename ObjectType, typename ContainerType>
|
||||
void CListModelBase<ObjectType, ContainerType>::insert(const ObjectType &object)
|
||||
{
|
||||
beginInsertRows(QModelIndex(), 0, 0);
|
||||
this->m_list.insert(this->m_list.begin(), object);
|
||||
this->m_container.insert(this->m_container.begin(), object);
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
/*
|
||||
* Clear
|
||||
*/
|
||||
template <typename ObjectType, typename ListType>
|
||||
void CListModelBase<ObjectType, ListType>::clear()
|
||||
template <typename ObjectType, typename ContainerType>
|
||||
void CListModelBase<ObjectType, ContainerType>::clear()
|
||||
{
|
||||
beginResetModel();
|
||||
this->m_list.clear();
|
||||
this->m_container.clear();
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
/*
|
||||
* Sort
|
||||
*/
|
||||
template <typename ObjectType, typename ListType> void CListModelBase<ObjectType, ListType>::sort(int column, Qt::SortOrder order)
|
||||
template <typename ObjectType, typename ContainerType> void CListModelBase<ObjectType, ContainerType>::sort(int column, Qt::SortOrder order)
|
||||
{
|
||||
this->m_sortedColumn = column;
|
||||
this->m_sortOrder = order;
|
||||
if (this->m_list.size() < 2) return; // nothing to do
|
||||
if (this->m_container.size() < 2) return; // nothing to do
|
||||
|
||||
// sort the values
|
||||
this->update(
|
||||
this->sortListByColumn(this->m_list, column, order)
|
||||
this->sortListByColumn(this->m_container, column, order)
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sort list
|
||||
*/
|
||||
template <typename ObjectType, typename ListType> ListType CListModelBase<ObjectType, ListType>::sortListByColumn(const ListType &list, int column, Qt::SortOrder order)
|
||||
template <typename ObjectType, typename ContainerType> ContainerType CListModelBase<ObjectType, ContainerType>::sortListByColumn(const ContainerType &list, int column, Qt::SortOrder order)
|
||||
{
|
||||
if (list.size() < 2) return list; // nothing to do
|
||||
int propertyIndex = this->m_columns.columnToPropertyIndex(column);
|
||||
@@ -173,8 +188,19 @@ namespace BlackGui
|
||||
BlackMisc::compareQVariants(bQv, aQv);
|
||||
return compare < 0;
|
||||
}
|
||||
);
|
||||
); // sorted
|
||||
}
|
||||
|
||||
/*
|
||||
* Make editable
|
||||
*/
|
||||
template <typename ObjectType, typename ContainerType> Qt::ItemFlags CListModelBase<ObjectType, ContainerType>::flags(const QModelIndex &index) const
|
||||
{
|
||||
Qt::ItemFlags f = QAbstractListModel::flags(index);
|
||||
if (this->m_columns.isEditable(index))
|
||||
return f | Qt::ItemIsEditable;
|
||||
else
|
||||
return f;
|
||||
}
|
||||
|
||||
// see here for the reason of thess forward instantiations
|
||||
@@ -184,6 +210,6 @@ namespace BlackGui
|
||||
template class CListModelBase<BlackMisc::Aviation::CAircraft, BlackMisc::Aviation::CAircraftList>;
|
||||
template class CListModelBase<BlackMisc::Network::CServer, BlackMisc::Network::CServerList>;
|
||||
template class CListModelBase<BlackMisc::Network::CUser, BlackMisc::Network::CUserList>;
|
||||
|
||||
template class CListModelBase<BlackMisc::Hardware::CKeyboardKey, BlackMisc::Hardware::CKeyboardKeyList>;
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -18,11 +18,11 @@ namespace BlackGui
|
||||
/*!
|
||||
* \brief List model
|
||||
*/
|
||||
template <typename ObjectType, typename ListType> class CListModelBase : public QAbstractListModel
|
||||
template <typename ObjectType, typename ContainerType> class CListModelBase : public QAbstractListModel
|
||||
{
|
||||
|
||||
protected:
|
||||
ListType m_list; //!< used container
|
||||
ContainerType m_container; //!< used container
|
||||
CColumns m_columns; //!< columns metadata
|
||||
int m_sortedColumn; //!< current sort column
|
||||
Qt::SortOrder m_sortOrder; //!< sort order (asc/desc)
|
||||
@@ -45,7 +45,7 @@ namespace BlackGui
|
||||
* \param order sort order (asccending / descending)
|
||||
* \return
|
||||
*/
|
||||
ListType sortListByColumn(const ListType &list, int column, Qt::SortOrder order);
|
||||
ContainerType sortListByColumn(const ContainerType &list, int column, Qt::SortOrder order);
|
||||
|
||||
public:
|
||||
|
||||
@@ -115,22 +115,34 @@ namespace BlackGui
|
||||
return this->m_sortOrder;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \copydoc QAbstractListModel::data()
|
||||
*/
|
||||
//! \brief Used container data
|
||||
virtual const ContainerType &getContainer() const
|
||||
{
|
||||
return this->m_container;
|
||||
}
|
||||
|
||||
//! \copydoc QAbstractListModel::data()
|
||||
virtual QVariant data(const QModelIndex &index, int role) const;
|
||||
|
||||
/*!
|
||||
* \copydoc QAbstractListModel::rowCount()
|
||||
*/
|
||||
//! \copydoc QAbstractListModel::rowCount()
|
||||
virtual int rowCount(const QModelIndex &index = QModelIndex()) const;
|
||||
|
||||
//! \copydoc QAbstractTableModel::flags
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
|
||||
/*!
|
||||
* \brief Update by new list
|
||||
* \param list
|
||||
* \return new list size
|
||||
*/
|
||||
virtual int update(const ListType &list);
|
||||
virtual int update(const ContainerType &list);
|
||||
|
||||
/*!
|
||||
* \brief Update single element
|
||||
* \param index
|
||||
* \param object
|
||||
*/
|
||||
virtual void update(const QModelIndex &index, const ObjectType &object);
|
||||
|
||||
/*!
|
||||
* \brief Object at row position
|
||||
@@ -139,24 +151,24 @@ namespace BlackGui
|
||||
*/
|
||||
virtual const ObjectType &at(const QModelIndex &index) const
|
||||
{
|
||||
if (index.row() < 0 || index.row() >= this->m_list.size())
|
||||
if (index.row() < 0 || index.row() >= this->m_container.size())
|
||||
{
|
||||
const static ObjectType def;
|
||||
return def;
|
||||
}
|
||||
else
|
||||
{
|
||||
return this->m_list[index.row()];
|
||||
return this->m_container[index.row()];
|
||||
}
|
||||
}
|
||||
|
||||
//! \copydoc QAbstractListModel::sort()
|
||||
virtual void sort(int column, Qt::SortOrder order);
|
||||
|
||||
//! \brief Similar to ListType::push_back
|
||||
//! \brief Similar to ContainerType::push_back
|
||||
virtual void push_back(const ObjectType &object);
|
||||
|
||||
//! \brief Similar to ListType::insert here inserts at first position
|
||||
//! \brief Similar to ContainerType::insert here inserts at first position
|
||||
virtual void insert(const ObjectType &object);
|
||||
|
||||
//! \brief clear the list
|
||||
|
||||
Reference in New Issue
Block a user