From 8ebd73115ff34430698454534d328660a228f521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Wed, 15 Nov 2023 01:24:39 +0100 Subject: [PATCH] Added "Open recent" menu --- .../actions/22x22/document-open-recent.svg | 4 + .../actions/22x22/document-open-recent.svg | 10 ++ src/GUI/gui.cpp | 118 ++++++++++++++++-- src/GUI/gui.h | 23 +++- src/GUI/icons.h | 2 + src/GUI/settings.cpp | 5 + src/GUI/settings.h | 6 + theme-color.qrc | 1 + theme-grayscale.qrc | 1 + 9 files changed, 162 insertions(+), 8 deletions(-) create mode 100644 icons/GUI/FlatColor/actions/22x22/document-open-recent.svg create mode 100644 icons/GUI/Papirus/actions/22x22/document-open-recent.svg diff --git a/icons/GUI/FlatColor/actions/22x22/document-open-recent.svg b/icons/GUI/FlatColor/actions/22x22/document-open-recent.svg new file mode 100644 index 00000000..648f5cad --- /dev/null +++ b/icons/GUI/FlatColor/actions/22x22/document-open-recent.svg @@ -0,0 +1,4 @@ + + + + diff --git a/icons/GUI/Papirus/actions/22x22/document-open-recent.svg b/icons/GUI/Papirus/actions/22x22/document-open-recent.svg new file mode 100644 index 00000000..01364f89 --- /dev/null +++ b/icons/GUI/Papirus/actions/22x22/document-open-recent.svg @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/src/GUI/gui.cpp b/src/GUI/gui.cpp index df4f640e..4149760d 100644 --- a/src/GUI/gui.cpp +++ b/src/GUI/gui.cpp @@ -59,12 +59,13 @@ #include "gui.h" +#define MAX_RECENT_FILES 10 #define TOOLBAR_ICON_SIZE 22 GUI::GUI() { QString activeMap; - QStringList disabledPOIs; + QStringList disabledPOIs, recentFiles; _poi = new POI(this); _dem = new DEMLoader(ProgramPaths::demDir(true), this); @@ -108,10 +109,13 @@ GUI::GUI() _movingTime = 0; _lastTab = 0; - readSettings(activeMap, disabledPOIs); + readSettings(activeMap, disabledPOIs, recentFiles); loadInitialMaps(activeMap); loadInitialPOIs(disabledPOIs); +#ifndef Q_OS_ANDROID + loadRecentFiles(recentFiles); +#endif // Q_OS_ANDROID updateGraphTabs(); updateStatusBarInfo(); @@ -261,6 +265,15 @@ void GUI::createActions() _statisticsAction->setActionGroup(_fileActionGroup); connect(_statisticsAction, &QAction::triggered, this, &GUI::statistics); addAction(_statisticsAction); +#ifndef Q_OS_ANDROID + _recentFilesActionGroup = new QActionGroup(this); + connect(_recentFilesActionGroup, &QActionGroup::triggered, this, + &GUI::recentFileSelected); + _clearRecentFilesAction = new QAction(tr("Clear list"), this); + _clearRecentFilesAction->setMenuRole(QAction::NoRole); + connect(_clearRecentFilesAction, &QAction::triggered, this, + &GUI::clearRecentFiles); +#endif // Q_OS_ANDROID // POI actions _poisActionGroup = new QActionGroup(this); @@ -614,6 +627,14 @@ void GUI::createMenus() { QMenu *fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(_openFileAction); +#ifndef Q_OS_ANDROID + _recentFilesMenu = fileMenu->addMenu(tr("Open recent")); + _recentFilesMenu->setIcon(QIcon::fromTheme(OPEN_RECENT_NAME, + QIcon(OPEN_RECENT_ICON))); + _recentFilesMenu->setEnabled(false); + _recentFilesEnd = _recentFilesMenu->addSeparator(); + _recentFilesMenu->addAction(_clearRecentFilesAction); +#endif // Q_OS_ANDROID fileMenu->addAction(_openDirAction); fileMenu->addSeparator(); #ifndef Q_OS_ANDROID @@ -1018,6 +1039,9 @@ bool GUI::openFile(const QString &fileName, bool tryUnknown, int &showError) updateNavigationActions(); updateStatusBarInfo(); updateWindowTitle(); +#ifndef Q_OS_ANDROID + updateRecentFiles(fileName); +#endif // Q_OS_ANDROID return true; } @@ -1992,6 +2016,48 @@ void GUI::updateWindowTitle() setWindowTitle(APP_NAME); } +#ifndef Q_OS_ANDROID +void GUI::updateRecentFiles(const QString &fileName) +{ + QAction *a = 0; + + QList actions(_recentFilesActionGroup->actions()); + for (int i = 0; i < actions.size(); i++) { + if (actions.at(i)->text() == fileName) { + a = actions.at(i); + break; + } + } + + if (a) + delete a; + else if (actions.size() == MAX_RECENT_FILES) + delete actions.last(); + + actions = _recentFilesActionGroup->actions(); + QAction *before = actions.size() ? actions.last() : _recentFilesEnd; + _recentFilesMenu->insertAction(before, + new QAction(fileName, _recentFilesActionGroup)); + _recentFilesMenu->setEnabled(true); +} + +void GUI::clearRecentFiles() +{ + QList actions(_recentFilesActionGroup->actions()); + + for (int i = 0; i < actions.size(); i++) + delete actions.at(i); + + _recentFilesMenu->setEnabled(false); +} + +void GUI::recentFileSelected(QAction *action) +{ + int showError = 1; + openFile(action->text(), true, showError); +} +#endif // Q_OS_ANDROID + void GUI::mapChanged(QAction *action) { _map = action->data().value(); @@ -2353,6 +2419,18 @@ void GUI::writeSettings() #endif // Q_OS_ANDROID settings.endGroup(); + /* File */ +#ifndef Q_OS_ANDROID + QList recentActions(_recentFilesActionGroup->actions()); + QStringList recent; + for (int i = 0; i < recentActions.size(); i++) + recent.append(recentActions.at(i)->text()); + + settings.beginGroup(SETTINGS_FILE); + WRITE(recentDataFiles, recent); + settings.endGroup(); +#endif // Q_OS_ANDROID + /* Map */ settings.beginGroup(SETTINGS_MAP); WRITE(activeMap, _map->name()); @@ -2372,11 +2450,11 @@ void GUI::writeSettings() settings.endGroup(); /* POI */ - QList actions(_poisActionGroup->actions()); + QList disabledActions(_poisActionGroup->actions()); QStringList disabled; - for (int i = 0; i < actions.size(); i++) - if (!actions.at(i)->isChecked()) - disabled.append(actions.at(i)->data().toString()); + for (int i = 0; i < disabledActions.size(); i++) + if (!disabledActions.at(i)->isChecked()) + disabled.append(disabledActions.at(i)->data().toString()); settings.beginGroup(SETTINGS_POI); WRITE(showPoi, _showPOIAction->isChecked()); @@ -2511,7 +2589,8 @@ void GUI::writeSettings() settings.endGroup(); } -void GUI::readSettings(QString &activeMap, QStringList &disabledPOIs) +void GUI::readSettings(QString &activeMap, QStringList &disabledPOIs, + QStringList &recentFiles) { #define READ(name) \ (Settings::name.read(settings)) @@ -2560,6 +2639,15 @@ void GUI::readSettings(QString &activeMap, QStringList &disabledPOIs) #endif // Q_OS_ANDROID settings.endGroup(); + /* File */ +#ifndef Q_OS_ANDROID + settings.beginGroup(SETTINGS_FILE); + recentFiles = READ(recentDataFiles); + settings.endGroup(); +#else // Q_OS_ANDROID + Q_UNUSED(recentFiles); +#endif // Q_OS_ANDROID + /* Map */ settings.beginGroup(SETTINGS_MAP); if (READ(showMap).toBool()) { @@ -3052,6 +3140,22 @@ void GUI::loadInitialPOIs(const QStringList &disabled) _unselectAllPOIAction->setEnabled(!poiActions.isEmpty()); } +#ifndef Q_OS_ANDROID +void GUI::loadRecentFiles(const QStringList &files) +{ + QAction *before = _recentFilesEnd; + + for (int i = 0; i < files.size(); i++) { + QAction *a = new QAction(files.at(i), _recentFilesActionGroup); + _recentFilesMenu->insertAction(before, a); + before = a; + } + + if (!files.isEmpty()) + _recentFilesMenu->setEnabled(true); +} +#endif // Q_OS_ANDROID + QAction *GUI::mapAction(const QString &name) { QList maps(_mapsActionGroup->actions()); diff --git a/src/GUI/gui.h b/src/GUI/gui.h index 90e5eb30..ba182ace 100644 --- a/src/GUI/gui.h +++ b/src/GUI/gui.h @@ -92,6 +92,10 @@ private slots: void poiFileChecked(QAction *action); void selectAllPOIs(); void unselectAllPOIs(); +#ifndef Q_OS_ANDROID + void recentFileSelected(QAction *action); + void clearRecentFiles(); +#endif // Q_OS_ANDROID void next(); void prev(); @@ -161,6 +165,9 @@ private: void updateWindowTitle(); bool updateGraphTabs(); void updateDEMDownloadAction(); +#ifndef Q_OS_ANDROID + void updateRecentFiles(const QString &fileName); +#endif // Q_OS_ANDROID TimeType timeType() const; Units units() const; @@ -174,10 +181,14 @@ private: qreal movingTime() const; QAction *mapAction(const QString &name); QGeoPositionInfoSource *positionSource(const Options &options); - void readSettings(QString &activeMap, QStringList &disabledPOIs); + void readSettings(QString &activeMap, QStringList &disabledPOIs, + QStringList &recentFiles); void loadInitialMaps(const QString &selected); void loadInitialPOIs(const QStringList &disabled); +#ifndef Q_OS_ANDROID + void loadRecentFiles(const QStringList &files); +#endif // Q_OS_ANDROID void loadOptions(); void updateOptions(const Options &options); @@ -198,11 +209,17 @@ private: #endif // Q_OS_ANDROID QMenu *_poiMenu; QMenu *_mapMenu; +#ifndef Q_OS_ANDROID + QMenu *_recentFilesMenu; +#endif // Q_OS_ANDROID QActionGroup *_fileActionGroup; QActionGroup *_navigationActionGroup; QActionGroup *_mapsActionGroup; QActionGroup *_poisActionGroup; +#ifndef Q_OS_ANDROID + QActionGroup *_recentFilesActionGroup; +#endif // Q_OS_ANDROID #if !defined(Q_OS_MAC) && !defined(Q_OS_ANDROID) QAction *_exitAction; #endif // Q_OS_MAC + Q_OS_ANDROID @@ -276,6 +293,10 @@ private: QAction *_showDEMTilesAction; QAction *_mapsEnd; QAction *_poisEnd; +#ifndef Q_OS_ANDROID + QAction *_clearRecentFilesAction; + QAction *_recentFilesEnd; +#endif // Q_OS_ANDROID QLabel *_fileNameLabel; QLabel *_distanceLabel; diff --git a/src/GUI/icons.h b/src/GUI/icons.h index c54e653c..3c4e902d 100644 --- a/src/GUI/icons.h +++ b/src/GUI/icons.h @@ -7,6 +7,7 @@ // Toolbar/menu icons #define OPEN_FILE_ICON ":/icons/" APP_NAME "/actions/22x22/document-open.svg" +#define OPEN_RECENT_ICON ":/icons/" APP_NAME "/actions/22x22/document-open-recent.svg" #define OPEN_DIR_ICON ":/icons/" APP_NAME "/actions/22x22/document-open-folder.svg" #define EXPORT_FILE_ICON ":/icons/" APP_NAME "/actions/22x22/document-export.svg" #define PRINT_FILE_ICON ":/icons/" APP_NAME "/actions/22x22/document-print.svg" @@ -40,6 +41,7 @@ // Desktop theme names #define OPEN_FILE_NAME "document-open" +#define OPEN_RECENT_NAME "document-open-recent" #define OPEN_DIR_NAME "document-open-folder" #define EXPORT_FILE_NAME "document-export" #define PRINT_FILE_NAME "document-print" diff --git a/src/GUI/settings.cpp b/src/GUI/settings.cpp index c63c4947..e835935f 100644 --- a/src/GUI/settings.cpp +++ b/src/GUI/settings.cpp @@ -268,3 +268,8 @@ const Settings::SettingMap Settings::positionPluginParameters const Settings::SettingList Settings::disabledPoiFiles = Settings::SettingList("disabled", "file"); + +#ifndef Q_OS_ANDROID +const Settings::SettingList Settings::recentDataFiles + = Settings::SettingList("recent", "file"); +#endif // Q_OS_ANDROID diff --git a/src/GUI/settings.h b/src/GUI/settings.h index b08a18eb..41a736c6 100644 --- a/src/GUI/settings.h +++ b/src/GUI/settings.h @@ -7,6 +7,7 @@ #define SETTINGS_WINDOW "Window" #define SETTINGS_SETTINGS "Settings" +#define SETTINGS_FILE "File" #define SETTINGS_MAP "Map" #define SETTINGS_GRAPH "Graph" #define SETTINGS_POI "POI" @@ -84,6 +85,11 @@ public: static const Setting showToolbars; #endif // Q_OS_ANDROID + /* File */ +#ifndef Q_OS_ANDROID + static const SettingList recentDataFiles; +#endif // Q_OS_ANDROID + /* Map */ static const Setting activeMap; static const Setting showMap; diff --git a/theme-color.qrc b/theme-color.qrc index 92f8c484..4ceba12c 100644 --- a/theme-color.qrc +++ b/theme-color.qrc @@ -1,6 +1,7 @@ icons/GUI/FlatColor/actions/22x22/document-open.svg + icons/GUI/FlatColor/actions/22x22/document-open-recent.svg icons/GUI/FlatColor/actions/22x22/document-open-folder.svg icons/GUI/FlatColor/actions/22x22/document-export.svg icons/GUI/FlatColor/actions/22x22/document-print.svg diff --git a/theme-grayscale.qrc b/theme-grayscale.qrc index ee883244..2181c115 100644 --- a/theme-grayscale.qrc +++ b/theme-grayscale.qrc @@ -1,6 +1,7 @@ icons/GUI/Papirus/actions/22x22/document-open.svg + icons/GUI/Papirus/actions/22x22/document-open-recent.svg icons/GUI/Papirus/actions/22x22/document-open-folder.svg icons/GUI/Papirus/actions/22x22/document-export.svg icons/GUI/Papirus/actions/22x22/document-print.svg