| 1 |
/* |
| 2 |
* Copyright 2015 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
import QtQuick 2.4 |
| 18 |
import Lomiri.Components as Toolkit |
| 19 |
|
| 20 |
/*! |
| 21 |
\qmltype Tab |
| 22 |
\inqmlmodule Lomiri.Components |
| 23 |
\ingroup lomiri |
| 24 |
\deprecated |
| 25 |
\brief Component to represent a single tab in a \l Tabs environment. |
| 26 |
|
| 27 |
Examples: See \l Tabs. |
| 28 |
*/ |
| 29 |
Toolkit.PageTreeNode { |
| 30 |
id: tab |
| 31 |
|
| 32 |
anchors.fill: parent ? parent : undefined |
| 33 |
|
| 34 |
/*! |
| 35 |
The title that is shown on the tab button used to select this tab. |
| 36 |
*/ |
| 37 |
property string title |
| 38 |
|
| 39 |
/*! |
| 40 |
\deprecated |
| 41 |
The location of the icon that is displayed inside the button used to select this tab (optional). |
| 42 |
Either \l title or iconSource, or both must be defined. |
| 43 |
Deprecated because our new tab buttons in the header do not display an icon. |
| 44 |
*/ |
| 45 |
property url iconSource |
| 46 |
|
| 47 |
/*! |
| 48 |
The contents of the Tab. Use a \l Page or a Loader that instantiates a Component or |
| 49 |
loads an external \l Page. |
| 50 |
When using a Loader, do not set the anchors or dimensions of the Loader so that the |
| 51 |
\l Page can control the height and prevent overlapping the header. |
| 52 |
Example: |
| 53 |
\qml |
| 54 |
import QtQuick 2.4 |
| 55 |
import Lomiri.Components |
| 56 |
MainView { |
| 57 |
width: units.gu(40) |
| 58 |
height: units.gu(50) |
| 59 |
|
| 60 |
Component { |
| 61 |
id: pageComponent |
| 62 |
Page { |
| 63 |
Label { |
| 64 |
anchors.centerIn: parent |
| 65 |
text: "Loaded when tab is selected." |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
Tabs { |
| 70 |
id: tabs |
| 71 |
Tab { |
| 72 |
title: i18n.tr("Simple page") |
| 73 |
page: Page { |
| 74 |
Label { |
| 75 |
anchors.centerIn: parent |
| 76 |
text: i18n.tr("Always loaded") |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
Tab { |
| 81 |
id: loaderTab |
| 82 |
title: i18n.tr("Page loader") |
| 83 |
page: Loader { |
| 84 |
// no anchors |
| 85 |
id: loader |
| 86 |
sourceComponent: tabs.selectedTab == loaderTab ? pageComponent : null |
| 87 |
onStatusChanged: if (loader.status == Loader.Ready) console.log('Loaded') |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
\endqml |
| 93 |
*/ |
| 94 |
property Item page: null |
| 95 |
|
| 96 |
// In case the page is a Loader with a Page inside, the Loader needs to be |
| 97 |
// anchored to the bottom of the Tab. Width and height are set by the Page. |
| 98 |
Binding { |
| 99 |
target: page |
| 100 |
property: "anchors.bottom" |
| 101 |
value: tab.bottom |
| 102 |
when: page |
| 103 |
} |
| 104 |
|
| 105 |
/*! |
| 106 |
\qmlproperty int index |
| 107 |
\readonly |
| 108 |
The property holds the index of the tab within the Tabs. |
| 109 |
*/ |
| 110 |
readonly property alias index: internal.index |
| 111 |
|
| 112 |
/*! |
| 113 |
When page is updated, set its parent to be tab. |
| 114 |
*/ |
| 115 |
onPageChanged: if (page) page.parent = tab |
| 116 |
|
| 117 |
/*! |
| 118 |
The tab is active when it is the selected tab of its parent Tabs item. |
| 119 |
Setting tab to active will automatically make child nodes active. |
| 120 |
*/ |
| 121 |
active: parentNode && parentNode.active && |
| 122 |
parentNode.hasOwnProperty("selectedTab") && parentNode.selectedTab === tab |
| 123 |
|
| 124 |
visible: active |
| 125 |
|
| 126 |
/*! |
| 127 |
\internal |
| 128 |
*/ |
| 129 |
onTitleChanged: { |
| 130 |
if (active) { |
| 131 |
// ensure the parent node is an instance of Tabs |
| 132 |
if (parentNode.hasOwnProperty("selectedTab")) { |
| 133 |
parentNode.modelChanged(); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/*! |
| 139 |
\internal |
| 140 |
*/ |
| 141 |
property alias __protected: internal |
| 142 |
Item { |
| 143 |
id: internal |
| 144 |
/* |
| 145 |
Specifies the index of the Tab in Tabs. |
| 146 |
*/ |
| 147 |
property int index: -1 |
| 148 |
|
| 149 |
/* |
| 150 |
Specifies whether the Tab has already been inserted in Tabs model or not. |
| 151 |
Pre-declared tabs are added one by one automatically before Tabs component |
| 152 |
completion, therefore we need this flag to exclude adding those Tab elements |
| 153 |
again which were already added. |
| 154 |
*/ |
| 155 |
property bool inserted: false |
| 156 |
|
| 157 |
/* |
| 158 |
Specifies whether the Tab was created dynamically or not. A dynamically created |
| 159 |
Tab is destroyed upon removal. |
| 160 |
*/ |
| 161 |
property bool dynamic: false |
| 162 |
|
| 163 |
/* |
| 164 |
This flag is used by the Tabs to determine whether the pre-declared Tab was removed |
| 165 |
from the Tabs model or not. The flag guards adding back pre-declared tabs upon Tabs |
| 166 |
component stack (children) change. |
| 167 |
*/ |
| 168 |
property bool removedFromTabs: false |
| 169 |
|
| 170 |
/*! |
| 171 |
Triggering this action will select the tab. Used by the tabs OverflowPanel. |
| 172 |
*/ |
| 173 |
property alias action: selectTabAction |
| 174 |
Toolkit.Action { |
| 175 |
id: selectTabAction |
| 176 |
text: tab.title |
| 177 |
objectName: "select_tab_"+index |
| 178 |
iconSource: tab.iconSource |
| 179 |
onTriggered: { |
| 180 |
if (internal.index < 0) return; |
| 181 |
if (tab.parentNode && tab.parentNode.hasOwnProperty("selectedTabIndex")) { |
| 182 |
tab.parentNode.selectedTabIndex = internal.index; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |