~unity-team/unity8/trunk

« back to all changes in this revision

Viewing changes to Components/Showable.qml

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.0
 
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
 
 
34
    property list<QtObject> hides
 
35
    property var showAnimation
 
36
    property var hideAnimation
 
37
 
 
38
    // automatically set the target on showAnimation and hideAnimation to be the
 
39
    // showable itself
 
40
    onShowAnimationChanged: if (showAnimation) showAnimation["target"] = showable
 
41
    onHideAnimationChanged: if (hideAnimation) hideAnimation["target"] = showable
 
42
 
 
43
    Component.onCompleted: required = shown;
 
44
 
 
45
    function __hideOthers() {
 
46
        var i
 
47
        for (i=0; i<hides.length; i++) {
 
48
            hides[i].hide()
 
49
        }
 
50
    }
 
51
 
 
52
    function show() {
 
53
        required = true;
 
54
        if (created) {
 
55
            __reallyShow(true);
 
56
        } else {
 
57
            __shouldShow = true;
 
58
        }
 
59
    }
 
60
 
 
61
    onCreatedChanged: {
 
62
        if (created && __shouldShow) {
 
63
            __reallyShow(true);
 
64
            __shouldShow = false;
 
65
        }
 
66
    }
 
67
 
 
68
    function showWithoutAnimation() {
 
69
        __reallyShow(false);
 
70
    }
 
71
 
 
72
    function __reallyShow(animated) {
 
73
        if (!available) {
 
74
            return false;
 
75
        }
 
76
 
 
77
        __hideOthers();
 
78
 
 
79
        if (hideAnimation != undefined && hideAnimation.running) {
 
80
            hideAnimation.stop();
 
81
        }
 
82
 
 
83
        if (showAnimation != undefined) {
 
84
            if (animated) {
 
85
                if (!showAnimation.running) {
 
86
                    showAnimation.restart()
 
87
                }
 
88
            } else {
 
89
                showAnimation.start();
 
90
                showAnimation.complete();
 
91
            }
 
92
        } else {
 
93
            visible = true;
 
94
        }
 
95
 
 
96
        shown = true;
 
97
        return true;
 
98
    }
 
99
 
 
100
    function hide() {
 
101
        if (showAnimation != undefined && showAnimation.running) {
 
102
            showAnimation.stop()
 
103
        }
 
104
        if (hideAnimation != undefined) {
 
105
            if (!hideAnimation.running) {
 
106
                hideAnimation.restart()
 
107
            }
 
108
        } else {
 
109
            visible = false
 
110
            required = false
 
111
        }
 
112
 
 
113
        shown = false
 
114
    }
 
115
 
 
116
    Connections {
 
117
        target: hideAnimation ? hideAnimation: null
 
118
        onRunningChanged: {
 
119
            if (!hideAnimation.running) {
 
120
                required = false;
 
121
            }
 
122
        }
 
123
    }
 
124
}