summaryrefslogtreecommitdiff
path: root/src/main-view/RecentFilesManager.cpp
diff options
context:
space:
mode:
authorJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
committerJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
commitd24f813f3f2a05c112e803e4256b53535895fc98 (patch)
tree601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/main-view/RecentFilesManager.cpp
Initial mirror commitHEADmaster
Diffstat (limited to 'src/main-view/RecentFilesManager.cpp')
-rw-r--r--src/main-view/RecentFilesManager.cpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/main-view/RecentFilesManager.cpp b/src/main-view/RecentFilesManager.cpp
new file mode 100644
index 0000000..d554eb4
--- /dev/null
+++ b/src/main-view/RecentFilesManager.cpp
@@ -0,0 +1,99 @@
+#include "RecentFilesManager.h"
+
+RecentFilesManager::RecentFilesManager(QObject* parent, QMenu* recentFilesMenu):
+ QObject(parent), recentFilesMenu(recentFilesMenu)
+{
+}
+
+void RecentFilesManager::createRecentFileActs(const QStringList& recentFiles)
+{
+ if(recentFiles.isEmpty())
+ {
+ recentFilesMenu->setEnabled(false);
+ return;
+ }
+ int recentFilesNum = recentFiles.size();
+ if(recentFilesNum > MaxRecentFiles)
+ recentFilesNum = MaxRecentFiles;
+ for(int i = 0; i < recentFilesNum; i++)
+ recentFilesMenu->addAction(createRecentFileAction(recentFiles[i]));
+ updateActionTexts();
+}
+
+QAction* RecentFilesManager::createRecentFileAction(const QString& filePath)
+{
+ QAction* action = new QAction(this);
+ action->setData(QDir::fromNativeSeparators(filePath));
+ connect(action, SIGNAL(triggered()), this, SLOT(triggerRecentFile()));
+ return action;
+}
+
+void RecentFilesManager::updateActionTexts()
+{
+ for(int i = 0; i < recentFilesMenu->actions().size(); i++)
+ {
+ QAction* action = recentFilesMenu->actions()[i];
+ QString filePath = getActionFile(action);
+ QString fileName = getShortFileName(filePath);
+ QString fileDir = QDir::toNativeSeparators(getShortDirPath(filePath));
+ QString text = QString("&%1 %2 (%3)").arg((i + 1) % 10)
+ .arg(fileName).arg(fileDir);
+ action->setText(text);
+ }
+}
+
+QString RecentFilesManager::getActionFile(QAction* action)
+{
+ return action->data().toString();
+}
+
+QString RecentFilesManager::getLastUsedFilePath() const
+{
+ if(recentFilesMenu->actions().isEmpty())
+ return QString();
+ return getActionFile(recentFilesMenu->actions().first());
+}
+
+QString RecentFilesManager::getShortDirPath(const QString& filePath) const
+{
+ QString path = QFileInfo(filePath).absolutePath();
+ QFontMetrics metrics(recentFilesMenu->font());
+ return metrics.elidedText(path, Qt::ElideMiddle, MaxPathLength);
+}
+
+void RecentFilesManager::triggerRecentFile()
+{
+ QAction *action = qobject_cast<QAction*>(sender());
+ if(action)
+ emit recentFileTriggered(getActionFile(action));
+}
+
+void RecentFilesManager::addFile( const QString& filePath)
+{
+ addRecentFileAction(filePath);
+ updateActionTexts();
+ emit addedFile();
+}
+
+void RecentFilesManager::addRecentFileAction(const QString& filePath)
+{
+ recentFilesMenu->setEnabled(true);
+ foreach(QAction* action, recentFilesMenu->actions())
+ if(action->data() == filePath)
+ recentFilesMenu->removeAction(action);
+ QAction* newAction = createRecentFileAction(filePath);
+ if(!recentFilesMenu->actions().isEmpty())
+ recentFilesMenu->insertAction(recentFilesMenu->actions().first(), newAction);
+ else
+ recentFilesMenu->addAction(newAction);
+ if(recentFilesMenu->actions().size() > MaxRecentFiles)
+ recentFilesMenu->removeAction(recentFilesMenu->actions().last());
+}
+
+QStringList RecentFilesManager::getFiles() const
+{
+ QStringList list;
+ foreach(QAction* action, recentFilesMenu->actions())
+ list << getActionFile(action);
+ return list;
+}