| 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 1.3 |
| 19 |
|
| 20 |
/*! |
| 21 |
\qmltype Sections |
| 22 |
\inqmlmodule Lomiri.Components |
| 23 |
\ingroup lomiri |
| 24 |
\since Lomiri.Components 1.3 |
| 25 |
\brief Display a list of sections that the user can select. By tapping |
| 26 |
on a section name the \l selectedIndex will be updated, and the |
| 27 |
associated \l Action is triggered. |
| 28 |
*/ |
| 29 |
StyledItem { |
| 30 |
id: sections |
| 31 |
styleName: "SectionsStyle" |
| 32 |
activeFocusOnTab: true |
| 33 |
|
| 34 |
/*! |
| 35 |
List of actions that represent the sections. |
| 36 |
The text of each action is displayed as the section name and clicking |
| 37 |
a section will update the \l selectedIndex. |
| 38 |
|
| 39 |
When \l selectedIndex is changed (by user interaction or by setting |
| 40 |
the value), actions[selectedIndex] will be triggered. |
| 41 |
|
| 42 |
Example: |
| 43 |
\qml |
| 44 |
Sections { |
| 45 |
actions: [ |
| 46 |
Action { |
| 47 |
text: "first" |
| 48 |
onTriggered: print("one") |
| 49 |
}, |
| 50 |
Action { |
| 51 |
text: "second" |
| 52 |
onTriggered: print("two") |
| 53 |
}, |
| 54 |
Action { |
| 55 |
text: "third" |
| 56 |
onTriggered: print("three") |
| 57 |
} |
| 58 |
] |
| 59 |
} |
| 60 |
\endqml |
| 61 |
It is strongly recommended to limit the number of sections to two or three. |
| 62 |
The actions are used as the model for the Sections by default. |
| 63 |
If no trigger functions need to be specified, \l model may be used directly |
| 64 |
without setting the actions property. If both \l actions and \l model are set, |
| 65 |
model overrides the actions. |
| 66 |
*/ |
| 67 |
property list<Action> actions |
| 68 |
|
| 69 |
/*! |
| 70 |
The input model for the sections. By default model takes the \l actions |
| 71 |
as input, but if no trigger functions need to be specified, it can be |
| 72 |
simplified to a list of strings naming the sections: |
| 73 |
\qml |
| 74 |
Sections { |
| 75 |
model: [ "one", "two", "three" ] |
| 76 |
onSelectedIndexChanged: { |
| 77 |
print("Selected section " + model[selectedIndex]); |
| 78 |
} |
| 79 |
} |
| 80 |
\endqml |
| 81 |
*/ |
| 82 |
property var model: actions |
| 83 |
onModelChanged: { |
| 84 |
if (internal.done) { |
| 85 |
if (!model || model.length === 0) { |
| 86 |
selectedIndex = -1; |
| 87 |
} else if (selectedIndex === 0) { |
| 88 |
// selectedIndex does not change, but action 0 should be triggered. |
| 89 |
internal.triggerAction(0); |
| 90 |
} else { |
| 91 |
// change selectedIndex, which will trigger action 0. |
| 92 |
selectedIndex = 0; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
Component.onCompleted: { |
| 98 |
internal.done = true; |
| 99 |
internal.triggerAction(selectedIndex); |
| 100 |
} |
| 101 |
QtObject { |
| 102 |
id: internal |
| 103 |
property bool done: false |
| 104 |
|
| 105 |
/*! |
| 106 |
Triggers the action associated with the given |
| 107 |
index, if that action exists. |
| 108 |
*/ |
| 109 |
function triggerAction(index) { |
| 110 |
if ((index >= 0) && (index < model.length)) { |
| 111 |
if (model[index].hasOwnProperty("trigger")) { |
| 112 |
model[index].trigger(); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/*! |
| 119 |
The index of the currently selected section in \l model. |
| 120 |
The default value is 0 if there is at least 1 section, or -1 for no sections. |
| 121 |
When the model is changed, selectedIndex is reset to 0 and the first action |
| 122 |
is triggered. |
| 123 |
Upon completion of the Sections component, if there is an Action associated |
| 124 |
with the selected index, that Action will be triggered. |
| 125 |
*/ |
| 126 |
property int selectedIndex: model ? 0 : -1 |
| 127 |
|
| 128 |
onSelectedIndexChanged: { |
| 129 |
internal.triggerAction(selectedIndex); |
| 130 |
} |
| 131 |
} |