DiveListModel: don't add the dives one at a time

Most of the time we are adding all the dives, so do this in a single model
operation. This makes the case when adding a single dive (in the undo delete
function) slightly more complicated, but that seems totally worth it for the
speedup in the common case.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2016-04-05 21:17:37 -07:00
parent f16a3a1709
commit 1634c62b9a
3 changed files with 30 additions and 24 deletions

View file

@ -142,14 +142,10 @@ void QMLManager::openLocalThenRemote(QString url)
if (informational_prefs.unit_system == IMPERIAL) if (informational_prefs.unit_system == IMPERIAL)
informational_prefs.units = IMPERIAL_units; informational_prefs.units = IMPERIAL_units;
prefs.units = informational_prefs.units; prefs.units = informational_prefs.units;
int i;
struct dive *d;
process_dives(false, false); process_dives(false, false);
DiveListModel::instance()->clear(); DiveListModel::instance()->clear();
for_each_dive (i, d) { DiveListModel::instance()->addAllDives();
DiveListModel::instance()->addDive(d); appendTextToLog(QStringLiteral("%1 dives loaded from cache").arg(dive_table.nr));
}
appendTextToLog(QStringLiteral("%1 dives loaded from cache").arg(i));
} }
appendTextToLog(QStringLiteral("have cloud credentials, trying to connect")); appendTextToLog(QStringLiteral("have cloud credentials, trying to connect"));
tryRetrieveDataFromBackend(); tryRetrieveDataFromBackend();
@ -390,15 +386,11 @@ void QMLManager::loadDivesWithValidCredentials()
if (informational_prefs.unit_system == IMPERIAL) if (informational_prefs.unit_system == IMPERIAL)
informational_prefs.units = IMPERIAL_units; informational_prefs.units = IMPERIAL_units;
prefs.units = informational_prefs.units; prefs.units = informational_prefs.units;
clear_dive_file_data();
DiveListModel::instance()->clear();
process_dives(false, false); process_dives(false, false);
DiveListModel::instance()->addAllDives();
int i; appendTextToLog(QStringLiteral("%1 dives loaded").arg(dive_table.nr));
struct dive *d;
for_each_dive (i, d) {
DiveListModel::instance()->addDive(d);
}
appendTextToLog(QStringLiteral("%1 dives loaded").arg(i));
if (dive_table.nr == 0) if (dive_table.nr == 0)
setStartPageText(tr("Cloud storage open successfully. No dives in dive list.")); setStartPageText(tr("Cloud storage open successfully. No dives in dive list."));
setLoadFromCloud(true); setLoadFromCloud(true);
@ -406,12 +398,8 @@ void QMLManager::loadDivesWithValidCredentials()
void QMLManager::refreshDiveList() void QMLManager::refreshDiveList()
{ {
int i;
struct dive *d;
DiveListModel::instance()->clear(); DiveListModel::instance()->clear();
for_each_dive (i, d) { DiveListModel::instance()->addAllDives();
DiveListModel::instance()->addDive(d);
}
} }
// update the dive and return the notes field, stripped of the HTML junk // update the dive and return the notes field, stripped of the HTML junk
@ -777,7 +765,9 @@ void QMLManager::undoDelete(int id)
deletedDive->tripflag = tripflag; deletedDive->tripflag = tripflag;
} }
record_dive(deletedDive); record_dive(deletedDive);
DiveListModel::instance()->addDive(deletedDive); QList<dive *>diveAsList;
diveAsList << deletedDive;
DiveListModel::instance()->addDive(diveAsList);
// make sure the changes get saved if the app is no longer in the foreground // make sure the changes get saved if the app is no longer in the foreground
// or if the user requests a save // or if the user requests a save
mark_divelist_changed(true); mark_divelist_changed(true);

View file

@ -31,13 +31,28 @@ DiveListModel::DiveListModel(QObject *parent) : QAbstractListModel(parent)
m_instance = this; m_instance = this;
} }
void DiveListModel::addDive(dive *d) void DiveListModel::addDive(QList<dive *>listOfDives)
{ {
beginInsertRows(QModelIndex(), rowCount(), rowCount()); if (listOfDives.isEmpty())
m_dives.append(new DiveObjectHelper(d)); return;
beginInsertRows(QModelIndex(), rowCount(), rowCount() + listOfDives.count() - 1);
foreach (dive *d, listOfDives) {
m_dives.append(new DiveObjectHelper(d));
}
endInsertRows(); endInsertRows();
} }
void DiveListModel::addAllDives()
{
QList<dive *>listOfDives;
int i;
struct dive *d;
for_each_dive (i, d)
listOfDives.append(d);
addDive(listOfDives);
}
void DiveListModel::insertDive(int i, DiveObjectHelper *newDive) void DiveListModel::insertDive(int i, DiveObjectHelper *newDive)
{ {
beginInsertRows(QModelIndex(), i, i); beginInsertRows(QModelIndex(), i, i);

View file

@ -30,7 +30,8 @@ public:
static DiveListModel *instance(); static DiveListModel *instance();
DiveListModel(QObject *parent = 0); DiveListModel(QObject *parent = 0);
void addDive(dive *d); void addDive(QList<dive *> listOfDives);
void addAllDives();
void insertDive(int i, DiveObjectHelper *newDive); void insertDive(int i, DiveObjectHelper *newDive);
void removeDive(int i); void removeDive(int i);
void removeDiveById(int id); void removeDiveById(int id);