File indexing completed on 2024-04-21 04:32:02

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 collection of background images to be used as overlays on
0013  * a canvas for the purpose of tracing.
0014  */
0015 
0016 // Class include
0017 #include "BackgroundImages.h"
0018 
0019 // Qt includes
0020 #include <QDataStream>
0021 
0022 // KF5 includes
0023 #include <KLocalizedString>
0024 
0025 // Application includes
0026 #include "BackgroundImage.h"
0027 #include "Exceptions.h"
0028 
0029 void BackgroundImages::clear()
0030 {
0031     m_backgroundImages.clear();
0032 }
0033 
0034 QListIterator<QSharedPointer<BackgroundImage>> BackgroundImages::backgroundImages()
0035 {
0036     return QListIterator<QSharedPointer<BackgroundImage>>(m_backgroundImages);
0037 }
0038 
0039 void BackgroundImages::addBackgroundImage(QSharedPointer<BackgroundImage> backgroundImage)
0040 {
0041     m_backgroundImages.append(backgroundImage);
0042 }
0043 
0044 bool BackgroundImages::removeBackgroundImage(QSharedPointer<BackgroundImage> backgroundImage)
0045 {
0046     return m_backgroundImages.removeOne(backgroundImage);
0047 }
0048 
0049 QRect BackgroundImages::fitBackgroundImage(QSharedPointer<BackgroundImage> backgroundImage, const QRect &rectangle)
0050 {
0051     QRect oldRectangle = backgroundImage->location();
0052     backgroundImage->setLocation(rectangle);
0053 
0054     return oldRectangle;
0055 }
0056 
0057 bool BackgroundImages::showBackgroundImage(QSharedPointer<BackgroundImage> backgroundImage, bool show)
0058 {
0059     bool shown = backgroundImage->isVisible();
0060     backgroundImage->setVisible(show);
0061 
0062     return shown;
0063 }
0064 
0065 QDataStream &operator<<(QDataStream &stream, const BackgroundImages &backgroundImages)
0066 {
0067     stream << qint32(backgroundImages.version);
0068     stream << qint32(backgroundImages.m_backgroundImages.count());
0069 
0070     for (auto backgroundImage : backgroundImages.m_backgroundImages) {
0071         stream << *backgroundImage;
0072     }
0073 
0074     if (stream.status() != QDataStream::Ok) {
0075         throw FailedWriteFile(stream.status());
0076     }
0077 
0078     return stream;
0079 }
0080 
0081 QDataStream &operator>>(QDataStream &stream, BackgroundImages &backgroundImages)
0082 {
0083     qint32 version;
0084     qint32 backgroundImageCount;
0085 
0086     stream >> version;
0087 
0088     switch (version) {
0089     case 100:
0090         stream >> backgroundImageCount;
0091 
0092         while (backgroundImageCount--) {
0093             QSharedPointer<BackgroundImage> backgroundImage(new BackgroundImage);
0094             stream >> *backgroundImage;
0095             backgroundImages.m_backgroundImages.append(backgroundImage);
0096         }
0097 
0098         break;
0099 
0100     default:
0101         throw InvalidFileVersion(QString(i18n("Background images version %1", version)));
0102         break;
0103     }
0104 
0105     if (stream.status() != QDataStream::Ok) {
0106         throw FailedReadFile(stream.status());
0107     }
0108 
0109     return stream;
0110 }