00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <QtGui/QVBoxLayout>
00015 #include <QtGui/QHBoxLayout>
00016 #include <QtGui/QToolButton>
00017 #include <QtGui/QPushButton>
00018 #include <QtGui/QFormLayout>
00019 #include <QtGui/QDialogButtonBox>
00020 #include <QtGui/QColorDialog>
00021 #include <QtGui/QLabel>
00022 #include "SeExprEditor.h"
00023 #include "SeExprEdHighlighter.h"
00024 #include "SeExprEdCompletionModel.h"
00025 #include "SeExprEdCurve.h"
00026 #include "SeExprEdColorCurve.h"
00027 #include "SeExprEdControl.h"
00028 #include "SeExprEdControlCollection.h"
00029 #include "SeExprEdEditableExpression.h"
00030 #include "SeExprEdEditable.h"
00031
00032
00033 SeExprEdControlCollection::SeExprEdControlCollection(QWidget* parent,bool showAddButton)
00034 :QWidget(parent),count(0),showAddButton(showAddButton),editableExpression(0)
00035 {
00036 controlLayout=new QVBoxLayout();
00037 controlLayout->setMargin(0);
00038 controlLayout->setSpacing(0);
00039 controlLayout->insertStretch(-1,100);
00040
00041 if(showAddButton){
00042 QPushButton* button=new QPushButton("Add Widget");
00043 button->setFocusPolicy(Qt::NoFocus);
00044 QHBoxLayout* buttonLayout=new QHBoxLayout();
00045 buttonLayout->insertStretch(-1,100);
00046 buttonLayout->addWidget(button,0);
00047 controlLayout->addLayout(buttonLayout);
00048 connect(button, SIGNAL(clicked()), SLOT(addControlDialog()));
00049 }
00050 setLayout(controlLayout);
00051 }
00052
00053 SeExprEdControlCollection::~SeExprEdControlCollection()
00054 {
00055 delete editableExpression;
00056 }
00057
00058
00059 SeExprEdAddDialog::SeExprEdAddDialog(int& count,QWidget* parent)
00060 :QDialog(parent)
00061 {
00062 QVBoxLayout *verticalLayout;
00063 verticalLayout = new QVBoxLayout();
00064 verticalLayout->setSpacing(3);
00065 verticalLayout->setMargin(3);
00066 setLayout(verticalLayout);
00067 QHBoxLayout *horizontalLayout = new QHBoxLayout();
00068
00069 horizontalLayout->addWidget(new QLabel("Variable"));
00070
00071 variableName = new QLineEdit(QString("$var%1").arg(count++));
00072
00073 horizontalLayout->addWidget(variableName);
00074 verticalLayout->addLayout(horizontalLayout);
00075
00076 tabWidget = new QTabWidget();
00077
00078
00079
00080 {
00081 QWidget* curveTab = new QWidget();
00082 QFormLayout* curveLayout = new QFormLayout(curveTab);
00083 curveLayout->setWidget(0,QFormLayout::LabelRole,new QLabel("Lookup"));
00084 curveLookup=new QLineEdit("$u");
00085 curveLayout->setWidget(0,QFormLayout::FieldRole,curveLookup);
00086 tabWidget->addTab(curveTab, QString("Curve"));
00087 }
00088
00089
00090 {
00091 QWidget* colorCurveTab = new QWidget();
00092 QFormLayout* colorCurveLayout = new QFormLayout(colorCurveTab);
00093 colorCurveLayout->setWidget(0,QFormLayout::LabelRole,new QLabel("Lookup"));
00094 colorCurveLookup=new QLineEdit("$u");
00095 colorCurveLayout->setWidget(0,QFormLayout::FieldRole,colorCurveLookup);
00096 tabWidget->addTab(colorCurveTab, QString("Color Curve"));
00097 }
00098
00099
00100 {
00101 QWidget* curveTab = new QWidget();
00102 QFormLayout* curveLayout = new QFormLayout(curveTab);
00103 curveLayout->setWidget(0,QFormLayout::LabelRole,new QLabel("Lookup"));
00104 curveLayout->setWidget(1,QFormLayout::LabelRole,new QLabel("Link"));
00105 animCurveLookup=new QLineEdit("$frame");
00106 animCurveLink=new QLineEdit("");
00107 curveLayout->setWidget(0,QFormLayout::FieldRole,animCurveLookup);
00108 curveLayout->setWidget(1,QFormLayout::FieldRole,animCurveLink);
00109 tabWidget->addTab(curveTab, QString("AnimCurve"));
00110 }
00111
00112
00113 {
00114 QWidget* intTab = new QWidget();
00115 QFormLayout* intFormLayout = new QFormLayout(intTab);
00116 intFormLayout->setWidget(0, QFormLayout::LabelRole, new QLabel("Default"));
00117 intFormLayout->setWidget(1, QFormLayout::LabelRole, new QLabel("Min"));
00118 intFormLayout->setWidget(2, QFormLayout::LabelRole, new QLabel("Max"));
00119 intDefault = new QLineEdit("0");
00120 intFormLayout->setWidget(0, QFormLayout::FieldRole, intDefault);
00121 intMin = new QLineEdit("0");
00122 intFormLayout->setWidget(1, QFormLayout::FieldRole, intMin);
00123 intMax = new QLineEdit("10");
00124 intFormLayout->setWidget(2, QFormLayout::FieldRole, intMax);
00125 tabWidget->addTab(intTab, QString("Int"));
00126 }
00127
00128
00129 {
00130 QWidget* floatTab = new QWidget();
00131 QFormLayout* floatFormLayout = new QFormLayout(floatTab);
00132 floatFormLayout->setWidget(0, QFormLayout::LabelRole, new QLabel("Default"));
00133 floatFormLayout->setWidget(1, QFormLayout::LabelRole, new QLabel("Min"));
00134 floatFormLayout->setWidget(2, QFormLayout::LabelRole, new QLabel("Max"));
00135 floatDefault = new QLineEdit("0");
00136 floatFormLayout->setWidget(0, QFormLayout::FieldRole, floatDefault);
00137 floatMin = new QLineEdit("0");
00138 floatFormLayout->setWidget(1, QFormLayout::FieldRole, floatMin);
00139 floatMax = new QLineEdit("1");
00140 floatFormLayout->setWidget(2, QFormLayout::FieldRole, floatMax);
00141
00142 tabWidget->addTab(floatTab, QString("Float"));
00143 }
00144
00145
00146 {
00147 QWidget* vectorTab = new QWidget();
00148 QFormLayout* vectorFormLayout = new QFormLayout(vectorTab);
00149 vectorFormLayout->setWidget(0, QFormLayout::LabelRole, new QLabel("Default"));
00150 vectorFormLayout->setWidget(1, QFormLayout::LabelRole, new QLabel("Min"));
00151 vectorFormLayout->setWidget(2, QFormLayout::LabelRole, new QLabel("Max"));
00152 vectorDefault0 = new QLineEdit("0");
00153 vectorDefault1 = new QLineEdit("0");
00154 vectorDefault2 = new QLineEdit("0");
00155 QHBoxLayout* compLayout=new QHBoxLayout();
00156 compLayout->addWidget(vectorDefault0);
00157 compLayout->addWidget(vectorDefault1);
00158 compLayout->addWidget(vectorDefault2);
00159 vectorFormLayout->setLayout(0, QFormLayout::FieldRole, compLayout);
00160 vectorMin = new QLineEdit("0");
00161 vectorFormLayout->setWidget(1, QFormLayout::FieldRole, vectorMin);
00162 vectorMax = new QLineEdit("1");
00163 vectorFormLayout->setWidget(2, QFormLayout::FieldRole, vectorMax);
00164
00165 tabWidget->addTab(vectorTab, QString("Vector"));
00166 }
00167
00168
00169 {
00170 QWidget* colorTab = new QWidget();
00171 QFormLayout* colorLayout = new QFormLayout(colorTab);
00172 colorWidget=new QPushButton();
00173 colorWidget->setFixedWidth(30);
00174 colorWidget->setFixedWidth(30);
00175 colorLayout->setWidget(0,QFormLayout::LabelRole,new QLabel("Color"));
00176 colorLayout->setWidget(0,QFormLayout::FieldRole,colorWidget);
00177 color=Qt::red;
00178 QPixmap colorPix(30,30);
00179 colorPix.fill(color);
00180 colorWidget->setIcon(QIcon(colorPix));
00181 tabWidget->addTab(colorTab, QString("Color"));
00182
00183 connect(colorWidget,SIGNAL(clicked()),this,SLOT(colorChooseClicked()));
00184 }
00185
00186
00187 {
00188 QWidget* stringTab = new QWidget();
00189 QFormLayout* stringLayout = new QFormLayout(stringTab);
00190 stringTypeWidget=new QComboBox();
00191 stringTypeWidget->addItem("string");
00192 stringTypeWidget->addItem("file");
00193 stringTypeWidget->addItem("directory");
00194 stringDefaultWidget=new QLineEdit();
00195 stringNameWidget=new QLineEdit("str1");
00196
00197 stringLayout->setWidget(0,QFormLayout::LabelRole,new QLabel("String Name"));
00198 stringLayout->setWidget(0,QFormLayout::FieldRole,stringNameWidget);
00199 stringLayout->setWidget(1,QFormLayout::LabelRole,new QLabel("String Type"));
00200 stringLayout->setWidget(1,QFormLayout::FieldRole,stringTypeWidget);
00201 stringLayout->setWidget(2,QFormLayout::LabelRole,new QLabel("String Default"));
00202 stringLayout->setWidget(3,QFormLayout::FieldRole,stringDefaultWidget);
00203
00204 tabWidget->addTab(stringTab, QString("String"));
00205 }
00206
00207 verticalLayout->addWidget(tabWidget);
00208
00209 QDialogButtonBox* buttonBox = new QDialogButtonBox();
00210 buttonBox->setOrientation(Qt::Horizontal);
00211 buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
00212
00213 verticalLayout->addWidget(buttonBox);
00214
00215
00216 QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
00217 QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
00218
00219 tabWidget->setCurrentIndex(0);
00220
00221 }
00222
00223 void SeExprEdAddDialog::colorChooseClicked()
00224 {
00225 color = QColorDialog::getColor(color);
00226 if(color.isValid())
00227 {
00228 QPixmap colorPix(30,30);
00229 colorPix.fill(color);
00230 colorWidget->setIcon(QIcon(colorPix));
00231 }
00232 }
00233
00234 void SeExprEdControlCollection::addControlDialog()
00235 {
00236 SeExprEdAddDialog* dialog=new SeExprEdAddDialog(count,this);
00237 if(dialog->exec()){
00238 QString s;
00239 switch(dialog->tabWidget->currentIndex()){
00240 case 0:
00241 s=QString("%1 = curve(%2,0,0,4,1,1,4);\n")
00242 .arg(dialog->variableName->text())
00243 .arg(dialog->curveLookup->text());
00244 break;
00245 case 1:
00246 s=QString("%1 = ccurve(%2,0,[0,0,0],4,1,[1,1,1],4);\n")
00247 .arg(dialog->variableName->text())
00248 .arg(dialog->colorCurveLookup->text());
00249 break;
00250 case 2:
00251 s=QString("%1 = animCurve(%2,\"constant\",\"constant\",0,\"%3\");")
00252 .arg(dialog->variableName->text())
00253 .arg(dialog->animCurveLookup->text())
00254 .arg(dialog->animCurveLink->text());
00255 break;
00256 case 3:
00257 s=dialog->variableName->text()+" = "+dialog->intDefault->text()
00258 +"; # "+dialog->intMin->text()+","+dialog->intMax->text()+"\n";
00259 break;
00260 case 4:
00261 s=QString("%1 = %2; # %3, %4\n").arg(dialog->variableName->text())
00262 .arg(dialog->floatDefault->text())
00263 .arg(atof(dialog->floatMin->text().toStdString().c_str()),0,'f',3)
00264 .arg(atof(dialog->floatMax->text().toStdString().c_str()),0,'f',3);
00265 break;
00266 case 5:
00267 s=QString("%1 = [%2,%3,%4]; # %5, %6\n").arg(dialog->variableName->text())
00268 .arg(dialog->vectorDefault0->text())
00269 .arg(dialog->vectorDefault1->text())
00270 .arg(dialog->vectorDefault2->text())
00271 .arg(atof(dialog->vectorMin->text().toStdString().c_str()),0,'f',3)
00272 .arg(atof(dialog->vectorMax->text().toStdString().c_str()),0,'f',3);
00273 break;
00274 case 6:
00275 s=QString("%1 = [%2,%3,%4];\n")
00276 .arg(dialog->variableName->text())
00277 .arg(dialog->color.redF())
00278 .arg(dialog->color.greenF())
00279 .arg(dialog->color.blueF());
00280 break;
00281 case 7:
00282 s=QString("\"%1\" #%2 %3\n")
00283 .arg(dialog->stringDefaultWidget->text())
00284 .arg(dialog->stringTypeWidget->currentText())
00285 .arg(dialog->stringNameWidget->text());
00286 break;
00287 }
00288 emit insertString(s.toStdString());
00289 }
00290 }
00291
00292 bool SeExprEdControlCollection::
00293 rebuildControls(const QString& expressionText,std::vector<QString>& variables)
00294 {
00295
00296 SeExprEdEditableExpression* newEditable=new SeExprEdEditableExpression;
00297 newEditable->setExpr(expressionText.toStdString());
00298
00299
00300
00301 bool newVariables=true;
00302 if(editableExpression && editableExpression->getVariables()==newEditable->getVariables()) newVariables=false;
00303 if(newVariables){
00304 const std::vector<std::string>& vars=newEditable->getVariables();
00305 variables.clear();
00306 for(size_t k=0;k<vars.size();k++){
00307 variables.push_back(("$"+vars[k]).c_str());
00308 }
00309 }
00310
00311 if (newEditable->size()==0 && !editableExpression) return false;
00312
00313 if(editableExpression && editableExpression->controlsMatch(*newEditable)){
00314
00315 editableExpression->updateString(*newEditable);
00316 delete newEditable;
00317 }else{
00318
00319
00320
00321 for (unsigned int i = 0; i < _controls.size(); i++){
00322 controlLayout->removeWidget(_controls[i]);
00323 delete _controls[i];
00324 }
00325 _linkedId=-1;
00326 _controls.clear();
00327
00328
00329 delete editableExpression;
00330 editableExpression=newEditable;
00331
00332
00333 for(size_t i=0;i<editableExpression->size();i++){
00334 SeExprEdEditable* editable=(*editableExpression)[i];
00335 SeExprEdControl* widget=0;
00336
00337
00338 if(SeExprEdNumberEditable* x=dynamic_cast<SeExprEdNumberEditable*>(editable)) widget=new SeExprEdNumberControl(i,x);
00339 else if(SeExprEdVectorEditable* x=dynamic_cast<SeExprEdVectorEditable*>(editable)) widget=new SeExprEdVectorControl(i,x);
00340 else if(SeExprEdStringEditable* x=dynamic_cast<SeExprEdStringEditable*>(editable)) widget=new SeExprEdStringControl(i,x);
00341 else if(SeExprEdCurveEditable* x=dynamic_cast<SeExprEdCurveEditable*>(editable)) widget=new SeExprEdCurveControl(i,x);
00342 else if(SeExprEdColorCurveEditable* x=dynamic_cast<SeExprEdColorCurveEditable*>(editable)) widget=new SeExprEdCCurveControl(i,x);
00343 else if(SeExprEdAnimCurveEditable* x=dynamic_cast<SeExprEdAnimCurveEditable*>(editable)){
00344 widget=new SeExprEdAnimCurveControl(i,x);
00345 }
00346 else{
00347 std::cerr<<"SeExpr editor logic error, cannot find a widget for the given editable"<<std::endl;
00348 }
00349 if(widget){
00350
00351 int insertPoint=controlLayout->count()-1;
00352 if(showAddButton) insertPoint--;
00353 controlLayout->insertWidget(insertPoint,widget);
00354 _controls.push_back(widget);
00355 connect(widget, SIGNAL(controlChanged(int)), SLOT(singleControlChanged(int)));
00356 connect(widget, SIGNAL(linkColorEdited(int,QColor)), SLOT(linkColorEdited(int,QColor)));
00357 connect(widget, SIGNAL(linkColorLink(int)), SLOT(linkColorLink(int)));
00358 }else{
00359 std::cerr<<"Expr Editor Logic ERROR did not make widget"<<std::endl;
00360 }
00361 }
00362 }
00363 return newVariables;
00364 }
00365
00366 void SeExprEdControlCollection::showEditor(int idx)
00367 {
00368 if (idx < 0 || idx >= (int)_controls.size())
00369 return;
00370
00371
00372
00373 SeExprEdAnimCurveControl *control = dynamic_cast<SeExprEdAnimCurveControl *>(_controls[idx]);
00374 if (!control)
00375 return;
00376
00377 control->editGraphClicked();
00378 }
00379
00380 void SeExprEdControlCollection::
00381 linkColorLink(int id)
00382 {
00383 _linkedId=id;
00384 for(unsigned int i=0;i<_controls.size();i++){
00385 _controls[i]->linkDisconnect(_linkedId);
00386 }
00387 }
00388
00389 void SeExprEdControlCollection::
00390 linkColorEdited(int id,QColor color)
00391 {
00392 if(id==_linkedId)
00393 emit linkColorOutput(color);
00394 }
00395
00396
00397 void SeExprEdControlCollection::
00398 linkColorInput(QColor color)
00399 {
00400
00401 if(_linkedId<0 || _linkedId>=(int)_controls.size()) return;
00402 _controls[_linkedId]->setColor(color);
00403 }
00404
00405 void SeExprEdControlCollection::
00406 updateText(const int id,QString& text)
00407 {
00408 Q_UNUSED(id);
00409 if(editableExpression)
00410 text=QString(editableExpression->getEditedExpr().c_str());
00411 }
00412
00413 void SeExprEdControlCollection::
00414 singleControlChanged(int id)
00415 {
00416 emit controlChanged(id);
00417 }
00418