summaryrefslogtreecommitdiff
path: root/qml/TopicPage.qml
blob: 8ff7127fb9b981d6f93ccb3cad6796859b190be4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import QtQuick 1.1
import com.nokia.meego 1.1
import com.nokia.extras 1.1
import com.javispedro.tapasboard 1.0

Page {
	id: topicPage

	anchors.leftMargin: UiConstants.DefaultMargin
	anchors.rightMargin: UiConstants.DefaultMargin

	property Board board;
	property int topicId;

	/** -1: Do not keep view; -2: Keep view on last post; >0: Keep view on a post. */
	property int _keepView: -1;

	tools: ToolBarLayout {
		ToolIcon {
			platformIconId: "toolbar-back"
			onClicked: pageStack.pop()
		}
		ToolIcon {
			platformIconId: "toolbar-add"
			onClicked: {
				var component = Qt.createComponent("NewPostSheet.qml");
				if (component.status === Component.Ready) {
					var sheet = component.createObject(topicPage, {
														   "board": board,
														   "topicId": topicId
													   });
					sheet.open();
				}
			}
		}
		ToolIcon {
			platformIconId: board.busy ? "toolbar-cancle" : "toolbar-refresh"
			onClicked: {
				if (board.busy) {
					board.cancelAllActions();
				} else {
					topicModel.refresh();
				}
			}
		}
		ToolIcon {
			platformIconId: "toolbar-view-menu"
			onClicked: (topicMenu.status === DialogStatus.Closed) ? topicMenu.open() : topicMenu.close()
		}
	}

	Menu {
		id: topicMenu
		MenuLayout {
			MenuItem {
				text: qsTr("Go to first post");
				onClicked: postsView.positionViewAtIndex(0, ListView.Beginning);
			}
			MenuItem {
				text: qsTr("Go to last post")
				onClicked: {
					_keepView = -2; // Keep on last post
					postsView.positionViewAtEnd();
				}
			}
		}

	}

	ListView {
		id: postsView
		anchors.fill: parent
		model: TopicModel {
			id: topicModel
			board: topicPage.board
			topicId: topicPage.topicId

			onFirstUnreadPostChanged: {
				postsView.positionViewAtIndex(firstUnreadPost, ListView.Beginning);
				topicModel.markAsRead();
				_keepView = firstUnreadPost;
			}
		}
		section.property: "humanDate"
		section.criteria: ViewSection.FullString
		section.delegate: GroupHeader {
			width: parent.width
			text: section
		}

		cacheBuffer: 200

		delegate: Item {
			id: postItem

			height: postItemRectangle.height + UiConstants.ButtonSpacing * 2
			width: parent.width

			Rectangle {
				id: postItemRectangle
				width: parent.width
				height: postItemColumn.height + UiConstants.DefaultMargin
				anchors.centerIn: parent

				visible: model.postId >= 0
				color: "white"
				radius: 20

				border.width: model.unread ? 2 : 0
				border.color: "black"

				Column {
					id: postItemColumn
					anchors.left: parent.left
					anchors.right: parent.right
					anchors.margins: UiConstants.DefaultMargin
					anchors.verticalCenter: parent.verticalCenter
					spacing: 2

					Item {
						id: authorInfo

						width: parent.width
						height: childrenRect.height

						Image {
							id: authorIcon
							height: 48
							width: 48
							fillMode: Image.PreserveAspectFit
							source: model.icon
							sourceSize.height: 48
						}

						Text {
							id: authorName
							text: model.userName
							font: UiConstants.SmallTitleFont
							textFormat: Text.PlainText
						}

						Text {
							id: authorTime
							anchors.top: parent.top
							anchors.right: parent.right
							text: model.humanTime
							font: UiConstants.SubtitleFont
							textFormat: Text.PlainText
						}

						states: State {
							name: "with-image"
							when: authorIcon.status === Image.Ready

							AnchorChanges {
								target: authorName
								anchors.bottom: authorIcon.bottom
								anchors.left: authorIcon.right
							}

							PropertyChanges {
								target: authorName
								anchors.leftMargin: UiConstants.ButtonSpacing
							}
						}
					}

					Text {
						text: model.title
						width: parent.width
						font: UiConstants.TitleFont
						visible: text != ""
						textFormat: Text.PlainText
						wrapMode: Text.Wrap
					}

					Text {
						text: model.content
						width: parent.width
						font: UiConstants.SubtitleFont
						textFormat: Text.RichText
						wrapMode: Text.Wrap
						onLinkActivated: Qt.openUrlExternally(link)
					}
				}
			}
		}
	}

	Connections {
		target: _keepView !== -1 ? postsView : null
		onMovementStarted: {
			_keepView = -1; // Disable keep view
		}
		onContentHeightChanged: {
			if (_keepView == -2) {
				postsView.positionViewAtEnd();
			} else if (_keepView >= 0) {
				postsView.positionViewAtIndex(_keepView, ListView.Beginning);
			}
		}
	}

	ScrollDecorator {
		flickableItem: postsView
	}

	BusyIndicator {
		anchors.centerIn: parent
		platformStyle: BusyIndicatorStyle { size: "large" }
		visible: postsView.count == 0 && board.busy
		running: visible
	}
}