newdialog.cpp 15.7 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33 34 35 36
#include "newdialog.h"
#include "ui_newdialog.h"
#include "basefilewizard.h"

37 38
#include <utils/stylehelper.h>

con's avatar
con committed
39
#include <coreplugin/coreconstants.h>
40
#include <coreplugin/featureprovider.h>
con's avatar
con committed
41

42 43 44 45 46 47 48 49 50
#include <QAbstractProxyModel>
#include <QSortFilterProxyModel>
#include <QItemSelectionModel>
#include <QHeaderView>
#include <QPushButton>
#include <QStandardItem>
#include <QItemDelegate>
#include <QPainter>
#include <QDebug>
con's avatar
con committed
51 52 53

Q_DECLARE_METATYPE(Core::IWizard*)

54 55 56

namespace {

con's avatar
con committed
57 58
const int ICON_SIZE = 22;

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
struct WizardContainer
{
    WizardContainer() : wizard(0), wizardOption(0) {}
    WizardContainer(Core::IWizard *w, int i): wizard(w), wizardOption(i) {}
    Core::IWizard *wizard;
    int wizardOption;
};

inline Core::IWizard *wizardOfItem(const QStandardItem *item = 0)
{
    if (!item)
        return 0;
    return item->data(Qt::UserRole).value<WizardContainer>().wizard;
}

class PlatformFilterProxyModel : public QSortFilterProxyModel
{
//    Q_OBJECT
public:
    PlatformFilterProxyModel(QObject *parent = 0): QSortFilterProxyModel(parent) {}

    void setPlatform(const QString& platform)
    {
        m_platform = platform;
        invalidateFilter();
    }

    bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
    {
        if (!sourceParent.isValid())
            return true;

        QModelIndex sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent);
        Core::IWizard *wizard = wizardOfItem(qobject_cast<QStandardItemModel*>(sourceModel())->itemFromIndex(sourceIndex));
        if (wizard)
            return wizard->isAvailable(m_platform);

        return true;
    }
private:
    QString m_platform;
};

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
class TwoLevelProxyModel : public QAbstractProxyModel
{
//    Q_OBJECT
public:
    TwoLevelProxyModel(QObject *parent = 0): QAbstractProxyModel(parent) {}

    QModelIndex index(int row, int column, const QModelIndex &parent) const
    {
        QModelIndex ourModelIndex = sourceModel()->index(row, column, mapToSource(parent));
        return createIndex(row, column, ourModelIndex.internalPointer());
    }

    QModelIndex parent(const QModelIndex &index) const
    {
        return mapFromSource(mapToSource(index).parent());
    }

    int rowCount(const QModelIndex &index) const
    {
        if (index.isValid() && index.parent().isValid() && !index.parent().parent().isValid())
            return 0;
        else
            return sourceModel()->rowCount(mapToSource(index));
    }

    int columnCount(const QModelIndex &index) const
    {
        return sourceModel()->columnCount(mapToSource(index));
    }

    QModelIndex	mapFromSource (const QModelIndex &index) const
    {
        if (!index.isValid())
            return QModelIndex();
        return createIndex(index.row(), index.column(), index.internalPointer());
    }

    QModelIndex	mapToSource (const QModelIndex &index) const
    {
        if (!index.isValid())
            return QModelIndex();
        return static_cast<TwoLevelProxyModel*>(sourceModel())->createIndex(index.row(), index.column(), index.internalPointer());
    }
};

147
#define ROW_HEIGHT 24
148 149 150 151 152 153 154 155 156

class FancyTopLevelDelegate : public QItemDelegate
{
public:
    FancyTopLevelDelegate(QObject *parent = 0)
        : QItemDelegate(parent) {}

    void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
    {
157 158
        QStyleOptionViewItem newoption = option;
        if (!(option.state & QStyle::State_Enabled)) {
159 160 161 162 163 164 165 166 167
            QLinearGradient gradient(rect.topLeft(), rect.bottomLeft());
            gradient.setColorAt(0, option.palette.window().color().lighter(106));
            gradient.setColorAt(1, option.palette.window().color().darker(106));
            painter->fillRect(rect, gradient);
            painter->setPen(option.palette.window().color().darker(130));
            if (rect.top())
                painter->drawLine(rect.topRight(), rect.topLeft());
            painter->drawLine(rect.bottomRight(), rect.bottomLeft());

168 169 170
            // Fake enabled state
            newoption.state |= newoption.state | QStyle::State_Enabled;
        }
171

172
        QItemDelegate::drawDisplay(painter, newoption, rect, text);
173 174 175 176 177 178
    }

    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QSize size = QItemDelegate::sizeHint(option, index);

179 180

