53 #include "ui_dialogvariables.h"
54 #include "../vwidgets/vwidgetpopup.h"
55 #include "../vmisc/vsettings.h"
56 #include "../qmuparser/qmudef.h"
57 #include "../qmuparser/qmutokenparser.h"
58 #include "../vpatterndb/vtranslatevars.h"
59 #include "../vpatterndb/calculator.h"
60 #include "../vtools/dialogs/support/edit_formula_dialog.h"
62 #include <QFileDialog>
64 #include <QMessageBox>
65 #include <QCloseEvent>
66 #include <QTableWidget>
68 #include <QTableWidgetItem>
71 #define DIALOG_MAX_FORMULA_HEIGHT 64
85 , formulaBaseHeight(0)
94 setWindowFlags(Qt::Window);
95 setWindowFlags((windowFlags() | Qt::WindowStaysOnTopHint) & ~Qt::WindowContextHelpButtonHint);
97 ui->name_LineEdit->setClearButtonEnabled(
true);
98 ui->filter_LineEdit->installEventFilter(
this);
101 ui->formula_PlainTextEdit->installEventFilter(
this);
103 qApp->Settings()->GetOsSeparator() ? setLocale(QLocale()) : setLocale(QLocale::c());
105 qCDebug(vDialog,
"Showing variables.");
108 const bool freshCall =
true;
128 ui->tabWidget->setCurrentIndex(0);
129 ui->name_LineEdit->setValidator(
new QRegularExpressionValidator(QRegularExpression(
132 connect(
ui->variables_TableWidget, &QTableWidget::itemSelectionChanged,
this,
148 connect(
ui->variables_TableWidget->horizontalHeader(), &QHeaderView::sectionClicked, [
this]()
154 if (
ui->variables_TableWidget->rowCount() > 0)
156 ui->variables_TableWidget->selectRow(0);
160 auto clearFilterString = [
this] ()
162 ui->filter_LineEdit->clear();
165 if (
ui->tabWidget->currentIndex() == 0)
168 ui->variables_TableWidget->horizontalHeader()->setSortIndicator(-1, Qt::AscendingOrder);
173 connect(
ui->tabWidget, &QTabWidget::currentChanged,
this, clearFilterString);
188 ui->variables_TableWidget->blockSignals(
true);
189 ui->variables_TableWidget->clearContents();
195 for (i = variables.constBegin(); i != variables.constEnd(); ++i)
198 map.insert(variable->getIndex(), i.key());
201 qint32 currentRow = -1;
202 QMapIterator<quint32, QString> iMap(map);
203 ui->variables_TableWidget->setRowCount ( variables.size() );
204 while (iMap.hasNext())
210 addCell(
ui->variables_TableWidget, variable->GetName(), currentRow, 0, Qt::AlignVCenter);
211 addCell(
ui->variables_TableWidget,
qApp->LocaleToString(*variable->GetValue()), currentRow, 1,
212 Qt::AlignHCenter | Qt::AlignVCenter, variable->IsFormulaOk());
217 formula =
qApp->TrVars()->FormulaToUser(variable->GetFormula(),
qApp->Settings()->GetOsSeparator());
222 formula = variable->GetFormula();
225 addCell(
ui->variables_TableWidget, formula, currentRow, 2, Qt::AlignVCenter);
230 ui->variables_TableWidget->resizeColumnsToContents();
231 ui->variables_TableWidget->resizeRowsToContents();
234 ui->variables_TableWidget->setColumnWidth(0, 350);
235 ui->variables_TableWidget->horizontalHeader()->setStretchLastSection(
true);
236 ui->variables_TableWidget->blockSignals(
false);
240 template <
typename T>
245 qint32 currentRow = -1;
246 QMapIterator<QString, T> i(varTable);
250 qreal length = *i.value()->GetValue();
252 table->setRowCount ( varTable.size() );
254 QTableWidgetItem *item =
new QTableWidgetItem(i.key());
255 item->setTextAlignment(Qt::AlignLeft);
256 table->setItem(currentRow, 0, item);
258 item =
new QTableWidgetItem(
qApp->LocaleToString(length));
259 item->setTextAlignment(Qt::AlignHCenter);
260 table->setItem(currentRow, 1, item);
262 table->resizeColumnsToContents();
263 table->resizeRowsToContents();
264 table->verticalHeader()->setDefaultSectionSize(20);
330 const QString header = table->horizontalHeaderItem(column)->text();
331 const QString unitHeader = QString(
"%1 (%2)").arg(header).arg(unit);
332 table->horizontalHeaderItem(column)->setText(unitHeader);
340 QTableWidgetItem *item =
new QTableWidgetItem(text);
341 item->setTextAlignment(aligment);
344 Qt::ItemFlags flags = item->flags();
345 flags &= ~(Qt::ItemIsEditable);
346 item->setFlags(flags);
350 QBrush brush = item->foreground();
351 brush.setColor(Qt::red);
352 item->setForeground(brush);
355 table->setItem(row, column, item);
374 QString clear = name;
387 if (formula.isEmpty())
389 label->setText(tr(
"Error") +
" (" + postfix +
"). " + tr(
"Empty field."));
390 label->setToolTip(tr(
"Empty field"));
401 f =
qApp->TrVars()->FormulaFromUser(formula,
qApp->Settings()->GetOsSeparator());
407 f.replace(
"\n",
" ");
408 QScopedPointer<Calculator> cal(
new Calculator());
411 if (qIsInf(result) || qIsNaN(result))
413 label->setText(tr(
"Error") +
" (" + postfix +
").");
414 label->setToolTip(tr(
"Invalid result. Value is infinite or NaN. Please, check your calculations."));
418 label->setText(
qApp->LocaleToString(result) +
" " + postfix);
419 label->setToolTip(tr(
"Value"));
424 label->setText(tr(
"Error") +
" (" + postfix +
"). " + tr(
"Parser error: %1").arg(e.
GetMsg()));
425 label->setToolTip(tr(
"Parser error: %1").arg(e.
GetMsg()));
436 ui->toolButtonUp->setEnabled(
false);
437 ui->toolButtonDown->setEnabled(
false);
441 if (
ui->variables_TableWidget->rowCount() > 0)
443 const QTableWidgetItem *name =
ui->variables_TableWidget->item(
ui->variables_TableWidget->currentRow(), 0);
446 ui->removeCustomVariable_ToolButton->setEnabled(not
variableUsed(name->text()));
450 ui->removeCustomVariable_ToolButton->setEnabled(
false);
453 if (
ui->variables_TableWidget->rowCount() >= 2)
455 if (
ui->variables_TableWidget->currentRow() == 0)
457 ui->toolButtonUp->setEnabled(
false);
458 ui->toolButtonDown->setEnabled(
true);
460 else if (
ui->variables_TableWidget->currentRow() ==
ui->variables_TableWidget->rowCount()-1)
462 ui->toolButtonUp->setEnabled(
true);
463 ui->toolButtonDown->setEnabled(
false);
467 ui->toolButtonUp->setEnabled(
true);
468 ui->toolButtonDown->setEnabled(
true);
473 ui->toolButtonUp->setEnabled(
false);
474 ui->toolButtonDown->setEnabled(
false);
487 ui->removeCustomVariable_ToolButton->setEnabled(enabled);
489 ui->toolButtonUp->setEnabled(enabled);
490 ui->toolButtonDown->setEnabled(enabled);
495 ui->name_LineEdit->blockSignals(
true);
496 ui->name_LineEdit->clear();
497 ui->name_LineEdit->blockSignals(
false);
499 ui->description_PlainTextEdit->blockSignals(
true);
500 ui->description_PlainTextEdit->clear();
501 ui->description_PlainTextEdit->blockSignals(
false);
503 ui->calculatedValue_Label->blockSignals(
true);
504 ui->calculatedValue_Label->clear();
505 ui->calculatedValue_Label->blockSignals(
false);
507 ui->formula_PlainTextEdit->blockSignals(
true);
508 ui->formula_PlainTextEdit->clear();
509 ui->formula_PlainTextEdit->blockSignals(
false);
512 ui->formula_ToolButton->setEnabled(enabled);
513 ui->name_LineEdit->setEnabled(enabled);
514 ui->description_PlainTextEdit->setEnabled(enabled);
515 ui->formula_PlainTextEdit->setEnabled(enabled);
530 for(
int i = 0; i < expressions.size(); ++i)
532 if (expressions.at(i).expression.indexOf(name) != -1)
537 QScopedPointer<qmu::QmuTokenParser> cal(
new qmu::QmuTokenParser(expressions.at(i).expression,
false,
541 if (cal->GetTokens().values().contains(name))
578 ui->lineLengths_TableWidget->clearContents();
579 ui->curveLengths_TableWidget->clearContents();
580 ui->curveAngles_TableWidget->clearContents();
581 ui->lineAngles_TableWidget->clearContents();
582 ui->arcRadiuses_TableWidget->clearContents();
605 const int row =
ui->variables_TableWidget->currentRow();
609 ui->variables_TableWidget->blockSignals(
true);
610 ui->variables_TableWidget->selectRow(row);
611 ui->variables_TableWidget->blockSignals(
false);
623 qCDebug(vDialog,
"Add a new custom variable");
626 qint32 currentRow = -1;
628 if (
ui->variables_TableWidget->currentRow() == -1)
630 currentRow =
ui->variables_TableWidget->rowCount();
635 currentRow =
ui->variables_TableWidget->currentRow()+1;
636 const QTableWidgetItem *item =
ui->variables_TableWidget->item(
ui->variables_TableWidget->currentRow(), 0);
643 ui->variables_TableWidget->selectRow(currentRow);
652 const int row =
ui->variables_TableWidget->currentRow();
659 const QTableWidgetItem *name =
ui->variables_TableWidget->item(row, 0);
665 if (
ui->variables_TableWidget->rowCount() > 0)
667 ui->variables_TableWidget->selectRow(0);
678 const int row =
ui->variables_TableWidget->currentRow();
685 const QTableWidgetItem *name =
ui->variables_TableWidget->item(row, 0);
691 ui->variables_TableWidget->selectRow(row-1);
697 const int row =
ui->variables_TableWidget->currentRow();
704 const QTableWidgetItem *name =
ui->variables_TableWidget->item(row, 0);
710 ui->variables_TableWidget->selectRow(row+1);
716 const int row =
ui->variables_TableWidget->currentRow();
723 const QTableWidgetItem *name =
ui->variables_TableWidget->item(row, 0);
730 QString tempName = newName;
733 tempName = tempName + QLatin1String(
"_") + QString().number(num);
747 ui->variables_TableWidget->blockSignals(
true);
748 ui->variables_TableWidget->selectRow(row);
749 ui->variables_TableWidget->blockSignals(
false);
755 const int row =
ui->variables_TableWidget->currentRow();
762 const QTableWidgetItem *name =
ui->variables_TableWidget->item(row, 0);
767 const QTextCursor cursor =
ui->description_PlainTextEdit->textCursor();
768 ui->variables_TableWidget->blockSignals(
true);
769 ui->variables_TableWidget->selectRow(row);
770 ui->variables_TableWidget->blockSignals(
false);
771 ui->description_PlainTextEdit->setTextCursor(cursor);
777 const int row =
ui->variables_TableWidget->currentRow();
784 const QTableWidgetItem *name =
ui->variables_TableWidget->item(row, 0);
787 QString text =
ui->formula_PlainTextEdit->toPlainText();
788 text.replace(
"\n",
" ");
790 QTableWidgetItem *formula =
ui->variables_TableWidget->item(row, 2);
791 if (formula->text() == text)
793 QTableWidgetItem *result =
ui->variables_TableWidget->item(row, 1);
796 ui->calculatedValue_Label->setText(result->text() +
" " +postfix);
804 ui->calculatedValue_Label->setText(tr(
"Error") +
" (" + postfix +
"). " + tr(
"Empty field."));
816 const QString formula =
qApp->TrVars()->FormulaFromUser(text,
qApp->Settings()->GetOsSeparator());
828 const QTextCursor cursor =
ui->formula_PlainTextEdit->textCursor();
829 ui->variables_TableWidget->blockSignals(
true);
830 ui->variables_TableWidget->selectRow(row);
831 ui->variables_TableWidget->blockSignals(
false);
832 ui->formula_PlainTextEdit->setTextCursor(cursor);
838 const int row =
ui->variables_TableWidget->currentRow();
845 const QTableWidgetItem *name =
ui->variables_TableWidget->item(row, 0);
849 dialog->setWindowTitle(tr(
"Edit variable"));
850 dialog->SetFormula(
qApp->TrVars()->TryFormulaFromUser(
ui->formula_PlainTextEdit->toPlainText().replace(
"\n",
" "),
851 qApp->Settings()->GetOsSeparator()));
853 dialog->setPostfix(postfix);
855 if (dialog->exec() == QDialog::Accepted)
858 const QTableWidgetItem *name =
ui->variables_TableWidget->item(row, 0);
864 ui->variables_TableWidget->selectRow(row);
874 ui->formula_PlainTextEdit->blockSignals(
true);
875 ui->name_LineEdit->blockSignals(
true);
876 ui->description_PlainTextEdit->blockSignals(
true);
886 if (event->type() == QEvent::LanguageChange)
889 ui->retranslateUi(
this);
893 QWidget::changeEvent(event);
899 if (QLineEdit *textEdit = qobject_cast<QLineEdit *>(
object))
901 if (event->type() == QEvent::KeyPress)
903 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(event);
904 if ((keyEvent->key() == Qt::Key_Period) && (keyEvent->modifiers() & Qt::KeypadModifier))
906 if (
qApp->Settings()->GetOsSeparator())
908 textEdit->insert(QLocale().decimalPoint());
912 textEdit->insert(QLocale::c().decimalPoint());
930 QDialog::showEvent(event);
931 if ( event->spontaneous() )
942 const QSize sz =
qApp->Settings()->GetIncrementsDialogSize();
943 if (not sz.isEmpty())
959 qApp->Settings()->SetIncrementsDialogSize(size());
961 DialogTool::resizeEvent(event);
967 if (
ui->variables_TableWidget->rowCount() > 0)
972 const QTableWidgetItem *name =
ui->variables_TableWidget->item(
ui->variables_TableWidget->currentRow(), 0);
986 ui->name_LineEdit->blockSignals(
true);
988 ui->name_LineEdit->blockSignals(
false);
990 ui->description_PlainTextEdit->blockSignals(
true);
991 ui->description_PlainTextEdit->setPlainText(variable->GetDescription());
992 ui->description_PlainTextEdit->blockSignals(
false);
995 ui->formula_PlainTextEdit->blockSignals(
true);
1000 formula =
qApp->TrVars()->FormulaToUser(variable->GetFormula(),
qApp->Settings()->GetOsSeparator());
1005 formula = variable->GetFormula();
1008 ui->formula_PlainTextEdit->setPlainText(formula);
1009 ui->formula_PlainTextEdit->blockSignals(
false);
1021 currentTable->blockSignals(
true);
1023 if (filterString.isEmpty())
1026 for (
auto i = 0; i < currentTable->rowCount(); ++i)
1028 currentTable->showRow(i);
1030 ui->variables_TableWidget->horizontalHeader()->setSortIndicator(-1, Qt::AscendingOrder);
1035 ui->toolButtonUp->setEnabled(
false);
1036 ui->toolButtonDown->setEnabled(
false);
1037 for (
auto i = 0; i < currentTable->rowCount(); i++)
1039 currentTable->hideRow(i);
1042 for (
auto item : currentTable->findItems(filterString, Qt::MatchContains))
1046 currentTable->showRow(item->row());
1051 currentTable->blockSignals(
false);
The Calculator class for calculation formula.
The DialogVariables class show dialog increments. Tables of all variables in program will be here.
void saveCustomVariableName(const QString &text)
QList< QSharedPointer< QTableWidget > > tableList
void addCustomVariable()
clickedToolButtonAdd create new row in table
QString clearCustomVariableName(const QString &name) const
void fillControlPointLengths()
QString getCustomVariableName() const
void fillCustomVariables(bool freshCall=false)
fillCustomVariables fill data for variables table
virtual void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
closeEvent handle when dialog cloded
VPattern * doc
doc dom document container
void saveCustomVariableDescription()
void enablePieces(bool enabled)
virtual void changeEvent(QEvent *event) Q_DECL_OVERRIDE
void saveCustomVariableFormula()
void fillTable(const QMap< QString, T > &varTable, QTableWidget *table)
void showHeaderUnits(QTableWidget *table, int column, const QString &unit)
DialogVariables(VContainer *data, VPattern *doc, QWidget *parent=nullptr)
DialogVariables create dialog.
virtual ~DialogVariables() Q_DECL_OVERRIDE
QVector< QPair< QString, QString > > renameList
void removeCustomVariable()
clickedToolButtonRemove remove one row from table
void fillLineLengths()
FillLengthLines fill data for table of lines lengths.
Ui::DialogVariables * ui
ui keeps information about user interface
void renameCache(const QString &name, const QString &newName)
void FullUpdateFromFile()
FullUpdateFromFile update information in tables form file.
virtual void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE
VContainer * data
data container with data
void fillCurveLengths()
FillLengthSplines fill data for table of splines lengths.
bool variableUsed(const QString &name) const
void showCustomVariableDetails()
virtual bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE
void addCell(QTableWidget *table, const QString &text, int row, int column, int aligment, bool ok=true)
void filterVariables(const QString &filterString)
bool evalVariableFormula(const QString &formula, bool fromUser, VContainer *data, QLabel *label)
virtual void showEvent(QShowEvent *event) Q_DECL_OVERRIDE
showEvent handle when window show
void FullUpdateFromFile()
FullUpdateFromFile update tool data form file.
QVector< VFormulaField > ListExpressions() const
QVector< VFormulaField > ListIncrementExpressions() const
The VContainer class container of all variables.
const QMap< QString, QSharedPointer< VArcRadius > > arcRadiusesData() const
const QMap< QString, QSharedPointer< VLineAngle > > lineAnglesData() const
const QMap< QString, QSharedPointer< VCurveAngle > > curveAnglesData() const
const QMap< QString, QSharedPointer< VCurveCLength > > controlPointLengthsData() const
static bool IsUnique(const QString &name)
const QMap< QString, QSharedPointer< VCurveLength > > curveLengthsData() const
const QMap< QString, QSharedPointer< VLengthLine > > lineLengthsData() const
const QHash< QString, QSharedPointer< VInternalVariable > > * DataVariables() const
const QMap< QString, QSharedPointer< VIncrement > > variablesData() const
QSharedPointer< T > GetVariable(QString name) const
GetVariable return varible by name.
The VExceptionBadId class for exception bad id.
The VIncrement class keep data row of increment table.
The VPattern class working with pattern file.
void MoveDownIncrement(const QString &name)
void replaceNameInFormula(QVector< VFormulaField > &expressions, const QString &name, const QString &newName)
void setIncrementDescription(const QString &name, const QString &text)
void LiteParseIncrements()
void addEmptyCustomVariable(const QString &name)
void SetIncrementFormula(const QString &name, const QString &text)
void MoveUpIncrement(const QString &name)
void setIncrementName(const QString &name, const QString &text)
void removeCustomVariable(const QString &name)
virtual void LiteParseTree(const Document &parse) Q_DECL_OVERRIDE
LiteParseTree lite parse file.
void addEmptyCustomVariableAfter(const QString &after, const QString &name)
Error class of the parser.
const QString & GetMsg() const
Returns the message string for this error.
const QString degreeSymbol
QString UnitsToStr(const Unit &unit, const bool translate)
UnitsToStr translate unit to string.
const QString CustomIncrSign