refs #325, added async update member function to CListModelBase / CViewBase (+ resize modes)

* model:
** required a non templated base class for Q_OBJECT / slots
** added worker for thread, QConcurrent is not used as it does not work with template classes
* views:
** added cpp file for view base class
** added template parameter for container in views
** different modes how resizing will be applied
This commit is contained in:
Klaus Basan
2014-09-18 02:10:36 +02:00
parent 89dc0fb289
commit dc3eff1c1f
14 changed files with 713 additions and 194 deletions

View File

@@ -26,36 +26,16 @@ namespace BlackGui
/*
* Column count
*/
template <typename ObjectType, typename ContainerType>
int CListModelBase<ObjectType, ContainerType>::columnCount(const QModelIndex & /** modelIndex **/) const
int CListModelBaseNonTemplate::columnCount(const QModelIndex & /** modelIndex **/) const
{
int c = this->m_columns.size();
return c;
}
/*
* Row count
*/
template <typename ObjectType, typename ContainerType>
int CListModelBase<ObjectType, ContainerType>::rowCount(const QModelIndex & /** parent */) const
{
return this->m_container.size();
}
/*
* Column to property index
*/
template <typename ObjectType, typename ContainerType>
BlackMisc::CPropertyIndex CListModelBase<ObjectType, ContainerType>::columnToPropertyIndex(int column) const
{
return this->m_columns.columnToPropertyIndex(column);
}
/*
* Header data
*/
template <typename ObjectType, typename ContainerType> QVariant
CListModelBase<ObjectType, ContainerType>::headerData(int section, Qt::Orientation orientation, int role) const
QVariant CListModelBaseNonTemplate::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal)
{
@@ -75,6 +55,54 @@ namespace BlackGui
return QVariant();
}
/*
* Column to property index
*/
BlackMisc::CPropertyIndex CListModelBaseNonTemplate::columnToPropertyIndex(int column) const
{
return this->m_columns.columnToPropertyIndex(column);
}
/*
* Sort column?
*/
bool CListModelBaseNonTemplate::hasValidSortColumn() const
{
return this->m_sortedColumn >= 0 && this->m_sortedColumn < this->m_columns.size();
}
/*
* Make editable
*/
Qt::ItemFlags CListModelBaseNonTemplate::flags(const QModelIndex &index) const
{
Qt::ItemFlags f = QAbstractListModel::flags(index);
if (this->m_columns.isEditable(index))
return f | Qt::ItemIsEditable;
else
return f;
}
/*
* Row count
*/
template <typename ObjectType, typename ContainerType>
int CListModelBase<ObjectType, ContainerType>::rowCount(const QModelIndex & /** parent */) const
{
return this->m_container.size();
}
/*
* Valid index?
*/
template <typename ObjectType, typename ContainerType>
bool CListModelBase<ObjectType, ContainerType>::isValidIndex(const QModelIndex &index) const
{
if (!index.isValid()) return false;
return (index.row() >= 0 && index.row() < this->m_container.size() &&
index.column() >= 0 && index.column() < this->columnCount(index));
}
/*
* Data
*/
@@ -100,12 +128,26 @@ namespace BlackGui
int CListModelBase<ObjectType, ContainerType>::update(const ContainerType &container, bool sort)
{
// KWB remove: qDebug() will be removed soon
qDebug() << "update" << this->objectName() << "size" << container.size();
qDebug() << "update" << this->objectName() << "size" << container.size() << "thread:" << QThread::currentThreadId();
// Keep sorting out of begin/end reset model
QTime myTimer;
ContainerType sortedContainer;
bool performSort = sort && container.size() > 1 && this->hasValidSortColumn();
if (performSort)
{
myTimer.start();
sortedContainer = this->sortContainerByColumn(container, this->getSortColumn(), this->m_sortOrder);
qDebug() << this->objectName() << "Sort performed ms:" << myTimer.restart() << "thread:" << QThread::currentThreadId();
}
this->beginResetModel();
this->m_container = (sort && container.size() > 1 && this->hasValidSortColumn() ?
this->sortListByColumn(container, this->getSortColumn(), this->m_sortOrder) :
container);
this->m_container = performSort ? sortedContainer : container;
this->endResetModel();
// TODO: KWB remove
qDebug() << this->objectName() << "Reset performed ms:" << myTimer.restart() << "objects:" << this->m_container.size() << "thread:" << QThread::currentThreadId();
return this->m_container.size();
}
@@ -123,6 +165,56 @@ namespace BlackGui
emit this->dataChanged(i1, i2); // which range has been changed
}
/*
* Async update
*/
template <typename ObjectType, typename ContainerType>
BlackGui::IUpdateWorker *CListModelBase<ObjectType, ContainerType>::updateAsync(const ContainerType &container, bool sort)
{
// TODO: mutex
CModelUpdateWorker *worker = new CModelUpdateWorker(this, container, sort);
if (worker->start()) { return worker; }
// start failed, we have responsibility to clean up the worker
Q_ASSERT_X(false, "CModelBase", "cannot start worker");
worker->terminate();
return nullptr;
}
/*
* Container size decides async/sync
*/
template <typename ObjectType, typename ContainerType>
void CListModelBase<ObjectType, ContainerType>::updateContainerMaybeAsync(const ContainerType &container, bool sort)
{
if (container.size() > asyncThreshold && sort)
{
// larger container with sorting
updateAsync(container, sort);
}
else
{
update(container, sort);
}
}
/*
* At
*/
template <typename ObjectType, typename ContainerType>
const ObjectType &CListModelBase<ObjectType, ContainerType>::at(const QModelIndex &index) const
{
if (index.row() < 0 || index.row() >= this->m_container.size())
{
const static ObjectType def; // default object
return def;
}
else
{
return this->m_container[index.row()];
}
}
/*
* Push back
*/
@@ -159,14 +251,23 @@ namespace BlackGui
/*
* Clear
*/
template <typename ObjectType, typename ContainerType>
void CListModelBase<ObjectType, ContainerType>::clear()
template <typename ObjectType, typename ContainerType> void CListModelBase<ObjectType, ContainerType>::clear()
{
beginResetModel();
this->m_container.clear();
endResetModel();
}
/*
* Update on container
*/
template <typename ObjectType, typename ContainerType> int CListModelBase<ObjectType, ContainerType>::performUpdateContainer(const QVariant &variant, bool sort)
{
ContainerType c;
c.convertFromQVariant(variant);
return this->update(c, sort);
}
/*
* Sort requested by abstract model
*/
@@ -180,21 +281,23 @@ namespace BlackGui
if (this->m_container.size() < 2) return; // nothing to do
// sort the values
this->update(this->m_container, true);
this->updateContainerMaybeAsync(this->m_container, true);
}
/*
* Sort list
*/
template <typename ObjectType, typename ContainerType> ContainerType CListModelBase<ObjectType, ContainerType>::sortListByColumn(const ContainerType &list, int column, Qt::SortOrder order)
template <typename ObjectType, typename ContainerType> ContainerType CListModelBase<ObjectType, ContainerType>::sortContainerByColumn(const ContainerType &container, int column, Qt::SortOrder order) const
{
if (list.size() < 2) return list; // nothing to do
if (container.size() < 2) return container; // nothing to do
// this is the only part not really thread safe, but columns do not change so far
BlackMisc::CPropertyIndex propertyIndex = this->m_columns.columnToPropertyIndex(column);
Q_ASSERT(!propertyIndex.isEmpty());
if (propertyIndex.isEmpty()) return list; // at release build do nothing
if (propertyIndex.isEmpty()) return container; // at release build do nothing
// sort the values
auto p = [ = ](const ObjectType & a, const ObjectType & b) -> bool
const auto p = [ = ](const ObjectType & a, const ObjectType & b) -> bool
{
QVariant aQv = a.propertyByIndex(propertyIndex);
QVariant bQv = b.propertyByIndex(propertyIndex);
@@ -205,20 +308,11 @@ namespace BlackGui
};
// KWB: qDebug() will be removed soon
qDebug() << "sort" << this->objectName() << "column" << column << propertyIndex.toQString();
return list.sorted(p); // synchronous 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;
QTime t;
t.start();
const ContainerType sorted = container.sorted(p);
qDebug() << "Sort" << this->objectName() << "column" << column << "index:" << propertyIndex.toQString() << "ms:" << t.elapsed() << "thread:" << QThread::currentThreadId();
return sorted;
}
// see here for the reason of thess forward instantiations
@@ -232,5 +326,6 @@ namespace BlackGui
template class CListModelBase<BlackMisc::Network::CUser, BlackMisc::Network::CUserList>;
template class CListModelBase<BlackMisc::Network::CClient, BlackMisc::Network::CClientList>;
template class CListModelBase<BlackMisc::Settings::CSettingKeyboardHotkey, BlackMisc::Settings::CSettingKeyboardHotkeyList>;
}
} // namespace
} // namespace

