From e4d2d8a2eddcddde89e2497efeb9069eac8857a9 Mon Sep 17 00:00:00 2001
From: Thomas Hartmann <Thomas.Hartmann@nokia.com>
Date: Fri, 10 Dec 2010 22:01:55 +0100
Subject: [PATCH] QmlDesigner.itemlibrary: filter item library by imports

Only show items that are available and use requiredImport for filtering
---
 .../integration/designdocumentcontroller.cpp  |  1 -
 .../itemlibrary/itemlibrarymodel.cpp          | 75 ++++++++++++-------
 .../components/itemlibrary/itemlibrarymodel.h |  3 +-
 .../itemlibrary/itemlibraryview.cpp           | 15 +++-
 .../components/itemlibrary/itemlibraryview.h  |  3 +
 .../itemlibrary/itemlibrarywidget.cpp         | 12 ++-
 .../itemlibrary/itemlibrarywidget.h           |  3 +
 7 files changed, 78 insertions(+), 34 deletions(-)

diff --git a/src/plugins/qmldesigner/components/integration/designdocumentcontroller.cpp b/src/plugins/qmldesigner/components/integration/designdocumentcontroller.cpp
index 0c3a6b03809..edf94e0f8fc 100644
--- a/src/plugins/qmldesigner/components/integration/designdocumentcontroller.cpp
+++ b/src/plugins/qmldesigner/components/integration/designdocumentcontroller.cpp
@@ -355,7 +355,6 @@ void DesignDocumentController::loadCurrentModel()
 
     m_d->model->attachView(m_d->nodeInstanceView.data());
     m_d->model->attachView(m_d->navigator.data());
-    m_d->itemLibraryView->widget()->setItemLibraryInfo(m_d->model->metaInfo().itemLibraryInfo());
     m_d->itemLibraryView->widget()->setResourcePath(QFileInfo(m_d->fileName).absolutePath());
 
     if (!m_d->componentAction) {
diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarymodel.cpp b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarymodel.cpp
index ccdcfc359c2..7971cc37516 100644
--- a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarymodel.cpp
+++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarymodel.cpp
@@ -29,6 +29,8 @@
 
 #include "itemlibrarymodel.h"
 #include "itemlibraryinfo.h"
+#include <model.h>
+#include <nodemetainfo.h>
 
 #include <QVariant>
 #include <QMimeData>
@@ -378,7 +380,12 @@ bool ItemLibraryModel::isItemVisible(int itemLibId)
     return elementModel(sectionLibId)->isItemVisible(itemLibId);
 }
 
-void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo)
+QString entryToImport(const ItemLibraryEntry &entry)
+{
+    return entry.requiredImport() + " " + QString::number(entry.majorVersion()) + "." + QString::number(entry.minorVersion());
+}
+
+void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo, Model *model)
 {
     QMap<QString, int> sections;
 
@@ -387,36 +394,46 @@ void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo)
     m_sections.clear();
     m_nextLibId = 0;
 
