From f10552c711ffe335538edbc7ca00ddacf77195cd Mon Sep 17 00:00:00 2001 From: Herwig Birke Date: Thu, 21 Feb 2019 17:12:16 +0100 Subject: [PATCH] import --- cimportdialog.cpp | 47 ++++++++++++++++++----------------------------- cimportdialog.h | 44 +++++++++++++++++++++++--------------------- cmainwindow.cpp | 37 +++++++++++++++++++++++-------------- cmainwindow.h | 6 ++++++ common.cpp | 26 +++++++++++++++++++++++++- common.h | 10 ++++++++++ cpicture.cpp | 17 ++++++++++++++++- cpicture.h | 9 +++++++++ 8 files changed, 130 insertions(+), 66 deletions(-) diff --git a/cimportdialog.cpp b/cimportdialog.cpp index 0324e06..147b8c0 100644 --- a/cimportdialog.cpp +++ b/cimportdialog.cpp @@ -8,8 +8,6 @@ #include "common.h" -#include "cpicture.h" - #include "cdatetimepicker.h" #include @@ -28,13 +26,14 @@ #include -cImportDialog::cImportDialog(const QString& szRootPath, QWidget *parent) : +cImportDialog::cImportDialog(const QString& szRootPath, cPictureList& pictureList, QWidget *parent) : QDialog(parent), ui(new Ui::cImportDialog), m_szRootPath(szRootPath), + m_pictureList(pictureList), m_lpFolderViewModel(nullptr), m_lpThumbnailViewModel(nullptr), - m_lpThumbnailFilterProxyModel(nullptr), + m_lpThumbnailSortFilterProxyModel(nullptr), m_bLoading(false), m_bHasImported(false), m_lpRootItem(nullptr) @@ -47,7 +46,7 @@ cImportDialog::~cImportDialog() { delete m_lpThumbnailViewModel; delete m_lpFolderViewModel; - delete m_lpThumbnailFilterProxyModel; + delete m_lpThumbnailSortFilterProxyModel; delete ui; } @@ -62,9 +61,9 @@ void cImportDialog::initUI() ui->m_lpFolderView->setModel(m_lpFolderViewModel); m_lpThumbnailViewModel = new QStandardItemModel; - m_lpThumbnailFilterProxyModel = new cThumbnailSortFilterProxyModel(this); - ui->m_lpThumbnailView->setModel(m_lpThumbnailFilterProxyModel); - m_lpThumbnailFilterProxyModel->setSourceModel(m_lpThumbnailViewModel); + m_lpThumbnailSortFilterProxyModel = new cThumbnailSortFilterProxyModel(this); + ui->m_lpThumbnailView->setModel(m_lpThumbnailSortFilterProxyModel); + m_lpThumbnailSortFilterProxyModel->setSourceModel(m_lpThumbnailViewModel); QSettings settings; @@ -143,7 +142,7 @@ void cImportDialog::onThumbnailSelected(const QItemSelection& /*selection*/, con if(!index.isValid()) return; - cPicture* lpPicture = m_lpThumbnailFilterProxyModel->data(index, Qt::UserRole+1).value(); + cPicture* lpPicture = m_lpThumbnailSortFilterProxyModel->data(index, Qt::UserRole+1).value(); lpToolBoxInfo->setPicture(lpPicture); } @@ -156,7 +155,7 @@ void cImportDialog::onFolderSelected(const QItemSelection& /*selection*/, const if(!lpItem) return; - m_lpThumbnailFilterProxyModel->setFilterPath(lpItem->data(Qt::UserRole+2).toString()); + m_lpThumbnailSortFilterProxyModel->setFilterPath(lpItem->data(Qt::UserRole+2).toString()); } void cImportDialog::accept() @@ -220,20 +219,18 @@ void cImportDialog::onRead() void cImportDialog::onImport() { - QModelIndexList selected = ui->m_lpThumbnailView->selectionModel()->selectedRows(); QDir dir; QFile file; ui->m_lpProgress->setVisible(true); - ui->m_lpProgress->setRange(0, selected.count()); + ui->m_lpProgress->setRange(0, ui->m_lpThumbnailView->selectionModel()->selectedIndexes().count()); - for(int x = 0;x < selected.count();x++) + for(int x = 0;x < ui->m_lpThumbnailView->selectionModel()->selectedIndexes().count();x++) { - QStandardItem* lpItem = m_lpThumbnailViewModel->itemFromIndex(selected[x]); - - if(lpItem) + QModelIndex index = ui->m_lpThumbnailView->selectionModel()->selectedIndexes()[x]; + if(index.isValid()) { - cPicture* lpPicture = lpItem->data().value(); + cPicture* lpPicture = m_lpThumbnailSortFilterProxyModel->data(index, Qt::UserRole+1).value(); if(lpPicture) { @@ -251,26 +248,18 @@ void cImportDialog::onImport() szDestPath.append(lpPicture->cameraModel() + QDir::separator()); szDest.append(szDestPath); + szDest.append(lpPicture->fileName()); - if(dir.mkpath(szDest)) +// if(copyFile(szSource, szDest, ui->m_lpMove->isChecked())) + if(copyFile(szSource, szDest, false)) { - szDest.append(lpPicture->fileName()); - - if(file.exists(szDest)) - file.remove(szDest); - - file.copy(szSource, szDest); ui->m_lpProgress->setValue(x); qApp->processEvents(); lpPicture->setFilePath(szDestPath.left(szDestPath.length()-1).replace("\\", "/")); lpPicture->toDB(); - - if(ui->m_lpMove->isChecked()) - { -// file.remove(szSource); - } + m_pictureList.add(lpPicture); } } } diff --git a/cimportdialog.h b/cimportdialog.h index 604b43a..8d59907 100644 --- a/cimportdialog.h +++ b/cimportdialog.h @@ -12,6 +12,7 @@ #include #include +#include "cpicture.h" #include "cthumbnailsortfilterproxymodel.h" @@ -36,7 +37,7 @@ public: \param szRootPath \param parent */ - explicit cImportDialog(const QString& szRootPath, QWidget *parent = nullptr); + explicit cImportDialog(const QString& szRootPath, cPictureList& pictureList, QWidget *parent = nullptr); /*! \brief @@ -50,7 +51,7 @@ public: \fn hasImported \return bool */ - bool hasImported(); + bool hasImported(); private slots: /*! @@ -58,19 +59,19 @@ private slots: \fn onPathSelect */ - void onPathSelect(); + void onPathSelect(); /*! \brief \fn onRead */ - void onRead(); + void onRead(); /*! \brief \fn onImport */ - void onImport(); + void onImport(); /*! \brief @@ -78,7 +79,7 @@ private slots: \param selection \param previous */ - void onThumbnailSelected(const QItemSelection& selection, const QItemSelection& previous); + void onThumbnailSelected(const QItemSelection& selection, const QItemSelection& previous); /*! \brief @@ -86,30 +87,31 @@ private slots: \param selection \param previous */ - void onFolderSelected(const QItemSelection& selection, const QItemSelection& previous); + void onFolderSelected(const QItemSelection& selection, const QItemSelection& previous); private: - Ui::cImportDialog* ui; /*!< TODO: describe */ - QString m_szRootPath; /*!< TODO: describe */ - QStandardItemModel* m_lpFolderViewModel; /*!< TODO: describe */ - QStandardItemModel* m_lpThumbnailViewModel; /*!< TODO: describe */ - cThumbnailSortFilterProxyModel* m_lpThumbnailFilterProxyModel; /*!< TODO: describe */ - bool m_bLoading; /*!< TODO: describe */ - bool m_bHasImported; /*!< TODO: describe */ - QStandardItem* m_lpRootItem; /*!< TODO: describe */ + Ui::cImportDialog* ui; /*!< TODO: describe */ + QString m_szRootPath; /*!< TODO: describe */ + cPictureList& m_pictureList; /*!< TODO: describe */ + QStandardItemModel* m_lpFolderViewModel; /*!< TODO: describe */ + QStandardItemModel* m_lpThumbnailViewModel; /*!< TODO: describe */ + cThumbnailSortFilterProxyModel* m_lpThumbnailSortFilterProxyModel; /*!< TODO: describe */ + bool m_bLoading; /*!< TODO: describe */ + bool m_bHasImported; /*!< TODO: describe */ + QStandardItem* m_lpRootItem; /*!< TODO: describe */ /*! \brief \fn initUI */ - void initUI(); + void initUI(); /*! \brief \fn createActions */ - void createActions(); + void createActions(); /*! \brief @@ -118,27 +120,27 @@ private: \param szPath \param bRecursive */ - void readDirectory(const QString& szPath, bool bRecursive); + void readDirectory(const QString& szPath, bool bRecursive); /*! \brief \fn accept */ - void accept(); + void accept(); /*! \brief \fn reject */ - void reject(); + void reject(); /*! \brief \fn savePosition */ - void savePosition(); + void savePosition(); protected: }; diff --git a/cmainwindow.cpp b/cmainwindow.cpp index 5871b6c..d570e5d 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -124,14 +124,23 @@ void cMainWindow::loadData(bool bProgressBar) m_bLoading = true; m_pictureList.clear(); + m_pictureList.load(m_lpSplashScreen, bProgressBar ? m_lpProgressBar : nullptr); + + displayData(); + + m_bLoading = false; +} + +void cMainWindow::displayData() +{ m_lpThumbnailViewModel->clear(); m_lpFolderViewModel->clear(); + ui->m_lpStatusBar->showMessage(tr("refreshing...")); + qApp->processEvents(); m_lpRootItem = new QStandardItem("library"); m_lpFolderViewModel->appendRow(m_lpRootItem); - m_pictureList.load(m_lpSplashScreen, bProgressBar ? m_lpProgressBar : nullptr); - for(int x = 0;x < m_pictureList.count();x++) { QIcon icon; @@ -159,8 +168,7 @@ void cMainWindow::loadData(bool bProgressBar) } ui->m_lpFolderView->expandAll(); - - m_bLoading = false; + ui->m_lpStatusBar->showMessage(tr("done."), 3); } void cMainWindow::closeEvent(QCloseEvent *event) @@ -412,15 +420,11 @@ void cMainWindow::onFileOpen() void cMainWindow::onFileImport() { - cImportDialog importDialog(m_pictureLibrary.rootPath(), this); + cImportDialog importDialog(m_pictureLibrary.rootPath(), m_pictureList, this); importDialog.exec(); if(importDialog.hasImported()) - { - m_lpProgressBar->setVisible(true); - loadData(true); - m_lpProgressBar->setVisible(false); - } + displayData(); } void cMainWindow::onThumbnailViewContextMenu(const QPoint& pos) @@ -458,12 +462,17 @@ void cMainWindow::onChangeDate() lpPicture->setDateTime(dateTimePicker.dateTime()); QString szPath = QString::number(lpPicture->dateTime().date().year()) + "/" + lpPicture->dateTime().date().toString("yyyy-MM-dd"); - lpPicture->setFilePath(szPath); m_lpThumbnailSortFilterProxyModel->setData(index, QVariant::fromValue(szPath), Qt::UserRole+2); - insertPath(szPath, m_lpRootItem); - m_lpFolderSortFilterProxyModel->sort(0); + if(copyFile(m_pictureLibrary.rootPath() + QDir::separator() + lpPicture->filePath() + QDir::separator() + lpPicture->fileName(), + m_pictureLibrary.rootPath() + QDir::separator() + szPath + QDir::separator() + lpPicture->fileName(), true)) + { + lpPicture->setFilePath(szPath); + lpPicture->toDB(); + insertPath(szPath, m_lpRootItem); + m_lpFolderSortFilterProxyModel->sort(0); - m_lpThumbnailSortFilterProxyModel->invalidate(); + m_lpThumbnailSortFilterProxyModel->invalidate(); + } } } diff --git a/cmainwindow.h b/cmainwindow.h index c4c31ef..7f0814e 100644 --- a/cmainwindow.h +++ b/cmainwindow.h @@ -126,6 +126,12 @@ private: \param bProgressBar */ void loadData(bool bProgressBar = false); + /*! + \brief + + \fn displayData + */ + void displayData(); protected: /*! diff --git a/common.cpp b/common.cpp index a130283..4f035c6 100644 --- a/common.cpp +++ b/common.cpp @@ -41,7 +41,7 @@ QByteArray image2Blob(const QImage &image) QStandardItem* insertPath(QString szPath, QStandardItem* lpRootItem) { if(!lpRootItem) - return(0); + return(nullptr); QString szPath1 = szPath.replace("\\", "/"); QStringList szPathList = szPath1.split("/"); @@ -85,3 +85,27 @@ QStandardItem* insertPath(QString szPath, QStandardItem* lpRootItem) return(lpCurRoot); } + +bool copyFile(const QString& szSource, const QString& szDest, bool bDelete) +{ + QString szDestPath; + QString szDestFilePath = szDest; + QDir dir; + QFile file; + + szDestFilePath.replace("\\", "/"); + + szDestPath = szDestFilePath.left(szDestFilePath.lastIndexOf("/")); + + dir.mkpath(szDestPath); + + if(file.exists(szDestFilePath)) + file.remove(szDestFilePath); + + file.copy(szSource, szDestFilePath); + + if(bDelete) + file.remove(szSource); + + return(true); +} diff --git a/common.h b/common.h index 8ba20eb..916bad9 100644 --- a/common.h +++ b/common.h @@ -54,5 +54,15 @@ QByteArray image2Blob(const QImage& image); */ QStandardItem* insertPath(QString szPath, QStandardItem* lpRootItem); +/*! + \brief + + \fn copyFile + \param szSource + \param szDest + \param bDelete + \return bool +*/ +bool copyFile(const QString& szSource, const QString& szDest, bool bDelete = false); #endif // COMMON_H diff --git a/cpicture.cpp b/cpicture.cpp index 59954ad..a849b20 100644 --- a/cpicture.cpp +++ b/cpicture.cpp @@ -86,7 +86,7 @@ bool cPicture::toDB() if(m_iID != -1) { - query.prepare("SELECT id FROM file WHERE id=:id;"); + query.prepare("SELECT id FROM picture WHERE id=:id;"); query.bindValue(":id", m_iID); if(!query.exec()) { @@ -534,6 +534,21 @@ cPicture* cPictureList::add(qint32 iID, bool bNoCheck) return(lpNew); } +bool cPictureList::add(cPicture* lpPicture, bool bNoCheck) +{ + if(bNoCheck) + { + append(lpPicture); + return(true); + } + + if(contains(lpPicture)) + return(false); + + append(lpPicture); + return(true); +} + cPicture* cPictureList::find(qint32 iID) { for(int i = 0;i < count();i++) diff --git a/cpicture.h b/cpicture.h index 05f189a..0bf0aab 100644 --- a/cpicture.h +++ b/cpicture.h @@ -517,6 +517,15 @@ public: /*! \brief + \fn add + \param cPicture + \param bNoCheck + \return bool + */ + bool add(cPicture* lpPicture, bool bNoCheck = false); + /*! + \brief + \fn find \param iID \return cPicture