mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-05-03 07:35:53 +08:00
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:
@@ -20,7 +20,7 @@ namespace BlackGui
|
||||
namespace Views
|
||||
{
|
||||
//! Aircrafts view
|
||||
class CAircraftView : public CViewBase<Models::CAircraftListModel>
|
||||
class CAircraftView : public CViewBase<Models::CAircraftListModel, BlackMisc::Aviation::CAircraftList>
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace BlackGui
|
||||
namespace Views
|
||||
{
|
||||
//! Airports view
|
||||
class CAirportView : public CViewBase<Models::CAirportListModel>
|
||||
class CAirportView : public CViewBase<Models::CAirportListModel, BlackMisc::Aviation::CAirportList>
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
@@ -46,8 +46,7 @@ namespace BlackGui
|
||||
void CAtcStationView::changedAtcStationConnectionStatus(const Aviation::CAtcStation &station, bool added)
|
||||
{
|
||||
this->m_model->changedAtcStationConnectionStatus(station, added);
|
||||
this->resizeColumnsToContents();
|
||||
this->resizeRowsToContents();
|
||||
this->resizeToContents();
|
||||
}
|
||||
|
||||
void CAtcStationView::customMenu(QMenu &menu) const
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace BlackGui
|
||||
namespace Views
|
||||
{
|
||||
//! ATC stations view
|
||||
class CAtcStationView : public CViewBase<Models::CAtcStationListModel>
|
||||
class CAtcStationView : public CViewBase<Models::CAtcStationListModel, BlackMisc::Aviation::CAtcStationList>
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace BlackGui
|
||||
namespace Views
|
||||
{
|
||||
//! Client view
|
||||
class CClientView : public CViewBase<Models::CClientListModel>
|
||||
class CClientView : public CViewBase<Models::CClientListModel, BlackMisc::Network::CClientList>
|
||||
{
|
||||
public:
|
||||
//! Constructor
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace BlackGui
|
||||
namespace Views
|
||||
{
|
||||
//! Keyboard key view
|
||||
class CKeyboardKeyView : public CViewBase<Models::CKeyboardKeyListModel>
|
||||
class CKeyboardKeyView : public CViewBase<Models::CKeyboardKeyListModel, BlackMisc::Settings::CSettingKeyboardHotkeyList>
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace BlackGui
|
||||
namespace Views
|
||||
{
|
||||
//! User view
|
||||
class CNameVariantPairView : public CViewBase<Models::CNameVariantPairModel>
|
||||
class CNameVariantPairView : public CViewBase<Models::CNameVariantPairModel, BlackMisc::CNameVariantPairList>
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace BlackGui
|
||||
namespace Views
|
||||
{
|
||||
//! Network servers
|
||||
class CServerView : public CViewBase<Models::CServerListModel>
|
||||
class CServerView : public CViewBase<Models::CServerListModel, BlackMisc::Network::CServerList>
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace BlackGui
|
||||
namespace Views
|
||||
{
|
||||
//! Status message view
|
||||
class CStatusMessageView : public CViewBase<Models::CStatusMessageListModel>
|
||||
class CStatusMessageView : public CViewBase<Models::CStatusMessageListModel, BlackMisc::CStatusMessageList>
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace BlackGui
|
||||
namespace Views
|
||||
{
|
||||
//! User view
|
||||
class CUserView : public CViewBase<Models::CUserListModel>
|
||||
class CUserView : public CViewBase<Models::CUserListModel, BlackMisc::Network::CUserList>
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
261
src/blackgui/views/viewbase.cpp
Normal file
261
src/blackgui/views/viewbase.cpp
Normal file
@@ -0,0 +1,261 @@
|
||||
/* 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 "viewbase.h"
|
||||
#include "../models/statusmessagelistmodel.h"
|
||||
#include "../models/namevariantpairlistmodel.h"
|
||||
#include "../models/atcstationlistmodel.h"
|
||||
#include "../models/aircraftlistmodel.h"
|
||||
#include "../models/airportlistmodel.h"
|
||||
#include "../models/serverlistmodel.h"
|
||||
#include "../models/userlistmodel.h"
|
||||
#include "../models/clientlistmodel.h"
|
||||
#include "../models/keyboardkeylistmodel.h"
|
||||
|
||||
#include <QHeaderView>
|
||||
#include <QModelIndex>
|
||||
#include <QTime>
|
||||
#include <QAction>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackGui::Models;
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
namespace Views
|
||||
{
|
||||
|
||||
void CViewBaseNonTemplate::resizeToContents()
|
||||
{
|
||||
this->performResizeToContents();
|
||||
}
|
||||
|
||||
CViewBaseNonTemplate::CViewBaseNonTemplate(QWidget *parent) : QTableView(parent)
|
||||
{
|
||||
this->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
connect(this, &QWidget::customContextMenuRequested, this, &CViewBaseNonTemplate::ps_customMenuRequested);
|
||||
}
|
||||
|
||||
void CViewBaseNonTemplate::customMenu(QMenu &menu) const
|
||||
{
|
||||
menu.addAction(BlackMisc::CIcons::refresh16(), "Update", this, SIGNAL(requestUpdate()));
|
||||
menu.addAction(BlackMisc::CIcons::delete16(), "Clear", this, SLOT(ps_clear()));
|
||||
menu.addSeparator();
|
||||
menu.addAction(BlackMisc::CIcons::resize16(), "Full resize", this, SLOT(fullResizeToContents()));
|
||||
|
||||
// resize to content might decrease performance,
|
||||
// so I only allow changing to "content resizing" if size matches
|
||||
bool enabled = !this->reachedResizeThreshold();
|
||||
QAction *actionInteractiveResize = new QAction(&menu);
|
||||
actionInteractiveResize->setObjectName(this->objectName().append("ActionResizing"));
|
||||
actionInteractiveResize->setIconText("Resize (auto)");
|
||||
actionInteractiveResize->setIcon(CIcons::viewMulticolumn());
|
||||
actionInteractiveResize->setCheckable(true);
|
||||
actionInteractiveResize->setChecked(this->m_resizeMode == ResizingAuto);
|
||||
actionInteractiveResize->setEnabled(enabled);
|
||||
menu.addAction(actionInteractiveResize);
|
||||
connect(actionInteractiveResize, &QAction::toggled, this, &CViewBaseNonTemplate::ps_toggleResizeMode);
|
||||
}
|
||||
|
||||
int CViewBaseNonTemplate::getHorizontalHeaderFontHeight() const
|
||||
{
|
||||
QFontMetrics m(this->getHorizontalHeaderFont());
|
||||
int h = m.height();
|
||||
return h;
|
||||
}
|
||||
|
||||
void CViewBaseNonTemplate::standardInit()
|
||||
{
|
||||
int fh = qRound(1.5 * this->getHorizontalHeaderFontHeight());
|
||||
this->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); // faster mode
|
||||
this->horizontalHeader()->setStretchLastSection(true);
|
||||
this->verticalHeader()->setDefaultSectionSize(fh);
|
||||
this->verticalHeader()->setMinimumSectionSize(fh);
|
||||
this->initRowsResizeModeToInteractive();
|
||||
}
|
||||
|
||||
void CViewBaseNonTemplate::initRowsResizeModeToInteractive()
|
||||
{
|
||||
const int h = this->verticalHeader()->minimumSectionSize();
|
||||
this->setRowsResizeModeToInteractive(h);
|
||||
}
|
||||
|
||||
void CViewBaseNonTemplate::setRowsResizeModeToInteractive(int height)
|
||||
{
|
||||
QHeaderView *verticalHeader = this->verticalHeader();
|
||||
Q_ASSERT(verticalHeader);
|
||||
verticalHeader->setSectionResizeMode(QHeaderView::Interactive);
|
||||
verticalHeader->setDefaultSectionSize(height);
|
||||
}
|
||||
|
||||
bool CViewBaseNonTemplate::resize() const
|
||||
{
|
||||
if (m_resizeMode == ResizingOnce) { return m_resizeCount < 1; }
|
||||
if (m_resizeMode == ResizingAuto)
|
||||
{
|
||||
if (reachedResizeThreshold()) return false;
|
||||
if (m_resizeAutoNthTime < 2) return true;
|
||||
return (m_resizeCount % m_resizeAutoNthTime) == 0;
|
||||
}
|
||||
return m_resizeMode == ResizingOff;
|
||||
}
|
||||
|
||||
void CViewBaseNonTemplate::fullResizeToContents()
|
||||
{
|
||||
// KWB remove
|
||||
QTime t;
|
||||
t.start();
|
||||
|
||||
m_resizeCount++;
|
||||
this->resizeColumnsToContents();
|
||||
this->resizeRowsToContents();
|
||||
|
||||
qDebug() << this->objectName() << "resize ms:" << t.elapsed() << QThread::currentThreadId();
|
||||
}
|
||||
|
||||
void CViewBaseNonTemplate::ps_customMenuRequested(QPoint pos)
|
||||
{
|
||||
QMenu menu;
|
||||
this->customMenu(menu);
|
||||
if (menu.isEmpty()) { return; }
|
||||
|
||||
QPoint globalPos = this->mapToGlobal(pos);
|
||||
menu.exec(globalPos);
|
||||
}
|
||||
|
||||
void CViewBaseNonTemplate::ps_toggleResizeMode(bool checked)
|
||||
{
|
||||
if (checked)
|
||||
{
|
||||
this->m_resizeMode = ResizingAuto;
|
||||
}
|
||||
else
|
||||
{
|
||||
this->m_resizeMode = ResizingOff;
|
||||
}
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType> int CViewBase<ModelClass, ContainerType>::updateContainer(const ContainerType &container, bool sort, bool resize)
|
||||
{
|
||||
Q_ASSERT(this->m_model);
|
||||
int c = this->m_model->update(container, sort);
|
||||
if (resize) { this->resizeToContents(); }
|
||||
return c;
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType> IUpdateWorker *CViewBase<ModelClass, ContainerType>::updateContainerAsync(const ContainerType &container, bool sort, bool resize)
|
||||
{
|
||||
// TODO: mutex
|
||||
CViewUpdateWorker *worker = new CViewUpdateWorker(this, container, sort, resize);
|
||||
if (worker->start()) { return worker; }
|
||||
|
||||
// start failed, we have responsibility to clean up the worker
|
||||
Q_ASSERT_X(false, "CViewBase", "cannot start worker");
|
||||
worker->terminate();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType> void CViewBase<ModelClass, ContainerType>::updateContainerMaybeAsync(const ContainerType &container, bool sort, bool resize)
|
||||
{
|
||||
if (container.size() > asyncThreshold && sort)
|
||||
{
|
||||
// larger container with sorting
|
||||
updateContainerAsync(container, sort, resize);
|
||||
}
|
||||
else
|
||||
{
|
||||
updateContainer(container, sort, resize);
|
||||
}
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType> int CViewBase<ModelClass, ContainerType>::rowCount() const
|
||||
{
|
||||
Q_ASSERT(this->m_model);
|
||||
return this->m_model->rowCount();
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType> int CViewBase<ModelClass, ContainerType>::columnCount() const
|
||||
{
|
||||
Q_ASSERT(this->m_model);
|
||||
return this->m_model->columnCount(QModelIndex());
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType> bool CViewBase<ModelClass, ContainerType>::isEmpty() const
|
||||
{
|
||||
Q_ASSERT(this->m_model);
|
||||
return this->m_model->rowCount() < 1;
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType> void CViewBase<ModelClass, ContainerType>::setObjectName(const QString &name)
|
||||
{
|
||||
// then name here is mainly set for debugging purposes so each model can be identified
|
||||
Q_ASSERT(m_model);
|
||||
QTableView::setObjectName(name);
|
||||
QString modelName = QString(name).append(':').append(this->m_model->getTranslationContext());
|
||||
this->m_model->setObjectName(modelName);
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType> void CViewBase<ModelClass, ContainerType>::setSortIndicator()
|
||||
{
|
||||
if (this->m_model->hasValidSortColumn())
|
||||
{
|
||||
Q_ASSERT(this->horizontalHeader());
|
||||
this->horizontalHeader()->setSortIndicator(
|
||||
this->m_model->getSortColumn(),
|
||||
this->m_model->getSortOrder());
|
||||
}
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType> void CViewBase<ModelClass, ContainerType>::standardInit(ModelClass *model)
|
||||
{
|
||||
Q_ASSERT(model || this->m_model);
|
||||
if (model)
|
||||
{
|
||||
this->m_model = model;
|
||||
connect(this->m_model, &ModelClass::rowCountChanged, this, &CViewBase::countChanged);
|
||||
}
|
||||
this->setModel(this->m_model); // via QTableView
|
||||
CViewBaseNonTemplate::standardInit();
|
||||
this->setSortIndicator();
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType> void CViewBase<ModelClass, ContainerType>::performResizeToContents()
|
||||
{
|
||||
// small set or large set?
|
||||
if (this->resize())
|
||||
{
|
||||
this->fullResizeToContents();
|
||||
}
|
||||
else
|
||||
{
|
||||
this->m_resizeCount++; // skipped resize
|
||||
}
|
||||
}
|
||||
|
||||
template <class ModelClass, class ContainerType> int CViewBase<ModelClass, ContainerType>::performUpdateContainer(const QVariant &variant, bool sort, bool resize)
|
||||
{
|
||||
ContainerType c;
|
||||
c.convertFromQVariant(variant);
|
||||
return this->updateContainer(c, sort, resize);
|
||||
}
|
||||
|
||||
// see here for the reason of thess forward instantiations
|
||||
// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html
|
||||
template class CViewBase<BlackGui::Models::CStatusMessageListModel, BlackMisc::CStatusMessageList>;
|
||||
template class CViewBase<BlackGui::Models::CNameVariantPairModel, BlackMisc::CNameVariantPairList>;
|
||||
template class CViewBase<BlackGui::Models::CAtcStationListModel, BlackMisc::Aviation::CAtcStationList>;
|
||||
template class CViewBase<BlackGui::Models::CAircraftListModel, BlackMisc::Aviation::CAircraftList>;
|
||||
template class CViewBase<BlackGui::Models::CAirportListModel, BlackMisc::Aviation::CAirportList>;
|
||||
template class CViewBase<BlackGui::Models::CServerListModel, BlackMisc::Network::CServerList>;
|
||||
template class CViewBase<BlackGui::Models::CUserListModel, BlackMisc::Network::CUserList>;
|
||||
template class CViewBase<BlackGui::Models::CClientListModel, BlackMisc::Network::CClientList>;
|
||||
template class CViewBase<BlackGui::Models::CKeyboardKeyListModel, BlackMisc::Settings::CSettingKeyboardHotkeyList>;
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
@@ -13,66 +13,132 @@
|
||||
#define BLACKGUI_VIEWBASE_H
|
||||
|
||||
#include "blackmisc/icons.h"
|
||||
#include "blackgui/updateworker.h"
|
||||
#include <QTableView>
|
||||
#include <QHeaderView>
|
||||
#include <QMenu>
|
||||
#include <QPoint>
|
||||
#include <QFont>
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
namespace Views
|
||||
{
|
||||
|
||||
//! Non templated base class, allows Q_OBJECT and signals to be used
|
||||
//! Non templated base class, allows Q_OBJECT and signals / slots to be used
|
||||
class CViewBaseNonTemplate : public QTableView
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
//! Resize mode
|
||||
//! \remarks Using own resizing (other than QHeaderView::ResizeMode)
|
||||
enum ResizeMode
|
||||
{
|
||||
ResizingAuto, //!< always resizing, \sa m_resizeAutoNthTime
|
||||
ResizingOnce, //!< only one time
|
||||
ResizingOff
|
||||
};
|
||||
|
||||
//! When to use asynchronous updates
|
||||
static const int asyncThreshold = 50;
|
||||
|
||||
//! Clear data
|
||||
virtual void clear() = 0;
|
||||
|
||||
//! Current rows resize mode
|
||||
ResizeMode getResizeMode() const { return m_resizeMode; }
|
||||
|
||||
//! Set resize mode
|
||||
void setResizeMode(ResizeMode mode) { m_resizeMode = mode; }
|
||||
|
||||
//! In ResizeAuto mode, how often to update. "1" updates every time, "2" every 2nd time, ..
|
||||
void setAutoResizeFrequency(int updateEveryNthTime) { this->m_resizeAutoNthTime = updateEveryNthTime; }
|
||||
|
||||
//! Header (horizontal) font
|
||||
const QFont &getHorizontalHeaderFont() const { Q_ASSERT(this->horizontalHeader()); return this->horizontalHeader()->font(); }
|
||||
|
||||
//! Horizontal font height
|
||||
int getHorizontalHeaderFontHeight() const;
|
||||
|
||||
signals:
|
||||
//! Ask for new data
|
||||
void requestUpdate();
|
||||
|
||||
//! Asynchronous update finished
|
||||
void asyncUpdateFinished();
|
||||
|
||||
public slots:
|
||||
//! Resize to contents, strategy depends on container size
|
||||
virtual void resizeToContents();
|
||||
|
||||
//! Full resizing to content, might be slow
|
||||
virtual void fullResizeToContents();
|
||||
|
||||
protected:
|
||||
//! Constructor
|
||||
CViewBaseNonTemplate(QWidget *parent) : QTableView(parent)
|
||||
{
|
||||
this->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
connect(this, &QWidget::customContextMenuRequested, this, &CViewBaseNonTemplate::ps_customMenuRequested);
|
||||
}
|
||||
CViewBaseNonTemplate(QWidget *parent);
|
||||
|
||||
//! Method creating the menu
|
||||
//! \remarks override this method to contribute to the menu
|
||||
virtual void customMenu(QMenu &menu) const
|
||||
virtual void customMenu(QMenu &menu) const;
|
||||
|
||||
//! Perform resizing / non slot method for template
|
||||
virtual void performResizeToContents() = 0;
|
||||
|
||||
//! Init as interactive, as this allows manually resizing
|
||||
void initRowsResizeModeToInteractive();
|
||||
|
||||
//! Set fixed row height (vertical header with height)
|
||||
//! \sa initRowsResizeModeToFixed
|
||||
virtual void setRowsResizeModeToInteractive(int height);
|
||||
|
||||
//! Helper method with template free signature
|
||||
//! \param variant contains the container
|
||||
//! \param sort
|
||||
//! \param resize
|
||||
virtual int performUpdateContainer(const QVariant &variant, bool sort, bool resize) = 0;
|
||||
|
||||
//! Skip resizing
|
||||
virtual bool skipResize() const = 0;
|
||||
|
||||
//! Resize or skip resize?
|
||||
virtual bool resize() const;
|
||||
|
||||
//! Init default values
|
||||
virtual void standardInit();
|
||||
|
||||
ResizeMode m_resizeMode = ResizingAuto; //!< mode
|
||||
int m_resizeCount = 0; //!< flag / counter,how many resize activities
|
||||
int m_skipResizeThreshold = 40; //!< when to skip resize
|
||||
int m_resizeAutoNthTime = 1; //!< with ResizeAuto, resize every n-th time
|
||||
|
||||
protected slots:
|
||||
//! Helper method with template free signature serving as callback from threaded worker
|
||||
int updateContainer(const QVariant &variant, bool sort, bool resize)
|
||||
{
|
||||
menu.addAction(BlackMisc::CIcons::refresh16(), "Update", this, SIGNAL(requestUpdate()));
|
||||
menu.addAction(BlackMisc::CIcons::delete16(), "Clear", this, SLOT(ps_clear()));
|
||||
return this->performUpdateContainer(variant, sort, resize);
|
||||
}
|
||||
|
||||
private slots:
|
||||
//! Custom menu was requested
|
||||
void ps_customMenuRequested(QPoint pos)
|
||||
{
|
||||
QMenu menu;
|
||||
this->customMenu(menu);
|
||||
if (menu.isEmpty()) { return; }
|
||||
void ps_customMenuRequested(QPoint pos);
|
||||
|
||||
QPoint globalPos = this->mapToGlobal(pos);
|
||||
menu.exec(globalPos);
|
||||
}
|
||||
//! Toggle the resize mode
|
||||
void ps_toggleResizeMode(bool checked);
|
||||
|
||||
//! Clear the model
|
||||
virtual void ps_clear() { this->clear(); }
|
||||
|
||||
};
|
||||
|
||||
//! Base class for views
|
||||
template <class ModelClass> class CViewBase : public CViewBaseNonTemplate
|
||||
template <class ModelClass, class ContainerType> class CViewBase : public CViewBaseNonTemplate
|
||||
{
|
||||
|
||||
public:
|
||||
//! Destructor
|
||||
virtual ~CViewBase() {}
|
||||
|
||||
//! Model
|
||||
ModelClass *derivedModel() { return this->m_model; }
|
||||
@@ -84,24 +150,21 @@ namespace BlackGui
|
||||
virtual void clear() override { Q_ASSERT(this->m_model); this->m_model->clear(); }
|
||||
|
||||
//! Update whole container
|
||||
template<class ContainerType> int updateContainer(const ContainerType &container, bool sort = true, bool resize = true)
|
||||
{
|
||||
Q_ASSERT(this->m_model);
|
||||
int c = this->m_model->update(container, sort);
|
||||
if (!resize) return c;
|
||||
this->resizeColumnsToContents();
|
||||
this->resizeRowsToContents();
|
||||
return c;
|
||||
}
|
||||
int updateContainer(const ContainerType &container, bool sort = true, bool resize = true);
|
||||
|
||||
//! Update whole container in background
|
||||
//! \returns Worker or nullptr if worker cannot be started
|
||||
IUpdateWorker *updateContainerAsync(const ContainerType &container, bool sort = true, bool resize = true);
|
||||
|
||||
//! Based on size call sync / async update
|
||||
void updateContainerMaybeAsync(const ContainerType &container, bool sort = true, bool resize = true);
|
||||
|
||||
//! Insert
|
||||
template<class ObjectType> void insert(const ObjectType &value, bool resize = true)
|
||||
{
|
||||
Q_ASSERT(this->m_model);
|
||||
this->m_model->insert(value);
|
||||
if (!resize) return;
|
||||
this->resizeColumnsToContents();
|
||||
this->resizeRowsToContents();
|
||||
if (resize) { this->performResizeToContents(); }
|
||||
}
|
||||
|
||||
//! Value object at
|
||||
@@ -112,35 +175,16 @@ namespace BlackGui
|
||||
}
|
||||
|
||||
//! Row count
|
||||
int rowCount() const
|
||||
{
|
||||
Q_ASSERT(this->m_model);
|
||||
return this->m_model->rowCount();
|
||||
}
|
||||
int rowCount() const;
|
||||
|
||||
//! Column count
|
||||
int columnCount() const
|
||||
{
|
||||
Q_ASSERT(this->m_model);
|
||||
return this->m_model->columnCount();
|
||||
}
|
||||
int columnCount() const;
|
||||
|
||||
//! Any data?
|
||||
bool isEmpty() const
|
||||
{
|
||||
Q_ASSERT(this->m_model);
|
||||
return this->m_model->rowCount() < 1;
|
||||
}
|
||||
bool isEmpty() const;
|
||||
|
||||
//! Set own name and the model's name
|
||||
void setObjectName(const QString &name)
|
||||
{
|
||||
// then name here is mainly set for debugging purposes so each model can be identified
|
||||
Q_ASSERT(m_model);
|
||||
QTableView::setObjectName(name);
|
||||
QString modelName = QString(name).append(':').append(this->m_model->getTranslationContext());
|
||||
this->m_model->setObjectName(modelName);
|
||||
}
|
||||
virtual void setObjectName(const QString &name);
|
||||
|
||||
protected:
|
||||
ModelClass *m_model = nullptr; //!< corresponding model
|
||||
@@ -152,37 +196,77 @@ namespace BlackGui
|
||||
if (model) { this->setModel(this->m_model); }
|
||||
}
|
||||
|
||||
//! Destructor
|
||||
virtual ~CViewBase() {}
|
||||
|
||||
//! Set the search indicator based on model
|
||||
void setSortIndicator()
|
||||
void setSortIndicator();
|
||||
|
||||
//! \copydoc CViewBaseNonTemplate::standardInit
|
||||
void standardInit(ModelClass *model = nullptr);
|
||||
|
||||
//! \copydoc CViewBaseNonTemplate::reachedResizeThreshold
|
||||
virtual bool reachedResizeThreshold() const override { return this->rowCount() > m_skipResizeThreshold; }
|
||||
|
||||
//! \copydoc CViewBaseNonTemplate::performResizing
|
||||
virtual void performResizeToContents() override;
|
||||
|
||||
//! \copydoc CViewBaseNonTemplate::performUpdateContainer
|
||||
virtual int performUpdateContainer(const QVariant &variant, bool sort, bool resize) override;
|
||||
|
||||
// ---- worker -----------------------------------------------------------------------------------
|
||||
|
||||
//! Worker class performing update and sorting in background
|
||||
class CViewUpdateWorker : public BlackGui::IUpdateWorker
|
||||
{
|
||||
if (this->m_model->hasValidSortColumn())
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
CViewUpdateWorker(CViewBase *view, const ContainerType &container, bool sort, bool resize) :
|
||||
BlackGui::IUpdateWorker(sort), m_view(view), m_container(container), m_resize(resize)
|
||||
{
|
||||
this->horizontalHeader()->setSortIndicator(
|
||||
this->m_model->getSortColumn(),
|
||||
this->m_model->getSortOrder());
|
||||
Q_ASSERT(view);
|
||||
this->m_sortColumn = view->derivedModel()->getSortColumn();
|
||||
this->m_sortOrder = view->derivedModel()->getSortOrder();
|
||||
this->setObjectName(view->objectName().append(":CViewUpdateWorker"));
|
||||
connect(this, &CViewUpdateWorker::updateFinished, view, &CViewBase::asyncUpdateFinished, Qt::QueuedConnection);
|
||||
}
|
||||
}
|
||||
|
||||
//! Resize to content
|
||||
void resizeToContents()
|
||||
{
|
||||
this->resizeColumnsToContents();
|
||||
this->resizeRowsToContents();
|
||||
}
|
||||
//! Destructor
|
||||
virtual ~CViewUpdateWorker() {}
|
||||
|
||||
protected:
|
||||
//! \copydoc CUpdateWorkerPrivate::update
|
||||
virtual void update() override
|
||||
{
|
||||
Q_ASSERT(m_view);
|
||||
Q_ASSERT(m_view->derivedModel());
|
||||
if (m_view)
|
||||
{
|
||||
// KWB remove later
|
||||
qDebug() << this->objectName() << "worker thread:" << QThread::currentThreadId();
|
||||
|
||||
// resize has to be in main thread
|
||||
ModelClass *model = m_view->derivedModel();
|
||||
if (m_sort)
|
||||
{
|
||||
// thread safe sort:
|
||||
// 1) the container itself is copied when worker is created and hence thread safe
|
||||
// 2) the sort order itself is not really thread safe,
|
||||
// but always represents the latest value from CListModelBase/QAbstractListModel::sort()
|
||||
m_container = model->sortContainerByColumn(m_container, m_sortColumn, m_sortOrder);
|
||||
}
|
||||
// now update view itself thread safe, but time for sort was saved
|
||||
QMetaObject::invokeMethod(m_view, "updateContainer", Qt::QueuedConnection,
|
||||
Q_ARG(QVariant, m_container.toQVariant()), Q_ARG(bool, false), Q_ARG(bool, m_resize));
|
||||
}
|
||||
}
|
||||
|
||||
CViewBase *m_view = nullptr; //!< view to be updated, actually const but invokeMethod does not allow const
|
||||
ContainerType m_container; //!< container with data
|
||||
bool m_resize; //!< with resizing
|
||||
};
|
||||
|
||||
// ---- worker -----------------------------------------------------------------------------------
|
||||
|
||||
//! Init
|
||||
void standardInit(ModelClass *model = nullptr)
|
||||
{
|
||||
Q_ASSERT(model || this->m_model);
|
||||
if (model) { this->m_model = model; }
|
||||
this->setModel(this->m_model); // via QTableView
|
||||
this->setSortIndicator();
|
||||
this->horizontalHeader()->setStretchLastSection(true);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
} // namespace
|
||||
#endif // guard
|
||||
|
||||
Reference in New Issue
Block a user