Add "Recent files ..." to Qt Quick GUI

This commit is contained in:
Martchus 2018-06-16 15:07:46 +02:00
parent a358346150
commit 932dc9cb59
4 changed files with 97 additions and 15 deletions

View File

@ -46,6 +46,11 @@ Kirigami.ApplicationWindow {
iconName: "document-open"
onTriggered: fileDialog.openExisting()
},
Kirigami.Action {
text: qsTr("Recently opened ...")
iconName: "document-open-recent"
children: createRecentFileActions()
},
Kirigami.Action {
text: "Save modifications"
enabled: nativeInterface.fileOpen
@ -110,11 +115,10 @@ Kirigami.ApplicationWindow {
if (fileUrls.length < 1) {
return
}
nativeInterface.filePath = fileUrls[0]
if (selectExisting) {
nativeInterface.load()
nativeInterface.load(fileUrls[0])
} else {
nativeInterface.create()
nativeInterface.create(fileUrls[0])
}
}
onRejected: {
@ -161,6 +165,15 @@ Kirigami.ApplicationWindow {
}
}
Component {
id: recentFileActionComponent
Kirigami.Action {
property string filePath
text: filePath.substring(filePath.lastIndexOf('/') + 1)
onTriggered: nativeInterface.load(filePath)
}
}
function clearStack() {
pageStack.pop(root.pageStack.initialPage, Controls.StackView.Immediate)
}
@ -174,4 +187,15 @@ Kirigami.ApplicationWindow {
rootIndex)
}))
}
function createRecentFileActions() {
var recentFiles = nativeInterface.recentFiles
var actions = []
for (var i = 0, count = recentFiles.length; i !== count; ++i) {
actions.push(recentFileActionComponent.createObject(root, {
filePath: recentFiles[i]
}))
}
return actions
}
}

View File

