QtMvvm  1.1.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
PresentingStackView.qml
1 import QtQuick 2.10
2 import QtQuick.Controls 2.3
3 
38 StackView {
39  id: _presenterStack
40 
54  property int animDuration: 150
69  property int opDuration: 75
70 
72  property var _clearItems: []
73 
86  function presentItem(item) {
87  if(typeof item.presentAsRoot == "boolean" && item.presentAsRoot) {
88  if(safeReplace(null, item))
89  return true;
90  else
91  return false;
92  } else {
93  if(push(item))
94  return true;
95  else
96  return false;
97  }
98  }
99 
109  function closeAction() {
110  if(_presenterStack.currentItem &&
111  typeof _presenterStack.currentItem.closeAction == "function") {
112  if(_presenterStack.currentItem.closeAction())
113  return true;
114  }
115 
116  if(_presenterStack.safePop())
117  return true;
118  else
119  return false;
120  }
121 
134  function safePop(item, operation) {
135  var resItem = pop(item, operation)
136  if(resItem) {
137  _clearItems.push(resItem);
138  return true;
139  } else
140  return false;
141  }
142 
157  function safeReplace(target, item, properties, operation) {
158  var items = [];
159  for(var i = depth -1; i >= 0; i--) {
160  var cItem = get(i, StackView.ForceLoad);
161  _clearItems.push(cItem);
162  if(cItem === target)
163  break;
164  }
165  if(replace(target, item, properties, operation))
166  return true;
167  else
168  return false;
169  }
170 
172  function clearWaitingItems() {
173  _clearItems.forEach(function(item) {
174  if(typeof item.afterPop == "function")
175  item.afterPop();
176  item.destroy();
177  });
178  _clearItems = [];
179  }
180 
181  pushEnter: Transition {
182  PropertyAnimation {
183  property: "y"
184  easing.type: Easing.InOutQuad
185  from: height * 0.3
186  to: 0
187  duration: _presenterStack.animDuration
188  }
189  PropertyAnimation {
190  property: "opacity"
191  from: 0.0
192  to: 1.0
193  duration: _presenterStack.opDuration
194  }
195  }
196  pushExit: Transition {
197  PauseAnimation {
198  duration: _presenterStack.animDuration
199  }
200 
201  onRunningChanged: {
202  if(!running)
203  Qt.callLater(clearWaitingItems)
204  }
205  }
206  popEnter: Transition {
207  PauseAnimation {
208  duration: _presenterStack.animDuration
209  }
210  }
211  popExit: Transition {
212  PropertyAnimation {
213  property: "y"
214  easing.type: Easing.InOutQuad
215  from: 0
216  to: height * 0.3
217  duration: _presenterStack.animDuration
218  }
219  SequentialAnimation {
220  PauseAnimation {
221  duration: _presenterStack.animDuration - _presenterStack.opDuration
222  }
223  PropertyAnimation {
224  id: pp1
225  property: "opacity"
226  from: 1.0
227  to: 0.0
228  duration: _presenterStack.opDuration
229  }
230  }
231 
232  onRunningChanged: {
233  if(!running)
234  Qt.callLater(clearWaitingItems)
235  }
236  }
237  replaceEnter: pushEnter
238  replaceExit: pushExit
239 }