mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Cleanup: make DiveTripModel a global object
DiveTripModel (the model describing the dive-list) was destroyed and recreated on every reset of the list. This seems excessive. Instead - in analogy to most other models - make it a single global object. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
236f0512be
commit
89e0c3f464
7 changed files with 31 additions and 28 deletions
|
@ -36,8 +36,7 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
|
||||||
model->setSortRole(DiveTripModel::SORT_ROLE);
|
model->setSortRole(DiveTripModel::SORT_ROLE);
|
||||||
model->setFilterKeyColumn(-1); // filter all columns
|
model->setFilterKeyColumn(-1); // filter all columns
|
||||||
model->setFilterCaseSensitivity(Qt::CaseInsensitive);
|
model->setFilterCaseSensitivity(Qt::CaseInsensitive);
|
||||||
DiveTripModel *tripModel = new DiveTripModel(this);
|
model->setSourceModel(DiveTripModel::instance());
|
||||||
model->setSourceModel(tripModel);
|
|
||||||
setModel(model);
|
setModel(model);
|
||||||
|
|
||||||
setSortingEnabled(false);
|
setSortingEnabled(false);
|
||||||
|
@ -50,7 +49,7 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
|
||||||
installEventFilter(this);
|
installEventFilter(this);
|
||||||
|
|
||||||
for (int i = DiveTripModel::NR; i < DiveTripModel::COLUMNS; i++)
|
for (int i = DiveTripModel::NR; i < DiveTripModel::COLUMNS; i++)
|
||||||
calculateInitialColumnWidth(tripModel, i);
|
calculateInitialColumnWidth(i);
|
||||||
setColumnWidths();
|
setColumnWidths();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,13 +71,13 @@ DiveListView::~DiveListView()
|
||||||
settings.endGroup();
|
settings.endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiveListView::calculateInitialColumnWidth(const DiveTripModel &tripModel, int col)
|
void DiveListView::calculateInitialColumnWidth(int col)
|
||||||
{
|
{
|
||||||
const QFontMetrics metrics(defaultModelFont());
|
const QFontMetrics metrics(defaultModelFont());
|
||||||
int em = metrics.width('m');
|
int em = metrics.width('m');
|
||||||
int zw = metrics.width('0');
|
int zw = metrics.width('0');
|
||||||
|
|
||||||
QString header_txt = tripModel.headerData(col, Qt::Horizontal, Qt::DisplayRole).toString();
|
QString header_txt = DiveTripModel::instance()->headerData(col, Qt::Horizontal, Qt::DisplayRole).toString();
|
||||||
int width = metrics.width(header_txt);
|
int width = metrics.width(header_txt);
|
||||||
int sw = 0;
|
int sw = 0;
|
||||||
switch (col) {
|
switch (col) {
|
||||||
|
@ -422,18 +421,13 @@ void DiveListView::reload(DiveTripModel::Layout layout, bool forceSort)
|
||||||
header()->setSectionsClickable(true);
|
header()->setSectionsClickable(true);
|
||||||
connect(header(), SIGNAL(sectionPressed(int)), this, SLOT(headerClicked(int)), Qt::UniqueConnection);
|
connect(header(), SIGNAL(sectionPressed(int)), this, SLOT(headerClicked(int)), Qt::UniqueConnection);
|
||||||
|
|
||||||
QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel *>(model());
|
DiveTripModel *tripModel = DiveTripModel::instance();
|
||||||
QAbstractItemModel *oldModel = m->sourceModel();
|
tripModel->setLayout(layout); // Note: setLayout() resets the whole model
|
||||||
DiveTripModel *tripModel = new DiveTripModel(this);
|
|
||||||
tripModel->setLayout(layout);
|
|
||||||
|
|
||||||
m->setSourceModel(tripModel);
|
|
||||||
if (oldModel)
|
|
||||||
delete oldModel;
|
|
||||||
|
|
||||||
if (!forceSort)
|
if (!forceSort)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel *>(model());
|
||||||
sortByColumn(sortColumn, currentOrder);
|
sortByColumn(sortColumn, currentOrder);
|
||||||
if (amount_selected && current_dive != NULL) {
|
if (amount_selected && current_dive != NULL) {
|
||||||
selectDive(selected_dive, true);
|
selectDive(selected_dive, true);
|
||||||
|
|
|
@ -78,7 +78,7 @@ private:
|
||||||
QMultiHash<dive_trip_t *, int> selectedDives;
|
QMultiHash<dive_trip_t *, int> selectedDives;
|
||||||
void merge_trip(const QModelIndex &a, const int offset);
|
void merge_trip(const QModelIndex &a, const int offset);
|
||||||
void setColumnWidths();
|
void setColumnWidths();
|
||||||
void calculateInitialColumnWidth(const DiveTripModel &tripModel, int col);
|
void calculateInitialColumnWidth(int col);
|
||||||
void backupExpandedRows();
|
void backupExpandedRows();
|
||||||
void restoreExpandedRows();
|
void restoreExpandedRows();
|
||||||
int lastVisibleColumn();
|
int lastVisibleColumn();
|
||||||
|
|
|
@ -437,6 +437,12 @@ int DiveItem::weight() const
|
||||||
return tw.grams;
|
return tw.grams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DiveTripModel *DiveTripModel::instance()
|
||||||
|
{
|
||||||
|
static DiveTripModel self;
|
||||||
|
return &self;
|
||||||
|
}
|
||||||
|
|
||||||
DiveTripModel::DiveTripModel(QObject *parent) :
|
DiveTripModel::DiveTripModel(QObject *parent) :
|
||||||
TreeModel(parent),
|
TreeModel(parent),
|
||||||
currentLayout(TREE)
|
currentLayout(TREE)
|
||||||
|
@ -586,9 +592,11 @@ void DiveTripModel::setupModelData()
|
||||||
|
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
|
|
||||||
|
clear();
|
||||||
if (autogroup)
|
if (autogroup)
|
||||||
autogroup_dives();
|
autogroup_dives();
|
||||||
dive_table.preexisting = dive_table.nr;
|
dive_table.preexisting = dive_table.nr;
|
||||||
|
QMap<dive_trip_t *, TripItem *> trips;
|
||||||
while (--i >= 0) {
|
while (--i >= 0) {
|
||||||
struct dive *dive = get_dive(i);
|
struct dive *dive = get_dive(i);
|
||||||
update_cylinder_related_info(dive);
|
update_cylinder_related_info(dive);
|
||||||
|
@ -598,7 +606,7 @@ void DiveTripModel::setupModelData()
|
||||||
diveItem->diveId = dive->id;
|
diveItem->diveId = dive->id;
|
||||||
|
|
||||||
if (!trip || currentLayout == LIST) {
|
if (!trip || currentLayout == LIST) {
|
||||||
diveItem->parent = rootItem;
|
diveItem->parent = rootItem.get();
|
||||||
rootItem->children.push_back(diveItem);
|
rootItem->children.push_back(diveItem);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -608,7 +616,7 @@ void DiveTripModel::setupModelData()
|
||||||
if (!trips.keys().contains(trip)) {
|
if (!trips.keys().contains(trip)) {
|
||||||
TripItem *tripItem = new TripItem();
|
TripItem *tripItem = new TripItem();
|
||||||
tripItem->trip = trip;
|
tripItem->trip = trip;
|
||||||
tripItem->parent = rootItem;
|
tripItem->parent = rootItem.get();
|
||||||
tripItem->children.push_back(diveItem);
|
tripItem->children.push_back(diveItem);
|
||||||
trips[trip] = tripItem;
|
trips[trip] = tripItem;
|
||||||
rootItem->children.push_back(tripItem);
|
rootItem->children.push_back(tripItem);
|
||||||
|
|
|
@ -93,6 +93,7 @@ public:
|
||||||
CURRENT
|
CURRENT
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static DiveTripModel *instance();
|
||||||
Qt::ItemFlags flags(const QModelIndex &index) const;
|
Qt::ItemFlags flags(const QModelIndex &index) const;
|
||||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
||||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
|
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
|
||||||
|
@ -102,7 +103,6 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setupModelData();
|
void setupModelData();
|
||||||
QMap<dive_trip_t *, TripItem *> trips;
|
|
||||||
Layout currentLayout;
|
Layout currentLayout;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -32,12 +32,12 @@ QVariant TreeItem::data(int, int) const
|
||||||
TreeModel::TreeModel(QObject *parent) : QAbstractItemModel(parent)
|
TreeModel::TreeModel(QObject *parent) : QAbstractItemModel(parent)
|
||||||
{
|
{
|
||||||
columns = 0; // I'm not sure about this one - I can't see where it gets initialized
|
columns = 0; // I'm not sure about this one - I can't see where it gets initialized
|
||||||
rootItem = new TreeItem();
|
rootItem.reset(new TreeItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
TreeModel::~TreeModel()
|
void TreeModel::clear()
|
||||||
{
|
{
|
||||||
delete rootItem;
|
rootItem.reset(new TreeItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant TreeModel::data(const QModelIndex &index, int role) const
|
QVariant TreeModel::data(const QModelIndex &index, int role) const
|
||||||
|
@ -64,7 +64,7 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) con
|
||||||
if (!hasIndex(row, column, parent))
|
if (!hasIndex(row, column, parent))
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
|
|
||||||
TreeItem *parentItem = (!parent.isValid()) ? rootItem : static_cast<TreeItem *>(parent.internalPointer());
|
TreeItem *parentItem = (!parent.isValid()) ? rootItem.get() : static_cast<TreeItem *>(parent.internalPointer());
|
||||||
|
|
||||||
TreeItem *childItem = parentItem->children[row];
|
TreeItem *childItem = parentItem->children[row];
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ QModelIndex TreeModel::parent(const QModelIndex &index) const
|
||||||
TreeItem *childItem = static_cast<TreeItem *>(index.internalPointer());
|
TreeItem *childItem = static_cast<TreeItem *>(index.internalPointer());
|
||||||
TreeItem *parentItem = childItem->parent;
|
TreeItem *parentItem = childItem->parent;
|
||||||
|
|
||||||
if (parentItem == rootItem || !parentItem)
|
if (parentItem == rootItem.get() || !parentItem)
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
|
|
||||||
return createIndex(parentItem->row(), 0, parentItem);
|
return createIndex(parentItem->row(), 0, parentItem);
|
||||||
|
@ -90,7 +90,7 @@ int TreeModel::rowCount(const QModelIndex &parent) const
|
||||||
TreeItem *parentItem;
|
TreeItem *parentItem;
|
||||||
|
|
||||||
if (!parent.isValid())
|
if (!parent.isValid())
|
||||||
parentItem = rootItem;
|
parentItem = rootItem.get();
|
||||||
else
|
else
|
||||||
parentItem = static_cast<TreeItem *>(parent.internalPointer());
|
parentItem = static_cast<TreeItem *>(parent.internalPointer());
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <QAbstractItemModel>
|
#include <QAbstractItemModel>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
struct TreeItem {
|
struct TreeItem {
|
||||||
Q_DECLARE_TR_FUNCTIONS(TreeItemDT)
|
Q_DECLARE_TR_FUNCTIONS(TreeItemDT)
|
||||||
|
@ -25,16 +26,16 @@ class TreeModel : public QAbstractItemModel {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
TreeModel(QObject *parent = 0);
|
TreeModel(QObject *parent = 0);
|
||||||
~TreeModel();
|
|
||||||
QVariant data(const QModelIndex &index, int role) const;
|
QVariant data(const QModelIndex &index, int role) const;
|
||||||
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
||||||
int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
||||||
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
|
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
|
||||||
QModelIndex parent(const QModelIndex &child) const;
|
QModelIndex parent(const QModelIndex &child) const;
|
||||||
|
void clear();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
int columns;
|
int columns;
|
||||||
TreeItem *rootItem;
|
std::unique_ptr<TreeItem> rootItem;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -187,7 +187,7 @@ void YearlyStatisticsModel::update_yearly_stats()
|
||||||
month++;
|
month++;
|
||||||
}
|
}
|
||||||
rootItem->children.append(item);
|
rootItem->children.append(item);
|
||||||
item->parent = rootItem;
|
item->parent = rootItem.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -199,7 +199,7 @@ void YearlyStatisticsModel::update_yearly_stats()
|
||||||
iChild->parent = item;
|
iChild->parent = item;
|
||||||
}
|
}
|
||||||
rootItem->children.append(item);
|
rootItem->children.append(item);
|
||||||
item->parent = rootItem;
|
item->parent = rootItem.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Show the statistic sorted by dive type */
|
/* Show the statistic sorted by dive type */
|
||||||
|
@ -213,6 +213,6 @@ void YearlyStatisticsModel::update_yearly_stats()
|
||||||
iChild->parent = item;
|
iChild->parent = item;
|
||||||
}
|
}
|
||||||
rootItem->children.append(item);
|
rootItem->children.append(item);
|
||||||
item->parent = rootItem;
|
item->parent = rootItem.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue