Workspace 6.21.5
Public Member Functions | List of all members
RecentFilesModel Class Reference

Model for viewing and editing a set of views in list form. More...

#include <Workspace/Presentation/recentfilesmodel.h>

Inherits QAbstractListModel.

Public Member Functions

 RecentFilesModel (QSettings &settings, const QString &recentSettingsTagName, QObject *parent=nullptr)
 
 ~RecentFilesModel () override
 
void addFile (const QString &fileName)
 
void clear ()
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 
bool isEmpty () const
 
int maxEntries () const
 
bool removeFile (const QString &fileName)
 
void removeIfNotExist ()
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
void setMaxEntries (int m)
 

Detailed Description

The model is a list model, where each item in the list represents a recently opened file. Callers can add files to the list (addFile(), and it will automatically manage inserting files them in a 'most recently used' order. The recently opened files are stored in a QSettings object, under the specified tag name. This makes the model persist between sessions.

Callers can also clear the list, and remove files that don't exist from the list.

Constructor & Destructor Documentation

◆ RecentFilesModel()

RecentFilesModel ( QSettings &  settings,
const QString &  recentSettingsTagName,
QObject *  parent = nullptr 
)
Parameters
settingsThe settings in which the list of recently opened files are to be stored / retrieved from.
recentSettingsTagNameThe tag name of the item under which we're storing the list of recently opened files in the settings.
parentThe parent of this model.

◆ ~RecentFilesModel()

~RecentFilesModel ( )
override

Member Function Documentation

◆ addFile()

void addFile ( const QString &  fileName)
Parameters
fileNameThe fileName to add to the model.

Adds a new file name to the model, ensuring that it is at the top of the list. If the file name already exists in the list, it will be removed from its old location and re-inserted at the top of the list.

◆ clear()

void clear ( )

Clears the list, removing all files from it.

◆ data()

QVariant data ( const QModelIndex &  index,
int  role = Qt::DisplayRole 
) const
override
Parameters
indexThe index of the node in the model from which we want to retrieve data.
roleThe role we are going to be using the data for. In our case, we only accept Qt::DisplayRole.
Returns
a QVariant containing the index's data in the form of a string.

Retrieves the data from the model at the provided index / role as a QVariant. Since our data is an ordered list of strings, we will always be returning strings here.

◆ flags()

Qt::ItemFlags flags ( const QModelIndex &  index) const
override

◆ isEmpty()

bool isEmpty ( ) const
Returns
True if model has no recent files, false otherwise.

◆ maxEntries()

int maxEntries ( ) const

◆ removeFile()

bool removeFile ( const QString &  fileName)

Remove a specific file from the list

Parameters
fileNameThe file to remove
Returns
If the files was found in the list and removed.

◆ removeIfNotExist()

void removeIfNotExist ( )

Removes all files in the model that do not exist on disk.

◆ rowCount()

int rowCount ( const QModelIndex &  parent = QModelIndex()) const
override
Parameters
parentThe index of the model under which we're counting its child rows.

Returns the number of rows in the model under a given parent (since this is a list model, a number greater than zero will only be returned if parent is invalid, i.e. the root node).

◆ setMaxEntries()

void setMaxEntries ( int  m)