From 1fc4fba69fed8607d9b961086a88bcf362a482a5 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Tue, 4 Apr 2017 19:21:30 +0200 Subject: Break down MainTab into smaller classes Maintab is one of our most complex classes, and it's something I'm not actually proud of. But it currently works and the idea of splitting it was in my head for quite a while. This is the third or fourth tentative of splitting it, and this time I let the most complex part of it untouched, the Notes and Equipment tab are way too complex to untangle right now on my limited time. A new class 'TabBase' should be used for any new tab that we may create, and added on the MainTab (see the new lines on the MainTab constructor). Also, Extra Info, Information, Photos and Statistics where ported to this new way helping reduce the number of lines and functions on the MainTab quite a bit. Overall this is a step in the right direction for the future. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- desktop-widgets/tab-widgets/TabDivePhotos.cpp | 98 +++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 desktop-widgets/tab-widgets/TabDivePhotos.cpp (limited to 'desktop-widgets/tab-widgets/TabDivePhotos.cpp') diff --git a/desktop-widgets/tab-widgets/TabDivePhotos.cpp b/desktop-widgets/tab-widgets/TabDivePhotos.cpp new file mode 100644 index 000000000..7f8b3a8b0 --- /dev/null +++ b/desktop-widgets/tab-widgets/TabDivePhotos.cpp @@ -0,0 +1,98 @@ +#include "TabDivePhotos.h" +#include "ui_TabDivePhotos.h" + +#include + +#include +#include +#include +#include +#include + +//TODO: Remove those in the future. +#include "../mainwindow.h" +#include "../divelistview.h" + +TabDivePhotos::TabDivePhotos(QWidget *parent) + : TabBase(parent), + ui(new Ui::TabDivePhotos()), + divePictureModel(DivePictureModel::instance()) +{ + ui->setupUi(this); + ui->photosView->setModel(divePictureModel); + ui->photosView->setSelectionMode(QAbstractItemView::MultiSelection); + + connect(ui->photosView, &DivePictureWidget::photoDoubleClicked, + [](const QString& path) { + QDesktopServices::openUrl(QUrl::fromLocalFile(path)); + } + ); +} + +TabDivePhotos::~TabDivePhotos() +{ + delete ui; +} + +void TabDivePhotos::clear() +{ + updateData(); +} + +void TabDivePhotos::contextMenuEvent(QContextMenuEvent *event) +{ + QMenu popup(this); + popup.addAction(tr("Load image(s) from file(s)"), this, &TabDivePhotos::addPhotosFromFile); + popup.addAction(tr("Load image(s) from web"), this, &TabDivePhotos::addPhotosFromURL); + popup.addSeparator(); + popup.addAction(tr("Delete selected images"), this, &TabDivePhotos::removeSelectedPhotos); + popup.addAction(tr("Delete all images"), this, &TabDivePhotos::removeAllPhotos); + popup.exec(event->globalPos()); + event->accept(); +} + +void TabDivePhotos::removeSelectedPhotos() +{ + bool last = false; + if (!ui->photosView->selectionModel()->hasSelection()) + return; + QModelIndexList indexes = ui->photosView->selectionModel()->selectedRows(); + if (indexes.count() == 0) + indexes = ui->photosView->selectionModel()->selectedIndexes(); + QModelIndex photo = indexes.first(); + do { + photo = indexes.first(); + last = indexes.count() == 1; + if (photo.isValid()) { + QString fileUrl = photo.data(Qt::DisplayPropertyRole).toString(); + if (fileUrl.length() > 0) + DivePictureModel::instance()->removePicture(fileUrl, last); + } + indexes.removeFirst(); + } while(!indexes.isEmpty()); +} + +//TODO: This looks overly wrong. We shouldn't call MainWindow to retrieve the DiveList to add Images. +void TabDivePhotos::addPhotosFromFile() +{ + MainWindow::instance()->dive_list()->loadImages(); +} + +void TabDivePhotos::addPhotosFromURL() +{ + MainWindow::instance()->dive_list()->loadWebImages(); +} + +void TabDivePhotos::removeAllPhotos() +{ + if (QMessageBox::warning(this, tr("Deleting Images"), tr("Are you sure you want to delete all images?"), QMessageBox::Cancel | QMessageBox::Ok, QMessageBox::Cancel) != QMessageBox::Cancel ) { + ui->photosView->selectAll(); + removeSelectedPhotos(); + } +} + +void TabDivePhotos::updateData() +{ + divePictureModel->updateDivePictures(); +} + -- cgit v1.2.3-70-g09d2