From 4197746095f6684f19056e0dac583108add75686 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Thu, 9 May 2019 16:21:42 +0200 Subject: [PATCH] ProjectExplorer: Let users delete several sessions at once Task-number: QTCREATORBUG-17668 Change-Id: I90dea6721ef8e7c1496f3a2c934f74edb62bbc96 Reviewed-by: hjk --- .../projectexplorer/projectwelcomepage.cpp | 2 +- src/plugins/projectexplorer/session.cpp | 16 ++++++-- src/plugins/projectexplorer/session.h | 3 +- src/plugins/projectexplorer/sessiondialog.cpp | 24 +++++++----- src/plugins/projectexplorer/sessiondialog.h | 2 +- src/plugins/projectexplorer/sessionmodel.cpp | 8 ++-- src/plugins/projectexplorer/sessionmodel.h | 4 +- src/plugins/projectexplorer/sessionview.cpp | 38 +++++++++++++------ src/plugins/projectexplorer/sessionview.h | 7 +++- 9 files changed, 69 insertions(+), 35 deletions(-) diff --git a/src/plugins/projectexplorer/projectwelcomepage.cpp b/src/plugins/projectexplorer/projectwelcomepage.cpp index 8d7896532d0..7827034c2af 100644 --- a/src/plugins/projectexplorer/projectwelcomepage.cpp +++ b/src/plugins/projectexplorer/projectwelcomepage.cpp @@ -399,7 +399,7 @@ public: else if (m_activeActionRects[1].contains(pos)) sessionModel->renameSession(ICore::mainWindow(), sessionName); else if (m_activeActionRects[2].contains(pos)) - sessionModel->deleteSession(sessionName); + sessionModel->deleteSessions(QStringList(sessionName)); return true; } } diff --git a/src/plugins/projectexplorer/session.cpp b/src/plugins/projectexplorer/session.cpp index 91110d35c3d..3ff82eefd02 100644 --- a/src/plugins/projectexplorer/session.cpp +++ b/src/plugins/projectexplorer/session.cpp @@ -792,11 +792,15 @@ bool SessionManager::renameSession(const QString &original, const QString &newNa /*! \brief Shows a dialog asking the user to confirm deleting the session \p session */ -bool SessionManager::confirmSessionDelete(const QString &session) +bool SessionManager::confirmSessionDelete(const QStringList &sessions) { + const QString title = sessions.size() == 1 ? tr("Delete Session") : tr("Delete Sessions"); + const QString question = sessions.size() == 1 + ? tr("Delete session %1?").arg(sessions.first()) + : tr("Delete these sessions?\n %1").arg(sessions.join("\n ")); return QMessageBox::question(ICore::mainWindow(), - tr("Delete Session"), - tr("Delete session %1?").arg(session), + title, + question, QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes; } @@ -814,6 +818,12 @@ bool SessionManager::deleteSession(const QString &session) return false; } +void SessionManager::deleteSessions(const QStringList &sessions) +{ + for (const QString &session : sessions) + deleteSession(session); +} + bool SessionManager::cloneSession(const QString &original, const QString &clone) { if (!d->m_sessions.contains(original)) diff --git a/src/plugins/projectexplorer/session.h b/src/plugins/projectexplorer/session.h index 3a49be83082..21800faa5be 100644 --- a/src/plugins/projectexplorer/session.h +++ b/src/plugins/projectexplorer/session.h @@ -63,8 +63,9 @@ public: static bool createSession(const QString &session); - static bool confirmSessionDelete(const QString &session); + static bool confirmSessionDelete(const QStringList &sessions); static bool deleteSession(const QString &session); + static void deleteSessions(const QStringList &sessions); static bool cloneSession(const QString &original, const QString &clone); static bool renameSession(const QString &original, const QString &newName); diff --git a/src/plugins/projectexplorer/sessiondialog.cpp b/src/plugins/projectexplorer/sessiondialog.cpp index 2dfb944a355..6cda77927ba 100644 --- a/src/plugins/projectexplorer/sessiondialog.cpp +++ b/src/plugins/projectexplorer/sessiondialog.cpp @@ -26,6 +26,8 @@ #include "sessiondialog.h" #include "session.h" +#include + #include #include @@ -131,7 +133,7 @@ SessionDialog::SessionDialog(QWidget *parent) : QDialog(parent) connect(m_ui.btClone, &QAbstractButton::clicked, m_ui.sessionView, &SessionView::cloneCurrentSession); connect(m_ui.btDelete, &QAbstractButton::clicked, - m_ui.sessionView, &SessionView::deleteCurrentSession); + m_ui.sessionView, &SessionView::deleteSelectedSessions); connect(m_ui.btSwitch, &QAbstractButton::clicked, m_ui.sessionView, &SessionView::switchToCurrentSession); connect(m_ui.btRename, &QAbstractButton::clicked, @@ -157,21 +159,23 @@ bool SessionDialog::autoLoadSession() const return m_ui.autoLoadCheckBox->checkState() == Qt::Checked; } -void SessionDialog::updateActions(const QString &session) +void SessionDialog::updateActions(const QStringList &sessions) { - if (session.isEmpty()) { + if (sessions.isEmpty()) { m_ui.btDelete->setEnabled(false); m_ui.btRename->setEnabled(false); m_ui.btClone->setEnabled(false); m_ui.btSwitch->setEnabled(false); - } else { - bool isDefault = (session == QLatin1String("default")); - bool isActive = (session == SessionManager::activeSession()); - m_ui.btDelete->setEnabled(!isActive && !isDefault); - m_ui.btRename->setEnabled(!isDefault); - m_ui.btClone->setEnabled(true); - m_ui.btSwitch->setEnabled(true); + return; } + const bool defaultIsSelected = sessions.contains("default"); + const bool activeIsSelected = Utils::anyOf(sessions, [](const QString &session) { + return session == SessionManager::activeSession(); + }); + m_ui.btDelete->setEnabled(!defaultIsSelected && !activeIsSelected); + m_ui.btRename->setEnabled(sessions.size() == 1 && !defaultIsSelected); + m_ui.btClone->setEnabled(sessions.size() == 1); + m_ui.btSwitch->setEnabled(sessions.size() == 1); } } // namespace Internal diff --git a/src/plugins/projectexplorer/sessiondialog.h b/src/plugins/projectexplorer/sessiondialog.h index 654736694ed..701748b55c0 100644 --- a/src/plugins/projectexplorer/sessiondialog.h +++ b/src/plugins/projectexplorer/sessiondialog.h @@ -49,7 +49,7 @@ public: bool autoLoadSession() const; private: - void updateActions(const QString &session); + void updateActions(const QStringList &sessions); Ui::SessionDialog m_ui; }; diff --git a/src/plugins/projectexplorer/sessionmodel.cpp b/src/plugins/projectexplorer/sessionmodel.cpp index 21b3ec3f48a..bcf13e96ddf 100644 --- a/src/plugins/projectexplorer/sessionmodel.cpp +++ b/src/plugins/projectexplorer/sessionmodel.cpp @@ -55,7 +55,7 @@ int SessionModel::indexOfSession(const QString &session) return SessionManager::sessions().indexOf(session); } -QString SessionModel::sessionAt(int row) +QString SessionModel::sessionAt(int row) const { return SessionManager::sessions().value(row, QString()); } @@ -209,12 +209,12 @@ void SessionModel::cloneSession(QWidget *parent, const QString &session) }); } -void SessionModel::deleteSession(const QString &session) +void SessionModel::deleteSessions(const QStringList &sessions) { - if (!SessionManager::confirmSessionDelete(session)) + if (!SessionManager::confirmSessionDelete(sessions)) return; beginResetModel(); - SessionManager::deleteSession(session); + SessionManager::deleteSessions(sessions); endResetModel(); } diff --git a/src/plugins/projectexplorer/sessionmodel.h b/src/plugins/projectexplorer/sessionmodel.h index 7feff2b433e..63f99842150 100644 --- a/src/plugins/projectexplorer/sessionmodel.h +++ b/src/plugins/projectexplorer/sessionmodel.h @@ -53,7 +53,7 @@ public: explicit SessionModel(QObject *parent = nullptr); int indexOfSession(const QString &session); - QString sessionAt(int row); + QString sessionAt(int row) const; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; @@ -72,7 +72,7 @@ public slots: void resetSessions(); void newSession(QWidget *parent); void cloneSession(QWidget *parent, const QString &session); - void deleteSession(const QString &session); + void deleteSessions(const QStringList &sessions); void renameSession(QWidget *parent, const QString &session); void switchToSession(const QString &session); diff --git a/src/plugins/projectexplorer/sessionview.cpp b/src/plugins/projectexplorer/sessionview.cpp index a28988836b5..07d918ca779 100644 --- a/src/plugins/projectexplorer/sessionview.cpp +++ b/src/plugins/projectexplorer/sessionview.cpp @@ -27,9 +27,12 @@ #include "session.h" -#include -#include +#include + #include +#include +#include +#include namespace ProjectExplorer { namespace Internal { @@ -57,7 +60,7 @@ SessionView::SessionView(QWidget *parent) { setItemDelegate(new RemoveItemFocusDelegate(this)); setSelectionBehavior(QAbstractItemView::SelectRows); - setSelectionMode(QAbstractItemView::SingleSelection); + setSelectionMode(QAbstractItemView::ExtendedSelection); setWordWrap(false); setRootIsDecorated(false); @@ -74,9 +77,8 @@ SessionView::SessionView(QWidget *parent) connect(this, &Utils::TreeView::activated, [this](const QModelIndex &index){ emit activated(m_sessionModel.sessionAt(index.row())); }); - connect(selectionModel(), &QItemSelectionModel::currentRowChanged, [this] - (const QModelIndex &index) { - emit selected(m_sessionModel.sessionAt(index.row())); + connect(selectionModel(), &QItemSelectionModel::selectionChanged, [this] { + emit selected(selectedSessions()); }); connect(&m_sessionModel, &SessionModel::sessionSwitched, @@ -92,9 +94,14 @@ void SessionView::createNewSession() m_sessionModel.newSession(this); } -void SessionView::deleteCurrentSession() +void SessionView::deleteSelectedSessions() { - m_sessionModel.deleteSession(currentSession()); + deleteSessions(selectedSessions()); +} + +void SessionView::deleteSessions(const QStringList &sessions) +{ + m_sessionModel.deleteSessions(sessions); } void SessionView::cloneCurrentSession() @@ -147,9 +154,18 @@ void SessionView::keyPressEvent(QKeyEvent *event) TreeView::keyPressEvent(event); return; } - const QString session = currentSession(); - if (!session.isEmpty() && session != "default" && session != SessionManager::activeSession()) - deleteCurrentSession(); + const QStringList sessions = selectedSessions(); + if (!sessions.contains("default") && !Utils::anyOf(sessions, + [](const QString &session) { return session == SessionManager::activeSession(); })) { + deleteSessions(sessions); + } +} + +QStringList SessionView::selectedSessions() const +{ + return Utils::transform(selectionModel()->selectedRows(), [this](const QModelIndex &index) { + return m_sessionModel.sessionAt(index.row()); + }); } } // namespace Internal diff --git a/src/plugins/projectexplorer/sessionview.h b/src/plugins/projectexplorer/sessionview.h index cab80dffb6a..21988c38383 100644 --- a/src/plugins/projectexplorer/sessionview.h +++ b/src/plugins/projectexplorer/sessionview.h @@ -41,7 +41,7 @@ public: explicit SessionView(QWidget *parent = nullptr); void createNewSession(); - void deleteCurrentSession(); + void deleteSelectedSessions(); void cloneCurrentSession(); void renameCurrentSession(); void switchToCurrentSession(); @@ -53,13 +53,16 @@ public: signals: void activated(const QString &session); - void selected(const QString &session); + void selected(const QStringList &sessions); void sessionSwitched(); private: void showEvent(QShowEvent* event) override; void keyPressEvent(QKeyEvent *event) override; + void deleteSessions(const QStringList &sessions); + QStringList selectedSessions() const; + SessionModel m_sessionModel; };