aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/advanceddockingsystem/workspaceinputdialog.cpp
blob: 56e7543323170e7f0ffa502a6ee324587df0f010 (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-2.1-or-later OR GPL-3.0-or-later

#include "workspaceinputdialog.h"

#include "advanceddockingsystemtr.h"
#include "dockmanager.h"

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

#include <QDialogButtonBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRegularExpression>
#include <QValidator>

namespace ADS {

class WorkspaceValidator : public QValidator
{
public:
    WorkspaceValidator(QObject *parent = nullptr);
    QValidator::State validate(QString &input, int &pos) const override;
};

WorkspaceValidator::WorkspaceValidator(QObject *parent)
    : QValidator(parent)
{}

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 (input.isEmpty())
        return QValidator::Intermediate;

    return QValidator::Acceptable;
}

WorkspaceNameInputDialog::WorkspaceNameInputDialog(DockManager *manager, QWidget *parent)
    : QDialog(parent)
    , m_manager(manager)
{
    auto label = new QLabel(Tr::tr("Enter the name of the workspace:"), this);
    m_newWorkspaceLineEdit = new QLineEdit(this);
    m_newWorkspaceLineEdit->setValidator(new WorkspaceValidator(this));
    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);

    m_okButton->setEnabled(false);
    m_switchToButton->setEnabled(false);

    connect(m_newWorkspaceLineEdit, &QLineEdit::textChanged, this, [this](const QString &) {
        m_okButton->setEnabled(m_newWorkspaceLineEdit->hasAcceptableInput());
        m_switchToButton->setEnabled(m_newWorkspaceLineEdit->hasAcceptableInput());
    });

    using namespace Utils::Layouting;

    Column {
        label,
        m_newWorkspaceLineEdit,
        buttons
    }.attachTo(this);
}

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

} // namespace ADS