aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/projectintropage.cpp
blob: 1f32cce6bcc775626839db634d9854ccaec2a11c (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "projectintropage.h"

#include "fancylineedit.h"
#include "filenamevalidatinglineedit.h"
#include "fileutils.h"
#include "infolabel.h"
#include "layoutbuilder.h"
#include "pathchooser.h"
#include "utilstr.h"
#include "wizard.h"

#include <QApplication>
#include <QCheckBox>
#include <QComboBox>
#include <QDir>
#include <QFrame>
#include <QLabel>
#include <QSpacerItem>

/*!
    \class Utils::ProjectIntroPage
    \inmodule QtCreator

    \brief The ProjectIntroPage class is the standard wizard page for a project,
    letting the user choose its name
    and path.

    Looks similar to FileWizardPage, but provides additional
    functionality:
    \list
    \li Contains a description label at the top for displaying introductory text.
    \li Does on the fly validation (connected to changed()) and displays
       warnings and errors in a status label at the bottom (the page is complete
       when fully validated, validatePage() is thus not implemented).
    \endlist

    \note Careful when changing projectintropage.ui. It must have main
    geometry cleared and QLayout::SetMinimumSize constraint on the main
    layout, otherwise, QWizard will squeeze it due to its strange expanding
    hacks.
*/

namespace Utils {

class ProjectIntroPagePrivate
{
public:
    bool m_complete = false;
    QRegularExpressionValidator m_projectNameValidator;
    QString m_projectNameValidatorUserMessage;
    bool m_forceSubProject = false;
    FilePaths m_projectDirectories;

    QLabel *m_descriptionLabel;
    FancyLineEdit *m_nameLineEdit;
    PathChooser *m_pathChooser;
    QCheckBox *m_projectsDirectoryCheckBox;
    QLabel *m_projectLabel;
    QComboBox *m_projectComboBox;
    InfoLabel *m_stateLabel;
};

ProjectIntroPage::ProjectIntroPage(QWidget *parent) :
    WizardPage(parent),
    d(new ProjectIntroPagePrivate)
{
    setTitle(Tr::tr("Introduction and Project Location"));

    d->m_descriptionLabel = new QLabel(this);
    d->m_descriptionLabel->setWordWrap(true);

    auto frame = new QFrame(this);
    frame->setFrameShape(QFrame::StyledPanel);
    frame->setFrameShadow(QFrame::Raised);

    d->m_nameLineEdit = new Utils::FancyLineEdit(frame);

    d->m_pathChooser = new Utils::PathChooser(frame);
    d->m_pathChooser->setObjectName("baseFolder"); // used by Squish
    d->m_pathChooser->setExpectedKind(PathChooser::Directory);
    d->m_pathChooser->setDisabled(d->m_forceSubProject);
    d->m_pathChooser->setAllowPathFromDevice(true);

    d->m_projectsDirectoryCheckBox = new QCheckBox(Tr::tr("Use as default project location"));
    d->m_projectsDirectoryCheckBox->setObjectName("projectsDirectoryCheckBox");
    d->m_projectsDirectoryCheckBox->setDisabled(d->m_forceSubProject);

    d->m_projectComboBox = new QComboBox;
    d->m_projectComboBox->setVisible(d->m_forceSubProject);

    d->m_stateLabel = new Utils::InfoLabel(this);
    d->m_stateLabel->setObjectName("stateLabel");
    d->m_stateLabel->setWordWrap(true);
    d->m_stateLabel->setFilled(true);
    hideStatusLabel();

    d->m_nameLineEdit->setPlaceholderText(Tr::tr("Enter project name"));
    d->m_nameLineEdit->setObjectName("nameLineEdit");
    d->m_nameLineEdit->setFocus();
    d->m_nameLineEdit->setValidationFunction([this](FancyLineEdit *edit, QString *errorString) {
        return validateProjectName(edit->text(), errorString);
    });

    d->m_projectLabel = new QLabel("Project:");
    d->m_projectLabel->setVisible(d->m_forceSubProject);

    using namespace Layouting;

    Form {
        Tr::tr("Name:"), d->m_nameLineEdit, br,
        d->m_projectLabel, d->m_projectComboBox, br,
        Column { Space(12) }, br,
        Tr::tr("Create in:"), d->m_pathChooser, br,
        Span(2, d->m_projectsDirectoryCheckBox)
    }.attachTo(frame);

    Column {
        d->m_descriptionLabel,
        st,
        frame,
        d->m_stateLabel
    }.attachTo(this);

    connect(d->m_pathChooser, &PathChooser::textChanged,
            this, &ProjectIntroPage::slotChanged);
    connect(d->m_nameLineEdit, &QLineEdit::textChanged,
            this, &ProjectIntroPage::slotChanged);
    connect(d->m_pathChooser, &PathChooser::validChanged,
            this, &ProjectIntroPage::slotChanged);
    connect(d->m_pathChooser, &PathChooser::returnPressed,
            this, &ProjectIntroPage::slotActivated);
    connect(d->m_nameLineEdit, &FancyLineEdit::validReturnPressed,
            this, &ProjectIntroPage::slotActivated);
    connect(d->m_projectComboBox, &QComboBox::currentIndexChanged,
            this, &ProjectIntroPage::slotChanged);

    setProperty(SHORT_TITLE_PROPERTY, Tr::tr("Location"));
    registerFieldWithName(QLatin1String("Path"), d->m_pathChooser, "path", SIGNAL(textChanged(QString)));
    registerFieldWithName(QLatin1String("ProjectName"), d->m_nameLineEdit);
}

ProjectIntroPage::~ProjectIntroPage()
{
    delete d;
}

QString ProjectIntroPage::projectName() const
{
    return d->m_nameLineEdit->text();
}

FilePath ProjectIntroPage::filePath() const
{
    return d->m_pathChooser->filePath();
}

void ProjectIntroPage::setFilePath(const FilePath &path)
{
    d->m_pathChooser->setFilePath(path);
}

void ProjectIntroPage::setProjectNameRegularExpression(const QRegularExpression &regEx, const QString &userErrorMessage)
{
    Q_ASSERT_X(regEx.isValid(), Q_FUNC_INFO, qPrintable(regEx.errorString()));
    d->m_projectNameValidator.setRegularExpression(regEx);
    d->m_projectNameValidatorUserMessage = userErrorMessage;
}

void ProjectIntroPage::setProjectName(const QString &name)
{
    d->m_nameLineEdit->setText(name);
    d->m_nameLineEdit->selectAll();
}

QString ProjectIntroPage::description() const
{
    return d->m_descriptionLabel->text();
}

void ProjectIntroPage::setDescription(const QString &description)
{
    d->m_descriptionLabel->setText(description);
}

bool ProjectIntroPage::isComplete() const
{
    return d->m_complete;
}

bool ProjectIntroPage::validate()
{
    if (d->m_forceSubProject) {
        int index = d->m_projectComboBox->currentIndex();
        if (index == 0)
            return false;
        d->m_pathChooser->setFilePath(d->m_projectDirectories.at(index));
    }
    // Validate and display status
    if (!d->m_pathChooser->isValid()) {
        displayStatusMessage(InfoLabel::Error, d->m_pathChooser->errorMessage());
        return false;
    }

    // Name valid?
    switch (d->m_nameLineEdit->state()) {
    case FancyLineEdit::Invalid:
        displayStatusMessage(InfoLabel::Error, d->m_nameLineEdit->errorMessage());
        return false;
    case FancyLineEdit::DisplayingPlaceholderText:
        displayStatusMessage(InfoLabel::Error, Tr::tr("Name is empty."));
        return false;
    case FancyLineEdit::Valid:
        break;
    }

    // Check existence of the directory
    const FilePath projectDir =
        filePath().pathAppended(QDir::fromNativeSeparators(d->m_nameLineEdit->text()));

    if (!projectDir.exists()) { // All happy
        if (!d->m_pathChooser->filePath().exists()) {
            displayStatusMessage(InfoLabel::Information, Tr::tr("Directory \"%1\" will be created.")
                                 .arg(d->m_pathChooser->filePath().toUserOutput()));
        } else {
            hideStatusLabel();
        }
        return true;
    }

    if (projectDir.isDir()) {
        displayStatusMessage(InfoLabel::Warning, Tr::tr("The project already exists."));
        return true;
    }
    // Not a directory, but something else, likely causing directory creation to fail
    displayStatusMessage(InfoLabel::Error, Tr::tr("A file with that name already exists."));
    return false;
}

void ProjectIntroPage::fieldsUpdated()
{
    slotChanged();
}

void ProjectIntroPage::slotChanged()
{
    const bool newComplete = validate();
    if (newComplete != d->m_complete) {
        d->m_complete = newComplete;
        emit completeChanged();
    }
}

void ProjectIntroPage::slotActivated()
{
    if (d->m_complete)
        emit activated();
}

bool ProjectIntroPage::forceSubProject() const
{
    return d->m_forceSubProject;
}

void ProjectIntroPage::setForceSubProject(bool force)
{
    d->m_forceSubProject = force;
    d->m_projectLabel->setVisible(d->m_forceSubProject);
    d->m_projectComboBox->setVisible(d->m_forceSubProject);
    d->m_pathChooser->setDisabled(d->m_forceSubProject);
    d->m_projectsDirectoryCheckBox->setDisabled(d->m_forceSubProject);
}

void ProjectIntroPage::setProjectList(const QStringList &projectList)
{
    d->m_projectComboBox->clear();
    d->m_projectComboBox->addItems(projectList);
}

void ProjectIntroPage::setProjectDirectories(const FilePaths &directoryList)
{
    d->m_projectDirectories = directoryList;
}

int ProjectIntroPage::projectIndex() const
{
    return d->m_projectComboBox->currentIndex();
}

bool ProjectIntroPage::validateProjectName(const QString &name, QString *errorMessage)
{
    int pos = -1;
    // if we have a pattern it was set
    if (!d->m_projectNameValidator.regularExpression().pattern().isEmpty()) {
        if (name.isEmpty()) {
            if (errorMessage)
                *errorMessage = Tr::tr("Name is empty.");
            return false;
        }
        // pos is set by reference
        QString tmp = name;
        QValidator::State validatorState = d->m_projectNameValidator.validate(tmp, pos);

        // if pos is set by validate it is cought at the bottom where it shows
        // a more detailed error message
        if (validatorState != QValidator::Acceptable && (pos == -1 || pos >= name.size())) {
            if (errorMessage) {
                if (d->m_projectNameValidatorUserMessage.isEmpty())
                    *errorMessage = Tr::tr("Project name is invalid.");
                else
                    *errorMessage = d->m_projectNameValidatorUserMessage;
            }
            return false;
        }
    } else { // no validator means usually a qmake project
        // Validation is file name + checking for dots
        if (!FileNameValidatingLineEdit::validateFileName(name, false, errorMessage))
            return false;
        if (name.contains(QLatin1Char('.'))) {
            if (errorMessage)
                *errorMessage = Tr::tr("Invalid character \".\".");
            return false;
        }
        pos = FileUtils::indexOfQmakeUnfriendly(name);
    }
    if (pos >= 0) {
        if (errorMessage)
            *errorMessage = Tr::tr("Invalid character \"%1\" found.").arg(name.at(pos));
        return false;
    }
    return true;
}

void ProjectIntroPage::displayStatusMessage(InfoLabel::InfoType t, const QString &s)
{
    d->m_stateLabel->setType(t);
    d->m_stateLabel->setText(s);

    emit statusMessageChanged(t, s);
}

void ProjectIntroPage::hideStatusLabel()
{
    displayStatusMessage(InfoLabel::None, {});
}

bool ProjectIntroPage::useAsDefaultPath() const
{
    return d->m_projectsDirectoryCheckBox->isChecked();
}

void ProjectIntroPage::setUseAsDefaultPath(bool u)
{
    d->m_projectsDirectoryCheckBox->setChecked(u);
}

} // namespace Utils