Commit b281d6da authored by hjk's avatar hjk

TreeModel: Introduce a StaticTreeItem

This splits out the case of static string displays from the
TreeItem base class, making the base more lightweight.

Change-Id: If1f442011ec60094399a41b65d9b5015f432f82e
Reviewed-by: Eike Ziller's avatarEike Ziller <eike.ziller@qt.io>
parent 0c1cdc3a
......@@ -606,12 +606,7 @@ namespace Utils {
// TreeItem
//
TreeItem::TreeItem()
: m_parent(0), m_model(0), m_displays(0), m_flags(Qt::ItemIsEnabled|Qt::ItemIsSelectable)
{
}
TreeItem::TreeItem(const QStringList &displays, int flags)
: m_parent(0), m_model(0), m_displays(new QStringList(displays)), m_flags(flags)
: m_parent(0), m_model(0), m_flags(Qt::ItemIsEnabled|Qt::ItemIsSelectable)
{
}
......@@ -620,7 +615,6 @@ TreeItem::~TreeItem()
QTC_CHECK(m_parent == 0);
QTC_CHECK(m_model == 0);
removeChildren();
delete m_displays;
}
TreeItem *TreeItem::child(int pos) const
......@@ -636,8 +630,8 @@ int TreeItem::rowCount() const
QVariant TreeItem::data(int column, int role) const
{
if (role == Qt::DisplayRole && m_displays && column >= 0 && column < m_displays->size())
return m_displays->at(column);
Q_UNUSED(column);
Q_UNUSED(role);
return QVariant();
}
......@@ -1055,4 +1049,22 @@ TreeItem *TreeModel::takeItem(TreeItem *item)
return item;
}
StaticTreeItem::StaticTreeItem(const QStringList &displays)
: m_displays(displays)
{
}
QVariant StaticTreeItem::data(int column, int role) const
{
if (role == Qt::DisplayRole && column >= 0 && column < m_displays.size())
return m_displays.at(column);
return QVariant();
}
Qt::ItemFlags StaticTreeItem::flags(int column) const
{
Q_UNUSED(column);
return Qt::ItemIsEnabled;
}
} // namespace Utils
......@@ -39,7 +39,6 @@ class QTCREATOR_UTILS_EXPORT TreeItem
{
public:
TreeItem();
explicit TreeItem(const QStringList &displays, int flags = Qt::ItemIsEnabled);
virtual ~TreeItem();
virtual TreeItem *parent() const { return m_parent; }
......@@ -141,7 +140,6 @@ private:
TreeItem *m_parent; // Not owned.
TreeModel *m_model; // Not owned.
QStringList *m_displays;
Qt::ItemFlags m_flags;
protected:
......@@ -172,6 +170,18 @@ public:
}
};
class QTCREATOR_UTILS_EXPORT StaticTreeItem : public TreeItem
{
public:
StaticTreeItem(const QStringList &displays);
QVariant data(int column, int role) const override;
Qt::ItemFlags flags(int column) const override;
private:
QStringList m_displays;
};
// A general purpose multi-level model where each item can have its
// own (TreeItem-derived) type.
class QTCREATOR_UTILS_EXPORT TreeModel : public QAbstractItemModel
......@@ -270,10 +280,10 @@ public:
// A two-level model with a first level of static headers and a uniform second level.
template <class SecondLevelItemType>
class TwoLevelTreeModel : public LeveledTreeModel<TreeItem, SecondLevelItemType>
class TwoLevelTreeModel : public LeveledTreeModel<StaticTreeItem, SecondLevelItemType>
{
public:
using FirstLevelItem = TreeItem;
using FirstLevelItem = StaticTreeItem;
using SecondLevelItem = SecondLevelItemType;
using BaseType = LeveledTreeModel<FirstLevelItem, SecondLevelItem>;
......
......@@ -38,8 +38,7 @@ namespace Autotest {
namespace Internal {
TestTreeItem::TestTreeItem(const QString &name, const QString &filePath, Type type)
: TreeItem( { name } ),
m_name(name),
: m_name(name),
m_filePath(filePath),
m_type(type),
m_line(0),
......
......@@ -142,8 +142,8 @@ public:
CMakeToolItemModel::CMakeToolItemModel()
{
setHeader(QStringList() << tr("Name") << tr("Location"));
rootItem()->appendChild(new TreeItem(QStringList() << tr("Auto-detected") << QString() << QString()));
rootItem()->appendChild(new TreeItem(QStringList() << tr("Manual") << QString() << QString()));
rootItem()->appendChild(new StaticTreeItem({ tr("Auto-detected") }));
rootItem()->appendChild(new StaticTreeItem({ tr("Manual") }));
foreach (const CMakeTool *item, CMakeToolManager::cmakeTools())
addCMakeTool(item, false);
......
......@@ -72,8 +72,10 @@ class CategoryItem : public TreeItem
public:
CategoryItem(const QString &name, int order);
QVariant data(int column, int role) const override;
Qt::ItemFlags flags(int column) const override { Q_UNUSED(column); return Qt::ItemIsEnabled; }
private:
QString m_name;
int m_order;
};
......@@ -146,16 +148,18 @@ ILocatorFilter *FilterItem::filter() const
}
CategoryItem::CategoryItem(const QString &name, int order)
: TreeItem(QStringList(name)),
m_order(order)
: m_name(name), m_order(order)
{
}
QVariant CategoryItem::data(int column, int role) const
{
Q_UNUSED(column);
if (role == SortRole)
return m_order;
return TreeItem::data(column, role);
if (role == Qt::DisplayRole)
return m_name;
return QVariant();
}
LocatorSettingsPage::LocatorSettingsPage(Locator *plugin)
......
......@@ -107,8 +107,8 @@ KitModel::KitModel(QBoxLayout *parentLayout, QObject *parent)
m_parentLayout(parentLayout)
{
setHeader(QStringList(tr("Name")));
m_autoRoot = new TreeItem(QStringList(tr("Auto-detected")));
m_manualRoot = new TreeItem(QStringList(tr("Manual")));
m_autoRoot = new StaticTreeItem({ tr("Auto-detected") });
m_manualRoot = new StaticTreeItem({ tr("Manual") });
rootItem()->appendChild(m_autoRoot);
rootItem()->appendChild(m_manualRoot);
......
......@@ -114,8 +114,8 @@ public:
[](ToolChainFactory *factory) { return factory->canCreate();});
m_model.setHeader(QStringList() << ToolChainOptionsPage::tr("Name") << ToolChainOptionsPage::tr("Type"));
m_autoRoot = new TreeItem(QStringList() << ToolChainOptionsPage::tr("Auto-detected") << QString());
m_manualRoot = new TreeItem(QStringList() << ToolChainOptionsPage::tr("Manual") << QString());
m_autoRoot = new StaticTreeItem({ ToolChainOptionsPage::tr("Auto-detected") });
m_manualRoot = new StaticTreeItem({ ToolChainOptionsPage::tr("Manual") });
m_model.rootItem()->appendChild(m_autoRoot);
m_model.rootItem()->appendChild(m_manualRoot);
foreach (ToolChain *tc, ToolChainManager::toolChains()) {
......
......@@ -65,13 +65,8 @@ namespace Internal {
class QtVersionItem : public TreeItem
{
public:
QtVersionItem(const QString &name) // for auto/manual node
: TreeItem({name})
{}
QtVersionItem(BaseQtVersion *version) // for versions
: TreeItem(),
m_version(version)
explicit QtVersionItem(BaseQtVersion *version)
: m_version(version)
{}
~QtVersionItem()
......@@ -212,15 +207,14 @@ QtOptionsPageWidget::QtOptionsPageWidget(QWidget *parent)
m_ui->versionInfoWidget->setWidget(versionInfoWidget);
m_ui->versionInfoWidget->setState(DetailsWidget::NoSummary);
auto rootItem = new QtVersionItem(QLatin1String("root"));
m_autoItem = new QtVersionItem(tr("Auto-detected"));
rootItem->appendChild(m_autoItem);
m_manualItem = new QtVersionItem(tr("Manual"));
rootItem->appendChild(m_manualItem);
m_model = new TreeModel(rootItem);
m_model = new TreeModel;
m_model->setHeader({tr("Name"), tr("qmake Location"), tr("Type")});
m_autoItem = new StaticTreeItem({ tr("Auto-detected") });
m_model->rootItem()->appendChild(m_autoItem);
m_manualItem = new StaticTreeItem({ tr("Manual") });
m_model->rootItem()->appendChild(m_manualItem);
m_filterModel = new QSortFilterProxyModel(this);
m_filterModel->setSourceModel(m_model);
m_filterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
......@@ -292,8 +286,8 @@ QtVersionItem *QtOptionsPageWidget::currentItem() const
{
QModelIndex idx = m_ui->qtdirList->selectionModel()->currentIndex();
QModelIndex sourceIdx = m_filterModel->mapToSource(idx);
QtVersionItem *item = static_cast<QtVersionItem *>(m_model->itemForIndex(sourceIdx));
return item;
TreeItem *item = m_model->itemForIndex(sourceIdx);
return item->level() == 2 ? static_cast<QtVersionItem *>(item) : 0;
}
void QtOptionsPageWidget::cleanUpQtVersions()
......@@ -727,6 +721,9 @@ void QtOptionsPageWidget::qtVersionChanged()
void QtOptionsPageWidget::updateDescriptionLabel()
{
QtVersionItem *item = currentItem();
if (!item)
return;
const BaseQtVersion *version = item->version();
const ValidityInfo info = validInformation(version);
if (info.message.isEmpty()) {
......@@ -737,8 +734,7 @@ void QtOptionsPageWidget::updateDescriptionLabel()
m_versionUi->errorLabel->setToolTip(info.toolTip);
}
m_ui->infoWidget->setSummaryText(info.description);
if (item)
item->setIcon(info.icon);
item->setIcon(info.icon);
if (version) {
m_infoBrowser->setHtml(version->toHtml(true));
......
......@@ -51,9 +51,9 @@ static int countLevelItems(TreeItem *base, int level)
return n;
}
static TreeItem *createItem(const char *name)
static TreeItem *createItem(const QString &name)
{
return new TreeItem(QStringList {QString::fromLatin1(name)});
return new StaticTreeItem({ name });
}
void tst_TreeModel::testIteration()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment