28267d85371d48744d6e169ba4cdf956b700a121
[ubi] / qml / ubi / FileSelector.qml
1 import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
2 import "components"
3 import "UIConstants.js" as Const
4 import Qt 4.7
5 import Qt.labs.folderlistmodel 1.0
6
7
8 Item {
9     id: root
10
11     property string currentFilePath: fileSelector.currentFilePath
12     property bool load: fileSelector.load
13     property alias folder: folderModel.folder
14     property bool folderOnly: true
15
16     state: "invisible"
17
18     signal folderSelected(string folder)
19     signal fileSelected(string folder, string file)
20
21     height: parent.height
22     width: parent.width
23
24     function fixPath(path) {
25         path = path.toString();
26         //console.log(path);
27         var ind = path.lastIndexOf("/");
28         if(ind>=0) {
29             path = path.substr(ind+1);
30         }
31         if(path=="") path = "/";
32
33         return path;
34     }
35
36     function open() {
37         state = "visible";
38     }
39
40     function close() {
41         state = "invisible";
42     }
43
44     Rectangle {
45         id: fileSelector
46
47         width: parent.width
48         height: parent.height
49         radius: 10
50
51         property string currentFilePath: folder
52         property bool load: true
53         property string folder: folderModel.folder
54
55         state: "invisible"
56         color: Const.DEFAULT_DIALOG_BACKGROUND_COLOR
57
58         function setFolder(folder)
59         {
60             folderAnimation.folderToChange = folder;
61             folderAnimation.start();
62         }
63
64         function loadFile(filePath) {
65             engine.clearModels();
66             storageThread.loadByUrl(filePath);
67             fileSelector.currentFilePath = filePath;
68             gestureListView.currentSetFilename =
69                     helper.extractFilenameFromPath(filePath);
70
71             if (!viewSwitcher.running) {
72                 viewSwitcher.switchView(gestureListView, true);
73             }
74         }
75
76         function saveFile(filePath) {
77             helper.saveGestures(filePath, engine);
78             gestureListView.currentSetFilename =
79                     helper.extractFilenameFromPath(filePath);
80
81             if (!viewSwitcher.running) {
82                 viewSwitcher.switchView(gestureListView, true);
83             }
84         }
85
86         Rectangle {
87             id: pathController
88             width: parent.width
89             height: 80
90             anchors.top: parent.top
91             z: 1
92             color: Const.LIGHT_AUBERGINE_COLOR
93
94             Column {
95                 anchors.horizontalCenter: parent.horizontalCenter
96                 width: parent.width - 2 * Const.DEFAULT_MARGIN
97                 height: parent.height - Const.DEFAULT_MARGIN
98
99                 Spacer {}
100
101                 Row {
102                     width: parent.width
103                     spacing: Const.DEFAULT_MARGIN
104
105                     Button {
106                         id: currentFolderButton
107                         maxSize: 13
108                         label: fixPath(folderModel.folder)
109                         width: pathController.width-folderUpButton.width-cancelButton.width-4*Const.DEFAULT_MARGIN
110                         onButtonClicked: root.folderSelected(folderModel.folder)
111                         disabled: !root.folderOnly
112                     }
113                     Button {
114                         id: folderUpButton
115                         label: "up"
116                         iconSource: "images/up.png"
117                         onButtonClicked: fileSelector.setFolder(folderModel.parentFolder);
118                     }
119                     Button {
120                         id: cancelButton
121                         label: "cancel"
122                         iconSource: "images/close.png"
123                         onButtonClicked: {
124                             root.close();
125                             mask.state = "idle";
126                         }
127                     }
128                 }
129             }
130
131         } // pathController
132
133         Shadow {
134             y: pathController.height
135             z: 1
136         }
137
138         Rectangle {
139             id: folderModelContainer
140             width: parent.width
141             anchors.top: pathController.bottom;
142             anchors.bottom: parent.bottom
143             color: Const.TRANSPARENT
144             radius: 30
145
146             FolderListModel {
147                 id: folderModel
148                 //folder: "/"
149                 //nameFilters: [ "*.*" ]
150             }
151
152             Component {
153                 id: folderModelDelegate
154
155                 Rectangle {
156                     width: parent.width;
157                     height: folderModel.isFolder(index)? file.height + Const.DEFAULT_MARGIN : root.folderOnly? 0 : file.height + Const.DEFAULT_MARGIN
158                     color: Const.TRANSPARENT
159                     visible: root.folderOnly? folderModel.isFolder(index) : true
160
161                     File {
162                         id: file
163                         anchors.verticalCenter: parent.verticalCenter
164                         name: fileName
165                         filename: fileName
166                         isDirectory: folderModel.isFolder(index)
167                         textMax: root.width/17
168                         onClicked: {
169                             if(isDirectory)
170                                 fileSelector.setFolder(filePath);
171                             else
172                                 root.fileSelected(folderModel.folder,filename)
173                         }
174                     }
175                 }
176             } // Component
177
178             ListView {
179                 id: folderModelView
180
181                 anchors {
182                     fill: parent;
183                     margins: Const.TEXT_MARGIN
184                 }
185
186                 model: folderModel
187                 delegate: folderModelDelegate
188
189                 SequentialAnimation {
190                     id: folderAnimation
191
192                     property string folderToChange
193
194                     PropertyAnimation { target: folderModelView; property: "opacity"; to: 0; duration: 100 }
195                     PropertyAction { target: folderModel; property: "folder"; value: folderAnimation.folderToChange }
196                     PropertyAnimation { target: folderModelView; property: "opacity"; to: 1.0; duration: 100 }
197                 }
198             }
199
200         } // folderModelContainer
201     }
202
203     states: [
204         State {
205             name: "visible"
206             PropertyChanges { target: root; opacity: 1 }
207             PropertyChanges { target: root; y: Const.SYSTEM_BAR_HEIGHT }
208         },
209         State {
210             name: "invisible"
211             PropertyChanges { target: root; opacity: 0 }
212             PropertyChanges { target: root; y: root.parent.height }
213         }
214     ]
215
216     transitions: Transition {
217         NumberAnimation { properties: "opacity"; easing.type: Easing.InOutQuad }
218         NumberAnimation { properties: "y"; easing.type: Easing.InOutQuad }
219     }
220 }
221