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