QtMvvm  1.0.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.depth <= 1)
117  return false;
118  else {
119  if(_presenterStack.safePop())
120  return true;
121  else
122  return false;
123  }
124  }
125 
138  function safePop(item, operation) {
139  var resItem = pop(item, operation)
140  if(resItem) {
141  _clearItems.push(resItem);
142  return true;
143  } else
144  return false;
145  }
146 
161  function safeReplace(target, item, properties, operation) {
162  var items = [];
163  console.log("current depth: ", depth)
164  for(var i = depth -1; i >= 0; i--) {
165  var cItem = get(i, StackView.ForceLoad);
166  _clearItems.push(cItem);
167  if(cItem === target)
168  break;
169  }
170  if(replace(target, item, properties, operation))
171  return true;
172  else
173  return false;
174  }
175 
177  function clearWaitingItems() {
178  _clearItems.forEach(function(item) {
179  if(typeof item.afterPop == "function")
180  item.afterPop();
181  item.destroy();
182  });
183  _clearItems = [];
184  }
185 
186  pushEnter: Transition {
187  PropertyAnimation {
188  property: "y"
189  easing.type: Easing.InOutQuad
190  from: height * 0.3
191  to: 0
192  duration: _presenterStack.animDuration
193  }
194  PropertyAnimation {
195  property: "opacity"
196  from: 0.0
197  to: 1.0
198  duration: _presenterStack.opDuration
199  }
200  }
201  pushExit: Transition {
202  PauseAnimation {
203  duration: _presenterStack.animDuration
204  }
205 
206  onRunningChanged: {
207  if(!running)
208  Qt.callLater(clearWaitingItems)
209  }
210  }
211  popEnter: Transition {
212  PauseAnimation {
213  duration: _presenterStack.animDuration
214  }
215  }
216  popExit: Transition {
217  PropertyAnimation {
218  property: "y"
219  easing.type: Easing.InOutQuad
220  from: 0
221  to: height * 0.3
222  duration: _presenterStack.animDuration
223  }
224  SequentialAnimation {
225  PauseAnimation {
226  duration: _presenterStack.animDuration - _presenterStack.opDuration
227  }
228  PropertyAnimation {
229  id: pp1
230  property: "opacity"
231  from: 1.0
232  to: 0.0
233  duration: _presenterStack.opDuration
234  }
235  }
236 
237  onRunningChanged: {
238  if(!running)
239  Qt.callLater(clearWaitingItems)
240  }
241  }
242  replaceEnter: pushEnter
243  replaceExit: pushExit
244 }