Commit f0cb9846 authored by Bea Lam's avatar Bea Lam

Fix QmlDesigner following removal of Image::pixmap property. This creates

image providers to supply pixmaps so that the Image::source property can be
used instead.

Reviewed-by: kkoehne
parent e33b00c9
......@@ -32,6 +32,7 @@
#include <utils/filterlineedit.h>
#include "itemlibrarywidgets.h"
#include "itemlibrarymodel.h"
#include "itemlibraryimageprovider.h"
#include "customdraganddrop.h"
#include <QFileInfo>
......@@ -163,6 +164,9 @@ ItemLibrary::ItemLibrary(QWidget *parent) :
m_d->m_resourcesView->setModel(m_d->m_resourcesDirModel);
m_d->m_resourcesView->setIconSize(m_d->m_resIconSize);
/* create image provider for loading item icons */
m_d->m_itemsView->engine()->addImageProvider(QLatin1String("qmldesigner_itemlibrary"), new Internal::ItemLibraryImageProvider);
/* other widgets */
QTabBar *tabBar = new QTabBar(this);
tabBar->addTab(tr("Items", "Title of library items view"));
......
......@@ -6,8 +6,8 @@ VPATH += $$PWD
INCLUDEPATH += $$PWD
# Input
HEADERS += itemlibrary.h customdraganddrop.h itemlibrarymodel.h itemlibrarywidgets.h
SOURCES += itemlibrary.cpp customdraganddrop.cpp itemlibrarymodel.cpp itemlibrarywidgets.cpp
HEADERS += itemlibrary.h customdraganddrop.h itemlibrarymodel.h itemlibrarywidgets.h itemlibraryimageprovider.h
SOURCES += itemlibrary.cpp customdraganddrop.cpp itemlibrarymodel.cpp itemlibrarywidgets.cpp itemlibraryimageprovider.cpp
RESOURCES += itemlibrary.qrc
OTHER_FILES += \
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "itemlibraryimageprovider.h"
namespace QmlDesigner {
namespace Internal {
ItemLibraryImageProvider::ItemLibraryImageProvider() :
QDeclarativeImageProvider(QDeclarativeImageProvider::Pixmap)
{
}
QPixmap ItemLibraryImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
QPixmap pixmap(id);
if (size) {
size->setWidth(pixmap.width());
size->setHeight(pixmap.height());
}
if (requestedSize.isValid())
return pixmap.scaled(requestedSize);
return pixmap;
}
}
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef ITEMLIBRARYIMAGEPROVIDER_H
#define ITEMLIBRARYIMAGEPROVIDER_H
#include <QDeclarativeImageProvider>
namespace QmlDesigner {
namespace Internal {
class ItemLibraryImageProvider : public QDeclarativeImageProvider
{
public:
ItemLibraryImageProvider();
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
};
}
}
#endif // ITEMLIBRARYIMAGEPROVIDER_H
......@@ -176,7 +176,6 @@ ItemLibraryItemModel::ItemLibraryItemModel(QScriptEngine *scriptEngine, int item
m_scriptEngine(scriptEngine),
m_libId(itemLibId),
m_name(itemName),
m_icon(),
m_iconSize(64, 64)
{
QScriptValue pixmapScriptValue(m_scriptEngine->newVariant(QPixmap()));
......@@ -204,21 +203,18 @@ QString ItemLibraryItemModel::itemName() const
return m_name;
}
void ItemLibraryItemModel::setItemIcon(const QIcon &itemIcon)
void ItemLibraryItemModel::setItemIconPath(const QString &iconPath)
{
m_icon = itemIcon;
m_iconPath = iconPath;
QScriptValue pixmapScriptValue(m_scriptEngine->newVariant(m_icon.pixmap(m_iconSize)));
setProperty(QLatin1String("itemPixmap"), pixmapScriptValue);
setProperty(QLatin1String("itemLibraryIconPath"), "image://qmldesigner_itemlibrary/" + iconPath);
}
void ItemLibraryItemModel::setItemIconSize(const QSize &itemIconSize)
{
m_iconSize = itemIconSize;
// qDebug() << "set icon size" << itemIconSize;
setItemIcon(m_icon);
setItemIconPath(m_iconPath);
}
......@@ -414,12 +410,12 @@ void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo)
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.iconPath().isEmpty())
entry.setIconPath(QLatin1String(":/ItemLibrary/images/item-default-icon.png"));
if (entry.dragIcon().isNull())
entry.setDragIcon(createDragPixmap(getWidth(entry), getHeight(entry)));
itemModel->setItemIcon(entry.icon());
itemModel->setItemIconPath(entry.iconPath());
itemModel->setItemIconSize(m_itemIconSize);
sectionModel->addSectionEntry(itemModel);
m_sections.insert(itemId, sectionId);
......
......@@ -84,7 +84,7 @@ public:
int itemLibId() const;
QString itemName() const;
void setItemIcon(const QIcon &itemIcon);
void setItemIconPath(const QString &iconPath);
void setItemIconSize(const QSize &itemIconSize);
bool operator<(const ItemLibraryItemModel &other) const;
......@@ -93,7 +93,7 @@ private:
QWeakPointer<QScriptEngine> m_scriptEngine;
int m_libId;
QString m_name;
QIcon m_icon;
QString m_iconPath;
QSize m_iconSize;
};
......
......@@ -96,7 +96,7 @@ Item {
width: itemLibraryIconWidth // to be set in Qml context
height: itemLibraryIconHeight // to be set in Qml context
pixmap: itemPixmap // to be set by model
source: itemLibraryIconPath // to be set by model
}
Text {
......
......@@ -2,10 +2,12 @@ VPATH += $$PWD
INCLUDEPATH += $$PWD
SOURCES += stateseditorwidget.cpp \
stateseditormodel.cpp \
stateseditorview.cpp
stateseditorview.cpp \
stateseditorimageprovider.cpp
HEADERS += stateseditorwidget.h \
stateseditormodel.h \
stateseditorview.h
stateseditorview.h \
stateseditorimageprovider.cpp
RESOURCES += $$PWD/stateseditor.qrc
OTHER_FILES +=$$PWD/stateslist.qml \
$$PWD/HorizontalScrollBar.qml
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "stateseditorimageprovider.h"
#include "stateseditorview.h"
namespace QmlDesigner {
namespace Internal {
StatesEditorImageProvider::StatesEditorImageProvider(StatesEditorView *view) :
QDeclarativeImageProvider(QDeclarativeImageProvider::Pixmap),
m_view(view)
{
}
QPixmap StatesEditorImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
if (!m_view.isNull()) {
// discard the count number (see StatesEditorModel m_updateCounter)
QString s = id.mid(0, id.lastIndexOf(QLatin1Char('-')));
bool ok = false;
int state = s.toInt(&ok);
if (ok) {
QPixmap pm = m_view->renderState(state);
if (size)
*size = pm.size();
if (requestedSize.isValid())
return pm.scaled(requestedSize);
return pm;
}
}
return QPixmap();
}
}
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef STATESEDITORIMAGEPROVIDER_H
#define STATESEDITORIMAGEPROVIDER_H
#include <QDeclarativeImageProvider>
#include <QWeakPointer>
namespace QmlDesigner {
namespace Internal {
class StatesEditorView;
class StatesEditorImageProvider : public QDeclarativeImageProvider
{
public:
StatesEditorImageProvider(StatesEditorView *view);
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
private:
QWeakPointer<StatesEditorView> m_view;
};
}
}
#endif // STATESEDITORIMAGEPROVIDER_H
......@@ -42,12 +42,12 @@ namespace QmlDesigner {
namespace Internal {
StatesEditorModel::StatesEditorModel(QObject *parent) :
QAbstractListModel(parent)
QAbstractListModel(parent),
m_updateCounter(0)
{
QHash<int, QByteArray> roleNames;
roleNames.insert(StateNameRole, "stateName");
roleNames.insert(StatesPixmapRole, "statePixmap");
roleNames.insert(StateImageSourceRole, "stateImageSource");
setRoleNames(roleNames);
}
......@@ -78,11 +78,9 @@ QVariant StatesEditorModel::data(const QModelIndex &index, int role) const
result = m_stateNames.at(index.row());
break;
}
case StatesPixmapRole: {
// TODO: Maybe cache this?
if (!m_statesView.isNull()) {
result = m_statesView->renderState(index.row());
}
case StateImageSourceRole: {
if (!m_statesView.isNull())
return QString("image://qmldesigner_stateseditor/%1-%2").arg(index.row()).arg(m_updateCounter);
break;
}
}
......@@ -134,6 +132,14 @@ void StatesEditorModel::updateState(int i)
{
Q_ASSERT(i >= 0 && i < m_stateNames.count());
// QML images with the same URL are always cached, so this changes the URL each
// time to ensure the image is loaded from the StatesImageProvider and not the
// cache.
// TODO: only increase imageId when the scene has changed so that we can load
// from the cache instead where possible.
if (++m_updateCounter == INT_MAX)
m_updateCounter = 0;
emit dataChanged(createIndex(i, 0), createIndex(i, 0));
}
......
......@@ -47,7 +47,7 @@ class StatesEditorModel : public QAbstractListModel
enum {
StateNameRole = Qt::DisplayRole,
StatesPixmapRole = Qt::UserRole
StateImageSourceRole = Qt::UserRole,
};
public:
......@@ -71,6 +71,7 @@ signals:
private:
QList<QString> m_stateNames;
QWeakPointer<StatesEditorView> m_statesView;
int m_updateCounter;
};
} // namespace Itnernal
......
......@@ -30,6 +30,7 @@
#include "stateseditorwidget.h"
#include "stateseditormodel.h"
#include "stateseditorview.h"
#include "stateseditorimageprovider.h"
#include <qmlitemnode.h>
#include <invalidargumentexception.h>
......@@ -203,6 +204,10 @@ void StatesEditorWidget::setup(Model *model)
m_d->model = model;
if (m_d->statesEditorView.isNull())
m_d->statesEditorView = new Internal::StatesEditorView(m_d->statesEditorModel.data(), this);
m_d->listView->engine()->addImageProvider(
QLatin1String("qmldesigner_stateseditor"), new Internal::StatesEditorImageProvider(m_d->statesEditorView.data()));
m_d->statesEditorModel->setStatesEditorView(m_d->statesEditorView.data());
m_d->model->attachView(m_d->statesEditorView.data());
......
......@@ -145,7 +145,7 @@ Rectangle {
anchors.bottomMargin: 9
Image {
anchors.centerIn:parent
pixmap: statePixmap
source: stateImageSource
Rectangle {
anchors.fill:parent
color:"transparent"
......
......@@ -62,6 +62,7 @@ public:
QString name() const;
QString typeName() const;
QIcon icon() const;
QString iconPath() const;
int majorVersion() const;
int minorVersion() const;
QString category() const;
......@@ -77,7 +78,7 @@ public:
void setType(const QString &typeName, int majorVersion, int minorVersion);
void setName(const QString &name);
void setIcon(const QIcon &icon);
void setIconPath(const QString &iconPath);
void addProperty(const Property &p);
void addProperty(QString &name, QString &type, QString &value);
void setDragIcon(const QIcon &icon);
......
......@@ -46,6 +46,7 @@ public:
QString category;
int majorVersion;
int minorVersion;
QString iconPath;
QIcon icon;
QIcon dragIcon;
QList<PropertyContainer> properties;
......@@ -148,6 +149,11 @@ QIcon ItemLibraryEntry::icon() const
return m_data->icon;
}
QString ItemLibraryEntry::iconPath() const
{
return m_data->iconPath;
}
void ItemLibraryEntry::setName(const QString &name)
{
m_data->name = name;
......@@ -160,9 +166,9 @@ void ItemLibraryEntry::setType(const QString &typeName, int majorVersion, int mi
m_data->minorVersion = minorVersion;
}
void ItemLibraryEntry::setIcon(const QIcon &icon)
void ItemLibraryEntry::setIconPath(const QString &iconPath)
{
m_data->icon = icon;
m_data->iconPath = iconPath;
}
void ItemLibraryEntry::setQml(const QString &qml)
......@@ -184,6 +190,7 @@ QDataStream& operator<<(QDataStream& stream, const ItemLibraryEntry &itemLibrary
stream << itemLibraryEntry.majorVersion();
stream << itemLibraryEntry.minorVersion();
stream << itemLibraryEntry.icon();
stream << itemLibraryEntry.iconPath();
stream << itemLibraryEntry.category();
stream << itemLibraryEntry.dragIcon();
stream << itemLibraryEntry.m_data->properties;
......@@ -198,6 +205,7 @@ QDataStream& operator>>(QDataStream& stream, ItemLibraryEntry &itemLibraryEntry)
stream >> itemLibraryEntry.m_data->majorVersion;
stream >> itemLibraryEntry.m_data->minorVersion;
stream >> itemLibraryEntry.m_data->icon;
stream >> itemLibraryEntry.m_data->iconPath;
stream >> itemLibraryEntry.m_data->category;
stream >> itemLibraryEntry.m_data->dragIcon;
stream >> itemLibraryEntry.m_data->properties;
......
......@@ -205,8 +205,8 @@ void MetaInfoParser::handleNodeItemLibraryEntryElement(QXmlStreamReader &reader,
entry.setName(name);
QString iconPath = reader.attributes().value("icon").toString();
if (!iconPath.isEmpty())
entry.setIcon(QIcon(iconPath));
if (!iconPath.isEmpty())
entry.setIconPath(iconPath);
QString category = reader.attributes().value("category").toString();
if (!category.isEmpty())
......
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