2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2010-03-05 11:25:49 +01:00
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "newdialog.h"
|
|
|
|
|
#include "ui_newdialog.h"
|
|
|
|
|
#include "basefilewizard.h"
|
|
|
|
|
|
2010-04-16 14:12:55 +02:00
|
|
|
#include <utils/stylehelper.h>
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/coreconstants.h>
|
|
|
|
|
#include <coreplugin/dialogs/iwizard.h>
|
|
|
|
|
|
2010-04-16 14:12:55 +02:00
|
|
|
#include <QtGui/QAbstractProxyModel>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtGui/QHeaderView>
|
|
|
|
|
#include <QtGui/QPushButton>
|
2010-04-13 19:21:14 +02:00
|
|
|
#include <QtGui/QStandardItem>
|
2010-04-16 14:12:55 +02:00
|
|
|
#include <QtGui/QItemDelegate>
|
|
|
|
|
#include <QtGui/QPainter>
|
2009-11-27 10:33:24 +01:00
|
|
|
#include <QtCore/QDebug>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(Core::IWizard*)
|
|
|
|
|
|
2010-04-16 14:12:55 +02:00
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Qt::ItemFlags flags(const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
Qt::ItemFlags f = sourceModel()->flags(index);
|
|
|
|
|
if (!index.parent().isValid())
|
|
|
|
|
return f ^ Qt::ItemIsSelectable;
|
|
|
|
|
else
|
|
|
|
|
return f;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define CATEGORY_ROW_HEIGHT 18
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
if (rect.height() == CATEGORY_ROW_HEIGHT) {
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QItemDelegate::drawDisplay(painter, option, rect, text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const
|
|
|
|
|
{
|
|
|
|
|
if (rect.height() != CATEGORY_ROW_HEIGHT)
|
|
|
|
|
QItemDelegate::drawFocus(painter, option, rect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
QSize size = QItemDelegate::sizeHint(option, index);
|
|
|
|
|
|
|
|
|
|
if (!index.parent().isValid())
|
|
|
|
|
size = size.expandedTo(QSize(0, CATEGORY_ROW_HEIGHT));
|
|
|
|
|
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline Core::IWizard *wizardOfItem(const QStandardItem *item = 0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
if (!item)
|
|
|
|
|
return 0;
|
2010-04-13 19:21:14 +02:00
|
|
|
return item->data(Qt::UserRole).value<Core::IWizard*>();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-04-16 14:12:55 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace Core;
|
|
|
|
|
using namespace Core::Internal;
|
|
|
|
|
|
|
|
|
|
NewDialog::NewDialog(QWidget *parent) :
|
|
|
|
|
QDialog(parent),
|
|
|
|
|
m_ui(new Core::Internal::Ui::NewDialog),
|
2010-01-14 14:14:39 +01:00
|
|
|
m_okButton(0),
|
|
|
|
|
m_preferredWizardKinds(0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-04-13 19:21:14 +02:00
|
|
|
typedef QMap<QString, QStandardItem *> CategoryItemMap;
|
2008-12-02 12:01:29 +01:00
|
|
|
m_ui->setupUi(this);
|
|
|
|
|
m_okButton = m_ui->buttonBox->button(QDialogButtonBox::Ok);
|
|
|
|
|
m_okButton->setDefault(true);
|
2010-04-16 14:12:55 +02:00
|
|
|
m_okButton->setText(tr("&Choose..."));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-04-13 19:21:14 +02:00
|
|
|
m_model = new QStandardItemModel(this);
|
2010-04-16 14:12:55 +02:00
|
|
|
m_proxyModel = new TwoLevelProxyModel(this);
|
|
|
|
|
m_proxyModel->setSourceModel(m_model);
|
|
|
|
|
m_ui->templateCategoryView->setModel(m_proxyModel);
|
|
|
|
|
m_ui->templateCategoryView->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
|
|
m_ui->templateCategoryView->setItemDelegate(new FancyTopLevelDelegate);
|
|
|
|
|
|
|
|
|
|
m_ui->templatesView->setIconSize(QSize(22, 22));
|
2010-04-13 19:21:14 +02:00
|
|
|
|
2010-04-16 14:12:55 +02:00
|
|
|
connect(m_ui->templateCategoryView, SIGNAL(clicked(const QModelIndex&)),
|
|
|
|
|
this, SLOT(currentCategoryChanged(const QModelIndex&)));
|
|
|
|
|
connect(m_ui->templatesView, SIGNAL(clicked(const QModelIndex&)),
|
2010-04-13 19:21:14 +02:00
|
|
|
this, SLOT(currentItemChanged(const QModelIndex&)));
|
2010-04-16 14:12:55 +02:00
|
|
|
// connect(m_ui->templatesView, SIGNAL(activated(const QModelIndex&)),
|
|
|
|
|
// m_okButton, SLOT(animateClick()));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
connect(m_okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
|
|
|
|
|
connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-27 10:33:24 +01:00
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-14 14:14:39 +01:00
|
|
|
void NewDialog::setPreferredWizardKinds(IWizard::WizardKinds kinds)
|
|
|
|
|
{
|
|
|
|
|
m_preferredWizardKinds = kinds;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-27 10:33:24 +01:00
|
|
|
void NewDialog::setWizards(QList<IWizard*> wizards)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-04-13 19:21:14 +02:00
|
|
|
typedef QMap<QString, QStandardItem *> CategoryItemMap;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-11-27 10:33:24 +01:00
|
|
|
qStableSort(wizards.begin(), wizards.end(), wizardLessThan);
|
|
|
|
|
|
2010-04-16 14:12:55 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
CategoryItemMap categories;
|
|
|
|
|
|
2010-04-13 19:21:14 +02:00
|
|
|
m_model->clear();
|
2010-04-16 14:12:55 +02:00
|
|
|
QStandardItem *projectKindItem = new QStandardItem(tr("Projects"));
|
|
|
|
|
projectKindItem->setData(IWizard::ProjectWizard, Qt::UserRole);
|
|
|
|
|
QStandardItem *filesClassesKindItem = new QStandardItem(tr("Files & Classes"));
|
|
|
|
|
filesClassesKindItem->setData(IWizard::FileWizard, Qt::UserRole);
|
|
|
|
|
|
|
|
|
|
QStandardItem *parentItem = m_model->invisibleRootItem();
|
|
|
|
|
parentItem->appendRow(projectKindItem);
|
|
|
|
|
parentItem->appendRow(filesClassesKindItem);
|
|
|
|
|
|
|
|
|
|
static QPixmap dummyIcon(22, 22);
|
|
|
|
|
dummyIcon.fill(Qt::transparent);
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
foreach (IWizard *wizard, wizards) {
|
|
|
|
|
// ensure category root
|
|
|
|
|
const QString categoryName = wizard->category();
|
2010-04-16 14:12:55 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
CategoryItemMap::iterator cit = categories.find(categoryName);
|
|
|
|
|
if (cit == categories.end()) {
|
2010-04-13 19:21:14 +02:00
|
|
|
QStandardItem *categoryItem = new QStandardItem();
|
2010-04-16 14:12:55 +02:00
|
|
|
QStandardItem *kindItem;
|
|
|
|
|
switch (wizard->kind()) {
|
|
|
|
|
case IWizard::ProjectWizard:
|
|
|
|
|
kindItem = projectKindItem;
|
|
|
|
|
break;
|
|
|
|
|
case IWizard::ClassWizard:
|
|
|
|
|
case IWizard::FileWizard:
|
|
|
|
|
default:
|
|
|
|
|
kindItem = filesClassesKindItem;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
kindItem->appendRow(categoryItem);
|
2010-03-04 12:43:27 +01:00
|
|
|
categoryItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
|
2010-04-13 19:21:14 +02:00
|
|
|
categoryItem->setText(wizard->displayCategory());
|
|
|
|
|
categoryItem->setData(QVariant::fromValue(0), Qt::UserRole);
|
2008-12-02 12:01:29 +01:00
|
|
|
cit = categories.insert(categoryName, categoryItem);
|
|
|
|
|
}
|
|
|
|
|
// add item
|
2010-04-13 19:21:14 +02:00
|
|
|
QStandardItem *wizardItem = new QStandardItem(wizard->displayName());
|
2010-04-16 14:12:55 +02:00
|
|
|
QIcon wizardIcon;
|
|
|
|
|
|
|
|
|
|
// spacing hack. Add proper icons instead
|
|
|
|
|
if (wizard->icon().isNull())
|
|
|
|
|
wizardIcon = dummyIcon;
|
|
|
|
|
else
|
|
|
|
|
wizardIcon = wizard->icon();
|
|
|
|
|
wizardItem->setIcon(wizardIcon);
|
2010-04-13 19:21:14 +02:00
|
|
|
wizardItem->setData(QVariant::fromValue(wizard), Qt::UserRole);
|
2008-12-02 12:01:29 +01:00
|
|
|
wizardItem->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable);
|
2010-04-13 19:21:14 +02:00
|
|
|
cit.value()->appendRow(wizardItem);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-04-16 14:12:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!projectKindItem->hasChildren()) {
|
|
|
|
|
QModelIndex idx = projectKindItem->index();
|
|
|
|
|
m_model->removeRow(idx.row());
|
|
|
|
|
}
|
|
|
|
|
if (!filesClassesKindItem->hasChildren()) {
|
|
|
|
|
QModelIndex idx = filesClassesKindItem->index();
|
|
|
|
|
m_model->removeRow(idx.row());
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core::IWizard *NewDialog::showDialog()
|
|
|
|
|
{
|
2010-04-16 14:12:55 +02:00
|
|
|
for (int row = 0; row < m_proxyModel->rowCount(); ++row)
|
|
|
|
|
m_ui->templateCategoryView->setExpanded(m_proxyModel->index(row, 0), true);
|
|
|
|
|
|
|
|
|
|
// QStandardItem *itemToSelect = 0;
|
|
|
|
|
// if (m_preferredWizardKinds == 0) {
|
|
|
|
|
// if (QStandardItem *rootItem = m_model->invisibleRootItem()->child(0)) {
|
|
|
|
|
// if (rootItem->rowCount())
|
|
|
|
|
// itemToSelect = rootItem->child(0);
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// for (int i = 0; i < m_model->invisibleRootItem()->rowCount(); ++i) {
|
|
|
|
|
// QStandardItem *type = m_model->invisibleRootItem()->child(i);
|
|
|
|
|
// bool hasOnlyPreferred = true;
|
|
|
|
|
// for (int j = 0; j < category->rowCount(); ++j) {
|
|
|
|
|
// QStandardItem *item = category->child(j);
|
|
|
|
|
// if (!(item->data(Qt::UserRole).value<IWizard*>()
|
|
|
|
|
// ->kind() & m_preferredWizardKinds)) {
|
|
|
|
|
// hasOnlyPreferred = false;
|
|
|
|
|
// break;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// //m_ui->templatesTree->setExpanded(category->index(), hasOnlyPreferred);
|
|
|
|
|
// if (hasOnlyPreferred && itemToSelect == 0 && category->rowCount() > 0) {
|
|
|
|
|
// itemToSelect = category->child(0);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// if (itemToSelect) {
|
|
|
|
|
// m_ui->templateCategoryView->scrollTo(itemToSelect->index());
|
|
|
|
|
// m_ui->templateCategoryView->setCurrentIndex(itemToSelect->index());
|
|
|
|
|
// m_ui->templatesView->scrollTo(itemToSelect->index());
|
|
|
|
|
// m_ui->templatesView->setCurrentIndex(itemToSelect->index());
|
|
|
|
|
// }
|
2008-12-02 12:01:29 +01:00
|
|
|
updateOkButton();
|
|
|
|
|
if (exec() != Accepted)
|
|
|
|
|
return 0;
|
|
|
|
|
return currentWizard();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NewDialog::~NewDialog()
|
|
|
|
|
{
|
|
|
|
|
delete m_ui;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IWizard *NewDialog::currentWizard() const
|
|
|
|
|
{
|
2010-04-16 14:12:55 +02:00
|
|
|
return wizardOfItem(m_model->itemFromIndex(m_ui->templatesView->currentIndex()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NewDialog::currentCategoryChanged(const QModelIndex &index)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (index.parent() != m_model->invisibleRootItem()->index()) {
|
|
|
|
|
m_ui->templatesView->setModel(m_model);
|
|
|
|
|
m_ui->templatesView->setRootIndex(m_proxyModel->mapToSource(index));
|
|
|
|
|
} else {
|
|
|
|
|
m_ui->templatesView->setModel(0);
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-04-13 19:21:14 +02:00
|
|
|
void NewDialog::currentItemChanged(const QModelIndex &index)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-04-13 19:21:14 +02:00
|
|
|
QStandardItem* cat = m_model->itemFromIndex(index);
|
2008-12-02 12:01:29 +01:00
|
|
|
if (const IWizard *wizard = wizardOfItem(cat))
|
2010-04-16 14:12:55 +02:00
|
|
|
m_ui->templateDescription->setText(wizard->description());
|
2008-12-02 12:01:29 +01:00
|
|
|
else
|
2010-04-16 14:12:55 +02:00
|
|
|
m_ui->templateDescription->setText(QString());
|
2008-12-02 12:01:29 +01:00
|
|
|
updateOkButton();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NewDialog::okButtonClicked()
|
|
|
|
|
{
|
2010-04-16 14:12:55 +02:00
|
|
|
if (m_ui->templatesView->currentIndex().isValid())
|
2008-12-02 12:01:29 +01:00
|
|
|
accept();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NewDialog::updateOkButton()
|
|
|
|
|
{
|
|
|
|
|
m_okButton->setEnabled(currentWizard() != 0);
|
|
|
|
|
}
|