-
Notifications
You must be signed in to change notification settings - Fork 122
/
FunctionBrowser.cpp
1590 lines (1460 loc) · 43.3 KB
/
FunctionBrowser.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
#include "MantidQtMantidWidgets/FunctionBrowser.h"
#include "MantidQtMantidWidgets/SelectFunctionDialog.h"
//#include "MantidQtMantidWidgets/MultifitSetupDialog.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/IPeakFunction.h"
#include "MantidAPI/IBackgroundFunction.h"
#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/ParameterTie.h"
#include "MantidAPI/IConstraint.h"
#include "MantidAPI/ConstraintFactory.h"
#include "MantidAPI/Expression.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/LibraryManager.h"
#include "MantidAPI/CostFunctionFactory.h"
#include "MantidAPI/ICostFunction.h"
#include "MantidQtMantidWidgets/UserFunctionDialog.h"
#include "qttreepropertybrowser.h"
#include "qtpropertymanager.h"
// Suppress a warning coming out of code that isn't ours
#if defined(__INTEL_COMPILER)
#pragma warning disable 1125
#elif defined(__GNUC__)
#if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 6 )
#pragma GCC diagnostic push
#endif
#pragma GCC diagnostic ignored "-Woverloaded-virtual"
#endif
#include "qteditorfactory.h"
#include "StringDialogEditorFactory.h"
#include "DoubleEditorFactory.h"
#if defined(__INTEL_COMPILER)
#pragma warning enable 1125
#elif defined(__GNUC__)
#if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 6 )
#pragma GCC diagnostic pop
#endif
#endif
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QPushButton>
#include <QMenu>
#include <QMessageBox>
#include <QInputDialog>
#include <QSettings>
#include <QFileInfo>
#include <QApplication>
#include <QClipboard>
#include <QSignalMapper>
#include <QMetaMethod>
#include <QTreeWidget>
#include <algorithm>
namespace MantidQt
{
namespace MantidWidgets
{
namespace
{
class FormulaDialogEditor: public StringDialogEditor
{
public:
FormulaDialogEditor(QtProperty *property, QWidget *parent)
:StringDialogEditor(property,parent){}
protected slots:
void runDialog()
{
MantidQt::MantidWidgets::UserFunctionDialog *dlg = new MantidQt::MantidWidgets::UserFunctionDialog((QWidget*)parent(),getText());
if (dlg->exec() == QDialog::Accepted)
{
setText(dlg->getFormula());
updateProperty();
};
}
};
class FormulaDialogEditorFactory: public StringDialogEditorFactory
{
public:
FormulaDialogEditorFactory(QObject* parent):StringDialogEditorFactory(parent){}
protected:
using QtAbstractEditorFactoryBase::createEditor; // Avoid Intel compiler warning
QWidget *createEditor(QtStringPropertyManager *manager, QtProperty *property,QWidget *parent)
{
(void) manager; //Avoid unused warning
return new FormulaDialogEditor(property,parent);
}
};
}
/**
* Constructor
* @param parent :: The parent widget.
*/
FunctionBrowser::FunctionBrowser(QWidget *parent)
:QWidget(parent)
{
// create m_browser
createBrowser();
createActions();
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(m_browser);
layout->setContentsMargins(0,0,0,0);
}
/**
* Destructor
*/
FunctionBrowser::~FunctionBrowser()
{
}
/**
* Create the Qt property browser and set up property managers.
*/
void FunctionBrowser::createBrowser()
{
/* Create property managers: they create, own properties, get and set values */
m_functionManager = new QtGroupPropertyManager(this);
m_parameterManager = new QtDoublePropertyManager(this);
m_attributeStringManager = new QtStringPropertyManager(this);
m_attributeDoubleManager = new QtDoublePropertyManager(this);
m_attributeIntManager = new QtIntPropertyManager(this);
m_attributeBoolManager = new QtBoolPropertyManager(this);
m_indexManager = new QtStringPropertyManager(this);
m_tieManager = new QtStringPropertyManager(this);
m_constraintManager = new QtStringPropertyManager(this);
m_formulaManager = new QtStringPropertyManager(this);
m_attributeVectorManager = new QtGroupPropertyManager(this);
m_attributeSizeManager = new QtIntPropertyManager(this);
m_attributeVectorDoubleManager = new QtDoublePropertyManager(this);
// create editor factories
QtSpinBoxFactory *spinBoxFactory = new QtSpinBoxFactory(this);
DoubleEditorFactory *doubleEditorFactory = new DoubleEditorFactory(this);
QtLineEditFactory *lineEditFactory = new QtLineEditFactory(this);
QtCheckBoxFactory *checkBoxFactory = new QtCheckBoxFactory(this);
//StringDialogEditorFactory* stringDialogEditFactory = new StringDialogEditorFactory(this);
FormulaDialogEditorFactory* formulaDialogEditFactory = new FormulaDialogEditorFactory(this);
m_browser = new QtTreePropertyBrowser();
// assign factories to property managers
m_browser->setFactoryForManager(m_parameterManager, doubleEditorFactory);
m_browser->setFactoryForManager(m_attributeStringManager, lineEditFactory);
m_browser->setFactoryForManager(m_attributeDoubleManager, doubleEditorFactory);
m_browser->setFactoryForManager(m_attributeIntManager, spinBoxFactory);
m_browser->setFactoryForManager(m_attributeBoolManager, checkBoxFactory);
m_browser->setFactoryForManager(m_indexManager, lineEditFactory);
m_browser->setFactoryForManager(m_tieManager, lineEditFactory);
m_browser->setFactoryForManager(m_constraintManager, lineEditFactory);
m_browser->setFactoryForManager(m_formulaManager, formulaDialogEditFactory);
m_browser->setFactoryForManager(m_attributeSizeManager, spinBoxFactory);
m_browser->setFactoryForManager(m_attributeVectorDoubleManager, doubleEditorFactory);
m_browser->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_browser, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(popupMenu(const QPoint &)));
//connect(m_browser, SIGNAL(currentItemChanged(QtBrowserItem*)), this, SLOT(currentItemChanged(QtBrowserItem*)));
connect(m_attributeStringManager,SIGNAL(propertyChanged(QtProperty*)),this,SLOT(attributeChanged(QtProperty*)));
connect(m_attributeDoubleManager,SIGNAL(propertyChanged(QtProperty*)),this,SLOT(attributeChanged(QtProperty*)));
connect(m_attributeIntManager,SIGNAL(propertyChanged(QtProperty*)),this,SLOT(attributeChanged(QtProperty*)));
connect(m_attributeBoolManager,SIGNAL(propertyChanged(QtProperty*)),this,SLOT(attributeChanged(QtProperty*)));
connect(m_formulaManager,SIGNAL(propertyChanged(QtProperty*)),this,SLOT(attributeChanged(QtProperty*)));
connect(m_attributeVectorDoubleManager,SIGNAL(propertyChanged(QtProperty*)),this,SLOT(attributeVectorDoubleChanged(QtProperty*)));
}
/**
* Create and connect actions
*/
void FunctionBrowser::createActions()
{
m_actionAddFunction = new QAction("Add function",this);
connect(m_actionAddFunction,SIGNAL(triggered()),this,SLOT(addFunction()));
m_actionRemoveFunction = new QAction("Remove function",this);
connect(m_actionRemoveFunction,SIGNAL(triggered()),this,SLOT(removeFunction()));
m_actionFixParameter = new QAction("Fix",this);
connect(m_actionFixParameter,SIGNAL(triggered()),this,SLOT(fixParameter()));
m_actionRemoveTie = new QAction("Remove tie",this);
connect(m_actionRemoveTie,SIGNAL(triggered()),this,SLOT(removeTie()));
m_actionAddTie = new QAction("Add tie",this);
connect(m_actionAddTie,SIGNAL(triggered()),this,SLOT(addTie()));
m_actionFromClipboard = new QAction("Copy from clipboard",this);
connect(m_actionFromClipboard,SIGNAL(triggered()),this,SLOT(copyFromClipboard()));
m_actionToClipboard = new QAction("Copy to clipboard",this);
connect(m_actionToClipboard,SIGNAL(triggered()),this,SLOT(copyToClipboard()));
m_actionConstraints = new QAction("Custom",this);
connect(m_actionConstraints,SIGNAL(triggered()),this,SLOT(addConstraints()));
m_actionConstraints10 = new QAction("10%",this);
connect(m_actionConstraints10,SIGNAL(triggered()),this,SLOT(addConstraints10()));
m_actionConstraints50 = new QAction("50%",this);
connect(m_actionConstraints50,SIGNAL(triggered()),this,SLOT(addConstraints50()));
m_actionRemoveConstraints = new QAction("Remove constraints",this);
connect(m_actionRemoveConstraints,SIGNAL(triggered()),this,SLOT(removeConstraints()));
m_actionRemoveConstraint = new QAction("Remove",this);
connect(m_actionRemoveConstraint,SIGNAL(triggered()),this,SLOT(removeConstraint()));
}
/**
* Clear the contents
*/
void FunctionBrowser::clear()
{
m_browser->clear();
m_properties.clear();
}
/**
* Set the function in the browser
* @param funStr :: FunctionFactory function creation string
*/
void FunctionBrowser::setFunction(const QString& funStr)
{
if ( funStr.isEmpty() ) return;
try
{
auto fun = Mantid::API::FunctionFactory::Instance().createInitialized( funStr.toStdString() );
if ( !fun ) return;
this->setFunction(fun);
}
catch(...)
{
// error in the input string
}
}
/**
* Set the function in the browser
* @param fun :: A function
*/
void FunctionBrowser::setFunction(Mantid::API::IFunction_sptr fun)
{
clear();
addFunction(NULL,fun);
}
/**
* Add a sub-property to a parent property
* @param parent :: The parent property
* @param subproperty :: New sub-property
*/
FunctionBrowser::AProperty FunctionBrowser::addProperty(QtProperty* parent, QtProperty* subproperty)
{
AProperty ap;
ap.prop = subproperty;
if (parent == NULL)
{
ap.item = m_browser->addProperty(subproperty);
}
else
{
parent->addSubProperty(subproperty);
auto items = m_browser->items(subproperty);
if (items.isEmpty())
{
throw std::runtime_error("Unexpected error in FunctionBrowser [1]");
}
ap.item = items[0];
}
ap.parent = parent;
m_properties[subproperty] = ap;
return ap;
}
/**
* Remove and delete property
* @param prop :: Property to remove.
*/
void FunctionBrowser::removeProperty(QtProperty *prop)
{
auto p = m_properties.find(prop);
if (p == m_properties.end()) return;
AProperty ap = *p;
// remove references to the children
auto children = prop->subProperties();
foreach(QtProperty* child,children)
{
m_properties.erase(child);
}
m_properties.erase(p);
if ( isFunction(prop) )
{
m_ties.erase(prop);
}
if ( isTie(prop) )
{//
for(auto it = m_ties.begin(); it != m_ties.end(); ++it)
{
if (it.value().tieProp == prop)
{
m_ties.erase(it);
break;
}
}
}
if ( isConstraint(prop) )
{
for(auto it = m_constraints.begin(); it != m_constraints.end(); ++it)
{
auto& cp = it.value();
if ( cp.lower == prop )
{
if ( !cp.upper )
{
m_constraints.erase(it);
}
else
{
cp.lower = NULL;
}
break;
}
else if ( cp.upper == prop )
{
if ( !cp.lower )
{
m_constraints.erase(it);
}
else
{
cp.upper = NULL;
}
break;
}
}
}
// remove property from Qt browser
if (ap.parent)
{
ap.parent->removeSubProperty(prop);
}
else
{
m_browser->removeProperty(prop);
}
delete prop;
}
/**
* Add a function property
* @param parent :: Parent function property or NULL
* @param funName :: Function name
* @return :: A set AProperty struct
*/
FunctionBrowser::AProperty FunctionBrowser::addFunctionProperty(QtProperty* parent, QString funName)
{
// check that parent is a function property
if (parent && dynamic_cast<QtAbstractPropertyManager*>(m_functionManager) != parent->propertyManager())
{
throw std::runtime_error("Unexpected error in FunctionBrowser [2]");
}
QtProperty* prop = m_functionManager->addProperty(funName);
return addProperty(parent, prop);
}
/**
* Add a parameter property
* @param parent :: Parent function property
* @param paramName :: Parameter name
* @param paramValue :: Parameter value
*/
FunctionBrowser::AProperty FunctionBrowser::addParameterProperty(QtProperty* parent, QString paramName, double paramValue)
{
// check that parent is a function property
if (!parent || dynamic_cast<QtAbstractPropertyManager*>(m_functionManager) != parent->propertyManager())
{
throw std::runtime_error("Unexpected error in FunctionBrowser [3]");
}
QtProperty* prop = m_parameterManager->addProperty(paramName);
m_parameterManager->setValue(prop,paramValue);
return addProperty(parent,prop);
}
/**
* Set a function.
* @param prop :: Property of the function or NULL
* @param fun :: A function
*/
void FunctionBrowser::setFunction(QtProperty* prop, Mantid::API::IFunction_sptr fun)
{
auto children = prop->subProperties();
foreach(QtProperty* child, children)
{
removeProperty(child);
}
addAttributeAndParameterProperties(prop, fun);
}
/**
* Add a function.
* @param prop :: Property of the parent composite function or NULL
* @param fun :: FunctionFactory function creation string
*/
void FunctionBrowser::addFunction(QtProperty* prop, Mantid::API::IFunction_sptr fun)
{
if ( !prop )
{
AProperty ap = addFunctionProperty(NULL,QString::fromStdString(fun->name()));
setFunction(ap.prop, fun);
}
else
{
Mantid::API::IFunction_sptr parentFun = getFunction(prop);
if ( !parentFun ) return;
auto cf = boost::dynamic_pointer_cast<Mantid::API::CompositeFunction>(parentFun);
if ( !cf )
{
throw std::runtime_error("FunctionBrowser: CompositeFunction is expected for addFunction");
}
cf->addFunction(fun);
setFunction(prop, cf);
}
}
/**
* Attribute visitor to create a QtProperty. Depending on the attribute type
* the appropriate apply() method is used.
*/
class CreateAttributePropertyForFunctionBrowser: public Mantid::API::IFunction::ConstAttributeVisitor<FunctionBrowser::AProperty>
{
public:
CreateAttributePropertyForFunctionBrowser(FunctionBrowser* browser, QtProperty* parent,QString attName)
:m_browser(browser),m_parent(parent),m_attName(attName)
{
// check that parent is a function property
if (!m_parent || dynamic_cast<QtAbstractPropertyManager*>(m_browser->m_functionManager) != m_parent->propertyManager())
{
throw std::runtime_error("Unexpected error in FunctionBrowser [4]");
}
}
protected:
/// Create string property
FunctionBrowser::AProperty apply(const std::string& str)const
{
QtProperty* prop = NULL;
if ( m_attName == "Formula" )
{
prop = m_browser->m_formulaManager->addProperty(m_attName);
m_browser->m_formulaManager->setValue(prop, QString::fromStdString(str));
}
else
{
prop = m_browser->m_attributeStringManager->addProperty(m_attName);
m_browser->m_attributeStringManager->setValue(prop, QString::fromStdString(str));
}
return m_browser->addProperty(m_parent,prop);
}
/// Create double property
FunctionBrowser::AProperty apply(const double& d)const
{
QtProperty* prop = m_browser->m_attributeDoubleManager->addProperty(m_attName);
m_browser->m_attributeDoubleManager->setValue(prop, d);
return m_browser->addProperty(m_parent,prop);
}
/// Create int property
FunctionBrowser::AProperty apply(const int& i)const
{
QtProperty* prop = m_browser->m_attributeIntManager->addProperty(m_attName);
m_browser->m_attributeIntManager->setValue(prop, i);
return m_browser->addProperty(m_parent,prop);
}
/// Create bool property
FunctionBrowser::AProperty apply(const bool& b)const
{
QtProperty* prop = m_browser->m_attributeBoolManager->addProperty(m_attName);
m_browser->m_attributeBoolManager->setValue(prop, b);
return m_browser->addProperty(m_parent,prop);
}
/// Create vector property
FunctionBrowser::AProperty apply(const std::vector<double>& v)const
{
QtProperty* prop = m_browser->m_attributeVectorManager->addProperty(m_attName);
FunctionBrowser::AProperty aprop = m_browser->addProperty(m_parent,prop);
QtProperty* sizeProp = m_browser->m_attributeSizeManager->addProperty("Size");
m_browser->m_attributeSizeManager->setValue( sizeProp, static_cast<int>(v.size()) );
m_browser->addProperty( prop, sizeProp );
sizeProp->setEnabled(false);
m_browser->m_attributeVectorDoubleManager->blockSignals(true);
QString parName = "value[%1]";
for(size_t i = 0; i < v.size(); ++i)
{
QtProperty *dprop = m_browser->m_attributeVectorDoubleManager->addProperty( parName.arg(i) );
m_browser->m_attributeVectorDoubleManager->setValue( dprop, v[i] );
m_browser->addProperty( prop, dprop );
}
m_browser->m_attributeVectorDoubleManager->blockSignals(false);
m_browser->m_browser->setExpanded( aprop.item, false );
return aprop;
}
private:
FunctionBrowser* m_browser;
QtProperty* m_parent;
QString m_attName;
};
/**
* Attribute visitor to set an attribute from a QtProperty. Depending on the attribute type
* the appropriate apply() method is used.
*/
class SetAttributeFromProperty: public Mantid::API::IFunction::AttributeVisitor<>
{
public:
SetAttributeFromProperty(FunctionBrowser* browser, QtProperty* prop)
:m_browser(browser),m_prop(prop)
{
}
protected:
/// Set string attribute
void apply(std::string& str)const
{
QString attName = m_prop->propertyName();
if ( attName == "Formula" )
{
str = m_browser->m_formulaManager->value(m_prop).toStdString();
}
else
{
str = m_browser->m_attributeStringManager->value(m_prop).toStdString();
}
}
/// Set double attribute
void apply(double& d)const
{
d = m_browser->m_attributeDoubleManager->value(m_prop);
}
/// Set int attribute
void apply(int& i)const
{
i = m_browser->m_attributeIntManager->value(m_prop);
}
/// Set bool attribute
void apply(bool& b)const
{
b = m_browser->m_attributeBoolManager->value(m_prop);
}
/// Set vector attribute
void apply(std::vector<double>& v)const
{
//throw std::runtime_error("Vector setter not implemented.");
QList<QtProperty*> members = m_prop->subProperties();
if ( members.empty() ) throw std::runtime_error("FunctionBrowser: empty vector attribute group.");
int n = members.size() - 1;
if ( n == 0 )
{
v.clear();
return;
}
v.resize( n );
for(int i = 0; i < n; ++i)
{
v[i] = m_browser->m_attributeVectorDoubleManager->value( members[i+1] );
}
}
private:
FunctionBrowser* m_browser;
QtProperty* m_prop;
};
/**
* Add a attribute property
* @param parent :: Parent function property
* @param attName :: Attribute name
* @param att :: Attribute value
*/
FunctionBrowser::AProperty FunctionBrowser::addAttributeProperty(QtProperty* parent, QString attName, const Mantid::API::IFunction::Attribute& att)
{
CreateAttributePropertyForFunctionBrowser cap(this,parent,attName);
return att.apply(cap);
}
/**
* Add attribute and parameter properties to a function property. For a composite function
* adds all member functions' properties
* @param prop :: A function property
* @param fun :: Shared pointer to a created function
*/
void FunctionBrowser::addAttributeAndParameterProperties(QtProperty* prop, Mantid::API::IFunction_sptr fun)
{
// add the function index property
addIndexProperty(prop);
// add attribute properties
auto attributeNames = fun->getAttributeNames();
for(auto att = attributeNames.begin(); att != attributeNames.end(); ++att)
{
QString attName = QString::fromStdString(*att);
addAttributeProperty(prop, attName, fun->getAttribute(*att));
}
auto cf = boost::dynamic_pointer_cast<Mantid::API::CompositeFunction>(fun);
if (cf)
{// if composite add members
for(size_t i = 0; i < cf->nFunctions(); ++i)
{
AProperty ap = addFunctionProperty(prop,QString::fromStdString(cf->getFunction(i)->name()));
addAttributeAndParameterProperties(ap.prop,cf->getFunction(i));
}
}
else
{// if simple add parameters
for(size_t i = 0; i < fun->nParams(); ++i)
{
QString name = QString::fromStdString(fun->parameterName(i));
double value = fun->getParameter(i);
AProperty ap = addParameterProperty(prop, name, value);
// if parameter has a tie
if (fun->isFixed(i))
{
auto tie = fun->getTie(i);
if (tie)
{
addTieProperty(ap.prop, QString::fromStdString(tie->asString()));
}
}
auto c = fun->getConstraint(i);
if ( c )
{
addConstraintProperties( ap.prop, QString::fromStdString( c->asString() ) );
}
}
}
}
/**
* Add property showing function's index in the composite function
* @param prop :: A function property
* @return :: AProperty struct for added property. If all fields are NULL - property wasn't added
* because it is the top function
*/
FunctionBrowser::AProperty FunctionBrowser::addIndexProperty(QtProperty* prop)
{
AProperty ap;
ap.item = NULL;
ap.parent = NULL;
ap.prop = NULL;
if (!prop) return ap;
if (!isFunction(prop)) return ap;
if ( !m_properties[prop].parent ) return ap;
QString index = "fff";
QtProperty* ip = m_indexManager->addProperty("Index");
ip->setEnabled(false);
m_indexManager->setValue(ip,index);
return addProperty(prop,ip);
}
/**
* Update function index properties
* @param prop :: A function property
* @param index :: The parent function's index
*/
void FunctionBrowser::updateFunctionIndices(QtProperty* prop, QString index)
{
if ( prop == NULL )
{
auto top = m_browser->properties();
if (top.isEmpty()) return;
prop = top[0];
}
auto children = prop->subProperties();
size_t i = 0;
foreach(QtProperty* child, children)
{
if (isFunction(child))
{
updateFunctionIndices(child,index + "f" + QString::number(i) + ".");
++i;
}
else if (isIndex(child))
{
m_indexManager->setValue(child,index);
}
}
}
/**
* Get property of the overall function.
*/
FunctionBrowser::AProperty FunctionBrowser::getFunctionProperty()
{
auto props = m_browser->properties();
if ( props.isEmpty() )
{
AProperty ap;
ap.item = NULL;
ap.parent = NULL;
ap.prop = NULL;
return ap;
}
QtProperty* prop = props[0];
return m_properties[prop];
}
/**
* Check if property is a function group
* @param prop :: Property to check
*/
bool FunctionBrowser::isFunction(QtProperty* prop) const
{
return prop && dynamic_cast<QtAbstractPropertyManager*>(m_functionManager) == prop->propertyManager();
}
/**
* Check if property is a function attribute
* @param prop :: Property to check
*/
bool FunctionBrowser::isStringAttribute(QtProperty* prop) const
{
return prop && (
dynamic_cast<QtAbstractPropertyManager*>(m_attributeStringManager) == prop->propertyManager() ||
dynamic_cast<QtAbstractPropertyManager*>(m_formulaManager) == prop->propertyManager()
);
}
/**
* Check if property is a function attribute
* @param prop :: Property to check
*/
bool FunctionBrowser::isDoubleAttribute(QtProperty* prop) const
{
return prop && dynamic_cast<QtAbstractPropertyManager*>(m_attributeDoubleManager) == prop->propertyManager();
}
/**
* Check if property is a function attribute
* @param prop :: Property to check
*/
bool FunctionBrowser::isIntAttribute(QtProperty* prop) const
{
return prop && dynamic_cast<QtAbstractPropertyManager*>(m_attributeIntManager) == prop->propertyManager();
}
/**
* Check if property is a function bool attribute
* @param prop :: Property to check
*/
bool FunctionBrowser::isBoolAttribute(QtProperty *prop) const
{
return prop && dynamic_cast<QtAbstractPropertyManager*>(m_attributeBoolManager) == prop->propertyManager();
}
/**
* Check if property is a function vector attribute
* @param prop :: Property to check
*/
bool FunctionBrowser::isVectorAttribute(QtProperty *prop) const
{
return prop && dynamic_cast<QtAbstractPropertyManager*>(m_attributeVectorManager) == prop->propertyManager();
}
/**
* Check if property is a function attribute
* @param prop :: Property to check
*/
bool FunctionBrowser::isAttribute(QtProperty* prop) const
{
return isStringAttribute(prop) || isDoubleAttribute(prop) || isIntAttribute(prop) ||
isBoolAttribute(prop) || isVectorAttribute(prop);
}
/**
* Check if property is a function parameter
* @param prop :: Property to check
*/
bool FunctionBrowser::isParameter(QtProperty* prop) const
{
return prop && dynamic_cast<QtAbstractPropertyManager*>(m_parameterManager) == prop->propertyManager();
}
/**
* Get parameter value as a string
* @param prop :: A parameter property
*/
double FunctionBrowser::getParameter(QtProperty* prop) const
{
return m_parameterManager->value(prop);
}
/**
* Check if a property is an index
* @param prop :: A property
*/
bool FunctionBrowser::isIndex(QtProperty* prop) const
{
return prop && dynamic_cast<QtAbstractPropertyManager*>(m_indexManager) == prop->propertyManager();
}
/**
* Get the function index for a property
* @param prop :: A property
*/
QString FunctionBrowser::getIndex(QtProperty* prop) const
{
if ( !prop ) return "";
if (isFunction(prop))
{
auto props = prop->subProperties();
if (props.isEmpty()) return "";
for(auto it = props.begin(); it != props.end(); ++it)
{
if ( isIndex(*it) )
{
return m_indexManager->value(*it);
}
}
return "";
}
auto ap = m_properties[prop];
return getIndex(ap.parent);
}
/**
* Add a tie property
* @param prop :: Parent parameter property
* @param tie :: A tie string
*/
FunctionBrowser::AProperty FunctionBrowser::addTieProperty(QtProperty* prop, QString tie)
{
if ( !prop )
{
throw std::runtime_error("FunctionBrowser: null property pointer");
}
AProperty ap;
ap.item = NULL;
ap.prop = NULL;
ap.parent = NULL;
if ( !isParameter(prop) ) return ap;
Mantid::API::Expression expr;
expr.parse(tie.toStdString());
// Do parameter names include composite function index
bool isComposite = false;
auto vars = expr.getVariables();
for(auto var = vars.begin(); var != vars.end(); ++var)
{
// nesting level of a particular variable
int n = static_cast<int>(std::count(var->begin(),var->end(),'.'));
if ( n != 0 )
{
isComposite = true;
}
}
// check that tie has form <paramName>=<expression>
if ( expr.name() != "=" )
{// prepend "<paramName>="
if ( !isComposite )
{
tie.prepend(prop->propertyName() + "=");
}
else
{
QString index = getIndex(prop);
tie.prepend(index + prop->propertyName() + "=");
}
}
// find the property of the function
QtProperty *funProp = isComposite? getFunctionProperty().prop : m_properties[prop].parent;
QtProperty* tieProp = m_tieManager->addProperty("Tie");
m_tieManager->setValue(tieProp, tie);
ap = addProperty(prop,tieProp);
ATie atie;
atie.paramProp = prop;
atie.tieProp = tieProp;
//atie.tie = tie;
m_ties.insert(funProp,atie);
return ap;
}
/**
* Check if a parameter property has a tie
* @param prop :: A parameter property
*/
bool FunctionBrowser::hasTie(QtProperty* prop) const
{
if ( !prop ) return false;
auto children = prop->subProperties();
foreach(QtProperty* child, children)
{
if ( child->propertyName() == "Tie" )
{
return true;
}
}
return false;
}
/**
* Check if a property is a tie
* @param prop :: A property
*/
bool FunctionBrowser::isTie(QtProperty* prop) const
{
return prop && dynamic_cast<QtAbstractPropertyManager*>(m_tieManager) == prop->propertyManager();
}
/**
* Get a tie for a parameter
* @param prop :: A parameter property
*/
std::string FunctionBrowser::getTie(QtProperty* prop) const
{
if ( !prop ) return "";
auto children = prop->subProperties();
foreach(QtProperty* child, children)
{
if ( child->propertyName() == "Tie" )
{
return m_tieManager->value(child).toStdString();
}
}
return "";
}
/**
* Add a constraint property
* @param prop :: Parent parameter property
* @param constraint :: A constraint string
*/
QList<FunctionBrowser::AProperty> FunctionBrowser::addConstraintProperties(QtProperty* prop, QString constraint)
{
if ( !isParameter(prop) ) return QList<FunctionBrowser::AProperty>();
QString lowerBoundStr = "";
QString upperBoundStr = "";
Mantid::API::Expression expr;
expr.parse(constraint.toStdString());
if ( expr.name() != "==" ) return QList<FunctionBrowser::AProperty>();
if ( expr.size() == 3 )
{// lower < param < upper
try
{
// check that the first and third terms are numbers
double d1 = boost::lexical_cast<double>( expr[0].name() ); (void)d1;
double d2 = boost::lexical_cast<double>( expr[2].name() ); (void)d2;
if ( expr[1].operator_name() == "<" && expr[2].operator_name() == "<" )
{
lowerBoundStr = QString::fromStdString( expr[0].name() );
upperBoundStr = QString::fromStdString( expr[2].name() );
}
else // assume that the operators are ">"
{
lowerBoundStr = QString::fromStdString( expr[2].name() );
upperBoundStr = QString::fromStdString( expr[0].name() );
}
}
catch(...)
{// error in constraint
return QList<FunctionBrowser::AProperty>();
}
}
else if ( expr.size() == 2 )
{// lower < param or param > lower etc
size_t paramPos = 0;