25 #include "internal/intro/berryIntroConstants.h"
26 #include "internal/berryKeywordRegistry.h"
41 ViewTreeItem(ViewTreeModel* model)
46 virtual ~ViewTreeItem()
48 QList<ViewTreeItem*> children = m_children;
49 if (m_parent) m_parent->removeChild(
this);
53 virtual QVariant data(
int role)
57 if (m_keywordCache.isEmpty())
59 m_keywordCache = QStringList(keywordLabels().toList());
61 return m_keywordCache;
66 virtual Qt::ItemFlags flags()
const
68 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
71 virtual QSet<QString> keywordLabels()
const
73 return QSet<QString>();
76 void appendChild(ViewTreeItem* child)
78 m_children.push_back(child);
79 child->m_parent =
this;
83 void removeChild(ViewTreeItem* child)
85 m_children.removeAll(child);
88 QList<ViewTreeItem*> takeChildren()
90 QList<ViewTreeItem*> children = m_children;
95 ViewTreeItem* childItem(
int row)
const
97 if (row < 0 || row >= m_children.size())
return nullptr;
98 return m_children.at(row);
101 ViewTreeItem* parentItem()
const
106 int childCount()
const
108 return m_children.size();
113 if (m_parent)
return m_parent->rowIndex(
this);
117 int rowIndex(
const ViewTreeItem* child)
const
119 return m_children.indexOf(const_cast<ViewTreeItem*>(child));
122 QList<ViewTreeItem*> m_children;
123 ViewTreeItem* m_parent;
124 ViewTreeModel* m_model;
128 QStringList m_keywordCache;
134 return item1->data(Qt::DisplayRole).toString() < item2->data(Qt::DisplayRole).toString();
137 struct RootTreeItem : ViewTreeItem
139 RootTreeItem(ViewTreeModel* model) : ViewTreeItem(model) {}
141 QVariant data(
int )
override {
return QVariant(); }
144 struct DescriptorTreeItem : ViewTreeItem
148 QVariant data(
int role)
override;
152 QSet<QString> keywordLabels()
const override;
157 struct CategoryTreeItem : ViewTreeItem
161 QVariant data(
int role)
override;
163 Qt::ItemFlags flags()
const override;
167 QSet<QString> keywordLabels()
const override;
173 void RemoveIntroView(QList<IViewDescriptor::Pointer>& list);
177 void CreateChildren();
185 struct ViewTreeModel::Impl
187 Impl(
const IWorkbenchWindow* window)
189 , viewRegistry(*window->GetWorkbench()->GetViewRegistry())
194 const IWorkbenchWindow* window;
195 IViewRegistry& viewRegistry;
197 QScopedPointer<RootTreeItem> rootItem;
201 : QAbstractItemModel(parent)
202 , d(new Impl(window))
204 d->rootItem.reset(
new RootTreeItem(
this));
206 QList<CategoryTreeItem*> categoryItems;
208 QList<IViewCategory::Pointer> categories = d->viewRegistry.GetCategories();
209 for (
auto category : categories)
211 if (category->GetViews().isEmpty())
continue;
212 CategoryTreeItem* categoryItem =
new CategoryTreeItem(
this, category);
213 if (categoryItem->childCount() == 0)
219 categoryItems.push_back(categoryItem);
225 if (categoryItems.size() == 1)
227 QList<ViewTreeItem*> items = categoryItems.front()->takeChildren();
228 for (
auto item : items)
230 d->rootItem->appendChild(item);
232 qDeleteAll(categoryItems);
236 for (
auto category : categoryItems)
238 d->rootItem->appendChild(category);
250 if (!index.isValid())
return QVariant();
252 return static_cast<ViewTreeItem*
>(index.internalPointer())->
data(role);
257 if (!index.isValid())
return nullptr;
259 return static_cast<ViewTreeItem*
>(index.internalPointer())->
flags();
264 if (role == Qt::DisplayRole && section == 0)
273 if (!hasIndex(row, column, parent))
275 return QModelIndex();
278 ViewTreeItem* parentItem =
nullptr;
279 if (!parent.isValid())
281 parentItem = d->rootItem.data();
285 parentItem =
static_cast<ViewTreeItem*
>(parent.internalPointer());
288 ViewTreeItem* childItem = parentItem->childItem(row);
291 return createIndex(row, column, childItem);
293 return QModelIndex();
298 if (!child.isValid())
300 return QModelIndex();
303 ViewTreeItem* childItem =
static_cast<ViewTreeItem*
>(child.internalPointer());
304 ViewTreeItem* parentItem = childItem->parentItem();
306 if (parentItem == d->rootItem.data())
308 return QModelIndex();
310 return createIndex(parentItem->row(), 0, parentItem);
315 ViewTreeItem* parentItem =
nullptr;
316 if (parent.column() > 0)
return 0;
318 if (!parent.isValid())
320 parentItem = d->rootItem.data();
324 parentItem =
static_cast<ViewTreeItem*
>(parent.internalPointer());
326 return parentItem->childCount();
342 : ViewTreeItem(model)
343 , m_descriptor(descriptor)
345 if (parent) parent->appendChild(
this);
348 QVariant DescriptorTreeItem::data(
int role)
350 if (role == Qt::DisplayRole)
352 return m_descriptor->GetLabel();
354 else if (role == Qt::DecorationRole)
356 return m_descriptor->GetImageDescriptor();
358 else if (role == Qt::ForegroundRole)
361 if (page.IsNotNull())
363 if (page->FindViewReference(m_descriptor->GetId()).IsNotNull())
365 return QBrush(QColor(Qt::gray));
371 return m_descriptor->GetDescription();
375 return m_descriptor->GetId();
377 return ViewTreeItem::data(role);
380 QSet<QString> DescriptorTreeItem::keywordLabels()
const
382 KeywordRegistry* registry = KeywordRegistry::GetInstance();
383 QStringList ids = m_descriptor->GetKeywordReferences();
384 QSet<QString> keywords;
385 keywords.insert(m_descriptor->GetLabel());
388 QString label = registry->GetKeywordLabel(
id);
389 for (
auto keyword : label.split(
' ', QString::SkipEmptyParts))
391 keywords.insert(keyword);
399 CategoryTreeItem::CategoryTreeItem(ViewTreeModel* model,
IViewCategory::Pointer category, ViewTreeItem* parent)
400 : ViewTreeItem(model)
401 , m_category(category)
403 if (parent) parent->appendChild(
this);
404 this->CreateChildren();
407 QVariant CategoryTreeItem::data(
int role)
409 if (role == Qt::DisplayRole)
411 return m_category->GetLabel();
413 else if (role == Qt::DecorationRole)
415 return QIcon::fromTheme(
"folder");
419 return m_category->GetId();
421 return ViewTreeItem::data(role);
424 Qt::ItemFlags CategoryTreeItem::flags()
const
426 return Qt::ItemIsEnabled;
429 QSet<QString> CategoryTreeItem::keywordLabels()
const
431 QSet<QString> keywords;
432 for(
auto child : this->m_children)
436 keywords.insert(keyword);
442 void CategoryTreeItem::CreateChildren()
444 auto viewDescriptors = m_category->GetViews();
445 RemoveIntroView(viewDescriptors);
446 for(
auto viewDescriptor : viewDescriptors)
448 new DescriptorTreeItem(this->m_model, viewDescriptor,
this);
452 void CategoryTreeItem::RemoveIntroView(QList<IViewDescriptor::Pointer>& list)
454 for (
auto view = list.begin(); view != list.end();)
456 if ((*view)->GetId() == IntroConstants::INTRO_VIEW_ID)
458 view = list.erase(view);
int rowCount(const QModelIndex &parent=QModelIndex()) const override
ViewTreeModel(const IWorkbenchWindow *window, QObject *parent=nullptr)
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
const IWorkbenchWindow * GetWorkbenchWindow() const
QVariant data(const QModelIndex &index, int role) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
berry::SmartPointer< Self > Pointer
bool CompareViewTreeItem(ViewTreeItem *item1, ViewTreeItem *item2)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QModelIndex parent(const QModelIndex &child) const override