Commit 8e9da728 authored by Kai Koehne's avatar Kai Koehne
Browse files

QuickDesigner: Factor item library info out into it's own class

Reviewed-by: Thomas Hartmann
parent 3d93dde8
......@@ -170,7 +170,7 @@ void ItemCreatorTool::createAtItem(const QRectF &rect)
return;
if (list.first() == "item") {
RewriterTransaction transaction = view()->beginRewriterTransaction();
ItemLibraryEntry itemLibraryEntry = view()->model()->metaInfo().itemLibraryEntry(list.at(1));
ItemLibraryEntry itemLibraryEntry = view()->model()->metaInfo().itemLibraryInfo().entry(list.at(1));
QmlItemNode newNode = view()->createQmlItemNode(itemLibraryEntry, pos, parentNode);
newNode.modelNode().variantProperty("width") = rect.width();
newNode.modelNode().variantProperty("height") = rect.height();
......
......@@ -387,7 +387,6 @@ bool ItemLibraryModel::isItemVisible(int itemLibId)
return elementModel(sectionLibId)->isItemVisible(itemLibId);
}
void ItemLibraryModel::update(const MetaInfo &metaInfo)
{
QMap<QString, int> sections;
......@@ -403,32 +402,36 @@ void ItemLibraryModel::update(const MetaInfo &metaInfo)
*m_metaInfo = metaInfo;
}
foreach (const QString &type, metaInfo.itemLibraryItems()) {
foreach (const ItemLibraryEntry &itemLibraryEntry, itemLibraryEntrys(type)) {
QString itemSectionName = itemLibraryEntry.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, itemLibraryEntry);
itemModel = new ItemLibraryItemModel(m_scriptEngine.data(), itemId, itemLibraryEntry.name());
itemModel->setItemIcon(itemLibraryEntry.icon());
itemModel->setItemIconSize(m_itemIconSize);
sectionModel->addSectionEntry(itemModel);
m_sections.insert(itemId, sectionId);
foreach (ItemLibraryEntry entry, metaInfo.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);
}
m_itemInfos.insert(itemId, entry);
itemModel = new ItemLibraryItemModel(m_scriptEngine.data(), itemId, entry.name());
// delayed creation of (default) icons
if (entry.icon().isNull())
entry.setIcon(QIcon(QLatin1String(":/ItemLibrary/images/item-default-icon.png")));
if (entry.dragIcon().isNull())
entry.setDragIcon(createDragPixmap(getWidth(entry), getHeight(entry)));
itemModel->setItemIcon(entry.icon());
itemModel->setItemIconSize(m_itemIconSize);
sectionModel->addSectionEntry(itemModel);
m_sections.insert(itemId, sectionId);
}
updateVisibility();
......@@ -499,7 +502,7 @@ void ItemLibraryModel::updateVisibility()
emit visibilityChanged();
}
static inline int getWidth(const ItemLibraryEntry &itemLibraryEntry)
int ItemLibraryModel::getWidth(const ItemLibraryEntry &itemLibraryEntry)
{
foreach (const ItemLibraryEntry::Property &property, itemLibraryEntry.properties())
{
......@@ -509,7 +512,7 @@ static inline int getWidth(const ItemLibraryEntry &itemLibraryEntry)
return 64;
}
static inline int getHeight(const ItemLibraryEntry &itemLibraryEntry)
int ItemLibraryModel::getHeight(const ItemLibraryEntry &itemLibraryEntry)
{
foreach (const ItemLibraryEntry::Property &property, itemLibraryEntry.properties())
{
......@@ -519,7 +522,7 @@ static inline int getHeight(const ItemLibraryEntry &itemLibraryEntry)
return 64;
}
static inline QPixmap createDragPixmap(int width, int height)
QPixmap ItemLibraryModel::createDragPixmap(int width, int height)
{
QImage dragImage(width, height, QImage::Format_RGB32); // TODO: draw item drag icon
dragImage.fill(0xffffffff);
......@@ -531,53 +534,6 @@ static inline QPixmap createDragPixmap(int width, int height)
return QPixmap::fromImage(dragImage);
}
QList<ItemLibraryEntry> ItemLibraryModel::itemLibraryEntrys(const QString &type)
{
QList<ItemLibraryEntry> itemLibraryEntryList;
NodeMetaInfo nodeInfo = m_metaInfo->nodeMetaInfo(type);
if (nodeInfo.isQmlGraphicsItem()) {
itemLibraryEntryList = m_metaInfo->itemLibraryEntrys(nodeInfo);
if (!m_metaInfo->hasNodeMetaInfo(type))
qWarning() << "ItemLibrary: type not declared: " << type;
static QIcon defaultIcon(QLatin1String(":/ItemLibrary/images/item-default-icon.png"));
if (itemLibraryEntryList.isEmpty() || !m_metaInfo->hasNodeMetaInfo(type)) {
QIcon icon = nodeInfo.icon();
if (icon.isNull())
icon = defaultIcon;
ItemLibraryEntry itemLibraryEntry;
itemLibraryEntry.setName(type);
itemLibraryEntry.setTypeName(nodeInfo.typeName());
itemLibraryEntry.setCategory(nodeInfo.category());
itemLibraryEntry.setIcon(icon);
itemLibraryEntry.setDragIcon(createDragPixmap(64, 64));
itemLibraryEntry.setMajorVersion(nodeInfo.majorVersion());
itemLibraryEntry.setMinorVersion(nodeInfo.minorVersion());
itemLibraryEntryList.append(itemLibraryEntry);
}
else {
foreach (ItemLibraryEntry itemLibraryEntry, itemLibraryEntryList) {
QIcon icon = itemLibraryEntry.icon();
if (itemLibraryEntry.icon().isNull())
itemLibraryEntry.setIcon(defaultIcon);
if (itemLibraryEntry.dragIcon().isNull())
itemLibraryEntry.setDragIcon(createDragPixmap(getWidth(itemLibraryEntry), getHeight(itemLibraryEntry)));
if (itemLibraryEntry.category().isEmpty())
itemLibraryEntry.setCategory(nodeInfo.category());
}
}
}
return itemLibraryEntryList;
}
} // namespace Internal
} // namespace QmlDesigner
......@@ -153,7 +153,10 @@ signals:
private:
void updateVisibility();
QList<ItemLibraryEntry> itemLibraryEntrys(const QString &type);
int getWidth(const ItemLibraryEntry &entry);
int getHeight(const ItemLibraryEntry &entry);
QPixmap createDragPixmap(int width, int height);
QWeakPointer<QScriptEngine> m_scriptEngine;
MetaInfo *m_metaInfo;
......
......@@ -171,8 +171,9 @@ void IdItemDelegate::paint(QPainter *painter,
if (icon.isNull())
{
// if node has no own icon, search for it in the itemlibrary
QList <ItemLibraryEntry> InfoList = node.metaInfo().metaInfo().itemLibraryEntrys(node.metaInfo());
foreach (const ItemLibraryEntry &entry, InfoList)
const ItemLibraryInfo libraryInfo = node.metaInfo().metaInfo().itemLibraryInfo();
QList <ItemLibraryEntry> infoList = libraryInfo.entriesForNodeMetaInfo(node.metaInfo());
foreach (const ItemLibraryEntry &entry, infoList)
{
if (entry.typeName()==node.metaInfo().typeName()) {
icon = entry.icon();
......
......@@ -45,7 +45,10 @@ class NodeMetaInfo;
namespace Internal {
class MetaInfoPrivate;
class ItemLibraryEntryData;
class ItemLibraryInfoPrivate;
}
class ItemLibraryEntry;
......@@ -93,8 +96,32 @@ private:
QExplicitlySharedDataPointer<Internal::ItemLibraryEntryData> m_data;
};
}
Q_DECLARE_METATYPE(QmlDesigner::ItemLibraryEntry)
class CORESHARED_EXPORT ItemLibraryInfo
{
friend class Internal::MetaInfoPrivate;
public:
ItemLibraryInfo();
ItemLibraryInfo(const ItemLibraryInfo &other);
~ItemLibraryInfo();
ItemLibraryInfo& operator=(const ItemLibraryInfo &other);
bool isValid();
QList<ItemLibraryEntry> entries() const;
QList<ItemLibraryEntry> entriesForNodeMetaInfo(const NodeMetaInfo &nodeMetaInfo) const;
ItemLibraryEntry entry(const QString &name) const;
ItemLibraryEntry addItemLibraryEntry(const NodeMetaInfo &nodeMetaInfo, const QString &itemLibraryRepresentationName);
void remove(const NodeMetaInfo &nodeMetaInfo);
void clear();
private:
static ItemLibraryInfo createItemLibraryInfo(const ItemLibraryInfo &parentInfo);
QSharedPointer<Internal::ItemLibraryInfoPrivate> m_data;
};
} // namespace QmlDesigner
#endif // ITEMLIBRARYINFO_H
......@@ -47,6 +47,7 @@ namespace QmlDesigner {
class ModelNode;
class AbstractProperty;
class ItemLibraryInfo;
namespace Internal {
class MetaInfoPrivate;
......@@ -58,6 +59,7 @@ namespace Internal {
CORESHARED_EXPORT bool operator==(const MetaInfo &first, const MetaInfo &second);
CORESHARED_EXPORT bool operator!=(const MetaInfo &first, const MetaInfo &second);
class CORESHARED_EXPORT MetaInfo
{
friend class QmlDesigner::Internal::MetaInfoPrivate;
......@@ -82,11 +84,11 @@ public:
bool hasEnumerator(const QString &enumeratorName) const;
EnumeratorMetaInfo enumerator(const QString &enumeratorName) const;
QStringList itemLibraryItems() const;
QList<ItemLibraryEntry> itemLibraryEntrys(const NodeMetaInfo &nodeMetaInfo) const;
ItemLibraryEntry itemLibraryEntry(const QString &name) const;
ItemLibraryInfo itemLibraryInfo() const;
QString fromQtTypes(const QString &type) const;
public:
static MetaInfo global();
static void clearGlobal();
......@@ -107,7 +109,6 @@ private:
EnumeratorMetaInfo addEnumerator(const QString &enumeratorScope, const QString &enumeratorName);
EnumeratorMetaInfo addFlag(const QString &enumeratorScope, const QString &enumeratorName);
ItemLibraryEntry addItemLibraryEntry(const NodeMetaInfo &nodeMetaInfo, const QString &itemLibraryEntryName);
bool isGlobal() const;
......
......@@ -95,7 +95,6 @@ public:
bool hasProperty(const QString &propertyName, bool resolveDotSyntax = false) const;
bool isContainer() const;
bool isVisibleToItemLibrary() const;
bool isWidget() const;
bool isGraphicsWidget() const;
......@@ -105,7 +104,6 @@ public:
bool isSubclassOf(const QString& type, int majorVersion = 4, int minorVersion = 6) const;
QIcon icon() const;
QString category() const;
private:
NodeMetaInfo();
......@@ -115,9 +113,7 @@ private:
void setTypeName(const QString &typeName);
void addProperty(const PropertyMetaInfo &property);
void setIsContainer(bool isContainer);
void setIsVisibleToItemLibrary(bool isVisibleToItemLibrary);
void setIcon(const QIcon &icon);
void setCategory(const QString &category);
void setQmlFile(const QString &filePath);
void setDefaultProperty(const QString &defaultProperty);
void setMajorVersion(int version);
......
......@@ -28,13 +28,9 @@
**************************************************************************/
#include "itemlibraryinfo.h"
#include "model/internalproperty.h"
#include "nodemetainfo.h"
#include <QSharedData>
#include <QString>
#include <QList>
#include <QtDebug>
#include <QIcon>
namespace QmlDesigner {
......@@ -55,7 +51,24 @@ public:
QList<PropertyContainer> properties;
QString qml;
};
}
class ItemLibraryInfoPrivate
{
public:
typedef QSharedPointer<ItemLibraryInfoPrivate> Pointer;
typedef QSharedPointer<ItemLibraryInfoPrivate> WeakPointer;
QMultiHash<NodeMetaInfo, ItemLibraryEntry> itemLibraryInfoHash;
QHash<QString, ItemLibraryEntry> itemLibraryInfoHashAll;
Pointer parentData;
};
} // namespace Internal
//
// ItemLibraryEntry
//
ItemLibraryEntry::ItemLibraryEntry(const ItemLibraryEntry &other)
: m_data(other.m_data)
......@@ -204,5 +217,103 @@ QDataStream& operator>>(QDataStream& stream, ItemLibraryEntry &itemLibraryEntry)
return stream;
}
//
// ItemLibraryInfo
//
ItemLibraryInfo::ItemLibraryInfo(const ItemLibraryInfo &other) :
m_data(other.m_data)
{
}
ItemLibraryInfo::ItemLibraryInfo() :
m_data(new Internal::ItemLibraryInfoPrivate())
{
}
ItemLibraryInfo::~ItemLibraryInfo()
{
}
ItemLibraryInfo& ItemLibraryInfo::operator=(const ItemLibraryInfo &other)
{
m_data = other.m_data;
return *this;
}
bool ItemLibraryInfo::isValid()
{
return m_data;
}
ItemLibraryInfo ItemLibraryInfo::createItemLibraryInfo(const ItemLibraryInfo &parentInfo)
{
ItemLibraryInfo info;
Q_ASSERT(parentInfo.m_data);
info.m_data->parentData = parentInfo.m_data;
return info;
}
QList<ItemLibraryEntry> ItemLibraryInfo::entriesForNodeMetaInfo(const NodeMetaInfo &nodeMetaInfo) const
{
QList<ItemLibraryEntry> itemLibraryItems;
Internal::ItemLibraryInfoPrivate::WeakPointer pointer(m_data);
while (pointer) {
itemLibraryItems += pointer->itemLibraryInfoHash.values(nodeMetaInfo);
pointer = pointer->parentData;
}
return itemLibraryItems;
}
ItemLibraryEntry ItemLibraryInfo::entry(const QString &name) const
{
Internal::ItemLibraryInfoPrivate::WeakPointer pointer(m_data);
while (pointer) {
if (pointer->itemLibraryInfoHashAll.contains(name))
return pointer->itemLibraryInfoHashAll.value(name);
pointer = pointer->parentData;
}
return ItemLibraryEntry();
}
QList<ItemLibraryEntry> ItemLibraryInfo::entries() const
{
QList<ItemLibraryEntry> list;
Internal::ItemLibraryInfoPrivate::WeakPointer pointer(m_data);
while (pointer) {
list += pointer->itemLibraryInfoHashAll.values();
pointer = pointer->parentData;
}
return list;
}
ItemLibraryEntry ItemLibraryInfo::addItemLibraryEntry(const NodeMetaInfo &nodeMetaInfo,
const QString &itemLibraryRepresentationName)
{
ItemLibraryEntry itemLibraryType;
itemLibraryType.setName(itemLibraryRepresentationName);
itemLibraryType.setTypeName(nodeMetaInfo.typeName());
itemLibraryType.setMajorVersion(nodeMetaInfo.majorVersion());
itemLibraryType.setMinorVersion(nodeMetaInfo.minorVersion());
m_data->itemLibraryInfoHash.insert(nodeMetaInfo, itemLibraryType);
m_data->itemLibraryInfoHashAll.insert(itemLibraryRepresentationName, itemLibraryType);
return itemLibraryType;
}
void ItemLibraryInfo::remove(const NodeMetaInfo &info)
{
m_data->itemLibraryInfoHash.remove(info);
m_data->itemLibraryInfoHashAll.remove(info.typeName());
}
void ItemLibraryInfo::clear()
{
m_data->itemLibraryInfoHash.clear();
m_data->itemLibraryInfoHashAll.clear();
}
} // namespace QmlDesigner
......@@ -82,9 +82,8 @@ public:
QMultiHash<QString, QString> m_superClassHash; // the list of direct superclasses
QHash<QString, NodeMetaInfo> m_nodeMetaInfoHash;
QHash<QString, EnumeratorMetaInfo> m_enumeratorMetaInfoHash;
QMultiHash<NodeMetaInfo, ItemLibraryEntry> m_itemLibraryInfoHash;
QHash<QString, ItemLibraryEntry> m_itemLibraryInfoHashAll;
QHash<QString, QString> m_QtTypesToQmlTypes;
ItemLibraryInfo m_itemLibraryInfo;
MetaInfo *m_q;
bool m_isInitialized;
......@@ -94,6 +93,8 @@ MetaInfoPrivate::MetaInfoPrivate(MetaInfo *q) :
m_q(q),
m_isInitialized(false)
{
if (!m_q->isGlobal())
m_itemLibraryInfo = ItemLibraryInfo::createItemLibraryInfo(m_q->global().itemLibraryInfo());
}
void MetaInfoPrivate::clear()
......@@ -101,8 +102,7 @@ void MetaInfoPrivate::clear()
m_superClassHash.clear();
m_nodeMetaInfoHash.clear();
m_enumeratorMetaInfoHash.clear();
m_itemLibraryInfoHash.clear();
m_itemLibraryInfoHashAll.clear();
m_itemLibraryInfo.clear();
m_isInitialized = false;
}
......@@ -484,23 +484,6 @@ QList<NodeMetaInfo> MetaInfo::directSuperClasses(const NodeMetaInfo &nodeInfo) c
return superClassList;
}
QList<ItemLibraryEntry> MetaInfo::itemLibraryEntrys(const NodeMetaInfo &nodeMetaInfo) const
{
QList<ItemLibraryEntry> itemLibraryItems = m_p->m_itemLibraryInfoHash.values(nodeMetaInfo);
if (!isGlobal())
itemLibraryItems += global().itemLibraryEntrys(nodeMetaInfo);
return itemLibraryItems;
}
ItemLibraryEntry MetaInfo::itemLibraryEntry(const QString &name) const
{
if (m_p->m_itemLibraryInfoHashAll.contains(name))
return m_p->m_itemLibraryInfoHashAll.value(name);
if (!isGlobal())
return global().itemLibraryEntry(name);
return ItemLibraryEntry();
}
QString MetaInfo::fromQtTypes(const QString &type) const
{
if (m_p->m_QtTypesToQmlTypes.contains(type))
......@@ -509,21 +492,6 @@ QString MetaInfo::fromQtTypes(const QString &type) const
return type;
}
QStringList MetaInfo::itemLibraryItems() const
{
QStringList completeList = m_p->m_nodeMetaInfoHash.keys();
QStringList finalList;
foreach (const QString &name, completeList) {
if (nodeMetaInfo(name).isVisibleToItemLibrary())
finalList.append(name);
}
if (!isGlobal())
finalList += global().itemLibraryItems();
return finalList;
}
/*!
\brief Returns whether className is the same type or a type derived from superClassName.
*/
......@@ -566,6 +534,11 @@ EnumeratorMetaInfo MetaInfo::enumerator(const QString &enumeratorName) const
return EnumeratorMetaInfo();
}
ItemLibraryInfo MetaInfo::itemLibraryInfo() const
{
return m_p->m_itemLibraryInfo;
}
/*!
\brief Access to the global meta information object.
You almost always want to use Model::metaInfo() instead.
......@@ -635,8 +608,7 @@ void MetaInfo::removeNodeInfo(NodeMetaInfo &info)
m_p->m_superClassHash.remove(info.typeName());
// TODO: Other types might specify type as parent type
m_p->m_itemLibraryInfoHash.remove(info);
m_p->m_itemLibraryInfoHashAll.remove(info.typeName());
m_p->m_itemLibraryInfo.remove(info);
} else if (!isGlobal()) {
global().removeNodeInfo(info);
......@@ -680,18 +652,6 @@ EnumeratorMetaInfo MetaInfo::addFlag(const QString &enumeratorScope, const QStri
return enumeratorMetaInfo;
}
ItemLibraryEntry MetaInfo::addItemLibraryEntry(const NodeMetaInfo &nodeMetaInfo, const QString &itemLibraryEntryName)
{
ItemLibraryEntry itemLibraryEntry;
itemLibraryEntry.setName(itemLibraryEntryName);
itemLibraryEntry.setTypeName(nodeMetaInfo.typeName());
itemLibraryEntry.setMajorVersion(nodeMetaInfo.majorVersion());
itemLibraryEntry.setMinorVersion(nodeMetaInfo.minorVersion());
m_p->m_itemLibraryInfoHash.insert(nodeMetaInfo, itemLibraryEntry);
m_p->m_itemLibraryInfoHashAll.insert(itemLibraryEntryName, itemLibraryEntry);
return itemLibraryEntry;
}
bool MetaInfo::isGlobal() const
{
return (this->m_p == s_global.m_p);
......@@ -706,4 +666,5 @@ bool operator!=(const MetaInfo &first, const MetaInfo &second)
{
return !(first == second);
}
} //namespace QmlDesigner
......@@ -179,16 +179,6 @@ void MetaInfoParser::handleNodeElement(QXmlStreamReader &reader)
nodeMetaInfo.setIsContainer(stringToBool(isContainer));
}
if (attributes.hasAttribute("showInItemLibrary")) {
const QString showInItemLibrary = attributes.value("showInItemLibrary").toString();
nodeMetaInfo.setIsVisibleToItemLibrary(stringToBool(showInItemLibrary));
}
if (attributes.hasAttribute("category")) {
const QString category = attributes.value("category").toString();
nodeMetaInfo.setCategory(category);
}
if (attributes.hasAttribute("icon")) {
const QString iconPath = reader.attributes().value("icon").toString();
nodeMetaInfo.setIcon(QIcon(iconPath));
......@@ -209,12 +199,16 @@ void MetaInfoParser::handleNodeItemLibraryEntryElement(QXmlStreamReader &reader,
if (reader.isStartElement() && reader.name() == "itemlibraryentry")
{
QString name = reader.attributes().value("name").toString();
ItemLibraryEntry itemLibraryEntry = m_metaInfo.addItemLibraryEntry(m_metaInfo.nodeMetaInfo(className), name);
ItemLibraryEntry itemLibraryEntry = m_metaInfo.itemLibraryInfo().addItemLibraryEntry(m_metaInfo.nodeMetaInfo(className), name);
QString iconPath = reader.attributes().value("icon").toString();
if (!iconPath.isEmpty())