-    foreach (ItemLibraryEntry entry, itemLibraryInfo->entries()) {
-        QString itemSectionName = entry.category();
-        ItemLibrarySectionModel *sectionModel;
-        ItemLibraryItemModel *itemModel;
-        int itemId = m_nextLibId++, sectionId;
-
-        if (sections.contains(itemSectionName)) {
-            sectionId = sections.value(itemSectionName);
-            sectionModel = elementModel(sectionId);
-        } else {
-            sectionId = m_nextLibId++;
-            sectionModel = new ItemLibrarySectionModel(m_scriptEngine.data(), sectionId, itemSectionName, this);
-            addElement(sectionModel, sectionId);
-            sections.insert(itemSectionName, sectionId);
-        }
+    QStringList imports;
+    foreach (const Import &import, model->imports())
+        if (import.isLibraryImport())
+            imports << import.url() + " " + import.version();
 
-        m_itemInfos.insert(itemId, entry);
-
-        itemModel = new ItemLibraryItemModel(m_scriptEngine.data(), itemId, entry.name());
-
-        // delayed creation of (default) icons
-        if (entry.iconPath().isEmpty())
-            entry.setIconPath(QLatin1String(":/ItemLibrary/images/item-default-icon.png"));
-        if (entry.dragIcon().isNull())
-            entry.setDragIcon(createDragPixmap(getWidth(entry), getHeight(entry)));
+    foreach (ItemLibraryEntry entry, itemLibraryInfo->entries()) {
 
-        itemModel->setItemIconPath(entry.iconPath());
-        itemModel->setItemIconSize(m_itemIconSize);
-        sectionModel->addSectionEntry(itemModel);
-        m_sections.insert(itemId, sectionId);
+         bool valid = model->metaInfo(entry.typeName(), entry.majorVersion(), entry.minorVersion()).isValid();
+
+        if (valid && entry.requiredImport().isEmpty() || imports.contains(entryToImport(entry))) {
+            QString itemSectionName = entry.category();
+            ItemLibrarySectionModel *sectionModel;
+            ItemLibraryItemModel *itemModel;
+            int itemId = m_nextLibId++, sectionId;
+
+            if (sections.contains(itemSectionName)) {
+                sectionId = sections.value(itemSectionName);
+                sectionModel = elementModel(sectionId);
+            } else {
+                sectionId = m_nextLibId++;
+                sectionModel = new ItemLibrarySectionModel(m_scriptEngine.data(), sectionId, itemSectionName, this);
+                addElement(sectionModel, sectionId);
+                sections.insert(itemSectionName, sectionId);
+            }
+
+            m_itemInfos.insert(itemId, entry);
+
+            itemModel = new ItemLibraryItemModel(m_scriptEngine.data(), itemId, entry.name());
+
+            // delayed creation of (default) icons
+            if (entry.iconPath().isEmpty())
+                entry.setIconPath(QLatin1String(":/ItemLibrary/images/item-default-icon.png"));
+            if (entry.dragIcon().isNull())
+                entry.setDragIcon(createDragPixmap(getWidth(entry), getHeight(entry)));
+
+            itemModel->setItemIconPath(entry.iconPath());
+            itemModel->setItemIconSize(m_itemIconSize);
+            sectionModel->addSectionEntry(itemModel);
+            m_sections.insert(itemId, sectionId);
+        }
     }
 
     updateVisibility();
diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarymodel.h b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarymodel.h
index de29910d1bb..5a5da139c7e 100644
--- a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarymodel.h
+++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarymodel.h
@@ -42,6 +42,7 @@ namespace QmlDesigner {
 
 class ItemLibraryInfo;
 class ItemLibraryEntry;
+class Model;
 
 namespace Internal {
 
@@ -131,7 +132,7 @@ public:
 
     QString searchText() const;
 
-    void update(ItemLibraryInfo *itemLibraryInfo);
+    void update(ItemLibraryInfo *itemLibraryInfo, Model *model);
 
     QString getTypeName(int libId);
     QMimeData *getMimeData(int libId);
diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.cpp b/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.cpp
index 7e1809365e0..90838769494 100644
--- a/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.cpp
+++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.cpp
@@ -1,5 +1,6 @@
 #include "itemlibraryview.h"
 #include "itemlibrarywidget.h"
+#include <import.h>
 
 namespace QmlDesigner {
 
@@ -21,19 +22,24 @@ ItemLibraryWidget *ItemLibraryView::widget()
 void ItemLibraryView::modelAttached(Model *model)
 {
     AbstractView::modelAttached(model);
+    m_widget->setModel(model);
+    updateImports();
 }
 
 void ItemLibraryView::modelAboutToBeDetached(Model *model)
 {
     AbstractView::modelAboutToBeDetached(model);
+    m_widget->setModel(0);
 }
 
-void ItemLibraryView::importAdded(const Import &import)
+void ItemLibraryView::importAdded(const Import &)
 {
+    updateImports();
 }
 
-void ItemLibraryView::importRemoved(const Import &import)
+void ItemLibraryView::importRemoved(const Import &)
 {
+    updateImports();
 }
 
 void ItemLibraryView::nodeCreated(const ModelNode &)
@@ -122,4 +128,9 @@ void ItemLibraryView::instancesCompleted(const QVector<ModelNode> &)
 
 }
 
+void ItemLibraryView::updateImports()
+{
+    m_widget->updateModel();
+}
+
 } //QmlDesigner
diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.h b/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.h
index ffd8687d612..1b09ec3c6b7 100644
--- a/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.h
+++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibraryview.h
@@ -79,6 +79,9 @@ public:
     void instancePropertyChange(const QList<QPair<ModelNode, QString> > &propertyList);
     void instancesCompleted(const QVector<ModelNode> &completedNodeList);
 
+protected:
+    void updateImports();
+
 private:
     QWeakPointer<ItemLibraryWidget> m_widget;
 };
diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp
index 87cf6dffbed..e8079d26bac 100644
--- a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp
+++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp
@@ -34,6 +34,8 @@
 #include "itemlibrarymodel.h"
 #include "itemlibraryimageprovider.h"
 #include "customdraganddrop.h"
+#include <model.h>
+#include <metainfo.h>
 
 #include <QFileInfo>
 #include <QFileIconProvider>
@@ -107,6 +109,7 @@ public:
 
     QSize m_itemIconSize, m_resIconSize;
     MyFileIconProvider m_iconProvider;
+    Model *model;
 };
 
 ItemLibraryWidgetPrivate::ItemLibraryWidgetPrivate(QObject *object) :
@@ -279,9 +282,16 @@ void ItemLibraryWidget::setSearchFilter(const QString &searchFilter)
     }
 }
 
+void ItemLibraryWidget::setModel(Model *model)
+{
+    m_d->model = model;
+    setItemLibraryInfo(model->metaInfo().itemLibraryInfo());
+    updateModel();
+}
+
 void ItemLibraryWidget::updateModel()
 {
-    m_d->m_itemLibraryModel->update(m_d->m_itemLibraryInfo.data());
+    m_d->m_itemLibraryModel->update(m_d->m_itemLibraryInfo.data(), m_d->model);
     updateSearch();
 }
 
diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.h b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.h
index 73263310ce7..1961b89397f 100644
--- a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.h
+++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.h
@@ -38,6 +38,7 @@ namespace QmlDesigner {
 class ItemLibraryWidgetPrivate;
 class MetaInfo;
 class ItemLibraryEntry;
+class Model;
 
 class ItemLibraryWidget : public QFrame
 {
@@ -60,6 +61,8 @@ public Q_SLOTS:
     void startDragAndDrop(int itemLibId);
     void showItemInfo(int itemLibId);
 
+    void setModel(Model *model);
+
 protected:
     void wheelEvent(QWheelEvent *event);
 
-- 
GitLab