From a5dc8a703a275edfc149f0d78aba3a003aba95c8 Mon Sep 17 00:00:00 2001
From: "tatiana.novikova" <tatiana.novikova@demlabs.net>
Date: Fri, 14 Feb 2020 15:34:23 +0300
Subject: [PATCH] [*] Change comboBox for composite models

---
 widgets/DapComboBox.qml        | 116 ++++++++++-----------------------
 widgets/DapComboBoxForm.ui.qml |  38 +++++------
 widgets/DapTextForm.ui.qml     |   6 +-
 3 files changed, 54 insertions(+), 106 deletions(-)

diff --git a/widgets/DapComboBox.qml b/widgets/DapComboBox.qml
index 6d11119..61c0e95 100644
--- a/widgets/DapComboBox.qml
+++ b/widgets/DapComboBox.qml
@@ -3,7 +3,6 @@ import QtQuick.Controls 2.0
 
 DapComboBoxForm
 {
-
     delegate:
         ItemDelegate
         {
@@ -32,16 +31,14 @@ DapComboBoxForm
             contentItem:
                 Rectangle
                 {
-                    id: rectTextComboBox
+                    id: rectangleTextComboBox
 
                     anchors.fill: parent
                     anchors.topMargin: paddingTopItemDelegate
                     anchors.leftMargin: popup.visible ? sidePaddingActive : sidePaddingNormal
                     anchors.rightMargin: popup.visible ? sidePaddingActive : sidePaddingNormal
+                    border.width: 1
                     color: "transparent"
-                    width: (index !== currentIndex) ?
-                               widthPopupComboBoxNormal - 2*sidePaddingActive - endRowPadding :
-                               widthPopupComboBoxNormal - indicatorWidth - indicatorLeftInterval - 2 * sidePaddingActive
 
                     FontMetrics
                     {
@@ -52,92 +49,47 @@ DapComboBoxForm
                     property int comboBoxCurrentIndex: currentIndex
                     Row
                     {
+                        id: textRow
+                        width: rectangleTextComboBox.width - ((index != currentIndex) ?
+                                                                  endRowPadding :
+                                                                  (indicatorWidth + indicatorLeftInterval)
+                                                              )
+                        height: rectangleTextComboBox.height
                         spacing: roleInterval
                         Repeater
                         {
+                            id: textRepeater
                             model: comboBoxTextRole.length
-                            Text
+                            DapText
                             {
-                                id: textDelegateComboBox
-                                font: fontComboBox
-                                color: hovered ? hilightColorText : normalColorText
-                                horizontalAlignment:
-                                {
-                                    if(index === 0)
-                                        return Text.AlignLeft;
-                                    if(index === comboBoxTextRole.length - 1)
-                                        return Text.AlignRight;
-                                    return Text.AlignHCenter;
-                                }
-                                width: (rectTextComboBox.width - roleInterval * (comboBoxTextRole.length - 1)) / comboBoxTextRole.length
-                                /*{
-                                        if(index === 0)
-                                            setModelDataWidth(rectTextComboBox.comboBoxIndex, comboBoxFontMetric, rectTextComboBox.width);
-                                         console.log("comboBoxIndex", rectTextComboBox.comboBoxIndex, "index", index, "comboBoxTextRole", comboBoxTextRole[index])
-                                         return comboBoxTextRoleWidth[index];
-                                }*/
-                                TextMetrics
+                                id: textComboBoxDelegate
+                                width: (textRow.width - roleInterval * (comboBoxTextRole.length - 1)) / comboBoxTextRole.length
+                                enabled: false
+                                fontDapText: fontComboBox
+                                textColor: hovered ? hilightColorText : normalColorText
+                                fullText: getModelData(rectangleTextComboBox.comboBoxIndex, comboBoxTextRole[index])
+
+                                Component.onCompleted:
                                 {
-                                    id: comboBoxTextMetric
-                                    font: fontComboBox
-                                    elide: Text.ElideRight
-                                    text: getModelData(rectTextComboBox.comboBoxIndex, comboBoxTextRole[index])
-                                    elideWidth: textDelegateComboBox.width
+                                   if(rectangleTextComboBox.comboBoxIndex == rectangleTextComboBox.comboBoxCurrentIndex)
+                                   {
+                                       var tmp = mainRow;
+                                       tmp[index] = elText;
+                                       mainRow = tmp;
+
+                                       if(index == 0)
+                                           mainLineText = comboBoxFontMetric.elidedText(fullText, Text.ElideRight, rectangleTextComboBox.width, Qt.TextShowMnemonic);
+                                   }
                                 }
-                                /*Component.onCompleted:
-                                {
-                                    setModelDataWidth(rectTextComboBox.comboBoxIndex, comboBoxFontMetric, rectTextComboBox.width);
-                                    textDelegateComboBox.width = comboBoxTextRoleWidth[index];
-                                }*/
-                                text:
-                                {
-                                    if(rectTextComboBox.comboBoxIndex != rectTextComboBox.comboBoxCurrentIndex)
-                                    {
-                                        if(comboBoxTextMetric.elideWidth < comboBoxFontMetric.tightBoundingRect(comboBoxTextMetric.text).width)
-                                            return checkElidedText(comboBoxFontMetric, comboBoxTextMetric);
-                                        else if(comboBoxTextMetric.elideWidth === comboBoxFontMetric.tightBoundingRect(comboBoxTextMetric.text).width)
-                                            return comboBoxTextMetric.text;
-                                        else
-                                            return comboBoxTextMetric.elidedText.replace('…', '..');
-                                    }
-                                    else
-                                    {
-                                        if(popup.visible)
-                                        {
-                                            if(comboBoxTextMetric.elideWidth < comboBoxFontMetric.tightBoundingRect(comboBoxTextMetric.text).width)
-                                                mainLineText[index] = checkElidedText(comboBoxFontMetric, comboBoxTextMetric);
-                                            else
-                                            {
-                                                if(comboBoxTextMetric.elideWidth === comboBoxFontMetric.tightBoundingRect(comboBoxTextMetric.text).width)
-                                                    mainLineText[index] = comboBoxTextMetric.text;
-                                                else
-                                                    mainLineText[index] = comboBoxTextMetric.elidedText.replace('…', '..');
-                                            }
-
-                                        }
-                                        else
-                                        {
-                                            if(index === 0)
-                                                mainLineText[index] = comboBoxFontMetric.elidedText(getModelData(rectTextComboBox.comboBoxIndex, comboBoxTextRole[index]),
-                                                                                                                        Text.ElideRight,
-                                                                                                                        (widthPopupComboBoxNormal - indicatorWidth - indicatorLeftInterval),
-                                                                                                                         Qt.TextShowMnemonic).replace('…', '..');
-                                        }
-                                        console.log("mainLineText[index]", "index", mainLineText[index], index)
-                                            return "";
-
-                                    }
-                                }
-                            }
 
+                            }
                         }
-                        Rectangle
-                        {
-                            id: endRowRectangle
-                            height: parent.height
-                            width: endRowPadding
-                            color: "transparent"
-                        }
+
+                    }
+                    onComboBoxCurrentIndexChanged:
+                    {
+                        mainLineText = comboBoxFontMetric.elidedText(getModelData(currentIndex, comboBoxTextRole[0]), Text.ElideRight, rectangleTextComboBox.width, Qt.TextShowMnemonic);
+                        console.log("mainLineText", mainLineText)
                     }
                 }
 
diff --git a/widgets/DapComboBoxForm.ui.qml b/widgets/DapComboBoxForm.ui.qml
index 448ea08..c111277 100644
--- a/widgets/DapComboBoxForm.ui.qml
+++ b/widgets/DapComboBoxForm.ui.qml
@@ -61,20 +61,20 @@ ComboBox
     property string colorDropShadow
     ///@detalis fontComboBox Font setting combobox.
     property alias fontComboBox: dapComboBox.font
-    //@detalis mainLineText Text without unneccesary part.
-    property var mainLineText: [""]
 
     ///@detalis comboBoxTextRole The model roles used for the ComboBox.
-    property var comboBoxTextRole: ["name"]
+    property var comboBoxTextRole: ["text"]
     ///@detalis comboBoxTextRoleWidth The model roles width used for the ComboBox.
     property var comboBoxTextRoleWidth: [100]
     ///@detalis roleInterval The width between text of model roles used for the ComboBox.
-    property int roleInterval: 5
+    property int roleInterval: 10
     ///@detalis endRowPadding The width of padding at the end of one row at ComboBox where it is need.
-    property int endRowPadding: 0
+    property int endRowPadding: 10
 
-    ///@detalis mainLineRole The model role for the main line of cloded ComboBox.
-    property string mainLineRole: "name"
+    ///@detalis mainRow The model role for the main line of cloded ComboBox.
+    property var mainRow: [""]
+    //@detalis mainLineText Text without unneccesary part.
+    property string mainLineText
 
 
 
@@ -107,30 +107,26 @@ ComboBox
     contentItem:
         Rectangle
         {
+            id: mainRectangle
             anchors.fill: parent
             anchors.verticalCenter: parent.verticalCenter
             anchors.leftMargin: popup.visible ? sidePaddingActive : sidePaddingNormal
-            width: widthPopupComboBoxNormal - indicatorWidth - indicatorLeftInterval
-            color: "transparent"
+             color: "transparent"
             Row
             {
+                width: widthPopupComboBoxNormal - indicatorWidth - indicatorLeftInterval
+                height: mainRectangle.height
                 spacing: roleInterval
                 Repeater
                 {
-                    id: mainLineRepeater
-                    model: popup.visible ? comboBoxTextRole.length : 1
+                    id: textCurrentRepeater
+                    model: (popup.visible) ? comboBoxTextRole.length : 1
                     Text
                     {
-                        id: mainLineComboBoxText
-                        height: dapComboBox.height
-                        text: index >= mainLineText.length ? "" : mainLineText[index]
-                        horizontalAlignment: (index === 0) ?
-                                                 Text.AlignLeft :
-                                                 ((index === comboBoxTextRole.length - 1) ? Text.AlignRight : Text.AlignHCenter)
-
-                        font: fontComboBox
-                        color: popup.visible ? hilightColorTopText : normalColorTopText
-                        verticalAlignment: Text.AlignVCenter
+                        anchors.verticalCenter: parent.verticalCenter
+                        text: (popup.visible) ? mainRow[index] : mainLineText
+                        width: contentWidth
+                        color: "green"
                     }
                 }
             }
diff --git a/widgets/DapTextForm.ui.qml b/widgets/DapTextForm.ui.qml
index 3cf02ce..7aeebc2 100644
--- a/widgets/DapTextForm.ui.qml
+++ b/widgets/DapTextForm.ui.qml
@@ -10,7 +10,7 @@ TextInput
     ///@detalis elText Elided text string
     property string elText
     ////@detalis fontDapText Font setting combobox.
-    property alias fontDapText: dapText.font
+    property font fontDapText
     ///@details elide Elide style.
     property int textElide: Text.ElideRight
     ///@detalis textMetric Text Metric to elide text
@@ -26,7 +26,7 @@ TextInput
     TextMetrics
     {
         id: dapTextInputTextMetric
-        font: parent.font
+        font: fontDapText
         elide: textElide
         text: fullText
     }
@@ -35,7 +35,7 @@ TextInput
     FontMetrics
     {
         id: dapTextInputFontMetric
-        font: parent.font
+        font: fontDapText
     }
     text: elText
 }
-- 
GitLab