-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAddPostView.qml
455 lines (367 loc) · 14.9 KB
/
AddPostView.qml
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import QtQuick.Controls.Styles 1.4
import QtQuick.Dialogs 1.1
Rectangle {
id: rootRectangle
anchors.fill: parent
property bool editing: false
property int postIndex
property string prevTitle
property date prevDate
property string prevContent
property string prevReaction
property int prevWeight
property int prevCalories
property bool prevRun
property var photos : [];
function reportError(txt) {
alertDialog.text = txt;
alertDialog.open();
return false;
}
function validateInput() {
if (titleField.text.trim().length < 1)
return reportError("Error. Title is Empty");
if (contentField.text.trim().length < 1)
return reportError("Error. Content is Empty");
if (!reactionGroup.current)
return reportError("Error. Choose a reaction first");
return true;
}
function initSelected(type) {
if (!editing)
return type === "yay" ? true: false;
return type === prevReaction ? true: false;
}
MessageDialog {
id: alertDialog
visible: false
title: "Alert"
text: ""
standardButtons: StandardButton.Ok
icon: StandardIcon.Warning
}
CustomToolbar {
id: mainToolBar
subtitle: "Add a new post"
Layout.preferredWidth: parent.width
ToolImageButton {
id: goBackButton
anchors.right: parent.right
imgSrc: "icons/back_black.png"
onClicked: {
mainStack.pop();
}
}
}
Rectangle {
id: contentWrapper
width: parent.width > 1200 ? 1080: parent.width * 0.9
anchors.top: mainToolBar.bottom
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
anchors.leftMargin: 10
anchors.rightMargin: 10
clip: true
Flickable {
anchors.fill: parent
contentWidth: parent.width
contentHeight: formWrapper.height + 40
interactive: true
boundsBehavior: Flickable.StopAtBounds
Column {
id: formWrapper
width: parent.width
spacing: 10
Column {
id: photosGroup
width: parent.width
spacing: 20
anchors.topMargin: 20
anchors.horizontalCenter: parent.horizontalCenter
FileDialog {
id: photosDialog
nameFilters: [ "Image files (*.jpg *.png)"]
title: "Please choose a file"
folder: shortcuts.pictures
selectFolder: false
selectMultiple: true
selectExisting: true
onAccepted: {
// check if any file got selected
if (this.fileUrls.length < 1)
return;
for (var i = 0; i < this.fileUrls.length; i++){
var photoUrl = this.fileUrls[i];
var resolvedUrl = Qt.resolvedUrl(photoUrl);
// check for duplicates
if (rootRectangle.photos.indexOf(resolvedUrl) >= 0)
continue;
// add to model
rootRectangle.photos.push(resolvedUrl);
photosModel.append({"icon": photoUrl});
// make photo path visible
photosPathView.visible = true
}
photosDialog.close();
}
onRejected: {
photosDialog.close();
}
Component.onCompleted: visible = false
}
Component {
id: pathPhotoDelegate
Column {
spacing: 2
scale: PathView.iconScale
opacity: PathView.iconOpacity
rotation: PathView.itemRotation
MouseArea {
width:64
height:64
Image {
width: parent.width
height: parent.height
source: icon
asynchronous: true
fillMode: Image.PreserveAspectFit
}
onClicked: {
console.log("Clicked:" + index);
photosPathView.currentIndex = index
selectedPhotoLarge.source = rootRectangle.photos[index]
selectedPhotoLarge.visible = true
}
onDoubleClicked: {
deleteDialog.currentIndex = index;
deleteDialog.open();
}
}
MessageDialog {
property int currentIndex: -1
id: deleteDialog
title: "May I have your attention please"
text: "Do you want to remove this image?"
icon: StandardIcon.Warning
standardButtons: StandardButton.Yes | StandardButton.No
onYes: {
if (deleteDialog.currentIndex > -1) {
if (photosModel.count == 1) {
photosPathView.visible = false;
selectedPhotoLarge.visible = false;
}
photos.splice(deleteDialog.currentIndex, 1);
photosModel.remove(deleteDialog.currentIndex, 1);
selectedPhotoLarge.source = rootRectangle.photos[photosPathView.currentIndex]
}
deleteDialog.currentIndex = -1;
}
onNo: {
deleteDialog.currentIndex = -1;
}
Component.onCompleted: visible = false
}
}
}
Text {
id: photosLabel
anchors.horizontalCenter: parent.horizontalCenter
text: "Photos"
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "upload photos"
onClicked: {
photosDialog.visible=true
}
}
ListModel {
id: photosModel
}
Image {
id: selectedPhotoLarge
anchors.horizontalCenter: parent.horizontalCenter
anchors.margins: 20
fillMode: Image.PreserveAspectCrop
width: parent.width * 0.6
height: 180
visible: false
}
PathView {
id: photosPathView
visible: false
anchors.horizontalCenter: parent.horizontalCenter
width: parent.width
height: 150
model: photosModel
delegate: pathPhotoDelegate
path: Ellipse {
width: contentWrapper.width
height: this.height - 40
}
}
}
Separator {}
Column {
id: titleGroup
width: parent.width
spacing: 12
anchors.horizontalCenter: parent.horizontalCenter
HighlightedText {
text: "Title"
}
TextField {
id: titleField
anchors.horizontalCenter: parent.horizontalCenter
width: parent.width * 0.7
style: TextFieldStyle {
padding.top: 4
padding.bottom: 4
padding.right: 20
padding.left: 20
}
font.pixelSize: 24
text: prevTitle
placeholderText: "Post title..."
}
}
Separator {}
// Reactions row
RowLayout {
height: 100
anchors.horizontalCenter: parent.horizontalCenter
spacing: 10
ExclusiveGroup { id: reactionGroup }
ReactionImage {
reaction: "angry"
selected: initSelected("angry")
}
ReactionImage {
reaction: "sad"
selected: initSelected("sad")
}
ReactionImage {
reaction: "yay"
selected: initSelected("yay")
}
ReactionImage {
reaction: "haha"
selected: initSelected("haha")
}
ReactionImage {
reaction: "wow"
selected: initSelected("wow")
}
}
Separator {}
Column {
id: contentGroup
spacing: 12
width: parent.width
anchors.horizontalCenter: parent.horizontalCenter
HighlightedText {
text: "Content"
}
TextArea {
id: contentField
anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: 18
text: prevContent
width: parent.width * 0.7
}
}
Separator {}
Column {
id: extraGroup
spacing: 10
anchors.horizontalCenter: parent.horizontalCenter
Text {
anchors.horizontalCenter: parent.horizontalCenter
text: "More information"
}
Row {
spacing: 10
Text{
anchors.verticalCenter: parent.verticalCenter
text: "Weight"
}
TextField {
id: weightField
text: prevWeight
anchors.verticalCenter: parent.verticalCenter
}
}
Row {
spacing: 10
Text{
anchors.verticalCenter: parent.verticalCenter
text: "Calories +/-"
}
TextField {
id: caloriesField
text: prevCalories
anchors.verticalCenter: parent.verticalCenter
}
}
Row {
spacing: 10
Text{
anchors.verticalCenter: parent.verticalCenter
text: "Did you run today?"
}
CheckBox {
id: runCheckBox
anchors.verticalCenter: parent.verticalCenter
checked: editing && prevRun
}
}
}
Separator {}
Button {
anchors.horizontalCenter: parent.horizontalCenter
style: ButtonStyle {
label: Text {
renderType: Text.NativeRendering
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
font.pointSize: 18
padding: 8
color: "#333333"
text: !editing ? "Add Post": "Save Changes"
}
}
onClicked: {
if (!validateInput())
return;
var title = titleField.text;
var date = new Date();
var content = contentField.text;
var reaction = reactionGroup.current.text;
var weight = parseInt(weightField.text);
var calories = parseInt(caloriesField.text);
var run = runCheckBox.checked;
if (editing) {
mediator.editPost(postIndex, title, date, content, reaction, weight, calories, run, rootRectangle.photos);
} else {
mediator.insertPost(title, date, content, reaction, weight, calories, run, rootRectangle.photos);
}
mainStack.pop();
}
}
}
}
}
Component.onCompleted: {
if (editing) {
for (var i = 0; i < photos.length; i++) {
photosModel.append({"icon": photos[i]});
// make photo path visible and run once
if (i == 0)
photosPathView.visible = true
}
}
}
}