refactored "recently opened files menu"
This commit is contained in:
parent
06a5a418bc
commit
fc13d3d6a9
|
@ -18,6 +18,7 @@
|
||||||
#include <qtutilities/enterpassworddialog/enterpassworddialog.h>
|
#include <qtutilities/enterpassworddialog/enterpassworddialog.h>
|
||||||
#include <qtutilities/misc/dialogutils.h>
|
#include <qtutilities/misc/dialogutils.h>
|
||||||
#include <qtutilities/misc/desktoputils.h>
|
#include <qtutilities/misc/desktoputils.h>
|
||||||
|
#include <qtutilities/misc/recentmenumanager.h>
|
||||||
|
|
||||||
#include <c++utilities/io/path.h>
|
#include <c++utilities/io/path.h>
|
||||||
#include <c++utilities/conversion/stringconversion.h>
|
#include <c++utilities/conversion/stringconversion.h>
|
||||||
|
@ -30,7 +31,6 @@
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
#include <QCloseEvent>
|
#include <QCloseEvent>
|
||||||
#include <QTimerEvent>
|
#include <QTimerEvent>
|
||||||
#include <QPushButton>
|
|
||||||
#include <QUndoStack>
|
#include <QUndoStack>
|
||||||
#include <QUndoView>
|
#include <QUndoView>
|
||||||
#include <QMimeData>
|
#include <QMimeData>
|
||||||
|
@ -43,6 +43,7 @@ using namespace std;
|
||||||
using namespace IoUtilities;
|
using namespace IoUtilities;
|
||||||
using namespace Io;
|
using namespace Io;
|
||||||
using namespace Dialogs;
|
using namespace Dialogs;
|
||||||
|
using namespace MiscUtils;
|
||||||
|
|
||||||
namespace QtGui {
|
namespace QtGui {
|
||||||
|
|
||||||
|
@ -124,21 +125,10 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||||
// load settings
|
// load settings
|
||||||
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName());
|
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName());
|
||||||
settings.beginGroup(QStringLiteral("mainwindow"));
|
settings.beginGroup(QStringLiteral("mainwindow"));
|
||||||
QStringList recentEntries = settings.value(QStringLiteral("recententries"), QStringList()).toStringList();
|
// init recent menu manager
|
||||||
QAction *action = nullptr;
|
m_recentMgr = new RecentMenuManager(m_ui->menuRecent, this);
|
||||||
m_ui->actionSepRecent->setSeparator(true);
|
m_recentMgr->restore(settings.value(QStringLiteral("recententries"), QStringList()).toStringList());
|
||||||
for(const QString &path : recentEntries) {
|
connect(m_recentMgr, &RecentMenuManager::fileSelected, this, &MainWindow::openFile);
|
||||||
if(!path.isEmpty()) {
|
|
||||||
action = new QAction(path, this);
|
|
||||||
action->setProperty("file_path", path);
|
|
||||||
m_ui->menuRecent->insertAction(m_ui->actionSepRecent, action);
|
|
||||||
connect(action, &QAction::triggered, this, &MainWindow::openRecentFile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(action) {
|
|
||||||
m_ui->menuRecent->actions().front()->setShortcut(QKeySequence(Qt::Key_F6));
|
|
||||||
m_ui->menuRecent->setEnabled(true);
|
|
||||||
}
|
|
||||||
// set position and size
|
// set position and size
|
||||||
resize(settings.value("size", size()).toSize());
|
resize(settings.value("size", size()).toSize());
|
||||||
move(settings.value("pos", QPoint(300, 200)).toPoint());
|
move(settings.value("pos", QPoint(300, 200)).toPoint());
|
||||||
|
@ -193,8 +183,6 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||||
connect(m_ui->actionAbout, &QAction::triggered, this, &MainWindow::showAboutDialog);
|
connect(m_ui->actionAbout, &QAction::triggered, this, &MainWindow::showAboutDialog);
|
||||||
connect(m_ui->actionOpen, &QAction::triggered, this, &MainWindow::showOpenFileDialog);
|
connect(m_ui->actionOpen, &QAction::triggered, this, &MainWindow::showOpenFileDialog);
|
||||||
connect(m_ui->actionSaveAs, &QAction::triggered, this, &MainWindow::showSaveFileDialog);
|
connect(m_ui->actionSaveAs, &QAction::triggered, this, &MainWindow::showSaveFileDialog);
|
||||||
// recent menu
|
|
||||||
connect(m_ui->actionClearRecent, &QAction::triggered, this, &MainWindow::clearRecent);
|
|
||||||
// add/remove account
|
// add/remove account
|
||||||
connect(m_ui->actionAddAccount, &QAction::triggered, this, &MainWindow::addAccount);
|
connect(m_ui->actionAddAccount, &QAction::triggered, this, &MainWindow::addAccount);
|
||||||
connect(m_ui->actionAddCategory, &QAction::triggered, this, &MainWindow::addCategory);
|
connect(m_ui->actionAddCategory, &QAction::triggered, this, &MainWindow::addCategory);
|
||||||
|
@ -291,16 +279,7 @@ void MainWindow::closeEvent(QCloseEvent *event)
|
||||||
settings.beginGroup(QStringLiteral("mainwindow"));
|
settings.beginGroup(QStringLiteral("mainwindow"));
|
||||||
settings.setValue(QStringLiteral("size"), size());
|
settings.setValue(QStringLiteral("size"), size());
|
||||||
settings.setValue(QStringLiteral("pos"), pos());
|
settings.setValue(QStringLiteral("pos"), pos());
|
||||||
QStringList existingEntires;
|
settings.setValue(QStringLiteral("recententries"), m_recentMgr->save());
|
||||||
QList<QAction *> entryActions = m_ui->menuRecent->actions();
|
|
||||||
existingEntires.reserve(entryActions.size());
|
|
||||||
for(const QAction *action : entryActions) {
|
|
||||||
QVariant path = action->property("file_path");
|
|
||||||
if(!path.isNull()) {
|
|
||||||
existingEntires << path.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
settings.setValue(QStringLiteral("recententries"), existingEntires);
|
|
||||||
settings.setValue(QStringLiteral("accountfilter"), m_ui->accountFilterLineEdit->text());
|
settings.setValue(QStringLiteral("accountfilter"), m_ui->accountFilterLineEdit->text());
|
||||||
settings.setValue(QStringLiteral("alwayscreatebackup"), m_ui->actionAlwaysCreateBackup->isChecked());
|
settings.setValue(QStringLiteral("alwayscreatebackup"), m_ui->actionAlwaysCreateBackup->isChecked());
|
||||||
QString pwVisibility;
|
QString pwVisibility;
|
||||||
|
@ -356,35 +335,6 @@ void MainWindow::showOpenFileDialog()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Opens a file from the "recently opened" list.
|
|
||||||
*
|
|
||||||
* This private slot is directly called when the corresponding QAction is triggered.
|
|
||||||
*/
|
|
||||||
void MainWindow::openRecentFile()
|
|
||||||
{
|
|
||||||
if(QAction* action = qobject_cast<QAction *>(sender())) {
|
|
||||||
QString path = action->property("file_path").toString();
|
|
||||||
if(!path.isEmpty()) {
|
|
||||||
if(QFile::exists(path)) {
|
|
||||||
openFile(path);
|
|
||||||
} else {
|
|
||||||
QMessageBox msg(this);
|
|
||||||
msg.setWindowTitle(QApplication::applicationName());
|
|
||||||
msg.setText(tr("The selected file can't be found anymore. Do you want to delete the obsolete entry from the list?"));
|
|
||||||
msg.setIcon(QMessageBox::Warning);
|
|
||||||
QPushButton *keepEntryButton = msg.addButton(tr("keep entry"), QMessageBox::NoRole);
|
|
||||||
QPushButton *deleteEntryButton = msg.addButton(tr("delete entry"), QMessageBox::YesRole);
|
|
||||||
msg.setEscapeButton(keepEntryButton);
|
|
||||||
msg.exec();
|
|
||||||
if(msg.clickedButton() == deleteEntryButton) {
|
|
||||||
delete action;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Shows the save file dialog and saves the file at the selected location.
|
* \brief Shows the save file dialog and saves the file at the selected location.
|
||||||
*/
|
*/
|
||||||
|
@ -556,7 +506,7 @@ bool MainWindow::showFile()
|
||||||
if(m_file.path().empty()) {
|
if(m_file.path().empty()) {
|
||||||
m_ui->statusBar->showMessage(tr("A new password list has been created."), 5000);
|
m_ui->statusBar->showMessage(tr("A new password list has been created."), 5000);
|
||||||
} else {
|
} else {
|
||||||
addRecentEntry(QString::fromStdString(m_file.path()));
|
m_recentMgr->addEntry(QString::fromStdString(m_file.path()));
|
||||||
m_ui->statusBar->showMessage(tr("The password list has been load."), 5000);
|
m_ui->statusBar->showMessage(tr("The password list has been load."), 5000);
|
||||||
}
|
}
|
||||||
updateWindowTitle();
|
updateWindowTitle();
|
||||||
|
@ -566,44 +516,6 @@ bool MainWindow::showFile()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Adds a recent entry for the specified \a path. Called within showFile().
|
|
||||||
*/
|
|
||||||
void MainWindow::addRecentEntry(const QString &path)
|
|
||||||
{
|
|
||||||
QList<QAction *> existingEntries = m_ui->menuRecent->actions();
|
|
||||||
QAction *entry = nullptr;
|
|
||||||
// remove shortcut from existing entries
|
|
||||||
for(QAction *existingEntry : existingEntries) {
|
|
||||||
existingEntry->setShortcut(QKeySequence());
|
|
||||||
// check whether existing entry matches entry to add
|
|
||||||
if(existingEntry->property("file_path").toString() == path) {
|
|
||||||
entry = existingEntry;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!entry) {
|
|
||||||
// remove old entries to have never more then 10 entries
|
|
||||||
for(int i = existingEntries.size() - 1; i > 8; --i) {
|
|
||||||
delete existingEntries[i];
|
|
||||||
}
|
|
||||||
existingEntries = m_ui->menuRecent->actions();
|
|
||||||
// create new action
|
|
||||||
entry = new QAction(path, this);
|
|
||||||
entry->setProperty("file_path", path);
|
|
||||||
connect(entry, &QAction::triggered, this, &MainWindow::openRecentFile);
|
|
||||||
} else {
|
|
||||||
// remove existing action (will be inserted again as first action)
|
|
||||||
m_ui->menuRecent->removeAction(entry);
|
|
||||||
}
|
|
||||||
// add shortcut for new entry
|
|
||||||
entry->setShortcut(QKeySequence(Qt::Key_F6));
|
|
||||||
// ensure menu is enabled
|
|
||||||
m_ui->menuRecent->setEnabled(true);
|
|
||||||
// add action as first action in the recent menu
|
|
||||||
m_ui->menuRecent->insertAction(m_ui->menuRecent->isEmpty() ? nullptr : m_ui->menuRecent->actions().front(), entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Updates the status of the UI elements.
|
* \brief Updates the status of the UI elements.
|
||||||
*/
|
*/
|
||||||
|
@ -885,7 +797,7 @@ bool MainWindow::saveFile()
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
setSomethingChanged(false);
|
setSomethingChanged(false);
|
||||||
addRecentEntry(QString::fromStdString(m_file.path()));
|
m_recentMgr->addEntry(QString::fromStdString(m_file.path()));
|
||||||
m_ui->statusBar->showMessage(tr("The password list has been saved."), 5000);
|
m_ui->statusBar->showMessage(tr("The password list has been saved."), 5000);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1160,20 +1072,6 @@ void MainWindow::changePassword()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Clears all entries in the "recently opened" list.
|
|
||||||
*/
|
|
||||||
void MainWindow::clearRecent()
|
|
||||||
{
|
|
||||||
QList<QAction *> entries = m_ui->menuRecent->actions();
|
|
||||||
for(auto i = entries.begin(), end = entries.end() - 2; i != end; ++i) {
|
|
||||||
if(*i != m_ui->actionClearRecent) {
|
|
||||||
delete *i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_ui->menuRecent->setEnabled(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Shows the tree view context menu.
|
* \brief Shows the tree view context menu.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <passwordfile/io/passwordfile.h>
|
#include <passwordfile/io/passwordfile.h>
|
||||||
|
|
||||||
#include <qtutilities/aboutdialog/aboutdialog.h>
|
#include <qtutilities/aboutdialog/aboutdialog.h>
|
||||||
|
|
||||||
#include <c++utilities/io/binaryreader.h>
|
#include <c++utilities/io/binaryreader.h>
|
||||||
#include <c++utilities/io/binarywriter.h>
|
#include <c++utilities/io/binarywriter.h>
|
||||||
|
|
||||||
|
@ -16,18 +17,20 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_FORWARD_DECLARE_CLASS(QCloseEvent)
|
||||||
class QCloseEvent;
|
QT_FORWARD_DECLARE_CLASS(QTreeWidgetItem)
|
||||||
class QTreeWidgetItem;
|
QT_FORWARD_DECLARE_CLASS(QUndoStack)
|
||||||
class QUndoStack;
|
QT_FORWARD_DECLARE_CLASS(QUndoView)
|
||||||
class QUndoView;
|
|
||||||
QT_END_NAMESPACE
|
|
||||||
|
|
||||||
namespace Io {
|
namespace Io {
|
||||||
DECLARE_ENUM(EntryType, int)
|
DECLARE_ENUM(EntryType, int)
|
||||||
DECLARE_ENUM(FieldType, int)
|
DECLARE_ENUM(FieldType, int)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace MiscUtils {
|
||||||
|
class RecentMenuManager;
|
||||||
|
}
|
||||||
|
|
||||||
namespace QtGui {
|
namespace QtGui {
|
||||||
|
|
||||||
class FieldModel;
|
class FieldModel;
|
||||||
|
@ -95,10 +98,6 @@ private Q_SLOTS:
|
||||||
// showing context menus
|
// showing context menus
|
||||||
void showTreeViewContextMenu();
|
void showTreeViewContextMenu();
|
||||||
void showTableViewContextMenu();
|
void showTableViewContextMenu();
|
||||||
// recent entries menu
|
|
||||||
void addRecentEntry(const QString &path);
|
|
||||||
void openRecentFile();
|
|
||||||
void clearRecent();
|
|
||||||
// other
|
// other
|
||||||
void showContainingDirectory();
|
void showContainingDirectory();
|
||||||
void clearClipboard();
|
void clearClipboard();
|
||||||
|
@ -125,6 +124,7 @@ private:
|
||||||
bool m_somethingChanged;
|
bool m_somethingChanged;
|
||||||
bool m_dontUpdateSelection;
|
bool m_dontUpdateSelection;
|
||||||
int m_clearClipboardTimer;
|
int m_clearClipboardTimer;
|
||||||
|
MiscUtils::RecentMenuManager *m_recentMgr;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -183,7 +183,7 @@
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>848</width>
|
<width>848</width>
|
||||||
<height>27</height>
|
<height>26</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QMenu" name="menuProgramm">
|
<widget class="QMenu" name="menuProgramm">
|
||||||
|
@ -194,15 +194,6 @@
|
||||||
<property name="toolTip">
|
<property name="toolTip">
|
||||||
<string>Recently opened files</string>
|
<string>Recently opened files</string>
|
||||||
</property>
|
</property>
|
||||||
<property name="title">
|
|
||||||
<string>&Recent</string>
|
|
||||||
</property>
|
|
||||||
<property name="icon">
|
|
||||||
<iconset theme="document-open-recent">
|
|
||||||
<normaloff>.</normaloff>.</iconset>
|
|
||||||
</property>
|
|
||||||
<addaction name="actionSepRecent"/>
|
|
||||||
<addaction name="actionClearRecent"/>
|
|
||||||
</widget>
|
</widget>
|
||||||
<addaction name="actionCreate"/>
|
<addaction name="actionCreate"/>
|
||||||
<addaction name="actionOpen"/>
|
<addaction name="actionOpen"/>
|
||||||
|
@ -410,15 +401,6 @@
|
||||||
<string>&Password generator</string>
|
<string>&Password generator</string>
|
||||||
</property>
|
</property>
|
||||||
</action>
|
</action>
|
||||||
<action name="actionClearRecent">
|
|
||||||
<property name="icon">
|
|
||||||
<iconset theme="edit-clear">
|
|
||||||
<normaloff>.</normaloff>.</iconset>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
|
||||||
<string>&Clear list</string>
|
|
||||||
</property>
|
|
||||||
</action>
|
|
||||||
<action name="actionAlwaysCreateBackup">
|
<action name="actionAlwaysCreateBackup">
|
||||||
<property name="checkable">
|
<property name="checkable">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
|
@ -492,7 +474,6 @@
|
||||||
<string>&Show undo stack</string>
|
<string>&Show undo stack</string>
|
||||||
</property>
|
</property>
|
||||||
</action>
|
</action>
|
||||||
<action name="actionSepRecent"/>
|
|
||||||
<action name="actionShowAlways">
|
<action name="actionShowAlways">
|
||||||
<property name="checkable">
|
<property name="checkable">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
|
|
Loading…
Reference in New Issue