From c6c2d31cacc39bae05438b123dd89b234f33e981 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 17 Feb 2014 02:11:23 +0100 Subject: [PATCH] refs #139, changed list model to support editable columns --- src/blackgui/listmodelbase.cpp | 106 ++++++++++++++++++++------------- src/blackgui/listmodelbase.h | 40 ++++++++----- 2 files changed, 92 insertions(+), 54 deletions(-) diff --git a/src/blackgui/listmodelbase.cpp b/src/blackgui/listmodelbase.cpp index 012670958..4c9f9adc8 100644 --- a/src/blackgui/listmodelbase.cpp +++ b/src/blackgui/listmodelbase.cpp @@ -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 - int CListModelBase::columnCount(const QModelIndex & /** modelIndex **/) const + template + int CListModelBase::columnCount(const QModelIndex & /** modelIndex **/) const { int c = this->m_columns.size(); return c; @@ -27,17 +28,17 @@ namespace BlackGui /* * Row count */ - template - int CListModelBase::rowCount(const QModelIndex & /** parent */) const + template + int CListModelBase::rowCount(const QModelIndex & /** parent */) const { - return this->m_list.size(); + return this->m_container.size(); } /* * Column to property index */ - template - int CListModelBase::columnToPropertyIndex(int column) const + template + int CListModelBase::columnToPropertyIndex(int column) const { return this->m_columns.columnToPropertyIndex(column); } @@ -45,15 +46,14 @@ namespace BlackGui /* * Header data */ - template QVariant - CListModelBase::headerData(int section, Qt::Orientation orientation, int role) const + template QVariant + CListModelBase::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 - QVariant CListModelBase::data(const QModelIndex &index, int role) const + template + QVariant CListModelBase::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 - int CListModelBase::update(const ListType &list) + template + int CListModelBase::update(const ContainerType &list) { - ListType copyList = (list.size() > 1 && this->hasValidSortColumn() ? - this->sortListByColumn(list, this->m_sortedColumn, this->m_sortOrder) : - list); + 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 + void CListModelBase::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 - void CListModelBase::push_back(const ObjectType &object) + template + void CListModelBase::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 - void CListModelBase::insert(const ObjectType &object) + template + void CListModelBase::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 - void CListModelBase::clear() + template + void CListModelBase::clear() { beginResetModel(); - this->m_list.clear(); + this->m_container.clear(); endResetModel(); } /* * Sort */ - template void CListModelBase::sort(int column, Qt::SortOrder order) + template void CListModelBase::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 ListType CListModelBase::sortListByColumn(const ListType &list, int column, Qt::SortOrder order) + template ContainerType CListModelBase::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 Qt::ItemFlags CListModelBase::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; template class CListModelBase; template class CListModelBase; - + template class CListModelBase; } // namespace diff --git a/src/blackgui/listmodelbase.h b/src/blackgui/listmodelbase.h index f69cb23aa..9168e917d 100644 --- a/src/blackgui/listmodelbase.h +++ b/src/blackgui/listmodelbase.h @@ -18,11 +18,11 @@ namespace BlackGui /*! * \brief List model */ - template class CListModelBase : public QAbstractListModel + template 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