diff --git a/src/blackgui/models/listmodelbase.cpp b/src/blackgui/models/listmodelbase.cpp index 0c5f8f724..19556bcb6 100644 --- a/src/blackgui/models/listmodelbase.cpp +++ b/src/blackgui/models/listmodelbase.cpp @@ -29,9 +29,6 @@ namespace BlackGui { namespace Models { - /* - * Column count - */ int CListModelBaseNonTemplate::columnCount(const QModelIndex &modelIndex) const { Q_UNUSED(modelIndex); @@ -39,9 +36,6 @@ namespace BlackGui return c; } - /* - * Header data - */ QVariant CListModelBaseNonTemplate::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) { return QVariant(); } @@ -62,41 +56,38 @@ namespace BlackGui return QVariant(); } - /* - * Column to property index - */ + QModelIndex CListModelBaseNonTemplate::index(int row, int column, const QModelIndex &parent) const + { + Q_UNUSED(parent); + return QAbstractItemModel::createIndex(row, column); + } + + QModelIndex CListModelBaseNonTemplate::parent(const QModelIndex &child) const + { + Q_UNUSED(child); + return QModelIndex(); + } + BlackMisc::CPropertyIndex CListModelBaseNonTemplate::columnToPropertyIndex(int column) const { return this->m_columns.columnToPropertyIndex(column); } - /* - * To column - */ int CListModelBaseNonTemplate::propertyIndexToColumn(const CPropertyIndex &propertyIndex) const { return m_columns.propertyIndexToColumn(propertyIndex); } - /* - * Property index - */ BlackMisc::CPropertyIndex CListModelBaseNonTemplate::modelIndexToPropertyIndex(const QModelIndex &index) const { return this->columnToPropertyIndex(index.column()); } - /* - * Sort column - */ void CListModelBaseNonTemplate::setSortColumnByPropertyIndex(const BlackMisc::CPropertyIndex &propertyIndex) { this->m_sortedColumn = this->m_columns.propertyIndexToColumn(propertyIndex); } - /* - * Sort column? - */ bool CListModelBaseNonTemplate::hasValidSortColumn() const { @@ -104,12 +95,9 @@ namespace BlackGui return this->m_columns.isSortable(this->m_sortedColumn); } - /* - * Flags - */ Qt::ItemFlags CListModelBaseNonTemplate::flags(const QModelIndex &index) const { - Qt::ItemFlags f = QAbstractListModel::flags(index); + Qt::ItemFlags f = QAbstractItemModel::flags(index); if (!index.isValid()) { return f; } bool editable = this->m_columns.isEditable(index); f = editable ? (f | Qt::ItemIsEditable) : (f ^ Qt::ItemIsEditable); @@ -123,25 +111,16 @@ namespace BlackGui return f; } - /* - * Translation context - */ const QString &CListModelBaseNonTemplate::getTranslationContext() const { return m_columns.getTranslationContext(); } - /* - * Update - */ int CListModelBaseNonTemplate::ps_updateContainer(const CVariant &variant, bool sort) { return this->performUpdateContainer(variant, sort); } - /* - * Row count - */ template int CListModelBase::rowCount(const QModelIndex &parentIndex) const { @@ -149,9 +128,6 @@ namespace BlackGui return this->getContainerOrFilteredContainer().size(); } - /* - * Valid index? - */ template bool CListModelBase::isValidIndex(const QModelIndex &index) const { @@ -160,9 +136,6 @@ namespace BlackGui index.column() >= 0 && index.column() < this->columnCount(index)); } - /* - * Data - */ template QVariant CListModelBase::data(const QModelIndex &index, int role) const { @@ -178,9 +151,6 @@ namespace BlackGui return formatter->data(role, obj.propertyByIndex(propertyIndex)).toQVariant(); } - /* - * Set data - */ template bool CListModelBase::setData(const QModelIndex &index, const QVariant &value, int role) { @@ -213,9 +183,6 @@ namespace BlackGui return false; } - /* - * Update - */ template int CListModelBase::update(const ContainerType &container, bool sort) { @@ -239,9 +206,6 @@ namespace BlackGui return newSize; } - /* - * Update - */ template void CListModelBase::update(const QModelIndex &index, const ObjectType &object) { @@ -256,12 +220,9 @@ namespace BlackGui template void CListModelBase::update(int rowIndex, const ObjectType &object) { - this->update(this->index(rowIndex), object); + this->update(this->index(rowIndex, 0), object); } - /* - * Async update - */ template BlackMisc::CWorker *CListModelBase::updateAsync(const ContainerType &container, bool sort) { @@ -277,9 +238,6 @@ namespace BlackGui return worker; } - /* - * Container size decides async/sync - */ template void CListModelBase::updateContainerMaybeAsync(const ContainerType &container, bool sort) { @@ -294,18 +252,12 @@ namespace BlackGui } } - /* - * Filter - */ template bool CListModelBase::hasFilter() const { return m_filter ? true : false; } - /* - * Remove filter - */ template void CListModelBase::removeFilter() { @@ -317,9 +269,6 @@ namespace BlackGui this->emitRowCountChanged(); } - /* - * Set filter - */ template void CListModelBase::setFilter(std::unique_ptr > &filter) { @@ -338,9 +287,6 @@ namespace BlackGui } } - /* - * At - */ template const ObjectType &CListModelBase::at(const QModelIndex &index) const { @@ -355,18 +301,12 @@ namespace BlackGui } } - /* - * Container - */ template const ContainerType &CListModelBase::getContainer() const { return this->m_container; } - /* - * Push back - */ template void CListModelBase::push_back(const ObjectType &object) { @@ -377,9 +317,6 @@ namespace BlackGui this->emitRowCountChanged(); } - /* - * insert - */ template void CListModelBase::insert(const ObjectType &object) { @@ -396,9 +333,6 @@ namespace BlackGui this->emitRowCountChanged(); } - /* - * Remove object - */ template void CListModelBase::remove(const ObjectType &object) { @@ -419,9 +353,6 @@ namespace BlackGui } } - /* - * Clear - */ template void CListModelBase::clear() { @@ -432,9 +363,12 @@ namespace BlackGui this->emitRowCountChanged(); } - /* - * Update on container - */ + template + bool CListModelBase::isEmpty() const + { + return this->m_container.isEmpty(); + } + template int CListModelBase::performUpdateContainer(const BlackMisc::CVariant &variant, bool sort) { @@ -487,9 +421,6 @@ namespace BlackGui this->updateContainerMaybeAsync(this->m_container, true); } - /* - * Sort list - */ template ContainerType CListModelBase::sortContainerByColumn(const ContainerType &container, int column, Qt::SortOrder order) const { diff --git a/src/blackgui/models/listmodelbase.h b/src/blackgui/models/listmodelbase.h index fb297d583..1855e1677 100644 --- a/src/blackgui/models/listmodelbase.h +++ b/src/blackgui/models/listmodelbase.h @@ -26,7 +26,7 @@ namespace BlackGui namespace Models { //! Non templated base class, allows Q_OBJECT and signals to be used - class CListModelBaseNonTemplate : public QAbstractListModel + class CListModelBaseNonTemplate : public QAbstractItemModel { Q_OBJECT @@ -37,12 +37,18 @@ namespace BlackGui //! Destructor virtual ~CListModelBaseNonTemplate() {} - //! \copydoc QAbstractListModel::columnCount() + //! \copydoc QAbstractItemModel::columnCount() virtual int columnCount(const QModelIndex &modelIndex = QModelIndex()) const override; //! \copydoc QAbstractItemModel::headerData() virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + //! \copydoc QAbstractItemModel::headerData() + virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; + + //! \copydoc QAbstractItemModel::parent() + virtual QModelIndex parent(const QModelIndex &child) const override; + //! Column to property index virtual BlackMisc::CPropertyIndex columnToPropertyIndex(int column) const; @@ -99,7 +105,7 @@ namespace BlackGui * \param parent */ CListModelBaseNonTemplate(const QString &translationContext, QObject *parent = nullptr) - : QAbstractListModel(parent), m_columns(translationContext), m_sortedColumn(-1), m_sortOrder(Qt::AscendingOrder) + : QAbstractItemModel(parent), m_columns(translationContext), m_sortedColumn(-1), m_sortOrder(Qt::AscendingOrder) { // non unique default name, set translation context as default this->setObjectName(translationContext); @@ -127,14 +133,14 @@ namespace BlackGui //! Used container data virtual const ContainerType &getContainer() const; - //! \copydoc QAbstractListModel::data() + //! \copydoc QAbstractItemModel::data() virtual QVariant data(const QModelIndex &index, int role) const override; - //! \copydoc QAbstractListModel::setData() + //! \copydoc QAbstractItemModel::setData() //! \sa CListModelBaseNonTemplate::flags virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - //! \copydoc QAbstractListModel::rowCount() + //! \copydoc QAbstractItemModel::rowCount() virtual int rowCount(const QModelIndex &parentIndex = QModelIndex()) const override; //! Update by new container @@ -156,7 +162,7 @@ namespace BlackGui //! Object at row position virtual const ObjectType &at(const QModelIndex &index) const; - //! \copydoc QAbstractListModel::sort() + //! \copydoc QAbstractItemModel::sort() virtual void sort(int column, Qt::SortOrder order) override; /*! @@ -191,6 +197,9 @@ namespace BlackGui //! Clear the list virtual void clear(); + //! Empty? + virtual bool isEmpty() const; + //! Filter available bool hasFilter() const;