Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Implement SqueezedComboBox for QML, and setup some properties with this.

Wolthera van Hövell tot Westerflier4931336220e0
diff --git a/plugins/dockers/textproperties/qml/Direction.qml b/plugins/dockers/textproperties/qml/Direction.qml
index 5d1cbde82b..23f3a30697 100644
--- a/plugins/dockers/textproperties/qml/Direction.qml
+++ b/plugins/dockers/textproperties/qml/Direction.qml
@@ -64,15 +64,18 @@ TextPropertyBase {
palette: directionCmbPalette.palette;
}
- ComboBox {
+ SqueezedComboBox {
id: directionCmb
Layout.fillWidth: true;
+ Layout.preferredWidth: implicitWidth;
model: [
- {text: i18nc("@label:inlistbox", "Left to Right"), value: KoSvgText.DirectionLeftToRight},
- {text: i18nc("@label:inlistbox", "Right to Left"), value: KoSvgText.DirectionRightToLeft}
+ {text: i18nc("@label:inlistbox", "Left to Right"), value: KoSvgText.DirectionLeftToRight, icon: "qrc:///22_light_format-text-direction-ltr.svg"},
+ {text: i18nc("@label:inlistbox", "Right to Left"), value: KoSvgText.DirectionRightToLeft, icon: "qrc:///22_light_format-text-direction-rtl.svg"}
]
textRole: "text";
valueRole: "value";
+ iconRole: "icon";
+ iconSize: 22;
onActivated: direction = currentValue;
wheelEnabled: true;
PaletteControl {
diff --git a/plugins/dockers/textproperties/qml/FontSize.qml b/plugins/dockers/textproperties/qml/FontSize.qml
index c89cbd11b3..9684bf6c68 100644
--- a/plugins/dockers/textproperties/qml/FontSize.qml
+++ b/plugins/dockers/textproperties/qml/FontSize.qml
@@ -70,7 +70,8 @@ TextPropertyBase {
id: fontSizeUnitCmb;
spinBoxControl: fontSizeSpn;
isFontSize: true;
- Layout.fillWidth: true;
+ Layout.preferredWidth: minimumUnitBoxWidth;
+ Layout.maximumWidth: implicitWidth;
}
}
}
diff --git a/plugins/dockers/textproperties/qml/FontStyle.qml b/plugins/dockers/textproperties/qml/FontStyle.qml
index 764006563e..30997245a5 100644
--- a/plugins/dockers/textproperties/qml/FontStyle.qml
+++ b/plugins/dockers/textproperties/qml/FontStyle.qml
@@ -107,7 +107,7 @@ CollapsibleGroupProperty {
Layout.maximumWidth: contentWidth;
}
- ComboBox {
+ SqueezedComboBox {
id: styleCmb;
model: fontStylesModel;
textRole: "display";
diff --git a/plugins/dockers/textproperties/qml/LetterSpacing.qml b/plugins/dockers/textproperties/qml/LetterSpacing.qml
index d67a10bf41..a7770b36e7 100644
--- a/plugins/dockers/textproperties/qml/LetterSpacing.qml
+++ b/plugins/dockers/textproperties/qml/LetterSpacing.qml
@@ -68,7 +68,8 @@ TextPropertyBase {
id: letterSpacingUnitCmb
spinBoxControl: letterSpacingSpn;
isFontSize: false;
- Layout.fillWidth: true;
+ Layout.preferredWidth: minimumUnitBoxWidth;
+ Layout.maximumWidth: implicitWidth;
allowPercentage: false; // CSS-Text-4 has percentages for letter-spacing, but so does SVG 1.1, and they both are implemented differently.
}
}
diff --git a/plugins/dockers/textproperties/qml/LineHeight.qml b/plugins/dockers/textproperties/qml/LineHeight.qml
index d449fc9e81..d2de247bd9 100644
--- a/plugins/dockers/textproperties/qml/LineHeight.qml
+++ b/plugins/dockers/textproperties/qml/LineHeight.qml
@@ -97,7 +97,7 @@ TextPropertyBase {
}
}
- ComboBox {
+ SqueezedComboBox {
id: lineHeightUnitCmb;
property QtObject spinBoxControl: lineHeightSpn;
model: [
diff --git a/plugins/dockers/textproperties/qml/SqueezedComboBox.qml b/plugins/dockers/textproperties/qml/SqueezedComboBox.qml
new file mode 100644
index 0000000000..fd3a6754a6
--- /dev/null
+++ b/plugins/dockers/textproperties/qml/SqueezedComboBox.qml
@@ -0,0 +1,117 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+import QtQuick 2.15
+import QtQuick.Controls 2.15
+import QtQuick.Layouts 1.15
+/**
+ This is a QML Interpretation of KisSqueezedComboBox, where the main combobox button is resized and elided,
+ but the pop-up is scaled so all items fit inside.
+
+ It can also handle an icon and tooltip.
+ */
+ComboBox {
+ id: squeezedComboBox;
+ property string iconRole: ""; ///< model roleName string to icon source.
+ property string toolTipRole: ""; ///< model roleName to tooltip.
+ property int iconSize: 12;
+ wheelEnabled: true;
+
+ height: contentItem.height;
+
+ contentItem: ItemDelegate {
+ id: contentItemDelegate;
+ width: parent.width;
+ palette: squeezedComboBox.palette;
+ property var mData: squeezedComboBox.model !== undefined? squeezedComboBox.model[squeezedComboBox.currentIndex]: {};
+ property string tooltipText: squeezedComboBox.toolTipRole && mData !== undefined ? mData[squeezedComboBox.toolTipRole] : "";
+ icon.height: squeezedComboBox.iconSize;
+ icon.width: squeezedComboBox.iconSize;
+ icon.color: highlighted? palette.highlightedText: palette.text;
+ icon.source: squeezedComboBox.iconRole && mData !== undefined ? mData[squeezedComboBox.iconRole] : "";
+ text: squeezedComboBox.textRole && mData !== undefined? mData[squeezedComboBox.textRole] : "";
+
+ background: Rectangle{ color:"transparent";}
+
+ ToolTip.text: tooltipText;
+ ToolTip.visible: highlighted && tooltipText.length > 0;
+ ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval;
+ onClicked: {
+ squeezedComboBox.popup.visible? squeezedComboBox.popup.close(): squeezedComboBox.popup.open();
+ }
+ }
+
+ TextMetrics {
+ id: textMetrics;
+ font: contentItemDelegate.font;
+ }
+
+ // It is surprisingly hard to get the listview to be the same size as the child rect, if we also want the children
+ // to grow if there's any more space. This function calculates that.
+ function recalculatePopupWidth() {
+ if (model === undefined) return;
+ let maxValue = 0;
+ for (let i = 0; i < count; i++) {
+ textMetrics.text = squeezedComboBox.textRole ? model[i][squeezedComboBox.textRole] : "";
+ maxValue = Math.max(maxValue, textMetrics.width);
+ }
+ let iconWidth = iconRole? iconSize: 0;
+ let total = iconWidth + maxValue + contentItemDelegate.leftPadding+contentItemDelegate.rightPadding+(contentItemDelegate.spacing*2);
+ view.maxWidth = total;
+ }
+
+ Component.onCompleted: recalculatePopupWidth();
+ onModelChanged: recalculatePopupWidth();
+
+ delegate: ItemDelegate {
+ id: squeezedComboDelegate;
+ width: parent && parent != undefined? parent.width: 0;
+ palette: squeezedComboBox.palette;
+ highlighted: squeezedComboBox.highlightedIndex === index;
+ // Depending on the model, either modelData (read only) or model(read/write) is available.
+ // See https://bugreports.qt.io/browse/QTBUG-111176
+ required property var modelData;
+ required property var model;
+ property var mData: modelData && modelData !== undefined? modelData: model;
+ required property int index;
+ property string tooltipText: squeezedComboBox.toolTipRole ? mData[squeezedComboBox.toolTipRole] : "";
+ icon.height: squeezedComboBox.iconSize;
+ icon.width: squeezedComboBox.iconSize;
+ icon.color: highlighted? palette.highlightedText: palette.text;
+ icon.source: squeezedComboBox.iconRole ? mData[squeezedComboBox.iconRole] : "";
+ text: squeezedComboBox.textRole ? mData[squeezedComboBox.textRole] : "";
+
+ ToolTip.text: tooltipText;
+ ToolTip.visible: highlighted && tooltipText.length > 0;
+ ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval;
+ }
+
+ popup: Popup {
+ id: squeezedComboPopup;
+ y: squeezedComboBox.height - 1;
+ x: squeezedComboBox.width - width;
+ width: contentWidth;
+ height: contentHeight;
+ padding: 1;
+
+ palette: squeezedComboBox.palette;
+
+ contentItem: ListView {
+ leftMargin: 0;
+ rightMargin: 0;
+ id: view;
+ property int maxWidth: 100;
+ implicitWidth: Math.max(maxWidth, squeezedComboBox.width);
+ width: implicitWidth;
+ implicitHeight: contentHeight;
+ height: implicitHeight;
+ clip: true;
+ model: squeezedComboBox.delegateModel;
+ currentIndex: squeezedComboBox.currentIndex;
+ ScrollBar.vertical: ScrollBar {
+ }
+ }
+ }
+}
diff --git a/plugins/dockers/textproperties/qml/TextAlign.qml b/plugins/dockers/textproperties/qml/TextAlign.qml
index 3b4a01d5c8..35f1e43f83 100644
--- a/plugins/dockers/textproperties/qml/TextAlign.qml
+++ b/plugins/dockers/textproperties/qml/TextAlign.qml
@@ -85,19 +85,21 @@ CollapsibleGroupProperty {
font.italic: properties.textAlignAllState === KoSvgTextPropertiesModel.PropertyTriState;
}
- ComboBox {
+ SqueezedComboBox {
id: textAlignAllCmb;
model: [
- {text: i18nc("@label:inlistbox", "Left"), value: KoSvgText.AlignLeft},
- {text: i18nc("@label:inlistbox", "Start"), value: KoSvgText.AlignStart},
- {text: i18nc("@label:inlistbox", "Center"), value: KoSvgText.AlignCenter},
- {text: i18nc("@label:inlistbox", "End"), value: KoSvgText.AlignEnd},
- {text: i18nc("@label:inlistbox", "Right"), value: KoSvgText.AlignRight},
- {text: i18nc("@label:inlistbox", "Justified"), value: KoSvgText.AlignJustify}
+ {text: i18nc("@label:inlistbox", "Left"), value: KoSvgText.AlignLeft, icon: "qrc:///16_light_format-justify-left.svg"},
+ {text: i18nc("@label:inlistbox", "Start"), value: KoSvgText.AlignStart, icon: "qrc:///16_light_format-justify-left.svg"},
+ {text: i18nc("@label:inlistbox", "Center"), value: KoSvgText.AlignCenter, icon: "qrc:///16_light_format-justify-center.svg"},
+ {text: i18nc("@label:inlistbox", "End"), value: KoSvgText.AlignEnd, icon: "qrc:///16_light_format-justify-right.svg"},
+ {text: i18nc("@label:inlistbox", "Right"), value: KoSvgText.AlignRight, icon: "qrc:///16_light_format-justify-right.svg"},
+ {text: i18nc("@label:inlistbox", "Justified"), value: KoSvgText.AlignJustify, icon: "qrc:///16_light_format-justify-fill.svg"}
]
Layout.fillWidth: true
textRole: "text";
valueRole: "value";
+ iconRole: "icon";
+ iconSize: 16;
onActivated: textAlignAll = currentValue;
wheelEnabled: true;
}
@@ -144,15 +146,17 @@ CollapsibleGroupProperty {
font.italic: properties.textAnchorState === KoSvgTextPropertiesModel.PropertyTriState;
}
- ComboBox {
+ SqueezedComboBox {
id: textAnchorCmb;
model: [
- {text: i18nc("@label:inlistbox", "Start"), value: KoSvgText.AnchorStart},
- {text: i18nc("@label:inlistbox", "Middle"), value: KoSvgText.AnchorMiddle},
- {text: i18nc("@label:inlistbox", "End"), value: KoSvgText.AnchorEnd}]
+ {text: i18nc("@label:inlistbox", "Start"), value: KoSvgText.AnchorStart, icon: "qrc:///16_light_format-justify-left.svg"},
+ {text: i18nc("@label:inlistbox", "Middle"), value: KoSvgText.AnchorMiddle, icon: "qrc:///16_light_format-justify-center.svg"},
+ {text: i18nc("@label:inlistbox", "End"), value: KoSvgText.AnchorEnd, icon: "qrc:///16_light_format-justify-right.svg"}]
Layout.fillWidth: true
textRole: "text";
valueRole: "value";
+ iconRole: "icon";
+ iconSize: 16;
onActivated: textAnchor = currentValue;
wheelEnabled: true;
}
diff --git a/plugins/dockers/textproperties/qml/TextIndent.qml b/plugins/dockers/textproperties/qml/TextIndent.qml
index 64fdd5286e..0b1da07398 100644
--- a/plugins/dockers/textproperties/qml/TextIndent.qml
+++ b/plugins/dockers/textproperties/qml/TextIndent.qml
@@ -83,7 +83,8 @@ CollapsibleGroupProperty {
UnitComboBox {
id: textIndentUnitCmb;
spinBoxControl: textIndentSpn;
- Layout.fillWidth: true;
+ Layout.preferredWidth: minimumUnitBoxWidth;
+ Layout.maximumWidth: implicitWidth;
}
}
diff --git a/plugins/dockers/textproperties/qml/UnitComboBox.qml b/plugins/dockers/textproperties/qml/UnitComboBox.qml
index a08ee90180..b0fb1277f3 100644
--- a/plugins/dockers/textproperties/qml/UnitComboBox.qml
+++ b/plugins/dockers/textproperties/qml/UnitComboBox.qml
@@ -7,13 +7,15 @@ import QtQuick 2.15
import QtQuick.Controls 2.15
import org.krita.flake.text 1.0
-ComboBox {
+SqueezedComboBox {
property QtObject spinBoxControl;
property bool isFontSize: false;
property int comboBoxUnit;
property bool allowPercentage: true;
wheelEnabled: true;
+ property int minimumUnitBoxWidth: height+indicator.width;
+
property string ptString: i18nc("@label:inlistbox", "Pt");
property string emString: i18nc("@label:inlistbox", "Em");
property string exString: i18nc("@label:inlistbox", "Ex");
diff --git a/plugins/dockers/textproperties/qml/WordSpacing.qml b/plugins/dockers/textproperties/qml/WordSpacing.qml
index bab6df66d5..7aeb3877fe 100644
--- a/plugins/dockers/textproperties/qml/WordSpacing.qml
+++ b/plugins/dockers/textproperties/qml/WordSpacing.qml
@@ -69,7 +69,8 @@ TextPropertyBase {
id: wordSpacingUnitCmb
spinBoxControl: wordSpacingSpn;
isFontSize: false;
- Layout.fillWidth: true;
+ Layout.preferredWidth: minimumUnitBoxWidth;
+ Layout.maximumWidth: implicitWidth;
allowPercentage: false; // CSS-Text-4 has percentages for word-spacing, but so does SVG 1.1, and they both are implemented differently.
}
}
diff --git a/plugins/dockers/textproperties/qml/WritingMode.qml b/plugins/dockers/textproperties/qml/WritingMode.qml
index 3ac3b10ea8..e2b497f5e3 100644
--- a/plugins/dockers/textproperties/qml/WritingMode.qml
+++ b/plugins/dockers/textproperties/qml/WritingMode.qml
@@ -50,18 +50,19 @@ TextPropertyBase {
}
- ComboBox {
+ SqueezedComboBox {
id: writingModeCmb
model: [
- {text: i18nc("@label:inlistbox", "Horizontal"), value: KoSvgText.HorizontalTB},
- {text: i18nc("@label:inlistbox", "Vertical, Right to Left"), value: KoSvgText.VerticalRL},
- {text: i18nc("@label:inlistbox", "Vertical, Left To Right"), value: KoSvgText.VerticalLR}
+ {text: i18nc("@label:inlistbox", "Horizontal"), value: KoSvgText.HorizontalTB, icon: "qrc:///22_light_format-text-direction-horizontal-tb.svg"},
+ {text: i18nc("@label:inlistbox", "Vertical, Right to Left"), value: KoSvgText.VerticalRL, icon: "qrc:///22_light_format-text-direction-vertical-rl.svg"},
+ {text: i18nc("@label:inlistbox", "Vertical, Left To Right"), value: KoSvgText.VerticalLR, icon: "qrc:///22_light_format-text-direction-vertical-lr.svg"}
]
Layout.fillWidth: true;
textRole: "text";
valueRole: "value";
+ iconRole: "icon";
+ iconSize: 22;
onActivated: writingMode = currentValue;
- wheelEnabled: true;
}
}
}
diff --git a/plugins/dockers/textproperties/textproperties.qrc b/plugins/dockers/textproperties/textproperties.qrc
index dc7e24cbfa..e37c3e84e2 100644
--- a/plugins/dockers/textproperties/textproperties.qrc
+++ b/plugins/dockers/textproperties/textproperties.qrc
@@ -44,5 +44,6 @@
<file alias="font-type-postscript.svg">icons/font-type-postscript.svg</file>
<file alias="font-type-opentype-variable.svg">icons/font-type-opentype-variable.svg</file>
<file alias="PaletteControl.qml">qml/PaletteControl.qml</file>
+ <file alias="SqueezedComboBox.qml">qml/SqueezedComboBox.qml</file>
</qresource>
</RCC>