kitmanagerconfigwidget.cpp 11.7 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Tobias Hunger's avatar
Tobias Hunger committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Tobias Hunger's avatar
Tobias Hunger committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Tobias Hunger's avatar
Tobias Hunger committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Tobias Hunger's avatar
Tobias Hunger committed
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
Tobias Hunger's avatar
Tobias Hunger committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Tobias Hunger's avatar
Tobias Hunger committed
29

Tobias Hunger's avatar
Tobias Hunger committed
30
#include "kitmanagerconfigwidget.h"
31
#include "projectexplorerconstants.h"
Tobias Hunger's avatar
Tobias Hunger committed
32

Tobias Hunger's avatar
Tobias Hunger committed
33
#include "kit.h"
Tobias Hunger's avatar
Tobias Hunger committed
34
#include "kitmanager.h"
35
#include "task.h"
Tobias Hunger's avatar
Tobias Hunger committed
36

37
#include <utils/detailswidget.h>
hjk's avatar
hjk committed
38
#include <utils/qtcassert.h>
39
#include <utils/stringutils.h>
40

41
#include <QAction>
42
43
#include <QRegularExpression>
#include <QRegularExpressionValidator>
Tobias Hunger's avatar
Tobias Hunger committed
44
#include <QFileDialog>
Eike Ziller's avatar
Eike Ziller committed
45
46
#include <QGridLayout>
#include <QLabel>
47
#include <QLineEdit>
hjk's avatar
hjk committed
48
#include <QPainter>
Eike Ziller's avatar
Eike Ziller committed
49
#include <QToolButton>
50
#include <QScrollArea>
Tobias Hunger's avatar
Tobias Hunger committed
51
#include <QSizePolicy>
Eike Ziller's avatar
Eike Ziller committed
52
#include <QStyle>
Tobias Hunger's avatar
Tobias Hunger committed
53

Tobias Hunger's avatar
Tobias Hunger committed
54
55
static const char WORKING_COPY_KIT_ID[] = "modified kit";

