Sat, 17 Mar 2018 11:35:07 +0200
removed the old Add History Line tool
1291 | 1 | #include "headeredit.h" |
2 | #include "ui_headeredit.h" | |
3 | #include "../lddocument.h" | |
4 | #include "../headerhistorymodel.h" | |
5 | ||
6 | static const QStringList categories { | |
7 | "", | |
8 | "Animal", "Antenna", "Arch", "Arm", "Bar", "Baseplate", "Belville", "Boat", "Bracket", | |
9 | "Brick", "Canvas", "Car", "Clikits", "Cockpit", "Cone", "Constraction", | |
10 | "Constraction Accessory", "Container", "Conveyor", "Crane", "Cylinder", "Dish", "Door", | |
11 | "Electric", "Exhaust", "Fence", "Figure", "Figure Accessory", "Flag", "Forklift", "Freestyle", | |
12 | "Garage", "Glass", "Grab", "Hinge", "Homemaker", "Hose", "Ladder", "Lever", "Magnet", "Minifig", | |
13 | "Minifig Accessory", "Minifig Footwear", "Minifig Headwear", "Minifig Hipwear", | |
14 | "Minifig Neckwear", "Monorail", "Panel", "Plane", "Plant", "Plate", "Platform", "Propellor", | |
15 | "Rack", "Roadsign", "Rock", "Scala", "Screw", "Sheet", "Slope", "Sphere", "Staircase", | |
16 | "Sticker", "Support", "Tail", "Tap", "Technic", "Tile", "Tipper", "Tractor", "Trailer", | |
17 | "Train", "Turntable", "Tyre", "Vehicle", "Wedge", "Wheel", "Winch", "Window", "Windscreen", | |
18 | "Wing", "Znap", | |
19 | }; | |
20 | ||
21 | HeaderEdit::HeaderEdit(QWidget* parent) : | |
22 | QWidget {parent}, | |
23 | ui {*new Ui_HeaderEdit}, | |
24 | headerHistoryModel {new HeaderHistoryModel {nullptr, this}} | |
25 | { | |
26 | ui.setupUi(this); | |
27 | ||
28 | this->ui.category->addItems(::categories); | |
29 | this->ui.category->setItemText(0, "(unspecified)"); | |
30 | this->ui.history->setModel(this->headerHistoryModel); | |
31 | ||
32 | connect( | |
33 | ui.description, | |
34 | &QLineEdit::textChanged, | |
35 | [&](const QString& text) | |
36 | { | |
37 | if (this->hasValidHeader()) | |
38 | { | |
39 | this->m_header->description = text; | |
40 | emit descriptionChanged(text); | |
41 | } | |
42 | } | |
43 | ); | |
44 | connect( | |
45 | ui.author, | |
46 | &QLineEdit::textChanged, | |
47 | [&](const QString& text) | |
48 | { | |
49 | if (this->hasValidHeader()) | |
50 | this->m_header->author = text; | |
51 | } | |
52 | ); | |
53 | connect( | |
54 | ui.winding, | |
55 | qOverload<int>(&QComboBox::currentIndexChanged), | |
56 | [&](int index) | |
57 | { | |
58 | if (this->hasValidHeader()) | |
59 | { | |
60 | this->m_header->winding = static_cast<Winding>(index); | |
61 | emit windingChanged(this->m_header->winding); | |
62 | } | |
63 | } | |
64 | ); | |
65 | connect( | |
66 | ui.license, | |
67 | qOverload<int>(&QComboBox::currentIndexChanged), | |
68 | [&](int index) | |
69 | { | |
70 | if (this->m_header) | |
71 | this->m_header->license = static_cast<decltype(LDHeader::license)>(index); | |
72 | } | |
73 | ); | |
74 | connect( | |
75 | ui.category, | |
76 | qOverload<int>(&QComboBox::currentIndexChanged), | |
77 | [&](int index) | |
78 | { | |
79 | if (this->hasValidHeader()) | |
80 | this->m_header->category = ::categories.value(index); | |
81 | } | |
82 | ); | |
83 | connect( | |
84 | ui.alias, | |
85 | &QCheckBox::stateChanged, | |
86 | [&](int state) | |
87 | { | |
88 | if (this->hasValidHeader()) | |
89 | assignFlag<LDHeader::Alias>(this->m_header->qualfiers, state == Qt::Checked); | |
90 | } | |
91 | ); | |
92 | connect( | |
93 | ui.physicalColor, | |
94 | &QCheckBox::stateChanged, | |
95 | [&](int state) | |
96 | { | |
97 | if (this->hasValidHeader()) | |
98 | { | |
99 | assignFlag<LDHeader::Physical_Color>( | |
100 | this->m_header->qualfiers, | |
101 | state == Qt::Checked | |
102 | ); | |
103 | } | |
104 | } | |
105 | ); | |
106 | connect( | |
107 | ui.flexibleSection, | |
108 | &QCheckBox::stateChanged, | |
109 | [&](int state) | |
110 | { | |
111 | if (this->hasValidHeader()) | |
112 | { | |
113 | assignFlag<LDHeader::Flexible_Section>( | |
114 | this->m_header->qualfiers, | |
115 | state == Qt::Checked | |
116 | ); | |
117 | } | |
118 | } | |
119 | ); | |
1292
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
120 | connect( |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
121 | ui.historyNew, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
122 | &QPushButton::clicked, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
123 | [&]() |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
124 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
125 | if (this->hasValidHeader()) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
126 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
127 | const QModelIndex index = this->ui.history->selectionModel()->currentIndex(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
128 | int row; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
129 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
130 | if (index.isValid()) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
131 | row = index.row() + 1; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
132 | else |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
133 | row = this->headerHistoryModel->rowCount(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
134 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
135 | this->headerHistoryModel->insertRows(row, 1, {}); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
136 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
137 | } |
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 | connect( |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
140 | ui.historyDelete, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
141 | &QPushButton::clicked, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
142 | [&]() |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
143 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
144 | const QModelIndex index = this->ui.history->selectionModel()->currentIndex(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
145 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
146 | if (this->hasValidHeader() and index.isValid()) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
147 | this->headerHistoryModel->removeRows(index.row(), 1, {}); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
148 | } |
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 | connect(ui.historyMoveUp, &QPushButton::clicked, [&](){ this->moveRows(-1); }); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
151 | connect(ui.historyMoveDown, &QPushButton::clicked, [&](){ this->moveRows(+2); }); |
1291 | 152 | this->setEnabled(this->hasValidHeader()); |
153 | } | |
154 | ||
1292
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
155 | void HeaderEdit::moveRows(int direction) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
156 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
157 | if (this->hasValidHeader()) |
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 | 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
|
161 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
162 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
163 | |
1291 | 164 | HeaderEdit::~HeaderEdit() |
165 | { | |
166 | delete this->headerHistoryModel; | |
167 | delete &this->ui; | |
168 | } | |
169 | ||
170 | void HeaderEdit::setHeader(LDHeader* header) | |
171 | { | |
172 | this->m_header = header; | |
173 | this->ui.description->setText(header->description); | |
174 | this->ui.author->setText(header->author); | |
175 | this->ui.category->setCurrentIndex(::categories.indexOf(header->category)); | |
176 | this->ui.license->setCurrentIndex(static_cast<int>(header->license)); | |
177 | this->ui.alias->setChecked(header->qualfiers & LDHeader::Alias); | |
178 | this->ui.physicalColor->setChecked(header->qualfiers & LDHeader::Physical_Color); | |
179 | this->ui.flexibleSection->setChecked(header->qualfiers & LDHeader::Flexible_Section); | |
180 | this->ui.cmdline->setText(header->cmdline); | |
181 | this->ui.winding->setCurrentIndex(header->winding); | |
182 | this->ui.keywords->document()->setPlainText(header->keywords); | |
183 | this->ui.help->document()->setPlainText(header->help); | |
184 | this->headerHistoryModel->setHeader(header); | |
185 | this->setEnabled(this->hasValidHeader()); | |
186 | } | |
187 | ||
188 | LDHeader* HeaderEdit::header() const | |
189 | { | |
190 | return this->m_header; | |
191 | } | |
192 | ||
193 | bool HeaderEdit::hasValidHeader() const | |
194 | { | |
195 | return this->m_header != nullptr and this->m_header->type != LDHeader::NoHeader; | |
196 | } |