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

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 "Layers.h"
0012 
0013 Layers::Layers()
0014     : m_currentLayer(-1)
0015 {
0016 }
0017 
0018 void Layers::clear()
0019 {
0020     m_layers.clear();
0021     m_currentLayer = -1;
0022 }
0023 
0024 const QList<Layer> &Layers::layers() const
0025 {
0026     return m_layers;
0027 }
0028 
0029 QStringList Layers::layerNames() const
0030 {
0031     QStringList namesList;
0032 
0033     QListIterator<Layer> iterator(m_layers);
0034 
0035     while (iterator.hasNext()) {
0036         namesList.append(iterator.next().name());
0037     }
0038 
0039     return namesList;
0040 }
0041 
0042 QList<int> Layers::layerNumbers() const
0043 {
0044     QList<int> numberList;
0045 
0046     QListIterator<Layer> iterator(m_layers);
0047 
0048     while (iterator.hasNext()) {
0049         numberList.append(iterator.next().number());
0050     }
0051 
0052     return numberList;
0053 }
0054 
0055 int Layers::currentLayer() const
0056 {
0057     return m_currentLayer;
0058 }
0059 
0060 void Layers::addLayer(int layer, const QString &name)
0061 {
0062     m_layers.append(Layer(layer, name));
0063 
0064     if (m_currentLayer == -1) {
0065         m_currentLayer = 0;
0066     }
0067 }
0068 
0069 void Layers::removeLayer(int number)
0070 {
0071     QMutableListIterator<Layer> iterator(m_layers);
0072 
0073     while (iterator.hasNext()) {
0074         if (iterator.next().number() == number) {
0075             iterator.remove();
0076         }
0077     }
0078 
0079     if (m_layers.count() == 0) {
0080         m_currentLayer = -1;
0081     }
0082 }
0083 
0084 void Layers::removeLayer(const QString &name)
0085 {
0086     QMutableListIterator<Layer> iterator(m_layers);
0087 
0088     while (iterator.hasNext()) {
0089         if (iterator.next().name() == name) {
0090             iterator.remove();
0091         }
0092     }
0093 
0094     if (m_layers.count() == 0) {
0095         m_currentLayer = -1;
0096     }
0097 }
0098 
0099 void Layers::setCurrentLayer(int layer)
0100 {
0101     if (m_layers.count() > 0) {
0102         m_currentLayer = layer;
0103     }
0104 }
0105 
0106 QDataStream &operator<<(QDataStream &stream, const Layers &layers)
0107 {
0108     stream << qint32(layers.version);
0109     stream << qint32(layers.m_currentLayer);
0110     stream << layers.m_layers;
0111 
0112     return stream;
0113 }
0114 
0115 QDataStream &operator>>(QDataStream &stream, Layers &layers)
0116 {
0117     qint32 version;
0118     qint32 currentLayer;
0119 
0120     stream >> version;
0121 
0122     switch (version) {
0123     case 100:
0124         stream >> currentLayer;
0125         layers.m_currentLayer = currentLayer;
0126         stream >> layers.m_layers;
0127         break;
0128 
0129     default:
0130         // not supported
0131         // throw exception
0132         break;
0133     }
0134 
0135     return stream;
0136 }