21 #include <QtGui/QLineEdit>
22 #include <QtGui/QPushButton>
23 #include <QtCore/QRegExp>
24 #include <QtGui/QSplitter>
25 #include <QtGui/QLabel>
26 #include <QtGui/QMouseEvent>
27 #include <QtGui/QKeyEvent>
28 #include <QtGui/QHBoxLayout>
29 #include <QtGui/QVBoxLayout>
30 #include <QtGui/QPaintEvent>
31 #include <QtGui/QPainter>
32 #include <QtGui/QScrollArea>
33 #include <QtGui/QSpacerItem>
34 #include <QtGui/QSizePolicy>
35 #include <QtGui/QTextCharFormat>
36 #include <QtGui/QCompleter>
37 #include <QtGui/QAbstractItemView>
38 #include <QtGui/QStandardItemModel>
39 #include <QtGui/QStringListModel>
40 #include <QtGui/QScrollBar>
41 #include <QtGui/QToolTip>
42 #include <QtGui/QListWidget>
43 #include <QtGui/QTreeView>
44 #include <QtGui/QAction>
45 #include <QtGui/QMenu>
47 #include <SeExpr2/Expression.h>
48 #include <SeExpr2/ExprNode.h>
49 #include <SeExpr2/ExprFunc.h>
50 #include <SeExpr2/ExprBuiltins.h>
62 : QLineEdit(parent), _id(id), _signaling(0)
77 QString newText=
exprTe->toPlainText();
81 exprTe->insertPlainText(newText);
100 : QWidget(parent),_updatingText(0),errorHeight(0)
107 setWindowTitle(
"Expression Editor");
108 setMinimumHeight(100);
114 QVBoxLayout* exprAndErrors=
new QVBoxLayout;
115 exprAndErrors->setMargin(0);
116 setLayout(exprAndErrors);
120 exprTe->setMinimumHeight(50);
124 while (QFontMetrics(QFont(
"Liberation Sans",fontsize)).width(
"abcdef")<38 && fontsize<20)
126 while (QFontMetrics(QFont(
"Liberation Sans",fontsize)).width(
"abcdef")>44 && fontsize>3)
129 exprTe->setFont(QFont(
"Liberation Sans",fontsize));
131 exprAndErrors->addWidget(
exprTe);
135 errorWidget->setSelectionMode(QAbstractItemView::SingleSelection);
136 errorWidget->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::Maximum));
146 connect(controls,SIGNAL(insertString(
const std::string&)),SLOT(
insertStr(
const std::string&)));
155 int start=item->data(Qt::UserRole).toInt();
156 int end=item->data(Qt::UserRole+1).toInt();
157 QTextCursor cursor=
exprTe->textCursor();
158 cursor.movePosition(QTextCursor::Start,QTextCursor::MoveAnchor);
159 cursor.movePosition(QTextCursor::Right,QTextCursor::MoveAnchor,start);
160 cursor.movePosition(QTextCursor::Right,QTextCursor::KeepAnchor,end-start+1);
161 exprTe->setTextCursor(cursor);
200 :QTextEdit(parent),lastStyleForHighlighter(0),_tip(0)
208 QTreeView* treePopup=
new QTreeView;
210 treePopup->setRootIsDecorated(
false);
211 treePopup->setMinimumWidth(300);
212 treePopup->setMinimumHeight(50);
213 treePopup->setItemsExpandable(
true);
216 completer->setCompletionMode(QCompleter::PopupCompletion);
217 completer->setCaseSensitivity(Qt::CaseInsensitive);
218 QObject::connect(
completer,SIGNAL(activated(
const QString&)),
this,
229 QTextEdit::focusInEvent(e);
235 QTextEdit::focusInEvent(e);
241 QTextEdit::mousePressEvent(event);
247 QTextEdit::mouseDoubleClickEvent(event);
257 QTextEdit::paintEvent(event);
262 if(event->modifiers() == Qt::ControlModifier){
263 if(event->delta()>0) zoomIn();
264 else if(event->delta()<0) zoomOut();
266 return QTextEdit::wheelEvent(event);
272 if (e->key()==Qt::Key_Return && e->modifiers()==Qt::ControlModifier){
275 }
else if(e->key()==Qt::Key_F4){
283 case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
284 case Qt::Key_Tab:
case Qt::Key_Backtab:
292 bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E);
294 QTextEdit::keyPressEvent(e);
296 const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
297 if (!
completer || (ctrlOrShift && e->text().isEmpty()))
300 bool hasModifier=(e->modifiers()!=Qt::NoModifier) && !ctrlOrShift;
303 QTextCursor tc=textCursor();
304 tc.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
305 QString line=tc.selectedText();
308 static QRegExp completion(
"^(?:.*[^A-Za-z0-9_$])?((?:\\$[A-Za-z0-9_]*)|[A-Za-z]+[A-Za-z0-9_]*)$");
309 int index=completion.indexIn(line);
310 QString completionPrefix;
311 if(index != -1 && !line.contains(
'#')){
312 completionPrefix=completion.cap(1);
317 if(!isShortcut && (hasModifier || e->text().isEmpty() || completionPrefix.length()<1 || index==-1)){
322 if(completionPrefix!=
completer->completionPrefix()){
323 completer->setCompletionPrefix(completionPrefix);
328 QRect cr=cursorRect();
329 cr.setWidth(
completer->popup()->sizeHintForColumn(0)+
completer->popup()->sizeHintForColumn(1)
330 +
completer->popup()->verticalScrollBar()->sizeHint().width());
338 static QRegExp inFunction(
"^(?:.*[^A-Za-z0-9_$])?([A-Za-z0-9_]+)\\([^()]*$");
339 int index2=inFunction.indexIn(line);
341 QString functionName=inFunction.cap(1);
343 QString tip=
"<b>"+tips[0]+
"</b>";
344 for(
int i=1;i<tips.size();i++){
356 QMenu *menu = createStandardContextMenu();
358 if (!menu->actions().empty())
360 QAction*
f = menu->actions().first();
362 menu->insertSeparator(f);
365 menu->exec(event->globalPos());
372 if(
string==
"")
return;
376 QRect cr=cursorRect();
378 cr.setWidth(cr.width()*3);
380 _tip=
new ExprPopupDoc(
this,mapToGlobal(cr.bottomLeft())+QPoint(0,6),string);
391 QTextCursor tc = textCursor();
392 int extra = completion.length() -
completer->completionPrefix().length();
393 tc.movePosition(QTextCursor::Left);
394 tc.movePosition(QTextCursor::EndOfWord);
395 tc.insertText(completion.right(extra));
396 if(completion[0]!=
'$') tc.insertText(
"(");
402 return exprTe->toPlainText().toStdString();
409 exprTe->insertPlainText(QString::fromStdString(expression));
411 exprTe->moveCursor(QTextCursor::Start);
412 if(doApply) emit
apply();
417 exprTe->insertPlainText(QString::fromStdString(str));
422 exprTe->append(QString::fromStdString(str));
427 QListWidgetItem* item=
new QListWidgetItem((
"Error: "+error).c_str(),
errorWidget);
428 item->setData(Qt::UserRole,startPos);
429 item->setData(Qt::UserRole+1,endPos);
432 const char* c=error.c_str();
435 if(*c ==
'\n') lines++;
void insertCompletion(const QString &completion)
QListWidget * errorWidget
void addVariable(const QString &str, const QString &comment)
std::vector< QString > local_variables
void mousePressEvent(QMouseEvent *event)
ExprCompletionModel * completionModel
void addError(const int startPos, const int endPos, const std::string &error)
void showTip(const QString &string)
</pre >< h3 > Binding our variable reference</h3 > If we now tried to use the variable would still not be found by our expressions To make it bindable we need to override the resolveVar() function as follows return
with numParticles numAttributes A variable block contains variable names and types but doesn t care what the values are< pre > void f(const std::string &s, MyParticleData *p, int outputDim=3)
void addFunction(const QString &function, const QString &docString)
ExprControlCollection * controls
void wheelEvent(QWheelEvent *e)
void replaceExtras(const ExprCompletionModel &completer)
void mouseDoubleClickEvent(QMouseEvent *event)
For a multi line expression
QTimer * controlRebuildTimer
void controlChanged(int id)
void appendStr(const std::string &str)
The result is computed int int< br >< divstyle="margin-left:40px;"> Picks values randomly between loRange and hiRange based on supplied index(which is automatically hashed). 
bool rebuildControls(const QString &expressionText, std::vector< QString > &variables)
Rebuild the controls given the new expressionText. Return any local variables found.
virtual void keyPressEvent(QKeyEvent *e)
void paintEvent(QPaintEvent *e)
void updateText(const int id, QString &text)
Request new text, given taking into account control id's new values.
void focusOutEvent(QFocusEvent *e)
ExprLineEdit(int id, QWidget *parent)
ExprEditor(QWidget *parent, ExprControlCollection *controls)
void clearExtraCompleters()
ExprHighlighter * highlighter
void registerExtraVariable(const std::string &name, const std::string &docString)
ExprTextEdit(QWidget *parent=0)
void fixStyle(const QPalette &palette)
QStyle * lastStyleForHighlighter
void textChangedCB(const QString &text)
void textChanged(int id, const QString &text)
QAction * _popupEnabledAction
void syncExtras(const ExprCompletionModel &otherModel)
void insertStr(const std::string &str)
void contextMenuEvent(QContextMenuEvent *event)
void focusInEvent(QFocusEvent *e)
void registerExtraFunction(const std::string &name, const std::string &docString)
QString getDocString(const QString &s)
void setExpr(const std::string &expression, const bool apply=false)