        size = size.expandedTo(QSize(0, ROW_HEIGHT));
181 182 183 184 185

        return size;
    }
};

con's avatar
con committed
186 187
}

188
Q_DECLARE_METATYPE(WizardContainer)
189

con's avatar
con committed
190 191 192 193 194 195
using namespace Core;
using namespace Core::Internal;

NewDialog::NewDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Core::Internal::Ui::NewDialog),
196
    m_okButton(0)
con's avatar
con committed
197
{
198
    typedef QMap<QString, QStandardItem *> CategoryItemMap;
199 200

    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
con's avatar
con committed
201 202 203
    m_ui->setupUi(this);
    m_okButton = m_ui->buttonBox->button(QDialogButtonBox::Ok);
    m_okButton->setDefault(true);
204
    m_okButton->setText(tr("&Choose..."));
con's avatar
con committed
205

206
    m_model = new QStandardItemModel(this);
207 208 209 210 211 212
    m_twoLevelProxyModel = new TwoLevelProxyModel(this);
    m_twoLevelProxyModel->setSourceModel(m_model);
    m_filterProxyModel = new PlatformFilterProxyModel(this);
    m_filterProxyModel->setSourceModel(m_model);

    m_ui->templateCategoryView->setModel(m_twoLevelProxyModel);
213 214 215
    m_ui->templateCategoryView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    m_ui->templateCategoryView->setItemDelegate(new FancyTopLevelDelegate);

con's avatar
con committed
216
    m_ui->templatesView->setIconSize(QSize(ICON_SIZE, ICON_SIZE));
217

Robert Loehning's avatar
Robert Loehning committed
218 219 220 221
    connect(m_ui->templateCategoryView, SIGNAL(clicked(QModelIndex)),
        this, SLOT(currentCategoryChanged(QModelIndex)));
    connect(m_ui->templatesView, SIGNAL(clicked(QModelIndex)),
        this, SLOT(currentItemChanged(QModelIndex)));
222 223

    connect(m_ui->templateCategoryView->selectionModel(),
Robert Loehning's avatar
Robert Loehning committed
224 225
            SIGNAL(currentChanged(QModelIndex,QModelIndex)),
            this, SLOT(currentCategoryChanged(QModelIndex)));
226
    connect(m_ui->templatesView,
Robert Loehning's avatar
Robert Loehning committed
227
            SIGNAL(doubleClicked(QModelIndex)),
228
            this, SLOT(okButtonClicked()));
con's avatar
con committed
229 230 231

    connect(m_okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
    connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
232 233

    connect(m_ui->comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(setSelectedPlatform(QString)));
con's avatar
con committed
234 235
}

Friedemann Kleint's avatar
Friedemann Kleint committed
236 237 238 239 240 241 242 243 244
// Sort by category. id
bool wizardLessThan(const IWizard *w1, const IWizard *w2)
{
    if (const int cc = w1->category().compare(w2->category()))
        return cc < 0;
    return w1->id().compare(w2->id()) < 0;
}

void NewDialog::setWizards(QList<IWizard*> wizards)
con's avatar
con committed
245
{
246
    typedef QMap<QString, QStandardItem *> CategoryItemMap;
con's avatar
con committed
247

Friedemann Kleint's avatar
Friedemann Kleint committed
248 249
    qStableSort(wizards.begin(), wizards.end(), wizardLessThan);

250
    CategoryItemMap platformMap;
con's avatar
con committed
251

252
    m_model->clear();
253 254
    QStandardItem *parentItem = m_model->invisibleRootItem();

255 256
    QStandardItem *projectKindItem = new QStandardItem(tr("Projects"));
    projectKindItem->setData(IWizard::ProjectWizard, Qt::UserRole);
257
    projectKindItem->setFlags(0); // disable item to prevent focus
258
    QStandardItem *filesClassesKindItem = new QStandardItem(tr("Files and Classes"));
259
    filesClassesKindItem->setData(IWizard::FileWizard, Qt::UserRole);
260
    filesClassesKindItem->setFlags(0); // disable item to prevent focus
261

262 263 264
    parentItem->appendRow(projectKindItem);
    parentItem->appendRow(filesClassesKindItem);

265
    if (m_dummyIcon.isNull()) {
266
        m_dummyIcon = QIcon(QLatin1String(Core::Constants::ICON_NEWFILE));
267
    }
268

269
    QStringList availablePlatforms = IWizard::allAvailablePlatforms();
270

271
    if (availablePlatforms.count() > 1) {
272
        m_ui->comboBox->addItem(tr("All Templates"), QString());
273
        foreach (const QString &platform, availablePlatforms) {
274
            const QString displayNameForPlatform = IWizard::displayNameForPlatform(platform);
275
            m_ui->comboBox->addItem(tr("%1 Templates").arg(displayNameForPlatform), platform);
276 277 278
        }
    } else {
        if (availablePlatforms.isEmpty()) {
279
            m_ui->comboBox->addItem(tr("All Templates"), QString());
280 281
        } else {
            const QString platform = availablePlatforms.first();
282
            const QString displayNameForPlatform = IWizard::displayNameForPlatform(platform);
283
            m_ui->comboBox->addItem(tr("%1 Templates").arg(displayNameForPlatform), platform);
284 285 286 287 288 289
        }
        m_ui->comboBox->setDisabled(true);
    }

    foreach (IWizard *wizard, wizards) {
        if (wizard->isAvailable(selectedPlatform())) {
290 291 292 293 294 295 296 297 298 299 300
            QStandardItem *kindItem;
            switch (wizard->kind()) {
            case IWizard::ProjectWizard:
                kindItem = projectKindItem;
                break;
            case IWizard::ClassWizard:
            case IWizard::FileWizard:
            default:
                kindItem = filesClassesKindItem;
                break;
            }
301
            addItem(kindItem, wizard);
con's avatar
con committed
302
        }
303
    }
con's avatar
con committed
304 305 306 307
}

Core::IWizard *NewDialog::showDialog()
{
308 309 310 311 312 313
    static QString lastCategory;
    QModelIndex idx;

    if (!lastCategory.isEmpty())
        foreach(QStandardItem* item, m_categoryItems) {
            if (item->data(Qt::UserRole) == lastCategory) {
314
                idx = m_twoLevelProxyModel->mapToSource(m_model->indexFromItem(item));
315 316 317
        }
    }
    if (!idx.isValid())
318
        idx = m_twoLevelProxyModel->index(0,0, m_twoLevelProxyModel->index(0,0));
319 320 321

    m_ui->templateCategoryView->setCurrentIndex(idx);

322 323 324
    // We need to set ensure that the category has default focus
    m_ui->templateCategoryView->setFocus(Qt::NoFocusReason);

325 326
    for (int row = 0; row < m_twoLevelProxyModel->rowCount(); ++row)
        m_ui->templateCategoryView->setExpanded(m_twoLevelProxyModel->index(row, 0), true);
327

328 329 330
    // Ensure that item description is visible on first show
    currentItemChanged(m_ui->templatesView->rootIndex().child(0,0));

con's avatar
con committed
331
    updateOkButton();
332 333 334 335

    const int retVal = exec();

    idx = m_ui->templateCategoryView->currentIndex();
Thomas Hartmann's avatar
Thomas Hartmann committed
336 337 338
    QStandardItem *currentItem = m_model->itemFromIndex(m_twoLevelProxyModel->mapToSource(idx));
    if (currentItem)
        lastCategory = currentItem->data(Qt::UserRole).toString();
339 340

    if (retVal != Accepted)
con's avatar
con committed
341
        return 0;
342

con's avatar
con committed
343 344 345
    return currentWizard();
}

346 347 348 349 350 351 352 353 354 355 356 357 358
QString NewDialog::selectedPlatform() const
{
    int index = m_ui->comboBox->currentIndex();

    return m_ui->comboBox->itemData(index).toString();
}

int NewDialog::selectedWizardOption() const
{
    QStandardItem *item = m_model->itemFromIndex(m_ui->templatesView->currentIndex());
    return item->data(Qt::UserRole).value<WizardContainer>().wizardOption;
}

con's avatar
con committed
359 360 361 362 363 364 365
NewDialog::~NewDialog()
{
    delete m_ui;
}

IWizard *NewDialog::currentWizard() const
{
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
    QModelIndex index = m_filterProxyModel->mapToSource(m_ui->templatesView->currentIndex());
    return wizardOfItem(m_model->itemFromIndex(index));
}

void NewDialog::addItem(QStandardItem *topLEvelCategoryItem, IWizard *wizard)
{
    const QString categoryName = wizard->category();
    QStandardItem *categoryItem = 0;
    for (int i = 0; i < topLEvelCategoryItem->rowCount(); i++) {
        if (topLEvelCategoryItem->child(i, 0)->data(Qt::UserRole) == categoryName)
            categoryItem = topLEvelCategoryItem->child(i, 0);
    }
    if (!categoryItem) {
        categoryItem = new QStandardItem();
        topLEvelCategoryItem->appendRow(categoryItem);
        m_categoryItems.append(categoryItem);
        categoryItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        categoryItem->setText(QLatin1String("  ") + wizard->displayCategory());
        categoryItem->setData(wizard->category(), Qt::UserRole);
    }

    QStandardItem *wizardItem = new QStandardItem(wizard->displayName());
    QIcon wizardIcon;

    // spacing hack. Add proper icons instead
    if (wizard->icon().isNull()) {
        wizardIcon = m_dummyIcon;
    } else {
        wizardIcon = wizard->icon();
    }
    wizardItem->setIcon(wizardIcon);
    wizardItem->setData(QVariant::fromValue(WizardContainer(wizard, 0)), Qt::UserRole);
    wizardItem->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    categoryItem->appendRow(wizardItem);

401 402 403 404 405
}

void NewDialog::currentCategoryChanged(const QModelIndex &index)
{
    if (index.parent() != m_model->invisibleRootItem()->index()) {
406 407 408 409
        m_ui->templatesView->setModel(m_filterProxyModel);
        QModelIndex sourceIndex = m_twoLevelProxyModel->mapToSource(index);
        sourceIndex = m_filterProxyModel->mapFromSource(sourceIndex);
        m_ui->templatesView->setRootIndex(sourceIndex);
410 411 412 413
        // Focus the first item by default
        m_ui->templatesView->setCurrentIndex(m_ui->templatesView->rootIndex().child(0,0));

        connect(m_ui->templatesView->selectionModel(),
Robert Loehning's avatar
Robert Loehning committed
414 415
                SIGNAL(currentChanged(QModelIndex,QModelIndex)),
                this, SLOT(currentItemChanged(QModelIndex)));
416
    }
con's avatar
con committed
417 418
}

419
void NewDialog::currentItemChanged(const QModelIndex &index)
con's avatar
con committed
420
{
421 422 423 424
    QModelIndex sourceIndex = m_filterProxyModel->mapToSource(index);
    QStandardItem* cat = (m_model->itemFromIndex(sourceIndex));
    if (const IWizard *wizard = wizardOfItem(cat)) {
        QString desciption = wizard->description();
425 426 427
        QStringList displayNamesForSupporttedPlatforms;
        foreach (const QString &platform, wizard->supportedPlatforms())
            displayNamesForSupporttedPlatforms << IWizard::displayNameForPlatform(platform);
428 429 430 431 432 433 434 435
        if (!Qt::mightBeRichText(desciption))
            desciption.replace(QLatin1Char('\n'), QLatin1String("<br>"));
        desciption += QLatin1String("<br><br><b>");
        if (wizard->flags().testFlag(IWizard::PlatformIndependent))
            desciption += tr("Platform independent") + QLatin1String("</b>");
        else
            desciption += tr("Supported Platforms")
                    + QLatin1String("</b>: <tt>")
436
                    + displayNamesForSupporttedPlatforms.join(QLatin1String(" "))
437 438 439 440
                    + QLatin1String("</tt>");

        m_ui->templateDescription->setHtml(desciption);

441 442 443 444 445 446 447
        if (!wizard->descriptionImage().isEmpty()) {
            m_ui->imageLabel->setVisible(true);
            m_ui->imageLabel->setPixmap(wizard->descriptionImage());
        } else {
            m_ui->imageLabel->setVisible(false);
        }

448
    } else {
449
        m_ui->templateDescription->setText(QString());
450
    }
con's avatar
con committed
451 452 453 454 455
    updateOkButton();
}

void NewDialog::okButtonClicked()
{
456
    if (m_ui->templatesView->currentIndex().isValid())
con's avatar
con committed
457 458 459 460 461 462 463
        accept();
}

void NewDialog::updateOkButton()
{
    m_okButton->setEnabled(currentWizard() != 0);
}
464 465 466 467 468 469

void NewDialog::setSelectedPlatform(const QString & /*platform*/)
{
    //The static cast allows us to keep PlatformFilterProxyModel anonymous
    static_cast<PlatformFilterProxyModel*>(m_filterProxyModel)->setPlatform(selectedPlatform());
}