File indexing completed on 2024-04-28 04:32:06

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 #include "PrinterConfiguration.h"
0012 
0013 #include <KLocalizedString>
0014 
0015 #include "Element.h"
0016 #include "Exceptions.h"
0017 
0018 PrinterConfiguration::PrinterConfiguration()
0019 {
0020 }
0021 
0022 PrinterConfiguration::PrinterConfiguration(const PrinterConfiguration &other)
0023 {
0024     *this = other;
0025 }
0026 
0027 PrinterConfiguration::~PrinterConfiguration()
0028 {
0029     qDeleteAll(m_pages);
0030 }
0031 
0032 bool PrinterConfiguration::isEmpty() const
0033 {
0034     foreach (Page *page, m_pages) {
0035         if (!page->elements().isEmpty()) {
0036             return false;
0037         }
0038     }
0039 
0040     return true;
0041 }
0042 
0043 void PrinterConfiguration::addPage(Page *page)
0044 {
0045     m_pages.append(page);
0046     updatePageNumbers();
0047 }
0048 
0049 void PrinterConfiguration::insertPage(int position, Page *page)
0050 {
0051     m_pages.insert(position, page);
0052     updatePageNumbers();
0053 }
0054 
0055 void PrinterConfiguration::removePage(Page *page)
0056 {
0057     m_pages.takeAt(m_pages.indexOf(page));
0058     updatePageNumbers();
0059 }
0060 
0061 QList<Page *> PrinterConfiguration::pages() const
0062 {
0063     return m_pages;
0064 }
0065 
0066 PrinterConfiguration &PrinterConfiguration::operator=(const PrinterConfiguration &other)
0067 {
0068     if (this != &other) {
0069         qDeleteAll(m_pages);
0070         m_pages.clear();
0071 
0072         QListIterator<Page *> pageIterator(other.m_pages);
0073 
0074         while (pageIterator.hasNext()) {
0075             m_pages.append(new Page(*pageIterator.next()));
0076         }
0077     }
0078 
0079     return *this;
0080 }
0081 
0082 QDataStream &operator<<(QDataStream &stream, const PrinterConfiguration &printerConfiguration)
0083 {
0084     stream << qint32(printerConfiguration.version);
0085 
0086     stream << printerConfiguration.m_pages.count();
0087     QListIterator<Page *> pageIterator(printerConfiguration.m_pages);
0088 
0089     while (pageIterator.hasNext()) {
0090         stream << *pageIterator.next();
0091     }
0092 
0093     if (stream.status() != QDataStream::Ok) {
0094         throw FailedWriteFile(stream.status());
0095     }
0096 
0097     return stream;
0098 }
0099 
0100 QDataStream &operator>>(QDataStream &stream, PrinterConfiguration &printerConfiguration)
0101 {
0102     qint32 version;
0103     qint32 pages;
0104     Page *page;
0105 
0106     stream >> version;
0107 
0108     switch (version) {
0109     case 100:
0110         stream >> pages;
0111 
0112         while (pages--) {
0113             page = new Page;
0114             stream >> *page;
0115             printerConfiguration.addPage(page);
0116         }
0117 
0118         break;
0119 
0120     default:
0121         throw InvalidFileVersion(QString(i18n("Printer configuration %1", version)));
0122         break;
0123     }
0124 
0125     if (stream.status() != QDataStream::Ok) {
0126         throw FailedReadFile(stream.status());
0127     }
0128 
0129     return stream;
0130 }
0131 
0132 void PrinterConfiguration::updatePageNumbers()
0133 {
0134     int p = 1;
0135     QListIterator<Page *> pageIterator(m_pages);
0136 
0137     while (pageIterator.hasNext()) {
0138         pageIterator.next()->setPageNumber(p++);
0139     }
0140 }