Newer
Older
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
#include "MantidQtMantidWidgets/UserFunctionDialog.h"
#include "MantidQtMantidWidgets/RenameParDialog.h"
#include "MantidAPI/Expression.h"
#include <QComboBox>
#include <QStringListModel>
#include <QDialogButtonBox>
#include <QMessageBox>
#include <algorithm>
using namespace MantidQt::MantidWidgets;
UserFunctionDialog::UserFunctionDialog(QWidget *parent,const QString& formula)
:QDialog(parent)
{
m_uiForm.setupUi(this);
connect(m_uiForm.lstCategory,SIGNAL(currentTextChanged(const QString&)),this,SLOT(selectCategory(const QString&)));
connect(m_uiForm.lstFunction,SIGNAL(currentTextChanged(const QString&)),this,SLOT(selectFunction(const QString&)));
connect(m_uiForm.btnSave,SIGNAL(clicked()),this,SLOT(saveFunction()));
connect(m_uiForm.btnAdd,SIGNAL(clicked()),this,SLOT(addExpression()));
connect(m_uiForm.btnUse,SIGNAL(clicked()),this,SLOT(accept()));
connect(m_uiForm.btnCancel,SIGNAL(clicked()),this,SLOT(reject()));
loadFunctions();
updateCategories();
if ( !formula.isEmpty() )
{
QRect rect = m_uiForm.teUserFunction->cursorRect();
QTextCursor cursor = m_uiForm.teUserFunction->cursorForPosition(rect.topLeft());
cursor.insertText(formula);
updateFunction();
}
}
/**
* Load saved functions form Mantid(.user).properties file.
* Property: userfunctions.CategoryName.FunctionName = Expression-in-Mu::Parser-format
*/
void UserFunctionDialog::loadFunctions()
{
// define the build-in functions
m_funs.insert("Base.abs","abs(x)");
m_funs.insert("Base.sin","sin(x)");
m_funs.insert("Base.cos","cos(x)");
//m_funs.insert("Base.","");
m_funs.insert("Built-in.Gauss","h*exp(-s*(x-c)^2)");
m_funs.insert("Built-in.ExpDecay","h*exp(-x/t)");
}
/**
* Update the GUI element displaying categories.
*/
void UserFunctionDialog::updateCategories()
{
QSet<QString> cats = names();
foreach(QString cat ,cats)
{
m_uiForm.lstCategory->addItem(cat);
}
}
/**
* Make a category current
* @param cat The category to select
*/
void UserFunctionDialog::selectCategory(const QString& cat)
{
//std::string category_key = "userfunctions."+cat.toStdString();
m_uiForm.lstFunction->clear();
QSet<QString> funs = names(cat);
foreach(QString fun ,funs)
{
QString key = cat + "." + fun;
QString value = m_funs[key];
if ( !value.isEmpty() )
{
m_uiForm.lstFunction->addItem(fun);
}
}
m_uiForm.lstFunction->setCurrentRow(0);
}
/**
* Make a function current
* @param fun The function to select
*/
void UserFunctionDialog::selectFunction(const QString& fun)
{
QString cat = m_uiForm.lstCategory->currentItem()->text();
m_uiForm.teExpression->clear();
QString fun_key = cat + "." + fun;
QString value = m_funs[fun_key];
QString comment = m_funs[fun_key + ".comment"];
if ( !comment.isEmpty() )
{
value += "\n\n" + comment;
}
m_uiForm.teExpression->setText(value);
}
/**
* Add selected expression to the user function
*/
void UserFunctionDialog::addExpression()
{
QString expr = m_uiForm.teExpression->toPlainText();
int iBr = expr.indexOf('\n');
if (iBr > 0)
{
expr.remove(iBr,expr.size());
}
checkParameters(expr);
QRect rect = m_uiForm.teUserFunction->cursorRect();
QTextCursor cursor = m_uiForm.teUserFunction->cursorForPosition(rect.topLeft());
if (cursor.position() > 0)
{
expr.prepend('+');
}
cursor.insertText(expr);
updateFunction();
}
/**
* Check an expression for name clashes with user function
* @param expr An expression prepared to be added to the user function.
*/
void UserFunctionDialog::checkParameters(QString& expr)
{
if (expr.isEmpty()) return;
QString fun = m_uiForm.teUserFunction->toPlainText();
if (fun.isEmpty()) return;
// collect parameter names in sets vars1 and vars2
Mantid::API::Expression e1;
e1.parse(fun.toStdString());
Mantid::API::Expression e2;
e2.parse(expr.toStdString());
std::set<std::string> vars1 = e1.getVariables();
std::set<std::string> vars2 = e2.getVariables();
vars1.erase("x");
vars2.erase("x");
// combine all names frm the two sets
std::vector<std::string> all(vars1.size()+vars2.size(),"");
std::set_union(vars1.begin(),vars1.end(),vars2.begin(),vars2.end(),all.begin());
std::vector<std::string>::iterator it = std::find(all.begin(),all.end(),"");
if (it != all.end())
{
all.erase(it,all.end());
}
// compare variable names and collect common names
std::vector<std::string> common(std::min(vars1.size(),vars2.size()),"");
std::set_intersection(vars1.begin(),vars1.end(),vars2.begin(),vars2.end(),common.begin());
// ask the user to rename the common names
if ( !common.empty() )
{
RenameParDialog dlg(all,common);
if (dlg.exec() == QDialog::Accepted)
{
std::set<std::string> vars2_new;
dlg.setOutput(vars2_new);
std::set<std::string>::const_iterator v2_old = vars2.begin();
std::set<std::string>::const_iterator v2_new = vars2_new.begin();
for(; v2_old != vars2.end(); ++v2_old,++v2_new)
{
e2.renameAll(*v2_old,*v2_new);
}
expr = QString::fromStdString(e2.str());
}
}
}
/**
* Updates the parameter list
*/
void UserFunctionDialog::updateFunction()
{
QString fun = m_uiForm.teUserFunction->toPlainText();
Mantid::API::Expression e;
e.parse(fun.toStdString());
std::set<std::string> vars = e.getVariables();
vars.erase("x");
QString params;
for(std::set<std::string>::iterator it=vars.begin();it!=vars.end();++it)
{
if (it != vars.begin())
{
params += ",";
}
params += QString::fromStdString(*it);
}
m_uiForm.leParams->setText(params);
}
/**
* Returns function names: If the input cat parameter is empty the returned set
* contains funtion categories, otherwise it returns function names in category cat.
* @param cat The category for which functions will be returned.
* @return A set of funtion names.
*/
QSet<QString> UserFunctionDialog::names(const QString& cat)const
{
QSet<QString> out;
if (cat.isEmpty())
{
QMap<QString,QString>::const_iterator it = m_funs.begin();
for(; it != m_funs.end(); ++it)
{
QStringList cn = it.key().split('.');
out.insert(cn[0]);
}
}
else
{
QMap<QString,QString>::const_iterator it = m_funs.begin();
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
for(; it != m_funs.end(); ++it)
{
QStringList cn = it.key().split('.');
if (cn[0] == cat)
{
out.insert(cn[1]);
}
}
}
return out;
}
/**
* Save the constructed function for future use
*/
void UserFunctionDialog::saveFunction()
{
QString cur_category = m_uiForm.lstCategory->currentItem()->text();
if (cur_category == "Base" || cur_category == "Built-in")
{
cur_category = "";
}
InputFunctionNameDialog* dlg = new InputFunctionNameDialog(this,cur_category);
if (dlg->exec() == QDialog::Accepted)
{
QString category;
QString name;
dlg->getFunctionName(category,name);
if (name.isEmpty())
{
QMessageBox::critical(this,"Mantid - Error","The function name is empty");
return;
}
// check if the category already exists
QList<QListWidgetItem*> items = m_uiForm.lstCategory->findItems(category,Qt::MatchExactly);
if ( !items.isEmpty() )
{// check if a function with this name already exists
const QSet<QString> functions = names(category);
QSet<QString>::const_iterator found = functions.find(name);
if (found != functions.end() &&
QMessageBox::question(this,"Mantid","A function with name "+name+" already exists in category "+category+".\n"
"Would you like to replace it?",QMessageBox::Yes|QMessageBox::No) == QMessageBox::No)
{
return;
}
}
QString fun = m_uiForm.teUserFunction->toPlainText();
QString fun_key = category+"."+name;
m_funs[fun_key] = fun;
updateCategories();
}//QDialog::Accepted
}
QStringList UserFunctionDialog::categories()const
{
QStringList out;
for(int i = 0; i < m_uiForm.lstCategory->count(); ++i)
{
out << m_uiForm.lstCategory->item(i)->text();
}
return out;
}
/**
* Constructor
* @param category The initial suggestion for the category
*/
InputFunctionNameDialog::InputFunctionNameDialog(QWidget *parent,const QString& category)
:QDialog(parent)
{
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(new QLabel("Enter new or select a category"));
m_category = new QComboBox();
m_category->addItems(((UserFunctionDialog*)parent)->categories());
m_category->setEditable(true);
int index = m_category->findText(category);
if (index >= 0)
{
m_category->setCurrentIndex(index);
}
layout->addWidget(m_category);
layout->addWidget(new QLabel("Enter a name for the new function"));
m_name = new QLineEdit();
layout->addWidget(m_name);
QDialogButtonBox* buttons = new QDialogButtonBox();
buttons->addButton("OK",QDialogButtonBox::AcceptRole);
buttons->addButton("Cancel",QDialogButtonBox::RejectRole);
buttons->setCenterButtons(true);
connect(buttons,SIGNAL(accepted()),this,SLOT(accept()));
connect(buttons,SIGNAL(rejected()),this,SLOT(reject()));
layout->addWidget(buttons);
setLayout(layout);
}
/**
* Return the entered category and function name
* @param category A string to recieve the category
* @param name A string to recieve the function name
*/
void InputFunctionNameDialog::getFunctionName(QString& category,QString& name)
{
category = m_category->currentText();
name = m_name->text();
}