aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/coreplugin/dialogs/newdialog.cpp
blob: ca194d2d85f4e8e1a1f069a4ef5248f48a9d266a (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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 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 "newdialog.h"
#include "ui_newdialog.h"
#include "basefilewizard.h"

#include <coreplugin/coreconstants.h>
#include <coreplugin/dialogs/iwizard.h>

#include <QtGui/QHeaderView>
#include <QtGui/QPushButton>
#include <QtCore/QDebug>

Q_DECLARE_METATYPE(Core::IWizard*)

static inline Core::IWizard *wizardOfItem(const QTreeWidgetItem *item = 0)
{
    if (!item)
        return 0;
    return qVariantValue<Core::IWizard*>(item->data(0, Qt::UserRole));
}

using namespace Core;
using namespace Core::Internal;

NewDialog::NewDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Core::Internal::Ui::NewDialog),
    m_okButton(0)
{
    typedef QMap<QString, QTreeWidgetItem *> CategoryItemMap;
    m_ui->setupUi(this);
    m_okButton = m_ui->buttonBox->button(QDialogButtonBox::Ok);
    m_okButton->setDefault(true);

    m_ui->watermark->setPixmap(BaseFileWizard::watermark());

    m_ui->templatesTree->header()->hide();
    connect(m_ui->templatesTree, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
        this, SLOT(currentItemChanged(QTreeWidgetItem*)));
    connect(m_ui->templatesTree, SIGNAL(itemActivated(QTreeWidgetItem*,int)), m_okButton, SLOT(animateClick()));

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

// 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)
{
    typedef QMap<QString, QTreeWidgetItem *> CategoryItemMap;

    qStableSort(wizards.begin(), wizards.end(), wizardLessThan);

    CategoryItemMap categories;
    QVariant wizardPtr;

    m_ui->templatesTree->clear();
    foreach (IWizard *wizard, wizards) {
        // ensure category root
        const QString categoryName = wizard->category();
        CategoryItemMap::iterator cit = categories.find(categoryName);
        if (cit == categories.end()) {
            QTreeWidgetItem *categoryItem = new QTreeWidgetItem(m_ui->templatesTree);
            categoryItem->setFlags(Qt::ItemIsEnabled);
            categoryItem->setText(0, wizard->displayCategory());
            qVariantSetValue<IWizard*>(wizardPtr, 0);
            categoryItem->setData(0, Qt::UserRole, wizardPtr);
            cit = categories.insert(categoryName, categoryItem);
        }
        // add item
        QTreeWidgetItem *wizardItem = new QTreeWidgetItem(cit.value(), QStringList(wizard->displayName()));
        wizardItem->setIcon(0, wizard->icon());
        qVariantSetValue<IWizard*>(wizardPtr, wizard);
        wizardItem->setData(0, Qt::UserRole, wizardPtr);
        wizardItem->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    }
}

Core::IWizard *NewDialog::showDialog()
{
    m_ui->templatesTree->expandAll();
    if (QTreeWidgetItem *rootItem = m_ui->templatesTree->topLevelItem(0)) {
        m_ui->templatesTree->scrollToItem(rootItem);
        if (rootItem->childCount())
            m_ui->templatesTree->setCurrentItem(rootItem->child(0));
    }
    updateOkButton();
    if (exec() != Accepted)
        return 0;
    return currentWizard();
}

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

IWizard *NewDialog::currentWizard() const
{
    return wizardOfItem(m_ui->templatesTree->currentItem());
}

void NewDialog::currentItemChanged(QTreeWidgetItem *cat)
{

    if (const IWizard *wizard = wizardOfItem(cat))
        m_ui->descLabel->setText(wizard->description());
    else
        m_ui->descLabel->setText(QString());
    updateOkButton();
}

void NewDialog::okButtonClicked()
{
    if (m_ui->templatesTree->currentItem())
        accept();
}

void NewDialog::updateOkButton()
{
    m_okButton->setEnabled(currentWizard() != 0);
}