Thu, 29 Mar 2018 10:42:45 +0300
refactor
1302 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
1326 | 3 | * Copyright (C) 2013 - 2018 Teemu Piippo |
1302 | 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 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
1291 | 19 | #include "headeredit.h" |
20 | #include "ui_headeredit.h" | |
21 | #include "../lddocument.h" | |
22 | #include "../headerhistorymodel.h" | |
23 | ||
24 | static const QStringList categories { | |
25 | "", | |
26 | "Animal", "Antenna", "Arch", "Arm", "Bar", "Baseplate", "Belville", "Boat", "Bracket", | |
27 | "Brick", "Canvas", "Car", "Clikits", "Cockpit", "Cone", "Constraction", | |
28 | "Constraction Accessory", "Container", "Conveyor", "Crane", "Cylinder", "Dish", "Door", | |
29 | "Electric", "Exhaust", "Fence", "Figure", "Figure Accessory", "Flag", "Forklift", "Freestyle", | |
30 | "Garage", "Glass", "Grab", "Hinge", "Homemaker", "Hose", "Ladder", "Lever", "Magnet", "Minifig", | |
31 | "Minifig Accessory", "Minifig Footwear", "Minifig Headwear", "Minifig Hipwear", | |
32 | "Minifig Neckwear", "Monorail", "Panel", "Plane", "Plant", "Plate", "Platform", "Propellor", | |
33 | "Rack", "Roadsign", "Rock", "Scala", "Screw", "Sheet", "Slope", "Sphere", "Staircase", | |
34 | "Sticker", "Support", "Tail", "Tap", "Technic", "Tile", "Tipper", "Tractor", "Trailer", | |
35 | "Train", "Turntable", "Tyre", "Vehicle", "Wedge", "Wheel", "Winch", "Window", "Windscreen", | |
36 | "Wing", "Znap", | |
37 | }; | |
38 | ||
39 | HeaderEdit::HeaderEdit(QWidget* parent) : | |
40 | QWidget {parent}, | |
41 | ui {*new Ui_HeaderEdit}, | |
42 | headerHistoryModel {new HeaderHistoryModel {nullptr, this}} | |
43 | { | |
44 | ui.setupUi(this); | |
45 | ||
46 | this->ui.category->addItems(::categories); | |
47 | this->ui.category->setItemText(0, "(unspecified)"); | |
48 | this->ui.history->setModel(this->headerHistoryModel); | |
49 | ||
50 | connect( | |
51 | ui.description, | |
52 | &QLineEdit::textChanged, | |
53 | [&](const QString& text) | |
54 | { | |
55 | if (this->hasValidHeader()) | |
56 | { | |
57 | this->m_header->description = text; | |
58 | emit descriptionChanged(text); | |
59 | } | |
60 | } | |
61 | ); | |
62 | connect( | |
63 | ui.author, | |
64 | &QLineEdit::textChanged, | |
65 | [&](const QString& text) | |
66 | { | |
67 | if (this->hasValidHeader()) | |
68 | this->m_header->author = text; | |
69 | } | |
70 | ); | |
71 | connect( | |
72 | ui.winding, | |
73 | qOverload<int>(&QComboBox::currentIndexChanged), | |
74 | [&](int index) | |
75 | { | |
76 | if (this->hasValidHeader()) | |
1306
be85306198a2
red/green view rework complete
Teemu Piippo <teemu@hecknology.net>
parents:
1302
diff
changeset
|
77 | this->m_model->setWinding(static_cast<Winding>(index)); |
1291 | 78 | } |
79 | ); | |
80 | connect( | |
81 | ui.license, | |
82 | qOverload<int>(&QComboBox::currentIndexChanged), | |
83 | [&](int index) | |
84 | { | |
85 | if (this->m_header) | |
86 | this->m_header->license = static_cast<decltype(LDHeader::license)>(index); | |
87 | } | |
88 | ); | |
89 | connect( | |
90 | ui.category, | |
91 | qOverload<int>(&QComboBox::currentIndexChanged), | |
92 | [&](int index) | |
93 | { | |
94 | if (this->hasValidHeader()) | |
95 | this->m_header->category = ::categories.value(index); | |
96 | } | |
97 | ); | |
98 | connect( | |
99 | ui.alias, | |
100 | &QCheckBox::stateChanged, | |
101 | [&](int state) | |
102 | { | |
103 | if (this->hasValidHeader()) | |
104 | assignFlag<LDHeader::Alias>(this->m_header->qualfiers, state == Qt::Checked); | |
105 | } | |
106 | ); | |
107 | connect( | |
108 | ui.physicalColor, | |
109 | &QCheckBox::stateChanged, | |
110 | [&](int state) | |
111 | { | |
112 | if (this->hasValidHeader()) | |
113 | { | |
114 | assignFlag<LDHeader::Physical_Color>( | |
115 | this->m_header->qualfiers, | |
116 | state == Qt::Checked | |
117 | ); | |
118 | } | |
119 | } | |
120 | ); | |
121 | connect( | |
122 | ui.flexibleSection, | |
123 | &QCheckBox::stateChanged, | |
124 | [&](int state) | |
125 | { | |
126 | if (this->hasValidHeader()) | |
127 | { | |
128 | assignFlag<LDHeader::Flexible_Section>( | |
129 | this->m_header->qualfiers, | |
130 | state == Qt::Checked | |
131 | ); | |
132 | } | |
133 | } | |
134 | ); | |
1292
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
135 | connect( |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
136 | ui.historyNew, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
137 | &QPushButton::clicked, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
138 | [&]() |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
139 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
140 | if (this->hasValidHeader()) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
141 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
142 | const QModelIndex index = this->ui.history->selectionModel()->currentIndex(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
143 | int row; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
144 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
145 | if (index.isValid()) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
146 | row = index.row() + 1; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
147 | else |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
148 | row = this->headerHistoryModel->rowCount(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
149 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
150 | this->headerHistoryModel->insertRows(row, 1, {}); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
151 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
152 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
153 | ); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
154 | connect( |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
155 | ui.historyDelete, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
156 | &QPushButton::clicked, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
157 | [&]() |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
158 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
159 | const QModelIndex index = this->ui.history->selectionModel()->currentIndex(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
160 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
161 | if (this->hasValidHeader() and index.isValid()) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
162 | this->headerHistoryModel->removeRows(index.row(), 1, {}); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
163 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
164 | ); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
165 | connect(ui.historyMoveUp, &QPushButton::clicked, [&](){ this->moveRows(-1); }); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
166 | connect(ui.historyMoveDown, &QPushButton::clicked, [&](){ this->moveRows(+2); }); |
1291 | 167 | this->setEnabled(this->hasValidHeader()); |
168 | } | |
169 | ||
1292
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
170 | void HeaderEdit::moveRows(int direction) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
171 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
172 | if (this->hasValidHeader()) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
173 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
174 | const QModelIndex index = this->ui.history->selectionModel()->currentIndex(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
175 | this->headerHistoryModel->moveRows({}, index.row(), 1, {}, index.row() + direction); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
176 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
177 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
178 | |
1291 | 179 | HeaderEdit::~HeaderEdit() |
180 | { | |
181 | delete this->headerHistoryModel; | |
182 | delete &this->ui; | |
183 | } | |
184 | ||
1306
be85306198a2
red/green view rework complete
Teemu Piippo <teemu@hecknology.net>
parents:
1302
diff
changeset
|
185 | void HeaderEdit::setDocument(LDDocument* document) |
1291 | 186 | { |
1306
be85306198a2
red/green view rework complete
Teemu Piippo <teemu@hecknology.net>
parents:
1302
diff
changeset
|
187 | LDHeader* header = &document->header; |
be85306198a2
red/green view rework complete
Teemu Piippo <teemu@hecknology.net>
parents:
1302
diff
changeset
|
188 | this->m_model = document; |
1291 | 189 | this->m_header = header; |
1340
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
190 | if (document->header.type != LDHeader::NoHeader) |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
191 | { |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
192 | this->ui.description->setText(header->description); |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
193 | this->ui.author->setText(header->author); |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
194 | this->ui.category->setCurrentIndex(::categories.indexOf(header->category)); |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
195 | this->ui.license->setCurrentIndex(static_cast<int>(header->license)); |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
196 | this->ui.alias->setChecked(header->qualfiers & LDHeader::Alias); |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
197 | this->ui.physicalColor->setChecked(header->qualfiers & LDHeader::Physical_Color); |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
198 | this->ui.flexibleSection->setChecked(header->qualfiers & LDHeader::Flexible_Section); |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
199 | this->ui.cmdline->setText(header->cmdline); |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
200 | this->ui.winding->setCurrentIndex(document->winding()); |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
201 | this->ui.keywords->document()->setPlainText(header->keywords); |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
202 | this->ui.help->document()->setPlainText(header->help); |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
203 | this->ui.type->setCurrentIndex(static_cast<int>(document->header.type) - 1); |
ea1b3ea9a3ca
more work on 8-primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
204 | } |
1291 | 205 | this->headerHistoryModel->setHeader(header); |
206 | this->setEnabled(this->hasValidHeader()); | |
207 | } | |
208 | ||
209 | bool HeaderEdit::hasValidHeader() const | |
210 | { | |
211 | return this->m_header != nullptr and this->m_header->type != LDHeader::NoHeader; | |
212 | } |