aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/advanceddockingsystem/workspacedialog.cpp
blob: 6594e05d7ca6d0c334c3e49355bf213a5cfa991c (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
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial

#include "workspacedialog.h"

#include "dockmanager.h"

#include <utils/algorithm.h>

#include <QInputDialog>
#include <QRegularExpression>
#include <QValidator>

namespace ADS {

class WorkspaceValidator : public QValidator
{
public:
    WorkspaceValidator(QObject *parent, const QStringList &workspaces);
    void fixup(QString &input) const override;
    QValidator::State validate(QString &input, int &pos) const override;

private:
    QStringList m_workspaces;
};

WorkspaceValidator::WorkspaceValidator(QObject *parent, const QStringList &workspaces)
    : QValidator(parent)
    , m_workspaces(workspaces)
{}

QValidator::State WorkspaceValidator::validate(QString &input, int &pos) const
{
    Q_UNUSED(pos)

    static const QRegularExpression rx("^[a-zA-Z0-9 ()\\-]*$");

    if (!rx.match(input).hasMatch())
        return QValidator::Invalid;

    if (m_workspaces.contains(input))
        return QValidator::Intermediate;
    else
        return QValidator::Acceptable;
}

void WorkspaceValidator::fixup(QString &input) const
{
    int i = 2;
    QString copy;
    do {
        copy = input + QLatin1String(" (") + QString::number(i) + QLatin1Char(')');
        ++i;
    } while (m_workspaces.contains(copy));
    input = copy;
}

WorkspaceNameInputDialog::WorkspaceNameInputDialog(DockManager *manager, QWidget *parent)
    : QDialog(parent)
    , m_manager(manager)
{
    auto hlayout = new QVBoxLayout(this);
    auto label = new QLabel(tr("Enter the name of the workspace:"), this);
    hlayout->addWidget(label);
    m_newWorkspaceLineEdit = new QLineEdit(this);
    m_newWorkspaceLineEdit->setValidator(new WorkspaceValidator(this, m_manager->workspaces()));
    hlayout->addWidget(m_newWorkspaceLineEdit);
    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]() { m_usedSwitchTo = true; });
    connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
    hlayout->addWidget(buttons);
    setLayout(hlayout);
}

void WorkspaceNameInputDialog::setActionText(const QString &actionText,
                                             const QString &openActionText)
{
    m_okButton->setText(actionText);
    m_switchToButton->setText(openActionText);
}

void WorkspaceNameInputDialog::setValue(const QString &value)
{
    m_newWorkspaceLineEdit->setText(value);
}

QString WorkspaceNameInputDialog::value() const
{
    return m_newWorkspaceLineEdit->text();
}

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

WorkspaceDialog::WorkspaceDialog(DockManager *manager, QWidget *parent)
    : QDialog(parent)
    , m_manager(manager)
{
    m_ui.setupUi(this);
    m_ui.workspaceView->setActivationMode(Utils::DoubleClickActivation);

    connect(m_ui.btCreateNew,
            &QAbstractButton::clicked,
            m_ui.workspaceView,
            &WorkspaceView::createNewWorkspace);
    connect(m_ui.btClone,
            &QAbstractButton::clicked,
            m_ui.workspaceView,
            &WorkspaceView::cloneCurrentWorkspace);
    connect(m_ui.btDelete,
            &QAbstractButton::clicked,
            m_ui.workspaceView,
            &WorkspaceView::deleteSelectedWorkspaces);
    connect(m_ui.btSwitch,
            &QAbstractButton::clicked,
            m_ui.workspaceView,
            &WorkspaceView::switchToCurrentWorkspace);
    connect(m_ui.btRename,
            &QAbstractButton::clicked,
            m_ui.workspaceView,
            &WorkspaceView::renameCurrentWorkspace);
    connect(m_ui.btReset,
            &QAbstractButton::clicked,
            m_ui.workspaceView,
            &WorkspaceView::resetCurrentWorkspace);
    connect(m_ui.workspaceView,
            &WorkspaceView::workspaceActivated,
            m_ui.workspaceView,
            &WorkspaceView::switchToCurrentWorkspace);
    connect(m_ui.workspaceView,
            &WorkspaceView::workspacesSelected,
            this,
            &WorkspaceDialog::updateActions);
    connect(m_ui.btImport,
            &QAbstractButton::clicked,
            m_ui.workspaceView,
            &WorkspaceView::importWorkspace);
    connect(m_ui.btExport,
            &QAbstractButton::clicked,
            m_ui.workspaceView,
            &WorkspaceView::exportCurrentWorkspace);

    m_ui.whatsAWorkspaceLabel->setOpenExternalLinks(true);

   updateActions(m_ui.workspaceView->selectedWorkspaces());
}

void WorkspaceDialog::setAutoLoadWorkspace(bool check)
{
    m_ui.autoLoadCheckBox->setChecked(check);
}

bool WorkspaceDialog::autoLoadWorkspace() const
{
    return m_ui.autoLoadCheckBox->checkState() == Qt::Checked;
}

DockManager *WorkspaceDialog::dockManager() const
{
    return m_manager;
}

void WorkspaceDialog::updateActions(const QStringList &workspaces)
{
    if (workspaces.isEmpty()) {
        m_ui.btDelete->setEnabled(false);
        m_ui.btRename->setEnabled(false);
        m_ui.btClone->setEnabled(false);
        m_ui.btReset->setEnabled(false);
        m_ui.btSwitch->setEnabled(false);
        m_ui.btExport->setEnabled(false);
        return;
    }
    const bool presetIsSelected = Utils::anyOf(workspaces, [this](const QString &workspace) {
        return m_manager->isWorkspacePreset(workspace);
    });
    const bool activeIsSelected = Utils::anyOf(workspaces, [this](const QString &workspace) {
        return workspace == m_manager->activeWorkspace();
    });
    m_ui.btDelete->setEnabled(!activeIsSelected && !presetIsSelected);
    m_ui.btRename->setEnabled(workspaces.size() == 1 && !presetIsSelected);
    m_ui.btClone->setEnabled(workspaces.size() == 1);
    m_ui.btReset->setEnabled(presetIsSelected);
    m_ui.btSwitch->setEnabled(workspaces.size() == 1);
    m_ui.btExport->setEnabled(workspaces.size() == 1);
}

} // namespace ADS