OGS
RecentFiles.cpp
Go to the documentation of this file.
1
15// ** INCLUDES **
16#include "RecentFiles.h"
17
18#include <QFileInfo>
19#include <QSettings>
20#include <utility>
21
22RecentFiles::RecentFiles(QObject* parent, const char* slot,
23 QString settingsName)
24 : QObject(parent), _settingsName(std::move(settingsName))
25{
26 _filesMenu = new QMenu(tr("Recent files"));
27 for (auto& fileAction : _fileActions)
28 {
29 fileAction = new QAction(this);
30 fileAction->setVisible(false);
31 connect(fileAction, SIGNAL(triggered()), parent, slot);
32 _filesMenu->addAction(fileAction);
33 }
35}
36
41
43{
44 return _filesMenu;
45}
46void RecentFiles::setCurrentFile(const QString& filename)
47{
48 _currentFile = filename;
49
50 QSettings settings;
51 QStringList files = settings.value(_settingsName).toStringList();
52 files.removeAll(filename);
53 files.prepend(filename);
54 while (files.size() > _maxFiles)
55 {
56 files.removeLast();
57 }
58
59 settings.setValue("recentFileList", files);
60
62}
64{
65 QSettings settings;
66 QStringList files = settings.value(_settingsName).toStringList();
67
68 int numFiles = qMin(files.size(), static_cast<int>(_maxFiles));
69
70 for (int i = 0; i < numFiles; ++i)
71 {
72 QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
73 _fileActions[i]->setText(text);
74 _fileActions[i]->setData(files[i]);
75 _fileActions[i]->setVisible(true);
76 }
77
78 for (int i = numFiles; i < _maxFiles; ++i)
79 {
80 _fileActions[i]->setVisible(false);
81 }
82}
83
84QString RecentFiles::strippedName(const QString& fullFileName)
85{
86 return QFileInfo(fullFileName).fileName();
87}
Definition of the RecentFiles class.
QAction * _fileActions[_maxFiles]
Definition RecentFiles.h:72
QString strippedName(const QString &fullFileName)
Returns the filename from a full file path.
void setCurrentFile(const QString &filename)
Should be called from the application when a file was used.
~RecentFiles() override
void updateRecentFileActions()
Updates the recent files list and writes it to the settings.
QMenu * menu()
Returns the created menu. Add this menu to your QMainWindow menu.
QString _currentFile
Definition RecentFiles.h:69
QString _settingsName
Definition RecentFiles.h:70
RecentFiles(QObject *parent, const char *slot, QString settingsName)
QMenu * _filesMenu
Definition RecentFiles.h:68