refactored "recently opened files menu"

This commit is contained in:
Martchus 2016-04-19 01:38:43 +02:00
parent 06a5a418bc
commit fc13d3d6a9
3 changed files with 20 additions and 141 deletions

View File

@ -18,6 +18,7 @@
#include <qtutilities/enterpassworddialog/enterpassworddialog.h>
#include <qtutilities/misc/dialogutils.h>
#include <qtutilities/misc/desktoputils.h>
#include <qtutilities/misc/recentmenumanager.h>
#include <c++utilities/io/path.h>
#include <c++utilities/conversion/stringconversion.h>
@ -30,7 +31,6 @@
#include <QSettings>
#include <QCloseEvent>
#include <QTimerEvent>
#include <QPushButton>
#include <QUndoStack>
#include <QUndoView>
#include <QMimeData>
@ -43,6 +43,7 @@ using namespace std;
using namespace IoUtilities;
using namespace Io;
using namespace Dialogs;
using namespace MiscUtils;
namespace QtGui {
@ -124,21 +125,10 @@ MainWindow::MainWindow(QWidget *parent) :
// load settings
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName());
settings.beginGroup(QStringLiteral("mainwindow"));
QStringList recentEntries = settings.value(QStringLiteral("recententries"), QStringList()).toStringList();
QAction *action = nullptr;
m_ui->actionSepRecent->setSeparator(true);
for(const QString &path : recentEntries) {
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);
}
// init recent menu manager
m_recentMgr = new RecentMenuManager(m_ui->menuRecent, this);
m_recentMgr->restore(settings.value(QStringLiteral("recententries"), QStringList()).toStringList());
connect(m_recentMgr, &RecentMenuManager::fileSelected, this, &MainWindow::openFile);
// set position and size
resize(settings.value("size", size()).toSize());
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->actionOpen, &QAction::triggered, this, &MainWindow::showOpenFileDialog);
connect(m_ui->actionSaveAs, &QAction::triggered, this, &MainWindow::showSaveFileDialog);
// recent menu
connect(m_ui->actionClearRecent, &QAction::triggered, this, &MainWindow::clearRecent);
// add/remove account
connect(m_ui->actionAddAccount, &QAction::triggered, this, &MainWindow::addAccount);
connect(m_ui->actionAddCategory, &QAction::triggered, this, &MainWindow::addCategory);
@ -291,16 +279,7 @@ void MainWindow::closeEvent(QCloseEvent *event)
settings.beginGroup(QStringLiteral("mainwindow"));
settings.setValue(QStringLiteral("size"), size());
settings.setValue(QStringLiteral("pos"), pos());
QStringList existingEntires;
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("recententries"), m_recentMgr->save());
settings.setValue(QStringLiteral("accountfilter"), m_ui->accountFilterLineEdit->text());
settings.setValue(QStringLiteral("alwayscreatebackup"), m_ui->actionAlwaysCreateBackup->isChecked());
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.
*/
@ -556,7 +506,7 @@ bool MainWindow::showFile()
if(m_file.path().empty()) {
m_ui->statusBar->showMessage(tr("A new password list has been created."), 5000);
} 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);
}
updateWindowTitle();
@ -566,44 +516,6 @@ bool MainWindow::showFile()
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.
*/
@ -885,7 +797,7 @@ bool MainWindow::saveFile()
return false;
} else {
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);
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.
*/

View File

@ -6,6 +6,7 @@
#include <passwordfile/io/passwordfile.h>
#include <qtutilities/aboutdialog/aboutdialog.h>
#include <c++utilities/io/binaryreader.h>
#include <c++utilities/io/binarywriter.h>
@ -16,18 +17,20 @@
#include <iostream>
#include <fstream>
QT_BEGIN_NAMESPACE
class QCloseEvent;
class QTreeWidgetItem;
class QUndoStack;
class QUndoView;
QT_END_NAMESPACE
QT_FORWARD_DECLARE_CLASS(QCloseEvent)
QT_FORWARD_DECLARE_CLASS(QTreeWidgetItem)
QT_FORWARD_DECLARE_CLASS(QUndoStack)
QT_FORWARD_DECLARE_CLASS(QUndoView)
namespace Io {
DECLARE_ENUM(EntryType, int)
DECLARE_ENUM(FieldType, int)
}
namespace MiscUtils {
class RecentMenuManager;
}
namespace QtGui {
class FieldModel;
@ -95,10 +98,6 @@ private Q_SLOTS:
// showing context menus
void showTreeViewContextMenu();
void showTableViewContextMenu();
// recent entries menu
void addRecentEntry(const QString &path);
void openRecentFile();
void clearRecent();
// other
void showContainingDirectory();
void clearClipboard();
@ -125,6 +124,7 @@ private:
bool m_somethingChanged;
bool m_dontUpdateSelection;
int m_clearClipboardTimer;
MiscUtils::RecentMenuManager *m_recentMgr;
};
}

View File

@ -183,7 +183,7 @@
<x>0</x>
<y>0</y>
<width>848</width>
<height>27</height>
<height>26</height>
</rect>
</property>
<widget class="QMenu" name="menuProgramm">
@ -194,15 +194,6 @@
<property name="toolTip">
<string>Recently opened files</string>
</property>
<property name="title">
<string>&amp;Recent</string>
</property>
<property name="icon">
<iconset theme="document-open-recent">
<normaloff>.</normaloff>.</iconset>
</property>
<addaction name="actionSepRecent"/>
<addaction name="actionClearRecent"/>
</widget>
<addaction name="actionCreate"/>
<addaction name="actionOpen"/>
@ -410,15 +401,6 @@
<string>&amp;Password generator</string>
</property>
</action>
<action name="actionClearRecent">
<property name="icon">
<iconset theme="edit-clear">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Clear list</string>
</property>
</action>
<action name="actionAlwaysCreateBackup">
<property name="checkable">
<bool>true</bool>
@ -492,7 +474,6 @@
<string>&amp;Show undo stack</string>
</property>
</action>
<action name="actionSepRecent"/>
<action name="actionShowAlways">
<property name="checkable">
<bool>true</bool>