diff --git a/libdap-qt-ui-qml.qrc b/libdap-qt-ui-qml.qrc index 96a76e108ff4d1374b8f3f8d72748751567ed9ac..ee0c2a1e768eee5bad5d3e11fe1fef786814a388 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 0000000000000000000000000000000000000000..3ca718a4455e20c0a2660b515989f20561105e7f --- /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 0000000000000000000000000000000000000000..ea18b880fe65f5aa5e653b224fab502804a531fe --- /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 + } +}