diff --git a/libs/ui/dialogs/KisSessionManagerDialog.cpp b/libs/ui/dialogs/KisSessionManagerDialog.cpp index 75ddb0064a..1c014d5505 100644 --- a/libs/ui/dialogs/KisSessionManagerDialog.cpp +++ b/libs/ui/dialogs/KisSessionManagerDialog.cpp @@ -1,164 +1,183 @@ /* * Copyright (c) 2018 Jouni Pentikäinen * * 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 2 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include "KisSessionManagerDialog.h" KisSessionManagerDialog::KisSessionManagerDialog(QWidget *parent) : QDialog(parent) { setupUi(this); connect(btnNew, SIGNAL(clicked()), this, SLOT(slotNewSession())); connect(btnRename, SIGNAL(clicked()), this, SLOT(slotRenameSession())); connect(btnSwitchTo, SIGNAL(clicked()), this, SLOT(slotSwitchSession())); connect(btnDelete, SIGNAL(clicked()), this, SLOT(slotDeleteSession())); connect(btnClose, SIGNAL(clicked()), this, SLOT(slotClose())); - m_model = KisResourceModelProvider::resourceModel(ResourceType::Sessions); lstSessions->setModel(m_model); lstSessions->setModelColumn(KisResourceModel::Name); connect(m_model, SIGNAL(beforeResourcesLayoutReset(QModelIndex)), this, SLOT(slotModelAboutToBeReset(QModelIndex))); connect(m_model, SIGNAL(afterResourcesLayoutReset()), this, SLOT(slotModelReset())); connect(lstSessions, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(slotSessionDoubleClicked(QModelIndex))); + + connect(lstSessions->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(slotModelSelectionChanged(QItemSelection, QItemSelection))); + updateButtons(); +} +void KisSessionManagerDialog::updateButtons() +{ + bool hasSelectedSession = getSelectedSession() != nullptr; + btnDelete->setEnabled(hasSelectedSession); + btnSwitchTo->setEnabled(hasSelectedSession); + btnRename->setEnabled(hasSelectedSession); } void KisSessionManagerDialog::slotNewSession() { QString name; KisSessionResourceSP session(new KisSessionResource(QString())); KoResourceServer *server = KisResourceServerProvider::instance()->sessionServer(); QString saveLocation = server->saveLocation(); QFileInfo fileInfo(saveLocation + name.split(" ").join("_") + session->defaultFileExtension()); bool fileOverwriteAccepted = false; while(!fileOverwriteAccepted) { name = QInputDialog::getText(this, i18n("Create session"), i18n("Session name:"), QLineEdit::Normal, name); if (name.isNull() || name.isEmpty()) { return; } else { fileInfo = QFileInfo(saveLocation + name.split(" ").join("_") + session->defaultFileExtension()); if (fileInfo.exists()) { int res = QMessageBox::warning(this, i18nc("@title:window", "Name Already Exists") , i18n("The name '%1' already exists, do you wish to overwrite it?", name) , QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); if (res == QMessageBox::Yes) fileOverwriteAccepted = true; } else { fileOverwriteAccepted = true; } } } session->setFilename(fileInfo.fileName()); session->setName(name); session->storeCurrentWindows(); server->addResource(session); KisPart::instance()->setCurrentSession(session); } void KisSessionManagerDialog::slotRenameSession() { QString name = QInputDialog::getText(this, i18n("Rename session"), i18n("New name:"), QLineEdit::Normal ); if (name.isNull() || name.isEmpty()) return; KisSessionResourceSP session = getSelectedSession(); if (!session) return; m_model->renameResource(session, name); } void KisSessionManagerDialog::slotSessionDoubleClicked(QModelIndex /*item*/) { slotSwitchSession(); slotClose(); } void KisSessionManagerDialog::slotSwitchSession() { KisSessionResourceSP session = getSelectedSession(); if (session) { bool closed = KisPart::instance()->closeSession(true); if (closed) { KisPart::instance()->restoreSession(session); } } } KisSessionResourceSP KisSessionManagerDialog::getSelectedSession() const { QModelIndex idx = lstSessions->currentIndex(); if (idx.isValid()) { KoResourceServer *server = KisResourceServerProvider::instance()->sessionServer(); QString name = m_model->data(idx, Qt::UserRole + KisResourceModel::Name).toString(); return server->resourceByName(name); } return nullptr; } void KisSessionManagerDialog::slotDeleteSession() { QModelIndex idx = lstSessions->currentIndex(); if (idx.isValid()) { m_model->removeResource(lstSessions->currentIndex()); } } void KisSessionManagerDialog::slotClose() { hide(); } void KisSessionManagerDialog::slotModelAboutToBeReset(QModelIndex) { QModelIndex idx = lstSessions->currentIndex(); if (idx.isValid()) { m_lastSessionId = m_model->data(idx, Qt::UserRole + KisResourceModel::Id).toInt(); } } void KisSessionManagerDialog::slotModelReset() { for (int i = 0; i < m_model->rowCount(); i++) { QModelIndex idx = m_model->index(i, 0); int id = m_model->data(idx, Qt::UserRole + KisResourceModel::Id).toInt(); if (id == m_lastSessionId) { lstSessions->setCurrentIndex(idx); } } + + updateButtons(); +} + +void KisSessionManagerDialog::slotModelSelectionChanged(QItemSelection selected, QItemSelection deselected) +{ + (void) selected; + (void) deselected; + + updateButtons(); } diff --git a/libs/ui/dialogs/KisSessionManagerDialog.h b/libs/ui/dialogs/KisSessionManagerDialog.h index 6e5f0a34f0..f2eb678f09 100644 --- a/libs/ui/dialogs/KisSessionManagerDialog.h +++ b/libs/ui/dialogs/KisSessionManagerDialog.h @@ -1,59 +1,61 @@ /* * Copyright (c) 2018 Jouni Pentikäinen * * 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 2 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KISSESSIONMANAGERDIALOG_H #define KISSESSIONMANAGERDIALOG_H #include #include "ui_wdgsessionmanager.h" #include class KisResourceModel; class KisSessionManagerDialog : public QDialog, Ui::DlgSessionManager { Q_OBJECT public: explicit KisSessionManagerDialog(QWidget *parent = nullptr); private Q_SLOTS: void slotNewSession(); void slotRenameSession(); void slotSwitchSession(); void slotDeleteSession(); void slotSessionDoubleClicked(QModelIndex item); void slotClose(); void slotModelAboutToBeReset(QModelIndex); void slotModelReset(); + void slotModelSelectionChanged(QItemSelection selected, QItemSelection deselected); private: + void updateButtons(); KisSessionResourceSP getSelectedSession() const; int m_lastSessionId; KisResourceModel* m_model; }; #endif