Working with new Increments.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2015-08-25 13:35:50 +03:00
parent f8b92a0e89
commit 5a1208655c
21 changed files with 1122 additions and 444 deletions

View File

@ -671,18 +671,11 @@ void TMainWindow::MoveUp()
return;
}
QTableWidgetItem *nameField = ui->tableWidget->item(ui->tableWidget->currentRow(), 0);
QTableWidgetItem *nameField = ui->tableWidget->item(row, 0);
m->MoveUp(nameField->text());
MeasurementsWasSaved(false);
RefreshData();
ui->tableWidget->blockSignals(true);
ui->tableWidget->selectRow(row-1);
ui->tableWidget->blockSignals(false);
Controls(); // Buttons remove, up, down
}
//---------------------------------------------------------------------------------------------------------------------
@ -695,18 +688,11 @@ void TMainWindow::MoveDown()
return;
}
QTableWidgetItem *nameField = ui->tableWidget->item(ui->tableWidget->currentRow(), 0);
QTableWidgetItem *nameField = ui->tableWidget->item(row, 0);
m->MoveDown(nameField->text());
MeasurementsWasSaved(false);
RefreshData();
ui->tableWidget->blockSignals(true);
ui->tableWidget->selectRow(row+1);
ui->tableWidget->blockSignals(false);
Controls(); // Buttons remove, up, down
}
//---------------------------------------------------------------------------------------------------------------------
@ -727,7 +713,7 @@ void TMainWindow::Fx()
QString text = ui->plainTextEditFormula->toPlainText();
text.replace("\n", " ");
text = qApp->TrVars()->FormulaFromUser(text, true);
dialog->SetFormula(text);
const QString postfix = VDomDocument::UnitsToStr(mUnit, true);//Show unit in dialog lable (cm, mm or inch)
dialog->setPostfix(postfix);
@ -752,7 +738,7 @@ void TMainWindow::AddCustom()
QString name;
do
{
name = QString(CustomSign + tr("M_%1")).arg(num);
name = CustomMSign + tr("M_%1").arg(num);
num++;
} while (data->IsUnique(name) == false);
@ -1000,7 +986,7 @@ void TMainWindow::ShowMData()
}
else
{
EvalFormula(meash->GetFormula(), meash->GetData(), ui->labelCalculatedValue);
EvalFormula(meash->GetFormula(), false, meash->GetData(), ui->labelCalculatedValue);
ui->plainTextEditFormula->blockSignals(true);
@ -1093,7 +1079,7 @@ void TMainWindow::SaveMName()
QString newName = ui->lineEditName->text();
if (meash->IsCustom())
{
newName = CustomSign + newName;
newName = CustomMSign + newName;
}
if (data->IsUnique(newName))
@ -1144,23 +1130,26 @@ void TMainWindow::SaveMValue()
return;
}
QString formula;
try
// Translate to internal look.
QSharedPointer<VMeasurement> meash = data->GetVariable<VMeasurement>(nameField->text());
const bool ok = EvalFormula(text, true, meash->GetData(), ui->labelCalculatedValue);
if (not ok)
{
// Translate to internal look.
formula = qApp->TrVars()->FormulaFromUser(text, true);
QSharedPointer<VMeasurement> meash = data->GetVariable<VMeasurement>(nameField->text());
EvalFormula(formula, meash->GetData(), ui->labelCalculatedValue);
}
catch (qmu::QmuParserError &e)
{
const QString postfix = VDomDocument::UnitsToStr(mUnit);//Show unit in dialog lable (cm, mm or inch)
ui->labelCalculatedValue->setText(tr("Error") + " (" + postfix + "). " +
tr("Parser error: %1").arg(e.GetMsg()));
return;
}
m->SetMValue(nameField->text(), formula);
try
{
const QString formula = qApp->TrVars()->FormulaFromUser(text, true);
m->SetMValue(nameField->text(), formula);
}
catch (qmu::QmuParserError &e) // Just in case something bad happens
{
Q_UNUSED(e)
return;
}
MeasurementsWasSaved(false);
@ -1809,7 +1798,7 @@ void TMainWindow::MFields(bool enabled)
QString TMainWindow::ClearCustomName(const QString &name) const
{
QString clear = name;
const int index = clear.indexOf(CustomSign);
const int index = clear.indexOf(CustomMSign);
if (index == 0)
{
clear.remove(0, 1);
@ -1818,20 +1807,29 @@ QString TMainWindow::ClearCustomName(const QString &name) const
}
//---------------------------------------------------------------------------------------------------------------------
void TMainWindow::EvalFormula(const QString &formula, VContainer *data, QLabel *label)
bool TMainWindow::EvalFormula(const QString &formula, bool fromUser, VContainer *data, QLabel *label)
{
const QString postfix = VDomDocument::UnitsToStr(pUnit);//Show unit in dialog lable (cm, mm or inch)
if (formula.isEmpty())
{
label->setText(tr("Error") + " (" + postfix + ")");
label->setText(tr("Error") + " (" + postfix + "). " + tr("Empty field."));
label->setToolTip(tr("Empty field"));
return false;
}
else
{
try
{
// Replace line return character with spaces for calc if exist
QString f = formula;
QString f;
if (not fromUser)
{
f = qApp->TrVars()->FormulaFromUser(formula, true);
}
else
{
f = formula;
}
f.replace("\n", " ");
Calculator *cal = new Calculator(data, mType);
const qreal result = UnitConvertor(cal->EvalFormula(f), mUnit, pUnit);
@ -1839,11 +1837,13 @@ void TMainWindow::EvalFormula(const QString &formula, VContainer *data, QLabel *
label->setText(qApp->LocaleToString(result) + " " +postfix);
label->setToolTip(tr("Value"));
return true;
}
catch (qmu::QmuParserError &e)
{
label->setText(tr("Error") + " (" + postfix + ")");
label->setText(tr("Error") + " (" + postfix + "). " + tr("Parser error: %1").arg(e.GetMsg()));
label->setToolTip(tr("Parser error: %1").arg(e.GetMsg()));
return false;
}
}
}
@ -1885,7 +1885,7 @@ void TMainWindow::GUIReadOnly(bool ro)
{
if (QTableWidgetItem *nameField = ui->tableWidget->item(ui->tableWidget->currentRow(), 0))
{
if (nameField->text().indexOf(CustomSign) == 0) // Check if custom
if (nameField->text().indexOf(CustomMSign) == 0) // Check if custom
{
ui->lineEditName->setReadOnly(ro);
}

View File

@ -166,7 +166,7 @@ private:
QString ClearCustomName(const QString &name) const;
void EvalFormula(const QString &formula, VContainer *data, QLabel *label);
bool EvalFormula(const QString &formula, bool fromUser, VContainer *data, QLabel *label);
void Open(const QString &pathTo, const QString &filter);
void GUIReadOnly(bool ro);

View File

@ -35,6 +35,7 @@
#include "../vwidgets/vmaingraphicsview.h"
#include "../version.h"
#include "../vmisc/logging.h"
#include "../qmuparser/qmuparsererror.h"
#include <QDebug>
#include <QDir>
@ -264,9 +265,17 @@ bool VApplication::notify(QObject *receiver, QEvent *event)
e.CriticalMessageBox(tr("Something's wrong!!"), mainWindow);
return true;
}
// These last two cases special. I found that we can't show here modal dialog with error message.
// Somehow program doesn't waite untile an error dialog will be closed. But if ignore this program will hang.
catch (const qmu::QmuParserError &e)
{
qCDebug(vApp, "Parser error: %s", e.GetMsg().toUtf8().constData());
abort();
}
catch (std::exception& e)
{
qCritical() << "Exception thrown:" << e.what();
qCDebug(vApp, "Critical error! Exception thrown: %s", e.what());
abort();
}
return false;
}

View File

@ -28,14 +28,16 @@
#include "dialogincrements.h"
#include "ui_dialogincrements.h"
#include "../../libs/vwidgets/doubledelegate.h"
#include "../../libs/vwidgets/textdelegate.h"
#include "../../libs/vwidgets/vwidgetpopup.h"
#include "../vwidgets/doubledelegate.h"
#include "../vwidgets/textdelegate.h"
#include "../vwidgets/vwidgetpopup.h"
#include "../xml/vstandardmeasurements.h"
#include "../xml/vindividualmeasurements.h"
#include "../../libs/vmisc/vsettings.h"
#include "../../libs/qmuparser/qmudef.h"
#include "../../libs/vpatterndb/vtranslatevars.h"
#include "../vmisc/vsettings.h"
#include "../qmuparser/qmudef.h"
#include "../vpatterndb/vtranslatevars.h"
#include "../vpatterndb/calculator.h"
#include "../vtools/dialogs/support/dialogeditwrongformula.h"
#include <QFileDialog>
#include <QDir>
@ -45,6 +47,8 @@
#include <QSettings>
#include <QTableWidgetItem>
#define DIALOG_MAX_FORMULA_HEIGHT 64
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief DialogIncrements create dialog
@ -53,22 +57,20 @@
* @param parent parent widget
*/
DialogIncrements::DialogIncrements(VContainer *data, VPattern *doc, QWidget *parent)
:DialogTool(data, NULL_ID, parent), ui(new Ui::DialogIncrements), data(data), doc(doc), row(0), column(0)
:DialogTool(data, NULL_ID, parent),
ui(new Ui::DialogIncrements),
data(data),
doc(doc),
formulaBaseHeight(0)
{
ui->setupUi(this);
formulaBaseHeight = ui->plainTextEditFormula->height();
qApp->Settings()->GetOsSeparator() ? setLocale(QLocale::system()) : setLocale(QLocale(QLocale::C));
qCDebug(vDialog, "Showing variables.");
ShowUnits();
TextDelegate *textDelegate = new TextDelegate(NameRegExp(), data, ui->tableWidgetIncrement);
ui->tableWidgetIncrement->setItemDelegateForColumn(0, textDelegate);// name
DoubleSpinBoxDelegate *doubleDelegate = new DoubleSpinBoxDelegate(ui->tableWidgetIncrement);
ui->tableWidgetIncrement->setItemDelegateForColumn(2, doubleDelegate);// base value
ui->tableWidgetIncrement->setItemDelegateForColumn(3, doubleDelegate);// in sizes
ui->tableWidgetIncrement->setItemDelegateForColumn(4, doubleDelegate);// in heights
FillIncrements();
FillLengthsLines();
FillLengthLinesAngles();
@ -78,15 +80,29 @@ DialogIncrements::DialogIncrements(VContainer *data, VPattern *doc, QWidget *par
FillAnglesArcs();
FillAnglesCurves();
connect(ui->tableWidgetIncrement, &QTableWidget::cellChanged, this, &DialogIncrements::IncrementChanged);
connect(ui->toolButtonAdd, &QPushButton::clicked, this, &DialogIncrements::clickedToolButtonAdd);
connect(ui->toolButtonRemove, &QPushButton::clicked, this, &DialogIncrements::clickedToolButtonRemove);
connect(this, &DialogIncrements::FullUpdateTree, this->doc, &VPattern::LiteParseTree);
connect(this, &DialogIncrements::haveLiteChange, this->doc, &VPattern::haveLiteChange);
connect(this->doc, &VPattern::FullUpdateFromFile, this, &DialogIncrements::FullUpdateFromFile);
ui->tabWidget->setCurrentIndex(0);
ui->lineEditName->setValidator( new QRegularExpressionValidator(QRegularExpression(NameRegExp())));
connect(ui->tableWidgetIncrement, &QTableWidget::itemSelectionChanged, this,
&DialogIncrements::ShowIncrementDetails);
connect(ui->toolButtonAdd, &QPushButton::clicked, this, &DialogIncrements::AddIncrement);
connect(ui->toolButtonRemove, &QToolButton::clicked, this, &DialogIncrements::RemoveIncrement);
connect(ui->toolButtonUp, &QToolButton::clicked, this, &DialogIncrements::MoveUp);
connect(ui->toolButtonDown, &QToolButton::clicked, this, &DialogIncrements::MoveDown);
connect(ui->pushButtonGrow, &QPushButton::clicked, this, &DialogIncrements::DeployFormula);
connect(ui->toolButtonExpr, &QToolButton::clicked, this, &DialogIncrements::Fx);
connect(ui->lineEditName, &QLineEdit::editingFinished, this, &DialogIncrements::SaveIncrName);
connect(ui->plainTextEditDescription, &QPlainTextEdit::textChanged, this, &DialogIncrements::SaveIncrDescription);
connect(ui->plainTextEditFormula, &QPlainTextEdit::textChanged, this, &DialogIncrements::SaveIncrFormula);
if (ui->tableWidgetIncrement->rowCount() > 0)
{
ui->tableWidgetIncrement->selectRow(0);
}
}
//---------------------------------------------------------------------------------------------------------------------
@ -95,6 +111,9 @@ DialogIncrements::DialogIncrements(VContainer *data, VPattern *doc, QWidget *par
*/
void DialogIncrements::FillIncrements()
{
ui->tableWidgetIncrement->blockSignals(true);
ui->tableWidgetIncrement->clearContents();
const QMap<QString, QSharedPointer<VIncrement> > increments = data->DataIncrements();
QMap<QString, QSharedPointer<VIncrement> >::const_iterator i;
QMap<quint32, QString> map;
@ -102,59 +121,39 @@ void DialogIncrements::FillIncrements()
for (i = increments.constBegin(); i != increments.constEnd(); ++i)
{
QSharedPointer<VIncrement> incr = i.value();
map.insert(incr->getId(), i.key());
map.insert(incr->getIndex(), i.key());
}
qint32 currentRow = -1;
QMapIterator<quint32, QString> iMap(map);
ui->tableWidgetIncrement->setRowCount ( increments.size() );
while (iMap.hasNext())
{
iMap.next();
QSharedPointer<VIncrement> incr = increments.value(iMap.value());
currentRow++;
ui->tableWidgetIncrement->setRowCount ( increments.size() );
QTableWidgetItem *item = new QTableWidgetItem(iMap.value());
item->setTextAlignment(Qt::AlignHCenter);
item->setFont(QFont("Times", 12, QFont::Bold));
item->setData(Qt::UserRole, incr->getId());
ui->tableWidgetIncrement->setItem(currentRow, 0, item);
AddCell(ui->tableWidgetIncrement, incr->GetName(), currentRow, 0, Qt::AlignVCenter); // name
AddCell(ui->tableWidgetIncrement, qApp->LocaleToString(*incr->GetValue()), currentRow, 1,
Qt::AlignHCenter | Qt::AlignVCenter, incr->IsFormulaOk()); // calculated value
if (qApp->patternType() == MeasurementsType::Standard)
QString formula;
try
{
item = new QTableWidgetItem(qApp->LocaleToString(data->GetTableValue(iMap.value(), qApp->patternType())));
item->setTextAlignment(Qt::AlignHCenter);
SetItemViewOnly(item);
ui->tableWidgetIncrement->setItem(currentRow, 1, item);
formula = qApp->TrVars()->FormulaToUser(incr->GetFormula());
}
catch (qmu::QmuParserError &e)
{
Q_UNUSED(e);
formula = incr->GetFormula();
}
item = new QTableWidgetItem(qApp->LocaleToString(incr->GetBase()));
item->setTextAlignment(Qt::AlignHCenter);
ui->tableWidgetIncrement->setItem(currentRow, 2, item);
if (qApp->patternType() == MeasurementsType::Standard)
{
item = new QTableWidgetItem(qApp->LocaleToString(incr->GetKsize()));
item->setTextAlignment(Qt::AlignHCenter);
ui->tableWidgetIncrement->setItem(currentRow, 3, item);
item = new QTableWidgetItem(qApp->LocaleToString(incr->GetKheight()));
item->setTextAlignment(Qt::AlignHCenter);
ui->tableWidgetIncrement->setItem(currentRow, 4, item);
}
item = new QTableWidgetItem(incr->GetDescription());
item->setToolTip(incr->GetDescription());
item->setTextAlignment(Qt::AlignLeft);
ui->tableWidgetIncrement->setItem(currentRow, 5, item);
}
if (ui->tableWidgetIncrement->rowCount()>0)
{
ui->toolButtonRemove->setEnabled(true);
AddCell(ui->tableWidgetIncrement, formula, currentRow, 2, Qt::AlignVCenter); // formula
}
ui->tableWidgetIncrement->resizeColumnsToContents();
ui->tableWidgetIncrement->resizeRowsToContents();
ui->tableWidgetIncrement->setCurrentCell( row, column );
ui->tableWidgetIncrement->horizontalHeader()->setStretchLastSection(true);
ui->tableWidgetIncrement->blockSignals(false);
}
//---------------------------------------------------------------------------------------------------------------------
@ -237,23 +236,13 @@ void DialogIncrements::FillAnglesCurves()
FillTable(data->DataAnglesCurves(), ui->tableWidgetAnglesCurves);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::SetItemViewOnly(QTableWidgetItem *item)
{
// set the item non-editable (view only), and non-selectable
Qt::ItemFlags flags = item->flags();
flags &= ~(Qt::ItemIsSelectable | Qt::ItemIsEditable); // reset/clear the flag
item->setFlags(flags);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::ShowUnits()
{
const QString unit = VDomDocument::UnitsToStr(qApp->patternUnit());
ShowHeaderUnits(ui->tableWidgetIncrement, 2, unit);// base value
ShowHeaderUnits(ui->tableWidgetIncrement, 3, unit);// in sizes
ShowHeaderUnits(ui->tableWidgetIncrement, 4, unit);// in heights
ShowHeaderUnits(ui->tableWidgetIncrement, 1, unit);// calculated value
ShowHeaderUnits(ui->tableWidgetIncrement, 2, unit);// formula
ShowHeaderUnits(ui->tableWidgetLines, 1, unit);// lengths
ShowHeaderUnits(ui->tableWidgetSplines, 1, unit);// lengths
@ -275,17 +264,157 @@ void DialogIncrements::ShowHeaderUnits(QTableWidget *table, int column, const QS
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::ShowSuccess() const
void DialogIncrements::AddCell(QTableWidget *table, const QString &text, int row, int column, int aligment, bool ok)
{
VWidgetPopup *popup = new VWidgetPopup();
QLabel *label = new QLabel(tr("Data successfully saved."));
QFont f = label->font();
f.setBold(true);
f.setPixelSize(16);
label->setFont(f);
popup->SetWidget(label);
popup->SetLifeTime(2000);
popup->Show(frameGeometry().center());
SCASSERT(table != nullptr);
QTableWidgetItem *item = new QTableWidgetItem(text);
item->setTextAlignment(aligment);
// set the item non-editable (view only), and non-selectable
Qt::ItemFlags flags = item->flags();
flags &= ~(Qt::ItemIsEditable); // reset/clear the flag
item->setFlags(flags);
if (not ok)
{
QBrush brush = item->foreground();
brush.setColor(Qt::red);
item->setForeground(brush);
}
table->setItem(row, column, item);
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogIncrements::ClearIncrementName(const QString &name) const
{
QString clear = name;
const int index = clear.indexOf(CustomIncrSign);
if (index == 0)
{
clear.remove(0, 1);
}
return clear;
}
//---------------------------------------------------------------------------------------------------------------------
bool DialogIncrements::EvalIncrementFormula(const QString &formula, bool fromUser, VContainer *data, QLabel *label)
{
const QString postfix = VDomDocument::UnitsToStr(qApp->patternUnit());//Show unit in dialog lable (cm, mm or inch)
if (formula.isEmpty())
{
label->setText(tr("Error") + " (" + postfix + "). " + tr("Empty field."));
label->setToolTip(tr("Empty field"));
return false;
}
else
{
try
{
QString f;
// Replace line return character with spaces for calc if exist
if (fromUser)
{
f = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
}
else
{
f = formula;
}
f.replace("\n", " ");
Calculator *cal = new Calculator(data, qApp->patternType());
const qreal result = cal->EvalFormula(f);
delete cal;
label->setText(qApp->LocaleToString(result) + " " + postfix);
label->setToolTip(tr("Value"));
return true;
}
catch (qmu::QmuParserError &e)
{
label->setText(tr("Error") + " (" + postfix + "). " + tr("Parser error: %1").arg(e.GetMsg()));
label->setToolTip(tr("Parser error: %1").arg(e.GetMsg()));
return false;
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::Controls()
{
if (ui->tableWidgetIncrement->rowCount() > 0)
{
ui->toolButtonRemove->setEnabled(true);
}
else
{
ui->toolButtonRemove->setEnabled(false);
}
if (ui->tableWidgetIncrement->rowCount() >= 2)
{
if (ui->tableWidgetIncrement->currentRow() == 0)
{
ui->toolButtonUp->setEnabled(false);
ui->toolButtonDown->setEnabled(true);
}
else if (ui->tableWidgetIncrement->currentRow() == ui->tableWidgetIncrement->rowCount()-1)
{
ui->toolButtonUp->setEnabled(true);
ui->toolButtonDown->setEnabled(false);
}
else
{
ui->toolButtonUp->setEnabled(true);
ui->toolButtonDown->setEnabled(true);
}
}
else
{
ui->toolButtonUp->setEnabled(false);
ui->toolButtonDown->setEnabled(false);
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::EnableDetails(bool enabled)
{
if (enabled)
{
Controls();
}
else
{
ui->toolButtonRemove->setEnabled(enabled);
ui->toolButtonUp->setEnabled(enabled);
ui->toolButtonDown->setEnabled(enabled);
}
if (not enabled)
{ // Clear
ui->lineEditName->blockSignals(true);
ui->lineEditName->clear();
ui->lineEditName->blockSignals(false);
ui->plainTextEditDescription->blockSignals(true);
ui->plainTextEditDescription->clear();
ui->plainTextEditDescription->blockSignals(false);
ui->labelCalculatedValue->blockSignals(true);
ui->labelCalculatedValue->clear();
ui->labelCalculatedValue->blockSignals(false);
ui->plainTextEditFormula->blockSignals(true);
ui->plainTextEditFormula->clear();
ui->plainTextEditFormula->blockSignals(false);
}
ui->pushButtonGrow->setEnabled(enabled);
ui->toolButtonExpr->setEnabled(enabled);
ui->lineEditName->setEnabled(enabled);
ui->plainTextEditDescription->setEnabled(enabled);
ui->plainTextEditFormula->setEnabled(enabled);
}
//---------------------------------------------------------------------------------------------------------------------
@ -294,11 +423,7 @@ void DialogIncrements::ShowSuccess() const
*/
void DialogIncrements::FullUpdateFromFile()
{
ui->tableWidgetIncrement->blockSignals(true);
ui->tableWidgetIncrement->clearContents();
FillIncrements();
ui->tableWidgetIncrement->horizontalHeader()->setStretchLastSection(true);
ui->tableWidgetIncrement->blockSignals(false);
ui->tableWidgetLines->clearContents();
FillLengthsLines();
@ -314,217 +439,327 @@ void DialogIncrements::FullUpdateFromFile()
/**
* @brief clickedToolButtonAdd create new row in table
*/
void DialogIncrements::clickedToolButtonAdd()
void DialogIncrements::AddIncrement()
{
qCDebug(vDialog, "Add new increment");
ui->tableWidgetIncrement->setFocus(Qt::OtherFocusReason);
ui->tableWidgetIncrement->blockSignals(true);
qint32 currentRow = ui->tableWidgetIncrement->rowCount();
ui->tableWidgetIncrement->insertRow( currentRow );
qCDebug(vDialog, "Add a new increment");
qint32 num = 1;
QString name;
do
{
name = QString(tr("Name_%1")).arg(num);
name = CustomIncrSign + tr("Increment_%1").arg(num);
num++;
} while (data->IsUnique(name)==false);
const quint32 id = data->getNextId();
const QString description(tr("Description"));
VIncrement *incr = new VIncrement(name, id, 0, description);
data->AddVariable(name, incr);
qint32 currentRow;
AddIncrementToFile(id, name, 0, 0, 0, description);
if (ui->tableWidgetIncrement->currentRow() == -1)
{
currentRow = ui->tableWidgetIncrement->rowCount();
doc->AddEmptyIncrement(name);
}
else
{
currentRow = ui->tableWidgetIncrement->currentRow()+1;
QTableWidgetItem *nameField = ui->tableWidgetIncrement->item(ui->tableWidgetIncrement->currentRow(), 0);
doc->AddEmptyIncrementAfter(nameField->text(), name);
}
QTableWidgetItem *item = new QTableWidgetItem(name);
item->setTextAlignment(Qt::AlignHCenter);
item->setFont(QFont("Times", 12, QFont::Bold));
item->setData(Qt::UserRole, id);
ui->tableWidgetIncrement->setItem(currentRow, 0, item);
ui->tableWidgetIncrement->setCurrentCell(currentRow, 0, QItemSelectionModel::ClearAndSelect);
item = new QTableWidgetItem("0");// calculated value
item->setTextAlignment(Qt::AlignHCenter);
// set the item non-editable (view only), and non-selectable
Qt::ItemFlags flags = item->flags();
flags &= ~(Qt::ItemIsSelectable | Qt::ItemIsEditable); // reset/clear the flag
item->setFlags(flags);
ui->tableWidgetIncrement->setItem(currentRow, 1, item);
item = new QTableWidgetItem("0");// base value
item->setTextAlignment(Qt::AlignHCenter);
ui->tableWidgetIncrement->setItem(currentRow, 2, item);
item = new QTableWidgetItem("0");// in sizes
item->setTextAlignment(Qt::AlignHCenter);
ui->tableWidgetIncrement->setItem(currentRow, 3, item);
item = new QTableWidgetItem("0"); // in heights
item->setTextAlignment(Qt::AlignHCenter);
ui->tableWidgetIncrement->setItem(currentRow, 4, item);
item = new QTableWidgetItem(description);
item->setTextAlignment(Qt::AlignLeft);
ui->tableWidgetIncrement->setItem(currentRow, 5, item);
ui->toolButtonRemove->setEnabled(true);
ui->tableWidgetIncrement->blockSignals(false);
emit haveLiteChange();
ShowSuccess();
FullUpdateTree(Document::LiteParse);
ui->tableWidgetIncrement->selectRow(currentRow);
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief clickedToolButtonRemove remove one row from table
*/
void DialogIncrements::clickedToolButtonRemove()
void DialogIncrements::RemoveIncrement()
{
ui->tableWidgetIncrement->blockSignals(true);
const int row = ui->tableWidgetIncrement->currentRow();
QTableWidgetItem *item = ui->tableWidgetIncrement->currentItem();
qint32 row = item->row();
QTableWidgetItem *itemName = ui->tableWidgetIncrement->item(row, 0);
qCDebug(vDialog, "Remove increment %s", itemName->text().toUtf8().constData());
data->RemoveIncrement(itemName->text());
quint32 id = qvariant_cast<quint32>(itemName->data(Qt::UserRole));
QDomElement domElement = doc->elementById(id);
if (domElement.isElement())
if (row == -1)
{
QDomNodeList list = doc->elementsByTagName(VPattern::TagIncrements);
list.at(0).removeChild(domElement);
return;
}
QTableWidgetItem *nameField = ui->tableWidgetIncrement->item(row, 0);
doc->RemoveIncrement(nameField->text());
FullUpdateTree(Document::LiteParse);
if (ui->tableWidgetIncrement->rowCount() > 0)
{
ui->tableWidgetIncrement->selectRow(0);
}
else
{
qCDebug(vDialog, "Could not find object with id %u", id);
EnableDetails(false);
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::MoveUp()
{
const int row = ui->tableWidgetIncrement->currentRow();
if (row == -1)
{
return;
}
ui->tableWidgetIncrement->removeRow(row);
if (ui->tableWidgetIncrement->rowCount() == 0)
QTableWidgetItem *nameField = ui->tableWidgetIncrement->item(row, 0);
doc->MoveUpIncrement(nameField->text());
FullUpdateTree(Document::LiteParse);
ui->tableWidgetIncrement->selectRow(row-1);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::MoveDown()
{
const int row = ui->tableWidgetIncrement->currentRow();
if (row == -1)
{
ui->toolButtonRemove->setEnabled(false);
return;
}
QTableWidgetItem *nameField = ui->tableWidgetIncrement->item(row, 0);
doc->MoveDownIncrement(nameField->text());
FullUpdateTree(Document::LiteParse);
ui->tableWidgetIncrement->selectRow(row+1);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::SaveIncrName()
{
const int row = ui->tableWidgetIncrement->currentRow();
if (row == -1)
{
return;
}
QTableWidgetItem *nameField = ui->tableWidgetIncrement->item(row, 0);
const QString newName = CustomIncrSign + ui->lineEditName->text();
if (data->IsUnique(newName))
{
doc->SetIncrementName(nameField->text(), newName);
FullUpdateTree(Document::LiteParse);
ui->tableWidgetIncrement->blockSignals(true);
ui->tableWidgetIncrement->selectRow(row);
ui->tableWidgetIncrement->blockSignals(false);
}
else
{
ui->lineEditName->setText(ClearIncrementName(nameField->text()));
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::SaveIncrDescription()
{
const int row = ui->tableWidgetIncrement->currentRow();
if (row == -1)
{
return;
}
QTableWidgetItem *nameField = ui->tableWidgetIncrement->item(row, 0);
doc->SetIncrementDescription(nameField->text(), ui->plainTextEditDescription->toPlainText());
FullUpdateTree(Document::LiteParse);
const QTextCursor cursor = ui->plainTextEditDescription->textCursor();
ui->tableWidgetIncrement->blockSignals(true);
ui->tableWidgetIncrement->selectRow(row);
ui->tableWidgetIncrement->blockSignals(false);
emit haveLiteChange();
ShowSuccess();
ui->plainTextEditDescription->setTextCursor(cursor);
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief AddIncrementToFile save created increment to file
* @param id id of increment
* @param name name
* @param base base value
* @param ksize increment in sizes
* @param kheight increment in heights
* @param description description of increment
*/
void DialogIncrements::AddIncrementToFile(const quint32 &id, const QString &name, const qreal &base, const qreal &ksize,
const qreal &kheight, const QString &description)
void DialogIncrements::SaveIncrFormula()
{
qCDebug(vDialog, "Saving new increment to file.");
qCDebug(vDialog, "%s", QString("Increment: id(%1), name(%2), base(%3), ksize(%4), kheight(%5), description(%6)")
.arg(id).arg(name).arg(base).arg(ksize).arg(kheight).arg(description).toUtf8().constData());
QDomElement element = doc->createElement(VPattern::TagIncrement);
const int row = ui->tableWidgetIncrement->currentRow();
doc->SetAttribute(element, VDomDocument::AttrId, id);
doc->SetAttribute(element, VPattern::IncrementName, name);
doc->SetAttribute(element, VPattern::IncrementBase, base);
doc->SetAttribute(element, VPattern::IncrementKsize, ksize);
doc->SetAttribute(element, VPattern::IncrementKgrowth, kheight);
doc->SetAttribute(element, VPattern::IncrementDescription, description);
QDomNodeList list = doc->elementsByTagName(VPattern::TagIncrements);
list.at(0).appendChild(element);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::HideColumns(QTableWidget *table)
{
SCASSERT(table != nullptr);
table->setColumnHidden( 1, true );// calculated value
table->setColumnHidden( 3, true );// in sizes
table->setColumnHidden( 4, true );// in heights
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief cellChanged cell in table was changed
* @param row number of row
* @param column number of column
*/
void DialogIncrements::IncrementChanged ( qint32 row, qint32 column )
{
qCDebug(vDialog, "Increment changed.");
const QTableWidgetItem *itemName = ui->tableWidgetIncrement->item(row, 0);
const QTableWidgetItem *item = ui->tableWidgetIncrement->item(row, column);
const quint32 id = qvariant_cast<quint32>(itemName->data(Qt::UserRole));
QDomElement domElement = doc->elementById(id);
if (domElement.isElement() == false)
if (row == -1)
{
qCDebug(vDialog, "Cant't find increment with id = %u", id);
return;
}
this->row = row;
switch (column)
QTableWidgetItem *nameField = ui->tableWidgetIncrement->item(row, 0);
// Replace line return character with spaces for calc if exist
QString text = ui->plainTextEditFormula->toPlainText();
text.replace("\n", " ");
QTableWidgetItem *formulaField = ui->tableWidgetIncrement->item(row, 2);
if (formulaField->text() == text)
{
case 0: // VPattern::IncrementName
qCDebug(vDialog, "Changed name to %s", item->text().toUtf8().constData());
doc->SetAttribute(domElement, VPattern::IncrementName, item->text());
data->ClearVariables(VarType::Increment);
this->column = 2;
emit FullUpdateTree(Document::LiteParse);
break;
case 2: // VPattern::IncrementBase
qCDebug(vDialog, "Changed base to %s", item->text().toUtf8().constData());
doc->SetAttribute(domElement, VPattern::IncrementBase, item->text());
this->column = 3;
emit FullUpdateTree(Document::LiteParse);
break;
case 3: // VPattern::IncrementKsize
qCDebug(vDialog, "Changed ksize to %s", item->text().toUtf8().constData());
doc->SetAttribute(domElement, VPattern::IncrementKsize, item->text());
this->column = 4;
emit FullUpdateTree(Document::LiteParse);
break;
case 4: // VPattern::IncrementKgrowth
qCDebug(vDialog, "Changed kheight to %s", item->text().toUtf8().constData());
doc->SetAttribute(domElement, VPattern::IncrementKgrowth, item->text());
this->column = 5;
emit FullUpdateTree(Document::LiteParse);
break;
case 5: // VPattern::IncrementDescription
{
qCDebug(vDialog, "Changed description to %s", item->text().toUtf8().constData());
doc->SetAttribute(domElement, VPattern::IncrementDescription, item->text());
QSharedPointer<VIncrement> incr = data->GetVariable<VIncrement>(itemName->text());
incr->SetDescription(item->text());
ui->tableWidgetIncrement->resizeColumnsToContents();
ui->tableWidgetIncrement->resizeRowsToContents();
this->column = 0;
ui->tableWidgetIncrement->setCurrentCell( row, this->column );
ui->tableWidgetIncrement->horizontalHeader()->setStretchLastSection(true);
break;
}
default:
break;
QTableWidgetItem *result = ui->tableWidgetIncrement->item(row, 1);
//Show unit in dialog lable (cm, mm or inch)
const QString postfix = VDomDocument::UnitsToStr(qApp->patternUnit());
ui->labelCalculatedValue->setText(result->text() + " " +postfix);
return;
}
emit haveLiteChange();
ShowSuccess();
if (text.isEmpty())
{
//Show unit in dialog lable (cm, mm or inch)
const QString postfix = VDomDocument::UnitsToStr(qApp->patternUnit());
ui->labelCalculatedValue->setText(tr("Error") + " (" + postfix + "). " + tr("Empty field."));
return;
}
QSharedPointer<VIncrement> incr = data->GetVariable<VIncrement>(nameField->text());
const bool ok = EvalIncrementFormula(text, true, incr->GetData(), ui->labelCalculatedValue);
if (not ok)
{
return;
}
try
{
const QString formula = qApp->TrVars()->FormulaFromUser(text, qApp->Settings()->GetOsSeparator());
doc->SetIncrementFormula(nameField->text(), formula);
}
catch (qmu::QmuParserError &e) // Just in case something bad happens
{
Q_UNUSED(e)
return;
}
FullUpdateTree(Document::LiteParse);
const QTextCursor cursor = ui->plainTextEditFormula->textCursor();
ui->tableWidgetIncrement->blockSignals(true);
ui->tableWidgetIncrement->selectRow(row);
ui->tableWidgetIncrement->blockSignals(false);
ui->plainTextEditFormula->setTextCursor(cursor);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::DeployFormula()
{
SCASSERT(ui->plainTextEditFormula != nullptr);
SCASSERT(ui->pushButtonGrow != nullptr)
if (ui->plainTextEditFormula->height() < DIALOG_MAX_FORMULA_HEIGHT)
{
ui->plainTextEditFormula->setFixedHeight(DIALOG_MAX_FORMULA_HEIGHT);
//Set icon from theme (internal for Windows system)
ui->pushButtonGrow->setIcon(QIcon::fromTheme("go-next",
QIcon(":/icons/win.icon.theme/16x16/actions/go-next.png")));
}
else
{
ui->plainTextEditFormula->setFixedHeight(formulaBaseHeight);
//Set icon from theme (internal for Windows system)
ui->pushButtonGrow->setIcon(QIcon::fromTheme("go-down",
QIcon(":/icons/win.icon.theme/16x16/actions/go-down.png")));
}
// I found that after change size of formula field, it was filed for angle formula, field for formula became black.
// This code prevent this.
setUpdatesEnabled(false);
repaint();
setUpdatesEnabled(true);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::Fx()
{
const int row = ui->tableWidgetIncrement->currentRow();
if (row == -1)
{
return;
}
QTableWidgetItem *nameField = ui->tableWidgetIncrement->item(row, 0);
QSharedPointer<VIncrement> incr = data->GetVariable<VIncrement>(nameField->text());
DialogEditWrongFormula *dialog = new DialogEditWrongFormula(incr->GetData(), NULL_ID, this);
dialog->setWindowTitle(tr("Edit increment"));
QString text = ui->plainTextEditFormula->toPlainText();
text.replace("\n", " ");
text = qApp->TrVars()->FormulaFromUser(text, qApp->Settings()->GetOsSeparator());
dialog->SetFormula(text);
const QString postfix = VDomDocument::UnitsToStr(qApp->patternUnit(), true);
dialog->setPostfix(postfix);//Show unit in dialog lable (cm, mm or inch)
if (dialog->exec() == QDialog::Accepted)
{
doc->SetIncrementFormula(nameField->text(), dialog->GetFormula());
FullUpdateTree(Document::LiteParse);
ui->tableWidgetIncrement->selectRow(row);
}
delete dialog;
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::closeEvent(QCloseEvent *event)
{
ui->plainTextEditFormula->blockSignals(true);
ui->lineEditName->blockSignals(true);
ui->plainTextEditDescription->blockSignals(true);
emit DialogClosed(QDialog::Accepted);
event->accept();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogIncrements::ShowIncrementDetails()
{
if (ui->tableWidgetIncrement->rowCount() > 0)
{
EnableDetails(true);
QTableWidgetItem *nameField = ui->tableWidgetIncrement->item(ui->tableWidgetIncrement->currentRow(), 0); // name
QSharedPointer<VIncrement> incr;
try
{
incr = data->GetVariable<VIncrement>(nameField->text());
}
catch(const VExceptionBadId &e)
{
Q_UNUSED(e);
EnableDetails(false);
return;
}
ui->lineEditName->blockSignals(true);
ui->lineEditName->setText(ClearIncrementName(incr->GetName()));
ui->lineEditName->blockSignals(false);
ui->plainTextEditDescription->blockSignals(true);
ui->plainTextEditDescription->setPlainText(incr->GetDescription());
ui->plainTextEditDescription->blockSignals(false);
EvalIncrementFormula(incr->GetFormula(), false, incr->GetData(), ui->labelCalculatedValue);
ui->plainTextEditFormula->blockSignals(true);
QString formula;
try
{
formula = qApp->TrVars()->FormulaToUser(incr->GetFormula());
}
catch (qmu::QmuParserError &e)
{
Q_UNUSED(e);
formula = incr->GetFormula();
}
ui->plainTextEditFormula->setPlainText(formula);
ui->plainTextEditFormula->blockSignals(false);
}
else
{
EnableDetails(false);
}
}
//---------------------------------------------------------------------------------------------------------------------
DialogIncrements::~DialogIncrements()
{

View File

@ -49,23 +49,30 @@ class DialogIncrements : public DialogTool
Q_OBJECT
public:
DialogIncrements(VContainer *data, VPattern *doc, QWidget *parent = nullptr);
~DialogIncrements();
public slots:
void clickedToolButtonAdd();
void clickedToolButtonRemove();
void IncrementChanged ( qint32 row, qint32 column );
void FullUpdateFromFile();
virtual ~DialogIncrements() Q_DECL_OVERRIDE;
signals:
/**
* @brief FullUpdateTree signal update data for dom document
*/
void FullUpdateTree(const Document &parse);
/**
* @brief haveLiteChange signal show sign of change
*/
void haveLiteChange();
protected:
virtual void closeEvent ( QCloseEvent * event ) Q_DECL_OVERRIDE;
private slots:
void ShowIncrementDetails();
void AddIncrement();
void RemoveIncrement();
void MoveUp();
void MoveDown();
void SaveIncrName();
void SaveIncrDescription();
void SaveIncrFormula();
void DeployFormula();
void Fx();
void FullUpdateFromFile();
private:
Q_DISABLE_COPY(DialogIncrements)
@ -78,11 +85,7 @@ private:
/** @brief doc dom document container */
VPattern *doc;
/** @brief row save number of row current selected cell */
qint32 row;
/** @brief column save number of column current selected cell */
qint32 column;
int formulaBaseHeight;
template <typename T>
void FillTable(const QMap<QString, T> varTable, QTableWidget *table);
@ -96,13 +99,15 @@ private:
void FillAnglesArcs();
void FillAnglesCurves();
void AddIncrementToFile(const quint32 &id, const QString &name, const qreal &base,
const qreal &ksize, const qreal &kheight, const QString &description);
void HideColumns(QTableWidget *table);
void SetItemViewOnly(QTableWidgetItem *item);
void ShowUnits();
void ShowHeaderUnits(QTableWidget *table, int column, const QString &unit);
void ShowSuccess() const;
void AddCell(QTableWidget *table, const QString &text, int row, int column, int aligment, bool ok = true);
QString ClearIncrementName(const QString &name) const;
bool EvalIncrementFormula(const QString &formula, bool fromUser, VContainer *data, QLabel *label);
void Controls();
void EnableDetails(bool enabled);
};
#endif // DIALOGINCREMENTS_H

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>979</width>
<height>574</height>
<height>680</height>
</rect>
</property>
<property name="minimumSize">
@ -45,108 +45,334 @@
<attribute name="title">
<string>Increments</string>
</attribute>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<widget class="QToolButton" name="toolButtonRemove">
<property name="enabled">
<bool>false</bool>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="text">
<string notr="true">...</string>
</property>
<property name="icon">
<iconset theme="list-remove">
<normaloff/>
</iconset>
</property>
</widget>
</item>
<item row="0" column="0" colspan="3">
<widget class="QTableWidget" name="tableWidgetIncrement">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="sortingEnabled">
<bool>false</bool>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<attribute name="horizontalHeaderCascadingSectionResizes">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderDefaultSectionSize">
<number>120</number>
</attribute>
<attribute name="horizontalHeaderMinimumSectionSize">
<number>70</number>
</attribute>
<attribute name="horizontalHeaderShowSortIndicator" stdset="0">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderCascadingSectionResizes">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderDefaultSectionSize">
<number>25</number>
</attribute>
<attribute name="verticalHeaderMinimumSectionSize">
<number>8</number>
</attribute>
<attribute name="verticalHeaderStretchLastSection">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string>Name</string>
<widget class="QTableWidget" name="tableWidgetIncrement">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>8</verstretch>
</sizepolicy>
</property>
</column>
<column>
<property name="text">
<string>The calculated value</string>
<property name="minimumSize">
<size>
<width>0</width>
<height>150</height>
</size>
</property>
</column>
<column>
<property name="text">
<string>Base value</string>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
</column>
<column>
<property name="text">
<string>In sizes</string>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
</column>
<column>
<property name="text">
<string>In heights</string>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
</column>
<column>
<property name="text">
<string>Description</string>
<property name="sortingEnabled">
<bool>false</bool>
</property>
</column>
</widget>
</item>
<item row="1" column="0">
<widget class="QToolButton" name="toolButtonAdd">
<property name="text">
<string notr="true">...</string>
</property>
<property name="icon">
<iconset theme="list-add">
<normaloff/>
</iconset>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<attribute name="horizontalHeaderCascadingSectionResizes">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderDefaultSectionSize">
<number>120</number>
</attribute>
<attribute name="horizontalHeaderMinimumSectionSize">
<number>70</number>
</attribute>
<attribute name="horizontalHeaderShowSortIndicator" stdset="0">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderCascadingSectionResizes">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderDefaultSectionSize">
<number>25</number>
</attribute>
<attribute name="verticalHeaderMinimumSectionSize">
<number>8</number>
</attribute>
<attribute name="verticalHeaderStretchLastSection">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string>Name</string>
</property>
</column>
<column>
<property name="text">
<string>The calculated value</string>
</property>
</column>
<column>
<property name="text">
<string>Formula</string>
</property>
</column>
</widget>
<widget class="QGroupBox" name="groupBoxDetails">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>4</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>236</height>
</size>
</property>
<property name="toolTip">
<string>Details</string>
</property>
<property name="title">
<string>Details</string>
</property>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
</property>
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item alignment="Qt::AlignLeft">
<widget class="QToolButton" name="toolButtonUp">
<property name="enabled">
<bool>false</bool>
</property>
<property name="toolTip">
<string>Move measurement up</string>
</property>
<property name="text">
<string notr="true">...</string>
</property>
<property name="icon">
<iconset theme="go-up">
<normaloff>../../tape</normaloff>../../tape</iconset>
</property>
</widget>
</item>
<item alignment="Qt::AlignLeft">
<widget class="QToolButton" name="toolButtonDown">
<property name="enabled">
<bool>false</bool>
</property>
<property name="toolTip">
<string>Move measurement down</string>
</property>
<property name="text">
<string notr="true">...</string>
</property>
<property name="icon">
<iconset theme="go-down">
<normaloff>../../tape</normaloff>../../tape</iconset>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="spacing">
<number>6</number>
</property>
<item alignment="Qt::AlignRight">
<widget class="QToolButton" name="toolButtonAdd">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string notr="true">...</string>
</property>
<property name="icon">
<iconset theme="list-add">
<normaloff/>
</iconset>
</property>
</widget>
</item>
<item alignment="Qt::AlignHCenter">
<widget class="QToolButton" name="toolButtonRemove">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string notr="true">...</string>
</property>
<property name="icon">
<iconset theme="list-remove">
<normaloff/>
</iconset>
</property>
</widget>
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="lineEditName">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="labelCalculated">
<property name="text">
<string>Calculated value:</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLabel" name="labelCalculatedValue">
<property name="text">
<string notr="true"/>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="labelFormula">
<property name="text">
<string>Formula:</string>
</property>
</widget>
</item>
<item row="5" column="1">
<layout class="QHBoxLayout" name="horizontalLayoutValue">
<item>
<widget class="QPlainTextEdit" name="plainTextEditFormula">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>24</height>
</size>
</property>
<property name="tabChangesFocus">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonGrow">
<property name="enabled">
<bool>false</bool>
</property>
<property name="maximumSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
<property name="sizeIncrement">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Show full calculation in message box&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string notr="true"/>
</property>
<property name="icon">
<iconset theme="go-down">
<normaloff>../../../libs/vtools/dialogs/support</normaloff>../../../libs/vtools/dialogs/support</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolButtonExpr">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string notr="true">...</string>
</property>
<property name="icon">
<iconset resource="../../../libs/vmisc/share/resources/icon.qrc">
<normaloff>:/icon/24x24/fx.png</normaloff>:/icon/24x24/fx.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Description:</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QPlainTextEdit" name="plainTextEditDescription">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>

View File

@ -45,6 +45,7 @@
#include "../qmuparser/qmuparsererror.h"
#include "../vgeometry/varc.h"
#include "../core/vapplication.h"
#include "../vpatterndb/calculator.h"
#include <QMessageBox>
#include <QUndoStack>
@ -2004,6 +2005,67 @@ void VPattern::ParseToolArcWithLength(VMainGraphicsScene *scene, QDomElement &do
}
}
//---------------------------------------------------------------------------------------------------------------------
qreal VPattern::EvalFormula(VContainer *data, const QString &formula, bool *ok) const
{
if (formula.isEmpty())
{
*ok = true;
return 0;
}
else
{
try
{
// Replace line return character with spaces for calc if exist
QString f = formula;
f.replace("\n", " ");
Calculator *cal = new Calculator(data, qApp->patternType());
const qreal result = cal->EvalFormula(f);
delete cal;
*ok = true;
return result;
}
catch (qmu::QmuParserError &e)
{
*ok = false;
return 0;
}
}
}
//---------------------------------------------------------------------------------------------------------------------
QDomElement VPattern::MakeEmptyIncrement(const QString &name)
{
QDomElement element = createElement(TagIncrement);
SetAttribute(element, IncrementName, name);
SetAttribute(element, IncrementFormula, QString("0"));
SetAttribute(element, IncrementDescription, QString(""));
return element;
}
//---------------------------------------------------------------------------------------------------------------------
QDomElement VPattern::FindIncrement(const QString &name) const
{
QDomNodeList list = elementsByTagName(TagIncrement);
for (int i=0; i < list.size(); ++i)
{
const QDomElement domElement = list.at(i).toElement();
if (domElement.isNull() == false)
{
const QString parameter = domElement.attribute(IncrementName);
if (parameter == name)
{
return domElement;
}
}
}
return QDomElement();
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief ParseSplineElement parse spline tag.
@ -2135,6 +2197,7 @@ void VPattern::ParseToolsElement(VMainGraphicsScene *scene, const QDomElement &d
*/
void VPattern::ParseIncrementsElement(const QDomNode &node)
{
int index = 0;
QDomNode domNode = node.firstChild();
while (domNode.isNull() == false)
{
@ -2145,14 +2208,24 @@ void VPattern::ParseIncrementsElement(const QDomNode &node)
{
if (domElement.tagName() == TagIncrement)
{
const quint32 id = GetParametrId(domElement);
const QString name = GetParametrString(domElement, IncrementName, "");
const qreal base = GetParametrDouble(domElement, IncrementBase, "0");
const qreal ksize = GetParametrDouble(domElement, IncrementKsize, "0");
const qreal kgrowth = GetParametrDouble(domElement, IncrementKgrowth, "0");
const QString desc = GetParametrString(domElement, IncrementDescription, "Description");
data->UpdateId(id);
data->AddVariable(name, new VIncrement(name, id, base, desc));
QString desc;
try
{
desc = GetParametrString(domElement, IncrementDescription);
}
catch (VExceptionEmptyParameter &e)
{
Q_UNUSED(e)
}
const QString formula = GetParametrString(domElement, IncrementFormula, "0");
bool ok = false;
const qreal value = EvalFormula(data, formula, &ok);
data->AddVariable(name, new VIncrement(data, name, index, value, formula, ok, desc));
++index;
}
}
}
@ -2174,6 +2247,108 @@ void VPattern::SetAuthor(const QString &text)
emit patternChanged(false);
}
//---------------------------------------------------------------------------------------------------------------------
void VPattern::AddEmptyIncrement(const QString &name)
{
const QDomElement element = MakeEmptyIncrement(name);
const QDomNodeList list = elementsByTagName(TagIncrements);
list.at(0).appendChild(element);
emit patternChanged(false);
}
//---------------------------------------------------------------------------------------------------------------------
void VPattern::AddEmptyIncrementAfter(const QString &after, const QString &name)
{
const QDomElement element = MakeEmptyIncrement(name);
const QDomElement sibling = FindIncrement(after);
const QDomNodeList list = elementsByTagName(TagIncrements);
if (sibling.isNull())
{
list.at(0).appendChild(element);
}
else
{
list.at(0).insertAfter(element, sibling);
}
emit patternChanged(false);
}
//---------------------------------------------------------------------------------------------------------------------
void VPattern::RemoveIncrement(const QString &name)
{
const QDomNodeList list = elementsByTagName(TagIncrements);
list.at(0).removeChild(FindIncrement(name));
emit patternChanged(false);
}
//---------------------------------------------------------------------------------------------------------------------
void VPattern::MoveUpIncrement(const QString &name)
{
const QDomElement node = FindIncrement(name);
if (not node.isNull())
{
const QDomElement prSibling = node.previousSiblingElement(TagIncrement);
if (not prSibling.isNull())
{
const QDomNodeList list = elementsByTagName(TagIncrements);
list.at(0).insertBefore(node, prSibling);
}
}
emit patternChanged(false);
}
//---------------------------------------------------------------------------------------------------------------------
void VPattern::MoveDownIncrement(const QString &name)
{
const QDomElement node = FindIncrement(name);
if (not node.isNull())
{
const QDomElement nextSibling = node.nextSiblingElement(TagIncrement);
if (not nextSibling.isNull())
{
const QDomNodeList list = elementsByTagName(TagIncrements);
list.at(0).insertAfter(node, nextSibling);
}
}
emit patternChanged(false);
}
//---------------------------------------------------------------------------------------------------------------------
void VPattern::SetIncrementName(const QString &name, const QString &text)
{
QDomElement node = FindIncrement(name);
if (not node.isNull())
{
SetAttribute(node, IncrementName, text);
emit patternChanged(false);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPattern::SetIncrementFormula(const QString &name, const QString &text)
{
QDomElement node = FindIncrement(name);
if (not node.isNull())
{
SetAttribute(node, IncrementFormula, text);
emit patternChanged(false);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPattern::SetIncrementDescription(const QString &name, const QString &text)
{
QDomElement node = FindIncrement(name);
if (not node.isNull())
{
SetAttribute(node, IncrementDescription, text);
emit patternChanged(false);
}
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GenerateLabel create label for pattern piece of point.
@ -2266,6 +2441,7 @@ void VPattern::PrepareForParse(const Document &parse)
else if (parse == Document::LiteParse)
{
data->ClearUniqueNames();
data->ClearVariables(VarType::Increment);
data->ClearVariables(VarType::ArcLength);
data->ClearVariables(VarType::LineAngle);
data->ClearVariables(VarType::LineLength);

View File

@ -65,6 +65,16 @@ public:
QString GetAuthor() const;
void SetAuthor(const QString &text);
void AddEmptyIncrement(const QString &name);
void AddEmptyIncrementAfter(const QString &after, const QString &name);
void RemoveIncrement(const QString &name);
void MoveUpIncrement(const QString &name);
void MoveDownIncrement(const QString &name);
void SetIncrementName(const QString &name, const QString &text);
void SetIncrementFormula(const QString &name, const QString &text);
void SetIncrementDescription(const QString &name, const QString &text);
virtual QString GenerateLabel(const LabelType &type, const QString &reservedName = QString())const Q_DECL_OVERRIDE;
public slots:
@ -151,6 +161,11 @@ private:
void ParseToolArc(VMainGraphicsScene *scene, QDomElement &domElement, const Document &parse);
void ParseNodeArc(const QDomElement &domElement, const Document &parse);
void ParseToolArcWithLength(VMainGraphicsScene *scene, QDomElement &domElement, const Document &parse);
qreal EvalFormula(VContainer *data, const QString &formula, bool *ok) const;
QDomElement MakeEmptyIncrement(const QString &name);
QDomElement FindIncrement(const QString &name) const;
};
#endif // VPATTERN_H

View File

@ -28,7 +28,8 @@
#include "ifcdef.h"
const QString CustomSign = QStringLiteral("@");
const QString CustomMSign = QStringLiteral("@");
const QString CustomIncrSign = QStringLiteral("#");
#define DefWidth 1.2//mm

View File

@ -32,7 +32,8 @@
#include <QtGlobal>
#include "../vmisc/def.h"
extern const QString CustomSign;
extern const QString CustomMSign;
extern const QString CustomIncrSign;
#ifdef Q_OS_WIN32
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;

View File

@ -97,9 +97,7 @@ const QString VAbstractPattern::AttrS54 = QStringLiteral("s54");
const QString VAbstractPattern::AttrS56 = QStringLiteral("s56");
const QString VAbstractPattern::IncrementName = QStringLiteral("name");
const QString VAbstractPattern::IncrementBase = QStringLiteral("base");
const QString VAbstractPattern::IncrementKsize = QStringLiteral("ksize");
const QString VAbstractPattern::IncrementKgrowth = QStringLiteral("kgrowth");
const QString VAbstractPattern::IncrementFormula = QStringLiteral("formula");
const QString VAbstractPattern::IncrementDescription = QStringLiteral("description");
//---------------------------------------------------------------------------------------------------------------------

View File

@ -168,9 +168,7 @@ public:
static const QString AttrS56;
static const QString IncrementName;
static const QString IncrementBase;
static const QString IncrementKsize;
static const QString IncrementKgrowth;
static const QString IncrementFormula;
static const QString IncrementDescription;
signals:

View File

@ -239,11 +239,8 @@ QmuParserError::QmuParserError ( EErrorCodes iErrc, const QString &sTok, const Q
m_ErrMsg ( QmuParserErrorMsg::Instance() )
{
m_sMsg = m_ErrMsg[m_iErrc];
qDebug()<<m_sMsg;
m_sMsg.replace("$POS$", QString().setNum ( m_iPos ));
qDebug()<<m_sMsg;
m_sMsg.replace("$TOK$", m_sTok );
qDebug()<<m_sMsg;
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -524,7 +524,7 @@ QStringList VMeasurements::ListKnown() const
const QStringList list = ListAll();
for (int i=0; i < list.size(); ++i)
{
if (list.at(i).indexOf(CustomSign) != 0)
if (list.at(i).indexOf(CustomMSign) != 0)
{
listNames.append(list.at(i));
}

View File

@ -137,8 +137,7 @@ void Calculator::InitVariables(const VContainer *data, const QMap<int, QString>
if (vars->contains(i.value()))
{
QSharedPointer<VInternalVariable> var = vars->value(i.value());
if ((patternType == MeasurementsType::Standard) &&
(var->GetType() == VarType::Measurement || var->GetType() == VarType::Increment))
if (patternType == MeasurementsType::Standard && var->GetType() == VarType::Measurement)
{
QSharedPointer<VVariable> m = data->GetVariable<VVariable>(i.value());
m->SetValue(data->size(), data->height(), *data->GetPatternUnit());

View File

@ -47,8 +47,9 @@ VIncrement::VIncrement()
* @param base value
* @param description description of increment
*/
VIncrement::VIncrement(const QString &name, quint32 id, qreal base, QString description)
:VVariable(name, base, description), d(new VIncrementData(id))
VIncrement::VIncrement(VContainer *data, const QString &name, quint32 index, qreal base, const QString &formula,
bool ok, const QString description)
:VVariable(name, base, description), d(new VIncrementData(data, index, formula, ok))
{
SetType(VarType::Increment);
}
@ -76,20 +77,28 @@ VIncrement::~VIncrement()
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief getId return id of row
* @return id
* @brief getIndex return index of row
* @return index
*/
quint32 VIncrement::getId() const
quint32 VIncrement::getIndex() const
{
return d->id;
return d->index;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief setId set id of row
* @param value id
*/
void VIncrement::setId(const quint32 &value)
QString VIncrement::GetFormula() const
{
d->id = value;
return d->formula;
}
//---------------------------------------------------------------------------------------------------------------------
bool VIncrement::IsFormulaOk() const
{
return d->formulaOk;
}
//---------------------------------------------------------------------------------------------------------------------
VContainer *VIncrement::GetData()
{
return &d->data;
}

View File

@ -32,6 +32,7 @@
#include "vvariable.h"
class VIncrementData;
class VContainer;
/**
* @brief The VIncrement class keep data row of increment table
@ -40,13 +41,17 @@ class VIncrement :public VVariable
{
public:
VIncrement();
VIncrement(const QString &name, quint32 id, qreal base, QString description = QString());
VIncrement(VContainer *data, const QString &name, quint32 index, qreal base, const QString &formula, bool ok,
const QString description = QString());
VIncrement(const VIncrement &incr);
VIncrement &operator=(const VIncrement &incr);
virtual ~VIncrement() Q_DECL_OVERRIDE;
quint32 getId() const;
void setId(const quint32 &value);
quint32 getIndex() const;
QString GetFormula() const;
bool IsFormulaOk() const;
VContainer *GetData();
private:
QSharedDataPointer<VIncrementData> d;
};

View File

@ -32,6 +32,7 @@
#include <QSharedData>
#include "../ifc/ifcdef.h"
#include "vcontainer.h"
#ifdef Q_CC_GNU
#pragma GCC diagnostic push
@ -43,21 +44,24 @@ class VIncrementData : public QSharedData
public:
VIncrementData()
:id(NULL_ID)
:index(NULL_ID), formula(QString()), formulaOk(false), data(VContainer(nullptr, nullptr))
{}
VIncrementData(quint32 id)
:id(id)
VIncrementData(VContainer *data, quint32 index, const QString &formula, bool ok)
:index(index), formula(formula), formulaOk(ok), data(*data)
{}
VIncrementData(const VIncrementData &incr)
:QSharedData(incr), id(incr.id)
:QSharedData(incr), index(incr.index), formula(incr.formula), formulaOk(incr.formulaOk), data(incr.data)
{}
virtual ~VIncrementData();
/** @brief id each increment have unique identificator */
quint32 id;
quint32 index;
QString formula;
bool formulaOk;
VContainer data;
};
VIncrementData::~VIncrementData()

View File

@ -219,7 +219,7 @@ QString VMeasurement::GetFormula() const
//---------------------------------------------------------------------------------------------------------------------
bool VMeasurement::IsCustom() const
{
if (GetName().indexOf(CustomSign) == 0)
if (GetName().indexOf(CustomMSign) == 0)
{
return true;
}

View File

@ -58,7 +58,6 @@ public:
virtual ~VMeasurementData();
/** @brief description description measurement */
VContainer data;
quint32 index;
QString formula;

View File

@ -705,6 +705,7 @@ QString VTranslateVars::STDescription(const QString &id) const
* @brief FormulaFromUser replace all known tokens in formula to internal look. Also change decimal
* separator in numbers.
* @param formula expression that need translate
* @throw qmu::QmuParserError in case of a wrong expression
* @return translated expression
*/
QString VTranslateVars::FormulaFromUser(const QString &formula, bool osSeparator) const