00001 #include "constants.h"
00002
00003 #include "stateDialogs.h"
00004 #include "editor.h"
00005 #include "state.h"
00006 #include "transforms.h"
00007 #include "stringProcessor.h"
00008 #include "parser.h"
00009
00010 #include <QPushButton>
00011 #include <QLayout>
00012 #include <QLabel>
00013 #include <QLineEdit>
00014 #include <QCheckBox>
00015 #include <QComboBox>
00016 #include <QStringList>
00017 #include <QWidget>
00018 #include <QGroupBox>
00019 #include <QFontMetrics>
00020 #include <QString>
00021 #include <QMenu>
00022 #include <QColor>
00023 #include <QRegExpValidator>
00024 #include <QRegExp>
00025 #include <QMessageBox>
00026
00027 #include <QtDebug>
00028
00029
00030 NameAcceptDialog::NameAcceptDialog(QWidget *parent)
00031 : QDialog(parent), edtLabel(NULL), edtName(NULL)
00032 {
00033
00034 }
00035
00036
00037 void NameAcceptDialog::myAccept()
00038 {
00039 QString errorMsg;
00040 if (edtName && !Parser::checkNameCorrectness(edtName->text(), &errorMsg))
00041 {
00042 showMessage(errorMsg);
00043 return;
00044 }
00045
00046 try
00047 {
00048 StringProcessor::computeCharacterList(edtLabel->text());
00049 }
00050 catch(const StringProcessorBraceMatchException &e)
00051 {
00052 DBGLOG("StringProcessorException: " << e.what());
00053 showMessage("Brace matching failed. Don't use unbalanced braces ('{', '}')!");
00054 return;
00055 }
00056 catch(const StringProcessorException &e)
00057 {
00058 RELLOG("Unexpected StringProcessorException: " << e.what());
00059 showMessage("Unexpected error in StringProcessor\nMaybe brace matching failed. Don't use unbalanced braces ('{', '}')!");
00060 return;
00061 }
00062
00063 accept();
00064 }
00065
00066 void NameAcceptDialog::showMessage(const QString &msg)
00067 {
00068 QMessageBox::warning(this,"Incorrect syntax!", msg, tr("&Ok"));
00069 }
00070
00071
00072
00073
00074 StateDialog::StateDialog(Editor *editor, const QList<QString> &typeList, EMode mode, bool hasInitial, QWidget* parent)
00075 : NameAcceptDialog(parent), editor(editor)
00076 {
00077
00078 QLabel* lblLabel = new QLabel(QString("Label"), this);
00079 edtLabel = new QLineEdit(this);
00080 checkAutoName = new QCheckBox(QString("Automatically generated name"),this);
00081 QLabel* lblName = new QLabel(QString("Name"), this);
00082 edtName = new QLineEdit(this);
00083
00084 QLabel* lblType = new QLabel(QString("Type"), this);
00085 comboType = new QComboBox(this);
00086 foreach(QString typeName, typeList)
00087 {
00088 comboType->addItem(typeName);
00089 }
00090
00091 checkDimmed = new QCheckBox(QString("Dimmed"),this);
00092
00093 connect(checkAutoName,SIGNAL(clicked()),this, SLOT(changeAutoName()));
00094
00095 checkQuickInitial = new QCheckBox(QString("Quick mark Initial"), this);
00096 const QString tip = tr("Only allowed to add single Initial transition to state which has no initials yet.\nOnly \\InitialW can be added.");
00097 checkQuickInitial->setToolTip(tip);
00098 checkQuickInitial->setWhatsThis(tip);
00099 checkQuickInitial->setEnabled(!hasInitial);
00100 checkQuickInitial->setChecked(hasInitial);
00101
00102 QWidget *hBox = new QWidget(this);
00103 QPushButton *buttonOk = new QPushButton("Ok");
00104 QPushButton *buttonCancel = new QPushButton("Cancel");
00105 QHBoxLayout *hBoxLayout = new QHBoxLayout;
00106 hBoxLayout->addWidget(buttonOk);
00107 hBoxLayout->addWidget(buttonCancel);
00108 hBox->setLayout(hBoxLayout);
00109 connect(buttonOk, SIGNAL(clicked()), this, SLOT(testAccept()));
00110 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
00111
00112 QGridLayout* gridLayout = new QGridLayout(this);
00113
00114 gridLayout->addWidget(lblLabel, 0, 0);
00115 gridLayout->addWidget(edtLabel, 0, 1);
00116 gridLayout->addWidget(checkAutoName, 1, 0, 1, 2);
00117 gridLayout->addWidget(lblName, 2, 0);
00118 gridLayout->addWidget(edtName, 2, 1);
00119 gridLayout->addWidget(lblType, 3, 0);
00120 gridLayout->addWidget(comboType, 3, 1);
00121 gridLayout->addWidget(checkDimmed,4,0,1,2);
00122 gridLayout->addWidget(checkQuickInitial, 5, 0, 1, 2);
00123 gridLayout->addWidget(hBox, 6, 0, 1, 2);
00124
00125 QString s = "State dialog";
00126 if (mode == eAdd)
00127 s += " - add";
00128 else
00129 s += " - edit";
00130
00131 setWindowTitle(s);
00132 setWindowIcon(QIcon(":images/state.xpm"));
00133 }
00134
00135 bool StateDialog::quickInitial() const
00136 {
00137 return checkQuickInitial->isEnabled() ? checkQuickInitial->isChecked() : false;
00138 }
00139
00140 QString StateDialog::getName() const
00141 {
00142 return edtName->text();
00143 }
00144
00145 QString StateDialog::getLabel() const
00146 {
00147 return edtLabel->text();
00148 }
00149
00150 int StateDialog::getType() const
00151 {
00152 return comboType->currentIndex();
00153 }
00154
00155 bool StateDialog::isDimmed() const
00156 {
00157 return checkDimmed->isChecked();
00158 }
00159
00160 bool StateDialog::isAutoNammed() const
00161 {
00162 return checkAutoName->isChecked();
00163 }
00164
00165 void StateDialog::setName(const QString& name, bool editing)
00166 {
00167 edtName->setText(name);
00168 currentStateName = (editing) ? name : "";
00169 }
00170
00171 void StateDialog::setLabel(const QString& label)
00172 {
00173 edtLabel->setText(label);
00174 }
00175
00176 void StateDialog::setType(int type)
00177 {
00178 comboType->setCurrentIndex(type);
00179 }
00180
00181 void StateDialog::setDimmed(bool dim)
00182 {
00183 checkDimmed->setChecked(dim);
00184 }
00185
00186 void StateDialog::setAutoNammed(bool aN){
00187 edtName->setEnabled(!aN);
00188 checkAutoName->setChecked(aN);
00189 }
00190
00191
00192
00193
00194 void StateDialog::changeAutoName()
00195 {
00196 if(checkAutoName->isChecked())
00197 {
00198 if(QMessageBox::question(this,"Use automatic name?",
00199 "Would you realy use automatic name? Current name will be lost!",
00200 tr("&Yes"),tr("&No")))
00201 {
00202 checkAutoName->setChecked(false);
00203 return;
00204 }
00205
00206 QRegExp rx("^Q[0-9]{1,10}$");
00207 QRegExpValidator v(rx,this);
00208
00209 int pos = 0;
00210 if (v.validate(currentStateName,pos) == QValidator::Acceptable)
00211 {
00212 edtName->setText(currentStateName);
00213 }
00214 else
00215 {
00216 edtName->setText(editor->getUniqueAutoName());
00217 }
00218 }
00219 edtName->setEnabled(!checkAutoName->isChecked());
00220 }
00221
00222
00223
00224 bool StateDialog::nameBraceMatch()
00225 {
00226 QString name = edtName->text();
00227 int b_count = 0;
00228 int pos = 0;
00229
00230 QRegExp braceRegex("(\\{|\\})");
00231 while((pos = name.indexOf(braceRegex, pos)) != -1)
00232 {
00233 if (braceRegex.cap(1) == "{")
00234 {
00235 b_count++;
00236 }
00237 else
00238 {
00239 Q_ASSERT(braceRegex.cap(1) == "}");
00240 b_count--;
00241 if (b_count < 0) return false;
00242 }
00243 pos++;
00244 }
00245
00246 return (b_count == 0);
00247 }
00248
00249 void StateDialog::testAccept()
00250 {
00251 QString s;
00252 s = edtName->text();
00253 if (!checkAutoName->isChecked())
00254 {
00255 if (s == "")
00256 {
00257 QMessageBox::warning(this,"Wrong format of name!",
00258 "Name might not be empty!");
00259 return;
00260 }
00261 if (s != currentStateName)
00262 {
00263 if (editor->testStateName(s))
00264 {
00265 QMessageBox::warning(this,"Name si used!",
00266 "This state name is now used by another state. Use another name please!");
00267 return;
00268 }
00269 }
00270 }
00271 else
00272 {
00273 if (s != currentStateName)
00274 {
00275
00276 }
00277 }
00278
00279 if (s.contains(QRegExp("(?:\\{|\\})")))
00280 {
00281 if (!nameBraceMatch())
00282 {
00283 NameAcceptDialog::showMessage("Brace matching failed. Don't use unbalanced braces ('{', '}')!");
00284 return;
00285 }
00286 }
00287
00288 myAccept();
00289 }
00290
00291
00292
00293
00294
00295
00296
00297 StateExtendedDialog::StateExtendedDialog(QWidget *parent)
00298 : QDialog(parent)
00299 {
00300 QStringList sls;
00301 sls.append("solid");
00302 sls.append("dashed");
00303 sls.append("dotted");
00304 sls.append("none");
00305
00306 QStringList slf;
00307 slf.append("solid");
00308 slf.append("vlines");
00309 slf.append("hlines");
00310 slf.append("crosshatch");
00311 slf.append("none");
00312
00313 QRegExp rx("^[\\-]{0,1}[0-9]{1,3}\\.[0-9]{1,3}$");
00314 QValidator *v = new QRegExpValidator(rx,this);
00315
00316
00317 QLabel *lineStyle = new QLabel(QString("StateLineStyle"),this);
00318 comboLineStyle = new QComboBox(this);
00319 comboLineStyle->addItems(sls);
00320 comboLineStyle->setCurrentIndex(0);
00321
00322 QLabel *lineWidth = new QLabel(QString("StateLineWidth"),this);
00323 edtLineWidth = new QLineEdit(this);
00324 edtLineWidth->setValidator(v);
00325
00326 QLabel *lineColor = new QLabel(QString("StateLineColor"),this);
00327 edtLineColor = new QLineEdit(this);
00328
00329 QLabel *labelColor = new QLabel(QString("StateLabelColor"),this);
00330 edtLabelColor = new QLineEdit(this);
00331
00332 QLabel *labelScale = new QLabel(QString("StateLabelScale"),this);
00333 edtLabelScale = new QLineEdit(this);
00334 edtLabelScale->setValidator(v);
00335
00336 QLabel *fillStatus = new QLabel(QString("StateFillStatus"),this);
00337 comboFillStatus = new QComboBox(this);
00338 comboFillStatus->addItems(slf);
00339 comboFillStatus->setCurrentIndex(0);
00340
00341 QLabel *fillColor = new QLabel(QString("StateFillColor"),this);
00342 edtFillColor = new QLineEdit(this);
00343
00344
00345 QLabel *dimLineStyle = new QLabel(QString("DimStateLineStyle"),this);
00346 comboDimLineStyle = new QComboBox(this);
00347 comboDimLineStyle->addItems(sls);
00348 comboDimLineStyle->setCurrentIndex(0);
00349
00350 QLabel *dimLineColor = new QLabel(QString("DimStateLineColor"),this);
00351 edtDimLineColor = new QLineEdit(this);
00352
00353 QLabel *dimLineCoef = new QLabel(QString("DimStateLineCoef"),this);
00354 edtDimLineCoef = new QLineEdit(this);
00355 edtDimLineCoef->setValidator(v);
00356
00357 QLabel *dimLabelColor = new QLabel(QString("DimStateLabelColor"),this);
00358 edtDimLabelColor = new QLineEdit(this);
00359
00360 QLabel *dimFillColor = new QLabel(QString("DimStateFillColor"),this);
00361 edtDimFillColor = new QLineEdit(this);
00362
00363
00364 QLabel *lineDblCoef = new QLabel(QString("StateLineDblCoef"),this);
00365 edtLineDblCoef = new QLineEdit(this);
00366 edtLineDblCoef->setValidator(v);
00367
00368 QLabel *lineDblSep = new QLabel(QString("StateLineDblSep"),this);
00369 edtLineDblSep= new QLineEdit(this);
00370 edtLineDblSep->setValidator(v);
00371
00372
00373 QWidget *hBox = new QWidget(this);
00374 QPushButton *buttonOk = new QPushButton("Ok");
00375 QPushButton *buttonCancel = new QPushButton("Cancel");
00376 QHBoxLayout *hBoxLayout = new QHBoxLayout;
00377 hBoxLayout->addWidget(buttonOk);
00378 hBoxLayout->addWidget(buttonCancel);
00379 hBox->setLayout(hBoxLayout);
00380 hBox->setMaximumWidth(300);
00381 connect(buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
00382 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
00383
00384 QGroupBox *advancedGroup = new QGroupBox("Advanced parameters");
00385 QGridLayout *advancedLayout = new QGridLayout(advancedGroup);
00386 advancedLayout->addWidget(lineStyle,0,0);
00387 advancedLayout->addWidget(comboLineStyle,0,1);
00388 advancedLayout->addWidget(lineWidth,0,2);
00389 advancedLayout->addWidget(edtLineWidth,0,3);
00390 advancedLayout->addWidget(lineColor,1,0);
00391 advancedLayout->addWidget(edtLineColor,1,1);
00392 advancedLayout->addWidget(labelColor,1,2);
00393 advancedLayout->addWidget(edtLabelColor,1,3);
00394 advancedLayout->addWidget(labelScale,2,0,1,2,Qt::AlignRight);
00395 advancedLayout->addWidget(edtLabelScale,2,2,1,2,Qt::AlignLeft);
00396 advancedLayout->addWidget(fillStatus,3,0);
00397 advancedLayout->addWidget(comboFillStatus,3,1);
00398 advancedLayout->addWidget(fillColor,3,2);
00399 advancedLayout->addWidget(edtFillColor,3,3);
00400
00401 QGroupBox *presetGroup = new QGroupBox("Preset parameters (dim state)");
00402 QGridLayout* presetLayout = new QGridLayout(presetGroup);
00403 presetLayout->addWidget(dimLineStyle, 0,0);
00404 presetLayout->addWidget(comboDimLineStyle, 0,1);
00405 presetLayout->addWidget(dimLineCoef, 0,2);
00406 presetLayout->addWidget(edtDimLineCoef, 0,3);
00407 presetLayout->addWidget(dimLineColor, 1,0);
00408 presetLayout->addWidget(edtDimLineColor, 1,1);
00409 presetLayout->addWidget(dimLabelColor, 1,2);
00410 presetLayout->addWidget(edtDimLabelColor, 1,3);
00411 presetLayout->addWidget(dimFillColor, 2,0,1,2, Qt::AlignRight);
00412 presetLayout->addWidget(edtDimFillColor, 2,2,1,2, Qt::AlignLeft);
00413
00414 QGroupBox *doubleGroup = new QGroupBox("Line double parameters (final state)");
00415 QHBoxLayout *doubleLayout = new QHBoxLayout(doubleGroup);
00416 doubleLayout->addWidget(lineDblCoef);
00417 doubleLayout->addWidget(edtLineDblCoef);
00418 doubleLayout->addWidget(lineDblSep);
00419 doubleLayout->addWidget(edtLineDblSep);
00420
00421 QVBoxLayout *dialogLayout = new QVBoxLayout(this);
00422 dialogLayout->addWidget(advancedGroup);
00423 dialogLayout->addWidget(presetGroup);
00424 dialogLayout->addWidget(doubleGroup);
00425 dialogLayout->addWidget(hBox, 0, Qt::AlignHCenter);
00426
00427 QString s = "Extended parameters";
00428 setWindowTitle(s);
00429 setWindowIcon(QIcon(":images/state.xpm"));
00430 }
00431
00432 void StateExtendedDialog::setParams(State *state){
00433 comboLineStyle->setCurrentIndex(trLineStyleI(state->stateLineStyle));
00434 edtLineWidth->setText(QString("%1").arg(state->stateLineWidth));
00435 edtLineColor->setText(state->stateLineColor);
00436 edtLabelColor->setText(state->stateLabelColor);
00437 edtLabelScale->setText(QString("%1").arg(state->stateLabelScale));
00438 comboFillStatus->setCurrentIndex(trFillStatusI(state->stateFillStatus));
00439 edtFillColor->setText(state->stateFillColor);
00440
00441 comboDimLineStyle->setCurrentIndex(trLineStyleI(state->dimStateLineStyle));
00442 edtDimLineColor->setText(state->dimStateLineColor);
00443 edtDimLineCoef->setText(QString("%1").arg(state->dimStateLineCoef));
00444 edtDimLabelColor->setText(state->dimStateLabelColor);
00445 edtDimFillColor->setText(state->dimStateFillColor);
00446
00447 edtLineDblCoef->setText(QString("%1").arg(state->stateLineDoubleCoef));
00448 edtLineDblSep->setText(QString("%1").arg(state->stateLineDoubleSep));
00449 }
00450
00451 Qt::PenStyle StateExtendedDialog::getLineStyle(){
00452 return trLineStyle(comboLineStyle->currentIndex());
00453 }
00454
00455 float StateExtendedDialog::getLineWidth(){
00456 return edtLineWidth->text().toFloat();
00457 }
00458
00459 QString StateExtendedDialog::getLineColor(){
00460 return edtLineColor->text();
00461 }
00462
00463 QString StateExtendedDialog::getLabelColor(){
00464 return edtLabelColor->text();
00465 }
00466
00467 float StateExtendedDialog::getLabelScale(){
00468 return edtLabelScale->text().toFloat();
00469 }
00470
00471 Qt::BrushStyle StateExtendedDialog::getFillStatus(){
00472 return trFillStatus(comboFillStatus->currentIndex());
00473 }
00474
00475 QString StateExtendedDialog::getFillColor(){
00476 return edtFillColor->text();
00477 }
00478
00479 Qt::PenStyle StateExtendedDialog::getDimLineStyle(){
00480 return trLineStyle(comboDimLineStyle->currentIndex());
00481 }
00482
00483 QString StateExtendedDialog::getDimLineColor(){
00484 return edtDimLineColor->text();
00485 }
00486
00487 float StateExtendedDialog::getDimLineCoef(){
00488 return edtDimLineCoef->text().toFloat();
00489 }
00490
00491 QString StateExtendedDialog::getDimLabelColor(){
00492 return edtDimLabelColor->text();
00493 }
00494
00495 QString StateExtendedDialog::getDimFillColor(){
00496 return edtDimFillColor->text();
00497 }
00498
00499 float StateExtendedDialog::getLineDblCoef(){
00500 return edtLineDblCoef->text().toFloat();
00501 }
00502
00503 float StateExtendedDialog::getLineDblSep(){
00504 return edtLineDblSep->text().toFloat();
00505 }
00506
00507