Files
qt-creator/src/plugins/projectexplorer/sessiondialog.cpp

183 lines
6.1 KiB
C++
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator.
2008-12-02 12:01:29 +01:00
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
2010-12-17 16:01:08 +01:00
**
****************************************************************************/
2008-12-02 16:19:05 +01:00
2008-12-02 12:01:29 +01:00
#include "sessiondialog.h"
#include "session.h"
#include <utils/algorithm.h>
#include <QInputDialog>
#include <QValidator>
2008-12-02 12:01:29 +01:00
using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;
namespace ProjectExplorer {
namespace Internal {
class SessionValidator : public QValidator
{
public:
SessionValidator(QObject *parent, const QStringList &sessions);
void fixup(QString & input) const override;
QValidator::State validate(QString & input, int & pos) const override;
2008-12-02 12:01:29 +01:00
private:
QStringList m_sessions;
};
SessionValidator::SessionValidator(QObject *parent, const QStringList &sessions)
2008-12-02 12:01:29 +01:00
: QValidator(parent), m_sessions(sessions)
{
}
QValidator::State SessionValidator::validate(QString &input, int &pos) const
{
Q_UNUSED(pos)
if (input.contains(QLatin1Char('/'))
|| input.contains(QLatin1Char(':'))
|| input.contains(QLatin1Char('\\'))
|| input.contains(QLatin1Char('?'))
|| input.contains(QLatin1Char('*')))
return QValidator::Invalid;
2008-12-02 12:01:29 +01:00
if (m_sessions.contains(input))
return QValidator::Intermediate;
else
return QValidator::Acceptable;
}
void SessionValidator::fixup(QString &input) const
{
int i = 2;
QString copy;
do {
copy = input + QLatin1String(" (") + QString::number(i) + QLatin1Char(')');
2008-12-02 12:01:29 +01:00
++i;
} while (m_sessions.contains(copy));
input = copy;
}
SessionNameInputDialog::SessionNameInputDialog(QWidget *parent)
: QDialog(parent)
2008-12-02 12:01:29 +01:00
{
auto hlayout = new QVBoxLayout(this);
auto label = new QLabel(tr("Enter the name of the session:"), this);
2008-12-02 12:01:29 +01:00
hlayout->addWidget(label);
m_newSessionLineEdit = new QLineEdit(this);
m_newSessionLineEdit->setValidator(new SessionValidator(this, SessionManager::sessions()));
2008-12-02 12:01:29 +01:00
hlayout->addWidget(m_newSessionLineEdit);
auto buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
m_okButton = buttons->button(QDialogButtonBox::Ok);
m_switchToButton = new QPushButton;
buttons->addButton(m_switchToButton, QDialogButtonBox::AcceptRole);
connect(m_switchToButton, &QPushButton::clicked, this, [this] {
m_usedSwitchTo = true;
});
connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
2008-12-02 12:01:29 +01:00
hlayout->addWidget(buttons);
setLayout(hlayout);
}
void SessionNameInputDialog::setActionText(const QString &actionText, const QString &openActionText)
{
m_okButton->setText(actionText);
m_switchToButton->setText(openActionText);
}
void SessionNameInputDialog::setValue(const QString &value)
{
m_newSessionLineEdit->setText(value);
}
QString SessionNameInputDialog::value() const
2008-12-02 12:01:29 +01:00
{
return m_newSessionLineEdit->text();
}
bool SessionNameInputDialog::isSwitchToRequested() const
{
return m_usedSwitchTo;
}
SessionDialog::SessionDialog(QWidget *parent) : QDialog(parent)
2008-12-02 12:01:29 +01:00
{
m_ui.setupUi(this);
m_ui.sessionView->setActivationMode(Utils::DoubleClickActivation);
2008-12-02 12:01:29 +01:00
connect(m_ui.btCreateNew, &QAbstractButton::clicked,
m_ui.sessionView, &SessionView::createNewSession);
connect(m_ui.btClone, &QAbstractButton::clicked,
m_ui.sessionView, &SessionView::cloneCurrentSession);
connect(m_ui.btDelete, &QAbstractButton::clicked,
m_ui.sessionView, &SessionView::deleteSelectedSessions);
connect(m_ui.btSwitch, &QAbstractButton::clicked,
m_ui.sessionView, &SessionView::switchToCurrentSession);
connect(m_ui.btRename, &QAbstractButton::clicked,
m_ui.sessionView, &SessionView::renameCurrentSession);
connect(m_ui.sessionView, &SessionView::sessionActivated,
m_ui.sessionView, &SessionView::switchToCurrentSession);
connect(m_ui.sessionView, &SessionView::sessionsSelected,
this, &SessionDialog::updateActions);
connect(m_ui.sessionView, &SessionView::sessionSwitched,
this, &QDialog::reject);
2008-12-02 12:01:29 +01:00
m_ui.whatsASessionLabel->setOpenExternalLinks(true);
}
void SessionDialog::setAutoLoadSession(bool check)
{
m_ui.autoLoadCheckBox->setChecked(check);
}
bool SessionDialog::autoLoadSession() const
{
return m_ui.autoLoadCheckBox->checkState() == Qt::Checked;
}
void SessionDialog::updateActions(const QStringList &sessions)
{
if (sessions.isEmpty()) {
m_ui.btDelete->setEnabled(false);
m_ui.btRename->setEnabled(false);
m_ui.btClone->setEnabled(false);
m_ui.btSwitch->setEnabled(false);
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);
2008-12-02 12:01:29 +01:00
}
2008-12-02 16:19:05 +01:00
} // namespace Internal
} // namespace ProjectExplorer