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

0001 /*
0002  * Copyright (C) 2003-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 "LibraryPattern.h"
0012 
0013 #include <QListWidget>
0014 
0015 #include "KeycodeLineEdit.h"
0016 #include "LibraryListWidgetItem.h"
0017 #include "Pattern.h"
0018 
0019 LibraryPattern::LibraryPattern()
0020 {
0021     m_pattern = new Pattern;
0022 }
0023 
0024 LibraryPattern::LibraryPattern(Pattern *pattern, qint32 key, Qt::KeyboardModifiers modifiers, qint16 baseline)
0025     : m_pattern(pattern)
0026     , m_key(key)
0027     , m_modifiers(modifiers)
0028     , m_baseline(baseline)
0029     , m_libraryListWidgetItem(nullptr)
0030     , m_changed(false)
0031 {
0032 }
0033 
0034 LibraryPattern::LibraryPattern(QByteArray data, qint32 key, Qt::KeyboardModifiers modifiers, qint16 baseline)
0035     : m_key(key)
0036     , m_modifiers(modifiers)
0037     , m_baseline(baseline)
0038     , m_libraryListWidgetItem(nullptr)
0039     , m_changed(false)
0040 {
0041     QDataStream stream(&data, QIODevice::ReadOnly);
0042     stream.setVersion(QDataStream::Qt_3_3);
0043     QString scheme;
0044     qint32 width;
0045     qint32 height;
0046     stream >> scheme >> width >> height;
0047     m_pattern = new Pattern;
0048     m_pattern->palette().setSchemeName(scheme);
0049     m_pattern->stitches().resize(width, height);
0050 
0051     QMap<int, QColor> colors;
0052     int colorIndex;
0053 
0054     for (int y = 0; y < height; ++y) {
0055         for (int x = 0; x < width; ++x) {
0056             QPoint cell(x, y);
0057             qint8 stitches;
0058             stream >> stitches;
0059 
0060             while (stitches--) {
0061                 qint8 type;
0062                 QColor color;
0063                 stream >> type >> color;
0064 
0065                 if ((colorIndex = colors.key(color, -1)) == -1) {
0066                     colorIndex = m_pattern->palette().add(color);
0067                     colors.insert(colorIndex, color);
0068                 }
0069 
0070                 m_pattern->stitches().addStitch(cell, static_cast<Stitch::Type>(type), colorIndex);
0071             }
0072         }
0073     }
0074 
0075     qint32 backstitches;
0076     stream >> backstitches;
0077 
0078     while (backstitches--) {
0079         QPoint start;
0080         QPoint end;
0081         QColor color;
0082         stream >> start >> end >> color;
0083 
0084         if ((colorIndex = colors.key(color, -1)) == -1) {
0085             colorIndex = m_pattern->palette().add(color);
0086             colors.insert(colorIndex, color);
0087         }
0088 
0089         m_pattern->stitches().addBackstitch(start, end, colorIndex);
0090     }
0091 
0092     qint32 knots;
0093     stream >> knots;
0094 
0095     while (knots--) {
0096         QPoint position;
0097         QColor color;
0098         stream >> position >> color;
0099 
0100         if ((colorIndex = colors.key(color, -1)) == -1) {
0101             colorIndex = m_pattern->palette().add(color);
0102             colors.insert(colorIndex, color);
0103         }
0104 
0105         m_pattern->stitches().addFrenchKnot(position, colorIndex);
0106     }
0107 }
0108 
0109 qint32 LibraryPattern::key() const
0110 {
0111     return m_key;
0112 }
0113 
0114 Qt::KeyboardModifiers LibraryPattern::modifiers() const
0115 {
0116     return m_modifiers;
0117 }
0118 
0119 qint16 LibraryPattern::baseline() const
0120 {
0121     return m_baseline;
0122 }
0123 
0124 Pattern *LibraryPattern::pattern()
0125 {
0126     return m_pattern;
0127 }
0128 
0129 LibraryListWidgetItem *LibraryPattern::libraryListWidgetItem() const
0130 {
0131     return m_libraryListWidgetItem;
0132 }
0133 
0134 bool LibraryPattern::hasChanged() const
0135 {
0136     return m_changed;
0137 }
0138 
0139 void LibraryPattern::setKeyModifiers(qint32 key, Qt::KeyboardModifiers modifiers)
0140 {
0141     m_key = key;
0142     m_modifiers = modifiers;
0143     m_libraryListWidgetItem->setText(KeycodeLineEdit::keyString(key, modifiers));
0144     m_changed = true;
0145 }
0146 
0147 void LibraryPattern::setBaseline(qint16 baseline)
0148 {
0149     m_baseline = baseline;
0150     m_changed = true;
0151 }
0152 
0153 void LibraryPattern::setLibraryListWidgetItem(LibraryListWidgetItem *libraryListWidgetItem)
0154 {
0155     m_libraryListWidgetItem = libraryListWidgetItem;
0156     libraryListWidgetItem->setText(KeycodeLineEdit::keyString(m_key, m_modifiers));
0157 }
0158 
0159 QDataStream &operator<<(QDataStream &stream, const LibraryPattern &libraryPattern)
0160 {
0161     stream << libraryPattern.version;
0162     stream << libraryPattern.m_key;
0163     stream << qint32(libraryPattern.m_modifiers);
0164     stream << libraryPattern.m_baseline;
0165     stream << *(libraryPattern.m_pattern);
0166     return stream;
0167 }
0168 
0169 QDataStream &operator>>(QDataStream &stream, LibraryPattern &libraryPattern)
0170 {
0171     qint32 version;
0172     qint32 modifiers;
0173 
0174     stream >> version;
0175 
0176     switch (version) {
0177     case 100:
0178         stream >> libraryPattern.m_key;
0179         stream >> modifiers;
0180         libraryPattern.m_modifiers = Qt::KeyboardModifiers(modifiers);
0181         stream >> libraryPattern.m_baseline;
0182         stream >> *(libraryPattern.m_pattern);
0183         libraryPattern.m_libraryListWidgetItem = nullptr;
0184         libraryPattern.m_changed = false;
0185         break;
0186 
0187     default:
0188         // not supported
0189         // throw exception
0190         break;
0191     }
0192 
0193     return stream;
0194 }