2 * This file is part of CameraPlus.
4 * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "quillitem.h"
25 #include <QStyleOptionGraphicsItem>
28 QuillItem::QuillItem(QDeclarativeItem *parent) :
29 QDeclarativeItem(parent), m_file(0), m_error(false) {
31 setFlag(QGraphicsItem::ItemHasNoContents, false);
34 QuillItem::~QuillItem() {
35 delete m_file; m_file = 0;
38 void QuillItem::initialize(const QUrl& url, const QString& mimeType) {
45 m_file->deleteLater();
48 m_file = new QuillFile(url.toLocalFile(), mimeType);
50 QObject::connect(m_file, SIGNAL(error(QuillError)),
51 this, SLOT(fileError()), Qt::QueuedConnection);
52 QObject::connect(m_file, SIGNAL(imageAvailable(QuillImageList)),
53 this, SLOT(fileLoaded()), Qt::QueuedConnection);
54 QObject::connect(m_file, SIGNAL(removed()),
55 m_file, SLOT(deleteLater()), Qt::QueuedConnection);
61 m_file->setDisplayLevel(0);
68 bool QuillItem::error() const {
72 void QuillItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
76 QRectF rect = boundingRect();
77 painter->fillRect(rect, Qt::black);
83 QImage image = m_file->image(0);
89 QSizeF imageSize = QSizeF(image.size());
90 QSizeF widgetSize = rect.size();
91 imageSize.scale(widgetSize, Qt::KeepAspectRatio);
93 QPointF pos = QPointF(widgetSize.width() - imageSize.width(),
94 widgetSize.height() - imageSize.height()) / 2;
96 painter->drawImage(QRectF(pos, imageSize), image, QRect(0, 0, image.width(), image.height()));
99 void QuillItem::fileLoaded() {
103 bool QuillItem::fileError() {
108 QuillError err = m_file->error();
110 if (err.errorCode() != QuillError::NoError) {
111 qWarning() << "Error loading file" << m_file->fileName()
112 << "Code" << err.errorCode() << "Source" << err.errorSource();
114 QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
115 Q_ARG(QString, err.errorData()));
117 m_file->deleteLater(); m_file = 0;