Unity 8
Showable.qml
1 /*
2  * Copyright (C) 2013 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.4
18 
19 Item {
20  id: showable
21 
22  property bool available: true
23  property bool shown: true
24 
25  /* If your showable supports on demand content creation/destruction,
26  set this to false when destroyed and true when ready to be shown.
27  NOTE: You should load your content when "required" is true and
28  destroy when "required" is false
29  */
30  property bool created: true
31  property bool required
32  property bool __shouldShow: false
33  property bool __skipShowAnimation: false
34  property bool __skipHideAnimation: false
35 
36  property list<QtObject> hides
37  property var showAnimation
38  property var hideAnimation
39 
40  // automatically set the target on showAnimation and hideAnimation to be the
41  // showable itself
42  onShowAnimationChanged: if (showAnimation) showAnimation["target"] = showable
43  onHideAnimationChanged: if (hideAnimation) hideAnimation["target"] = showable
44 
45  Component.onCompleted: required = shown;
46 
47  function __hideOthers() {
48  var i
49  for (i=0; i<hides.length; i++) {
50  hides[i].hide()
51  }
52  }
53 
54  function show() {
55  required = true;
56  if (created) {
57  __reallyShow();
58  } else {
59  __shouldShow = true;
60  }
61  }
62 
63  function showNow() {
64  __skipShowAnimation = true;
65  show();
66  }
67 
68  onCreatedChanged: {
69  if (created && __shouldShow) {
70  __reallyShow();
71  __shouldShow = false;
72  }
73  }
74 
75  function __reallyShow() {
76  if (!available) {
77  __skipShowAnimation = false;
78  return false;
79  }
80 
81  __hideOthers();
82 
83  if (hideAnimation != undefined && hideAnimation.running) {
84  hideAnimation.stop();
85  }
86 
87  if (showAnimation != undefined) {
88  if (!showAnimation.running) {
89  showAnimation.restart()
90  }
91  if (__skipShowAnimation) {
92  showAnimation.complete();
93  }
94  } else {
95  visible = true;
96  }
97 
98  shown = true;
99  __skipShowAnimation = false;
100  return true;
101  }
102 
103  /*
104  Will be called right before starting the hideAnimation.
105  */
106  property var prepareToHide: function(){}
107 
108  function hide() {
109  if (showAnimation != undefined && showAnimation.running) {
110  showAnimation.stop()
111  }
112 
113  if (typeof prepareToHide === "function") {
114  prepareToHide();
115  } else {
116  console.warn("Showable.prepareToHide should be a function, but it's a " +
117  (typeof prepareToHide) + " instead");
118  }
119 
120  if (hideAnimation != undefined) {
121  if (!hideAnimation.running) {
122  hideAnimation.restart()
123  }
124  if (__skipHideAnimation) {
125  hideAnimation.complete();
126  }
127  } else {
128  visible = false
129  required = false
130  }
131 
132  shown = false
133  __skipHideAnimation = false;
134  }
135 
136  function hideNow() {
137  __skipHideAnimation = true;
138  hide();
139  }
140 
141  Connections {
142  target: hideAnimation ? hideAnimation: null
143  onRunningChanged: {
144  if (!hideAnimation.running) {
145  required = false;
146  }
147  }
148  }
149 }