From 87406f70c46aa7d1ef61d6ef556dc56f26403e3f Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Mon, 24 Oct 2016 08:12:14 +0300 Subject: [PATCH] add initial support of tag suggestion --- .../package/contents/ui/widget.qml | 1 + .../awesome-widget/plugin/awkeyoperations.cpp | 4 +- .../package/contents/ui/widget.qml | 1 + sources/desktop-panel/plugin/dpadds.cpp | 10 +++- sources/desktop-panel/plugin/dpadds.h | 4 +- sources/qml/AWTextEditor.qml | 56 +++++++++++++++++-- 6 files changed, 65 insertions(+), 11 deletions(-) diff --git a/sources/awesome-widget/package/contents/ui/widget.qml b/sources/awesome-widget/package/contents/ui/widget.qml index 9da83cc..87699df 100644 --- a/sources/awesome-widget/package/contents/ui/widget.qml +++ b/sources/awesome-widget/package/contents/ui/widget.qml @@ -73,6 +73,7 @@ Item { AWTextEditor { id: textPattern + backend: awKeys } } diff --git a/sources/awesome-widget/plugin/awkeyoperations.cpp b/sources/awesome-widget/plugin/awkeyoperations.cpp index f1b11c1..b025552 100644 --- a/sources/awesome-widget/plugin/awkeyoperations.cpp +++ b/sources/awesome-widget/plugin/awkeyoperations.cpp @@ -155,9 +155,7 @@ QStringList AWKeyOperations::dictKeys() const for (auto item : m_graphicalItems->activeItems()) allKeys.append(item->tag(QString("bar"))); // static keys - QStringList staticKeys = QString(STATIC_KEYS).split(QChar(',')); - std::for_each(staticKeys.cbegin(), staticKeys.cend(), - [&allKeys](const QString &key) { allKeys.append(key); }); + allKeys.append(QString(STATIC_KEYS).split(QChar(','))); // sort in valid order allKeys.sort(); diff --git a/sources/desktop-panel/package/contents/ui/widget.qml b/sources/desktop-panel/package/contents/ui/widget.qml index 7c1ed11..66ab15b 100644 --- a/sources/desktop-panel/package/contents/ui/widget.qml +++ b/sources/desktop-panel/package/contents/ui/widget.qml @@ -57,6 +57,7 @@ Item { AWTextEditor { id: textPattern + backend: dpAdds } } diff --git a/sources/desktop-panel/plugin/dpadds.cpp b/sources/desktop-panel/plugin/dpadds.cpp index 6882700..9161025 100644 --- a/sources/desktop-panel/plugin/dpadds.cpp +++ b/sources/desktop-panel/plugin/dpadds.cpp @@ -72,15 +72,21 @@ int DPAdds::currentDesktop() const } -QStringList DPAdds::dictKeys(const bool, const QString) const +QStringList DPAdds::dictKeys(const bool sorted, const QString regexp) const { + qCDebug(LOG_DP) << "Should be sorted" << sorted << "and filter applied" + << regexp; + QStringList allKeys; allKeys.append(QString("mark")); allKeys.append(QString("name")); allKeys.append(QString("number")); allKeys.append(QString("total")); - return allKeys; + if (sorted) + allKeys.sort(); + + return allKeys.filter(QRegExp(regexp)); } diff --git a/sources/desktop-panel/plugin/dpadds.h b/sources/desktop-panel/plugin/dpadds.h index 9905587..f645c02 100644 --- a/sources/desktop-panel/plugin/dpadds.h +++ b/sources/desktop-panel/plugin/dpadds.h @@ -45,8 +45,8 @@ public: virtual ~DPAdds(); Q_INVOKABLE bool isDebugEnabled() const; Q_INVOKABLE int currentDesktop() const; - Q_INVOKABLE QStringList dictKeys(const bool = true, - const QString = QString()) const; + Q_INVOKABLE QStringList dictKeys(const bool sorted = true, + const QString regexp = QString()) const; Q_INVOKABLE int numberOfDesktops() const; Q_INVOKABLE QString toolTipImage(const int desktop) const; Q_INVOKABLE QString parsePattern(const QString pattern, diff --git a/sources/qml/AWTextEditor.qml b/sources/qml/AWTextEditor.qml index 55384df..03a1104 100644 --- a/sources/qml/AWTextEditor.qml +++ b/sources/qml/AWTextEditor.qml @@ -16,12 +16,60 @@ ***************************************************************************/ import QtQuick 2.0 -import QtQuick.Controls 1.3 as QtControls +import QtQuick.Controls 2.0 as QtControls -QtControls.TextArea { +Item { width: parent.width height: parent.height * 4 / 5 - textFormat: TextEdit.PlainText - text: plasmoid.configuration.text + + property var backend + property alias text: textArea.text + + QtControls.TextArea { + id: textArea + anchors.fill: parent + textFormat: TextEdit.PlainText + + QtControls.ToolTip { + id: tooltip + } + + onTextChanged: { + var currentTag = getLastTag() + // exit if there are spaces or empty + if ((currentTag.indexOf(" ") != -1) || (currentTag.length == 0)) { + tooltip.visible = false + return + } + var tags = backend.dictKeys(true, "^" + currentTag).join('\n') + // exit if no suggestion found + if (tags.length == 0) { + tooltip.visible = false + return + } + // update tooltip and show it + tooltip.text = tags + changeTooltipPosition() + tooltip.visible = true + } + } + + function changeTooltipPosition() { + tooltip.x = textArea. cursorRectangle.x + tooltip.y = textArea. cursorRectangle.y + } + + function getLastTag() { + // get substring to analyze + var substring = textArea.getText(0, textArea.cursorPosition) + // find last position of index in the given substring + var signIndex = substring.lastIndexOf('$') + 1 + if ((signIndex == 0) || (signIndex == textArea.cursorPosition)) + return "" + // get current tag text + return substring.substr(signIndex) + } } + +