<commit_before>/*!
 * This file is part of CameraPlus.
 *
 * Copyright (C) 2012-2013 <NAME> <<EMAIL>>
 *
 * 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 <QPainter>
#include <QDir>
#if defined(QT4)
#include <QDeclarativeInfo>
#elif defined(QT5)
#include <QQmlInfo>
#endif

#if defined(QT5)
QuillItem::QuillItem(QQuickItem *parent) :
  QQuickPaintedItem(parent),
#else
QuillItem::QuillItem(QDeclarativeItem *parent) :
  QDeclarativeItem(parent),
#endif
  m_file(0),
  m_error(false),
  m_displayLevel(DisplayLevelInvalid),
  m_priority(PriorityLow) {

#if defined(QT4)
  setFlag(QGraphicsItem::ItemHasNoContents, false);
#endif

}

QuillItem::~QuillItem() {
  if (m_file) {
    delete m_file;
    m_file = 0;
  }
}

void QuillItem::componentComplete() {
#if defined(QT4)
  QDeclarativeItem::componentComplete();
#elif defined(QT5)
  QQuickPaintedItem::componentComplete();
#endif

  m_file = new QuillFile(m_url.toLocalFile(), m_mimeType);
  m_file->setPriority(m_priority == PriorityHigh ?
		      QuillFile::Priority_High : QuillFile::Priority_Low);

  QObject::connect(m_file, SIGNAL(error(QuillError)),
		   this, SLOT(fileError()), Qt::QueuedConnection);
  QObject::connect(m_file, SIGNAL(imageAvailable(QuillImageList)),
		   this, SLOT(fileLoaded()), Qt::QueuedConnection);
  QObject::connect(m_file, SIGNAL(removed()),
	   m_file, SLOT(deleteLater()), Qt::QueuedConnection);

  if (fileError()) {
    return;
  }

  if (m_displayLevel == DisplayLevelInvalid) {
    qmlInfo(this) << "display level not set";
    return;
  }

  m_file->setDisplayLevel(m_displayLevel);

  fileError();
}

QUrl QuillItem::url() const {
  return m_url;
}

void QuillItem::setUrl(const QUrl& url) {
  if (url == m_url) {
    return;
  }

  if (m_file) {
    qmlInfo(this) << "url cannot be set after initialization";
    return;
  }

  m_url = url;

  emit urlChanged();
}

QString QuillItem::mimeType() const {
  return m_mimeType;
}

void QuillItem::setMimeType(const QString& mimeType) {
  if (mimeType == m_mimeType) {
    return;
  }

  if (m_file) {
    qmlInfo(this) << "mime type cannot be set after initialization";
    return;
  }

  m_mimeType = mimeType;

  emit mimeTypeChanged();
}

QuillItem::DisplayLevel QuillItem::displayLevel() const {
  return m_displayLevel;
}

void QuillItem::setDisplayLevel(const QuillItem::DisplayLevel& level) {
  if (m_displayLevel == level) {
    return;
  }

  m_displayLevel = level;

  if (m_file) {
    m_file->setDisplayLevel(level);
    fileError();
  }

  emit displayLevelChanged();
}

QuillItem::Priority QuillItem::priority() const {
  return m_priority;
}

void QuillItem::setPriority(const QuillItem::Priority& priority) {
  if (priority == m_priority) {
    return;
  }

  m_priority = priority;

  if (m_file) {
    m_file->setPriority(m_priority == PriorityHigh ?
			QuillFile::Priority_High : QuillFile::Priority_Low);
    fileError();
  }

  emit priorityChanged();
}

bool QuillItem::error() const {
  return m_error;
}

#if defined(QT4)
void QuillItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
  Q_UNUSED(widget);
  Q_UNUSED(option);
#elif defined(QT5)
void QuillItem::paint(QPainter* painter) {
#endif

  QRectF rect = boundingRect();
  painter->fillRect(rect, Qt::black);

  if (!m_file) {
    return;
  }

  QImage image = m_file->image(m_displayLevel);

  if (image.isNull()) {
    if (m_displayLevel == QuillItem::DisplayLevelLarge
	&& m_file->hasThumbnail(QuillItem::DisplayLevelFullScreen)) {
      image = m_file->image(QuillItem::DisplayLevelFullScreen);
      qmlInfo(this) << "Using fullscreen preview until the large preview gets loaded";
    } else {
      return;
    }
  }

  QSizeF imageSize = QSizeF(image.size());
  QSizeF widgetSize = rect.size();<commit_msg>remove the debug output about using the lower preview level<commit_after>/*!
 * This file is part of CameraPlus.
 *
 * Copyright (C) 2012-2013 <NAME> <<EMAIL>>
 *
 * 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 <QPainter>
#include <QDir>
#if defined(QT4)
#include <QDeclarativeInfo>
#elif defined(QT5)
#include <QQmlInfo>
#endif

#if defined(QT5)
QuillItem::QuillItem(QQuickItem *parent) :
  QQuickPaintedItem(parent),
#else
QuillItem::QuillItem(QDeclarativeItem *parent) :
  QDeclarativeItem(parent),
#endif
  m_file(0),
  m_error(false),
  m_displayLevel(DisplayLevelInvalid),
  m_priority(PriorityLow) {

#if defined(QT4)
  setFlag(QGraphicsItem::ItemHasNoContents, false);
#endif

}

QuillItem::~QuillItem() {
  if (m_file) {
    delete m_file;
    m_file = 0;
  }
}

void QuillItem::componentComplete() {
#if defined(QT4)
  QDeclarativeItem::componentComplete();
#elif defined(QT5)
  QQuickPaintedItem::componentComplete();
#endif

  m_file = new QuillFile(m_url.toLocalFile(), m_mimeType);
  m_file->setPriority(m_priority == PriorityHigh ?
		      QuillFile::Priority_High : QuillFile::Priority_Low);

  QObject::connect(m_file, SIGNAL(error(QuillError)),
		   this, SLOT(fileError()), Qt::QueuedConnection);
  QObject::connect(m_file, SIGNAL(imageAvailable(QuillImageList)),
		   this, SLOT(fileLoaded()), Qt::QueuedConnection);
  QObject::connect(m_file, SIGNAL(removed()),
	   m_file, SLOT(deleteLater()), Qt::QueuedConnection);

  if (fileError()) {
    return;
  }

  if (m_displayLevel == DisplayLevelInvalid) {
    qmlInfo(this) << "display level not set";
    return;
  }

  m_file->setDisplayLevel(m_displayLevel);

  fileError();
}

QUrl QuillItem::url() const {
  return m_url;
}

void QuillItem::setUrl(const QUrl& url) {
  if (url == m_url) {
    return;
  }

  if (m_file) {
    qmlInfo(this) << "url cannot be set after initialization";
    return;
  }

  m_url = url;

  emit urlChanged();
}

QString QuillItem::mimeType() const {
  return m_mimeType;
}

void QuillItem::setMimeType(const QString& mimeType) {
  if (mimeType == m_mimeType) {
    return;
  }

  if (m_file) {
    qmlInfo(this) << "mime type cannot be set after initialization";
    return;
  }

  m_mimeType = mimeType;

  emit mimeTypeChanged();
}

QuillItem::DisplayLevel QuillItem::displayLevel() const {
  return m_displayLevel;
}

void QuillItem::setDisplayLevel(const QuillItem::DisplayLevel& level) {
  if (m_displayLevel == level) {
    return;
  }

  m_displayLevel = level;

  if (m_file) {
    m_file->setDisplayLevel(level);
    fileError();
  }

  emit displayLevelChanged();
}

QuillItem::Priority QuillItem::priority() const {
  return m_priority;
}

void QuillItem::setPriority(const QuillItem::Priority& priority) {
  if (priority == m_priority) {
    return;
  }

  m_priority = priority;

  if (m_file) {
    m_file->setPriority(m_priority == PriorityHigh ?
			QuillFile::Priority_High : QuillFile::Priority_Low);
    fileError();
  }

  emit priorityChanged();
}

bool QuillItem::error() const {
  return m_error;
}

#if defined(QT4)
void QuillItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
  Q_UNUSED(widget);
  Q_UNUSED(option);
#elif defined(QT5)
void QuillItem::paint(QPainter* painter) {
#endif

  QRectF rect = boundingRect();
  painter->fillRect(rect, Qt::black);

  if (!m_file) {
    return;
  }

  QImage image = m_file->image(m_displayLevel);

  if (image.isNull()) {
    if (m_displayLevel == QuillItem::DisplayLevelLarge
	&& m_file->hasThumbnail(QuillItem::DisplayLevelFullScreen)) {
      image = m_file->image(QuillItem::DisplayLevelFullScreen);
    } else {
      return;
    }
  }

  QSizeF imageSize = QSizeF(image.size());
  QSizeF widgetSize = rect.size();
{'fraction_non_alphanumeric': 0.12235632820285683, 'fraction_numerical': 0.006865241944413686, 'mean_word_length': 4.5445058317986495, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1953843', 'n_tokens_mistral': 3163, 'n_tokens_neox': 3022, 'n_words': 760}