valentina/src/libs/vtools/dialogs/tools/dialogflippingbyline.cpp
2020-11-07 15:02:30 +02:00

630 lines
22 KiB
C++

/************************************************************************
**
** @file dialogflippingbyline.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 12 9, 2016
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2016 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "dialogflippingbyline.h"
#include <QColor>
#include <QComboBox>
#include <QCompleter>
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPointF>
#include <QPointer>
#include <QPushButton>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QSharedPointer>
#include <QStringList>
#include <QToolButton>
#include <Qt>
#include <new>
#include "../../visualization/visualization.h"
#include "../../visualization/line/operation/vistoolflippingbyline.h"
#include "../ifc/xml/vabstractpattern.h"
#include "../ifc/xml/vdomdocument.h"
#include "../qmuparser/qmudef.h"
#include "../vgeometry/vpointf.h"
#include "../vmisc/vabstractapplication.h"
#include "../vmisc/vcommonsettings.h"
#include "../vmisc/compatibility.h"
#include "../vpatterndb/vcontainer.h"
#include "../vwidgets/vabstractmainwindow.h"
#include "../vwidgets/vmaingraphicsscene.h"
#include "../vwidgets/vmaingraphicsview.h"
#include "ui_dialogflippingbyline.h"
//---------------------------------------------------------------------------------------------------------------------
DialogFlippingByLine::DialogFlippingByLine(const VContainer *data, quint32 toolId, QWidget *parent)
: DialogTool(data, toolId, parent),
ui(new Ui::DialogFlippingByLine),
stage1(true),
m_suffix(),
flagName(true),
flagGroupName(true),
flagError(false)
{
ui->setupUi(this);
ui->lineEditSuffix->setText(qApp->getCurrentDocument()->GenerateSuffix());
InitOkCancelApply(ui);
FillComboBoxPoints(ui->comboBoxFirstLinePoint);
FillComboBoxPoints(ui->comboBoxSecondLinePoint);
connect(ui->lineEditSuffix, &QLineEdit::textChanged, this, &DialogFlippingByLine::SuffixChanged);
connect(ui->lineEditVisibilityGroup, &QLineEdit::textChanged, this, &DialogFlippingByLine::GroupNameChanged);
connect(ui->comboBoxFirstLinePoint, &QComboBox::currentTextChanged,
this, &DialogFlippingByLine::PointChanged);
connect(ui->comboBoxSecondLinePoint, &QComboBox::currentTextChanged,
this, &DialogFlippingByLine::PointChanged);
vis = new VisToolFlippingByLine(data);
ui->tabWidget->setCurrentIndex(0);
SetTabStopDistance(ui->plainTextEditToolNotes);
}
//---------------------------------------------------------------------------------------------------------------------
DialogFlippingByLine::~DialogFlippingByLine()
{
delete ui;
}
//---------------------------------------------------------------------------------------------------------------------
quint32 DialogFlippingByLine::GetFirstLinePointId() const
{
return getCurrentObjectId(ui->comboBoxFirstLinePoint);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SetFirstLinePointId(quint32 value)
{
ChangeCurrentData(ui->comboBoxFirstLinePoint, value);
VisToolFlippingByLine *operation = qobject_cast<VisToolFlippingByLine *>(vis);
SCASSERT(operation != nullptr)
operation->SetFirstLinePointId(value);
}
//---------------------------------------------------------------------------------------------------------------------
quint32 DialogFlippingByLine::GetSecondLinePointId() const
{
return getCurrentObjectId(ui->comboBoxSecondLinePoint);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SetSecondLinePointId(quint32 value)
{
ChangeCurrentData(ui->comboBoxSecondLinePoint, value);
VisToolFlippingByLine *operation = qobject_cast<VisToolFlippingByLine *>(vis);
SCASSERT(operation != nullptr)
operation->SetSecondLinePointId(value);
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogFlippingByLine::GetSuffix() const
{
return m_suffix;
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SetSuffix(const QString &value)
{
m_suffix = value;
ui->lineEditSuffix->setText(value);
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogFlippingByLine::GetVisibilityGroupName() const
{
return ui->lineEditVisibilityGroup->text();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SetVisibilityGroupName(const QString &name)
{
ui->lineEditVisibilityGroup->setText(name.isEmpty() ? tr("Rotation") : name);
}
//---------------------------------------------------------------------------------------------------------------------
bool DialogFlippingByLine::HasLinkedVisibilityGroup() const
{
return ui->groupBoxVisibilityGroup->isChecked();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SetHasLinkedVisibilityGroup(bool linked)
{
ui->groupBoxVisibilityGroup->setChecked(linked);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SetVisibilityGroupTags(const QStringList &tags)
{
ui->lineEditGroupTags->setText(tags.join(", "));
}
//---------------------------------------------------------------------------------------------------------------------
QStringList DialogFlippingByLine::GetVisibilityGroupTags() const
{
return ui->lineEditGroupTags->text().split(',');
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SetGroupCategories(const QStringList &categories)
{
m_groupTags = categories;
ui->lineEditGroupTags->SetCompletion(m_groupTags);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::ShowDialog(bool click)
{
if (stage1 && not click)
{
if (sourceObjects.isEmpty())
{
return;
}
stage1 = false;
VMainGraphicsScene *scene = qobject_cast<VMainGraphicsScene *>(qApp->getCurrentScene());
SCASSERT(scene != nullptr)
scene->clearSelection();
VisToolFlippingByLine *operation = qobject_cast<VisToolFlippingByLine *>(vis);
SCASSERT(operation != nullptr)
operation->SetObjects(SourceToObjects(sourceObjects));
operation->VisualMode();
scene->ToggleArcSelection(false);
scene->ToggleElArcSelection(false);
scene->ToggleSplineSelection(false);
scene->ToggleSplinePathSelection(false);
scene->ToggleArcHover(false);
scene->ToggleElArcHover(false);
scene->ToggleSplineHover(false);
scene->ToggleSplinePathHover(false);
qApp->getSceneView()->AllowRubberBand(false);
FillSourceList();
emit ToolTip(tr("Select first line point"));
}
else if (not stage1 && prepare && click)
{
setModal(true);
emit ToolTip(QString());
show();
}
}
//---------------------------------------------------------------------------------------------------------------------
QVector<SourceItem> DialogFlippingByLine::GetSourceObjects() const
{
return sourceObjects;
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SetSourceObjects(const QVector<SourceItem> &value)
{
sourceObjects = value;
FillSourceList();
VisToolFlippingByLine *operation = qobject_cast<VisToolFlippingByLine *>(vis);
SCASSERT(operation != nullptr)
operation->SetObjects(SourceToObjects(sourceObjects));
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::ChosenObject(quint32 id, const SceneObject &type)
{
if (not stage1 && not prepare)// After first choose we ignore all objects
{
if (type == SceneObject::Point)
{
auto obj = std::find_if(sourceObjects.begin(), sourceObjects.end(),
[id](const SourceItem &sItem) { return sItem.id == id; });
switch (number)
{
case 0:
if (obj != sourceObjects.end())
{
emit ToolTip(tr("Select first line point that is not part of the list of objects"));
return;
}
if (SetObject(id, ui->comboBoxFirstLinePoint, tr("Select second line point")))
{
number++;
VisToolFlippingByLine *operation = qobject_cast<VisToolFlippingByLine *>(vis);
SCASSERT(operation != nullptr)
operation->SetFirstLinePointId(id);
operation->RefreshGeometry();
}
break;
case 1:
if (obj != sourceObjects.end())
{
emit ToolTip(tr("Select second line point that is not part of the list of objects"));
return;
}
if (getCurrentObjectId(ui->comboBoxFirstLinePoint) != id)
{
if (SetObject(id, ui->comboBoxSecondLinePoint, QString()))
{
if (flagError)
{
number = 0;
prepare = true;
VisToolFlippingByLine *operation = qobject_cast<VisToolFlippingByLine *>(vis);
SCASSERT(operation != nullptr)
operation->SetSecondLinePointId(id);
operation->RefreshGeometry();
}
}
}
break;
default:
break;
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SelectedObject(bool selected, quint32 object, quint32 tool)
{
Q_UNUSED(tool)
if (stage1)
{
auto obj = std::find_if(sourceObjects.begin(), sourceObjects.end(),
[object](const SourceItem &sItem) { return sItem.id == object; });
if (selected)
{
if (obj == sourceObjects.cend())
{
SourceItem item;
item.id = object;
sourceObjects.append(item);
}
}
else
{
if (obj != sourceObjects.end())
{
sourceObjects.erase(obj);
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SuffixChanged()
{
QLineEdit* edit = qobject_cast<QLineEdit*>(sender());
if (edit)
{
const QString suffix = edit->text();
if (suffix.isEmpty())
{
flagName = false;
ChangeColor(ui->labelSuffix, errorColor);
ui->labelStatus->setText(tr("Invalid suffix"));
CheckState();
return;
}
else
{
if (m_suffix != suffix)
{
QRegularExpression rx(NameRegExp());
const QStringList uniqueNames = data->AllUniqueNames();
for (auto &uniqueName : uniqueNames)
{
const QString name = uniqueName + suffix;
if (not rx.match(name).hasMatch() || not data->IsUnique(name))
{
flagName = false;
ChangeColor(ui->labelSuffix, errorColor);
ui->labelStatus->setText(tr("Invalid suffix"));
CheckState();
return;
}
}
}
}
flagName = true;
ChangeColor(ui->labelSuffix, OkColor(this));
}
CheckState();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::GroupNameChanged()
{
QLineEdit* edit = qobject_cast<QLineEdit*>(sender());
if (edit)
{
const QString name = edit->text();
if (name.isEmpty())
{
flagGroupName = false;
ChangeColor(ui->labelGroupName, errorColor);
ui->labelStatus->setText(tr("Invalid group name"));
CheckState();
return;
}
flagGroupName = true;
ChangeColor(ui->labelGroupName, OkColor(this));
}
CheckState();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::ShowSourceDetails(int row)
{
ui->lineEditAlias->setDisabled(true);
if (ui->listWidget->count() == 0)
{
return;
}
const auto sourceItem = qvariant_cast<SourceItem>(ui->listWidget->item(row)->data(Qt::UserRole));
const QSharedPointer<VGObject> obj = data->GetGObject(sourceItem.id);
ui->labelAlias->setText(obj->getType() == GOType::Point ? tr("Label:") : tr("Alias:"));
ui->lineEditAlias->blockSignals(true);
ui->lineEditAlias->setText(sourceItem.alias);
ui->lineEditAlias->setEnabled(true);
ui->lineEditAlias->blockSignals(false);
SetAliasValid(sourceItem.id, SourceAliasValid(sourceItem, obj, data,
OriginAlias(sourceItem.id, sourceObjects, obj)));
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::AliasChanged(const QString &text)
{
if (ui->listWidget->count() == 0)
{
return;
}
if (auto *item = ui->listWidget->currentItem())
{
auto sourceItem = qvariant_cast<SourceItem>(item->data(Qt::UserRole));
sourceItem.alias = text;
item->setData(Qt::UserRole, QVariant::fromValue(sourceItem));
ValidateSourceAliases();
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::ShowVisualization()
{
AddVisualization<VisToolFlippingByLine>();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SaveData()
{
m_suffix = ui->lineEditSuffix->text();
sourceObjects.clear();
sourceObjects.reserve(ui->listWidget->count());
for (int i=0; i<ui->listWidget->count(); ++i)
{
if (const QListWidgetItem *item = ui->listWidget->item(i))
{
auto sourceItem = qvariant_cast<SourceItem>(item->data(Qt::UserRole));
sourceObjects.append(sourceItem);
}
}
VisToolFlippingByLine *operation = qobject_cast<VisToolFlippingByLine *>(vis);
SCASSERT(operation != nullptr)
operation->SetObjects(SourceToObjects(sourceObjects));
operation->SetFirstLinePointId(GetFirstLinePointId());
operation->SetSecondLinePointId(GetSecondLinePointId());
operation->RefreshGeometry();
QStringList groupTags = ui->lineEditGroupTags->text().split(',');
for (auto &tag : groupTags)
{
tag = tag.trimmed();
if (not m_groupTags.contains(tag))
{
m_groupTags.append(tag);
}
}
ui->lineEditGroupTags->SetCompletion(m_groupTags);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::PointChanged()
{
QColor color = OkColor(this);
flagError = true;
ChangeColor(ui->labelFirstLinePoint, color);
ChangeColor(ui->labelSecondLinePoint, color);
quint32 id1 = getCurrentObjectId(ui->comboBoxFirstLinePoint);
auto obj1 = std::find_if(sourceObjects.begin(), sourceObjects.end(),
[id1](const SourceItem &sItem) { return sItem.id == id1; });
quint32 id2 = getCurrentObjectId(ui->comboBoxSecondLinePoint);
auto obj2 = std::find_if(sourceObjects.begin(), sourceObjects.end(),
[id2](const SourceItem &sItem) { return sItem.id == id2; });
if (getCurrentObjectId(ui->comboBoxFirstLinePoint) == getCurrentObjectId(ui->comboBoxSecondLinePoint))
{
flagError = false;
color = errorColor;
ChangeColor(ui->labelFirstLinePoint, color);
ChangeColor(ui->labelSecondLinePoint, color);
ui->labelStatus->setText(tr("Invalid line points"));
}
else if (obj1 != sourceObjects.end())
{
flagError = false;
color = errorColor;
ChangeColor(ui->labelFirstLinePoint, color);
ui->labelStatus->setText(tr("Invalid first line point"));
}
else if (obj2 != sourceObjects.end())
{
flagError = false;
color = errorColor;
ChangeColor(ui->labelSecondLinePoint, color);
ui->labelStatus->setText(tr("Invalid second line point"));
}
CheckState();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::FillSourceList()
{
ui->listWidget->blockSignals(true);
ui->listWidget->clear();
int row = -1;
for (auto &sourceItem : sourceObjects)
{
const QSharedPointer<VGObject> obj = data->GetGObject(sourceItem.id);
bool valid = SourceAliasValid(sourceItem, obj, data, OriginAlias(sourceItem.id, sourceObjects, obj));
auto *item = new QListWidgetItem(valid ? obj->ObjectName() : obj->ObjectName() + '*');
item->setToolTip(obj->ObjectName());
item->setData(Qt::UserRole, QVariant::fromValue(sourceItem));
ui->listWidget->insertItem(++row, item);
}
ui->listWidget->blockSignals(false);
if (ui->listWidget->count() > 0)
{
ui->listWidget->setCurrentRow(0);
}
ValidateSourceAliases();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::ValidateSourceAliases()
{
for (int i=0; i<ui->listWidget->count(); ++i)
{
if (const QListWidgetItem *item = ui->listWidget->item(i))
{
auto sourceItem = qvariant_cast<SourceItem>(item->data(Qt::UserRole));
const QSharedPointer<VGObject> obj = data->GetGObject(sourceItem.id);
if (not SourceAliasValid(sourceItem, obj, data, OriginAlias(sourceItem.id, sourceObjects, obj)))
{
flagAlias = false;
ui->labelStatus->setText(obj->getType() == GOType::Point ? tr("Invalid label") : tr("Invalid alias"));
SetAliasValid(sourceItem.id, false);
CheckState();
return;
}
else
{
SetAliasValid(sourceItem.id, true);
}
}
}
flagAlias = true;
CheckState();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SetAliasValid(quint32 id, bool valid)
{
if (ui->listWidget->currentRow() != -1)
{
auto *item = ui->listWidget->item(ui->listWidget->currentRow());
const auto sourceItem = qvariant_cast<SourceItem>(item->data(Qt::UserRole));
if (id == sourceItem.id)
{
const QSharedPointer<VGObject> obj = data->GetGObject(sourceItem.id);
item->setText(valid ? obj->ObjectName() : obj->ObjectName() + '*');
ChangeColor(ui->labelAlias, valid ? OkColor(this) : errorColor);
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogFlippingByLine::SetNotes(const QString &notes)
{
ui->plainTextEditToolNotes->setPlainText(notes);
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogFlippingByLine::GetNotes() const
{
return ui->plainTextEditToolNotes->toPlainText();
}
//---------------------------------------------------------------------------------------------------------------------
bool DialogFlippingByLine::IsValid() const
{
bool ready = flagError && flagName && flagGroupName && flagAlias;
if (ready)
{
ui->labelStatus->setText(tr("Ready"));
}
return ready;
}