From 2f0d6d4a7df7c8508a4f831818e41c11cffdd513 Mon Sep 17 00:00:00 2001 From: Aleks Kissinger Date: Mon, 24 Dec 2018 23:35:08 +0100 Subject: added Open Recent (closes #40) --- src/data/tikzdocument.cpp | 44 ++++++++++++++++++++++++++++++++------------ src/data/tikzdocument.h | 3 +++ src/gui/mainmenu.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/gui/mainmenu.h | 3 +++ src/gui/mainmenu.ui | 11 +++++++++++ src/tikzit.cpp | 42 ++++++++++++++++++++++++++++++++++-------- src/tikzit.h | 2 ++ 7 files changed, 122 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/data/tikzdocument.cpp b/src/data/tikzdocument.cpp index 863f1fd..b89da10 100644 --- a/src/data/tikzdocument.cpp +++ b/src/data/tikzdocument.cpp @@ -69,6 +69,8 @@ void TikzDocument::open(QString fileName) return; } + addToRecentFiles(); + QTextStream in(&file); _tikz = in.readAll(); file.close(); @@ -98,10 +100,10 @@ bool TikzDocument::save() { return saveAs(); } else { MainWindow *win = tikzit->activeWindow(); - if (win != 0 && !win->tikzScene()->enabled()) { + if (win != nullptr && !win->tikzScene()->enabled()) { win->tikzScene()->parseTikz(win->tikzSource()); if (!win->tikzScene()->enabled()) { - auto resp = QMessageBox::question(0, + auto resp = QMessageBox::question(nullptr, tr("Tikz failed to parse"), tr("Cannot save file with invalid TiKZ source. Revert changes and save?")); if (resp == QMessageBox::Yes) win->tikzScene()->setEnabled(true); @@ -123,7 +125,8 @@ bool TikzDocument::save() { setClean(); return true; } else { - QMessageBox::warning(0, "Save Failed", "Could not open file: '" + _fileName + "' for writing."); + QMessageBox::warning(nullptr, + "Save Failed", "Could not open file: '" + _fileName + "' for writing."); } } @@ -140,6 +143,29 @@ void TikzDocument::setClean() _undoStack->setClean(); } +QString TikzDocument::fileName() const +{ + return _fileName; +} + +void TikzDocument::addToRecentFiles() +{ + QSettings settings("tikzit", "tikzit"); + if (!_fileName.isEmpty()) { + QStringList recentFiles = settings.value("recent-files").toStringList(); + + // if the file is in the list already, shift it to the top. Otherwise, add it. + recentFiles.removeAll(_fileName); + recentFiles.prepend(_fileName); + + // keep max 10 files + while (recentFiles.size() > 10) recentFiles.removeLast(); + + settings.setValue("recent-files", recentFiles); + tikzit->updateRecentFiles(); + } +} + void TikzDocument::setGraph(Graph *graph) { _graph = graph; @@ -148,10 +174,10 @@ void TikzDocument::setGraph(Graph *graph) bool TikzDocument::saveAs() { MainWindow *win = tikzit->activeWindow(); - if (win != 0 && !win->tikzScene()->enabled()) { + if (win != nullptr && !win->tikzScene()->enabled()) { win->tikzScene()->parseTikz(win->tikzSource()); if (!win->tikzScene()->enabled()) { - auto resp = QMessageBox::question(0, + auto resp = QMessageBox::question(nullptr, tr("Tikz failed to parse"), tr("Cannot save file with invalid TiKZ source. Revert changes and save?")); if (resp == QMessageBox::Yes) win->tikzScene()->setEnabled(true); @@ -170,19 +196,13 @@ bool TikzDocument::saveAs() { dialog.setDirectory(settings.value("previous-file-path").toString()); dialog.setOption(QFileDialog::DontUseNativeDialog); -// QString fileName = QFileDialog::getSaveFileName(tikzit->activeWindow(), -// tr("Save File As"), -// settings.value("previous-file-path").toString(), -// tr("TiKZ Files (*.tikz)"), -// nullptr, -// QFileDialog::DontUseNativeDialog); - if (dialog.exec() && !dialog.selectedFiles().isEmpty()) { QString fileName = dialog.selectedFiles()[0]; _fileName = fileName; if (save()) { // clean state might not change, so update title bar manually tikzit->activeWindow()->updateFileName(); + addToRecentFiles(); return true; } } diff --git a/src/data/tikzdocument.h b/src/data/tikzdocument.h index a5f3534..3b5990a 100644 --- a/src/data/tikzdocument.h +++ b/src/data/tikzdocument.h @@ -52,6 +52,8 @@ public: bool isClean() const; void setClean(); + QString fileName() const; + private: Graph *_graph; QString _tikz; @@ -59,6 +61,7 @@ private: QString _shortName; QUndoStack *_undoStack; bool _parseSuccess; + void addToRecentFiles(); signals: diff --git a/src/gui/mainmenu.cpp b/src/gui/mainmenu.cpp index 6f6ab00..d4372ed 100644 --- a/src/gui/mainmenu.cpp +++ b/src/gui/mainmenu.cpp @@ -33,6 +33,8 @@ MainMenu::MainMenu() ui.actionCheck_for_updates_automatically->setChecked(settings.value("check-for-updates").toBool()); ui.actionCheck_for_updates_automatically->blockSignals(false); } + + updateRecentFiles(); } void MainMenu::addDocks(QMenu *m) @@ -48,6 +50,32 @@ QAction *MainMenu::updatesAction() return ui.actionCheck_for_updates_automatically; } +void MainMenu::updateRecentFiles() +{ + QSettings settings("tikzit", "tikzit"); + ui.menuOpen_Recent->clear(); + + QStringList recentFiles = settings.value("recent-files").toStringList(); + //qDebug() << "update:" << recentFiles; + + QAction *action; + foreach (QString f, recentFiles) { + QFileInfo fi(f); + action = new QAction(fi.fileName(), ui.menuOpen_Recent); + action->setData(f); + ui.menuOpen_Recent->addAction(action); + connect(action, SIGNAL(triggered()), + this, SLOT(openRecent())); + } + + ui.menuOpen_Recent->addSeparator(); + action = new QAction("Clear List", ui.menuOpen_Recent); + connect(action, SIGNAL(triggered()), + tikzit, SLOT(clearRecentFiles())); + ui.menuOpen_Recent->addAction(action); + ui.menuOpen_Recent->repaint(); +} + // File void MainMenu::on_actionNew_triggered() { @@ -82,6 +110,15 @@ void MainMenu::on_actionExit_triggered() tikzit->quit(); } +void MainMenu::openRecent() +{ + if (sender() != nullptr) { + if (QAction *action = dynamic_cast(sender())) { + tikzit->open(action->data().toString()); + } + } +} + // Edit void MainMenu::on_actionUndo_triggered() diff --git a/src/gui/mainmenu.h b/src/gui/mainmenu.h index e1477b4..8acef49 100644 --- a/src/gui/mainmenu.h +++ b/src/gui/mainmenu.h @@ -30,6 +30,7 @@ public: MainMenu(); void addDocks(QMenu *m); QAction *updatesAction(); + void updateRecentFiles(); private: Ui::MainMenu ui; @@ -43,6 +44,8 @@ public slots: void on_actionSave_As_triggered(); void on_actionExit_triggered(); + void openRecent(); + // Edit void on_actionUndo_triggered(); void on_actionRedo_triggered(); diff --git a/src/gui/mainmenu.ui b/src/gui/mainmenu.ui index 58a2ff0..097430c 100644 --- a/src/gui/mainmenu.ui +++ b/src/gui/mainmenu.ui @@ -14,8 +14,14 @@ File + + + Open Recent + + + @@ -344,6 +350,11 @@ Ctrl+R + + + Clear Menu + + diff --git a/src/tikzit.cpp b/src/tikzit.cpp index e12053b..e81706c 100644 --- a/src/tikzit.cpp +++ b/src/tikzit.cpp @@ -276,16 +276,28 @@ void Tikzit::open(QString fileName) if (!fileName.isEmpty()) { if (_windows.size() == 1 && _windows[0]->tikzDocument()->isClean() && - _windows[0]->tikzDocument()->shortName().isEmpty()) - { + _windows[0]->tikzDocument()->shortName().isEmpty()) + { _windows[0]->open(fileName); _windows[0]->show(); - } - else { - MainWindow *w = new MainWindow(); - w->show(); - w->open(fileName); - _windows << w; + } + else + { + bool found = false; + foreach (MainWindow *w, _windows) { + if (w->tikzDocument()->fileName() == fileName) { + w->raise(); + w->activateWindow(); + found = true; + } + } + + if (!found) { + MainWindow *w = new MainWindow(); + _windows << w; + w->show(); + w->open(fileName); + } } } } @@ -354,6 +366,20 @@ QString Tikzit::styleFilePath() const return _styleFilePath; } +void Tikzit::updateRecentFiles() +{ + foreach (MainWindow *w, _windows) { + w->menu()->updateRecentFiles(); + } +} + +void Tikzit::clearRecentFiles() +{ + QSettings settings("tikzit", "tikzit"); + settings.setValue("recent-files", QStringList()); + updateRecentFiles(); +} + void Tikzit::setCheckForUpdates(bool check) { QSettings settings("tikzit", "tikzit"); diff --git a/src/tikzit.h b/src/tikzit.h index 9011cc3..24bf56b 100644 --- a/src/tikzit.h +++ b/src/tikzit.h @@ -134,8 +134,10 @@ public: //StylePalette *stylePalette() const; QString styleFilePath() const; + void updateRecentFiles(); public slots: + void clearRecentFiles(); void setCheckForUpdates(bool check); void checkForUpdates(bool manual); void updateAuto(QNetworkReply *reply); -- cgit v1.2.3