aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/sessiondialog.cpp
blob: 203901f0e918bb4bda0c653ec83bfa311ef6f44b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "sessiondialog.h"

#include "session.h"
#include "sessionview.h"

#include <utils/algorithm.h>
#include <utils/layoutbuilder.h>

#include <QCheckBox>
#include <QDialogButtonBox>
#include <QFrame>
#include <QInputDialog>
#include <QLabel>
#include <QPushButton>
#include <QValidator>

namespace ProjectExplorer::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;
private:
    QStringList m_sessions;
};

SessionValidator::SessionValidator(QObject *parent, const QStringList &sessions)
    : 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;

    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(')');
        ++i;
    } while (m_sessions.contains(copy));
    input = copy;
}

SessionNameInputDialog::SessionNameInputDialog(QWidget *parent)
    : QDialog(parent)
{
    m_newSessionLineEdit = new QLineEdit(this);
    m_newSessionLineEdit->setValidator(new SessionValidator(this, SessionManager::sessions()));

    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;
    });

    using namespace Utils::Layouting;
    Column {
        tr("Enter the name of the session:"),
        m_newSessionLineEdit,
        buttons,
    }.attachTo(this);

    connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
}

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
{
    return m_newSessionLineEdit->text();
}

bool SessionNameInputDialog::isSwitchToRequested() const
{
    return m_usedSwitchTo;
}

SessionDialog::SessionDialog(QWidget *parent) : QDialog(parent)
{
    resize(550, 400);
    setWindowTitle(tr("Session Manager"));


    auto sessionView = new SessionView(this);
    sessionView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    sessionView->setActivationMode(Utils::DoubleClickActivation);

    auto createNewButton = new QPushButton(tr("&New"));

    m_renameButton = new QPushButton(tr("&Rename"));
    m_cloneButton = new QPushButton(tr("C&lone"));
    m_deleteButton = new QPushButton(tr("&Delete"));
    m_switchButton = new QPushButton(tr("&Switch To"));

    m_autoLoadCheckBox = new QCheckBox(tr("Restore last session on startup"));

    auto line = new QFrame(this);
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);

    auto buttonBox = new QDialogButtonBox(this);
    buttonBox->setStandardButtons(QDialogButtonBox::Close);

    m_switchButton->setDefault(true);

    // FIXME: Simplify translator's work.
    auto whatsASessionLabel = new QLabel(
        tr("<a href=\"qthelp://org.qt-project.qtcreator/doc/creator-project-managing-sessions.html\">"
           "What is a Session?</a>"));
    whatsASessionLabel->setOpenExternalLinks(true);

    using namespace Utils::Layouting;

    Column {
        Row {
            sessionView,
            Column {
                createNewButton,
                m_renameButton,
                m_cloneButton,
                m_deleteButton,
                m_switchButton,
                st
            }
        },
        m_autoLoadCheckBox,
        line,
        Row { whatsASessionLabel, buttonBox },
    }.attachTo(this);

    connect(createNewButton, &QAbstractButton::clicked,
            sessionView, &SessionView::createNewSession);
    connect(m_cloneButton, &QAbstractButton::clicked,
            sessionView, &SessionView::cloneCurrentSession);
    connect(m_deleteButton, &QAbstractButton::clicked,
            sessionView, &SessionView::deleteSelectedSessions);
    connect(m_switchButton, &QAbstractButton::clicked,
            sessionView, &SessionView::switchToCurrentSession);
    connect(m_renameButton, &QAbstractButton::clicked,
            sessionView, &SessionView::renameCurrentSession);
    connect(sessionView, &SessionView::sessionActivated,
            sessionView, &SessionView::switchToCurrentSession);

    connect(sessionView, &SessionView::sessionsSelected,
            this, &SessionDialog::updateActions);
    connect(sessionView, &SessionView::sessionSwitched,
            this, &QDialog::reject);

    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
}

void SessionDialog::setAutoLoadSession(bool check)
{
    m_autoLoadCheckBox->setChecked(check);
}

bool SessionDialog::autoLoadSession() const
{
    return m_autoLoadCheckBox->checkState() == Qt::Checked;
}

void SessionDialog::updateActions(const QStringList &sessions)
{
    if (sessions.isEmpty()) {
        m_deleteButton->setEnabled(false);
        m_renameButton->setEnabled(false);
        m_cloneButton->setEnabled(false);
        m_switchButton->setEnabled(false);
        return;
    }
    const bool defaultIsSelected = sessions.contains("default");
    const bool activeIsSelected = Utils::anyOf(sessions, [](const QString &session) {
        return session == SessionManager::activeSession();
    });
    m_deleteButton->setEnabled(!defaultIsSelected && !activeIsSelected);
    m_renameButton->setEnabled(sessions.size() == 1 && !defaultIsSelected);
    m_cloneButton->setEnabled(sessions.size() == 1);
    m_switchButton->setEnabled(sessions.size() == 1);
}

} // ProjectExplorer::Internal