OGS
RecentFiles Class Reference

Detailed Description

The RecentFiles class provides functionality to store information about recently used files (e.g. loaded or saved files). Example Usage:

RecentFiles* recentFiles = new RecentFiles(this, SLOT(openRecentFile()), "recentFileList", "OpenGeoSys");
connect(this, SIGNAL(fileUsed(QString)), recentFiles, SLOT(setCurrentFile(QString)));
menu_File->addMenu( recentFiles->menu() );
void setCurrentFile(const QString &filename)
Should be called from the application when a file was used.
QMenu * menu()
Returns the created menu. Add this menu to your QMainWindow menu.
RecentFiles(QObject *parent, const char *slot, QString settingsName)

with:

{
QAction* action = qobject_cast<QAction*>(sender());
if (action)
loadFile(action->data().toString());
}
void openRecentFile()

Definition at line 38 of file RecentFiles.h.

#include <RecentFiles.h>

Inheritance diagram for RecentFiles:
[legend]
Collaboration diagram for RecentFiles:
[legend]

Public Slots

void setCurrentFile (const QString &filename)
 Should be called from the application when a file was used.
 

Public Member Functions

 RecentFiles (QObject *parent, const char *slot, QString settingsName)
 
 ~RecentFiles () override
 
QMenu * menu ()
 Returns the created menu. Add this menu to your QMainWindow menu.
 

Private Types

enum  { _maxFiles = 5 }
 

Private Member Functions

void updateRecentFileActions ()
 Updates the recent files list and writes it to the settings.
 
QString strippedName (const QString &fullFileName)
 Returns the filename from a full file path.
 

Private Attributes

QMenu * _filesMenu
 
QString _currentFile
 
QString _settingsName
 
QAction * _fileActions [_maxFiles]
 

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private
Enumerator
_maxFiles 

Definition at line 71 of file RecentFiles.h.

71{ _maxFiles = 5 };

Constructor & Destructor Documentation

◆ RecentFiles()

RecentFiles::RecentFiles ( QObject * parent,
const char * slot,
QString settingsName )

Constructor. Example Usage:

RecentFiles recentFiles = new RecentFiles(this, SLOT(mySlot(QString)), "recentFileList");
Parameters
parentThe parent object. Normally the QMainWindow instance
slotA slot on parent which is called when a recent file is clicked. Use this with Qts SLOT() macro!
settingsNameThe setting key

Definition at line 22 of file RecentFiles.cpp.

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}
QAction * _fileActions[_maxFiles]
Definition RecentFiles.h:72
void updateRecentFileActions()
Updates the recent files list and writes it to the settings.
QString _settingsName
Definition RecentFiles.h:70
QMenu * _filesMenu
Definition RecentFiles.h:68

References _fileActions, _filesMenu, and updateRecentFileActions().

◆ ~RecentFiles()

RecentFiles::~RecentFiles ( )
override

Definition at line 37 of file RecentFiles.cpp.

38{
39 delete _filesMenu;
40}

References _filesMenu.

Member Function Documentation

◆ menu()

QMenu * RecentFiles::menu ( )

Returns the created menu. Add this menu to your QMainWindow menu.

Definition at line 42 of file RecentFiles.cpp.

43{
44 return _filesMenu;
45}

References _filesMenu.

Referenced by MainWindow::MainWindow().

◆ setCurrentFile

void RecentFiles::setCurrentFile ( const QString & filename)
slot

Should be called from the application when a file was used.

Definition at line 46 of file RecentFiles.cpp.

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}
QString _currentFile
Definition RecentFiles.h:69

References _currentFile, _maxFiles, _settingsName, and updateRecentFileActions().

◆ strippedName()

QString RecentFiles::strippedName ( const QString & fullFileName)
private

Returns the filename from a full file path.

Definition at line 84 of file RecentFiles.cpp.

85{
86 return QFileInfo(fullFileName).fileName();
87}

Referenced by updateRecentFileActions().

◆ updateRecentFileActions()

void RecentFiles::updateRecentFileActions ( )
private

Updates the recent files list and writes it to the settings.

Definition at line 63 of file RecentFiles.cpp.

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}
QString strippedName(const QString &fullFileName)
Returns the filename from a full file path.

References _fileActions, _maxFiles, _settingsName, and strippedName().

Referenced by RecentFiles(), and setCurrentFile().

Member Data Documentation

◆ _currentFile

QString RecentFiles::_currentFile
private

Definition at line 69 of file RecentFiles.h.

Referenced by setCurrentFile().

◆ _fileActions

QAction* RecentFiles::_fileActions[_maxFiles]
private

Definition at line 72 of file RecentFiles.h.

Referenced by RecentFiles(), and updateRecentFileActions().

◆ _filesMenu

QMenu* RecentFiles::_filesMenu
private

Definition at line 68 of file RecentFiles.h.

Referenced by RecentFiles(), ~RecentFiles(), and menu().

◆ _settingsName

QString RecentFiles::_settingsName
private

Definition at line 70 of file RecentFiles.h.

Referenced by setCurrentFile(), and updateRecentFileActions().


The documentation for this class was generated from the following files: