2023-03-23 15:25:43 +00:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
|
|
|
|
#ifndef RECENTFILES_H
|
|
|
|
#define RECENTFILES_H
|
|
|
|
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QFile>
|
|
|
|
|
2023-06-07 14:11:25 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
2023-03-23 15:25:43 +00:00
|
|
|
class QSettings;
|
2023-06-07 14:11:25 +00:00
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
2023-07-03 08:49:46 +00:00
|
|
|
class RecentFiles : public QObject
|
2023-03-23 15:25:43 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
enum class RemoveReason {
|
2023-07-03 11:48:47 +00:00
|
|
|
Other,
|
2023-03-23 15:25:43 +00:00
|
|
|
Duplicate
|
|
|
|
};
|
|
|
|
Q_ENUM(RemoveReason)
|
|
|
|
|
2023-06-20 11:49:49 +00:00
|
|
|
using QObject::QObject;
|
2023-03-23 15:25:43 +00:00
|
|
|
|
|
|
|
// Access to QStringList member functions
|
2023-07-03 08:49:46 +00:00
|
|
|
const QStringList recentFiles() const {return m_files; }
|
|
|
|
bool isEmpty() const { return m_files.isEmpty(); }
|
2023-03-23 15:25:43 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
qsizetype maxFiles() const { return m_maxFiles; }
|
|
|
|
void setMaxFiles(qsizetype maxFiles) { m_maxFiles = maxFiles; }
|
|
|
|
|
|
|
|
public slots:
|
2023-05-09 20:29:35 +00:00
|
|
|
void addFile(const QString &fileName) { addFile(fileName, EmitPolicy::EmitWhenChanged); }
|
2023-03-23 15:25:43 +00:00
|
|
|
void addFiles(const QStringList &fileNames);
|
2023-07-03 08:49:46 +00:00
|
|
|
void removeFile(const QString &fileName) { removeFile(m_files.indexOf(fileName)); }
|
2023-07-03 11:48:47 +00:00
|
|
|
void removeFile(qsizetype index) {removeFile(index, RemoveReason::Other); }
|
2023-03-23 15:25:43 +00:00
|
|
|
void saveSettings(QSettings &settings, const QString &key) const;
|
|
|
|
bool restoreFromSettings(QSettings &settings, const QString &key);
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void countChanged(int count);
|
|
|
|
void changed();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Private removers with reason
|
|
|
|
void removeFile(qsizetype index, RemoveReason reason);
|
2023-07-03 08:49:46 +00:00
|
|
|
void removeFile(const QString &fileName, RemoveReason reason) {removeFile(m_files.indexOf(fileName), reason); }
|
2023-03-23 15:25:43 +00:00
|
|
|
|
|
|
|
// Private adder with emit policy
|
|
|
|
enum class EmitPolicy {
|
|
|
|
EmitWhenChanged,
|
|
|
|
NeverEmit
|
|
|
|
};
|
|
|
|
|
|
|
|
void addFile(const QString &fileName, EmitPolicy policy);
|
|
|
|
|
|
|
|
qsizetype m_maxFiles = 10;
|
|
|
|
|
2023-07-03 08:49:46 +00:00
|
|
|
QStringList m_files;
|
|
|
|
|
2023-03-23 15:25:43 +00:00
|
|
|
// Constexprs for settings
|
|
|
|
static constexpr QLatin1StringView s_maxFiles = QLatin1StringView("maxFiles");
|
|
|
|
static constexpr QLatin1StringView s_openMode = QLatin1StringView("openMode");
|
|
|
|
static constexpr QLatin1StringView s_fileNames = QLatin1StringView("fileNames");
|
|
|
|
static constexpr QLatin1StringView s_file = QLatin1StringView("file");
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // RECENTFILES_H
|