Tobias Hunger's avatar
Tobias Hunger committed
56
57
58
namespace ProjectExplorer {
namespace Internal {

hjk's avatar
hjk committed
59
KitManagerConfigWidget::KitManagerConfigWidget(Kit *k) :
Eike Ziller's avatar
Eike Ziller committed
60
61
    m_layout(new QGridLayout),
    m_iconButton(new QToolButton),
62
    m_nameEdit(new QLineEdit),
63
    m_fileSystemFriendlyNameLineEdit(new QLineEdit),
Tobias Hunger's avatar
Tobias Hunger committed
64
65
    m_kit(k),
    m_modifiedKit(new Kit(Core::Id(WORKING_COPY_KIT_ID))),
66
67
    m_fixingKit(false),
    m_hasUniqueName(true)
Tobias Hunger's avatar
Tobias Hunger committed
68
{
69
70
71
    static const Qt::Alignment alignment
            = static_cast<Qt::Alignment>(style()->styleHint(QStyle::SH_FormLayoutLabelAlignment));

hjk's avatar
hjk committed
72
73
74
75
    m_layout->addWidget(m_nameEdit, 0, WidgetColumn);
    m_layout->addWidget(m_iconButton, 0, ButtonColumn);
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

76
77
78
79
80
81
82
83
    QString toolTip =
        tr("<html><head/><body><p>The name of the kit suitable for generating "
           "directory names. This value is used for the variable <i>%1</i>, "
           "which for example determines the name of the shadow build directory."
           "</p></body></html>").arg(QLatin1String(Constants::VAR_CURRENTKIT_FILESYSTEMNAME));
    QLabel *label = createLabel(tr("File system name:"), toolTip);
    m_layout->addWidget(label, 1, LabelColumn, alignment);
    m_fileSystemFriendlyNameLineEdit->setToolTip(toolTip);
84
    QRegularExpression fileSystemFriendlyNameRegexp(QLatin1String("^[A-Za-z0-9_-]*$"));
85
    Q_ASSERT(fileSystemFriendlyNameRegexp.isValid());
86
    m_fileSystemFriendlyNameLineEdit->setValidator(new QRegularExpressionValidator(fileSystemFriendlyNameRegexp, m_fileSystemFriendlyNameLineEdit));
87
88
89
    m_layout->addWidget(m_fileSystemFriendlyNameLineEdit, 1, WidgetColumn);
    connect(m_fileSystemFriendlyNameLineEdit, SIGNAL(textChanged(QString)), this, SLOT(setFileSystemFriendlyName()));

hjk's avatar
hjk committed
90
91
    QWidget *inner = new QWidget;
    inner->setLayout(m_layout);
92
93

    QScrollArea *scroll = new QScrollArea;
hjk's avatar
hjk committed
94
    scroll->setWidget(inner);
95
96
97
98
    scroll->setFrameShape(QFrame::NoFrame);
    scroll->setWidgetResizable(true);
    scroll->setFocusPolicy(Qt::NoFocus);

hjk's avatar
hjk committed
99
100
101
    QGridLayout *mainLayout = new QGridLayout(this);
    mainLayout->setMargin(1);
    mainLayout->addWidget(scroll, 0, 0);
102

103
104
    toolTip = tr("Kit name and icon.");
    label = createLabel(tr("Name:"), toolTip);
105
    m_layout->addWidget(label, 0, LabelColumn, alignment);
hjk's avatar
hjk committed
106
    m_iconButton->setToolTip(toolTip);
Tobias Hunger's avatar
Tobias Hunger committed
107
108
109
110

    discard();

    connect(m_iconButton, SIGNAL(clicked()), this, SLOT(setIcon()));
Tobias Hunger's avatar
Tobias Hunger committed
111
112
    connect(m_nameEdit, SIGNAL(textChanged(QString)), this, SLOT(setDisplayName()));

113
    QObject *km = KitManager::instance();
Tobias Hunger's avatar
Tobias Hunger committed
114
115
116
117
118
119
120
121
    connect(km, SIGNAL(unmanagedKitUpdated(ProjectExplorer::Kit*)),
            this, SLOT(workingCopyWasUpdated(ProjectExplorer::Kit*)));
    connect(km, SIGNAL(kitUpdated(ProjectExplorer::Kit*)),
            this, SLOT(kitWasUpdated(ProjectExplorer::Kit*)));
}

KitManagerConfigWidget::~KitManagerConfigWidget()
{
Tobias Hunger's avatar
Tobias Hunger committed
122
123
    qDeleteAll(m_widgets);
    m_widgets.clear();
124
125
    qDeleteAll(m_actions);
    m_actions.clear();
Tobias Hunger's avatar
Tobias Hunger committed
126

127
    KitManager::deleteKit(m_modifiedKit);
Tobias Hunger's avatar
Tobias Hunger committed
128
    // Make sure our workingCopy did not get registered somehow:
129
    foreach (const Kit *k, KitManager::kits())
hjk's avatar
hjk committed
130
        QTC_CHECK(k->id() != Core::Id(WORKING_COPY_KIT_ID));
Tobias Hunger's avatar
Tobias Hunger committed
131
132
}

Tobias Hunger's avatar
Tobias Hunger committed
133
QString KitManagerConfigWidget::displayName() const
Tobias Hunger's avatar
Tobias Hunger committed
134
{
135
    return m_modifiedKit->displayName();
Tobias Hunger's avatar
Tobias Hunger committed
136
137
}

Tobias Hunger's avatar
Tobias Hunger committed
138
void KitManagerConfigWidget::apply()
Tobias Hunger's avatar
Tobias Hunger committed
139
{
Jing Bai's avatar
Jing Bai committed
140
    bool mustSetDefault = m_isDefaultKit;
Tobias Hunger's avatar
Tobias Hunger committed
141
142
143
144
145
    bool mustRegister = false;
    if (!m_kit) {
        mustRegister = true;
        m_kit = new Kit;
    }
146
    m_kit->copyFrom(m_modifiedKit); //m_isDefaultKit is reset in discard() here.
Tobias Hunger's avatar
Tobias Hunger committed
147
    if (mustRegister)
148
        KitManager::registerKit(m_kit);
Tobias Hunger's avatar
Tobias Hunger committed
149

Jing Bai's avatar
Jing Bai committed
150
    if (mustSetDefault)
151
        KitManager::setDefaultKit(m_kit);
Jing Bai's avatar
Jing Bai committed
152
153

    m_isDefaultKit = mustSetDefault;
Tobias Hunger's avatar
Tobias Hunger committed
154
    emit dirty();
Tobias Hunger's avatar
Tobias Hunger committed
155
156
}

Tobias Hunger's avatar
Tobias Hunger committed
157
void KitManagerConfigWidget::discard()
Tobias Hunger's avatar
Tobias Hunger committed
158
{
Tobias Hunger's avatar
Tobias Hunger committed
159
160
    if (m_kit) {
        m_modifiedKit->copyFrom(m_kit);
161
        m_isDefaultKit = (m_kit == KitManager::defaultKit());
Tobias Hunger's avatar
Tobias Hunger committed
162
163
164
165
166
167
    } else {
        // This branch will only ever get reached once during setup of widget for a not-yet-existing
        // kit.
        m_isDefaultKit = false;
    }
    m_iconButton->setIcon(m_modifiedKit->icon());
168
    m_nameEdit->setText(m_modifiedKit->unexpandedDisplayName());
169
    m_fileSystemFriendlyNameLineEdit->setText(m_modifiedKit->customFileSystemFriendlyName());
Tobias Hunger's avatar
Tobias Hunger committed
170
    emit dirty();
Tobias Hunger's avatar
Tobias Hunger committed
171
172
}

Tobias Hunger's avatar
Tobias Hunger committed
173
bool KitManagerConfigWidget::isDirty() const
Tobias Hunger's avatar
Tobias Hunger committed
174
{
Tobias Hunger's avatar
Tobias Hunger committed
175
176
    return !m_kit
            || !m_kit->isEqual(m_modifiedKit)
177
            || m_isDefaultKit != (KitManager::defaultKit() == m_kit);
Tobias Hunger's avatar
Tobias Hunger committed
178
179
180
181
182
183
184
}

bool KitManagerConfigWidget::isValid() const
{
    return m_modifiedKit->isValid();
}

185
186
bool KitManagerConfigWidget::hasWarning() const
{
187
    return m_modifiedKit->hasWarning() || !m_hasUniqueName;
188
189
}

Tobias Hunger's avatar
Tobias Hunger committed
190
191
QString KitManagerConfigWidget::validityMessage() const
{
192
193
194
195
196
197
    QList<Task> tmp;
    if (!m_hasUniqueName) {
        tmp.append(Task(Task::Warning, tr("Display name is not unique."), Utils::FileName(), -1,
                        ProjectExplorer::Constants::TASK_CATEGORY_COMPILE));
    }
    return m_modifiedKit->toHtml(tmp);
Tobias Hunger's avatar
Tobias Hunger committed
198
199
}

Tobias Hunger's avatar
Tobias Hunger committed
200
void KitManagerConfigWidget::addConfigWidget(ProjectExplorer::KitConfigWidget *widget)
Tobias Hunger's avatar
Tobias Hunger committed
201
{
hjk's avatar
hjk committed
202
203
204
205
206
207
    QTC_ASSERT(widget, return);
    QTC_ASSERT(!m_widgets.contains(widget), return);

    QString name = widget->displayName();
    QString toolTip = widget->toolTip();

208
209
    QAction *action = new QAction(tr("Mark as Mutable"), 0);
    action->setCheckable(true);
Tobias Hunger's avatar
Tobias Hunger committed
210
    action->setData(QVariant::fromValue(qobject_cast<QObject *>(widget)));
211
212
213
214
215
216
217
    action->setChecked(widget->isMutable());
    action->setEnabled(!widget->isSticky());
    widget->mainWidget()->addAction(action);
    widget->mainWidget()->setContextMenuPolicy(Qt::ActionsContextMenu);
    connect(action, SIGNAL(toggled(bool)), this, SLOT(updateMutableState()));
    m_actions << action;

hjk's avatar
hjk committed
218
219
220
221
    int row = m_layout->rowCount();
    m_layout->addWidget(widget->mainWidget(), row, WidgetColumn);
    if (QWidget *button = widget->buttonWidget())
        m_layout->addWidget(button, row, ButtonColumn);
Tobias Hunger's avatar
Tobias Hunger committed
222

223
224
225
226
    static const Qt::Alignment alignment
        = static_cast<Qt::Alignment>(style()->styleHint(QStyle::SH_FormLayoutLabelAlignment));
    QLabel *label = createLabel(name, toolTip);
    m_layout->addWidget(label, row, LabelColumn, alignment);
Tobias Hunger's avatar
Tobias Hunger committed
227
    m_widgets.append(widget);
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    m_labels.append(label);
}

void KitManagerConfigWidget::updateVisibility()
{
    int count = m_widgets.count();
    for (int i = 0; i < count; ++i) {
        KitConfigWidget *widget = m_widgets.at(i);
        bool visible = widget->visibleInKit();
        widget->mainWidget()->setVisible(visible);
        if (widget->buttonWidget())
            widget->buttonWidget()->setVisible(visible);
        m_labels.at(i)->setVisible(visible);
    }
Tobias Hunger's avatar
Tobias Hunger committed
242
243
}

244
245
246
247
248
void KitManagerConfigWidget::setHasUniqueName(bool unique)
{
    m_hasUniqueName = unique;
}

249
void KitManagerConfigWidget::makeStickySubWidgetsReadOnly()
Tobias Hunger's avatar
Tobias Hunger committed
250
{
251
252
253
254
    foreach (KitConfigWidget *w, m_widgets) {
        if (w->isSticky())
            w->makeReadOnly();
    }
Tobias Hunger's avatar
Tobias Hunger committed
255
256
}

Tobias Hunger's avatar
Tobias Hunger committed
257
258
259
260
261
262
263
264
265
266
267
268
Kit *KitManagerConfigWidget::workingCopy() const
{
    return m_modifiedKit;
}

bool KitManagerConfigWidget::configures(Kit *k) const
{
    return m_kit == k;
}

void KitManagerConfigWidget::setIsDefaultKit(bool d)
{
Jing Bai's avatar
Jing Bai committed
269
    if (m_isDefaultKit == d)
Tobias Hunger's avatar
Tobias Hunger committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
        return;
    m_isDefaultKit = d;
    emit dirty();
}

bool KitManagerConfigWidget::isDefaultKit() const
{
    return m_isDefaultKit;
}

void KitManagerConfigWidget::removeKit()
{
    if (!m_kit)
        return;
284
    KitManager::deregisterKit(m_kit);
Tobias Hunger's avatar
Tobias Hunger committed
285
286
}

Tobias Hunger's avatar
Tobias Hunger committed
287
void KitManagerConfigWidget::setIcon()
Tobias Hunger's avatar
Tobias Hunger committed
288
{
289
290
291
292
    const Utils::FileName path = Utils::FileName::fromString(
                QFileDialog::getOpenFileName(this, tr("Select Icon"),
                                             m_modifiedKit->iconPath().toString(),
                                             tr("Images (*.png *.xpm *.jpg)")));
Tobias Hunger's avatar
Tobias Hunger committed
293
294
295
    if (path.isEmpty())
        return;

296
    const QIcon icon = Kit::icon(path);
Tobias Hunger's avatar
Tobias Hunger committed
297
298
299
300
    if (icon.isNull())
        return;

    m_iconButton->setIcon(icon);
Tobias Hunger's avatar
Tobias Hunger committed
301
    m_modifiedKit->setIconPath(path);
Tobias Hunger's avatar
Tobias Hunger committed
302
303
304
    emit dirty();
}

Tobias Hunger's avatar
Tobias Hunger committed
305
306
void KitManagerConfigWidget::setDisplayName()
{
307
    int pos = m_nameEdit->cursorPosition();
308
    m_modifiedKit->setUnexpandedDisplayName(m_nameEdit->text());
309
    m_nameEdit->setCursorPosition(pos);
Tobias Hunger's avatar
Tobias Hunger committed
310
311
}

312
313
314
315
316
317
318
void KitManagerConfigWidget::setFileSystemFriendlyName()
{
    const int pos = m_fileSystemFriendlyNameLineEdit->cursorPosition();
    m_modifiedKit->setCustomFileSystemFriendlyName(m_fileSystemFriendlyNameLineEdit->text());
    m_fileSystemFriendlyNameLineEdit->setCursorPosition(pos);
}

Tobias Hunger's avatar
Tobias Hunger committed
319
320
321
322
323
324
325
326
327
328
329
void KitManagerConfigWidget::workingCopyWasUpdated(Kit *k)
{
    if (k != m_modifiedKit || m_fixingKit)
        return;

    m_fixingKit = true;
    k->fix();
    m_fixingKit = false;

    foreach (KitConfigWidget *w, m_widgets)
        w->refresh();
330
331
332
333

    if (k->unexpandedDisplayName() != m_nameEdit->text())
        m_nameEdit->setText(k->unexpandedDisplayName());

334
    m_fileSystemFriendlyNameLineEdit->setText(k->customFileSystemFriendlyName());
Tobias Hunger's avatar
Tobias Hunger committed
335
    m_iconButton->setIcon(k->icon());
336
    updateVisibility();
Tobias Hunger's avatar
Tobias Hunger committed
337
338
339
340
341
342
343
    emit dirty();
}

void KitManagerConfigWidget::kitWasUpdated(Kit *k)
{
    if (m_kit == k)
        discard();
344
    updateVisibility();
Tobias Hunger's avatar
Tobias Hunger committed
345
346
}

347
348
349
350
351
352
353
354
355
356
357
358
void KitManagerConfigWidget::updateMutableState()
{
    QAction *action = qobject_cast<QAction *>(sender());
    if (!action)
        return;
    KitConfigWidget *widget = qobject_cast<KitConfigWidget *>(action->data().value<QObject *>());
    if (!widget)
        return;
    widget->setMutable(action->isChecked());
    emit dirty();
}

359
QLabel *KitManagerConfigWidget::createLabel(const QString &name, const QString &toolTip)
360
361
362
{
    QLabel *label = new QLabel(name);
    label->setToolTip(toolTip);
363
    return label;
364
365
}

hjk's avatar
hjk committed
366
void KitManagerConfigWidget::paintEvent(QPaintEvent *)
367
{
hjk's avatar
hjk committed
368
369
370
371
    QPainter p(this);
    if (m_background.size() != size())
        m_background = Utils::DetailsWidget::createBackground(size(), 0, this);
    p.drawPixmap(rect(), m_background);
372
373
}

Tobias Hunger's avatar
Tobias Hunger committed
374
375
} // namespace Internal
} // namespace ProjectExplorer