Port new dialog from QTreeWidget to QTreeView and QStandardItemModel

This is a preperation towards being able to present the dialog
in a different layout, with slightly improved usability.

Reviewed-by: jbache
This commit is contained in:
Daniel Molkentin
2010-04-13 19:21:14 +02:00
parent e5d562b15d
commit 3bb69df02a
3 changed files with 44 additions and 41 deletions

View File

@@ -36,15 +36,16 @@
#include <QtGui/QHeaderView> #include <QtGui/QHeaderView>
#include <QtGui/QPushButton> #include <QtGui/QPushButton>
#include <QtGui/QStandardItem>
#include <QtCore/QDebug> #include <QtCore/QDebug>
Q_DECLARE_METATYPE(Core::IWizard*) Q_DECLARE_METATYPE(Core::IWizard*)
static inline Core::IWizard *wizardOfItem(const QTreeWidgetItem *item = 0) static inline Core::IWizard *wizardOfItem(const QStandardItem *item = 0)
{ {
if (!item) if (!item)
return 0; return 0;
return qVariantValue<Core::IWizard*>(item->data(0, Qt::UserRole)); return item->data(Qt::UserRole).value<Core::IWizard*>();
} }
using namespace Core; using namespace Core;
@@ -56,16 +57,20 @@ NewDialog::NewDialog(QWidget *parent) :
m_okButton(0), m_okButton(0),
m_preferredWizardKinds(0) m_preferredWizardKinds(0)
{ {
typedef QMap<QString, QTreeWidgetItem *> CategoryItemMap; typedef QMap<QString, QStandardItem *> CategoryItemMap;
m_ui->setupUi(this); m_ui->setupUi(this);
m_okButton = m_ui->buttonBox->button(QDialogButtonBox::Ok); m_okButton = m_ui->buttonBox->button(QDialogButtonBox::Ok);
m_okButton->setDefault(true); m_okButton->setDefault(true);
m_okButton->setText(tr("&Create")); m_okButton->setText(tr("&Create"));
m_ui->templatesTree->header()->hide(); m_ui->templatesTree->header()->hide();
connect(m_ui->templatesTree, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), m_model = new QStandardItemModel(this);
this, SLOT(currentItemChanged(QTreeWidgetItem*))); m_ui->templatesTree->setModel(m_model);
connect(m_ui->templatesTree, SIGNAL(itemActivated(QTreeWidgetItem*,int)), m_okButton, SLOT(animateClick()));
connect(m_ui->templatesTree, SIGNAL(clicked(const QModelIndex&)),
this, SLOT(currentItemChanged(const QModelIndex&)));
connect(m_ui->templatesTree, SIGNAL(activated(const QModelIndex&)),
m_okButton, SLOT(animateClick()));
connect(m_okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked())); connect(m_okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject())); connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
@@ -86,65 +91,65 @@ void NewDialog::setPreferredWizardKinds(IWizard::WizardKinds kinds)
void NewDialog::setWizards(QList<IWizard*> wizards) void NewDialog::setWizards(QList<IWizard*> wizards)
{ {
typedef QMap<QString, QTreeWidgetItem *> CategoryItemMap; typedef QMap<QString, QStandardItem *> CategoryItemMap;
qStableSort(wizards.begin(), wizards.end(), wizardLessThan); qStableSort(wizards.begin(), wizards.end(), wizardLessThan);
CategoryItemMap categories; CategoryItemMap categories;
QVariant wizardPtr;
m_ui->templatesTree->clear(); m_model->clear();
foreach (IWizard *wizard, wizards) { foreach (IWizard *wizard, wizards) {
// ensure category root // ensure category root
const QString categoryName = wizard->category(); const QString categoryName = wizard->category();
CategoryItemMap::iterator cit = categories.find(categoryName); CategoryItemMap::iterator cit = categories.find(categoryName);
if (cit == categories.end()) { if (cit == categories.end()) {
QTreeWidgetItem *categoryItem = new QTreeWidgetItem(m_ui->templatesTree); QStandardItem *parentItem = m_model->invisibleRootItem();
QStandardItem *categoryItem = new QStandardItem();
parentItem->appendRow(categoryItem);
categoryItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); categoryItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
categoryItem->setText(0, wizard->displayCategory()); categoryItem->setText(wizard->displayCategory());
qVariantSetValue<IWizard*>(wizardPtr, 0); categoryItem->setData(QVariant::fromValue(0), Qt::UserRole);
categoryItem->setData(0, Qt::UserRole, wizardPtr);
cit = categories.insert(categoryName, categoryItem); cit = categories.insert(categoryName, categoryItem);
} }
// add item // add item
QTreeWidgetItem *wizardItem = new QTreeWidgetItem(cit.value(), QStringList(wizard->displayName())); QStandardItem *wizardItem = new QStandardItem(wizard->displayName());
wizardItem->setIcon(0, wizard->icon()); wizardItem->setIcon(wizard->icon());
qVariantSetValue<IWizard*>(wizardPtr, wizard); wizardItem->setData(QVariant::fromValue(wizard), Qt::UserRole);
wizardItem->setData(0, Qt::UserRole, wizardPtr);
wizardItem->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable); wizardItem->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable);
cit.value()->appendRow(wizardItem);
} }
} }
Core::IWizard *NewDialog::showDialog() Core::IWizard *NewDialog::showDialog()
{ {
QTreeWidgetItem *itemToSelect = 0; QStandardItem *itemToSelect = 0;
if (m_preferredWizardKinds == 0) { if (m_preferredWizardKinds == 0) {
m_ui->templatesTree->expandAll(); m_ui->templatesTree->expandAll();
if (QTreeWidgetItem *rootItem = m_ui->templatesTree->topLevelItem(0)) { if (QStandardItem *rootItem = m_model->invisibleRootItem()->child(0)) {
if (rootItem->childCount()) if (rootItem->rowCount())
itemToSelect = rootItem->child(0); itemToSelect = rootItem->child(0);
} }
} else { } else {
for (int i = 0; i < m_ui->templatesTree->topLevelItemCount(); ++i) { for (int i = 0; i < m_model->invisibleRootItem()->rowCount(); ++i) {
QTreeWidgetItem *category = m_ui->templatesTree->topLevelItem(i); QStandardItem *category = m_model->invisibleRootItem()->child(i);
bool hasOnlyPreferred = true; bool hasOnlyPreferred = true;
for (int j = 0; j < category->childCount(); ++j) { for (int j = 0; j < category->rowCount(); ++j) {
QTreeWidgetItem *item = category->child(j); QStandardItem *item = category->child(j);
if (!(item->data(0, Qt::UserRole).value<IWizard*>() if (!(item->data(Qt::UserRole).value<IWizard*>()
->kind() & m_preferredWizardKinds)) { ->kind() & m_preferredWizardKinds)) {
hasOnlyPreferred = false; hasOnlyPreferred = false;
break; break;
} }
} }
category->setExpanded(hasOnlyPreferred); m_ui->templatesTree->setExpanded(category->index(), hasOnlyPreferred);
if (hasOnlyPreferred && itemToSelect == 0 && category->childCount() > 0) { if (hasOnlyPreferred && itemToSelect == 0 && category->rowCount() > 0) {
itemToSelect = category->child(0); itemToSelect = category->child(0);
} }
} }
} }
if (itemToSelect) { if (itemToSelect) {
m_ui->templatesTree->scrollToItem(itemToSelect); m_ui->templatesTree->scrollTo(itemToSelect->index());
m_ui->templatesTree->setCurrentItem(itemToSelect); m_ui->templatesTree->setCurrentIndex(itemToSelect->index());
} }
updateOkButton(); updateOkButton();
if (exec() != Accepted) if (exec() != Accepted)
@@ -159,12 +164,12 @@ NewDialog::~NewDialog()
IWizard *NewDialog::currentWizard() const IWizard *NewDialog::currentWizard() const
{ {
return wizardOfItem(m_ui->templatesTree->currentItem()); return wizardOfItem(m_model->itemFromIndex(m_ui->templatesTree->currentIndex()));
} }
void NewDialog::currentItemChanged(QTreeWidgetItem *cat) void NewDialog::currentItemChanged(const QModelIndex &index)
{ {
QStandardItem* cat = m_model->itemFromIndex(index);
if (const IWizard *wizard = wizardOfItem(cat)) if (const IWizard *wizard = wizardOfItem(cat))
m_ui->descLabel->setText(wizard->description()); m_ui->descLabel->setText(wizard->description());
else else
@@ -174,7 +179,7 @@ void NewDialog::currentItemChanged(QTreeWidgetItem *cat)
void NewDialog::okButtonClicked() void NewDialog::okButtonClicked()
{ {
if (m_ui->templatesTree->currentItem()) if (m_ui->templatesTree->currentIndex().isValid())
accept(); accept();
} }

View File

@@ -37,8 +37,10 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QPushButton; class QPushButton;
class QTreeWidgetItem; class QStandardItem;
class QStandardItemModel;
class QStringList; class QStringList;
class QModelIndex;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace Core { namespace Core {
@@ -63,7 +65,7 @@ public:
Core::IWizard *showDialog(); Core::IWizard *showDialog();
private slots: private slots:
void currentItemChanged(QTreeWidgetItem *cat); void currentItemChanged(const QModelIndex &);
void okButtonClicked(); void okButtonClicked();
void updateOkButton(); void updateOkButton();
@@ -71,6 +73,7 @@ private:
Core::IWizard *currentWizard() const; Core::IWizard *currentWizard() const;
Ui::NewDialog *m_ui; Ui::NewDialog *m_ui;
QStandardItemModel *m_model;
QPushButton *m_okButton; QPushButton *m_okButton;
IWizard::WizardKinds m_preferredWizardKinds; IWizard::WizardKinds m_preferredWizardKinds;
}; };

View File

@@ -15,18 +15,13 @@
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<widget class="QTreeWidget" name="templatesTree"> <widget class="QTreeView" name="templatesTree">
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>400</width> <width>400</width>
<height>301</height> <height>301</height>
</size> </size>
</property> </property>
<column>
<property name="text">
<string>1</string>
</property>
</column>
</widget> </widget>
</item> </item>
<item> <item>