mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
core: replace divesite_table_t by a vector of std::unique_ptr<>s
This is a long commit, because it introduces a new abstraction: a general std::vector<> of std::unique_ptrs<>. Moreover, it replaces a number of pointers by C++ references, when the callee does not suppoert null objects. This simplifies memory management and makes ownership more explicit. It is a proof-of-concept and a test-bed for the other core data structrures. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
411188728d
commit
e39dea3d68
41 changed files with 451 additions and 426 deletions
|
@ -10,20 +10,18 @@
|
|||
|
||||
#include <QShortcut>
|
||||
|
||||
// Caller keeps ownership of "imported". The contents of "imported" will be consumed on execution of the dialog.
|
||||
// On return, it will be empty.
|
||||
DivesiteImportDialog::DivesiteImportDialog(struct dive_site_table &imported, QString source, QWidget *parent) : QDialog(parent),
|
||||
importedSource(std::move(source))
|
||||
DivesiteImportDialog::DivesiteImportDialog(dive_site_table imported, QString source, QWidget *parent) : QDialog(parent),
|
||||
importedSites(std::move(imported)),
|
||||
importedSource(std::move(source)),
|
||||
divesiteImportedModel(std::make_unique<DivesiteImportedModel>())
|
||||
{
|
||||
QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_W), this);
|
||||
QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q), this);
|
||||
|
||||
divesiteImportedModel = new DivesiteImportedModel(this);
|
||||
|
||||
int startingWidth = defaultModelFont().pointSize();
|
||||
|
||||
ui.setupUi(this);
|
||||
ui.importedDivesitesView->setModel(divesiteImportedModel);
|
||||
ui.importedDivesitesView->setModel(divesiteImportedModel.get());
|
||||
ui.importedDivesitesView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
ui.importedDivesitesView->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
ui.importedDivesitesView->horizontalHeader()->setStretchLastSection(true);
|
||||
|
@ -35,45 +33,36 @@ DivesiteImportDialog::DivesiteImportDialog(struct dive_site_table &imported, QSt
|
|||
ui.selectAllButton->setEnabled(true);
|
||||
ui.unselectAllButton->setEnabled(true);
|
||||
|
||||
connect(ui.importedDivesitesView, &QTableView::clicked, divesiteImportedModel, &DivesiteImportedModel::changeSelected);
|
||||
connect(ui.selectAllButton, &QPushButton::clicked, divesiteImportedModel, &DivesiteImportedModel::selectAll);
|
||||
connect(ui.unselectAllButton, &QPushButton::clicked, divesiteImportedModel, &DivesiteImportedModel::selectNone);
|
||||
connect(ui.importedDivesitesView, &QTableView::clicked, divesiteImportedModel.get(), &DivesiteImportedModel::changeSelected);
|
||||
connect(ui.selectAllButton, &QPushButton::clicked, divesiteImportedModel.get(), &DivesiteImportedModel::selectAll);
|
||||
connect(ui.unselectAllButton, &QPushButton::clicked, divesiteImportedModel.get(), &DivesiteImportedModel::selectNone);
|
||||
connect(close, SIGNAL(activated()), this, SLOT(close()));
|
||||
connect(quit, SIGNAL(activated()), parent, SLOT(close()));
|
||||
|
||||
ui.ok->setEnabled(true);
|
||||
|
||||
importedSites = imported;
|
||||
imported.nr = imported.allocated = 0;
|
||||
imported.dive_sites = nullptr;
|
||||
|
||||
divesiteImportedModel->repopulate(&importedSites);
|
||||
}
|
||||
|
||||
DivesiteImportDialog::~DivesiteImportDialog()
|
||||
{
|
||||
clear_dive_site_table(&importedSites);
|
||||
}
|
||||
|
||||
void DivesiteImportDialog::on_cancel_clicked()
|
||||
{
|
||||
clear_dive_site_table(&importedSites);
|
||||
done(-1);
|
||||
}
|
||||
|
||||
void DivesiteImportDialog::on_ok_clicked()
|
||||
{
|
||||
// delete non-selected dive sites
|
||||
struct dive_site_table selectedSites = empty_dive_site_table;
|
||||
for (int i = 0; i < importedSites.nr; i++)
|
||||
if (divesiteImportedModel->data(divesiteImportedModel->index(i, 0), Qt::CheckStateRole) == Qt::Checked) {
|
||||
struct dive_site *newSite = new dive_site;
|
||||
*newSite = *importedSites.dive_sites[i];
|
||||
add_dive_site_to_table(newSite, &selectedSites);
|
||||
}
|
||||
dive_site_table selectedSites;
|
||||
for (size_t i = 0; i < importedSites.size(); i++) {
|
||||
if (divesiteImportedModel->data(divesiteImportedModel->index(i, 0), Qt::CheckStateRole) == Qt::Checked)
|
||||
selectedSites.push_back(std::move(importedSites[i]));
|
||||
}
|
||||
importedSites.clear(); // Hopefully, the model is not used thereafter!
|
||||
|
||||
Command::importDiveSites(&selectedSites, importedSource);
|
||||
clear_dive_site_table(&selectedSites);
|
||||
clear_dive_site_table(&importedSites);
|
||||
Command::importDiveSites(std::move(selectedSites), importedSource);
|
||||
accept();
|
||||
}
|
||||
|
|
|
@ -21,7 +21,8 @@ class DivesiteImportedModel;
|
|||
class DivesiteImportDialog : public QDialog {
|
||||
Q_OBJECT
|
||||
public:
|
||||
DivesiteImportDialog(struct dive_site_table &imported, QString source, QWidget *parent = 0 );
|
||||
// Note: takes ownership of importedd table
|
||||
DivesiteImportDialog(dive_site_table imported, QString source, QWidget *parent = 0);
|
||||
~DivesiteImportDialog();
|
||||
|
||||
public
|
||||
|
@ -31,10 +32,10 @@ slots:
|
|||
|
||||
private:
|
||||
Ui::DivesiteImportDialog ui;
|
||||
struct dive_site_table importedSites;
|
||||
dive_site_table importedSites;
|
||||
QString importedSource;
|
||||
|
||||
DivesiteImportedModel *divesiteImportedModel;
|
||||
std::unique_ptr<DivesiteImportedModel> divesiteImportedModel;
|
||||
};
|
||||
|
||||
#endif // DIVESITEIMPORTDIALOG_H
|
||||
|
|
|
@ -97,7 +97,7 @@ void DiveSiteListView::diveSiteAdded(struct dive_site *, int idx)
|
|||
|
||||
void DiveSiteListView::diveSiteChanged(struct dive_site *ds, int field)
|
||||
{
|
||||
int idx = get_divesite_idx(ds, divelog.sites);
|
||||
int idx = get_divesite_idx(ds, *divelog.sites);
|
||||
if (idx < 0)
|
||||
return;
|
||||
QModelIndex globalIdx = LocationInformationModel::instance()->index(idx, field);
|
||||
|
|
|
@ -388,9 +388,9 @@ bool DiveLocationFilterProxyModel::lessThan(const QModelIndex &source_left, cons
|
|||
// If there is a current location, sort by that - otherwise use the provided column
|
||||
if (has_location(¤tLocation)) {
|
||||
// The dive sites are -2 because of the first two items.
|
||||
struct dive_site *ds1 = get_dive_site(source_left.row() - 2, divelog.sites);
|
||||
struct dive_site *ds2 = get_dive_site(source_right.row() - 2, divelog.sites);
|
||||
return get_distance(&ds1->location, ¤tLocation) < get_distance(&ds2->location, ¤tLocation);
|
||||
auto loc1 = (*divelog.sites)[source_left.row() - 2]->location;
|
||||
auto loc2 = (*divelog.sites)[source_right.row() - 2]->location;
|
||||
return get_distance(&loc1, ¤tLocation) < get_distance(&loc2, ¤tLocation);
|
||||
}
|
||||
return source_left.data().toString().compare(source_right.data().toString(), Qt::CaseInsensitive) < 0;
|
||||
}
|
||||
|
@ -411,6 +411,9 @@ QVariant DiveLocationModel::data(const QModelIndex &index, int role) const
|
|||
static const QIcon plusIcon(":list-add-icon");
|
||||
static const QIcon geoCode(":geotag-icon");
|
||||
|
||||
if (index.row() < 0 || index.row() >= (int)divelog.sites->size() + 2)
|
||||
return QVariant();
|
||||
|
||||
if (index.row() <= 1) { // two special cases.
|
||||
if (index.column() == LocationInformationModel::DIVESITE)
|
||||
return QVariant::fromValue<dive_site *>(RECENTLY_ADDED_DIVESITE);
|
||||
|
@ -428,8 +431,8 @@ QVariant DiveLocationModel::data(const QModelIndex &index, int role) const
|
|||
}
|
||||
|
||||
// The dive sites are -2 because of the first two items.
|
||||
struct dive_site *ds = get_dive_site(index.row() - 2, divelog.sites);
|
||||
return LocationInformationModel::getDiveSiteData(ds, index.column(), role);
|
||||
const auto &ds = (*divelog.sites)[index.row() - 2];
|
||||
return LocationInformationModel::getDiveSiteData(*ds, index.column(), role);
|
||||
}
|
||||
|
||||
int DiveLocationModel::columnCount(const QModelIndex&) const
|
||||
|
@ -439,7 +442,7 @@ int DiveLocationModel::columnCount(const QModelIndex&) const
|
|||
|
||||
int DiveLocationModel::rowCount(const QModelIndex&) const
|
||||
{
|
||||
return divelog.sites->nr + 2;
|
||||
return (int)divelog.sites->size() + 2;
|
||||
}
|
||||
|
||||
Qt::ItemFlags DiveLocationModel::flags(const QModelIndex &index) const
|
||||
|
@ -560,12 +563,10 @@ void DiveLocationLineEdit::refreshDiveSiteCache()
|
|||
|
||||
static struct dive_site *get_dive_site_name_start_which_str(const QString &str)
|
||||
{
|
||||
struct dive_site *ds;
|
||||
int i;
|
||||
for_each_dive_site (i, ds, divelog.sites) {
|
||||
for (const auto &ds: *divelog.sites) {
|
||||
QString dsName = QString::fromStdString(ds->name);
|
||||
if (dsName.toLower().startsWith(str.toLower()))
|
||||
return ds;
|
||||
return ds.get();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -1409,12 +1409,12 @@ void MainWindow::on_actionImportDiveSites_triggered()
|
|||
parse_file(fileNamePtr.data(), &log);
|
||||
}
|
||||
// The imported dive sites still have pointers to imported dives - remove them
|
||||
for (int i = 0; i < log.sites->nr; ++i)
|
||||
log.sites->dive_sites[i]->dives.clear();
|
||||
for (const auto &ds: *log.sites)
|
||||
ds->dives.clear();
|
||||
|
||||
QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files");
|
||||
|
||||
DivesiteImportDialog divesiteImport(*log.sites, source, this);
|
||||
DivesiteImportDialog divesiteImport(std::move(*log.sites), source, this);
|
||||
divesiteImport.exec();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue