a0779692bfca8fde79c01e7d85bcdcece964ff2a
[feedingit] / psa_harmattan / feedingit / qml / ArticleViewer.qml
1 import Qt 4.7
2
3 Item {
4     id: articleViewer
5     //width: 480; height: 360;
6     width: parent.width; height: parent.height;
7     //property string feedid: "61ac1458d761423344998dc76770e36e" //articlesItem.feedid;
8     //property string hideReadArticles: "";
9     property alias articleShown: articleView.visible;
10     property bool zoomEnabled: false;
11     property bool vertPanningEnabled: true
12
13     function modulo(x,y) {
14         // Fixes modulo for negative numbers
15         return ((x%y)+y)%y;
16     }
17
18     function reload() {
19         articles.xml = articleViewer.feedid == "" ? "<?xml version=\"1.0\" encoding=\"utf-8\"?><xml></xml>" : controller.getArticlesXml(articleViewer.feedid);
20         articles.reload()
21     }
22
23     function next() {
24         if (articleView.visible) {
25             //articleView.positionViewAtIndex(modulo(articleView.currentIndex+1, articleView.count), ListView.Contain);
26             articleView.incrementCurrentIndex();
27         }
28     }
29
30     function prev() {
31         if (articleView.visible) {
32             //articleView.positionViewAtIndex(modulo(articleView.currentIndex-1, articleView.count), ListView.Contain);
33             articleView.decrementCurrentIndex();
34         }
35     }
36
37     function markAllAsRead() {
38         if (feedid!="") {
39             controller.markAllAsRead(feedid)
40             articles.reload();
41         }
42     }
43
44     function viewArticle(articleid) {
45         var index = 0;
46         for (var i=0; i<articleList.count; ++i) {
47             if (articles.get(0).articleid==articleid) {
48                 index = i;
49             }
50         }
51         articleView.positionViewAtIndex(index, ListView.Contain); articleView.visible = true;
52     }
53
54     ListView {
55         id: articleList; model: visualModel.parts.list; z: 6
56         width: parent.width; height: parent.height; /*x: 0;*/
57         cacheBuffer: 100;
58         flickDeceleration: 1500
59     }
60
61     ListView {
62         id: articleView; model: visualModel.parts.flip; orientation: ListView.Horizontal
63         width: parent.width; height: parent.height; visible: false; z:8
64         //onCurrentIndexChanged: photosGridView.positionViewAtIndex(currentIndex, GridView.Contain)
65         highlightRangeMode: ListView.StrictlyEnforceRange; snapMode: ListView.SnapOneItem
66         //cacheBuffer: 5;
67         onMovementStarted: articleViewer.vertPanningEnabled=false;
68         onMovementEnded: articleViewer.vertPanningEnabled=true;
69         highlightMoveDuration: 300;
70     }
71
72     Rectangle {
73         id: noArticle
74         //width: parent.width; height: parent.height;
75         //color: "#000000"
76         anchors.centerIn: parent;
77         visible: false;
78         z:8;
79         Text { id: noText; color: "#ffffff"; anchors.centerIn: parent; text: qsTr("No articles available"); }
80         Image { id: loadingImage; anchors.centerIn: parent; source: "common/images/loading.png";
81             height: 96; width: 96;
82             NumberAnimation on rotation {
83                 from: 0; to: 360; running: (loadingImage.visible == true); loops: Animation.Infinite; duration: 900
84             }
85         }
86
87         states: [ State {
88             name: "noArticle"; when: articles.count==0 && articles.status==XmlListModel.Ready
89             PropertyChanges { target: noArticle; visible: true; }
90             PropertyChanges { target: loadingImage; visible: false; }
91             PropertyChanges { target: noText; visible: true; }
92             }, State {
93             name: "loading"; when: articles.count==0 && articles.status != XmlListModel.Ready
94             PropertyChanges { target: noArticle; visible: true; }
95             PropertyChanges { target: noText; visible: false; }
96             PropertyChanges { target: loadingImage; visible: true; }
97             }
98         ]
99     }
100
101     VisualDataModel {
102         id: visualModel;
103         delegate: Package {
104                         id: packageItem
105                         Item { id: flipItem; Package.name: 'flip';  width: articleViewer.width; height: articleViewer.height;
106
107                             property string url: (articleView.visible && Math.abs(articleView.currentIndex-index)<2) ? path: ""; //http://localhost:8000/html/" + articleViewer.feedid + "/" + articleid : "";
108                             property string html: controller.getArticle(articleViewer.feedid, articleid)
109                             ArticleDisplay {
110                                 zoomEnabled: articleViewer.zoomEnabled;
111                                 property bool vertPanningEnabled: articleViewer.vertPanningEnabled;
112
113                                 states: [ State {
114                                         name: 'articleIsRead';
115                                     when: articleView.visible && articleView.currentIndex == index;
116                                     StateChangeScript {
117                                         name: "myScript"
118                                         script: {
119                                             flipItem.url=path; //"http://localhost:8000/html/" + articleViewer.feedid + "/" + articleid;
120                                             controller.setEntryRead(articleViewer.feedid, articleid)
121                                         }
122                                     }
123                                     }, State {
124                                         name: 'articleIsClose'; when: articleView.visible && Math.abs(articleView.currentIndex-index)<2;
125                                         StateChangeScript {
126                                             script: { flipItem.url=path; } //"http://localhost:8000/html/" + articleViewer.feedid + "/" + articleid;}
127                                         }
128                                     }
129                                 ]
130                             }
131                         }
132
133                         Item { Package.name: 'list';
134                                 id: wrapper; width: articleViewer.width; height: 86
135                                 Item {
136                                     id: moveMe
137                                     Rectangle { id: backRect; color: "black"; opacity: index % 2 ? 0.2 : 0.4; height: 84; width: wrapper.width; y: 1 }
138                                     Text {
139                                         anchors.fill: backRect
140                                         anchors.margins: 5
141                                         verticalAlignment: Text.AlignVCenter; text: title; color: (unread=="True") ? "white" : "#7b97fd";
142                                         width: wrapper.width; wrapMode: Text.WordWrap; font.bold: false;
143                                     }
144                                 }
145                                 MouseArea { anchors.fill: wrapper;
146                                     onClicked: { articleView.positionViewAtIndex(index, ListView.Contain); articleView.visible = true; }
147                                 }
148                         }
149                     }
150         model: articles
151     }
152
153     XmlListModel {
154         id: articles
155
156         //source: articleViewer.feedid == "" ? "" : "http://localhost:8000/articles/" + feedid + "?onlyUnread=" + hideReadArticles
157         //xml: articleViewer.feedid == "" ? "" : controller.getArticlesXml(articleViewer.feedid)
158         query: "/xml/article"
159
160         XmlRole { name: "title"; query: "title/string()" }
161         XmlRole { name: "articleid"; query: "articleid/string()"; isKey: true }
162         XmlRole { name: "path"; query: "path/string()" }
163         XmlRole { name: "unread"; query: "unread/string()"; isKey: true}
164     }
165
166
167 }