PropertyHandler.h 6.58 KB
Newer Older
Peterson, Peter's avatar
Peterson, Peter committed
1
2
3
4
#ifndef PROPERTY_HANDLER_H
#define PROPERTY_HANDLER_H

#include "MantidAPI/IFitFunction.h"
5
#include "WidgetDllOption.h"
Peterson, Peter's avatar
Peterson, Peter committed
6
7
8
9
10
11
12
13
14
15

/* Forward declarations */
namespace Mantid
{
  namespace API
  {
    class IPeakFunction;
    class CompositeFunction;
  }
}
16
17
18
19

//class FunctionCurve;
//class PlotCurve;
//class Graph;
Peterson, Peter's avatar
Peterson, Peter committed
20
21
22
23
24
25
26
27
28
class QtBrowserItem;
class QtProperty;

/* Qt includes */
#include <QString>
#include <QList>
#include <QMap>
#include <QObject>

29
30
31
32
33
34
namespace MantidQt
{
namespace MantidWidgets
{
class FitPropertyBrowser;

Peterson, Peter's avatar
Peterson, Peter committed
35
36
37
/**
 * Helps display and edit functions in FitPropertyBrowser
 */
38
class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS PropertyHandler:public QObject, public Mantid::API::FunctionHandler
Peterson, Peter's avatar
Peterson, Peter committed
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
{
  Q_OBJECT
public:
  // Constructor
  PropertyHandler(Mantid::API::IFitFunction* fun,
                  Mantid::API::CompositeFunction* parent,
                  FitPropertyBrowser* browser,
                  QtBrowserItem* item = NULL);

  /// Destructor
  ~PropertyHandler();

  /// overrides virtual init() which is called from IFitFunction::setHandler(...)
  void init();

  PropertyHandler* addFunction(const std::string& fnName);
  // Removes handled function from its parent function and 
  // properties from the browser
  void removeFunction();

  void renameChildren()const;

  /// Creates name for this function to be displayed
  /// in the browser
  QString functionName()const;

  QString functionPrefix()const;

  // Return composite function
  Mantid::API::CompositeFunction* cfun()const{return m_cf;}
  // Return peak function
  Mantid::API::IPeakFunction* pfun()const{return m_pf;}
71
  // Return IFitFunction
72
  Mantid::API::IFitFunction* ifun()const{return m_fun;}
Peterson, Peter's avatar
Peterson, Peter committed
73
74
75
76
77
  // Return the browser item
  QtBrowserItem* item()const{return m_item;}
  // Return the parent handler
  PropertyHandler* parentHandler()const;
  // Return the child's handler
Peterson, Peter's avatar
Peterson, Peter committed
78
  PropertyHandler* getHandler(std::size_t i)const;
Peterson, Peter's avatar
Peterson, Peter committed
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
  /** Returns 'this' if item == m_item and this is a composite function or
   * calls findCompositeFunction recursively with all its children or
   * zero
   */
  const Mantid::API::CompositeFunction* findCompositeFunction(QtBrowserItem* item)const;
  /** Returns 'this' if item == m_item or
   * calls findFunction recursively with all its children or
   * zero
   */
  const Mantid::API::IFitFunction* findFunction(QtBrowserItem* item)const;

  PropertyHandler* findHandler(QtProperty* prop);

  PropertyHandler* findHandler(const Mantid::API::IFitFunction* fun);

  /**
   * Set function parameter value read from a QtProperty
   * @param prop :: The (double) property with the new parameter value
   * @return true if successfull
   */
  bool setParameter(QtProperty* prop);

  // Check if it is a parameter property
  bool isParameter(QtProperty* prop);

  /**
   * Set function attribute value read from a QtProperty
   * @param prop :: The (string) property with the new attribute value
   * @return true if successfull
   */
  bool setAttribute(QtProperty* prop);

  /**
   * Set function's double attribute
   * @param attName :: The name of the attribute
   * @param attValue :: The new attribute value
   */
  void setAttribute(const QString& attName, const double& attValue);

  /**
   * Set function's attribute of any type.
   * @param attName :: The name of the attribute
   * @param attValue :: The new attribute value as a string. If the attValue's
   *  format doesn't match the attribute's type it is ignored.
   */
  void setAttribute(const QString& attName, const QString& attValue);

  /**
   * Update the parameter properties
   */
  void updateParameters();

  // Get property for function parameter parName
  QtProperty* getParameterProperty(const QString& parName)const;

  // Get parameter property which has the argument as a child (i.e. tie or conatraint)
  QtProperty* getParameterProperty(QtProperty* prop)const;

  /**
   * Change the type of the function (replace the function)
   * @param prop :: The "Type" property with new value
   */
  Mantid::API::IFitFunction* changeType(QtProperty* prop);

  void setHeight(const double& h);
  void setCentre(const double& c);
  void setWidth(const double& w);
  void setBase(const double& b){m_base = b;}
  void calcBase();//< caclulate baseline from workspace data
  void calcBaseAll();//< calc baseline for all peaks in the function

  double height()const;
  double centre()const;
  double width()const;
  double base()const{return m_base;}

  void addTie(const QString& tieStr);
  void fix(const QString& parName);
  void removeTie(QtProperty* prop);
  void removeTie(const QString& propName);

  void addConstraint(QtProperty* parProp,bool lo,bool up,double loBound,double upBound);
  void removeConstraint(QtProperty* parProp);

  // Return list of handlers of peak functions which can be used in PeakPickerTool
  // The item->pfun() will return a correct pointer to a peak
  // Non-const because it may return a non-const pointer to this.
  QList<PropertyHandler*> getPeakList();

  // Plot the function on a graph
169
  //void plot(Graph* g)const;
170
  bool& hasPlot() {return m_hasPlot;}
171
172
  //void replot()const;
  //void removePlot();
Peterson, Peter's avatar
Peterson, Peter committed
173
174
175
176
  void removeAllPlots();

  void fit();

177
178
179
180
181
  // update workspace property when workspaces added to or removed from ADS
  void updateWorkspaces(QStringList oldWorkspaces);
  // set workspace in workspace property to the function 
  void setFunctionWorkspace();

Peterson, Peter's avatar
Peterson, Peter committed
182
183
184
protected slots:

  // 
185
  void plotRemoved();
Peterson, Peter's avatar
Peterson, Peter committed
186

187
188
189
190
191
192
protected:

  void initAttributes();
  void initParameters();
  void initWorkspace();

Peterson, Peter's avatar
Peterson, Peter committed
193
194
195
196
private:
  FitPropertyBrowser* m_browser;
  Mantid::API::CompositeFunction* m_cf;//< if the function is composite holds pointer to it
  Mantid::API::IPeakFunction* m_pf;//< if the function is peak holds pointer to it
197
  //Mantid::API::IFitFunction* m_if;//< pointer to IFitFunction
198
  Mantid::API::CompositeFunction* m_parent; //< if the function has parent holds pointer to it
Peterson, Peter's avatar
Peterson, Peter committed
199
200
201
202
203
204
  QtProperty* m_type;
  QtBrowserItem* m_item;//< the browser item
  QList<QtProperty*> m_attributes; //< function attribute properties
  QList<QtProperty*> m_parameters; //< function parameter properties
  QMap<QString,QtProperty*> m_ties;//< tie properties
  QMap<QString,std::pair<QtProperty*,QtProperty*> > m_constraints;//< constraints
205
206
207
  bool m_isMultispectral; ///< true if fitting to multiple spectra using MultiBG function
  QtProperty* m_workspace; ///< workspace name for multispectral fitting
  QtProperty* m_workspaceIndex; ///< workspace index for multispectral fitting
Peterson, Peter's avatar
Peterson, Peter committed
208
209
  double m_base; //< the baseline for a peak
  int m_ci; //< approximate index in the workspace at the peak centre
210
  //mutable FunctionCurve* m_curve;//< the curve to plot the handled function
211
  mutable bool m_hasPlot;
Peterson, Peter's avatar
Peterson, Peter committed
212
213
};

214
215
216
} // MantidQt
} // API

Peterson, Peter's avatar
Peterson, Peter committed
217
#endif /* PROPERTY_HANDLER_H */