comparison base/RecentFiles.h @ 1704:452b48b29c2d single-point

Associate a label with each recent files entry, as well as the identifier (filename)
author Chris Cannam
date Mon, 13 May 2019 15:43:23 +0100
parents ad5f892c0c4d
children
comparison
equal deleted inserted replaced
1703:b17fb3a4560c 1704:452b48b29c2d
16 #ifndef SV_RECENT_FILES_H 16 #ifndef SV_RECENT_FILES_H
17 #define SV_RECENT_FILES_H 17 #define SV_RECENT_FILES_H
18 18
19 #include <QObject> 19 #include <QObject>
20 #include <QString> 20 #include <QString>
21 #include <QMutex>
21 #include <vector> 22 #include <vector>
22 #include <deque> 23 #include <deque>
23 24
24 /** 25 /**
25 * RecentFiles manages a list of the names of recently-used objects, 26 * RecentFiles manages a list of recently-used identifier strings,
26 * saving and restoring that list via QSettings. The names do not 27 * saving and restoring that list via QSettings. The identifiers do
27 * actually have to refer to files. 28 * not actually have to refer to files.
29 *
30 * Each entry must have a non-empty identifier, which is typically a
31 * filename, path, URI, or internal id, and may optionally also have a
32 * label, which is typically a user-visible convenience.
33 *
34 * RecentFiles is thread-safe - all access is serialised.
28 */ 35 */
29
30 class RecentFiles : public QObject 36 class RecentFiles : public QObject
31 { 37 {
32 Q_OBJECT 38 Q_OBJECT
33 39
34 public: 40 public:
35 /** 41 /**
36 * Construct a RecentFiles object that saves and restores in the 42 * Construct a RecentFiles object that saves and restores in the
37 * given QSettings group and truncates when the given count of 43 * given QSettings group and truncates when the given count of
38 * strings is reached. 44 * identifiers is reached.
39 */ 45 */
40 RecentFiles(QString settingsGroup = "RecentFiles", int maxCount = 10); 46 RecentFiles(QString settingsGroup = "RecentFiles",
47 int maxCount = 10);
41 48
42 virtual ~RecentFiles(); 49 virtual ~RecentFiles();
43 50
44 QString getSettingsGroup() const { return m_settingsGroup; } 51 /**
45 52 * Return the settingsGroup as passed to the constructor.
46 int getMaxCount() const { return m_maxCount; } 53 */
47 54 QString getSettingsGroup() const {
48 std::vector<QString> getRecent() const; 55 return m_settingsGroup;
56 }
49 57
50 /** 58 /**
51 * Add a name that should be treated as a literal string. 59 * Return the maxCount as passed to the constructor.
52 */ 60 */
53 void add(QString name); 61 int getMaxCount() const {
62 return m_maxCount;
63 }
64
65 /**
66 * Return the list of recent identifiers, without labels.
67 */
68 std::vector<QString> getRecentIdentifiers() const;
69
70 /**
71 * Return the list of recent identifiers, without labels. This is
72 * an alias for getRecentIdentifiers included for backward
73 * compatibility.
74 */
75 std::vector<QString> getRecent() const {
76 return getRecentIdentifiers();
77 }
78
79 /**
80 * Return the list of recent identifiers, with labels. Each
81 * returned entry is a pair of identifier and label in that order.
82 */
83 std::vector<std::pair<QString, QString>> getRecentEntries() const;
54 84
55 /** 85 /**
56 * Add a name that is known to be either a file path or a URL. If 86 * Add a literal identifier, optionally with a label.
57 * it looks like a URL, add it literally; otherwise treat it as a 87 *
58 * file path and canonicalise it appropriately. Also takes into 88 * If the identifier already exists in the recent entries list, it
59 * account the user preference for whether to include temporary 89 * is moved to the front of the list and its label is replaced
60 * files in the recent files menu: the file will not be added if 90 * with the given one.
61 * the preference is set and the file appears to be a temporary
62 * one.
63 */ 91 */
64 void addFile(QString name); 92 void add(QString identifier, QString label = "");
93
94 /**
95 * Add a name that is known to be either a file path or a URL,
96 * optionally with a label. If it looks like a URL, add it
97 * literally; otherwise treat it as a file path and canonicalise
98 * it appropriately. Also take into account the user preference
99 * for whether to include temporary files in the recent files
100 * menu: the file will not be added if the preference is set and
101 * the file appears to be a temporary one.
102 *
103 * If the identifier derived from the file path already exists in
104 * the recent entries list, it is moved to the front of the list
105 * and its label is replaced with the given one.
106 */
107 void addFile(QString filepath, QString label = "");
65 108
66 signals: 109 signals:
67 void recentChanged(); 110 void recentChanged();
68 111
69 protected: 112 private:
70 QString m_settingsGroup; 113 mutable QMutex m_mutex;
71 int m_maxCount;
72 114
73 std::deque<QString> m_names; 115 const QString m_settingsGroup;
116 const int m_maxCount;
117
118 std::deque<std::pair<QString, QString>> m_entries; // identifier, label
74 119
75 void read(); 120 void read();
76 void write(); 121 void write();
77 void truncateAndWrite(); 122 void truncateAndWrite();
78 }; 123 };