mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 10:46:35 +01:00
SessionManager: Add Session Manager dialog instead of menu structure
See #2331
This commit is contained in:
parent
bc65a0a038
commit
93d4d66054
@ -81,6 +81,11 @@ void MainMenu::initSuperMenu(QMenu* superMenu) const
|
||||
superMenu->addAction(m_actions[QSL("File/NewPrivateWindow")]);
|
||||
superMenu->addAction(m_actions[QSL("File/OpenFile")]);
|
||||
superMenu->addSeparator();
|
||||
QMenu* sessionsSubmenu = new QMenu(tr("Sessions"));
|
||||
connect(sessionsSubmenu, SIGNAL(aboutToShow()), mApp->sessionManager(), SLOT(aboutToShowSessionsMenu()));
|
||||
superMenu->addMenu(sessionsSubmenu);
|
||||
superMenu->addAction(m_actions[QSL("File/SessionManager")]);
|
||||
superMenu->addSeparator();
|
||||
superMenu->addAction(m_actions[QSL("File/SendLink")]);
|
||||
superMenu->addAction(m_actions[QSL("File/Print")]);
|
||||
superMenu->addSeparator();
|
||||
@ -529,17 +534,13 @@ void MainMenu::init()
|
||||
m_menuFile->addSeparator();
|
||||
|
||||
if (!mApp->isPrivate()) {
|
||||
action = new QAction(tr("New Session..."), this);
|
||||
connect(action, SIGNAL(triggered()), mApp->sessionManager(), SLOT(newSession()));
|
||||
m_actions[QSL("File/NewSession")] = action;
|
||||
m_menuFile->addAction(action);
|
||||
action = new QAction(tr("Save Session..."), this);
|
||||
connect(action, SIGNAL(triggered()), mApp->sessionManager(), SLOT(saveSession()));
|
||||
m_actions[QSL("File/SaveSession")] = action;
|
||||
m_menuFile->addAction(action);
|
||||
QMenu* sessionsSubmenu = new QMenu(tr("Sessions"));
|
||||
connect(sessionsSubmenu, SIGNAL(aboutToShow()), mApp->sessionManager(), SLOT(aboutToShowSessionsMenu()));
|
||||
m_menuFile->addMenu(sessionsSubmenu);
|
||||
action = new QAction(tr("Session Manager"), this);
|
||||
connect(action, SIGNAL(triggered()), mApp->sessionManager(), SLOT(openSessionManagerDialog()));
|
||||
m_actions[QSL("File/SessionManager")] = action;
|
||||
m_menuFile->addAction(action);
|
||||
m_menuFile->addSeparator();
|
||||
}
|
||||
|
||||
|
@ -167,6 +167,7 @@ SOURCES += \
|
||||
session/recoveryjsobject.cpp \
|
||||
session/restoremanager.cpp \
|
||||
session/sessionmanager.cpp \
|
||||
session/sessionmanagerdialog.cpp \
|
||||
sidebar/bookmarkssidebar.cpp \
|
||||
sidebar/historysidebar.cpp \
|
||||
sidebar/sidebar.cpp \
|
||||
@ -202,11 +203,13 @@ SOURCES += \
|
||||
tools/progressbar.cpp \
|
||||
tools/qzregexp.cpp \
|
||||
tools/qztools.cpp \
|
||||
tools/removeitemfocusdelegate.cpp \
|
||||
tools/scripts.cpp \
|
||||
tools/sqldatabase.cpp \
|
||||
tools/toolbutton.cpp \
|
||||
tools/treewidget.cpp \
|
||||
tools/widget.cpp \
|
||||
tools/wheelhelper.cpp \
|
||||
webengine/javascript/autofilljsobject.cpp \
|
||||
webengine/javascript/externaljsobject.cpp \
|
||||
webengine/loadrequest.cpp \
|
||||
@ -219,7 +222,6 @@ SOURCES += \
|
||||
webtab/searchtoolbar.cpp \
|
||||
webtab/tabbedwebview.cpp \
|
||||
webtab/webtab.cpp \
|
||||
tools/wheelhelper.cpp \
|
||||
|
||||
HEADERS += \
|
||||
3rdparty/fancytabwidget.h \
|
||||
@ -349,6 +351,7 @@ HEADERS += \
|
||||
session/recoveryjsobject.h \
|
||||
session/restoremanager.h \
|
||||
session/sessionmanager.h \
|
||||
session/sessionmanagerdialog.h \
|
||||
sidebar/bookmarkssidebar.h \
|
||||
sidebar/historysidebar.h \
|
||||
sidebar/sidebar.h \
|
||||
@ -385,11 +388,13 @@ HEADERS += \
|
||||
tools/progressbar.h \
|
||||
tools/qzregexp.h \
|
||||
tools/qztools.h \
|
||||
tools/removeitemfocusdelegate.h \
|
||||
tools/scripts.h \
|
||||
tools/sqldatabase.h \
|
||||
tools/toolbutton.h \
|
||||
tools/treewidget.h \
|
||||
tools/widget.h \
|
||||
tools/wheelhelper.h \
|
||||
webengine/javascript/autofilljsobject.h \
|
||||
webengine/javascript/externaljsobject.h \
|
||||
webengine/loadrequest.h \
|
||||
@ -402,7 +407,6 @@ HEADERS += \
|
||||
webtab/searchtoolbar.h \
|
||||
webtab/tabbedwebview.h \
|
||||
webtab/webtab.h \
|
||||
tools/wheelhelper.h \
|
||||
|
||||
FORMS += \
|
||||
adblock/adblockaddsubscriptiondialog.ui \
|
||||
@ -437,6 +441,7 @@ FORMS += \
|
||||
preferences/preferences.ui \
|
||||
preferences/thememanager.ui \
|
||||
preferences/useragentdialog.ui \
|
||||
session/sessionmanagerdialog.ui \
|
||||
sidebar/bookmarkssidebar.ui \
|
||||
sidebar/historysidebar.ui \
|
||||
tools/certificateinfowidget.ui \
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include "mainapplication.h"
|
||||
#include "restoremanager.h"
|
||||
#include "sessionmanager.h"
|
||||
#include "sessionmanagerdialog.h"
|
||||
#include "settings.h"
|
||||
|
||||
#include <QAction>
|
||||
@ -42,99 +43,28 @@ SessionManager::SessionManager(QObject* parent)
|
||||
{
|
||||
QFileSystemWatcher* sessionFilesWatcher = new QFileSystemWatcher({DataPaths::path(DataPaths::Sessions)}, this);
|
||||
connect(sessionFilesWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(sessionsDirectoryChanged()));
|
||||
connect(sessionFilesWatcher, &QFileSystemWatcher::directoryChanged, this, &SessionManager::sessionsMetaDataChanged);
|
||||
|
||||
loadSettings();
|
||||
}
|
||||
|
||||
static void addSessionSubmenu(QObject* receiver, QMenu* menu, const QString &title, const QString &filePath, const QFileInfo &lastActiveSessionFileInfo)
|
||||
{
|
||||
QMenu* sessionSubmenu = new QMenu(title, menu);
|
||||
QObject::connect(sessionSubmenu, SIGNAL(aboutToShow()), receiver, SLOT(aboutToShowSessionSubmenu()));
|
||||
|
||||
QAction* action = menu->addMenu(sessionSubmenu);
|
||||
action->setData(filePath);
|
||||
action->setCheckable(true);
|
||||
action->setChecked(QFileInfo(filePath) == lastActiveSessionFileInfo);
|
||||
}
|
||||
|
||||
static void addSessionsMetaDataToMenu(QObject* receiver, QMenu* menu, const QFileInfo &lastActiveSessionFileInfo, const QList<SessionManager::SessionMetaData> &sessionsMetaDataList)
|
||||
{
|
||||
for (const SessionManager::SessionMetaData &metaData : sessionsMetaDataList) {
|
||||
addSessionSubmenu(receiver, menu, metaData.name, metaData.filePath, lastActiveSessionFileInfo);
|
||||
}
|
||||
}
|
||||
|
||||
void SessionManager::aboutToShowSessionsMenu()
|
||||
{
|
||||
QMenu* menu = qobject_cast<QMenu*>(sender());
|
||||
menu->clear();
|
||||
|
||||
const QFileInfo lastActiveSessionInfo(m_lastActiveSessionPath);
|
||||
QActionGroup *group = new QActionGroup(menu);
|
||||
|
||||
fillSessionsMetaDataListIfNeeded();
|
||||
|
||||
addSessionsMetaDataToMenu(this, menu, lastActiveSessionInfo, m_sessionsMetaDataList);
|
||||
|
||||
menu->addSeparator();
|
||||
|
||||
if (QFile::exists(m_firstBackupSession))
|
||||
addSessionSubmenu(this, menu, tr("First Backup"), m_firstBackupSession, lastActiveSessionInfo);
|
||||
if (QFile::exists(m_secondBackupSession))
|
||||
addSessionSubmenu(this, menu, tr("Second Backup"), m_secondBackupSession, lastActiveSessionInfo);
|
||||
const auto sessions = sessionMetaData(/*withBackups*/ false);
|
||||
for (const SessionManager::SessionMetaData &metaData : sessions) {
|
||||
QAction* action = menu->addAction(metaData.name);
|
||||
action->setCheckable(true);
|
||||
action->setChecked(metaData.isActive);
|
||||
group->addAction(action);
|
||||
connect(action, &QAction::triggered, this, [=]() {
|
||||
switchToSession(metaData.filePath);
|
||||
});
|
||||
}
|
||||
|
||||
void SessionManager::aboutToShowSessionSubmenu()
|
||||
{
|
||||
QMenu* menu = qobject_cast<QMenu*>(sender());
|
||||
menu->clear();
|
||||
|
||||
const QString sessionFilePath = menu->menuAction()->data().toString();
|
||||
|
||||
const QFileInfo currentSessionFileInfo(m_lastActiveSessionPath);
|
||||
const QFileInfo sessionFileInfo(sessionFilePath);
|
||||
|
||||
QList<QAction*> actions;
|
||||
QAction* action;
|
||||
|
||||
if (sessionFileInfo != currentSessionFileInfo || mApp->restoreManager()) {
|
||||
if (sessionFileInfo != QFileInfo(m_firstBackupSession) && sessionFileInfo != QFileInfo(m_secondBackupSession)) {
|
||||
action = new QAction(SessionManager::tr("Switch To"), menu);
|
||||
action->setData(sessionFilePath);
|
||||
QObject::connect(action, SIGNAL(triggered(bool)), this, SLOT(switchToSession()));
|
||||
actions << action;
|
||||
}
|
||||
|
||||
action = new QAction(SessionManager::tr("Open"), menu);
|
||||
action->setData(sessionFilePath);
|
||||
QObject::connect(action, SIGNAL(triggered(bool)), this, SLOT(openSession()));
|
||||
actions << action;
|
||||
|
||||
action = new QAction(menu);
|
||||
action->setSeparator(true);
|
||||
actions << action;
|
||||
}
|
||||
|
||||
action = new QAction(SessionManager::tr("Clone"), menu);
|
||||
action->setData(sessionFilePath);
|
||||
QObject::connect(action, SIGNAL(triggered(bool)), this, SLOT(cloneSession()));
|
||||
actions << action;
|
||||
|
||||
if (sessionFileInfo != currentSessionFileInfo) {
|
||||
action = new QAction(SessionManager::tr("Rename"), menu);
|
||||
action->setData(sessionFilePath);
|
||||
QObject::connect(action, SIGNAL(triggered(bool)), this, SLOT(renameSession()));
|
||||
actions << action;
|
||||
}
|
||||
|
||||
if (sessionFileInfo != currentSessionFileInfo && sessionFileInfo != QFileInfo(defaultSessionPath()) &&
|
||||
sessionFileInfo != QFileInfo(m_firstBackupSession) && sessionFileInfo != QFileInfo(m_secondBackupSession)) {
|
||||
action = new QAction(SessionManager::tr("Remove"), menu);
|
||||
action->setData(sessionFilePath);
|
||||
QObject::connect(action, SIGNAL(triggered(bool)), this, SLOT(deleteSession()));
|
||||
actions << action;
|
||||
}
|
||||
|
||||
menu->addActions(actions);
|
||||
}
|
||||
|
||||
void SessionManager::sessionsDirectoryChanged()
|
||||
@ -142,15 +72,6 @@ void SessionManager::sessionsDirectoryChanged()
|
||||
m_sessionsMetaDataList.clear();
|
||||
}
|
||||
|
||||
void SessionManager::switchToSession()
|
||||
{
|
||||
QAction* action = qobject_cast<QAction*>(sender());
|
||||
if (!action)
|
||||
return;
|
||||
|
||||
openSession(action->data().toString(), true);
|
||||
}
|
||||
|
||||
void SessionManager::openSession(QString sessionFilePath, bool switchSession)
|
||||
{
|
||||
if (sessionFilePath.isEmpty()) {
|
||||
@ -161,6 +82,10 @@ void SessionManager::openSession(QString sessionFilePath, bool switchSession)
|
||||
sessionFilePath = action->data().toString();
|
||||
}
|
||||
|
||||
if (isActive(sessionFilePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
QVector<RestoreManager::WindowData> sessionData;
|
||||
RestoreManager::createFromFile(sessionFilePath, sessionData);
|
||||
|
||||
@ -178,6 +103,7 @@ void SessionManager::openSession(QString sessionFilePath, bool switchSession)
|
||||
}
|
||||
|
||||
m_lastActiveSessionPath = QFileInfo(sessionFilePath).canonicalFilePath();
|
||||
m_sessionsMetaDataList.clear();
|
||||
}
|
||||
|
||||
mApp->openSession(window, sessionData);
|
||||
@ -214,34 +140,15 @@ void SessionManager::renameSession(QString sessionFilePath, bool clone)
|
||||
QMessageBox::information(mApp->activeWindow(), tr("Error!"), tr("An error occurred when cloning session file."));
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (!QFile::rename(sessionFilePath, newSessionPath)) {
|
||||
} else {
|
||||
if (!QFile::rename(sessionFilePath, newSessionPath)) {
|
||||
QMessageBox::information(mApp->activeWindow(), tr("Error!"), tr("An error occurred when renaming session file."));
|
||||
return;
|
||||
}
|
||||
if (isActive(sessionFilePath)) {
|
||||
m_lastActiveSessionPath = newSessionPath;
|
||||
m_sessionsMetaDataList.clear();
|
||||
}
|
||||
|
||||
void SessionManager::cloneSession()
|
||||
{
|
||||
QAction* action = qobject_cast<QAction*>(sender());
|
||||
if (!action)
|
||||
return;
|
||||
|
||||
renameSession(action->data().toString(), true);
|
||||
}
|
||||
|
||||
void SessionManager::deleteSession()
|
||||
{
|
||||
QAction* action = qobject_cast<QAction*>(sender());
|
||||
if (!action)
|
||||
return;
|
||||
|
||||
const QString filePath = action->data().toString();
|
||||
|
||||
QMessageBox::StandardButton result = QMessageBox::information(mApp->activeWindow(), tr("Warning!"), tr("Are you sure to delete following session?\n%1")
|
||||
.arg(QDir::toNativeSeparators(filePath)), QMessageBox::Yes | QMessageBox::No);
|
||||
if (result == QMessageBox::Yes) {
|
||||
QFile::remove(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
@ -265,6 +172,25 @@ void SessionManager::saveSession()
|
||||
writeCurrentSession(filePath);
|
||||
}
|
||||
|
||||
void SessionManager::switchToSession(const QString &filePath)
|
||||
{
|
||||
openSession(filePath, /*switchSession*/ true);
|
||||
}
|
||||
|
||||
void SessionManager::cloneSession(const QString &filePath)
|
||||
{
|
||||
renameSession(filePath, /*clone*/ true);
|
||||
}
|
||||
|
||||
void SessionManager::deleteSession(const QString &filePath)
|
||||
{
|
||||
QMessageBox::StandardButton result = QMessageBox::information(mApp->activeWindow(), tr("Warning!"), tr("Are you sure to delete following session?\n%1")
|
||||
.arg(QDir::toNativeSeparators(filePath)), QMessageBox::Yes | QMessageBox::No);
|
||||
if (result == QMessageBox::Yes) {
|
||||
QFile::remove(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
void SessionManager::newSession()
|
||||
{
|
||||
bool ok;
|
||||
@ -294,6 +220,40 @@ void SessionManager::newSession()
|
||||
autoSaveLastSession();
|
||||
}
|
||||
|
||||
QList<SessionManager::SessionMetaData> SessionManager::sessionMetaData(bool withBackups)
|
||||
{
|
||||
fillSessionsMetaDataListIfNeeded();
|
||||
|
||||
auto out = m_sessionsMetaDataList;
|
||||
|
||||
if (withBackups && QFile::exists(m_firstBackupSession)) {
|
||||
SessionMetaData data;
|
||||
data.name = tr("First Backup");
|
||||
data.filePath = m_firstBackupSession;
|
||||
data.isBackup = true;
|
||||
out.append(data);
|
||||
}
|
||||
if (withBackups && QFile::exists(m_secondBackupSession)) {
|
||||
SessionMetaData data;
|
||||
data.name = tr("Second Backup");
|
||||
data.filePath = m_secondBackupSession;
|
||||
data.isBackup = true;
|
||||
out.append(data);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
bool SessionManager::isActive(const QString &filePath) const
|
||||
{
|
||||
return QFileInfo(filePath) == QFileInfo(m_lastActiveSessionPath);
|
||||
}
|
||||
|
||||
bool SessionManager::isActive(const QFileInfo &fileInfo) const
|
||||
{
|
||||
return fileInfo == QFileInfo(m_lastActiveSessionPath);
|
||||
}
|
||||
|
||||
void SessionManager::fillSessionsMetaDataListIfNeeded()
|
||||
{
|
||||
if (!m_sessionsMetaDataList.isEmpty())
|
||||
@ -316,12 +276,18 @@ void SessionManager::fillSessionsMetaDataListIfNeeded()
|
||||
SessionMetaData metaData;
|
||||
metaData.name = fileInfo.baseName();
|
||||
|
||||
if (fileInfo == QFileInfo(defaultSessionPath()))
|
||||
if (fileInfo == QFileInfo(defaultSessionPath())) {
|
||||
metaData.name = tr("Default Session");
|
||||
else if (fileNames.contains(fileInfo.baseName()))
|
||||
metaData.isDefault = true;
|
||||
} else if (fileNames.contains(fileInfo.baseName())) {
|
||||
metaData.name = fileInfo.fileName();
|
||||
else
|
||||
} else {
|
||||
metaData.name = fileInfo.baseName();
|
||||
}
|
||||
|
||||
if (isActive(fileInfo)) {
|
||||
metaData.isActive = true;
|
||||
}
|
||||
|
||||
fileNames << metaData.name;
|
||||
metaData.filePath = fileInfo.canonicalFilePath();
|
||||
@ -385,6 +351,12 @@ void SessionManager::writeCurrentSession(const QString &filePath)
|
||||
file.commit();
|
||||
}
|
||||
|
||||
void SessionManager::openSessionManagerDialog()
|
||||
{
|
||||
SessionManagerDialog *dialog = new SessionManagerDialog(mApp->getWindow());
|
||||
dialog->open();
|
||||
}
|
||||
|
||||
void SessionManager::autoSaveLastSession()
|
||||
{
|
||||
if (mApp->isPrivate() || mApp->isRestoring() || mApp->windowCount() == 0 || mApp->restoreManager()) {
|
||||
|
@ -22,7 +22,7 @@
|
||||
|
||||
class QAction;
|
||||
class QMenu;
|
||||
|
||||
class QFileInfo;
|
||||
|
||||
class QUPZILLA_EXPORT SessionManager : public QObject
|
||||
{
|
||||
@ -34,6 +34,9 @@ public:
|
||||
struct SessionMetaData {
|
||||
QString name;
|
||||
QString filePath;
|
||||
bool isActive = false;
|
||||
bool isDefault = false;
|
||||
bool isBackup = false;
|
||||
};
|
||||
|
||||
void loadSettings();
|
||||
@ -46,22 +49,30 @@ public:
|
||||
void backupSavedSessions();
|
||||
void writeCurrentSession(const QString &filePath);
|
||||
|
||||
signals:
|
||||
void sessionsMetaDataChanged();
|
||||
|
||||
public slots:
|
||||
void autoSaveLastSession();
|
||||
void openSessionManagerDialog();
|
||||
|
||||
private slots:
|
||||
void aboutToShowSessionsMenu();
|
||||
void aboutToShowSessionSubmenu();
|
||||
void sessionsDirectoryChanged();
|
||||
void switchToSession();
|
||||
void openSession(QString sessionFilePath = QString(), bool switchSession = false);
|
||||
void renameSession(QString sessionFilePath = QString(), bool clone = false);
|
||||
void cloneSession();
|
||||
void deleteSession();
|
||||
void saveSession();
|
||||
|
||||
void switchToSession(const QString &filePath);
|
||||
void cloneSession(const QString &filePath);
|
||||
void deleteSession(const QString &filePath);
|
||||
void newSession();
|
||||
|
||||
QList<SessionMetaData> sessionMetaData(bool withBackups = true);
|
||||
|
||||
private:
|
||||
bool isActive(const QString &filePath) const;
|
||||
bool isActive(const QFileInfo &fileInfo) const;
|
||||
void fillSessionsMetaDataListIfNeeded();
|
||||
|
||||
QList<SessionMetaData> m_sessionsMetaDataList;
|
||||
@ -69,6 +80,8 @@ private:
|
||||
QString m_firstBackupSession;
|
||||
QString m_secondBackupSession;
|
||||
QString m_lastActiveSessionPath;
|
||||
|
||||
friend class SessionManagerDialog;
|
||||
};
|
||||
|
||||
#endif // SESSIONMANAGER_H
|
||||
|
182
src/lib/session/sessionmanagerdialog.cpp
Normal file
182
src/lib/session/sessionmanagerdialog.cpp
Normal file
@ -0,0 +1,182 @@
|
||||
/* ============================================================
|
||||
* QupZilla - Qt web browser
|
||||
* Copyright (C) 2017 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#include "sessionmanagerdialog.h"
|
||||
#include "ui_sessionmanagerdialog.h"
|
||||
#include "mainapplication.h"
|
||||
#include "sessionmanager.h"
|
||||
#include "removeitemfocusdelegate.h"
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QDateTime>
|
||||
|
||||
SessionManagerDialog::SessionManagerDialog(QWidget *parent) :
|
||||
QDialog(parent),
|
||||
ui(new Ui::SessionManagerDialog)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
setAttribute(Qt::WA_DeleteOnClose);
|
||||
|
||||
ui->treeWidget->setItemDelegate(new RemoveItemFocusDelegate(ui->treeWidget));
|
||||
|
||||
connect(ui->newButton, &QPushButton::clicked, this, &SessionManagerDialog::newSession);
|
||||
connect(ui->renameButton, &QPushButton::clicked, this, &SessionManagerDialog::renameSession);
|
||||
connect(ui->cloneButton, &QPushButton::clicked, this, &SessionManagerDialog::cloneSession);
|
||||
connect(ui->deleteButton, &QPushButton::clicked, this, &SessionManagerDialog::deleteSession);
|
||||
connect(ui->switchToButton, &QPushButton::clicked, this, &SessionManagerDialog::switchToSession);
|
||||
connect(ui->treeWidget, &QTreeWidget::currentItemChanged, this, &SessionManagerDialog::updateButtons);
|
||||
|
||||
refresh();
|
||||
connect(mApp->sessionManager(), &SessionManager::sessionsMetaDataChanged, this, &SessionManagerDialog::refresh);
|
||||
}
|
||||
|
||||
SessionManagerDialog::~SessionManagerDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void SessionManagerDialog::newSession()
|
||||
{
|
||||
mApp->sessionManager()->newSession();
|
||||
}
|
||||
|
||||
void SessionManagerDialog::renameSession()
|
||||
{
|
||||
QTreeWidgetItem *item = ui->treeWidget->currentItem();
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
const QString filePath = item->data(0, SessionFileRole).toString();
|
||||
if (!filePath.isEmpty()) {
|
||||
mApp->sessionManager()->renameSession(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
void SessionManagerDialog::cloneSession()
|
||||
{
|
||||
QTreeWidgetItem *item = ui->treeWidget->currentItem();
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
const QString filePath = item->data(0, SessionFileRole).toString();
|
||||
if (!filePath.isEmpty()) {
|
||||
mApp->sessionManager()->cloneSession(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
void SessionManagerDialog::deleteSession()
|
||||
{
|
||||
QTreeWidgetItem *item = ui->treeWidget->currentItem();
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
const QString filePath = item->data(0, SessionFileRole).toString();
|
||||
if (!filePath.isEmpty()) {
|
||||
mApp->sessionManager()->deleteSession(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
void SessionManagerDialog::switchToSession()
|
||||
{
|
||||
QTreeWidgetItem *item = ui->treeWidget->currentItem();
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
const QString filePath = item->data(0, SessionFileRole).toString();
|
||||
if (!filePath.isEmpty()) {
|
||||
close();
|
||||
mApp->sessionManager()->switchToSession(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
void SessionManagerDialog::refresh()
|
||||
{
|
||||
ui->treeWidget->clear();
|
||||
|
||||
const auto sessions = mApp->sessionManager()->sessionMetaData();
|
||||
for (const auto &session : sessions) {
|
||||
QTreeWidgetItem *item = new QTreeWidgetItem;
|
||||
item->setText(0, session.name);
|
||||
item->setText(1, QFileInfo(session.filePath).lastModified().toString(Qt::DefaultLocaleShortDate));
|
||||
item->setData(0, SessionFileRole, session.filePath);
|
||||
item->setData(0, IsBackupSessionRole, session.isBackup);
|
||||
item->setData(0, IsActiveSessionRole, session.isActive);
|
||||
item->setData(0, IsDefaultSessionRole, session.isDefault);
|
||||
updateItem(item);
|
||||
ui->treeWidget->addTopLevelItem(item);
|
||||
}
|
||||
|
||||
updateButtons();
|
||||
}
|
||||
|
||||
void SessionManagerDialog::updateButtons()
|
||||
{
|
||||
QTreeWidgetItem *item = ui->treeWidget->currentItem();
|
||||
const bool isBackup = item && item->data(0, IsBackupSessionRole).toBool();
|
||||
const bool isActive = item && item->data(0, IsActiveSessionRole).toBool();
|
||||
const bool isDefault = item && item->data(0, IsDefaultSessionRole).toBool();
|
||||
|
||||
ui->renameButton->setEnabled(item && !isDefault && !isBackup);
|
||||
ui->cloneButton->setEnabled(item && !isBackup);
|
||||
ui->deleteButton->setEnabled(item && !isBackup && !isDefault && !isActive);
|
||||
ui->switchToButton->setEnabled(item && !isActive && !isBackup);
|
||||
}
|
||||
|
||||
void SessionManagerDialog::updateItem(QTreeWidgetItem *item)
|
||||
{
|
||||
const bool isBackup = item->data(0, IsBackupSessionRole).toBool();
|
||||
const bool isActive = item->data(0, IsActiveSessionRole).toBool();
|
||||
const bool isDefault = item->data(0, IsDefaultSessionRole).toBool();
|
||||
|
||||
QFont font = item->font(0);
|
||||
|
||||
if (isBackup) {
|
||||
const QColor color = palette().color(QPalette::Disabled, QPalette::WindowText);
|
||||
item->setForeground(0, color);
|
||||
item->setForeground(1, color);
|
||||
}
|
||||
|
||||
if (isActive) {
|
||||
font.setBold(true);
|
||||
item->setFont(0, font);
|
||||
item->setFont(1, font);
|
||||
}
|
||||
|
||||
if (isDefault) {
|
||||
font.setItalic(true);
|
||||
item->setFont(0, font);
|
||||
item->setFont(1, font);
|
||||
}
|
||||
}
|
||||
|
||||
void SessionManagerDialog::showEvent(QShowEvent *e)
|
||||
{
|
||||
QDialog::showEvent(e);
|
||||
resizeViewHeader();
|
||||
}
|
||||
|
||||
void SessionManagerDialog::resizeEvent(QResizeEvent *e)
|
||||
{
|
||||
QDialog::resizeEvent(e);
|
||||
resizeViewHeader();
|
||||
}
|
||||
|
||||
void SessionManagerDialog::resizeViewHeader()
|
||||
{
|
||||
const int headerWidth = ui->treeWidget->header()->width();
|
||||
ui->treeWidget->header()->resizeSection(0, headerWidth - headerWidth / 2.5);
|
||||
}
|
62
src/lib/session/sessionmanagerdialog.h
Normal file
62
src/lib/session/sessionmanagerdialog.h
Normal file
@ -0,0 +1,62 @@
|
||||
/* ============================================================
|
||||
* QupZilla - Qt web browser
|
||||
* Copyright (C) 2017 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#ifndef SESSIONMANAGERDIALOG_H
|
||||
#define SESSIONMANAGERDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
namespace Ui {
|
||||
class SessionManagerDialog;
|
||||
}
|
||||
|
||||
class QTreeWidgetItem;
|
||||
|
||||
class SessionManagerDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SessionManagerDialog(QWidget *parent = 0);
|
||||
~SessionManagerDialog();
|
||||
|
||||
private:
|
||||
enum Roles {
|
||||
SessionFileRole = Qt::UserRole + 10,
|
||||
IsBackupSessionRole,
|
||||
IsActiveSessionRole,
|
||||
IsDefaultSessionRole
|
||||
};
|
||||
|
||||
void newSession();
|
||||
void renameSession();
|
||||
void cloneSession();
|
||||
void deleteSession();
|
||||
void switchToSession();
|
||||
|
||||
void refresh();
|
||||
void updateButtons();
|
||||
void updateItem(QTreeWidgetItem *item);
|
||||
|
||||
void showEvent(QShowEvent *e) override;
|
||||
void resizeEvent(QResizeEvent *e) override;
|
||||
void resizeViewHeader();
|
||||
|
||||
Ui::SessionManagerDialog *ui;
|
||||
};
|
||||
|
||||
#endif // SESSIONMANAGERDIALOG_H
|
133
src/lib/session/sessionmanagerdialog.ui
Normal file
133
src/lib/session/sessionmanagerdialog.ui
Normal file
@ -0,0 +1,133 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>SessionManagerDialog</class>
|
||||
<widget class="QDialog" name="SessionManagerDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>466</width>
|
||||
<height>323</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Session Manager</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QTreeWidget" name="treeWidget">
|
||||
<property name="rootIsDecorated">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<column>
|
||||
<property name="text">
|
||||
<string>Session</string>
|
||||
</property>
|
||||
</column>
|
||||
<column>
|
||||
<property name="text">
|
||||
<string>Last Modified</string>
|
||||
</property>
|
||||
</column>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QPushButton" name="newButton">
|
||||
<property name="text">
|
||||
<string>New</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="renameButton">
|
||||
<property name="text">
|
||||
<string>Rename</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="cloneButton">
|
||||
<property name="text">
|
||||
<string>Clone</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="deleteButton">
|
||||
<property name="text">
|
||||
<string>Delete</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="switchToButton">
|
||||
<property name="text">
|
||||
<string>Switch to</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="1" column="0" colspan="2">
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Close</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>accepted()</signal>
|
||||
<receiver>SessionManagerDialog</receiver>
|
||||
<slot>accept()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>248</x>
|
||||
<y>254</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>157</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>rejected()</signal>
|
||||
<receiver>SessionManagerDialog</receiver>
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>316</x>
|
||||
<y>260</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>286</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
Loading…
Reference in New Issue
Block a user