]> git.sven.stormbind.net Git - sven/vym.git/blob - src/branchpropeditor.cpp
Replace Pierre as the maintainer
[sven/vym.git] / src / branchpropeditor.cpp
1 #include "branchpropeditor.h"
2
3 #include <QColorDialog>
4
5 #include "attributeitem.h"
6 #include "branchitem.h"
7 #include "frameobj.h"
8 #include "settings.h"
9 #include "vymmodel.h"
10
11 extern Settings settings;
12 extern QString vymName;
13
14 BranchPropertyEditor::BranchPropertyEditor(QWidget *parent)
15     : QDialog(parent) // FIXME-4 not updating when data is set elsewhere
16                       // (connect to dataCHanged)
17
18 {
19     ui.setupUi(this);
20
21     setWindowTitle(vymName + " - " + tr("Property Editor", "Window caption"));
22
23     branchObj = NULL;
24     branchItem = NULL;
25     model = NULL;
26
27     ui.tabWidget->setEnabled(false);
28
29     penColor = QColor(Qt::black);
30     brushColor = QColor(Qt::black);
31     QPixmap pix(16, 16);
32     pix.fill(penColor);
33     ui.framePenColorButton->setIcon(pix);
34     ui.frameBrushColorButton->setIcon(pix);
35
36     if (!settings.value("/mainwindow/showTestMenu", false).toBool())
37         ui.tabWidget->widget(3)->hide();
38
39     //Create Model and View to hold attributes
40     attributeModel = new QStandardItemModel (1, 3, this);
41     attributeModel->setHeaderData(0, 
42             Qt::Horizontal, 
43             tr("Name","Branchprop window: Attribute name")); 
44     attributeModel->setHeaderData(1, 
45             Qt::Horizontal,
46             tr("Value","Branchprop window: Attribute value"));
47     attributeModel->setHeaderData(2, 
48             Qt::Horizontal, 
49             tr("Type","Branchprop window: Attribute type")); 
50     ui.attributeTableView->setModel (attributeModel);
51
52     // Load Settings
53     resize(
54         settings
55             .value("/satellite/propertywindow/geometry/size", QSize(450, 600))
56             .toSize());
57     move(settings
58              .value("/satellite/propertywindow/geometry/pos", QPoint(250, 50))
59              .toPoint());
60
61     if (settings.value("/satellite/propertywindow/showWithMain", true).toBool())
62         show();
63     else
64         hide();
65     
66     connectSignals();
67 }
68
69 BranchPropertyEditor::~BranchPropertyEditor()
70 {
71     settings.setValue("/satellite/propertywindow/geometry/size", size());
72     settings.setValue("/satellite/propertywindow/geometry/pos", pos());
73     settings.setValue("/satellite/propertywindow/showWithMain", isVisible());
74
75     delete (attributeModel);
76 }
77
78 void BranchPropertyEditor::setItem(TreeItem *ti)
79 {
80     disconnectSignals();
81     if (!ti)
82         ui.tabWidget->setEnabled(false);
83     else if (ti->isBranchLikeType()) {
84         branchItem = (BranchItem *)ti;
85
86         branchObj = (BranchObj *)(branchItem->getLMO());
87         if (branchObj) // FIXME-4 replace by branchItem later, when Frame is
88                        // ported...
89         {
90             ui.tabWidget->setEnabled(true);
91             for (int i = 0; i < 4; ++i)
92                 ui.tabWidget->setTabEnabled(i, true);
93             ui.tabWidget->setTabEnabled(4, false);
94
95             // Frame
96             FrameObj::FrameType t = branchObj->getFrameType();
97             if (t == FrameObj::NoFrame) // FIXME-3 Check if all below depends on
98                                         // frame type???
99             {
100                 ui.frameTypeCombo->setCurrentIndex(0);
101                 penColor = Qt::white;
102                 brushColor = Qt::white;
103                 ui.colorGroupBox->setEnabled(false);
104                 ui.framePaddingSpinBox->setEnabled(false);
105                 ui.frameWidthSpinBox->setEnabled(false);
106                 ui.framePaddingLabel->setEnabled(false);
107                 ui.frameBorderLabel->setEnabled(false);
108                 ui.includeChildrenCheckBox->setEnabled(false);
109                 ui.includeChildrenCheckBox->setEnabled(false);
110             }
111             else {
112                 penColor = branchObj->getFramePenColor();
113                 brushColor = branchObj->getFrameBrushColor();
114                 QPixmap pix(16, 16);
115                 pix.fill(penColor);
116                 ui.framePenColorButton->setIcon(pix);
117                 pix.fill(brushColor);
118                 ui.frameBrushColorButton->setIcon(pix);
119                 ui.colorGroupBox->setEnabled(true);
120                 ui.framePaddingSpinBox->setEnabled(true);
121                 ui.framePaddingSpinBox->setValue(branchObj->getFramePadding());
122                 ui.frameWidthSpinBox->setEnabled(true);
123                 ui.frameWidthSpinBox->setValue(
124                     branchObj->getFrameBorderWidth());
125                 ui.framePaddingLabel->setEnabled(true);
126                 ui.frameBorderLabel->setEnabled(true);
127                 ui.includeChildrenCheckBox->setEnabled(true);
128
129                 switch (t) {
130                 case FrameObj::Rectangle:
131                     ui.frameTypeCombo->setCurrentIndex(1);
132                     break;
133                 case FrameObj::RoundedRectangle:
134                     ui.frameTypeCombo->setCurrentIndex(2);
135                     break;
136                 case FrameObj::Ellipse:
137                     ui.frameTypeCombo->setCurrentIndex(3);
138                     break;
139                 case FrameObj::Cloud:
140                     ui.frameTypeCombo->setCurrentIndex(4);
141                     break;
142                 default:
143                     break;
144                 }
145                 if (branchItem->getFrameIncludeChildren())
146                     ui.includeChildrenCheckBox->setCheckState(Qt::Checked);
147                 else
148                     ui.includeChildrenCheckBox->setCheckState(Qt::Unchecked);
149             }
150             // Link
151             if (branchItem->getHideLinkUnselected())
152                 ui.hideLinkIfUnselected->setCheckState(Qt::Checked);
153             else
154                 ui.hideLinkIfUnselected->setCheckState(Qt::Unchecked);
155
156             // Layout
157             if (branchItem->getIncludeImagesVer())
158                 ui.incImgVer->setCheckState(Qt::Checked);
159             else
160                 ui.incImgVer->setCheckState(Qt::Unchecked);
161             if (branchItem->getIncludeImagesHor())
162                 ui.incImgHor->setCheckState(Qt::Checked);
163             else
164                 ui.incImgHor->setCheckState(Qt::Unchecked);
165             if (branchItem->getChildrenLayout() == BranchItem::FreePositioning)
166                 ui.childrenFreePositioning->setCheckState(Qt::Checked);
167             else
168                 ui.childrenFreePositioning->setCheckState(Qt::Unchecked);
169
170             // Task
171             Task *task = branchItem->getTask();
172             if (task) {
173                 ui.taskPrioDelta->setEnabled(true);
174                 ui.taskPrioDelta->setValue(task->getPriorityDelta());
175                 ui.lineEditDateCreation->setText(
176                     task->getDateCreation().toString() + " - " +
177                     QString(tr("%1 days ago", "task related times"))
178                         .arg(task->getAgeCreation()));
179                 QDateTime dt = task->getDateModification();
180                 if (dt.isValid()) {
181                     ui.lineEditDateModification->setText(
182                         dt.toString() + " - " +
183                         QString(tr("%1 days ago", "task related times"))
184                             .arg(task->getAgeModification()));
185                 }
186                 else {
187                     ui.lineEditDateModification->setText("");
188                 }
189
190                 dt = task->getSleep();
191                 if (dt.isValid()) {
192                     QString s;
193                     qint64 daysSleep = task->getDaysSleep();
194                     daysSleep >= 0 ? s = QString(dt.toString() + " - " +
195                                                  tr("sleeping %1 days",
196                                                     "task related times"))
197                                              .arg(daysSleep)
198                                    : s = QString(tr("Task is awake",
199                                                     "task related times"));
200                     ui.lineEditSleep->setText(s);
201                 }
202                 else {
203                     ui.lineEditSleep->setText("");
204                 }
205             }
206             else {
207                 ui.taskPrioDelta->setEnabled(false);
208                 ui.taskPrioDelta->setValue(0);
209                 ui.lineEditDateCreation->setText("");
210                 ui.lineEditDateModification->setText("");
211                 ui.lineEditSleep->setText("");
212             }
213
214         // Attributes
215         attributeModel->removeRows(0, attributeModel->rowCount(), QModelIndex());
216
217         for (int i = 0; i < branchItem->attributeCount(); i++)
218         {
219             AttributeItem *ai = branchItem->getAttributeNum(i);
220             if (ai) {
221                 attributeModel->insertRow (i, QModelIndex ());
222                 attributeModel->setData(attributeModel->index(i, 0, QModelIndex()),
223                     ai->getKey());
224                 attributeModel->setData(attributeModel->index(i, 1, QModelIndex()),
225                     ai->getValue().toString());
226                 attributeModel->setData(attributeModel->index(i, 2, QModelIndex()),
227                     ai->getAttributeTypeString());
228             }
229         }
230
231         ui.attributeTableView->resizeColumnsToContents();
232
233         // Initialize Delegate
234         //attributeDelegate.setAttributeTable (mapEditor->attributeTable());
235         //ui.attributeTableView->setItemDelegate (&attributeDelegate);
236
237         } // BranchItem
238     }
239     else if (ti->getType() == TreeItem::Image) {
240         ui.tabWidget->setEnabled(true);
241         for (int i = 0; i < ui.tabWidget->count(); ++i)
242             ui.tabWidget->setTabEnabled(i, false);
243         ui.tabWidget->setTabEnabled(3, true);
244         ui.tabWidget->setCurrentIndex(3);
245     }
246     else if (ti->getType() == TreeItem::Attribute) {
247         ui.tabWidget->setEnabled(true);
248         for (int i = 0; i < 3; ++i)
249             ui.tabWidget->setTabEnabled(i, false);
250         ui.tabWidget->setTabEnabled(3, true);
251     }
252     else {
253         ui.tabWidget->setEnabled(false);
254     }
255     connectSignals();
256 }
257
258 void BranchPropertyEditor::setModel(VymModel *m)
259 {
260     model = m;
261     if (model)
262         setItem(model->getSelectedItem());
263     else
264         ui.tabWidget->setEnabled(false);
265 }
266
267 void BranchPropertyEditor::frameTypeChanged(int i)
268 {
269     if (model) {
270         switch (i) {
271         case 0:
272             model->setFrameType(FrameObj::NoFrame);
273             break;
274         case 1:
275             model->setFrameType(FrameObj::Rectangle);
276             break;
277         case 2:
278             model->setFrameType(FrameObj::RoundedRectangle);
279             break;
280         case 3:
281             model->setFrameType(FrameObj::Ellipse);
282             break;
283         case 4:
284             model->setFrameType(FrameObj::Cloud);
285             break;
286         }
287         setItem(branchItem);
288     }
289 }
290
291 void BranchPropertyEditor::framePenColorClicked()
292 {
293     if (model) {
294         QColor col = QColorDialog::getColor(penColor, this);
295         if (col.isValid()) {
296             penColor = col;
297             model->setFramePenColor(penColor);
298         }
299     }
300 }
301
302 void BranchPropertyEditor::frameBrushColorClicked()
303 {
304     if (model) {
305         QColor col = QColorDialog::getColor(brushColor, this);
306         if (col.isValid()) {
307             brushColor = col;
308             model->setFrameBrushColor(brushColor);
309         }
310     }
311 }
312
313 void BranchPropertyEditor::framePaddingChanged(int i)
314 {
315     if (model)
316         model->setFramePadding(i);
317 }
318
319 void BranchPropertyEditor::frameBorderWidthChanged(int i)
320 {
321     if (model)
322         model->setFrameBorderWidth(i);
323 }
324
325 void BranchPropertyEditor::frameIncludeChildrenChanged(int i)
326 {
327     if (model)
328         model->setFrameIncludeChildren(i);
329 }
330
331 void BranchPropertyEditor::linkHideUnselectedChanged(int i)
332 {
333     model->setHideLinkUnselected(i);
334 }
335
336 void BranchPropertyEditor::incImgVerChanged(int i)
337 {
338     if (model)
339         model->setIncludeImagesVer(i);
340 }
341
342 void BranchPropertyEditor::incImgHorChanged(int i)
343 {
344     if (model)
345         model->setIncludeImagesHor(i);
346 }
347
348 void BranchPropertyEditor::childrenFreePositioningChanged(int i)
349 {
350     if (model) {
351         if (i > 0)
352             model->setChildrenLayout(BranchItem::FreePositioning);
353         else
354             model->setChildrenLayout(BranchItem::AutoPositioning);
355     }
356 }
357
358 void BranchPropertyEditor::taskPriorityDeltaChanged(int n)
359 {
360     if (model)
361         model->setTaskPriorityDelta(n);
362 }
363
364 void BranchPropertyEditor::closeEvent(QCloseEvent *ce)
365 {
366     ce->accept(); // can be reopened with show()
367     hide();
368     emit(windowClosed());
369     return;
370 }
371
372 void BranchPropertyEditor::addAttributeClicked()
373 {
374     qDebug() << "BranchPropEditor::addAttribute";
375
376 /*
377     // Add empty line for adding attributes
378     attributeModel->insertRow (attributeModel->rowCount (),QModelIndex ());
379     attributeModel->setData(attributeModel->index(attributeModel->rowCount()-1,
380 0, QModelIndex()),  "Add new");
381     attributeModel->setData(attributeModel->index(attributeModel->rowCount()-1,
382 2, QModelIndex()),  "Undefined");
383
384     // Select attribute from list
385     ui.attributeTableView->edit
386 (attributeModel->index(attributeModel->rowCount()-1,0, QModelIndex() ));
387     ui.attributeTableView->resizeColumnsToContents();
388
389 //  QString attname=attributeModel->in
390 //  attributeModel->setData(attributeModel->index(attributeModel->rowCount()-1,
391 2, QModelIndex()),  );
392
393
394
395     ui.attributeTableView->edit
396 (attributeModel->index(attributeModel->rowCount()-1,1, QModelIndex() ));
397 */
398
399 }
400
401 void BranchPropertyEditor::deleteAttributeClicked()
402 {
403     qDebug() << "BranchPropEditor::deleteAttribute";
404 }
405
406 void BranchPropertyEditor::connectSignals()
407 {
408     // Frame
409     connect(ui.framePenColorButton, SIGNAL(clicked()), this,
410             SLOT(framePenColorClicked()));
411     connect(ui.framePaddingSpinBox, SIGNAL(valueChanged(int)), this,
412             SLOT(framePaddingChanged(int)));
413     connect(ui.frameWidthSpinBox, SIGNAL(valueChanged(int)), this,
414             SLOT(frameBorderWidthChanged(int)));
415     connect(ui.frameBrushColorButton, SIGNAL(clicked()), this,
416             SLOT(frameBrushColorClicked()));
417     connect(ui.frameTypeCombo, SIGNAL(currentIndexChanged(int)), this,
418             SLOT(frameTypeChanged(int)));
419     connect(ui.includeChildrenCheckBox, SIGNAL(stateChanged(int)), this,
420             SLOT(frameIncludeChildrenChanged(int)));
421
422     // Link
423     connect(ui.hideLinkIfUnselected, SIGNAL(stateChanged(int)), this,
424             SLOT(linkHideUnselectedChanged(int)));
425
426     // Layout
427     connect(ui.incImgVer, SIGNAL(stateChanged(int)), this,
428             SLOT(incImgVerChanged(int)));
429     connect(ui.incImgHor, SIGNAL(stateChanged(int)), this,
430             SLOT(incImgHorChanged(int)));
431     connect(ui.childrenFreePositioning, SIGNAL(stateChanged(int)), this,
432             SLOT(childrenFreePositioningChanged(int)));
433
434     // Tasks
435     connect(ui.taskPrioDelta, SIGNAL(valueChanged(int)), this,
436             SLOT(taskPriorityDeltaChanged(int)));
437
438     // Attributes
439     // For the time being hide above buttons, not used
440     /*
441     connect (
442         ui.addAttributeButton, SIGNAL (clicked()),
443         this, SLOT (addAttributeClicked()));
444     connect (
445         ui.deleteAttributeButton, SIGNAL (clicked()),
446         this, SLOT (deleteAttributeClicked()));
447
448     */
449     ui.addAttributeButton->hide();
450     ui.deleteAttributeButton->hide();
451 }
452
453 void BranchPropertyEditor::disconnectSignals()
454 {
455     // Frame
456     disconnect(ui.framePenColorButton, 0, 0, 0);
457     disconnect(ui.framePaddingSpinBox, 0, 0, 0);
458     disconnect(ui.frameWidthSpinBox, 0, 0, 0);
459     disconnect(ui.frameBrushColorButton, 0, 0, 0);
460     disconnect(ui.frameTypeCombo, 0, 0, 0);
461     disconnect(ui.includeChildrenCheckBox, 0, 0, 0);
462
463     // Link
464     disconnect(ui.hideLinkIfUnselected, 0, 0, 0);
465
466     // Layout
467     disconnect(ui.incImgVer, 0, 0, 0);
468     disconnect(ui.incImgHor, 0, 0, 0);
469     disconnect(ui.childrenFreePositioning, 0, 0, 0);
470
471     // Task
472     disconnect(ui.taskPrioDelta, 0, 0, 0);
473
474     // Attributes
475     disconnect (ui.addAttributeButton, 0, 0, 0);
476     disconnect (ui.deleteAttributeButton, 0, 0, 0);
477 }