/
PropertyItem.cpp
2574 lines (2151 loc) · 85.1 KB
/
PropertyItem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <algorithm>
# include <QComboBox>
# include <QFontDatabase>
# include <QLayout>
# include <QLocale>
# include <QPixmap>
# include <QTextStream>
# include <QTimer>
#endif
#include <Base/Tools.h>
#include <Base/Console.h>
#include <App/Application.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <App/PropertyGeo.h>
#include <App/PropertyFile.h>
#include <App/PropertyUnits.h>
#include <Gui/Application.h>
#include <Gui/Control.h>
#include <Gui/Document.h>
#include <Gui/Selection.h>
#include <Gui/ViewProviderDocumentObject.h>
#include <Gui/Placement.h>
#include <Gui/FileDialog.h>
#include <Gui/DlgPropertyLink.h>
#include <Gui/QuantitySpinBox.h>
#include "PropertyItem.h"
#include <SpinBox.h>
using namespace Gui::PropertyEditor;
TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertyItem, Base::BaseClass);
PropertyItem::PropertyItem() : parentItem(0), readonly(false), cleared(false)
{
precision = Base::UnitsApi::getDecimals();
}
PropertyItem::~PropertyItem()
{
qDeleteAll(childItems);
}
void PropertyItem::initialize()
{
}
void PropertyItem::reset()
{
qDeleteAll(childItems);
childItems.clear();
}
void PropertyItem::setPropertyData(const std::vector<App::Property*>& items)
{
//if we have a single property we can bind it for expression handling
if(items.size() == 1) {
const App::Property& p = *items.front();
if(!(p.getContainer()->getPropertyType(&p) & App::Prop_ReadOnly)) {
App::ObjectIdentifier id(p);
std::vector<App::ObjectIdentifier> paths;
p.getPaths(paths);
//there may be no paths available in this property (for example an empty constraint list)
if(id.getProperty() && !paths.empty())
bind(id);
}
else
setReadOnly(true);
}
propertyItems = items;
updateData();
this->initialize();
}
void PropertyItem::updateData()
{
bool ro = true;
for (std::vector<App::Property*>::const_iterator it = propertyItems.begin();
it != propertyItems.end(); ++it) {
App::PropertyContainer* parent = (*it)->getContainer();
if (parent)
ro &= (parent->isReadOnly(*it) || (*it)->StatusBits.test(2));
}
this->setReadOnly(ro);
}
const std::vector<App::Property*>& PropertyItem::getPropertyData() const
{
return propertyItems;
}
bool PropertyItem::hasProperty(const App::Property* prop) const
{
std::vector<App::Property*>::const_iterator it = std::find(propertyItems.begin(), propertyItems.end(), prop);
if (it != propertyItems.end())
return true;
else
return false;
}
bool PropertyItem::removeProperty(const App::Property* prop)
{
std::vector<App::Property*>::iterator it = std::find(propertyItems.begin(), propertyItems.end(), prop);
if (it != propertyItems.end()) {
propertyItems.erase(it);
}
return propertyItems.empty();
}
App::Property* PropertyItem::getFirstProperty()
{
if (propertyItems.empty())
return 0;
return propertyItems.front();
}
const App::Property* PropertyItem::getFirstProperty() const
{
if (propertyItems.empty())
return 0;
return propertyItems.front();
}
void PropertyItem::setParent(PropertyItem* parent)
{
parentItem = parent;
}
PropertyItem *PropertyItem::parent() const
{
return parentItem;
}
void PropertyItem::appendChild(PropertyItem *item)
{
childItems.append(item);
}
void PropertyItem::removeChildren(int from, int to)
{
int count = to-from+1;
for (int i=0; i<count; i++) {
PropertyItem* child = childItems.takeAt(from);
delete child;
}
}
PropertyItem *PropertyItem::child(int row)
{
return childItems.value(row);
}
int PropertyItem::childCount() const
{
return childItems.count();
}
int PropertyItem::columnCount() const
{
return 2;
}
void PropertyItem::setReadOnly(bool ro)
{
readonly = ro;
for (QList<PropertyItem*>::iterator it = childItems.begin(); it != childItems.end(); ++it)
(*it)->setReadOnly(ro);
}
bool PropertyItem::isReadOnly() const
{
return readonly;
}
void PropertyItem::setDecimals(int prec)
{
precision = prec;
}
int PropertyItem::decimals() const
{
return precision;
}
QVariant PropertyItem::toolTip(const App::Property* prop) const
{
return QVariant(QString::fromUtf8(prop->getDocumentation()));
}
QVariant PropertyItem::decoration(const App::Property* /*prop*/) const
{
return QVariant();
}
QVariant PropertyItem::toString(const QVariant& prop) const
{
return prop;
}
QVariant PropertyItem::value(const App::Property* /*prop*/) const
{
return QVariant();
}
void PropertyItem::setValue(const QVariant& /*value*/)
{
}
QString PropertyItem::pythonIdentifier(const App::Property* prop) const
{
App::PropertyContainer* parent = prop->getContainer();
if (parent->getTypeId() == App::Document::getClassTypeId()) {
App::Document* doc = static_cast<App::Document*>(parent);
QString docName = QString::fromAscii(App::GetApplication().getDocumentName(doc));
QString propName = QString::fromAscii(parent->getPropertyName(prop));
return QString::fromAscii("FreeCAD.getDocument(\"%1\").%2").arg(docName).arg(propName);
}
if (parent->getTypeId().isDerivedFrom(App::DocumentObject::getClassTypeId())) {
App::DocumentObject* obj = static_cast<App::DocumentObject*>(parent);
App::Document* doc = obj->getDocument();
QString docName = QString::fromAscii(App::GetApplication().getDocumentName(doc));
QString objName = QString::fromAscii(obj->getNameInDocument());
QString propName = QString::fromAscii(parent->getPropertyName(prop));
return QString::fromAscii("FreeCAD.getDocument(\"%1\").getObject(\"%2\").%3")
.arg(docName).arg(objName).arg(propName);
}
if (parent->getTypeId().isDerivedFrom(Gui::ViewProviderDocumentObject::getClassTypeId())) {
App::DocumentObject* obj = static_cast<Gui::ViewProviderDocumentObject*>(parent)->getObject();
App::Document* doc = obj->getDocument();
QString docName = QString::fromAscii(App::GetApplication().getDocumentName(doc));
QString objName = QString::fromAscii(obj->getNameInDocument());
QString propName = QString::fromAscii(parent->getPropertyName(prop));
return QString::fromAscii("FreeCADGui.getDocument(\"%1\").getObject(\"%2\").%3")
.arg(docName).arg(objName).arg(propName);
}
return QString();
}
QWidget* PropertyItem::createEditor(QWidget* /*parent*/, const QObject* /*receiver*/, const char* /*method*/) const
{
return 0;
}
void PropertyItem::setEditorData(QWidget * /*editor*/, const QVariant& /*data*/) const
{
}
QVariant PropertyItem::editorData(QWidget * /*editor*/) const
{
return QVariant();
}
QString PropertyItem::propertyName() const
{
if (propName.isEmpty())
return QLatin1String("<empty>");
return propName;
}
void PropertyItem::setPropertyName(const QString& name)
{
setObjectName(name);
QString display;
bool upper = false;
for (int i=0; i<name.length(); i++) {
if (name[i].isUpper() && !display.isEmpty()) {
// if there is a sequence of capital letters do not insert spaces
if (!upper)
display += QLatin1String(" ");
}
upper = name[i].isUpper();
display += name[i];
}
propName = display;
}
void PropertyItem::setPropertyValue(const QString& value)
{
for (std::vector<App::Property*>::const_iterator it = propertyItems.begin();
it != propertyItems.end(); ++it) {
App::PropertyContainer* parent = (*it)->getContainer();
if (parent && !parent->isReadOnly(*it) && !(*it)->StatusBits.test(2)) {
QString cmd = QString::fromAscii("%1 = %2").arg(pythonIdentifier(*it)).arg(value);
Gui::Application::Instance->runPythonCode((const char*)cmd.toUtf8());
}
}
}
QVariant PropertyItem::data(int column, int role) const
{
// property name
if (column == 0) {
if (role == Qt::DisplayRole)
return propertyName();
// no properties set
if (propertyItems.empty())
return QVariant();
else if (role == Qt::ToolTipRole)
return toolTip(propertyItems[0]);
else
return QVariant();
}
else {
// no properties set
if (propertyItems.empty()) {
PropertyItem* parent = this->parent();
if (!parent || !parent->parent())
return QVariant();
if (role == Qt::EditRole)
return parent->property(qPrintable(objectName()));
else if (role == Qt::DisplayRole) {
QVariant val = parent->property(qPrintable(objectName()));
return toString(val);
}
else
return QVariant();
}
if (role == Qt::EditRole)
return value(propertyItems[0]);
else if (role == Qt::DecorationRole)
return decoration(propertyItems[0]);
else if (role == Qt::DisplayRole)
return toString(value(propertyItems[0]));
else if (role == Qt::ToolTipRole)
return toolTip(propertyItems[0]);
else
return QVariant();
}
}
bool PropertyItem::setData (const QVariant& value)
{
//check if we have an expression set. If so we do nothing, as than the editor is responsible
//for issuing the relevant python code
if(hasExpression())
return true;
cleared = false;
// This is the basic mechanism to set the value to
// a property and if no property is set for this item
// it delegates it to its parent which sets then the
// property or delegates again to its parent...
if (propertyItems.empty()) {
PropertyItem* parent = this->parent();
if (!parent || !parent->parent())
return false;
parent->setProperty(qPrintable(objectName()),value);
return true;
}
else {
setValue(value);
return true;
}
}
Qt::ItemFlags PropertyItem::flags(int column) const
{
Qt::ItemFlags basicFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (column == 1 && !isReadOnly())
return basicFlags | Qt::ItemIsEditable;
else
return basicFlags;
}
int PropertyItem::row() const
{
if (parentItem)
return parentItem->childItems.indexOf(const_cast<PropertyItem*>(this));
return 0;
}
void PropertyItem::bind(const App::ObjectIdentifier& _path) {
Gui::ExpressionBinding::bind(_path);
propertyBound();
}
void PropertyItem::bind(const App::Property& prop) {
Gui::ExpressionBinding::bind(prop);
propertyBound();
}
// --------------------------------------------------------------------
TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertyStringItem, Gui::PropertyEditor::PropertyItem);
PropertyStringItem::PropertyStringItem()
{
}
QVariant PropertyStringItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyString::getClassTypeId()));
std::string value = static_cast<const App::PropertyString*>(prop)->getValue();
return QVariant(QString::fromUtf8(value.c_str()));
}
void PropertyStringItem::setValue(const QVariant& value)
{
if (!value.canConvert(QVariant::String))
return;
QString val = value.toString();
QString data = QString::fromAscii("\"%1\"").arg(val);
setPropertyValue(data);
}
QWidget* PropertyStringItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
QLineEdit *le = new QLineEdit(parent);
le->setFrame(false);
le->setReadOnly(isReadOnly());
QObject::connect(le, SIGNAL(textChanged(const QString&)), receiver, method);
return le;
}
void PropertyStringItem::setEditorData(QWidget *editor, const QVariant& data) const
{
QLineEdit *le = qobject_cast<QLineEdit*>(editor);
le->setText(data.toString());
}
QVariant PropertyStringItem::editorData(QWidget *editor) const
{
QLineEdit *le = qobject_cast<QLineEdit*>(editor);
return QVariant(le->text());
}
// --------------------------------------------------------------------
TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertyFontItem, Gui::PropertyEditor::PropertyItem);
PropertyFontItem::PropertyFontItem()
{
}
QVariant PropertyFontItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyFont::getClassTypeId()));
std::string value = static_cast<const App::PropertyFont*>(prop)->getValue();
return QVariant(QString::fromUtf8(value.c_str()));
}
void PropertyFontItem::setValue(const QVariant& value)
{
if (!value.canConvert(QVariant::String))
return;
QString val = value.toString();
QString data = QString::fromAscii("\"%1\"").arg(val);
setPropertyValue(data);
}
QWidget* PropertyFontItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
QComboBox *cb = new QComboBox(parent);
cb->setFrame(false);
cb->setDisabled(isReadOnly());
QObject::connect(cb, SIGNAL(activated(const QString&)), receiver, method);
return cb;
}
void PropertyFontItem::setEditorData(QWidget *editor, const QVariant& data) const
{
QComboBox *cb = qobject_cast<QComboBox*>(editor);
QFontDatabase fdb;
QStringList familyNames = fdb.families(QFontDatabase::Any);
cb->addItems(familyNames);
int index = familyNames.indexOf(data.toString());
cb->setCurrentIndex(index);
}
QVariant PropertyFontItem::editorData(QWidget *editor) const
{
QComboBox *cb = qobject_cast<QComboBox*>(editor);
return QVariant(cb->currentText());
}
// --------------------------------------------------------------------
TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertySeparatorItem, Gui::PropertyEditor::PropertyItem);
QWidget* PropertySeparatorItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
return 0;
}
// --------------------------------------------------------------------
TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertyIntegerItem, Gui::PropertyEditor::PropertyItem);
PropertyIntegerItem::PropertyIntegerItem()
{
}
QVariant PropertyIntegerItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyInteger::getClassTypeId()));
int value = (int)static_cast<const App::PropertyInteger*>(prop)->getValue();
return QVariant(value);
}
void PropertyIntegerItem::setValue(const QVariant& value)
{
if (!value.canConvert(QVariant::Int))
return;
int val = value.toInt();
QString data = QString::fromAscii("%1").arg(val);
setPropertyValue(data);
}
QWidget* PropertyIntegerItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
Gui::IntSpinBox *sb = new Gui::IntSpinBox(parent);
sb->setFrame(false);
sb->setReadOnly(isReadOnly());
QObject::connect(sb, SIGNAL(valueChanged(int)), receiver, method);
if(isBound()) {
sb->bind(getPath());
sb->setAutoApply(true);
}
return sb;
}
void PropertyIntegerItem::setEditorData(QWidget *editor, const QVariant& data) const
{
QSpinBox *sb = qobject_cast<QSpinBox*>(editor);
sb->setRange(INT_MIN, INT_MAX);
sb->setValue(data.toInt());
}
QVariant PropertyIntegerItem::editorData(QWidget *editor) const
{
QSpinBox *sb = qobject_cast<QSpinBox*>(editor);
return QVariant(sb->value());
}
QVariant PropertyIntegerItem::toString(const QVariant& v) const {
QString string(PropertyItem::toString(v).toString());
if(hasExpression())
string += QString::fromAscii(" ( %1 )").arg(QString::fromStdString(getExpressionString()));
return QVariant(string);
}
// --------------------------------------------------------------------
TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertyIntegerConstraintItem, Gui::PropertyEditor::PropertyItem);
PropertyIntegerConstraintItem::PropertyIntegerConstraintItem()
{
}
QVariant PropertyIntegerConstraintItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyIntegerConstraint::getClassTypeId()));
int value = (int)static_cast<const App::PropertyIntegerConstraint*>(prop)->getValue();
return QVariant(value);
}
void PropertyIntegerConstraintItem::setValue(const QVariant& value)
{
if (!value.canConvert(QVariant::Int))
return;
int val = value.toInt();
QString data = QString::fromAscii("%1").arg(val);
setPropertyValue(data);
}
QWidget* PropertyIntegerConstraintItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
Gui::IntSpinBox *sb = new Gui::IntSpinBox(parent);
sb->setFrame(false);
sb->setReadOnly(isReadOnly());
QObject::connect(sb, SIGNAL(valueChanged(int)), receiver, method);
if(isBound()) {
sb->bind(getPath());
sb->setAutoApply(true);
}
return sb;
}
void PropertyIntegerConstraintItem::setEditorData(QWidget *editor, const QVariant& /*data*/) const
{
const App::PropertyIntegerConstraint* prop = static_cast
<const App::PropertyIntegerConstraint*>(getFirstProperty());
const App::PropertyIntegerConstraint::Constraints* c = prop->getConstraints();
QSpinBox *sb = qobject_cast<QSpinBox*>(editor);
if (c) {
sb->setMinimum(c->LowerBound);
sb->setMaximum(c->UpperBound);
sb->setSingleStep(c->StepSize);
}
else {
sb->setMinimum(INT_MIN);
sb->setMaximum(INT_MAX);
}
sb->setValue(prop->getValue());
}
QVariant PropertyIntegerConstraintItem::editorData(QWidget *editor) const
{
QSpinBox *sb = qobject_cast<QSpinBox*>(editor);
return QVariant(sb->value());
}
QVariant PropertyIntegerConstraintItem::toString(const QVariant& v) const {
QString string(PropertyItem::toString(v).toString());
if(hasExpression())
string += QString::fromAscii(" ( %1 )").arg(QString::fromStdString(getExpressionString()));
return QVariant(string);
}
// --------------------------------------------------------------------
TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertyFloatItem, Gui::PropertyEditor::PropertyItem);
PropertyFloatItem::PropertyFloatItem()
{
}
QVariant PropertyFloatItem::toString(const QVariant& prop) const
{
double value = prop.toDouble();
QString data = QLocale::system().toString(value, 'f', decimals());
if(hasExpression())
data += QString::fromAscii(" ( %1 )").arg(QString::fromStdString(getExpressionString()));
return QVariant(data);
}
QVariant PropertyFloatItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyFloat::getClassTypeId()));
double value = static_cast<const App::PropertyFloat*>(prop)->getValue();
return QVariant(value);
}
void PropertyFloatItem::setValue(const QVariant& value)
{
if (!value.canConvert(QVariant::Double))
return;
double val = value.toDouble();
QString data = QString::fromAscii("%1").arg(val,0,'f',decimals());
setPropertyValue(data);
}
QWidget* PropertyFloatItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
Gui::DoubleSpinBox *sb = new Gui::DoubleSpinBox(parent);
sb->setFrame(false);
sb->setDecimals(decimals());
sb->setReadOnly(isReadOnly());
QObject::connect(sb, SIGNAL(valueChanged(double)), receiver, method);
if(isBound()) {
sb->bind(getPath());
sb->setAutoApply(true);
}
return sb;
}
void PropertyFloatItem::setEditorData(QWidget *editor, const QVariant& data) const
{
QDoubleSpinBox *sb = qobject_cast<QDoubleSpinBox*>(editor);
sb->setRange((double)INT_MIN, (double)INT_MAX);
sb->setValue(data.toDouble());
}
QVariant PropertyFloatItem::editorData(QWidget *editor) const
{
QDoubleSpinBox *sb = qobject_cast<QDoubleSpinBox*>(editor);
return QVariant(sb->value());
}
// --------------------------------------------------------------------
TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertyUnitItem, Gui::PropertyEditor::PropertyItem);
PropertyUnitItem::PropertyUnitItem()
{
}
QVariant PropertyUnitItem::toString(const QVariant& prop) const
{
const Base::Quantity& unit = prop.value<Base::Quantity>();
QString string = unit.getUserString();
if(hasExpression())
string += QString::fromAscii(" ( %1 )").arg(QString::fromStdString(getExpressionString()));
return QVariant(string);
}
QVariant PropertyUnitItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyQuantity::getClassTypeId()));
Base::Quantity value = static_cast<const App::PropertyQuantity*>(prop)->getQuantityValue();
return QVariant::fromValue<Base::Quantity>(value);
}
void PropertyUnitItem::setValue(const QVariant& value)
{
if (!value.canConvert<Base::Quantity>())
return;
const Base::Quantity& val = value.value<Base::Quantity>();
QString unit = QString::fromLatin1("'%1 %2'").arg(val.getValue()).arg(val.getUnit().getString());
setPropertyValue(unit);
}
QWidget* PropertyUnitItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
Gui::QuantitySpinBox *infield = new Gui::QuantitySpinBox(parent);
infield->setFrame(false);
infield->setMinimumHeight(0);
infield->setReadOnly(isReadOnly());
//if we are bound to an expression we need to bind it to the input field
if(isBound()) {
infield->bind(getPath());
infield->setAutoApply(true);
}
QObject::connect(infield, SIGNAL(valueChanged(double)), receiver, method);
return infield;
}
void PropertyUnitItem::setEditorData(QWidget *editor, const QVariant& data) const
{
const Base::Quantity& value = data.value<Base::Quantity>();
Gui::QuantitySpinBox *infield = qobject_cast<Gui::QuantitySpinBox*>(editor);
infield->setValue(value);
infield->selectAll();
}
QVariant PropertyUnitItem::editorData(QWidget *editor) const
{
Gui::QuantitySpinBox *infield = qobject_cast<Gui::QuantitySpinBox*>(editor);
Base::Quantity value = infield->value();
return QVariant::fromValue<Base::Quantity>(value);
}
// --------------------------------------------------------------------
TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertyUnitConstraintItem, Gui::PropertyEditor::PropertyUnitItem);
PropertyUnitConstraintItem::PropertyUnitConstraintItem()
{
}
void PropertyUnitConstraintItem::setEditorData(QWidget *editor, const QVariant& data) const
{
const Base::Quantity& value = data.value<Base::Quantity>();
Gui::QuantitySpinBox *infield = qobject_cast<Gui::QuantitySpinBox*>(editor);
infield->setValue(value);
infield->selectAll();
const App::PropertyQuantityConstraint* prop = static_cast
<const App::PropertyQuantityConstraint*>(getFirstProperty());
const App::PropertyQuantityConstraint::Constraints* c = prop->getConstraints();
if (c) {
infield->setMinimum(c->LowerBound);
infield->setMaximum(c->UpperBound);
infield->setSingleStep(c->StepSize);
}
else {
infield->setMinimum((double)INT_MIN);
infield->setMaximum((double)INT_MAX);
}
}
// --------------------------------------------------------------------
TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertyFloatConstraintItem, Gui::PropertyEditor::PropertyItem);
PropertyFloatConstraintItem::PropertyFloatConstraintItem()
{
}
QVariant PropertyFloatConstraintItem::toString(const QVariant& prop) const
{
double value = prop.toDouble();
QString data = QLocale::system().toString(value, 'f', decimals());
return QVariant(data);
}
QVariant PropertyFloatConstraintItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyFloatConstraint::getClassTypeId()));
double value = static_cast<const App::PropertyFloatConstraint*>(prop)->getValue();
return QVariant(value);
}
void PropertyFloatConstraintItem::setValue(const QVariant& value)
{
if (!value.canConvert(QVariant::Double))
return;
double val = value.toDouble();
QString data = QString::fromAscii("%1").arg(val,0,'f',decimals());
setPropertyValue(data);
}
QWidget* PropertyFloatConstraintItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
Gui::DoubleSpinBox *sb = new Gui::DoubleSpinBox(parent);
sb->setDecimals(decimals());
sb->setFrame(false);
sb->setReadOnly(isReadOnly());
QObject::connect(sb, SIGNAL(valueChanged(double)), receiver, method);
if(isBound()) {
sb->bind(getPath());
sb->setAutoApply(true);
}
return sb;
}
void PropertyFloatConstraintItem::setEditorData(QWidget *editor, const QVariant& /*data*/) const
{
const App::PropertyFloatConstraint* prop = static_cast
<const App::PropertyFloatConstraint*>(getFirstProperty());
const App::PropertyFloatConstraint::Constraints* c = prop->getConstraints();
QDoubleSpinBox *sb = qobject_cast<QDoubleSpinBox*>(editor);
if (c) {
sb->setMinimum(c->LowerBound);
sb->setMaximum(c->UpperBound);
sb->setSingleStep(c->StepSize);
}
else {
sb->setMinimum((double)INT_MIN);
sb->setMaximum((double)INT_MAX);
sb->setSingleStep(0.1);
}
sb->setValue(prop->getValue());
}
QVariant PropertyFloatConstraintItem::editorData(QWidget *editor) const
{
QDoubleSpinBox *sb = qobject_cast<QDoubleSpinBox*>(editor);
return QVariant(sb->value());
}
// --------------------------------------------------------------------
TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertyAngleItem, Gui::PropertyEditor::PropertyFloatItem);
PropertyAngleItem::PropertyAngleItem()
{
}
void PropertyAngleItem::setEditorData(QWidget *editor, const QVariant& data) const
{
const App::PropertyQuantityConstraint* prop = static_cast
<const App::PropertyQuantityConstraint*>(getFirstProperty());
const App::PropertyQuantityConstraint::Constraints* c = 0;
if (prop) {
c = prop->getConstraints();
}
QDoubleSpinBox *sb = qobject_cast<QDoubleSpinBox*>(editor);
if (c) {
sb->setMinimum(c->LowerBound);
sb->setMaximum(c->UpperBound);
sb->setSingleStep(c->StepSize);
}
else {
sb->setMinimum((double)INT_MIN);
sb->setMaximum((double)INT_MAX);
sb->setSingleStep(1.0);
}
sb->setValue(data.toDouble());
sb->setSuffix(QString::fromUtf8(" \xc2\xb0"));
}
QVariant PropertyAngleItem::toString(const QVariant& prop) const
{
double value = prop.toDouble();
QString data = QString::fromUtf8("%1 \xc2\xb0")
.arg(QLocale::system().toString(value, 'f', decimals()));
return QVariant(data);
}
// --------------------------------------------------------------------
TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertyBoolItem, Gui::PropertyEditor::PropertyItem);
PropertyBoolItem::PropertyBoolItem()
{
}
QVariant PropertyBoolItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyBool::getClassTypeId()));
bool value = ((App::PropertyBool*)prop)->getValue();
return QVariant(value);
}
void PropertyBoolItem::setValue(const QVariant& value)
{
if (!value.canConvert(QVariant::Bool))
return;
bool val = value.toBool();
QString data = (val ? QLatin1String("True") : QLatin1String("False"));
setPropertyValue(data);
}
QWidget* PropertyBoolItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
QComboBox *cb = new QComboBox(parent);
cb->setFrame(false);
cb->addItem(QLatin1String("false"));
cb->addItem(QLatin1String("true"));
cb->setDisabled(isReadOnly());
QObject::connect(cb, SIGNAL(activated(int)), receiver, method);
return cb;
}
void PropertyBoolItem::setEditorData(QWidget *editor, const QVariant& data) const
{
QComboBox *cb = qobject_cast<QComboBox*>(editor);
cb->setCurrentIndex(cb->findText(data.toString()));
}
QVariant PropertyBoolItem::editorData(QWidget *editor) const
{
QComboBox *cb = qobject_cast<QComboBox*>(editor);
return QVariant(cb->currentText());
}