QtMvvm  1.1.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
SearchBar.qml
1 import QtQuick 2.10
2 import QtQuick.Controls 2.3
3 import QtQuick.Layouts 1.3
4 import de.skycoder42.QtMvvm.Quick 1.1
5 
18 Item {
19  id: _searchBar
20 
30  property alias title: _titleLabel.text
43  property alias allowSearch: _searchButton.visible
53  property alias searchToolTip: _searchButton.text
64  readonly property bool inSearchMode: state == "search"
65 
78  property alias searchText: _searchField.text
90  readonly property alias searchButton: _searchButton
91 
93  function showTitle() {
94  state = "title";
95  }
96 
105  function showSearchBar(text) {
106  if(typeof text === "string")
107  searchText = text
108  state = "search";
109  }
110 
112  function toggleSearchState() {
113  if(!allowSearch)
114  return;
115  if(inSearchMode)
116  showTitle();
117  else
118  showSearchBar();
119  }
120 
121  onAllowSearchChanged: {
122  if(!allowSearch)
123  showTitle();
124  }
125 
126  RowLayout {
127  id: _barLayout
128  anchors.fill: parent
129  spacing: 0
130 
131  Item {
132  id: _labelContainer
133 
134  Layout.fillWidth: true
135  Layout.fillHeight: true
136  Layout.leftMargin: 16
137 
138  ToolBarLabel {
139  id: _titleLabel
140  anchors.fill: parent
141  leftPadding: 0
142  visible: !_searchField.visible
143  }
144 
145  TextField {
146  id: _searchField
147  horizontalAlignment: Qt.AlignLeft
148  verticalAlignment: Qt.AlignVCenter
149  anchors.right: parent.right
150  anchors.verticalCenter: parent.verticalCenter
151  height: Math.min(implicitHeight, parent.height)
152  width: parent.width
153  }
154  }
155 
156  ActionButton {
157  id: _searchButton
158  text: qsTr("Search…")
159  onClicked: toggleSearchState()
160  }
161  }
162 
163  states: [
164  State {
165  name: "title"
166  PropertyChanges {
167  target: _searchButton
168  icon.name: "search"
169  icon.source: "qrc:/de/skycoder42/qtmvvm/quick/icons/ic_search.svg"
170  }
171  PropertyChanges {
172  target: _titleLabel
173  visible: true
174  }
175  PropertyChanges {
176  target: _searchField
177  visible: false
178  width: 0
179  }
180  StateChangeScript {
181  name: "clearScript"
182  script: _searchField.clear();
183  }
184  },
185  State {
186  name: "search"
187  PropertyChanges {
188  target: _searchButton
189  icon.name: "gtk-close"
190  icon.source: "qrc:/de/skycoder42/qtmvvm/quick/icons/ic_close.svg"
191  }
192  PropertyChanges {
193  target: _titleLabel
194  visible: false
195  }
196  PropertyChanges {
197  target: _searchField
198  visible: true
199  width: _labelContainer.width
200  }
201  StateChangeScript {
202  name: "focusScript"
203  script: _searchField.forceActiveFocus();
204  }
205  }
206  ]
207 
208  transitions: [
209  Transition {
210  from: "title"
211  to: "search"
212  SequentialAnimation {
213  PropertyAnimation {
214  target: _searchField
215  property: "visible"
216  duration: 0
217  }
218  PropertyAnimation {
219  target: _searchField
220  property: "width"
221  duration: 250
222  easing.type: Easing.InOutCubic
223  }
224  PropertyAnimation {
225  target: _titleLabel
226  property: "visible"
227  duration: 0
228  }
229  }
230  },
231  Transition {
232  from: "search"
233  to: "title"
234  SequentialAnimation {
235  PropertyAnimation {
236  target: _titleLabel
237  property: "visible"
238  duration: 0
239  }
240  PropertyAnimation {
241  target: _searchField
242  property: "width"
243  duration: 250
244  easing.type: Easing.InOutCubic
245  }
246  PropertyAnimation {
247  target: _searchField
248  property: "visible"
249  duration: 0
250  }
251  }
252  }
253  ]
254 
255  state: "title"
256 }
An extension of the Label for better appearance when used in a toolbar.
The QML import for the QtMvvmQuick QML module.
Definition: ActionButton.qml:4
An extension of the ToolButton for better appearance.