|
| 1 | +/*************************************************************************** |
| 2 | + * * |
| 3 | + * This program is free software; you can redistribute it and/or modify * |
| 4 | + * it under the terms of the GNU General Public License as published by * |
| 5 | + * the Free Software Foundation; either version 2 of the License, or * |
| 6 | + * (at your option) any later version. * |
| 7 | + * * |
| 8 | + ***************************************************************************/ |
| 9 | + |
| 10 | +import QtQuick |
| 11 | +import QtQuick.Controls |
| 12 | +import QtQuick.Controls.Basic |
| 13 | + |
| 14 | +/** |
| 15 | + * A three-state segment control for boolean-like selection: All / True / False. |
| 16 | + * |
| 17 | + * Signals: |
| 18 | + * - selectionChanged( int index ) — emitted when the selected segment changes; |
| 19 | + * index maps to MMSegmentControl.Options enum values. |
| 20 | + * |
| 21 | + * Usage: |
| 22 | + * MMSegmentControl { |
| 23 | + * onSelectionChanged: function( index ) { console.log( index ) } |
| 24 | + * } |
| 25 | + */ |
| 26 | +Item { |
| 27 | + id: root |
| 28 | + |
| 29 | + enum Options { All, True, False } |
| 30 | + |
| 31 | + /* optional */ property int selectedIndex: MMSegmentControl.Options.All |
| 32 | + |
| 33 | + /* optional */ property string allText: qsTr( "All" ) |
| 34 | + /* optional */ property string trueText: qsTr( "True" ) |
| 35 | + /* optional */ property string falseText: qsTr( "False" ) |
| 36 | + |
| 37 | + signal selectionChanged( int index ) |
| 38 | + |
| 39 | + implicitWidth: row.implicitWidth + 2 * __style.margin8 |
| 40 | + implicitHeight: __style.row45 |
| 41 | + |
| 42 | + Rectangle { |
| 43 | + id: background |
| 44 | + |
| 45 | + anchors.fill: parent |
| 46 | + radius: __style.radius16 |
| 47 | + color: __style.lightGreenColor |
| 48 | + } |
| 49 | + |
| 50 | + Row { |
| 51 | + id: row |
| 52 | + |
| 53 | + anchors.fill: parent |
| 54 | + anchors.margins: __style.margin4 |
| 55 | + |
| 56 | + spacing: 0 |
| 57 | + |
| 58 | + Repeater { |
| 59 | + id: repeater |
| 60 | + |
| 61 | + model: [ |
| 62 | + { text: root.allText, index: MMSegmentControl.Options.All }, |
| 63 | + { text: root.trueText, index: MMSegmentControl.Options.True }, |
| 64 | + { text: root.falseText, index: MMSegmentControl.Options.False } |
| 65 | + ] |
| 66 | + |
| 67 | + delegate: Item { |
| 68 | + id: segment |
| 69 | + |
| 70 | + readonly property bool isSelected: root.selectedIndex === modelData.index |
| 71 | + |
| 72 | + width: ( row.width - row.spacing * ( repeater.count - 1 ) ) / repeater.count |
| 73 | + height: row.height |
| 74 | + |
| 75 | + Rectangle { |
| 76 | + id: segmentBackground |
| 77 | + |
| 78 | + anchors.fill: parent |
| 79 | + radius: __style.radius12 |
| 80 | + |
| 81 | + color: segment.isSelected ? __style.polarColor : __style.transparentColor |
| 82 | + |
| 83 | + border.color: segment.isSelected ? __style.forestColor : __style.transparentColor |
| 84 | + border.width: segment.isSelected ? 1.5 * __dp : 0 |
| 85 | + } |
| 86 | + |
| 87 | + MMText { |
| 88 | + id: label |
| 89 | + |
| 90 | + anchors.centerIn: parent |
| 91 | + |
| 92 | + text: modelData.text |
| 93 | + font: __style.t4 |
| 94 | + color: segment.isSelected ? __style.forestColor : __style.nightColor |
| 95 | + |
| 96 | + fontSizeMode: Text.Fit |
| 97 | + minimumPixelSize: 10 * __dp |
| 98 | + horizontalAlignment: Text.AlignHCenter |
| 99 | + |
| 100 | + leftPadding: __style.margin8 |
| 101 | + rightPadding: __style.margin8 |
| 102 | + } |
| 103 | + |
| 104 | + MouseArea { |
| 105 | + anchors.fill: parent |
| 106 | + onClicked: { |
| 107 | + if ( root.selectedIndex !== modelData.index ) { |
| 108 | + root.selectedIndex = modelData.index |
| 109 | + root.selectionChanged( modelData.index ) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments