Commit 8f909fec authored by Marco Bubke's avatar Marco Bubke Committed by Alessandro Portale

QmlDesigner: Remove ItemLibraryWidgetPrivate

Change-Id: I64277786012821985ffaab2cabcec83d4e017e24
Reviewed-by: default avatarAlessandro Portale <alessandro.portale@nokia.com>
parent 41b03629
......@@ -30,10 +30,8 @@
#include "itemlibrarywidget.h"
#include <utils/filterlineedit.h>
#include <utils/fileutils.h>
#include <coreplugin/coreconstants.h>
#include "itemlibrarycomponents.h"
#include "itemlibrarymodel.h"
#include "itemlibraryimageprovider.h"
#include "customdraganddrop.h"
......@@ -42,7 +40,6 @@
#include "rewritingexception.h"
#include <QFileInfo>
#include <QFileIconProvider>
#include <QFileSystemModel>
#include <QStackedWidget>
#include <QGridLayout>
......@@ -53,131 +50,62 @@
#include <QMenu>
#include <QApplication>
#include <QDeclarativeView>
#include <QDeclarativeItem>
#include <private/qdeclarativeengine_p.h>
namespace QmlDesigner {
class MyFileIconProvider : public QFileIconProvider
{
public:
MyFileIconProvider(const QSize &iconSize)
: QFileIconProvider(),
m_iconSize(iconSize)
{}
virtual QIcon icon ( const QFileInfo & info ) const
{
QPixmap pixmap(info.absoluteFilePath());
if (pixmap.isNull()) {
QIcon defaultIcon(QFileIconProvider::icon(info));
pixmap = defaultIcon.pixmap(defaultIcon.actualSize(m_iconSize));
}
if (pixmap.width() == m_iconSize.width()
&& pixmap.height() == m_iconSize.height())
return pixmap;
if ((pixmap.width() > m_iconSize.width())
|| (pixmap.height() > m_iconSize.height()))
return pixmap.scaled(m_iconSize, Qt::KeepAspectRatio,
Qt::SmoothTransformation);
QPoint offset((m_iconSize.width() - pixmap.width()) / 2,
(m_iconSize.height() - pixmap.height()) / 2);
QImage newIcon(m_iconSize, QImage::Format_ARGB32_Premultiplied);
newIcon.fill(Qt::transparent);
QPainter painter(&newIcon);
painter.drawPixmap(offset, pixmap);
return QPixmap::fromImage(newIcon);
}
private:
QSize m_iconSize;
};
// ---------- ItemLibraryPrivate
class ItemLibraryWidgetPrivate {
public:
ItemLibraryWidgetPrivate(QObject *object);
Internal::ItemLibraryModel *m_itemLibraryModel;
QFileSystemModel *m_resourcesFileSystemModel;
QStackedWidget *m_stackedWidget;
Utils::FilterLineEdit *m_lineEdit;
QDeclarativeView *m_itemsView;
Internal::ItemLibraryTreeView *m_resourcesView;
QWeakPointer<ItemLibraryInfo> m_itemLibraryInfo;
QSize m_itemIconSize, m_resIconSize;
MyFileIconProvider m_iconProvider;
Model *model;
};
ItemLibraryWidgetPrivate::ItemLibraryWidgetPrivate(QObject *object) :
m_itemLibraryModel(0),
m_resourcesFileSystemModel(0),
m_stackedWidget(0),
m_lineEdit(0),
m_itemsView(0),
m_resourcesView(0),
m_itemIconSize(24, 24),
m_resIconSize(24, 24),
m_iconProvider(m_resIconSize),
model(0)
{
Q_UNUSED(object);
}
ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) :
QFrame(parent),
d(new ItemLibraryWidgetPrivate(this)),
m_iconProvider(m_resIconSize),
m_itemIconSize(24, 24),
m_resIconSize(24, 24),
m_itemsView(new QDeclarativeView(this)),
m_resourcesView(new Internal::ItemLibraryTreeView(this)),
m_filterFlag(QtBasic)
{
setWindowTitle(tr("Library", "Title of library view"));
/* create Items view and its model */
d->m_itemsView = new QDeclarativeView(this);
d->m_itemsView->setAttribute(Qt::WA_OpaquePaintEvent);
d->m_itemsView->setAttribute(Qt::WA_NoSystemBackground);
d->m_itemsView->setAcceptDrops(false);
d->m_itemsView->setFocusPolicy(Qt::ClickFocus);
d->m_itemsView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
d->m_itemLibraryModel = new Internal::ItemLibraryModel(QDeclarativeEnginePrivate::getScriptEngine(d->m_itemsView->engine()), this);
d->m_itemLibraryModel->setItemIconSize(d->m_itemIconSize);
QDeclarativeContext *rootContext = d->m_itemsView->rootContext();
rootContext->setContextProperty(QLatin1String("itemLibraryModel"), d->m_itemLibraryModel);
rootContext->setContextProperty(QLatin1String("itemLibraryIconWidth"), d->m_itemIconSize.width());
rootContext->setContextProperty(QLatin1String("itemLibraryIconHeight"), d->m_itemIconSize.height());
m_itemsView->setAttribute(Qt::WA_OpaquePaintEvent);
m_itemsView->setAttribute(Qt::WA_NoSystemBackground);
m_itemsView->setAcceptDrops(false);
m_itemsView->setFocusPolicy(Qt::ClickFocus);
m_itemsView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
m_itemLibraryModel = new Internal::ItemLibraryModel(QDeclarativeEnginePrivate::getScriptEngine(m_itemsView->engine()), this);
m_itemLibraryModel->setItemIconSize(m_itemIconSize);
QDeclarativeContext *rootContext = m_itemsView->rootContext();
rootContext->setContextProperty(QLatin1String("itemLibraryModel"), m_itemLibraryModel.data());
rootContext->setContextProperty(QLatin1String("itemLibraryIconWidth"), m_itemIconSize.width());
rootContext->setContextProperty(QLatin1String("itemLibraryIconHeight"), m_itemIconSize.height());
QColor highlightColor = palette().highlight().color();
if (0.5*highlightColor.saturationF()+0.75-highlightColor.valueF() < 0)
highlightColor.setHsvF(highlightColor.hsvHueF(),0.1 + highlightColor.saturationF()*2.0, highlightColor.valueF());
d->m_itemsView->rootContext()->setContextProperty(QLatin1String("highlightColor"), highlightColor);
m_itemsView->rootContext()->setContextProperty(QLatin1String("highlightColor"), highlightColor);
// loading the qml has to come after all needed context properties are set
d->m_itemsView->setSource(QUrl("qrc:/ItemLibrary/qml/ItemsView.qml"));
m_itemsView->setSource(QUrl("qrc:/ItemLibrary/qml/ItemsView.qml"));
QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(d->m_itemsView->rootObject());
QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(m_itemsView->rootObject());
connect(rootItem, SIGNAL(itemSelected(int)), this, SLOT(showItemInfo(int)));
connect(rootItem, SIGNAL(itemDragged(int)), this, SLOT(startDragAndDrop(int)));
connect(this, SIGNAL(scrollItemsView(QVariant)), rootItem, SLOT(scrollView(QVariant)));
connect(this, SIGNAL(resetItemsView()), rootItem, SLOT(resetView()));
/* create Resources view and its model */
d->m_resourcesFileSystemModel = new QFileSystemModel(this);
d->m_resourcesFileSystemModel->setIconProvider(&d->m_iconProvider);
d->m_resourcesView = new Internal::ItemLibraryTreeView(this);
d->m_resourcesView->setModel(d->m_resourcesFileSystemModel);
d->m_resourcesView->setIconSize(d->m_resIconSize);
m_resourcesFileSystemModel = new QFileSystemModel(this);
m_resourcesFileSystemModel->setIconProvider(&m_iconProvider);
m_resourcesView->setModel(m_resourcesFileSystemModel.data());
m_resourcesView->setIconSize(m_resIconSize);
/* create image provider for loading item icons */
d->m_itemsView->engine()->addImageProvider(QLatin1String("qmldesigner_itemlibrary"), new Internal::ItemLibraryImageProvider);
m_itemsView->engine()->addImageProvider(QLatin1String("qmldesigner_itemlibrary"), new Internal::ItemLibraryImageProvider);
/* other widgets */
QTabBar *tabBar = new QTabBar(this);
......@@ -185,12 +113,12 @@ ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) :
tabBar->addTab(tr("Resources", "Title of library resources view"));
tabBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
d->m_lineEdit = new Utils::FilterLineEdit(this);
d->m_lineEdit->setObjectName(QLatin1String("itemLibrarySearchInput"));
d->m_lineEdit->setPlaceholderText(tr("<Filter>", "Library search input hint text"));
d->m_lineEdit->setDragEnabled(false);
d->m_lineEdit->setMinimumWidth(75);
d->m_lineEdit->setTextMargins(0, 0, 20, 0);
m_lineEdit = new Utils::FilterLineEdit(this);
m_lineEdit->setObjectName(QLatin1String("itemLibrarySearchInput"));
m_lineEdit->setPlaceholderText(tr("<Filter>", "Library search input hint text"));
m_lineEdit->setDragEnabled(false);
m_lineEdit->setMinimumWidth(75);
m_lineEdit->setTextMargins(0, 0, 20, 0);
QWidget *lineEditFrame = new QWidget(this);
lineEditFrame->setObjectName(QLatin1String("itemLibrarySearchInputFrame"));
QGridLayout *lineEditLayout = new QGridLayout(lineEditFrame);
......@@ -198,15 +126,15 @@ ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) :
lineEditLayout->setSpacing(0);
lineEditLayout->addItem(new QSpacerItem(5, 3, QSizePolicy::Fixed, QSizePolicy::Fixed), 0, 0, 1, 3);
lineEditLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Fixed, QSizePolicy::Fixed), 1, 0);
lineEditLayout->addWidget(d->m_lineEdit, 1, 1, 1, 1);
lineEditLayout->addWidget(m_lineEdit.data(), 1, 1, 1, 1);
lineEditLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Fixed, QSizePolicy::Fixed), 1, 2);
connect(d->m_lineEdit, SIGNAL(filterChanged(QString)), this, SLOT(setSearchFilter(QString)));
connect(m_lineEdit.data(), SIGNAL(filterChanged(QString)), this, SLOT(setSearchFilter(QString)));
d->m_stackedWidget = new QStackedWidget(this);
d->m_stackedWidget->addWidget(d->m_itemsView);
d->m_stackedWidget->addWidget(d->m_resourcesView);
m_stackedWidget = new QStackedWidget(this);
m_stackedWidget->addWidget(m_itemsView.data());
m_stackedWidget->addWidget(m_resourcesView.data());
connect(tabBar, SIGNAL(currentChanged(int)),
d->m_stackedWidget, SLOT(setCurrentIndex(int)));
m_stackedWidget.data(), SLOT(setCurrentIndex(int)));
connect(tabBar, SIGNAL(currentChanged(int)),
this, SLOT(updateSearch()));
......@@ -220,37 +148,28 @@ ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) :
layout->addWidget(tabBar, 0, 0, 1, 1);
layout->addWidget(spacer, 1, 0);
layout->addWidget(lineEditFrame, 2, 0, 1, 1);
layout->addWidget(d->m_stackedWidget, 3, 0, 1, 1);
layout->addWidget(m_stackedWidget.data(), 3, 0, 1, 1);
setResourcePath(QDir::currentPath());
setSearchFilter(QString());
/* style sheets */
setStyleSheet(QLatin1String(Utils::FileReader::fetchQrc(":/qmldesigner/stylesheet.css")));
d->m_resourcesView->setStyleSheet(
m_resourcesView->setStyleSheet(
QLatin1String(Utils::FileReader::fetchQrc(":/qmldesigner/scrollbar.css")));
}
ItemLibraryWidget::~ItemLibraryWidget()
{
/* workaround: delete the items view before the model is deleted.
This prevents qml warnings when the item library is destructed. */
delete d->m_itemsView;
delete d->m_resourcesView;
delete d;
}
void ItemLibraryWidget::setItemLibraryInfo(ItemLibraryInfo *itemLibraryInfo)
{
if (d->m_itemLibraryInfo.data() == itemLibraryInfo)
if (m_itemLibraryInfo.data() == itemLibraryInfo)
return;
if (d->m_itemLibraryInfo)
disconnect(d->m_itemLibraryInfo.data(), SIGNAL(entriesChanged()),
if (m_itemLibraryInfo)
disconnect(m_itemLibraryInfo.data(), SIGNAL(entriesChanged()),
this, SLOT(updateModel()));
d->m_itemLibraryInfo = itemLibraryInfo;
m_itemLibraryInfo = itemLibraryInfo;
if (itemLibraryInfo)
connect(d->m_itemLibraryInfo.data(), SIGNAL(entriesChanged()),
connect(m_itemLibraryInfo.data(), SIGNAL(entriesChanged()),
this, SLOT(updateModel()));
updateModel();
......@@ -261,9 +180,9 @@ void ItemLibraryWidget::updateImports()
{
FilterChangeFlag filter;
filter = QtBasic;
if (d->model) {
if (m_model) {
QStringList imports;
foreach (const Import &import, d->model->imports())
foreach (const Import &import, m_model->imports())
if (import.isLibraryImport())
imports << import.url();
if (imports.contains("com.nokia.meego", Qt::CaseInsensitive))
......@@ -308,10 +227,10 @@ QList<QToolButton *> ItemLibraryWidget::createToolBarWidgets()
void ItemLibraryWidget::setSearchFilter(const QString &searchFilter)
{
if (d->m_stackedWidget->currentIndex() == 0) {
d->m_itemLibraryModel->setSearchText(searchFilter);
if (m_stackedWidget->currentIndex() == 0) {
m_itemLibraryModel->setSearchText(searchFilter);
emit resetItemsView();
d->m_itemsView->update();
m_itemsView->update();
} else {
QStringList nameFilterList;
if (searchFilter.contains('.')) {
......@@ -322,17 +241,17 @@ void ItemLibraryWidget::setSearchFilter(const QString &searchFilter)
}
}
d->m_resourcesFileSystemModel->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
d->m_resourcesFileSystemModel->setNameFilterDisables(false);
d->m_resourcesFileSystemModel->setNameFilters(nameFilterList);
d->m_resourcesView->expandToDepth(1);
d->m_resourcesView->scrollToTop();
m_resourcesFileSystemModel->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
m_resourcesFileSystemModel->setNameFilterDisables(false);
m_resourcesFileSystemModel->setNameFilters(nameFilterList);
m_resourcesView->expandToDepth(1);
m_resourcesView->scrollToTop();
}
}
void ItemLibraryWidget::setModel(Model *model)
{
d->model = model;
m_model = model;
if (!model)
return;
setItemLibraryInfo(model->metaInfo().itemLibraryInfo());
......@@ -341,13 +260,13 @@ void ItemLibraryWidget::setModel(Model *model)
void ItemLibraryWidget::emitImportChecked()
{
if (!d->model)
if (!m_model)
return;
bool qtOnlyImport = false;
bool meegoImport = false;
foreach (const Import &import, d->model->imports()) {
foreach (const Import &import, m_model->imports()) {
if (import.isLibraryImport()) {
if (import.url().contains(QString("meego"), Qt::CaseInsensitive))
meegoImport = true;
......@@ -368,7 +287,7 @@ void ItemLibraryWidget::setImportFilter(FilterChangeFlag flag)
return;
static bool block = false;
if (!d->model)
if (!m_model)
return;
if (flag == m_filterFlag)
return;
......@@ -414,36 +333,36 @@ void ItemLibraryWidget::onMeegoChecked(bool b)
void ItemLibraryWidget::updateModel()
{
d->m_itemLibraryModel->update(d->m_itemLibraryInfo.data(), d->model);
m_itemLibraryModel->update(m_itemLibraryInfo.data(), m_model.data());
updateImports();
updateSearch();
}
void ItemLibraryWidget::updateSearch()
{
setSearchFilter(d->m_lineEdit->text());
setSearchFilter(m_lineEdit->text());
}
void ItemLibraryWidget::setResourcePath(const QString &resourcePath)
{
if (d->m_resourcesView->model() == d->m_resourcesFileSystemModel) {
d->m_resourcesFileSystemModel->setRootPath(resourcePath);
d->m_resourcesView->setRootIndex(d->m_resourcesFileSystemModel->index(resourcePath));
if (m_resourcesView->model() == m_resourcesFileSystemModel.data()) {
m_resourcesFileSystemModel->setRootPath(resourcePath);
m_resourcesView->setRootIndex(m_resourcesFileSystemModel->index(resourcePath));
}
updateSearch();
}
void ItemLibraryWidget::startDragAndDrop(int itemLibId)
{
QMimeData *mimeData = d->m_itemLibraryModel->getMimeData(itemLibId);
QMimeData *mimeData = m_itemLibraryModel->getMimeData(itemLibId);
CustomItemLibraryDrag *drag = new CustomItemLibraryDrag(this);
const QImage image = qvariant_cast<QImage>(mimeData->imageData());
drag->setPixmap(d->m_itemLibraryModel->getIcon(itemLibId).pixmap(32, 32));
drag->setPixmap(m_itemLibraryModel->getIcon(itemLibId).pixmap(32, 32));
drag->setPreview(QPixmap::fromImage(image));
drag->setMimeData(mimeData);
QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(d->m_itemsView->rootObject());
QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(m_itemsView->rootObject());
connect(rootItem, SIGNAL(stopDragAndDrop()), drag, SLOT(stopDrag()));
drag->exec();
......@@ -456,32 +375,62 @@ void ItemLibraryWidget::showItemInfo(int /*itemLibId*/)
void ItemLibraryWidget::wheelEvent(QWheelEvent *event)
{
if (d->m_stackedWidget->currentIndex() == 0 &&
d->m_itemsView->rect().contains(event->pos())) {
if (m_stackedWidget->currentIndex() == 0 &&
m_itemsView->rect().contains(event->pos())) {
emit scrollItemsView(event->delta());
event->accept();
} else
QFrame::wheelEvent(event);
}
void ItemLibraryWidget::removeImport(const QString &name)
{
if (!d->model)
return;
void ItemLibraryWidget::removeImport(const QString &name)
{
if (!m_model)
return;
QList<Import> toBeRemovedImportList;
foreach (const Import &import, m_model->imports())
if (import.isLibraryImport() && import.url().compare(name, Qt::CaseInsensitive) == 0)
toBeRemovedImportList.append(import);
QList<Import> toBeRemovedImportList;
foreach (const Import &import, d->model->imports())
if (import.isLibraryImport() && import.url().compare(name, Qt::CaseInsensitive) == 0)
toBeRemovedImportList.append(import);
m_model->changeImports(QList<Import>(), toBeRemovedImportList);
}
d->model->changeImports(QList<Import>(), toBeRemovedImportList);
}
void ItemLibraryWidget::addImport(const QString &name, const QString &version)
{
if (!m_model)
return;
m_model->changeImports(QList<Import>() << Import::createLibraryImport(name, version), QList<Import>());
}
void ItemLibraryWidget::addImport(const QString &name, const QString &version)
{
if (!d->model)
return;
d->model->changeImports(QList<Import>() << Import::createLibraryImport(name, version), QList<Import>());
}
QIcon ItemLibraryFileIconProvider::icon(const QFileInfo &info) const
{
QPixmap pixmap(info.absoluteFilePath());
if (pixmap.isNull()) {
QIcon defaultIcon(QFileIconProvider::icon(info));
pixmap = defaultIcon.pixmap(defaultIcon.actualSize(m_iconSize));
}
if (pixmap.width() == m_iconSize.width()
&& pixmap.height() == m_iconSize.height())
return pixmap;
if ((pixmap.width() > m_iconSize.width())
|| (pixmap.height() > m_iconSize.height()))
return pixmap.scaled(m_iconSize, Qt::KeepAspectRatio,
Qt::SmoothTransformation);
QPoint offset((m_iconSize.width() - pixmap.width()) / 2,
(m_iconSize.height() - pixmap.height()) / 2);
QImage newIcon(m_iconSize, QImage::Format_ARGB32_Premultiplied);
newIcon.fill(Qt::transparent);
QPainter painter(&newIcon);
painter.drawPixmap(offset, pixmap);
return QPixmap::fromImage(newIcon);
}
ItemLibraryFileIconProvider::ItemLibraryFileIconProvider(const QSize &iconSize)
: QFileIconProvider(),
m_iconSize(iconSize)
{}
}
......@@ -32,16 +32,43 @@
#define ITEMLIBRARYWIDGET_H
#include "itemlibraryinfo.h"
#include "itemlibrarycomponents.h"
#include <utils/filterlineedit.h>
#include <QFrame>
#include <QToolButton>
#include <QFileIconProvider>
#include <QDeclarativeView>
QT_BEGIN_NAMESPACE
class QFileSystemModel;
class QStackedWidget;
QT_END_NAMESPACE
namespace QmlDesigner {
class ItemLibraryWidgetPrivate;
class MetaInfo;
class ItemLibraryEntry;
class Model;
namespace Internal {
class ItemLibraryModel;
class ItemLibraryTreeView;
}
class ItemLibraryFileIconProvider : public QFileIconProvider
{
public:
ItemLibraryFileIconProvider(const QSize &iconSize);
QIcon icon( const QFileInfo & info ) const;
private:
QSize m_iconSize;
};
class ItemLibraryWidget : public QFrame
{
Q_OBJECT
......@@ -53,7 +80,6 @@ class ItemLibraryWidget : public QFrame
public:
ItemLibraryWidget(QWidget *parent = 0);
virtual ~ItemLibraryWidget();
void setItemLibraryInfo(ItemLibraryInfo *itemLibraryInfo);
QList<QToolButton *> createToolBarWidgets();
......@@ -91,7 +117,21 @@ signals:
void meegoChecked(bool b);
private:
ItemLibraryWidgetPrivate *d;
ItemLibraryFileIconProvider m_iconProvider;
QSize m_itemIconSize;
QSize m_resIconSize;
QWeakPointer<ItemLibraryInfo> m_itemLibraryInfo;
QWeakPointer<Internal::ItemLibraryModel> m_itemLibraryModel;
QWeakPointer<QFileSystemModel> m_resourcesFileSystemModel;
QWeakPointer<QStackedWidget> m_stackedWidget;
QWeakPointer<Utils::FilterLineEdit> m_lineEdit;
QScopedPointer<QDeclarativeView> m_itemsView;
QScopedPointer<Internal::ItemLibraryTreeView> m_resourcesView;
QWeakPointer<Model> m_model;
FilterChangeFlag m_filterFlag;
};
......
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