@ -11,6 +11,7 @@
#include <QDir>
#include <QFileInfo>
#include <QIcon>
#include <QSettings>
#include <QStringBuilder>
#include <stdexcept>
@ -22,24 +23,59 @@ using namespace Dialogs;
namespace QtGui {
Controller::Controller(const QString &filePath, QObject *parent)
Controller::Controller(QSettings &settings, const QString &filePath, QObject *parent)
: QObject(parent)
, m_settings(settings)
, m_fileOpen(false)
, m_fileModified(false)
{
setFilePath(filePath);
m_entryFilterModel.setSourceModel(&m_entryModel);
// share settings with main window
m_settings.beginGroup(QStringLiteral("mainwindow"));
m_recentFiles = m_settings.value(QStringLiteral("recententries")).toStringList();
// set initial file path
setFilePath(filePath);
}
void Controller::setFilePath(const QString &filePath)
{
if (m_filePath == filePath) {
// get rid of file:// prefix
QStringRef actualFilePath(&filePath);
if (filePath.startsWith(QLatin1String("file:"))) {
actualFilePath = filePath.midRef(5);
}
while (filePath.startsWith(QLatin1String("//"))) {
actualFilePath = actualFilePath.mid(1);
}
// skip if this path is already set
if (m_filePath == actualFilePath) {
return;
}
// assign full file path and file name
m_file.clear();
m_file.setPath(filePath.toLocal8Bit().data());
m_fileName = QString::fromLocal8Bit(IoUtilities::fileName(m_file.path()).data());
emit filePathChanged(m_filePath = filePath);
// handle recent files
auto index = m_recentFiles.indexOf(m_filePath);
if (!index) {
return;
}
if (index < 0) {
m_recentFiles.prepend(m_filePath);
} else if (index > 0) {
m_recentFiles[index].swap(m_recentFiles.first());
}
while (m_recentFiles.size() > 10) {
m_recentFiles.removeLast();
}
m_settings.setValue(QStringLiteral("recententries"), m_recentFiles);
emit recentFilesChanged(m_recentFiles);
}
void Controller::setPassword(const QString &password)
@ -51,8 +87,12 @@ void Controller::setPassword(const QString &password)
emit passwordChanged(m_password = password);
}
void Controller::load()
void Controller::load(const QString &filePath)
{
if (!filePath.isEmpty()) {
setFilePath(filePath);
}
resetFileStatus();
try {
m_file.load();
@ -72,8 +112,12 @@ void Controller::load()
}
}
void Controller::create()
void Controller::create(const QString &filePath)
{
if (!filePath.isEmpty()) {
setFilePath(filePath);
}
resetFileStatus();
try {
m_file.create();

View File

@ -10,6 +10,8 @@
#include <QObject>
#include <QPersistentModelIndex>
QT_FORWARD_DECLARE_CLASS(QSettings)
namespace QtGui {
class Controller : public QObject {
@ -26,9 +28,10 @@ class Controller : public QObject {
Q_PROPERTY(QString currentAccountName READ currentAccountName NOTIFY currentAccountChanged)
Q_PROPERTY(QList<QPersistentModelIndex> cutEntries READ cutEntries WRITE setCutEntries NOTIFY cutEntriesChanged)
Q_PROPERTY(bool canPaste READ canPaste NOTIFY cutEntriesChanged)
Q_PROPERTY(QStringList recentFiles READ recentFiles NOTIFY recentFilesChanged)
public:
explicit Controller(const QString &filePath = QString(), QObject *parent = nullptr);
explicit Controller(QSettings &settings, const QString &filePath = QString(), QObject *parent = nullptr);
const QString &filePath() const;
const QString &fileName() const;
@ -48,10 +51,11 @@ public:
Q_INVOKABLE void cutEntry(const QModelIndex &entryIndex);
Q_INVOKABLE QStringList pasteEntries(const QModelIndex &destinationParent, int row = -1);
bool canPaste() const;
const QStringList &recentFiles() const;
public slots:
void load();
void create();
void load(const QString &filePath = QString());
void create(const QString &filePath = QString());
void close();
void save();
@ -68,6 +72,7 @@ signals:
void fieldModelChanged();
void currentAccountChanged();
void cutEntriesChanged(const QList<QPersistentModelIndex> &cutEntries);
void recentFilesChanged(const QStringList &recentFiles);
private:
void resetFileStatus();
@ -75,6 +80,7 @@ private:
void setFileOpen(bool fileOpen);
void emitIoError(const QString &when);
QSettings &m_settings;
QString m_filePath;
QString m_fileName;
QString m_password;
@ -84,6 +90,7 @@ private:
EntryFilterModel m_entryFilterModel;
FieldModel m_fieldModel;
QList<QPersistentModelIndex> m_cutEntries;
QStringList m_recentFiles;
bool m_fileOpen;
bool m_fileModified;
};
@ -164,6 +171,11 @@ inline bool Controller::canPaste() const
return !m_cutEntries.isEmpty();
}
inline const QStringList &Controller::recentFiles() const
{
return m_recentFiles;
}
} // namespace QtGui
#endif // QT_QUICK_GUI_CONTROLLER_H

View File

@ -7,9 +7,9 @@
#include <qtutilities/resources/qtconfigarguments.h>
#include <qtutilities/resources/resources.h>
#include <QDebug>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QSettings>
#include <QTextCodec>
#include <QtQml>
@ -36,8 +36,10 @@ int runQuickGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs, c
qtConfigArgs.applySettings();
qtConfigArgs.applySettingsForQuickGui();
// load translations and enforce UTF-8 locale
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
// load settings from configuration file
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QStringLiteral(PROJECT_NAME));
// load translations
LOAD_QT_TRANSLATIONS;
// determine user paths
@ -52,7 +54,7 @@ int runQuickGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs, c
// init Quick GUI
QQmlApplicationEngine engine;
Controller controller(file);
Controller controller(settings, file);
QQmlContext *const context(engine.rootContext());
context->setContextProperty(QStringLiteral("userPaths"), userPaths);
context->setContextProperty(QStringLiteral("nativeInterface"), &controller);