From 7c507182ab9bca02d917553a7b43a53236cb07e6 Mon Sep 17 00:00:00 2001 From: Herwig Birke Date: Tue, 15 May 2018 18:26:12 +0200 Subject: [PATCH] . --- cmainwindow.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++++++++- cmainwindow.h | 24 ++++++++++++ 2 files changed, 119 insertions(+), 2 deletions(-) diff --git a/cmainwindow.cpp b/cmainwindow.cpp index dc24631..cb78597 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -68,6 +68,7 @@ cMainWindow::cMainWindow(QWidget *parent) : QSettings settings; m_szOldPath = settings.value("file/lastPath", QDir::homePath()).toString(); + updateRecentFileActions(); } cMainWindow::~cMainWindow() @@ -262,6 +263,16 @@ void cMainWindow::createFileActions() lpAction->setPriority(QAction::LowPriority); m_lpFileMenu->addSeparator(); + for(int i = 0; i < MaxRecentFiles;i++) + { + m_lpActionRecentFile[i] = new QAction(this); + m_lpActionRecentFile[i]->setVisible(false); + m_lpFileMenu->addAction(m_lpActionRecentFile[i]); + connect(m_lpActionRecentFile[i], &QAction::triggered, this, &cMainWindow::openRecentFile); + } + m_lpSeparatorRecent = m_lpFileMenu->addSeparator(); + m_lpSeparatorRecent->setVisible(false); + lpAction = m_lpFileMenu->addAction(tr("&Quit"), this, &QWidget::close); lpAction->setShortcut(Qt::CTRL + Qt::Key_Q); } @@ -842,6 +853,8 @@ void cMainWindow::onShowPropertiesWindow() lpWidget1->setWindow(ui->m_lpMdiArea->addSubWindow(lpPropertiesWindow)); ui->m_lpMainTab->addTab((QWidget*)lpWidget1, lpPropertiesWindow->windowTitle()); + connect(lpPropertiesWindow, &cPropertiesWindow::subWindowClosed, this, &cMainWindow::onSubWindowClosed); + lpPropertiesWindow->show(); } @@ -870,7 +883,6 @@ void cMainWindow::onShowPartWindow(cPart* lpPart) ui->m_lpMainTab->addTab((QWidget*)lpWidget1, lpPartWindow->windowTitle()); connect(lpPartWindow, &cPartWindow::showChapterWindow, this, &cMainWindow::onShowChapterWindow); - connect(lpPartWindow, &cPartWindow::subWindowClosed, this, &cMainWindow::onSubWindowClosed); lpPartWindow->show(); @@ -901,7 +913,6 @@ void cMainWindow::onShowChapterWindow(cChapter* lpChapter) ui->m_lpMainTab->addTab((QWidget*)lpWidget1, lpChapterWindow->windowTitle()); connect(lpChapterWindow, &cChapterWindow::showSceneWindow, this, &cMainWindow::onShowSceneWindow); - connect(lpChapterWindow, &cChapterWindow::subWindowClosed, this, &cMainWindow::onSubWindowClosed); lpChapterWindow->show(); @@ -1257,6 +1268,7 @@ void cMainWindow::onFileOpen() m_lpStoryBook->fillObjectList(ui->m_lpObjectList); m_lpStoryBook->fillRechercheList(ui->m_lpRechercheList); + setCurrentFile(szProjectName); updateWindowTitle(); } @@ -1276,6 +1288,8 @@ bool cMainWindow::onFileSave() if(!m_lpStoryBook->saveAs(szProjectName)) return(false); + + setCurrentFile(szProjectName); } else m_lpStoryBook->save(); @@ -1300,6 +1314,7 @@ bool cMainWindow::onFileSaveAs() return(false); m_bSomethingChanged = false; + setCurrentFile(szProjectName); updateWindowTitle(); return(true); @@ -1882,3 +1897,81 @@ QString cMainWindow::getProjectSaveName(const QString& szFileName) return(szProjectName); } + +void cMainWindow::setCurrentFile(const QString& fileName) +{ + QSettings settings; + QStringList files = settings.value("file/recentFiles").toStringList(); + files.removeAll(fileName); + files.prepend(fileName); + while(files.size() > MaxRecentFiles) + files.removeLast(); + + settings.setValue("file/recentFiles", files); + + updateRecentFileActions(); +} + +void cMainWindow::updateRecentFileActions() +{ + QSettings settings; + QStringList files = settings.value("file/recentFiles").toStringList(); + + int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles); + + for(int i = 0; i < numRecentFiles; i++) + { + QString text = tr("&%1 %2").arg(i + 1).arg(QFileInfo(files[i]).fileName()); + m_lpActionRecentFile[i]->setText(text); + m_lpActionRecentFile[i]->setData(files[i]); + m_lpActionRecentFile[i]->setVisible(true); + } + + for(int j = numRecentFiles; j < MaxRecentFiles; j++) + m_lpActionRecentFile[j]->setVisible(false); + + m_lpSeparatorRecent->setVisible(numRecentFiles > 0); +} + +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; + } + } + } + + QString szProjectName = lpAction->data().toString(); + if(szProjectName.isEmpty()) + return; + + 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); + + updateWindowTitle(); + } +} diff --git a/cmainwindow.h b/cmainwindow.h index c9095ac..a00fb24 100644 --- a/cmainwindow.h +++ b/cmainwindow.h @@ -675,6 +675,11 @@ private: QAction* m_lpActionRechercheEdit; /*!< TODO: describe */ QAction* m_lpActionRechercheDelete; /*!< TODO: describe */ + QAction* m_lpSeparatorRecent; + + enum { MaxRecentFiles = 5 }; + QAction* m_lpActionRecentFile[MaxRecentFiles]; + QActionGroup* m_lpAlignGroup; /*!< TODO: describe */ QFontComboBox* m_lpComboFont; /*!< TODO: describe */ @@ -756,6 +761,25 @@ private: */ QString getProjectSaveName(const QString& szFileName = QString()); + /*! + \brief + + \fn setCurrentFile + */ + void setCurrentFile(const QString& szFileName); + + /*! + \brief + + \fn openRecentFile + */ + void openRecentFile(); + /*! + \brief + + \fn updateRecentFileActions + */ + void updateRecentFileActions(); protected: /*! \brief