src/addObjectDialog.cpp

changeset 455
c5d14d112034
parent 421
7d26db0be944
child 460
b230ae09c8e5
equal deleted inserted replaced
454:d6b4ed3bf169 455:c5d14d112034
1 /* 1 /*
2 * LDForge: LDraw parts authoring CAD 2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 Santeri Piippo 3 * Copyright (C) 2013 Santeri Piippo
4 * 4 *
5 * This program is free software: you can redistribute it and/or modify 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 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 7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version. 8 * (at your option) any later version.
9 * 9 *
10 * This program is distributed in the hope that it will be useful, 10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details. 13 * GNU General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU General Public License 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/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */ 17 */
18 18
19 #include <qgridlayout.h> 19 #include <QGridLayout>
20 #include <qcheckbox.h> 20 #include <QCheckBox>
21 #include <qdialogbuttonbox.h> 21 #include <QDialogButtonBox>
22 #include <qspinbox.h> 22 #include <QSpinBox>
23 #include <qlabel.h> 23 #include <QLabel>
24 #include <qlistwidget.h> 24 #include <QListWidget>
25 #include <qtreewidget.h> 25 #include <QTreeWidget>
26 #include <qlineedit.h> 26 #include <QLineEdit>
27 #include <qpushbutton.h> 27 #include <QPushButton>
28 #include "gui.h" 28 #include "gui.h"
29 #include "addObjectDialog.h" 29 #include "addObjectDialog.h"
30 #include "file.h" 30 #include "file.h"
31 #include "colors.h" 31 #include "colors.h"
32 #include "colorSelectDialog.h" 32 #include "colorSelectDialog.h"
33 #include "history.h" 33 #include "history.h"
34 #include "widgets.h" 34 #include "widgets.h"
35 #include "misc.h" 35 #include "misc.h"
36 #include "primitives.h" 36 #include "primitives.h"
37 37 #include "build/moc_addObjectDialog.cpp"
38
39 // =============================================================================
40 // -----------------------------------------------------------------------------
38 class SubfileListItem : public QTreeWidgetItem { 41 class SubfileListItem : public QTreeWidgetItem {
39 PROPERTY (Primitive*, primInfo, setPrimInfo) 42 PROPERTY (Primitive*, primInfo, setPrimInfo)
40 43
41 public: 44 public:
42 SubfileListItem (QTreeWidgetItem* parent, Primitive* info) : 45 SubfileListItem (QTreeWidgetItem* parent, Primitive* info) :
44 SubfileListItem (QTreeWidget* parent, Primitive* info) : 47 SubfileListItem (QTreeWidget* parent, Primitive* info) :
45 QTreeWidgetItem (parent), m_primInfo (info) {} 48 QTreeWidgetItem (parent), m_primInfo (info) {}
46 }; 49 };
47 50
48 // ============================================================================= 51 // =============================================================================
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 52 // -----------------------------------------------------------------------------
50 // =============================================================================
51 AddObjectDialog::AddObjectDialog (const LDObject::Type type, LDObject* obj, QWidget* parent) : QDialog (parent) { 53 AddObjectDialog::AddObjectDialog (const LDObject::Type type, LDObject* obj, QWidget* parent) : QDialog (parent) {
52 setlocale (LC_ALL, "C"); 54 setlocale (LC_ALL, "C");
53 55
54 short coordCount = 0; 56 short coordCount = 0;
55 str typeName = LDObject::typeName (type); 57 str typeName = LDObject::typeName (type);
94 if (obj) 96 if (obj)
95 rb_bfcType->setValue ((int) static_cast<LDBFCObject*> (obj)->type); 97 rb_bfcType->setValue ((int) static_cast<LDBFCObject*> (obj)->type);
96 break; 98 break;
97 99
98 case LDObject::Subfile: 100 case LDObject::Subfile:
99 { 101 coordCount = 3;
100 coordCount = 3; 102
101 103 // If the primitive lister is busy writing data, we have to wait
102 // If the primitive lister is busy writing data, we have to wait 104 // for that to happen first. This should be quite considerably rare.
103 // for that to happen first. This should be quite considerably rare. 105 while (primitiveLoaderBusy())
104 while (primitiveLoaderBusy()) 106 ;
105 ; 107
106 108 tw_subfileList = new QTreeWidget();
107 tw_subfileList = new QTreeWidget(); 109 tw_subfileList->setHeaderLabel (tr ("Primitives"));
108 tw_subfileList->setHeaderLabel (tr ("Primitives")); 110
109 111 for (PrimitiveCategory& cat : g_PrimitiveCategories) {
110 for (PrimitiveCategory& cat : g_PrimitiveCategories) { 112 SubfileListItem* parentItem = new SubfileListItem (tw_subfileList, null);
111 SubfileListItem* parentItem = new SubfileListItem (tw_subfileList, null); 113 parentItem->setText (0, cat.name());
112 parentItem->setText (0, cat.name()); 114 QList<QTreeWidgetItem*> subfileItems;
113 QList<QTreeWidgetItem*> subfileItems; 115
116 for (Primitive& prim : cat.prims) {
117 SubfileListItem* item = new SubfileListItem (parentItem, &prim);
118 item->setText (0, fmt ("%1 - %2", prim.name, prim.title));
119 subfileItems << item;
114 120
115 for (Primitive& prim : cat.prims) { 121 // If this primitive is the one the current object points to,
116 SubfileListItem* item = new SubfileListItem (parentItem, &prim); 122 // select it by default
117 item->setText (0, fmt ("%1 - %2", prim.name, prim.title)); 123 if (obj && static_cast<LDSubfileObject*> (obj)->fileInfo()->name() == prim.name)
118 subfileItems << item; 124 tw_subfileList->setCurrentItem (item);
119
120 // If this primitive is the one the current object points to,
121 // select it by default
122 if (obj && static_cast<LDSubfileObject*> (obj)->fileInfo()->name() == prim.name)
123 tw_subfileList->setCurrentItem (item);
124 }
125
126 tw_subfileList->addTopLevelItem (parentItem);
127 } 125 }
128 126
129 connect (tw_subfileList, SIGNAL (itemSelectionChanged()), this, SLOT (slot_subfileTypeChanged())); 127 tw_subfileList->addTopLevelItem (parentItem);
130 lb_subfileName = new QLabel ("File:"); 128 }
131 le_subfileName = new QLineEdit; 129
132 le_subfileName->setFocus(); 130 connect (tw_subfileList, SIGNAL (itemSelectionChanged()), this, SLOT (slot_subfileTypeChanged()));
133 131 lb_subfileName = new QLabel ("File:");
134 if (obj) { 132 le_subfileName = new QLineEdit;
135 LDSubfileObject* ref = static_cast<LDSubfileObject*> (obj); 133 le_subfileName->setFocus();
136 le_subfileName->setText (ref->fileInfo()->name()); 134
137 } 135 if (obj) {
138 break; 136 LDSubfileObject* ref = static_cast<LDSubfileObject*> (obj);
139 } 137 le_subfileName->setText (ref->fileInfo()->name());
138 }
139 break;
140 140
141 default: 141 default:
142 critical (fmt ("Unhandled LDObject type %1 (%2) in AddObjectDialog", (int) type, typeName)); 142 critical (fmt ("Unhandled LDObject type %1 (%2) in AddObjectDialog", (int) type, typeName));
143 return; 143 return;
144 } 144 }
244 setWindowIcon (icon); 244 setWindowIcon (icon);
245 delete defaults; 245 delete defaults;
246 } 246 }
247 247
248 // ============================================================================= 248 // =============================================================================
249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 249 // -----------------------------------------------------------------------------
250 // =============================================================================
251 void AddObjectDialog::setButtonBackground (QPushButton* button, short colnum) { 250 void AddObjectDialog::setButtonBackground (QPushButton* button, short colnum) {
252 LDColor* col = getColor (colnum); 251 LDColor* col = getColor (colnum);
253 252
254 button->setIcon (getIcon ("palette")); 253 button->setIcon (getIcon ("palette"));
255 button->setAutoFillBackground (true); 254 button->setAutoFillBackground (true);
257 if (col) 256 if (col)
258 button->setStyleSheet (fmt ("background-color: %1", col->hexcode)); 257 button->setStyleSheet (fmt ("background-color: %1", col->hexcode));
259 } 258 }
260 259
261 // ============================================================================= 260 // =============================================================================
262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 261 // -----------------------------------------------------------------------------
263 // =============================================================================
264 str AddObjectDialog::currentSubfileName() { 262 str AddObjectDialog::currentSubfileName() {
265 SubfileListItem* item = static_cast<SubfileListItem*> (tw_subfileList->currentItem()); 263 SubfileListItem* item = static_cast<SubfileListItem*> (tw_subfileList->currentItem());
266 264
267 if (item->primInfo() == null) 265 if (item->primInfo() == null)
268 return ""; // selected a heading 266 return ""; // selected a heading
269 267
270 return item->primInfo()->name; 268 return item->primInfo()->name;
271 } 269 }
272 270
273 // ============================================================================= 271 // =============================================================================
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 272 // -----------------------------------------------------------------------------
275 // =============================================================================
276 void AddObjectDialog::slot_colorButtonClicked() { 273 void AddObjectDialog::slot_colorButtonClicked() {
277 ColorSelector::getColor (colnum, colnum, this); 274 ColorSelector::getColor (colnum, colnum, this);
278 setButtonBackground (pb_color, colnum); 275 setButtonBackground (pb_color, colnum);
279 } 276 }
280 277
281 // ============================================================================= 278 // =============================================================================
282 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 279 // -----------------------------------------------------------------------------
283 // =============================================================================
284 void AddObjectDialog::slot_subfileTypeChanged() { 280 void AddObjectDialog::slot_subfileTypeChanged() {
285 str name = currentSubfileName(); 281 str name = currentSubfileName();
286 282
287 if (name.length() > 0) 283 if (name.length() > 0)
288 le_subfileName->setText (name); 284 le_subfileName->setText (name);
289 } 285 }
290 286
291 // ============================================================================= 287 // =============================================================================
292 template<class T> T* initObj (LDObject*& obj) { 288 // -----------------------------------------------------------------------------
289 template<class T> static T* initObj (LDObject*& obj) {
293 if (obj == null) 290 if (obj == null)
294 obj = new T; 291 obj = new T;
295 292
296 return static_cast<T*> (obj); 293 return static_cast<T*> (obj);
297 } 294 }
298 295
299 // ============================================================================= 296 // =============================================================================
300 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 297 // -----------------------------------------------------------------------------
301 // =============================================================================
302 void AddObjectDialog::staticDialog (const LDObject::Type type, LDObject* obj) { 298 void AddObjectDialog::staticDialog (const LDObject::Type type, LDObject* obj) {
303 setlocale (LC_ALL, "C"); 299 setlocale (LC_ALL, "C");
304 300
305 // FIXME: Redirect to Edit Raw 301 // FIXME: Redirect to Edit Raw
306 if (obj && obj->getType() == LDObject::Error) 302 if (obj && obj->getType() == LDObject::Error)
407 LDFile::current()->insertObj (idx, obj); 403 LDFile::current()->insertObj (idx, obj);
408 } 404 }
409 405
410 g_win->fullRefresh(); 406 g_win->fullRefresh();
411 } 407 }
412
413 #include "build/moc_addObjectDialog.cpp"

mercurial