forked from qt-creator/qt-creator
Sessions: prepare SessionModel for reuse
refactoring redundant code without a behavior change Change-Id: I264ee28ebf9b2d01899ece3f01c424032bda56ca Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -122,23 +122,16 @@ void SessionModel::resetSessions()
|
|||||||
endResetModel();
|
endResetModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SessionModel::newSession()
|
||||||
|
{
|
||||||
|
runNewSessionDialog("", &SessionManager::createSession);
|
||||||
|
}
|
||||||
|
|
||||||
void SessionModel::cloneSession(const QString &session)
|
void SessionModel::cloneSession(const QString &session)
|
||||||
{
|
{
|
||||||
SessionNameInputDialog newSessionInputDialog(SessionManager::sessions(), nullptr);
|
runNewSessionDialog(session + " (2)", [session](const QString &newName) {
|
||||||
newSessionInputDialog.setWindowTitle(tr("New Session Name"));
|
SessionManager::cloneSession(session, newName);
|
||||||
newSessionInputDialog.setValue(session + QLatin1String(" (2)"));
|
});
|
||||||
|
|
||||||
if (newSessionInputDialog.exec() == QDialog::Accepted) {
|
|
||||||
QString newSession = newSessionInputDialog.value();
|
|
||||||
if (newSession.isEmpty() || SessionManager::sessions().contains(newSession))
|
|
||||||
return;
|
|
||||||
beginResetModel();
|
|
||||||
SessionManager::cloneSession(session, newSession);
|
|
||||||
endResetModel();
|
|
||||||
|
|
||||||
if (newSessionInputDialog.isSwitchToRequested())
|
|
||||||
SessionManager::loadSession(newSession);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SessionModel::deleteSession(const QString &session)
|
void SessionModel::deleteSession(const QString &session)
|
||||||
@@ -151,21 +144,34 @@ void SessionModel::deleteSession(const QString &session)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SessionModel::renameSession(const QString &session)
|
void SessionModel::renameSession(const QString &session)
|
||||||
|
{
|
||||||
|
runNewSessionDialog(session, [session](const QString &newName) {
|
||||||
|
SessionManager::cloneSession(session, newName);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void SessionModel::switchToSession(const QString &session)
|
||||||
|
{
|
||||||
|
SessionManager::loadSession(session);
|
||||||
|
emit sessionSwitched();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SessionModel::runNewSessionDialog(const QString &suggestedName, std::function<void(const QString &)> createSession)
|
||||||
{
|
{
|
||||||
SessionNameInputDialog newSessionInputDialog(SessionManager::sessions(), nullptr);
|
SessionNameInputDialog newSessionInputDialog(SessionManager::sessions(), nullptr);
|
||||||
newSessionInputDialog.setWindowTitle(tr("New Session Name"));
|
newSessionInputDialog.setWindowTitle(tr("New Session Name"));
|
||||||
newSessionInputDialog.setValue(session);
|
newSessionInputDialog.setValue(suggestedName);
|
||||||
|
|
||||||
if (newSessionInputDialog.exec() == QDialog::Accepted) {
|
if (newSessionInputDialog.exec() == QDialog::Accepted) {
|
||||||
QString newSession = newSessionInputDialog.value();
|
QString newSession = newSessionInputDialog.value();
|
||||||
if (newSession.isEmpty() || SessionManager::sessions().contains(newSession))
|
if (newSession.isEmpty() || SessionManager::sessions().contains(newSession))
|
||||||
return;
|
return;
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
SessionManager::renameSession(session, newSession);
|
createSession(newSession);
|
||||||
endResetModel();
|
endResetModel();
|
||||||
|
|
||||||
if (newSessionInputDialog.isSwitchToRequested())
|
if (newSessionInputDialog.isSwitchToRequested())
|
||||||
SessionManager::loadSession(newSession);
|
switchToSession(newSession);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
|||||||
@@ -27,6 +27,8 @@
|
|||||||
|
|
||||||
#include <QAbstractListModel>
|
#include <QAbstractListModel>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace ProjectExplorer {
|
namespace ProjectExplorer {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
@@ -45,11 +47,19 @@ public:
|
|||||||
|
|
||||||
Q_SCRIPTABLE bool isDefaultVirgin() const;
|
Q_SCRIPTABLE bool isDefaultVirgin() const;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void sessionSwitched();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void resetSessions();
|
void resetSessions();
|
||||||
|
void newSession();
|
||||||
void cloneSession(const QString &session);
|
void cloneSession(const QString &session);
|
||||||
void deleteSession(const QString &session);
|
void deleteSession(const QString &session);
|
||||||
void renameSession(const QString &session);
|
void renameSession(const QString &session);
|
||||||
|
void switchToSession(const QString &session);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void runNewSessionDialog(const QString &suggestedName, std::function<void(const QString &)> createSession);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
|||||||
Reference in New Issue
Block a user