~mrqtros/ubuntu-rssreader-app/ubuntu-rssreader-app-to-tabs

« back to all changes in this revision

Viewing changes to shorts/qml/components/ArticleViewItem.qml

  • Committer: Roman Shchekin
  • Date: 2015-07-04 08:38:18 UTC
  • Revision ID: mrqtros@gmail.com-20150704083818-ecojm3nmy5bpkxrb
Merge with the Reboot project.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  license GPL v3 ...........
 
3
 
 
4
  description of this file:
 
5
  a page for viewing a user selected RSS feed ;
 
6
 
 
7
*/
 
8
 
 
9
import QtQuick 2.4
 
10
import QtQuick.XmlListModel 2.0
 
11
import Ubuntu.Components 1.2
 
12
import Ubuntu.Components.ListItems 1.0 as ListItem
 
13
import Ubuntu.Components.Popups 1.0
 
14
 
 
15
import "../utils/dateutils.js" as DateUtils
 
16
import "../utils/databasemodule_v2.js" as DB
 
17
 
 
18
Item {
 
19
    id: articleViewRoot
 
20
 
 
21
    signal articleStatusChanged(int tagId, int articleId, string status)
 
22
    signal articleFavouriteChanged(var article, string favourite)
 
23
 
 
24
    property bool showEmptyPlaceholder: true
 
25
    property bool isEmbeddedMode: false // Deprecated for now.
 
26
 
 
27
    property string feedTitle: ""
 
28
    property var articleModel: null
 
29
    property var modelItem: null
 
30
 
 
31
    property bool __preventIndexChangeHandler: false
 
32
 
 
33
    function setFeed(model, index) {
 
34
        /* Setting new model and not-null index will cause two change events instead of one.
 
35
         * Settings "preventIndexChangeHandler" to true helps to avoid it.
 
36
         */
 
37
        if (articleModel != model && index !== 0)
 
38
            __preventIndexChangeHandler = true
 
39
        articleModel = model
 
40
        setNewIndex(index)
 
41
    }
 
42
 
 
43
    function setNewIndex(rssIndex) {
 
44
        rssListview.currentIndex = rssIndex
 
45
        rssListview.positionViewAtIndex(rssListview.currentIndex, ListView.Center)
 
46
    }
 
47
 
 
48
    function showNextArticle() {
 
49
        var index = rssListview.currentIndex + 1
 
50
        if (index < rssListview.model.count) {
 
51
            rssListview.currentIndex = index
 
52
        }
 
53
    }
 
54
 
 
55
    function showPrevArticle() {
 
56
        var index = rssListview.currentIndex - 1
 
57
        if (index >= 0) {
 
58
            rssListview.currentIndex = index
 
59
        }
 
60
    }
 
61
 
 
62
 
 
63
    //////////////////////////////////////////////      a listview to show the RSS content
 
64
    ListView {
 
65
        id: rssListview
 
66
 
 
67
        property int contentFontSize: optionsKeeper.fontSize()
 
68
        property bool contentDarkTheme: optionsKeeper.useDarkTheme()
 
69
 
 
70
        width: parent.width
 
71
        anchors {
 
72
            bottom: parent.bottom
 
73
            bottomMargin: units.gu(1)
 
74
            top: parent.top
 
75
            topMargin: units.gu(1)
 
76
        }
 
77
 
 
78
        model: articleModel
 
79
        delegate: xmlDelegate
 
80
        snapMode: ListView.SnapOneItem
 
81
        cacheBuffer: 90 // value in pixels, for what?
 
82
        // boundsBehavior: Flickable.StopAtBounds
 
83
        orientation: ListView.Horizontal
 
84
        contentHeight: parent.width * count
 
85
        highlightFollowsCurrentItem: true
 
86
        highlightMoveVelocity: 1000
 
87
        highlightRangeMode: ListView.StrictlyEnforceRange
 
88
        clip: true
 
89
 
 
90
        onCurrentIndexChanged: {
 
91
            console.log("ListView onCurrentIndexChanged", currentIndex, __preventIndexChangeHandler)
 
92
 
 
93
            if (__preventIndexChangeHandler) {
 
94
                __preventIndexChangeHandler = false
 
95
                return
 
96
            }
 
97
 
 
98
            if (articleModel == null || articleModel.get == undefined) {
 
99
                console.log("---- Stange behavior ----")
 
100
                console.trace()
 
101
                return
 
102
            }
 
103
 
 
104
            if (articleModel.count == 0) // It is normal bevaviour.
 
105
                return
 
106
 
 
107
            modelItem = articleModel.get(currentIndex)
 
108
            feedTitle = modelItem.feed_name
 
109
 
 
110
            if (modelItem.status != "1") {
 
111
                var dbResult = DB.updateArticleStatus(modelItem.id, "1")
 
112
                if (dbResult.rowsAffected == 1) {
 
113
                    articleStatusChanged(modelItem.tagId, modelItem.id, "1")
 
114
                }
 
115
            }
 
116
        }
 
117
 
 
118
        Label {
 
119
            // We want to show it only when view isn't initialized.
 
120
            visible: !articleModel && articleViewRoot.showEmptyPlaceholder
 
121
            anchors.centerIn: parent
 
122
            text: i18n.tr("Select article")
 
123
            fontSize: "large"
 
124
        }
 
125
    }
 
126
 
 
127
    //////////////////////////////////////////////      delegate for ListView
 
128
    Component {
 
129
        id: xmlDelegate
 
130
 
 
131
        Flickable {
 
132
            id: scrollArea
 
133
            objectName: "articleview_flickable"
 
134
 
 
135
            clip: true
 
136
 
 
137
            width: rssListview.width
 
138
            height: rssListview.height
 
139
 
 
140
            contentWidth: width
 
141
            contentHeight: innerAreaColumn.height
 
142
 
 
143
            Column {
 
144
                id: innerAreaColumn
 
145
 
 
146
                spacing: units.gu(2)
 
147
                width: parent.width
 
148
 
 
149
                property int mediaDownloadInProgress: 0
 
150
 
 
151
                function mediaDownloaded() {
 
152
                    mediaDownloadInProgress = mediaDownloadInProgress - 1
 
153
                }
 
154
 
 
155
                Row {
 
156
                    width: parent.width - units.gu(4)
 
157
                    height: labelTime.paintedHeight
 
158
                    anchors.horizontalCenter: parent.horizontalCenter
 
159
                    spacing: units.gu(1)
 
160
 
 
161
                    Image {
 
162
                        id: imgFavourite
 
163
                        anchors.verticalCenter: labelTime.verticalCenter
 
164
                        fillMode: Image.PreserveAspectCrop
 
165
                        source: Qt.resolvedUrl("/img/qml/icons/favorite-selected.svg")
 
166
                        sourceSize.height: modelItem == null ? 0 : (modelItem.favourite == "1" ? units.gu(2) : 0)
 
167
                        visible: modelItem == null ? false : (modelItem.favourite == "1")
 
168
                        smooth: true
 
169
                    }
 
170
 
 
171
                    Label {
 
172
                        id: labelTime
 
173
                        text: DateUtils.formatRelativeTime(i18n, pubdate)
 
174
                        fontSize: "small"
 
175
                        width: parent.width - units.gu(3)
 
176
                        wrapMode: Text.WrapAtWordBoundaryOrAnywhere
 
177
                    }
 
178
                }
 
179
 
 
180
                Label {
 
181
                    objectName: "articleviewitem_title"
 
182
                    fontSize: "large"
 
183
                    text: model.title
 
184
                    anchors.horizontalCenter: parent.horizontalCenter
 
185
                    width: parent.width - units.gu(4)
 
186
                    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
 
187
                }
 
188
 
 
189
                Label {
 
190
                    //                    fontSize: "large"
 
191
                    color: "grey"
 
192
                    text: model.author ? model.author : ""
 
193
                    anchors.horizontalCenter: parent.horizontalCenter
 
194
                    width: parent.width - units.gu(4)
 
195
                    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
 
196
                    visible: text !== "" && text !== '""'
 
197
                }
 
198
 
 
199
                Label {
 
200
                    text: model.content
 
201
 
 
202
                    fontSize: {
 
203
                        switch(rssListview.contentFontSize) {
 
204
                        case 0:
 
205
                            return "small"
 
206
                        case 1:
 
207
                            return "medium"
 
208
                        case 2:
 
209
                            return "large"
 
210
                        }
 
211
                    }
 
212
                    // color: rssListview.contentDarkTheme ? "lightgray" : "white" // TODO BUG design needed
 
213
                    linkColor: "lightblue"
 
214
                    anchors.horizontalCenter: parent.horizontalCenter
 
215
                    width: parent.width - units.gu(4)
 
216
                    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
 
217
                    clip: true
 
218
 
 
219
                    onLinkActivated: Qt.openUrlExternally(model.link)
 
220
                }
 
221
 
 
222
                Label {
 
223
                    id: label_feedname
 
224
                    text: model.feed_name ? model.feed_name : ""
 
225
                    fontSize: "small"
 
226
                    anchors.horizontalCenter: parent.horizontalCenter
 
227
                    width: parent.width - units.gu(4)
 
228
                    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
 
229
                }
 
230
            } // Column
 
231
 
 
232
        } // Flickable
 
233
    } // Component
 
234
 
 
235
    Component {
 
236
        id: readingOptionsPopoverComponent
 
237
        ReadingOptions { }
 
238
    } // Comp
 
239
}