6c534f890ed25a8ef89017e7837ca87c52aee400
[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: folderModel.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             console.log(root.folder);
61             console.log(folder);
62
63             folderAnimation.folderToChange = folder;
64             folderAnimation.start();
65         }
66
67         function loadFile(filePath) {
68             engine.clearModels();
69             storageThread.loadByUrl(filePath);
70             fileSelector.currentFilePath = filePath;
71             gestureListView.currentSetFilename =
72                     helper.extractFilenameFromPath(filePath);
73
74             if (!viewSwitcher.running) {
75                 viewSwitcher.switchView(gestureListView, true);
76             }
77         }
78
79         function saveFile(filePath) {
80             helper.saveGestures(filePath, engine);
81             gestureListView.currentSetFilename =
82                     helper.extractFilenameFromPath(filePath);
83
84             if (!viewSwitcher.running) {
85                 viewSwitcher.switchView(gestureListView, true);
86             }
87         }
88
89         Rectangle {
90             id: pathController
91             width: parent.width
92             height: 80
93             anchors.top: parent.top
94             z: 1
95             color: Const.LIGHT_AUBERGINE_COLOR
96
97             Column {
98                 anchors.horizontalCenter: parent.horizontalCenter
99                 width: parent.width - 2 * Const.DEFAULT_MARGIN
100                 height: parent.height - Const.DEFAULT_MARGIN
101
102                 Spacer {}
103
104                 Row {
105                     width: parent.width
106                     spacing: Const.DEFAULT_MARGIN
107
108                     Button {
109                         id: currentFolderButton
110                         maxSize: 13
111                         label: fixPath(folderModel.folder)
112                         width: pathController.width-folderUpButton.width-cancelButton.width-4*Const.DEFAULT_MARGIN
113                         onButtonClicked: root.folderSelected(folderModel.folder)
114                         disabled: !root.folderOnly
115                     }
116                     Button {
117                         id: folderUpButton
118                         label: "up"
119                         iconSource: "images/up.png"
120                         onButtonClicked: fileSelector.setFolder(folderModel.parentFolder);
121                     }
122                     Button {
123                         id: cancelButton
124                         label: "cancel"
125                         iconSource: "images/close.png"
126                         onButtonClicked: {
127                             root.close();
128                             mask.state = "idle";
129                         }
130                     }
131                 }
132             }
133
134         } // pathController
135
136         Shadow {
137             y: pathController.height
138             z: 1
139         }
140
141         Rectangle {
142             id: folderModelContainer
143             width: parent.width
144             anchors.top: pathController.bottom;
145             anchors.bottom: parent.bottom
146             color: Const.TRANSPARENT
147             radius: 30
148
149             FolderListModel {
150                 id: folderModel
151                 //folder: "/"
152                 //nameFilters: [ "*.*" ]
153             }
154
155             Component {
156                 id: folderModelDelegate
157
158                 Rectangle {
159                     width: parent.width;
160                     height: folderModel.isFolder(index)? file.height + Const.DEFAULT_MARGIN : root.folderOnly? 0 : file.height + Const.DEFAULT_MARGIN
161                     color: Const.TRANSPARENT
162                     visible: root.folderOnly? folderModel.isFolder(index) : true
163
164                     File {
165                         id: file
166                         anchors.verticalCenter: parent.verticalCenter
167                         name: fileName
168                         filename: fileName
169                         isDirectory: folderModel.isFolder(index)
170                         textMax: root.width/17
171                         onClicked: {
172                             if(isDirectory)
173                                 fileSelector.setFolder(filePath);
174                             else
175                                 root.fileSelected(folderModel.folder,filename)
176                         }
177                     }
178                 }
179             } // Component
180
181             ListView {
182                 id: folderModelView
183
184                 anchors {
185                     fill: parent;
186                     margins: Const.TEXT_MARGIN
187                 }
188
189                 model: folderModel
190                 delegate: folderModelDelegate
191
192                 SequentialAnimation {
193                     id: folderAnimation
194
195                     property string folderToChange
196
197                     PropertyAnimation { target: folderModelView; property: "opacity"; to: 0; duration: 100 }
198                     PropertyAction { target: folderModel; property: "folder"; value: folderAnimation.folderToChange }
199                     PropertyAnimation { target: folderModelView; property: "opacity"; to: 1.0; duration: 100 }
200                 }
201             }
202
203         } // folderModelContainer
204     }
205
206     states: [
207         State {
208             name: "visible"
209             PropertyChanges { target: root; opacity: 1 }
210             PropertyChanges { target: root; y: Const.SYSTEM_BAR_HEIGHT }
211         },
212         State {
213             name: "invisible"
214             PropertyChanges { target: root; opacity: 0 }
215             PropertyChanges { target: root; y: root.parent.height }
216         }
217     ]
218
219     transitions: Transition {
220         NumberAnimation { properties: "opacity"; easing.type: Easing.InOutQuad }
221         NumberAnimation { properties: "y"; easing.type: Easing.InOutQuad }
222     }
223 }
224