View File

@@ -13,24 +13,26 @@
#define BLACKGUI_LISTMODELBASE_H
#include "blackgui/models/columns.h"
#include "blackgui/updateworker.h"
#include "blackmisc/propertyindex.h"
#include <QAbstractItemModel>
#include <QThread>
namespace BlackGui
{
namespace Models
{
/*!
* List model
*/
template <typename ObjectType, typename ContainerType> class CListModelBase : public QAbstractListModel
//! Non templated base class, allows Q_OBJECT and signals to be used
class CListModelBaseNonTemplate : public QAbstractListModel
{
Q_OBJECT
public:
//! Number of elements when to use asynchronous updates
static const int asyncThreshold = 50;
//! Destructor
virtual ~CListModelBase() {}
virtual ~CListModelBaseNonTemplate() {}
//! \copydoc QAbstractListModel::columnCount()
virtual int columnCount(const QModelIndex &modelIndex) const override;
@@ -47,14 +49,6 @@ namespace BlackGui
return this->columnToPropertyIndex(index.column());
}
//! Valid index (in range)
virtual bool isValidIndex(const QModelIndex &index) const
{
if (!index.isValid()) return false;
return (index.row() >= 0 && index.row() < this->m_container.size() &&
index.column() >= 0 && index.column() < this->columnCount(index));
}
//! Set sort column
virtual void setSortColumn(int column) { this->m_sortedColumn = column; }
@@ -71,14 +65,67 @@ namespace BlackGui
virtual int getSortColumn() const { return this->m_sortedColumn; }
//! Has valid sort column?
virtual bool hasValidSortColumn() const
{
return this->m_sortedColumn >= 0 && this->m_sortedColumn < this->m_columns.size();
}
virtual bool hasValidSortColumn() const;
//! Get sort order
virtual Qt::SortOrder getSortOrder() const { return this->m_sortOrder; }
//! \copydoc QAbstractTableModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override;
//! Translation context
virtual const QString &getTranslationContext() const
{
return m_columns.getTranslationContext();
}
signals:
//! Asynchronous update finished
void asyncUpdateFinished();
protected slots:
//! Helper method with template free signature
int updateContainer(const QVariant &variant, bool sort)
{
return this->performUpdateContainer(variant, sort);
}
protected:
/*!
* Constructor
* \param translationContext I18N context
* \param parent
*/
CListModelBaseNonTemplate(const QString &translationContext, QObject *parent = nullptr)
: QAbstractListModel(parent), m_columns(translationContext), m_sortedColumn(-1), m_sortOrder(Qt::AscendingOrder)
{
// non unique default name, set translation context as default
this->setObjectName(translationContext);
}
//! Helper method with template free signature
virtual int performUpdateContainer(const QVariant &variant, bool sort) = 0;
CColumns m_columns; //!< columns metadata
int m_sortedColumn; //!< current sort column
Qt::SortOrder m_sortOrder; //!< sort order (asc/desc)
};
/*!
* List model
*/
template <typename ObjectType, typename ContainerType> class CListModelBase :
public CListModelBaseNonTemplate
{
public:
//! Destructor
virtual ~CListModelBase() {}
//! Valid index (in range)
virtual bool isValidIndex(const QModelIndex &index) const;
//! Used container data
virtual const ContainerType &getContainer() const { return this->m_container; }
@@ -88,13 +135,17 @@ namespace BlackGui
//! \copydoc QAbstractListModel::rowCount()
virtual int rowCount(const QModelIndex &index = QModelIndex()) const override;
//! \copydoc QAbstractTableModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override;
//! Update by new container
//! \remarks a sorting is performed only if a valid sort column is set
virtual int update(const ContainerType &container, bool sort = true);
//! Asynchronous update
//! \return worker or nullptr if worker could not be started
virtual BlackGui::IUpdateWorker *updateAsync(const ContainerType &container, bool sort = true);
//! Update by new container
virtual void updateContainerMaybeAsync(const ContainerType &container, bool sort = true);
//! Update single element
virtual void update(const QModelIndex &index, const ObjectType &object);
@@ -105,22 +156,21 @@ namespace BlackGui
}
//! Object at row position
virtual const ObjectType &at(const QModelIndex &index) const
{
if (index.row() < 0 || index.row() >= this->m_container.size())
{
const static ObjectType def; // default object
return def;
}
else
{
return this->m_container[index.row()];
}
}
virtual const ObjectType &at(const QModelIndex &index) const;
//! \copydoc QAbstractListModel::sort()
virtual void sort(int column, Qt::SortOrder order) override;
/*!
* Sort container by given column / order. This is used by sort() but als
* for asynchronous updates in the views
* \param container used list
* \param column column inder
* \param order sort order (ascending / descending)
* \threadsafe
*/
ContainerType sortContainerByColumn(const ContainerType &container, int column, Qt::SortOrder order) const;
//! Similar to ContainerType::push_back
virtual void push_back(const ObjectType &object);
@@ -133,17 +183,8 @@ namespace BlackGui
//! Clear the list
virtual void clear();
//! Translation context
virtual const QString &getTranslationContext() const
{
return m_columns.getTranslationContext();
}
protected:
ContainerType m_container; //!< used container
CColumns m_columns; //!< columns metadata
int m_sortedColumn; //!< current sort column
Qt::SortOrder m_sortOrder; //!< sort order (asc/desc)
/*!
* Constructor
@@ -151,22 +192,61 @@ namespace BlackGui
* \param parent
*/
CListModelBase(const QString &translationContext, QObject *parent = nullptr)
: QAbstractListModel(parent), m_columns(translationContext), m_sortedColumn(-1), m_sortOrder(Qt::AscendingOrder)
{
// non unique default name, set translation context as default
this->setObjectName(translationContext);
}
: CListModelBaseNonTemplate(translationContext, parent)
{ }
/*!
* Sort container by given column / order. This is used by sort().
* \param list used list
* \param column column inder
* \param order sort order (ascending / descending)
* \return
*/
ContainerType sortListByColumn(const ContainerType &list, int column, Qt::SortOrder order);
//! \copydoc CModelBaseNonTemplate::performUpdateContainer
virtual int performUpdateContainer(const QVariant &variant, bool sort) override;
// ---- worker -----------------------------------------------------------------------------------
//! Worker class performing update and sorting in background
class CModelUpdateWorker : public BlackGui::IUpdateWorker
{
public:
//! Constructor
CModelUpdateWorker(CListModelBase *model, const ContainerType &container, bool sort) :
BlackGui::IUpdateWorker(sort), m_model(model), m_container(container)
{
Q_ASSERT(model);
this->m_sortColumn = model->getSortColumn();
this->m_sortOrder = model->getSortOrder();
connect(this, &CModelUpdateWorker::updateFinished, model, &CListModelBase::asyncUpdateFinished, Qt::QueuedConnection);
this->setObjectName(model->objectName().append(":CModelUpdateWorker"));
}
//! Destructor
virtual ~CModelUpdateWorker() {}
protected:
//! \copydoc CUpdateWorkerPrivate::update
virtual void update() override
{
// KWB remove later
qDebug() << this->objectName() << "thread:" << QThread::currentThreadId();
if (m_model)
{
if (m_sort)
{
// almost thread safe sorting in background
m_container = m_model->sortContainerByColumn(m_container, m_sortColumn, m_sortOrder);
}
// now update model itself thread safe, but time for sort was saved
QMetaObject::invokeMethod(m_model, "updateContainer", Qt::QueuedConnection,
Q_ARG(QVariant, m_container.toQVariant()), Q_ARG(bool, false));
}
}
CListModelBase *m_model = nullptr; //!< model to be updated, actually const but invokeMethod does not allow const
ContainerType m_container; //!< container with data
};
// ---- worker -----------------------------------------------------------------------------------
};
} // namespace
} // namespace
#endif // guard