File indexing completed on 2024-04-28 04:31:54

0001 /*
0002  * Copyright (C) 2010-2015 by Stephen Allewell
0003  * steve.allewell@gmail.com
0004  *
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 2 of the License, or
0008  * (at your option) any later version.
0009  */
0010 
0011 /** @file
0012  * This file implements a background image to be used as an overlay on a canvas for
0013  * the purpose of tracing.
0014  */
0015 
0016 // Class include
0017 #include "BackgroundImage.h"
0018 
0019 // Qt includes
0020 #include <QDataStream>
0021 
0022 // KF5 includes
0023 #include <KLocalizedString>
0024 
0025 // Application includes
0026 #include "Exceptions.h"
0027 
0028 BackgroundImage::BackgroundImage(const QUrl &url, const QRect &location)
0029     : m_url(url)
0030     , m_location(location)
0031     , m_visible(true)
0032 {
0033     m_status = m_image.load(m_url.path());
0034 
0035     if (m_status) {
0036         generateIcon();
0037     }
0038 }
0039 
0040 const QUrl &BackgroundImage::url() const
0041 {
0042     return m_url;
0043 }
0044 
0045 const QRect &BackgroundImage::location() const
0046 {
0047     return m_location;
0048 }
0049 
0050 bool BackgroundImage::isVisible() const
0051 {
0052     return m_visible;
0053 }
0054 
0055 bool BackgroundImage::isValid() const
0056 {
0057     return m_status;
0058 }
0059 
0060 const QImage &BackgroundImage::image() const
0061 {
0062     return m_image;
0063 }
0064 
0065 const QIcon &BackgroundImage::icon() const
0066 {
0067     return m_icon;
0068 }
0069 
0070 void BackgroundImage::setLocation(const QRect &location)
0071 {
0072     m_location = location;
0073 }
0074 
0075 void BackgroundImage::setVisible(bool visible)
0076 {
0077     m_visible = visible;
0078 }
0079 
0080 void BackgroundImage::generateIcon()
0081 {
0082     m_icon = QPixmap::fromImage(m_image).scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation);
0083 }
0084 
0085 QDataStream &operator<<(QDataStream &stream, const BackgroundImage &backgroundImage)
0086 {
0087     stream << qint32(backgroundImage.version);
0088     stream << backgroundImage.m_url;
0089     stream << backgroundImage.m_location;
0090     stream << backgroundImage.m_visible;
0091     stream << backgroundImage.m_status;
0092     stream << backgroundImage.m_image;
0093     return stream;
0094 }
0095 
0096 QDataStream &operator>>(QDataStream &stream, BackgroundImage &backgroundImage)
0097 {
0098     qint32 version;
0099 
0100     stream >> version;
0101 
0102     switch (version) {
0103     case 101:
0104         stream >> backgroundImage.m_url;
0105         stream >> backgroundImage.m_location;
0106         stream >> backgroundImage.m_visible;
0107         stream >> backgroundImage.m_status;
0108         stream >> backgroundImage.m_image;
0109         backgroundImage.generateIcon();
0110         break;
0111 
0112     case 100:
0113         stream >> backgroundImage.m_url;
0114         stream >> backgroundImage.m_location;
0115         stream >> backgroundImage.m_visible;
0116         stream >> backgroundImage.m_status;
0117         stream >> backgroundImage.m_image;
0118         stream >> backgroundImage.m_icon;
0119         break;
0120 
0121     default:
0122         throw InvalidFileVersion(QString(i18n("Background image version %1", version)));
0123         break;
0124     }
0125 
0126     return stream;
0127 }