1 /*
2 * Copyright (C) 2003-2015 by Stephen Allewell
3 * steve.allewell@gmail.com
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11
12 #include "LibraryPattern.h"
13
14 #include <QListWidget>
15
16 #include "KeycodeLineEdit.h"
17 #include "LibraryListWidgetItem.h"
18 #include "Pattern.h"
19
20
LibraryPattern()21 LibraryPattern::LibraryPattern()
22 {
23 m_pattern = new Pattern;
24 }
25
26
LibraryPattern(Pattern * pattern,qint32 key,Qt::KeyboardModifiers modifiers,qint16 baseline)27 LibraryPattern::LibraryPattern(Pattern *pattern, qint32 key, Qt::KeyboardModifiers modifiers, qint16 baseline)
28 : m_pattern(pattern),
29 m_key(key),
30 m_modifiers(modifiers),
31 m_baseline(baseline),
32 m_libraryListWidgetItem(nullptr),
33 m_changed(false)
34 {
35 }
36
37
LibraryPattern(QByteArray data,qint32 key,Qt::KeyboardModifiers modifiers,qint16 baseline)38 LibraryPattern::LibraryPattern(QByteArray data, qint32 key, Qt::KeyboardModifiers modifiers, qint16 baseline)
39 : m_key(key),
40 m_modifiers(modifiers),
41 m_baseline(baseline),
42 m_libraryListWidgetItem(nullptr),
43 m_changed(false)
44 {
45 QDataStream stream(&data, QIODevice::ReadOnly);
46 stream.setVersion(QDataStream::Qt_3_3);
47 QString scheme;
48 qint32 width;
49 qint32 height;
50 stream >> scheme
51 >> width
52 >> height;
53 m_pattern = new Pattern;
54 m_pattern->palette().setSchemeName(scheme);
55 m_pattern->stitches().resize(width, height);
56
57 QMap<int, QColor> colors;
58 int colorIndex;
59
60 for (int y = 0 ; y < height ; ++y) {
61 for (int x = 0 ; x < width ; ++x) {
62 QPoint cell(x, y);
63 qint8 stitches;
64 stream >> stitches;
65
66 while (stitches--) {
67 qint8 type;
68 QColor color;
69 stream >> type >> color;
70
71 if ((colorIndex = colors.key(color, -1)) == -1) {
72 colorIndex = m_pattern->palette().add(color);
73 colors.insert(colorIndex, color);
74 }
75
76 m_pattern->stitches().addStitch(cell, static_cast<Stitch::Type>(type), colorIndex);
77 }
78 }
79 }
80
81 qint32 backstitches;
82 stream >> backstitches;
83
84 while (backstitches--) {
85 QPoint start;
86 QPoint end;
87 QColor color;
88 stream >> start >> end >> color;
89
90 if ((colorIndex = colors.key(color, -1)) == -1) {
91 colorIndex = m_pattern->palette().add(color);
92 colors.insert(colorIndex, color);
93 }
94
95 m_pattern->stitches().addBackstitch(start, end, colorIndex);
96 }
97
98 qint32 knots;
99 stream >> knots;
100
101 while (knots--) {
102 QPoint position;
103 QColor color;
104 stream >> position >> color;
105
106 if ((colorIndex = colors.key(color, -1)) == -1) {
107 colorIndex = m_pattern->palette().add(color);
108 colors.insert(colorIndex, color);
109 }
110
111 m_pattern->stitches().addFrenchKnot(position, colorIndex);
112 }
113 }
114
115
key() const116 qint32 LibraryPattern::key() const
117 {
118 return m_key;
119 }
120
121
modifiers() const122 Qt::KeyboardModifiers LibraryPattern::modifiers() const
123 {
124 return m_modifiers;
125 }
126
127
baseline() const128 qint16 LibraryPattern::baseline() const
129 {
130 return m_baseline;
131 }
132
133
pattern()134 Pattern *LibraryPattern::pattern()
135 {
136 return m_pattern;
137 }
138
139
libraryListWidgetItem() const140 LibraryListWidgetItem *LibraryPattern::libraryListWidgetItem() const
141 {
142 return m_libraryListWidgetItem;
143 }
144
145
hasChanged() const146 bool LibraryPattern::hasChanged() const
147 {
148 return m_changed;
149 }
150
151
setKeyModifiers(qint32 key,Qt::KeyboardModifiers modifiers)152 void LibraryPattern::setKeyModifiers(qint32 key, Qt::KeyboardModifiers modifiers)
153 {
154 m_key = key;
155 m_modifiers = modifiers;
156 m_libraryListWidgetItem->setText(KeycodeLineEdit::keyString(key, modifiers));
157 m_changed = true;
158 }
159
160
setBaseline(qint16 baseline)161 void LibraryPattern::setBaseline(qint16 baseline)
162 {
163 m_baseline = baseline;
164 m_changed = true;
165 }
166
167
setLibraryListWidgetItem(LibraryListWidgetItem * libraryListWidgetItem)168 void LibraryPattern::setLibraryListWidgetItem(LibraryListWidgetItem *libraryListWidgetItem)
169 {
170 m_libraryListWidgetItem = libraryListWidgetItem;
171 libraryListWidgetItem->setText(KeycodeLineEdit::keyString(m_key, m_modifiers));
172 }
173
174
operator <<(QDataStream & stream,const LibraryPattern & libraryPattern)175 QDataStream &operator<<(QDataStream &stream, const LibraryPattern &libraryPattern)
176 {
177 stream << libraryPattern.version;
178 stream << libraryPattern.m_key;
179 stream << qint32(libraryPattern.m_modifiers);
180 stream << libraryPattern.m_baseline;
181 stream << *(libraryPattern.m_pattern);
182 return stream;
183 }
184
185
operator >>(QDataStream & stream,LibraryPattern & libraryPattern)186 QDataStream &operator>>(QDataStream &stream, LibraryPattern &libraryPattern)
187 {
188 qint32 version;
189 qint32 modifiers;
190
191 stream >> version;
192
193 switch (version) {
194 case 100:
195 stream >> libraryPattern.m_key;
196 stream >> modifiers;
197 libraryPattern.m_modifiers = Qt::KeyboardModifiers(modifiers);
198 stream >> libraryPattern.m_baseline;
199 stream >> *(libraryPattern.m_pattern);
200 libraryPattern.m_libraryListWidgetItem = nullptr;
201 libraryPattern.m_changed = false;
202 break;
203
204 default:
205 // not supported
206 // throw exception
207 break;
208
209 }
210
211 return stream;
212 }
213