From af358cb6ec885db5a87a0b34b48387e571239b25 Mon Sep 17 00:00:00 2001 From: Kirill Anisimov <kirill.anisimov@demlabs.net> Date: Fri, 31 Jan 2020 19:33:27 +0000 Subject: [PATCH] Features 3030 --- libdap-qt-ui-qml.qrc | 2 ++ widgets/DapText.qml | 61 ++++++++++++++++++++++++++++++++++++++ widgets/DapTextForm.ui.qml | 43 +++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 widgets/DapText.qml create mode 100644 widgets/DapTextForm.ui.qml diff --git a/libdap-qt-ui-qml.qrc b/libdap-qt-ui-qml.qrc index 96a76e1..ee0c2a1 100644 --- a/libdap-qt-ui-qml.qrc +++ b/libdap-qt-ui-qml.qrc @@ -22,6 +22,8 @@ <file>widgets/DapRadioButtonForm.ui.qml</file> <file>widgets/DapScrollView.qml</file> <file>widgets/DapScrollViewForm.ui.qml</file> + <file>widgets/DapText.qml</file> + <file>widgets/DapTextForm.ui.qml</file> <file>widgets/DapScrollViewHandling.qml</file> </qresource> </RCC> diff --git a/widgets/DapText.qml b/widgets/DapText.qml new file mode 100644 index 0000000..3ca718a --- /dev/null +++ b/widgets/DapText.qml @@ -0,0 +1,61 @@ +import QtQuick 2.4 + +DapTextForm +{ + onTextChanged: + { + elidedText = (width > 2) ? elideByWidth(width) : (maxSymbolCapacity > 2) ? elideByCapacity(maxSymbolCapacity) : text + width = _metrics.boundingRect(elidedText).width + height = _metrics.boundingRect(elidedText).height + } + + // "Copy Button" handler + function copy() + { + _fullText.selectAll() + _fullText.copy() + } + + // Elides text by number of symbols + function elideByCapacity(maxSymbols) + { + var res = "" + if (text.length > maxSymbols) + { + switch (elide) + { + case Qt.ElideLeft: + res = ".." + text.slice(-maxSymbols, text.length-1) + break + case Qt.ElideMiddle: + res = text.slice(0, Math.floor(maxSymbols/2)) + ".." + text.slice(Math.floor(maxSymbols/2) + 1, text.length-1) + break + case Qt.ElideRight: + res = text.slice(0, maxSymbols) + ".." + break + default: + res = text + break + } + } + else + { + res = text + } + return res + } + + // Elides text by width + function elideByWidth(maxWidth) + { + if (_metrics.advanceWidth(text) > maxWidth) + { + var symbolsNum = (maxWidth / _metrics.averageCharacterWidth) + return elideByCapacity(Math.floor(symbolsNum)) + } + else + { + return text + } + } +} diff --git a/widgets/DapTextForm.ui.qml b/widgets/DapTextForm.ui.qml new file mode 100644 index 0000000..ea18b88 --- /dev/null +++ b/widgets/DapTextForm.ui.qml @@ -0,0 +1,43 @@ +import QtQuick 2.4 +import QtQuick.Controls 2.2 + +Item +{ + id: dapText + + ///@details Text. + property alias text: fullText.text + ///@details Text color. + property alias color: elidedText.color + ///@details Font. + property alias font: elidedText.font + ///@details Elide style. + property int elide: Qt.ElideRight + ///@details The number of symbols to display. + property int maxSymbolCapacity: 0 + ///@details Text to display. + property alias elidedText: elidedText.text + + property alias _fullText: fullText + property alias _metrics: metrics + + // Displays the text + Text + { + id: elidedText + } + + // Calculates elide + FontMetrics + { + id: metrics + font: parent.font + } + + // Stores full string to copy + TextInput + { + id: fullText + visible: false + } +} -- GitLab