From 000062b7b7ade68bc6a65c470ab34f3e61ece220 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 20 Apr 2019 19:30:35 +0300 Subject: [PATCH] Refactoring. Move static functions to dialogtoolbox.h. --HG-- branch : develop --- src/libs/vtools/dialogs/dialogtoolbox.cpp | 226 ++++++++++++++++++- src/libs/vtools/dialogs/dialogtoolbox.h | 11 + src/libs/vtools/dialogs/tools/dialogtool.cpp | 224 ------------------ src/libs/vtools/dialogs/tools/dialogtool.h | 10 - 4 files changed, 236 insertions(+), 235 deletions(-) diff --git a/src/libs/vtools/dialogs/dialogtoolbox.cpp b/src/libs/vtools/dialogs/dialogtoolbox.cpp index ddf645ff2..fca2f4bbc 100644 --- a/src/libs/vtools/dialogs/dialogtoolbox.cpp +++ b/src/libs/vtools/dialogs/dialogtoolbox.cpp @@ -31,6 +31,8 @@ #include "../vmisc/vabstractapplication.h" #include "../vpatterndb/calculator.h" #include "../vpatterndb/vcontainer.h" +#include "../vpatterndb/vpiecenode.h" +#include "../vgeometry/vpointf.h" #include #include @@ -43,12 +45,79 @@ #include #include #include +#include +#include const QColor errorColor = Qt::red; namespace { - const int dialogMaxFormulaHeight = 80; +const int dialogMaxFormulaHeight = 80; + +//--------------------------------------------------------------------------------------------------------------------- +bool DoublePoint(const VPieceNode &firstNode, const VPieceNode &secondNode, const VContainer *data) +{ + if (firstNode.GetTypeTool() == Tool::NodePoint && not (firstNode.GetId() == NULL_ID) + && secondNode.GetTypeTool() == Tool::NodePoint && not (secondNode.GetId() == NULL_ID)) + { + // don't ignore the same point twice + if (firstNode.GetId() == secondNode.GetId()) + { + return true; + } + + // But ignore the same coordinate if a user wants + if (not firstNode.IsCheckUniqueness() || not secondNode.IsCheckUniqueness()) + { + return false; + } + + try + { + const QSharedPointer firstPoint = data->GeometricObject(firstNode.GetId()); + const QSharedPointer secondPoint = data->GeometricObject(secondNode.GetId()); + + return firstPoint->toQPointF() == secondPoint->toQPointF(); + } + catch(const VExceptionBadId &) + { + return true; + } + } + + return false; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool DoubleCurve(const VPieceNode &firstNode, const VPieceNode &secondNode) +{ + if (firstNode.GetTypeTool() != Tool::NodePoint && not (firstNode.GetId() == NULL_ID) + && secondNode.GetTypeTool() != Tool::NodePoint && not (secondNode.GetId() == NULL_ID)) + { + // don't ignore the same curve twice + if (firstNode.GetId() == secondNode.GetId()) + { + return true; + } + } + + return false; +} +} + +//--------------------------------------------------------------------------------------------------------------------- +VPieceNode RowNode(QListWidget *listWidget, int i) +{ + SCASSERT(listWidget != nullptr); + + if (i < 0 || i >= listWidget->count()) + { + return VPieceNode(); + } + + const QListWidgetItem *rowItem = listWidget->item(i); + SCASSERT(rowItem != nullptr); + return qvariant_cast(rowItem->data(Qt::UserRole)); } //--------------------------------------------------------------------------------------------------------------------- @@ -238,3 +307,158 @@ void CheckPointLabel(QDialog *dialog, QLineEdit* edit, QLabel *labelEditNamePoin ChangeColor(labelEditNamePoint, OkColor(dialog)); } } + +//--------------------------------------------------------------------------------------------------------------------- +int FindNotExcludedNodeDown(QListWidget *listWidget, int candidate) +{ + SCASSERT(listWidget != nullptr); + + int index = -1; + if (candidate < 0 || candidate >= listWidget->count()) + { + return index; + } + + int i = candidate; + VPieceNode rowNode; + do + { + const QListWidgetItem *rowItem = listWidget->item(i); + SCASSERT(rowItem != nullptr); + rowNode = qvariant_cast(rowItem->data(Qt::UserRole)); + + if (not rowNode.IsExcluded()) + { + index = i; + } + + ++i; + } + while (rowNode.IsExcluded() && i < listWidget->count()); + + return index; +} + +//--------------------------------------------------------------------------------------------------------------------- +int FindNotExcludedNodeUp(QListWidget *listWidget, int candidate) +{ + SCASSERT(listWidget != nullptr); + + int index = -1; + if (candidate < 0 || candidate >= listWidget->count()) + { + return index; + } + + int i = candidate; + VPieceNode rowNode; + do + { + const QListWidgetItem *rowItem = listWidget->item(i); + SCASSERT(rowItem != nullptr); + rowNode = qvariant_cast(rowItem->data(Qt::UserRole)); + + if (not rowNode.IsExcluded()) + { + index = i; + } + + --i; + } + while (rowNode.IsExcluded() && i > -1); + + return index; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool FirstPointEqualLast(QListWidget *listWidget, const VContainer *data) +{ + SCASSERT(listWidget != nullptr); + if (listWidget->count() > 1) + { + const VPieceNode topNode = RowNode(listWidget, FindNotExcludedNodeDown(listWidget, 0)); + const VPieceNode bottomNode = RowNode(listWidget, FindNotExcludedNodeUp(listWidget, listWidget->count()-1)); + + return DoublePoint(topNode, bottomNode, data); + } + return false; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool DoublePoints(QListWidget *listWidget, const VContainer *data) +{ + SCASSERT(listWidget != nullptr); + for (int i=0, sz = listWidget->count()-1; icount()-1; i pointLabels; + int countPoints = 0; + for (int i=0; i < listWidget->count(); ++i) + { + const QListWidgetItem *rowItem = listWidget->item(i); + SCASSERT(rowItem != nullptr); + const VPieceNode rowNode = qvariant_cast(rowItem->data(Qt::UserRole)); + if (rowNode.GetTypeTool() == Tool::NodePoint) + { + ++countPoints; + pointLabels.insert(rowNode.GetId()); + } + } + + return countPoints == pointLabels.size(); +} + +//--------------------------------------------------------------------------------------------------------------------- +QString DialogWarningIcon() +{ + const QIcon icon = QIcon::fromTheme("dialog-warning", + QIcon(":/icons/win.icon.theme/16x16/status/dialog-warning.png")); + + const QPixmap pixmap = icon.pixmap(QSize(16, 16)); + QByteArray byteArray; + QBuffer buffer(&byteArray); + pixmap.save(&buffer, "PNG"); + return QStringLiteral(" "); +} + +//--------------------------------------------------------------------------------------------------------------------- +QFont NodeFont(QFont font, bool nodeExcluded) +{ + font.setPointSize(12); + font.setWeight(QFont::Bold); + font.setStrikeOut(nodeExcluded); + return font; +} diff --git a/src/libs/vtools/dialogs/dialogtoolbox.h b/src/libs/vtools/dialogs/dialogtoolbox.h index e46bc3fa3..38ce98a22 100644 --- a/src/libs/vtools/dialogs/dialogtoolbox.h +++ b/src/libs/vtools/dialogs/dialogtoolbox.h @@ -44,6 +44,8 @@ class QWidget; class QColor; class QLineEdit; class VContainer; +class QListWidget; +class VPieceNode; constexpr int formulaTimerTimeout = 300; @@ -65,6 +67,7 @@ struct FormulaData QT_WARNING_POP +VPieceNode RowNode(QListWidget *listWidget, int i); void MoveCursorToEnd(QPlainTextEdit *plainTextEdit); void DeployFormula(QDialog *dialog, QPlainTextEdit *formula, QPushButton *buttonGrowLength, int formulaBaseHeight); bool FilterObject(QObject *object, QEvent *event); @@ -73,5 +76,13 @@ void ChangeColor(QWidget *widget, const QColor &color); QColor OkColor(QWidget *widget); void CheckPointLabel(QDialog *dialog, QLineEdit* edit, QLabel *labelEditNamePoint, const QString &pointName, const VContainer *data, bool &flag); +int FindNotExcludedNodeDown(QListWidget *listWidget, int candidate); +int FindNotExcludedNodeUp(QListWidget *listWidget, int candidate); +bool FirstPointEqualLast(QListWidget *listWidget, const VContainer *data); +bool DoublePoints(QListWidget *listWidget, const VContainer *data); +bool DoubleCurves(QListWidget *listWidget); +bool EachPointLabelIsUnique(QListWidget *listWidget); +QString DialogWarningIcon(); +QFont NodeFont(QFont font, bool nodeExcluded = false); #endif // DIALOGTOOLBOX_H diff --git a/src/libs/vtools/dialogs/tools/dialogtool.cpp b/src/libs/vtools/dialogs/tools/dialogtool.cpp index f8440db8d..b5a7fdaf0 100644 --- a/src/libs/vtools/dialogs/tools/dialogtool.cpp +++ b/src/libs/vtools/dialogs/tools/dialogtool.cpp @@ -77,74 +77,6 @@ template class QSharedPointer; Q_LOGGING_CATEGORY(vDialog, "v.dialog") -namespace -{ -//--------------------------------------------------------------------------------------------------------------------- -VPieceNode RowNode(QListWidget *listWidget, int i) -{ - SCASSERT(listWidget != nullptr); - - if (i < 0 || i >= listWidget->count()) - { - return VPieceNode(); - } - - const QListWidgetItem *rowItem = listWidget->item(i); - SCASSERT(rowItem != nullptr); - return qvariant_cast(rowItem->data(Qt::UserRole)); -} - -//--------------------------------------------------------------------------------------------------------------------- -bool DoublePoint(const VPieceNode &firstNode, const VPieceNode &secondNode, const VContainer *data) -{ - if (firstNode.GetTypeTool() == Tool::NodePoint && not (firstNode.GetId() == NULL_ID) - && secondNode.GetTypeTool() == Tool::NodePoint && not (secondNode.GetId() == NULL_ID)) - { - // don't ignore the same point twice - if (firstNode.GetId() == secondNode.GetId()) - { - return true; - } - - // But ignore the same coordinate if a user wants - if (not firstNode.IsCheckUniqueness() || not secondNode.IsCheckUniqueness()) - { - return false; - } - - try - { - const QSharedPointer firstPoint = data->GeometricObject(firstNode.GetId()); - const QSharedPointer secondPoint = data->GeometricObject(secondNode.GetId()); - - return firstPoint->toQPointF() == secondPoint->toQPointF(); - } - catch(const VExceptionBadId &) - { - return true; - } - } - - return false; -} - -//--------------------------------------------------------------------------------------------------------------------- -bool DoubleCurve(const VPieceNode &firstNode, const VPieceNode &secondNode) -{ - if (firstNode.GetTypeTool() != Tool::NodePoint && not (firstNode.GetId() == NULL_ID) - && secondNode.GetTypeTool() != Tool::NodePoint && not (secondNode.GetId() == NULL_ID)) - { - // don't ignore the same curve twice - if (firstNode.GetId() == secondNode.GetId()) - { - return true; - } - } - - return false; -} -} - //--------------------------------------------------------------------------------------------------------------------- /** * @brief DialogTool create dialog @@ -461,162 +393,6 @@ quint32 DialogTool::DNumber(const QString &baseName) const return num; } -//--------------------------------------------------------------------------------------------------------------------- -int DialogTool::FindNotExcludedNodeDown(QListWidget *listWidget, int candidate) -{ - SCASSERT(listWidget != nullptr); - - int index = -1; - if (candidate < 0 || candidate >= listWidget->count()) - { - return index; - } - - int i = candidate; - VPieceNode rowNode; - do - { - const QListWidgetItem *rowItem = listWidget->item(i); - SCASSERT(rowItem != nullptr); - rowNode = qvariant_cast(rowItem->data(Qt::UserRole)); - - if (not rowNode.IsExcluded()) - { - index = i; - } - - ++i; - } - while (rowNode.IsExcluded() && i < listWidget->count()); - - return index; -} - -//--------------------------------------------------------------------------------------------------------------------- -int DialogTool::FindNotExcludedNodeUp(QListWidget *listWidget, int candidate) -{ - SCASSERT(listWidget != nullptr); - - int index = -1; - if (candidate < 0 || candidate >= listWidget->count()) - { - return index; - } - - int i = candidate; - VPieceNode rowNode; - do - { - const QListWidgetItem *rowItem = listWidget->item(i); - SCASSERT(rowItem != nullptr); - rowNode = qvariant_cast(rowItem->data(Qt::UserRole)); - - if (not rowNode.IsExcluded()) - { - index = i; - } - - --i; - } - while (rowNode.IsExcluded() && i > -1); - - return index; -} - -//--------------------------------------------------------------------------------------------------------------------- -bool DialogTool::FirstPointEqualLast(QListWidget *listWidget, const VContainer *data) -{ - SCASSERT(listWidget != nullptr); - if (listWidget->count() > 1) - { - const VPieceNode topNode = RowNode(listWidget, FindNotExcludedNodeDown(listWidget, 0)); - const VPieceNode bottomNode = RowNode(listWidget, FindNotExcludedNodeUp(listWidget, listWidget->count()-1)); - - return DoublePoint(topNode, bottomNode, data); - } - return false; -} - -//--------------------------------------------------------------------------------------------------------------------- -bool DialogTool::DoublePoints(QListWidget *listWidget, const VContainer *data) -{ - SCASSERT(listWidget != nullptr); - for (int i=0, sz = listWidget->count()-1; icount()-1; i pointLabels; - int countPoints = 0; - for (int i=0; i < listWidget->count(); ++i) - { - const QListWidgetItem *rowItem = listWidget->item(i); - SCASSERT(rowItem != nullptr); - const VPieceNode rowNode = qvariant_cast(rowItem->data(Qt::UserRole)); - if (rowNode.GetTypeTool() == Tool::NodePoint) - { - ++countPoints; - pointLabels.insert(rowNode.GetId()); - } - } - - return countPoints == pointLabels.size(); -} - -//--------------------------------------------------------------------------------------------------------------------- -QString DialogTool::DialogWarningIcon() -{ - const QIcon icon = QIcon::fromTheme("dialog-warning", - QIcon(":/icons/win.icon.theme/16x16/status/dialog-warning.png")); - - const QPixmap pixmap = icon.pixmap(QSize(16, 16)); - QByteArray byteArray; - QBuffer buffer(&byteArray); - pixmap.save(&buffer, "PNG"); - const QString url = QString(" "); - return url; -} - -//--------------------------------------------------------------------------------------------------------------------- -QFont DialogTool::NodeFont(QFont font, bool nodeExcluded) -{ - font.setPointSize(12); - font.setWeight(QFont::Bold); - font.setStrikeOut(nodeExcluded); - return font; -} - //--------------------------------------------------------------------------------------------------------------------- QString DialogTool::GetNodeName(const VPieceNode &node, bool showDetails) const { diff --git a/src/libs/vtools/dialogs/tools/dialogtool.h b/src/libs/vtools/dialogs/tools/dialogtool.h index 6ff54c365..d743acf85 100644 --- a/src/libs/vtools/dialogs/tools/dialogtool.h +++ b/src/libs/vtools/dialogs/tools/dialogtool.h @@ -209,16 +209,6 @@ protected: */ virtual void SaveData() {} quint32 DNumber(const QString &baseName) const; - - static int FindNotExcludedNodeDown(QListWidget *listWidget, int candidate); - static int FindNotExcludedNodeUp(QListWidget *listWidget, int candidate); - static bool FirstPointEqualLast(QListWidget *listWidget, const VContainer *data); - static bool DoublePoints(QListWidget *listWidget, const VContainer *data); - static bool DoubleCurves(QListWidget *listWidget); - static bool EachPointLabelIsUnique(QListWidget *listWidget); - static QString DialogWarningIcon(); - static QFont NodeFont(QFont font, bool nodeExcluded = false); - QString GetNodeName(const VPieceNode &node, bool showDetails = false) const; void NewNodeItem(QListWidget *listWidget, const VPieceNode &node, bool showPassmark = true);