From d9b79fa9770a3e19afd62a27827bc1ad0bd1f169 Mon Sep 17 00:00:00 2001 From: Herwig Birke Date: Tue, 12 Mar 2019 16:06:42 +0100 Subject: [PATCH] open file, recent file --- cmainwindow.cpp | 242 +++++++++++++++++++++---------------------- cmainwindow.h | 10 ++ cpicturelibrary.cpp | 13 +-- cpicturelibrary.h | 2 +- ctoolboxlocation.cpp | 5 + ctoolboxlocation.h | 6 ++ ctoolboxperson.cpp | 5 + ctoolboxperson.h | 6 ++ ctoolboxtag.cpp | 5 + ctoolboxtag.h | 6 ++ 10 files changed, 168 insertions(+), 132 deletions(-) diff --git a/cmainwindow.cpp b/cmainwindow.cpp index f8ea31e..5a760dd 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -30,6 +30,7 @@ #include #include #include +#include cMainWindow::cMainWindow(cSplashScreen* lpSplashScreen, QWidget *parent) : @@ -48,7 +49,12 @@ cMainWindow::cMainWindow(cSplashScreen* lpSplashScreen, QWidget *parent) : { initUI(); createActions(); - loadData(); + +// onFileNew(); + QSettings settings; + m_szOldPath = settings.value("file/lastPath", QDir::homePath()).toString(); + m_pictureLibrary.openDatabase(m_szOldPath); + loadData(true); updateRecentFileActions(); } @@ -67,6 +73,8 @@ void cMainWindow::initUI() { ui->setupUi(this); + QIcon::setThemeName("TangoMFK"); + m_lpFolderViewModel = new QStandardItemModel; m_lpFolderSortFilterProxyModel = new cFolderSortFilterProxyModel(this); ui->m_lpFolderView->setModel(m_lpFolderSortFilterProxyModel); @@ -145,21 +153,77 @@ void cMainWindow::createContextActions() connect(m_lpUnsetHDRAction, &QAction::triggered, this, &cMainWindow::onUnsetHDR); } +void cMainWindow::closeEvent(QCloseEvent *event) +{ + QSettings settings; + settings.setValue("main/width", QVariant::fromValue(size().width())); + settings.setValue("main/height", QVariant::fromValue(size().height())); + settings.setValue("main/x", QVariant::fromValue(x())); + settings.setValue("main/y", QVariant::fromValue(y())); + if(this->isMaximized()) + settings.setValue("main/maximized", QVariant::fromValue(true)); + else + settings.setValue("main/maximized", QVariant::fromValue(false)); + + QList sizes = ui->m_lpSplitter->sizes(); + + for(int x = 0;x < sizes.count();x++) + settings.setValue(QString("main/splitter%1").arg(x+1), QVariant::fromValue(sizes[x])); + + event->accept(); +} + +void cMainWindow::createFileActions() +{ + m_lpFileMenu = menuBar()->addMenu(tr("&File")); + m_lpFileToolBar = addToolBar(tr("File Actions")); + + const QIcon newIcon = QIcon::fromTheme("document-new"); + m_lpFileNewAction = m_lpFileMenu->addAction(newIcon, tr("&New"), this, &cMainWindow::onFileNew); + m_lpFileToolBar->addAction(m_lpFileNewAction); + m_lpFileNewAction->setPriority(QAction::LowPriority); + m_lpFileNewAction->setShortcut(QKeySequence::New); + + const QIcon openIcon = QIcon::fromTheme("document-open"); + m_lpFileOpenAction = m_lpFileMenu->addAction(openIcon, tr("&Open..."), this, &cMainWindow::onFileOpen); + m_lpFileOpenAction->setShortcut(QKeySequence::Open); + m_lpFileToolBar->addAction(m_lpFileOpenAction); + + m_lpFileSaveAsAction = m_lpFileMenu->addAction(tr("Save &As..."), this, &cMainWindow::onFileSaveAs); + m_lpFileSaveAsAction->setPriority(QAction::LowPriority); + m_lpFileMenu->addSeparator(); + + m_lpFileImportAction = m_lpFileMenu->addAction(tr("&Import..."), this, &cMainWindow::onFileImport); + m_lpFileToolBar->addAction(m_lpFileImportAction); + + m_lpFileMenu->addSeparator(); + + for(int i = 0; i < MaxRecentFiles;i++) + { + m_lpRecentFileAction[i] = new QAction(this); + m_lpRecentFileAction[i]->setVisible(false); + m_lpFileMenu->addAction(m_lpRecentFileAction[i]); + connect(m_lpRecentFileAction[i], &QAction::triggered, this, &cMainWindow::openRecentFile); + } + m_lpSeparatorRecent = m_lpFileMenu->addSeparator(); + m_lpSeparatorRecent->setVisible(false); + + m_lpFileQuitAction = m_lpFileMenu->addAction(tr("&Quit"), this, &QWidget::close); + m_lpFileQuitAction->setShortcut(Qt::CTRL + Qt::Key_Q); +} + void cMainWindow::loadData(bool bProgressBar) { m_bLoading = true; m_personList.clear(); m_personList.load(m_lpSplashScreen, bProgressBar ? m_lpProgressBar : nullptr); - ui->m_lpToolBoxPerson->setPersonList(&m_personList); m_locationList.clear(); m_locationList.load(m_lpSplashScreen, bProgressBar ? m_lpProgressBar : nullptr); - ui->m_lpToolBoxLocation->setLocationList(&m_locationList); m_tagList.clear(); m_tagList.load(m_lpSplashScreen, bProgressBar ? m_lpProgressBar : nullptr); - ui->m_lpToolBoxTags->setTagList(&m_tagList); m_pictureList.clear(); m_pictureList.load(m_personList, m_locationList, m_tagList, m_lpSplashScreen, bProgressBar ? m_lpProgressBar : nullptr); @@ -173,9 +237,17 @@ void cMainWindow::displayData() { m_lpThumbnailViewModel->clear(); m_lpFolderViewModel->clear(); + ui->m_lpToolBoxPerson->clearPersonList(); + ui->m_lpToolBoxLocation->clearLocationList(); + ui->m_lpToolBoxTags->clearTagList(); + ui->m_lpStatusBar->showMessage(tr("refreshing...")); qApp->processEvents(); + ui->m_lpToolBoxPerson->setPersonList(&m_personList); + ui->m_lpToolBoxLocation->setLocationList(&m_locationList); + ui->m_lpToolBoxTags->setTagList(&m_tagList); + m_lpRootItem = new QStandardItem("library"); m_lpFolderViewModel->appendRow(m_lpRootItem); @@ -298,63 +370,6 @@ void cMainWindow::cleanFolderTree(const QString& folder) } } -void cMainWindow::closeEvent(QCloseEvent *event) -{ - QSettings settings; - settings.setValue("main/width", QVariant::fromValue(size().width())); - settings.setValue("main/height", QVariant::fromValue(size().height())); - settings.setValue("main/x", QVariant::fromValue(x())); - settings.setValue("main/y", QVariant::fromValue(y())); - if(this->isMaximized()) - settings.setValue("main/maximized", QVariant::fromValue(true)); - else - settings.setValue("main/maximized", QVariant::fromValue(false)); - - QList sizes = ui->m_lpSplitter->sizes(); - - for(int x = 0;x < sizes.count();x++) - settings.setValue(QString("main/splitter%1").arg(x+1), QVariant::fromValue(sizes[x])); - - event->accept(); -} - -void cMainWindow::createFileActions() -{ - m_lpFileMenu = menuBar()->addMenu(tr("&File")); - m_lpFileToolBar = addToolBar(tr("File Actions")); - - const QIcon newIcon = QIcon::fromTheme("document-new"); - m_lpFileNewAction = m_lpFileMenu->addAction(newIcon, tr("&New"), this, &cMainWindow::onFileNew); - m_lpFileToolBar->addAction(m_lpFileNewAction); - m_lpFileNewAction->setPriority(QAction::LowPriority); - m_lpFileNewAction->setShortcut(QKeySequence::New); - - const QIcon openIcon = QIcon::fromTheme("document-open"); - m_lpFileOpenAction = m_lpFileMenu->addAction(openIcon, tr("&Open..."), this, &cMainWindow::onFileOpen); - m_lpFileOpenAction->setShortcut(QKeySequence::Open); - m_lpFileToolBar->addAction(m_lpFileOpenAction); - - m_lpFileMenu->addSeparator(); - - m_lpFileImportAction = m_lpFileMenu->addAction(tr("&Import..."), this, &cMainWindow::onFileImport); - m_lpFileToolBar->addAction(m_lpFileImportAction); - - m_lpFileMenu->addSeparator(); - - for(int i = 0; i < MaxRecentFiles;i++) - { - m_lpRecentFileAction[i] = new QAction(this); - m_lpRecentFileAction[i]->setVisible(false); - m_lpFileMenu->addAction(m_lpRecentFileAction[i]); - connect(m_lpRecentFileAction[i], &QAction::triggered, this, &cMainWindow::openRecentFile); - } - m_lpSeparatorRecent = m_lpFileMenu->addSeparator(); - m_lpSeparatorRecent->setVisible(false); - - m_lpFileQuitAction = m_lpFileMenu->addAction(tr("&Quit"), this, &QWidget::close); - m_lpFileQuitAction->setShortcut(Qt::CTRL + Qt::Key_Q); -} - void cMainWindow::onThumbnailSelected(const QItemSelection& /*selection*/, const QItemSelection& /*previous*/) { cPictureList pictureList; @@ -449,42 +464,21 @@ void cMainWindow::openRecentFile() QAction* lpAction = qobject_cast(sender()); if(lpAction) { -// if(m_lpStoryBook) -// { -// if(m_bSomethingChanged) -// { -// switch(QMessageBox::question(this, tr("Save"), m_lpStoryBook->title() + tr(" has been changed.\nDo you want to save?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel)) -// { -// case QMessageBox::Yes: -// if(!onFileSave()) -// return; -// break; -// case QMessageBox::No: -// break; -// case QMessageBox::Cancel: -// return; -// default: -// return; -// } -// } -// } + QSettings settings; + QString szPath = lpAction->data().toString(); + if(szPath.isEmpty()) + return; -// QString szProjectName = lpAction->data().toString(); -// if(szProjectName.isEmpty()) -// return; + if(!m_pictureLibrary.openDatabase(szPath)) + return; -// delete m_lpStoryBook; + loadData(true); -// m_lpStoryBook = new cStoryBook(szProjectName); + setCurrentFile(szPath); + m_szOldPath = szPath; + settings.setValue("file/lastPath", QVariant::fromValue(m_szOldPath)); -// m_lpStoryBook->fillOutlineList(ui->m_lpOutlineList); -// m_lpStoryBook->fillCharacterList(ui->m_lpCharacterList); -// m_lpStoryBook->fillPlaceList(ui->m_lpPlaceList); -// m_lpStoryBook->fillObjectList(ui->m_lpObjectList); -// m_lpStoryBook->fillRechercheList(ui->m_lpRechercheList); - -// setCurrentFile(szProjectName); -// updateWindowTitle(); + // updateWindowTitle(); } } @@ -525,45 +519,43 @@ void cMainWindow::onFileNew() void cMainWindow::onFileOpen() { -// if(m_lpStoryBook) -// { -// if(m_bSomethingChanged) -// { -// switch(QMessageBox::question(this, tr("Save"), m_lpStoryBook->title() + tr(" has been changed.\nDo you want to save?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel)) -// { -// case QMessageBox::Yes: -// if(!onFileSave()) -// return; -// break; -// case QMessageBox::No: -// break; -// case QMessageBox::Cancel: -// return; -// default: -// return; -// } -// } -// } + QSettings settings; + QString szPath = settings.value("file/lastPath", QDir::homePath()).toString(); + szPath = QFileDialog::getExistingDirectory(this, tr("Open Database"), szPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); -// QString szProjectName = getProjectLoadName(); + if(szPath.isEmpty()) + return; + + if(!m_pictureLibrary.openDatabase(szPath)) + return; + + loadData(m_lpProgressBar); + + setCurrentFile(szPath); + m_szOldPath = szPath; + settings.setValue("file/lastPath", QVariant::fromValue(m_szOldPath)); + +// updateWindowTitle(); +} + +bool cMainWindow::onFileSaveAs() +{ +// QString szProjectName = getProjectSaveName(); // if(szProjectName.isEmpty()) -// return; +// return(false); -// ui->m_lpMdiArea->closeAllSubWindows(); +// QFile file(szProjectName); +// if(file.exists()) +// file.remove(); -// if(m_lpStoryBook) -// delete m_lpStoryBook; - -// m_lpStoryBook = new cStoryBook(szProjectName); - -// m_lpStoryBook->fillOutlineList(ui->m_lpOutlineList); -// m_lpStoryBook->fillCharacterList(ui->m_lpCharacterList); -// m_lpStoryBook->fillPlaceList(ui->m_lpPlaceList); -// m_lpStoryBook->fillObjectList(ui->m_lpObjectList); -// m_lpStoryBook->fillRechercheList(ui->m_lpRechercheList); +// if(!m_lpStoryBook->saveAs(szProjectName)) +// return(false); +// m_bSomethingChanged = false; // setCurrentFile(szProjectName); // updateWindowTitle(); + + return(true); } void cMainWindow::onFileImport() diff --git a/cmainwindow.h b/cmainwindow.h index c211003..2ab1084 100644 --- a/cmainwindow.h +++ b/cmainwindow.h @@ -80,12 +80,15 @@ private: cLocationList m_locationList; /*!< TODO: describe */ cTagList m_tagList; /*!< TODO: describe */ + QString m_szOldPath; /*!< TODO: describe */ + QMenu* m_lpFileMenu; /*!< TODO: describe */ QToolBar* m_lpFileToolBar; /*!< TODO: describe */ QAction* m_lpFileNewAction; /*!< TODO: describe */ QAction* m_lpFileOpenAction; /*!< TODO: describe */ + QAction* m_lpFileSaveAsAction; /*!< TODO: describe */ QAction* m_lpFileImportAction; /*!< TODO: describe */ QAction* m_lpFileQuitAction; /*!< TODO: describe */ @@ -215,6 +218,13 @@ private slots: /*! \brief + \fn onFileSaveAs + \return bool + */ + bool onFileSaveAs(); + /*! + \brief + \fn onFileImport */ void onFileImport(); diff --git a/cpicturelibrary.cpp b/cpicturelibrary.cpp index 555189f..fa88790 100644 --- a/cpicturelibrary.cpp +++ b/cpicturelibrary.cpp @@ -16,16 +16,15 @@ cPictureLibrary::cPictureLibrary(QObject *parent) : QObject(parent) { - if(!openDatabase()) - return; } -bool cPictureLibrary::openDatabase() +bool cPictureLibrary::openDatabase(const QString& szPath) { - QSettings settings; - m_szRootPath = settings.value("database/rootPath", QDir::homePath()).toString(); + if(m_db.isOpen()) + m_db.close(); - QString szDB = m_szRootPath + "/" + "pictureLibrary.db"; + m_szRootPath = ""; + QString szDB = szPath + "/" + "pictureLibrary.db"; m_db = QSqlDatabase::addDatabase("QSQLITE"); m_db.setHostName("localhost"); @@ -56,6 +55,8 @@ bool cPictureLibrary::openDatabase() query.finish(); } + m_szRootPath = szPath; + return(true); } diff --git a/cpicturelibrary.h b/cpicturelibrary.h index 37601ad..24699fd 100644 --- a/cpicturelibrary.h +++ b/cpicturelibrary.h @@ -35,7 +35,7 @@ public: \fn openDatabase \return bool */ - bool openDatabase(); + bool openDatabase(const QString& szPath); /*! \brief diff --git a/ctoolboxlocation.cpp b/ctoolboxlocation.cpp index e6e439c..f92c359 100644 --- a/ctoolboxlocation.cpp +++ b/ctoolboxlocation.cpp @@ -59,6 +59,11 @@ void cToolBoxLocation::createActions() connect(m_lpLocationListModel, &QStandardItemModel::itemChanged, this, &cToolBoxLocation::onLocationChanged); } +void cToolBoxLocation::clearLocationList() +{ + m_lpLocationListModel->clear(); +} + void cToolBoxLocation::setLocationList(cLocationList* lpLocationList) { m_bLoading = true; diff --git a/ctoolboxlocation.h b/ctoolboxlocation.h index b7aa51c..6223310 100644 --- a/ctoolboxlocation.h +++ b/ctoolboxlocation.h @@ -43,6 +43,12 @@ public: */ ~cToolBoxLocation(); + /*! + \brief + + \fn clearLocationList + */ + void clearLocationList(); /*! \brief diff --git a/ctoolboxperson.cpp b/ctoolboxperson.cpp index add981a..0cb6ecf 100644 --- a/ctoolboxperson.cpp +++ b/ctoolboxperson.cpp @@ -64,6 +64,11 @@ void cToolBoxPerson::createActions() connect(m_lpPersonListModel, &QStandardItemModel::itemChanged, this, &cToolBoxPerson::onPersonChanged); } +void cToolBoxPerson::clearPersonList() +{ + m_lpPersonListModel->clear(); +} + void cToolBoxPerson::setPersonList(cPersonList* lpPersonList) { m_bLoading = true; diff --git a/ctoolboxperson.h b/ctoolboxperson.h index 5c35750..eb5a08b 100644 --- a/ctoolboxperson.h +++ b/ctoolboxperson.h @@ -43,6 +43,12 @@ public: */ ~cToolBoxPerson(); + /*! + \brief + + \fn clearPersonList + */ + void clearPersonList(); /*! \brief diff --git a/ctoolboxtag.cpp b/ctoolboxtag.cpp index 9b2a52f..453ba91 100644 --- a/ctoolboxtag.cpp +++ b/ctoolboxtag.cpp @@ -64,6 +64,11 @@ void cToolBoxTag::createActions() connect(m_lpTagListModel, &QStandardItemModel::itemChanged, this, &cToolBoxTag::onTagChanged); } +void cToolBoxTag::clearTagList() +{ + m_lpTagListModel->clear(); +} + void cToolBoxTag::setTagList(cTagList* lpTagList) { m_bLoading = true; diff --git a/ctoolboxtag.h b/ctoolboxtag.h index d5be515..06485ba 100644 --- a/ctoolboxtag.h +++ b/ctoolboxtag.h @@ -43,6 +43,12 @@ public: */ ~cToolBoxTag(); + /*! + \brief + + \fn clearTagList + */ + void clearTagList(); /*! \brief