aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qt4projectmanager/qt-s60/s60devicespreferencepane.cpp
blob: 47aa0e558d86fbc05f53db1f024e4f89fc5d97ed (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "s60devicespreferencepane.h"
#include "ui_s60devicespreferencepane.h"

#include <qt4projectmanager/qt4projectmanagerconstants.h>

#include <utils/qtcassert.h>
#include <coreplugin/coreconstants.h>

#include <QtCore/QDir>
#include <QtCore/QtDebug>
#include <QtCore/QSharedPointer>

#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>
#include <QtGui/QIcon>
#include <QtGui/QApplication>
#include <QtGui/QStyle>
#include <QtGui/QStandardItemModel>
#include <QtGui/QStandardItem>

enum { deviceRole = Qt::UserRole + 1 };

enum Columns { DefaultColumn, EpocColumn, QtColumn, ColumnCount };

typedef QSharedPointer<Qt4ProjectManager::Internal::S60Devices::Device> DevicePtr;
Q_DECLARE_METATYPE(DevicePtr)

typedef QList<QStandardItem *> StandardItemList;

namespace Qt4ProjectManager {
namespace Internal {

static inline DevicePtr deviceFromItem(const QStandardItem *item)
{
    return qvariant_cast<DevicePtr>(item->data(deviceRole));
}

// Device model storing a shared pointer to the device as user data.
// Provides a checkable 'default' column which works exclusively.
class S60DevicesModel : public QStandardItemModel {
    Q_OBJECT
public:
    typedef QList<S60Devices::Device> DeviceList;

    explicit S60DevicesModel(bool defaultColumnCheckable, QObject *parent = 0);

    void setDevices(const DeviceList &list);
    DeviceList devices() const;
    void appendDevice(const S60Devices::Device &device);

private slots:
    void slotItemChanged(QStandardItem *item);

private:
    const bool m_defaultColumnCheckable;
};

S60DevicesModel::S60DevicesModel(bool defaultColumnCheckable, QObject *parent) :
        QStandardItemModel(0, ColumnCount, parent),
        m_defaultColumnCheckable(defaultColumnCheckable)
{
    QStringList headers;
    headers << S60DevicesBaseWidget::tr("Default")
            << S60DevicesBaseWidget::tr("SDK Location")
            << S60DevicesBaseWidget::tr("Qt Location");
    setHorizontalHeaderLabels(headers);

    if (m_defaultColumnCheckable)
        connect(this, SIGNAL(itemChanged(QStandardItem*)),
                this, SLOT(slotItemChanged(QStandardItem*)));
}

void S60DevicesModel::appendDevice(const S60Devices::Device &device)
{
    // Create SDK/Qt column items with shared pointer to entry as data.
    const QVariant deviceData = qVariantFromValue(DevicePtr(new S60Devices::Device(device)));

    const Qt::ItemFlags flags = Qt::ItemIsEnabled|Qt::ItemIsSelectable;

    QStandardItem *defaultItem = new QStandardItem;
    if (m_defaultColumnCheckable) {
        defaultItem->setCheckable(true);
        defaultItem->setCheckState(device.isDefault ? Qt::Checked : Qt::Unchecked);
        // Item is only checkable if it is not the default.
        Qt::ItemFlags checkFlags = flags;
        if (!device.isDefault)
            checkFlags |= Qt::ItemIsUserCheckable;
        defaultItem->setFlags(checkFlags);
    } else {
        defaultItem->setIcon(device.isDefault ? QIcon(QLatin1String(":/extensionsystem/images/ok.png")) : QIcon());
    }

    defaultItem->setData(deviceData);

    QStandardItem *epocItem = new QStandardItem(QDir::toNativeSeparators(device.epocRoot));
    epocItem->setFlags(flags);
    epocItem->setData(deviceData);

    const QString qtDesc = device.qt.isEmpty() ?
                           S60DevicesModel::tr("No Qt installed") :
                           QDir::toNativeSeparators(device.qt);
    QStandardItem *qtItem = new QStandardItem(qtDesc);
    qtItem->setFlags(flags);
    qtItem->setData(deviceData);

    const QString tooltip = device.toHtml();
    epocItem->setToolTip(tooltip);
    qtItem->setToolTip(tooltip);

    StandardItemList row;
    row << defaultItem << epocItem << qtItem;
    appendRow(row);
}

void S60DevicesModel::setDevices(const DeviceList &list)
{
    removeRows(0, rowCount());
    foreach(const S60Devices::Device &device, list)
        appendDevice(device);
}

S60DevicesModel::DeviceList S60DevicesModel::devices() const
{
    S60DevicesModel::DeviceList rc;
    const int count = rowCount();
    for (int r = 0; r < count; r++)
        rc.push_back(S60Devices::Device(*deviceFromItem(item(r, 0))));
    return rc;
}

void S60DevicesModel::slotItemChanged(QStandardItem *changedItem)
{
    // Sync all "default" checkmarks. Emulate an exclusive group
    // by enabling only the unchecked items (preventing the user from unchecking)
    // and uncheck all other items. Protect against recursion.
    if (changedItem->column() != DefaultColumn || changedItem->checkState() != Qt::Checked)
        return;
    const int row = changedItem->row();
    const int count = rowCount();
    for (int r = 0; r < count; r++) {
        QStandardItem *rowItem = item(r, DefaultColumn);
        if (r == row) { // Prevent uncheck.
            rowItem->setFlags(rowItem->flags() & ~Qt::ItemIsUserCheckable);
            deviceFromItem(rowItem)->isDefault = true;
        } else {
            // Uncheck others.
            rowItem->setCheckState(Qt::Unchecked);
            rowItem->setFlags(rowItem->flags() | Qt::ItemIsUserCheckable);
            deviceFromItem(rowItem)->isDefault = false;
        }
    }
}

// --------------- S60DevicesBaseWidget
S60DevicesBaseWidget::S60DevicesBaseWidget(unsigned flags, QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::S60DevicesPreferencePane),
    m_model(new S60DevicesModel(flags & DeviceDefaultCheckable))
{
    m_ui->setupUi(this);
    m_ui->addButton->setIcon(QIcon(QLatin1String(Core::Constants::ICON_PLUS)));
    connect(m_ui->addButton, SIGNAL(clicked()), this, SLOT(addDevice()));
    m_ui->removeButton->setIcon(QIcon(QLatin1String(Core::Constants::ICON_MINUS)));
    connect(m_ui->removeButton, SIGNAL(clicked()), this, SLOT(removeDevice()));
    m_ui->refreshButton->setIcon(qApp->style()->standardIcon(QStyle::SP_BrowserReload));
    connect(m_ui->refreshButton, SIGNAL(clicked()), this, SLOT(refresh()));
    m_ui->changeQtButton->setIcon(QIcon(QLatin1String(":/welcome/images/qt_logo.png")));
    connect(m_ui->changeQtButton, SIGNAL(clicked()), this, SLOT(changeQtVersion()));

    m_ui->list->setModel(m_model);
    connect(m_ui->list->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
            this, SLOT(currentChanged(QModelIndex,QModelIndex)));

    m_ui->addButton->setVisible(flags & ShowAddButton);
    m_ui->removeButton->setVisible(flags & ShowAddButton);
    m_ui->removeButton->setEnabled(false);
    m_ui->changeQtButton->setVisible(flags & ShowChangeQtButton);
    m_ui->removeButton->setEnabled(false);
    m_ui->refreshButton->setVisible(flags & ShowRefreshButton);
}

S60DevicesBaseWidget::~S60DevicesBaseWidget()
{
    delete m_ui;
}

QStandardItem *S60DevicesBaseWidget::currentItem() const
{
    // Return the column-0 item.
    QModelIndex current = m_ui->list->currentIndex();
    if (current.isValid()) {
        if (current.row() != 0)
            current = current.sibling(current.row(), 0);
        return m_model->itemFromIndex(current);
    }
    return 0;
}

S60DevicesBaseWidget::DeviceList S60DevicesBaseWidget::devices() const
{
    return m_model->devices();
}

void S60DevicesBaseWidget::setDevices(const DeviceList &s60devices,
                                      const QString &errorString)
{
    m_model->setDevices(s60devices);

    for (int c = 0; c < ColumnCount; c++)
        m_ui->list->resizeColumnToContents(c);

    if (errorString.isEmpty()) {
        clearErrorLabel();
    } else {
        setErrorLabel(errorString);
    }
}

void S60DevicesBaseWidget::changeQtVersion()
{
    if (const QStandardItem *item = currentItem()) {
        const QString qtDir = promptDirectory(tr("Choose Qt folder"));
        if (!qtDir.isEmpty()) {
            const DevicePtr device = deviceFromItem(item);
            device->qt = qtDir;
        }
    }
}

void S60DevicesBaseWidget::removeDevice()
{
    if (const QStandardItem *item = currentItem())
        m_model->removeRows(item->row(), 1);
}

void S60DevicesBaseWidget::currentChanged(const QModelIndex &current,
                                          const QModelIndex & /* previous */)
{
    const bool hasItem = current.isValid();
    m_ui->changeQtButton->setEnabled(hasItem);
    m_ui->removeButton->setEnabled(hasItem);
}

void S60DevicesBaseWidget::setErrorLabel(const QString& t)
{
    m_ui->errorLabel->setText(t);
    m_ui->errorLabel->setVisible(true);
}

void S60DevicesBaseWidget::clearErrorLabel()
{
    m_ui->errorLabel->setVisible(false);
}

QString S60DevicesBaseWidget::promptDirectory(const QString &title)
{
    return QFileDialog::getExistingDirectory(this, title);
}

void S60DevicesBaseWidget::appendDevice(const S60Devices::Device &d)
{
    m_model->appendDevice(d);
}

int S60DevicesBaseWidget::deviceCount() const
{
    return m_model->rowCount();
}

// ============ AutoDetectS60DevicesWidget

AutoDetectS60DevicesWidget::AutoDetectS60DevicesWidget(QWidget *parent,
                                                       AutoDetectS60Devices *devices,
                                                       bool changeQtVersionEnabled) :
    S60DevicesBaseWidget(ShowRefreshButton | (changeQtVersionEnabled ? unsigned(ShowChangeQtButton) : 0u),
                         parent),
    m_devices(devices)
{
    refresh();
}

void AutoDetectS60DevicesWidget::refresh()
{
    m_devices->detectDevices();
    setDevices(m_devices->devices(), m_devices->errorString());
}

// ============ GnuPocS60DevicesWidget
GnuPocS60DevicesWidget::GnuPocS60DevicesWidget(QWidget *parent) :
    S60DevicesBaseWidget(ShowAddButton|ShowRemoveButton|ShowChangeQtButton|DeviceDefaultCheckable,
                         parent)
{
}

void GnuPocS60DevicesWidget::addDevice()
{
    // 1) Prompt for GnuPoc
    const QString epocRoot = promptDirectory(tr("Step 1 of 2: Choose GnuPoc folder"));
    if (epocRoot.isEmpty())
        return;
    // 2) Prompt for Qt. Catch equal inputs just in case someone clicks very rapidly.
    QString qtDir;
    while (true) {
        qtDir = promptDirectory(tr("Step 2 of 2: Choose Qt folder"));
        if (qtDir.isEmpty())
            return;
        if (qtDir == epocRoot) {
            QMessageBox::warning(this, tr("Adding GnuPoc"),
                                 tr("GnuPoc and Qt folders must not be identical."));
        } else {
            break;
        }
    }
    // Add a device, make default if first.
    S60Devices::Device device = GnuPocS60Devices::createDevice(epocRoot, qtDir);
    if (deviceCount() == 0)
        device.isDefault = true;
    appendDevice(device);
}

// ================= S60DevicesPreferencePane
S60DevicesPreferencePane::S60DevicesPreferencePane(S60Devices *devices, QObject *parent)
        : Core::IOptionsPage(parent),
        m_widget(0),
        m_devices(devices)
{
}

S60DevicesPreferencePane::~S60DevicesPreferencePane()
{
}

QString S60DevicesPreferencePane::id() const
{
    return QLatin1String("Z.S60 SDKs");
}

QString S60DevicesPreferencePane::displayName() const
{
    return tr("S60 SDKs");
}

QString S60DevicesPreferencePane::category() const
{
    return QLatin1String(Constants::QT_SETTINGS_CATEGORY);
}

QString S60DevicesPreferencePane::displayCategory() const
{
    return QCoreApplication::translate("Qt4ProjectManager", Constants::QT_SETTINGS_CATEGORY);
}

QIcon S60DevicesPreferencePane::categoryIcon() const
{
    return QIcon(Constants::QT_SETTINGS_CATEGORY_ICON);
}

S60DevicesBaseWidget *S60DevicesPreferencePane::createWidget(QWidget *parent) const
{
    // Symbian ABLD/Raptor: Qt installed into SDK, cannot change
    if (AutoDetectS60QtDevices *aqd = qobject_cast<AutoDetectS60QtDevices *>(m_devices))
        return new AutoDetectS60DevicesWidget(parent, aqd, false);
    // Not used yet: Manual association of Qt with auto-detected SDK
    if (AutoDetectS60Devices *ad = qobject_cast<AutoDetectS60Devices *>(m_devices))
        return new AutoDetectS60DevicesWidget(parent, ad, true);
    if (GnuPocS60Devices *gd = qobject_cast<GnuPocS60Devices*>(m_devices)) {
        GnuPocS60DevicesWidget *gw = new GnuPocS60DevicesWidget(parent);
        gw->setDevices(gd->devices());
        return gw;
    }
    return 0; // Huh?
}

QWidget *S60DevicesPreferencePane::createPage(QWidget *parent)
{
    if (m_widget)
        delete m_widget;
    m_widget = createWidget(parent);
    QTC_ASSERT(m_widget, return 0)
    return m_widget;
}

void S60DevicesPreferencePane::apply()
{
    if (!m_widget) // page was never shown
        return;

    m_devices->setDevices(m_widget->devices());
}

void S60DevicesPreferencePane::finish()
{
}

} // namespace Internal
} // namespace Qt4ProjectManager

#include "s60devicespreferencepane.moc"