diff --git a/examples/simpleexamples/Sidebar.qml b/examples/simpleexamples/Sidebar.qml --- a/examples/simpleexamples/Sidebar.qml +++ b/examples/simpleexamples/Sidebar.qml @@ -20,7 +20,7 @@ import QtQuick 2.1 import QtQuick.Controls 2.3 as Controls import QtQuick.Layouts 1.2 -import org.kde.kirigami 2.5 as Kirigami +import org.kde.kirigami 2.6 as Kirigami Kirigami.ApplicationWindow { id: root @@ -118,8 +118,23 @@ Item { Layout.fillWidth: true } - Controls.TextField { + Kirigami.TextField { + id: searchField + + placeholderText: "Search..." + + focusSequence: "Ctrl+F" + + rightAction: Kirigami.Action { + iconName: "edit-clear" + visible: searchField.text != "" + onTriggered: { + searchField.text = "" + searchField.accepted() + } + } + onAccepted: console.log("Search text is " + searchField.text) } } } diff --git a/kirigami.qrc b/kirigami.qrc --- a/kirigami.qrc +++ b/kirigami.qrc @@ -17,6 +17,7 @@ src/controls/templates/InlineMessage.qml src/controls/InlineMessage.qml src/controls/ToolBarApplicationHeader.qml + src/controls/TextField.qml src/controls/private/PrivateActionToolButton.qml src/controls/private/GlobalDrawerActionItem.qml src/controls/private/ContextDrawerActionItem.qml diff --git a/kirigami.qrc.in b/kirigami.qrc.in --- a/kirigami.qrc.in +++ b/kirigami.qrc.in @@ -17,6 +17,7 @@ @kirigami_QML_DIR@/src/controls/templates/InlineMessage.qml @kirigami_QML_DIR@/src/controls/InlineMessage.qml @kirigami_QML_DIR@/src/controls/ToolBarApplicationHeader.qml + @kirigami_QML_DIR@/src/controls/TextField.qml @kirigami_QML_DIR@/src/controls/private/PrivateActionToolButton.qml @kirigami_QML_DIR@/src/controls/private/GlobalDrawerActionItem.qml @kirigami_QML_DIR@/src/controls/private/ContextDrawerActionItem.qml diff --git a/src/controls/TextField.qml b/src/controls/TextField.qml new file mode 100644 --- /dev/null +++ b/src/controls/TextField.qml @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2019 Carl-Lucien Schwan * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library/Lesser General Public License + * version 2, or (at your option) any later version, as published by the + * Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details + * + * You should have received a copy of the GNU Library/Lesser General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +import QtQuick 2.5 +import QtQuick.Controls 2.1 +import org.kde.kirigami 2.4 as Kirigami + +/** + * This is advanced textfield. It is recommanded to us this class when there + * is a need to create a create a textfield with actions (e.g a clear action). + * + * Example usage for a search field: + * @code + * import org.kde.kirigami 2.??? as Kirigami + * + * Kirigami.TextField { + * id: searchField + * + * placeholderText: i18n("Search...") + * + * focusSequences: "Ctrl+F" + * + * rightAction: Kirigami.Action { + * iconName: "edit-clear" + * visible: searchField.text != "" + * onTriggered: { + * searchField.text = "" + * searchField.accepted() + * } + * } + * + * onAccepted: console.log("Search text is " + searchField.text) + * } + */ +TextField +{ + id: root + + /** + * focusSequence: keysequence + * This property hold the a list of shortcut sequence that put the text + * field into focus. + */ + property string focusSequence + + /** + * leftAction: QtObject + * This propery hold the action that is left in the field. By default this + * field is null. + */ + property QtObject leftAction + + /** + * rightAction: QtObject + * This propery hold the action that is right in the field. By default this + * field is null. + */ + property QtObject rightAction + + Shortcut { + id: focusShortcut + enabled: root.focusSequence != null + sequence: root.focusSequence + onActivated: { + root.forceActiveFocus() + root.selectAll() + } + } + + hoverEnabled: true + + ToolTip { + delay: Kirigami.Units.longDuration + visible: root.focusSequence != null && root.focusSequence.hovered + text: root.focusSequence != null ? root.focusSequence.nativeText : "" + } + + ToolButton { + anchors { + top: parent.top + left: parent.left + bottom: parent.bottom + margins: Kirigami.Units.smallSpacing + } + icon.name: root.leftAction != null ? root.rightAction.iconName : "" + visible: root.leftAction != null && root.leftAction.visible + onClicked: root.leftAction.trigger() + } + + ToolButton { + anchors { + top: parent.top + right: parent.right + bottom: parent.bottom + margins: Kirigami.Units.smallSpacing + } + icon.name: root.rightAction != null ? root.rightAction.iconName : "" + visible: root.rightAction != null && root.rightAction.visible + onClicked: root.rightAction.trigger() + } +} diff --git a/src/kirigamiplugin.cpp b/src/kirigamiplugin.cpp --- a/src/kirigamiplugin.cpp +++ b/src/kirigamiplugin.cpp @@ -177,6 +177,9 @@ qmlRegisterType(componentUrl(QStringLiteral("LinkButton.qml")), uri, 2, 6, "LinkButton"); qmlRegisterType(componentUrl(QStringLiteral("UrlButton.qml")), uri, 2, 6, "UrlButton"); + //2.7 + qmlRegisterType(componentUrl(QStringLiteral("TextField.qml")), uri, 2, 6, "TextField"); + qmlProtectModule(uri, 2); } diff --git a/src/qmldir b/src/qmldir --- a/src/qmldir +++ b/src/qmldir @@ -46,3 +46,4 @@ AboutPage 2.6 AboutPage.qml UrlButton 2.6 UrlButton.qml LinkButton 2.6 LinkButton.qml +TextField 2.6 TextField.qml