+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
#include "quillitem.h"
#include <QuillFile>
#include <QUrl>
#include <QDir>
QuillItem::QuillItem(QDeclarativeItem *parent) :
- QDeclarativeItem(parent), m_file(0) {
+ QDeclarativeItem(parent), m_file(0), m_error(false) {
setFlag(QGraphicsItem::ItemHasNoContents, false);
-
- static bool init = false;
- if (!init) {
- Quill::setPreviewLevelCount(1);
- Quill::setPreviewSize(0, QSize(854, 480)); // TODO:
- Quill::setMinimumPreviewSize(0, QSize(854, 480)); // TODO:
- Quill::setThumbnailExtension("jpeg"); // TODO:
- Quill::setThumbnailFlavorName(0, "screen");
- Quill::setBackgroundRenderingColor(Qt::black);
- QString tempPath(QDir::homePath() + QDir::separator() + ".config" +
- QDir::separator() + "quill" + QDir::separator() + "tmp");
- QDir().mkpath(tempPath);
- Quill::setTemporaryFilePath(tempPath);
- init = true;
- }
}
QuillItem::~QuillItem() {
delete m_file; m_file = 0;
}
-void QuillItem::componentComplete() {
- QDeclarativeItem::componentComplete();
-
- recreate();
-}
-
-QUrl QuillItem::source() const {
- return m_url;
-}
-
-void QuillItem::setSource(const QUrl& src) {
- if (src == source()) {
- return;
- }
-
- m_url = src;
-
- if (isComponentComplete()) {
- recreate();
+void QuillItem::initialize(const QUrl& url, const QString& mimeType) {
+ if (m_error) {
+ m_error = false;
+ emit errorChanged();
}
- emit sourceChanged();
-}
-
-QString QuillItem::mimeType() const {
- return m_type;
-}
-
-void QuillItem::setMimeType(const QString& mime) {
- if (mimeType() == mime) {
- return;
- }
-
- m_type = mime;
-
- if (isComponentComplete()) {
- recreate();
- }
-
- emit mimeTypeChanged();
-}
-
-void QuillItem::recreate() {
if (m_file) {
m_file->deleteLater();
}
- m_file = new QuillFile(m_url.toLocalFile(), m_type);
+ m_file = new QuillFile(url.toLocalFile(), mimeType);
QObject::connect(m_file, SIGNAL(error(QuillError)),
- this, SLOT(fileError()), Qt::QueuedConnection);
+ this, SLOT(fileError()), Qt::QueuedConnection);
QObject::connect(m_file, SIGNAL(imageAvailable(QuillImageList)),
- this, SLOT(fileLoaded()), Qt::QueuedConnection);
+ this, SLOT(fileLoaded()), Qt::QueuedConnection);
QObject::connect(m_file, SIGNAL(removed()),
m_file, SLOT(deleteLater()), Qt::QueuedConnection);
}
}
+bool QuillItem::error() const {
+ return m_error;
+}
+
void QuillItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
Q_UNUSED(widget);
+ Q_UNUSED(option);
+
+ QRectF rect = boundingRect();
+ painter->fillRect(rect, Qt::black);
if (!m_file) {
return;
QImage image = m_file->image(0);
- if (!image.isNull()) {
- painter->drawImage(option->rect, image);
+ if (image.isNull()) {
+ return;
}
+
+ QSizeF imageSize = QSizeF(image.size());
+ QSizeF widgetSize = rect.size();
+ imageSize.scale(widgetSize, Qt::KeepAspectRatio);
+
+ QPointF pos = QPointF(widgetSize.width() - imageSize.width(),
+ widgetSize.height() - imageSize.height()) / 2;
+
+ painter->drawImage(QRectF(pos, imageSize), image, QRect(0, 0, image.width(), image.height()));
}
void QuillItem::fileLoaded() {
QuillError err = m_file->error();
if (err.errorCode() != QuillError::NoError) {
- qWarning() << "Error loading file" << m_file->fileName() << err.errorCode();
+ qWarning() << "Error loading file" << m_file->fileName()
+ << "Code" << err.errorCode() << "Source" << err.errorSource();
QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
Q_ARG(QString, err.errorData()));
+
m_file->deleteLater(); m_file = 0;
+
+ if (!m_error) {
+ m_error = true;
+
+ emit errorChanged();
+ }
+
return true;
}