From 2610487fe576484ea1762ef9b5d9f27aeea31cf5 Mon Sep 17 00:00:00 2001
From: dismine <dismine@gmail.com>
Date: Tue, 4 Mar 2014 19:13:10 +0200
Subject: [PATCH] Refactoring.

--HG--
branch : feature
---
 src/dialogs/tools/dialogalongline.cpp | 23 ++++++++---------------
 src/dialogs/tools/dialognormal.cpp    | 23 ++++++++---------------
 src/dialogs/tools/dialogtool.cpp      | 15 +++++++++++++++
 src/dialogs/tools/dialogtool.h        |  1 +
 4 files changed, 32 insertions(+), 30 deletions(-)

diff --git a/src/dialogs/tools/dialogalongline.cpp b/src/dialogs/tools/dialogalongline.cpp
index 26e72d9e9..730ad762d 100644
--- a/src/dialogs/tools/dialogalongline.cpp
+++ b/src/dialogs/tools/dialogalongline.cpp
@@ -85,30 +85,23 @@ void DialogAlongLine::ChoosedObject(quint32 id, const Scene::Scenes &type)
 {
     if (type == Scene::Point)
     {
-        const VPointF *point = data->GeometricObject<const VPointF *>(id);
         if (number == 0)
         {
-            qint32 index = ui->comboBoxFirstPoint->findText(point->name());
-            if ( index != -1 )
-            { // -1 for not found
-                ui->comboBoxFirstPoint->setCurrentIndex(index);
+            if (ChoosedPoint(id, ui->comboBoxFirstPoint, tr("Select second point of line")))
+            {
                 number++;
-                emit ToolTip(tr("Select second point of line"));
                 return;
             }
         }
         if (number == 1)
         {
-            qint32 index = ui->comboBoxSecondPoint->findText(point->name());
-            if ( index != -1 )
-            { // -1 for not found
-                ui->comboBoxSecondPoint->setCurrentIndex(index);
-                number = 0;
-                emit ToolTip("");
-            }
-            if (isInitialized == false)
+            if (ChoosedPoint(id, ui->comboBoxSecondPoint, ""))
             {
-                this->show();
+                number = 0;
+                if (isInitialized == false)
+                {
+                    this->show();
+                }
             }
         }
     }
diff --git a/src/dialogs/tools/dialognormal.cpp b/src/dialogs/tools/dialognormal.cpp
index 99ea9f05f..401b4aa64 100644
--- a/src/dialogs/tools/dialognormal.cpp
+++ b/src/dialogs/tools/dialognormal.cpp
@@ -102,30 +102,23 @@ void DialogNormal::ChoosedObject(quint32 id, const Scene::Scenes &type)
 {
     if (type == Scene::Point)
     {
-        const VPointF *point = data->GeometricObject<const VPointF *>(id);
         if (number == 0)
         {
-            qint32 index = ui->comboBoxFirstPoint->findText(point->name());
-            if ( index != -1 )
-            { // -1 for not found
-                ui->comboBoxFirstPoint->setCurrentIndex(index);
+            if (ChoosedPoint(id, ui->comboBoxFirstPoint, tr("Select second point of line")))
+            {
                 number++;
-                emit ToolTip(tr("Select second point of line"));
                 return;
             }
         }
         if (number == 1)
         {
-            qint32 index = ui->comboBoxSecondPoint->findText(point->name());
-            if ( index != -1 )
-            { // -1 for not found
-                ui->comboBoxSecondPoint->setCurrentIndex(index);
-                number = 0;
-                emit ToolTip("");
-            }
-            if (isInitialized == false)
+            if (ChoosedPoint(id, ui->comboBoxSecondPoint, ""))
             {
-                this->show();
+                number = 0;
+                if (isInitialized == false)
+                {
+                    this->show();
+                }
             }
         }
     }
diff --git a/src/dialogs/tools/dialogtool.cpp b/src/dialogs/tools/dialogtool.cpp
index 03ce0823f..496c52be6 100644
--- a/src/dialogs/tools/dialogtool.cpp
+++ b/src/dialogs/tools/dialogtool.cpp
@@ -384,6 +384,21 @@ quint32 DialogTool::getCurrentObjectId(QComboBox *box) const
     }
 }
 
+bool DialogTool::ChoosedPoint(const quint32 &id, QComboBox *box, const QString &toolTip)
+{
+    Q_CHECK_PTR(box);
+    const VPointF *point = data->GeometricObject<const VPointF *>(id);
+    Q_CHECK_PTR(point);
+    const qint32 index = box->findText(point->name());
+    if ( index != -1 )
+    { // -1 for not found
+        box->setCurrentIndex(index);
+        emit ToolTip(toolTip);
+        return true;
+    }
+    return false;
+}
+
 void DialogTool::FillList(QComboBox *box, const QMap<QString, quint32> &list) const
 {
     Q_CHECK_PTR(box);
diff --git a/src/dialogs/tools/dialogtool.h b/src/dialogs/tools/dialogtool.h
index 9b68e07bb..48ee2b043 100644
--- a/src/dialogs/tools/dialogtool.h
+++ b/src/dialogs/tools/dialogtool.h
@@ -398,6 +398,7 @@ protected:
      * @return id or -1 if combobox is empty
      */
     quint32           getCurrentObjectId(QComboBox *box) const;
+    bool ChoosedPoint(const quint32 &id, QComboBox *box, const QString &toolTip);
 private:
     /**
      * @brief FillList fill combobox list