Kstars

TimePage.qml
1 // SPDX-FileCopyrightText: 2016 Artem Fedoskin <[email protected]>
2 // SPDX-License-Identifier: GPL-2.0-or-later
3 
4 import QtQuick.Controls 2.0
5 import QtQuick 2.6
6 import QtQuick.Layouts 1.1
7 import Qt.labs.calendar 1.0
8 import "../constants" 1.0
9 
10 KSPage {
11  //Change it to Popup when it will become more stable
12  id: timePopup
13  title: xi18n("Set Time")
14 
15  property date currentDate: new Date()
16 
17  property int userYear: tumblerYear.model[tumblerYear.currentIndex]
18  property int userMonth: tumblerMonth.currentIndex
19  property int userDay: tumblerDay.currentIndex + 1
20  property int userHour: tumblerHour.currentIndex
21  property int userMinutes: tumblerMinute.currentIndex
22  //property int userWeek:
23  signal updateCalendar()
24  signal updateTumblers()
25 
26  function range(start, end) {
27  var list = [];
28  for (var i = start; i <= end; i++) {
29  list.push(i);
30  }
31  return list
32  }
33 
34  function getWeekNumber(year,month,day) {
35  var d = new Date(year,month,day);
36  d.setHours(0,0,0);
37  // Set to nearest Thursday: current date + 4 - current day number
38  // Make Sunday's day number 7
39  d.setDate(d.getDate() + 4 - (d.getDay()||7));
40  // Get first day of year
41  var yearStart = new Date(d.getFullYear(),0,1);
42  // Calculate full weeks to nearest Thursday
43  var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7)
44  // Return array of year and week number
45  return weekNo;
46  }
47 
48  function weeksInYear(year) {
49  var d = new Date(year, 11, 31);
50  var week = getWeekNumber(d)[1];
51  return week == 1? getWeekNumber(d.setDate(24))[1] : week;
52  }
53 
54  function daysInMonth(month, year) {
55  return new Date(year, month, 0).getDate();
56  }
57 
58  function setCurrentDate() {
59  currentDate = new Date()
60  tumblerYear.setYear(currentDate.getFullYear())
61  tumblerMonth.setMonth(currentDate.getMonth())
62  tumblerDay.setDay(currentDate.getDate())
63  tumblerHour.setHour(currentDate.getHours())
64  tumblerMinute.setMinute(currentDate.getMinutes())
65  tumblerWeek.setWeek(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate())
66  }
67 
68  GridLayout {
69  anchors.centerIn: parent
70  id: timeGrid
71  columnSpacing: 10
72 
73  flow: !window.isPortrait ? GridLayout.LeftToRight : GridLayout.TopToBottom
74 
75  ColumnLayout {
76  Layout.fillHeight: true
77  Layout.fillWidth: true
78  spacing: 0
79  RowLayout {
80  anchors {
81  left: parent.left
82  right: parent.right
83  }
84 
85  Button {
86  text: "<"
87  anchors{
88  left: parent.left
89  verticalCenter: parent.verticalCenter
90  }
91  onClicked: {
92  if(userMonth - 1 == -1) {
93  tumblerMonth.setMonth(11)
94  tumblerYear.setYear(userYear - 1)
95  } else {
96  tumblerMonth.setMonth(userMonth - 1)
97  }
98  }
99  }
100 
101  KSText {
102  font.pointSize: 14
103  text: Qt.locale().standaloneMonthName(userMonth) + " " + monthGrid.year
104  anchors.centerIn: parent
105  }
106 
107  Button {
108  text: ">"
109  anchors{
110  verticalCenter: parent.verticalCenter
111  right: parent.right
112  }
113  onClicked: {
114  if(userMonth + 1 == 12) {
115  tumblerMonth.setMonth(0)
116  tumblerYear.setYear(userYear + 1)
117  } else {
118  tumblerMonth.setMonth(userMonth + 1)
119  }
120  }
121  }
122  }
123 
124  GridLayout {
125  id: calendar
126  columns: 2
127  /*Layout.fillHeight: true
128  Layout.fillWidth: true*/
129  Layout.minimumHeight: 150
130  Layout.minimumWidth: 150
131  /*Layout.maximumHeight: 250
132  Layout.maximumWidth: 250*/
133 
134  DayOfWeekRow {
135  Layout.column: 1
136  Layout.fillWidth: true
137  }
138 
139  WeekNumberColumn {
140  id: weeks
141  month: monthGrid.month
142  year: monthGrid.year
143  locale: monthGrid.locale
144  Layout.fillHeight: true
145  }
146 
147  MonthGrid {
148  id: monthGrid
149  spacing: 0
150  month: userMonth
151  year: userYear
152  property int day: userDay
153  Layout.fillWidth: true
154  Layout.fillHeight: true
155 
156  delegate: Rectangle {
157  id: rect
158  height: 30
159  width: 30
160  color: "#00FFFFFF"
161  property color highlightColor: Num.sysPalette.highlight
162  property int selectedDay // Initialize with userDay
163 
164  Connections {
165  target: timePopup
166  onUserDayChanged: {
167  if(timePopup.userDay == model.day && model.month === monthGrid.month)
168  rect.color = highlightColor
169  else rect.color = "#00FFFFFF"
170  }
171  }
172 
173  border {
174  width: 1
175  color: "#DCDCDC"
176  }
177 
178  KSText {
179  anchors.centerIn: parent
180  horizontalAlignment: Text.AlignHCenter
181  verticalAlignment: Text.AlignVCenter
182  opacity: model.month === monthGrid.month ? 1 : 0.5
183  text: model.day
184  font: monthGrid.font
185  }
186 
187  MouseArea {
188  anchors.fill: parent
189 
190  onClicked: {
191  if(model.month != monthGrid.month) {
192  monthGrid.month = model.month
193  userMonth = model.month
194  } else {
195  rect.color = highlightColor
196  userDay = model.day
197  }
198  updateTumblers()
199  }
200  }
201  }
202 
203  }
204  }
205  }
206 
207  ColumnLayout {
208  id: tumblersCol
209 
210  GridLayout {
211  id: tumblersGrid
212  //FIX IT: Didn't find a better way to make tumblers smaller
213  property int tumblerWidth: 35
214  property int tumblerHeight: 50
215  Layout.fillWidth: true
216  flow: window.isPortrait ? Flow.LeftToRight : Flow.TopToBottom
217 
218  GroupBox {
219  id: dateGB
220  Layout.fillWidth: true
221 
222  RowLayout {
223  anchors {
224  left: parent.left
225  right: parent.right
226  }
227 
228  Layout.fillWidth: true
229 
230  ColumnLayout {
231  KSLabel {
232  id:labelYear
233  text: xi18n("Year")
234  }
235  Tumbler {
236  id: tumblerYear
237  implicitHeight: tumblersGrid.tumblerHeight
238  implicitWidth: tumblersGrid.tumblerWidth
239 
240  model: range(1616,2500)
241  anchors.horizontalCenter: labelYear.horizontalCenter
242  property bool setByCalendar: false
243  Connections {
244  target: timePopup
245  onUpdateTumblers: {
246  tumblerYear.setByCalendar = true
247  tumblerYear.setYear(userYear)
248  }
249  }
250 
251  Component.onCompleted: {
252  setYear(currentDate.getFullYear())
253  }
254 
255  onCurrentIndexChanged: {
256  if(!setByCalendar) {
257  //userDate.setFullYear(model[currentIndex])
258  updateCalendar()
259  }
260  setByCalendar = false
261  tumblerMonth.checkDays()
262  }
263 
264  function setYear(year) {
265  var index = model.indexOf(year)
266  if(index != -1) currentIndex = index
267  }
268  }
269  }
270 
271  ColumnLayout {
272  Layout.fillHeight: true
273  KSLabel {
274  id:labelMonth
275  text: xi18n("Month")
276  }
277 
278  Tumbler {
279  id: tumblerMonth
280  model: range(1,12)
281  implicitHeight: tumblersGrid.tumblerHeight
282  implicitWidth: tumblersGrid.tumblerWidth
283 
284  anchors.horizontalCenter: labelMonth.horizontalCenter
285  property bool setByCalendar: false
286  //implicitWidth: labelMonth.width
287 
288  Component.onCompleted: {
289  setMonth(currentDate.getMonth())
290  }
291 
292  Connections {
293  target: timePopup
294  onUpdateTumblers: {
295  tumblerMonth.setByCalendar = true
296  tumblerMonth.setMonth(userMonth)//userDate.getMonth())
297  }
298  }
299 
300  onCurrentIndexChanged: {
301  userMonth = currentIndex
302  checkDays()
303  }
304 
305  //Called whenever we change month or year. Handles number of days in month
306  function rangeDays() {
307  var month = currentIndex+1
308  var year = tumblerYear.model[tumblerYear.currentIndex]
309 
310  return range(1,daysInMonth(month,year))
311  }
312 
313  function setMonth(month) {
314  if(month >= 0 && month <= 11)
315  currentIndex = month
316  }
317 
318  function checkDays() {
319  tumblerDay.changeModel(rangeDays())
320  }
321  }
322  }
323 
324  ColumnLayout {
325  visible: false
326  KSLabel {
327  id:labelWeek
328  text: xi18n("Week")
329  }
330 
331  Tumbler {
332  id: tumblerWeek
333  model: range(1,52)
334  anchors.horizontalCenter: labelWeek.horizontalCenter
335  implicitHeight: tumblersGrid.tumblerHeight
336  implicitWidth: tumblersGrid.tumblerWidth
337  property bool setByCalendar: false
338 
339  onCurrentIndexChanged: {
340  if(!setByCalendar) {
341  //Start counting weeks from the beginning of year
342  /*var day = userDay
343  day.setMonth(0)
344  day.setDate(1)
345 
346  day.setDate((currentIndex+1)*7)
347  tumblerMonth.setMonth(day.getMonth())
348  tumblerDay.setDay(day.getDate())
349 
350  userDate = day
351 
352  updateCalendar()*/
353  }
354  setByCalendar = false
355  }
356 
357  Connections {
358  target: timePopup
359  onUpdateTumblers: {
360  tumblerWeek.setByCalendar = true
361  tumblerWeek.setWeek(userYear, userMonth + 1, userDay)
362  }
363  }
364 
365  function setWeek(year, month, day) {
366  currentIndex = getWeekNumber(year, month, day) - 1
367  }
368  }
369  }
370 
371  ColumnLayout {
372  KSLabel {
373  id:labelDay
374  text: xi18n("Day")
375  }
376 
377  Tumbler {
378  id: tumblerDay
379  implicitHeight: tumblersGrid.tumblerHeight
380  implicitWidth: tumblersGrid.tumblerWidth
381  anchors.horizontalCenter: labelDay.horizontalCenter
382 
383  model: range(1,daysInMonth(currentDate.getMonth() + 1,currentDate.getFullYear()))
384  property bool setByCalendar: false
385 
386  function changeModel(newModel) {
387  var prevIndex = currentIndex
388  if(!model || model.length !== newModel.length) {
389  model = newModel
390  if(prevIndex >= 0 && prevIndex < model.length) currentIndex = prevIndex
391  }
392  }
393 
394  Connections {
395  target: timePopup
396  onUpdateTumblers: {
397  tumblerDay.setByCalendar = true
398  tumblerDay.setDay(userDay)
399  }
400  }
401 
402  onCurrentIndexChanged: {
403  userDay = currentIndex + 1
404  }
405 
406  Component.onCompleted: {
407  setDay(currentDate.getDate())
408  }
409 
410  function setDay(day) {
411  if(day > 0 && day <= model.length)
412  currentIndex = day - 1
413  }
414  }
415  }
416  }
417  }
418 
419  RowLayout {
420  anchors {
421  left: parent.left
422  right: parent.right
423  }
424 
425  Button {
426  visible: !window.isPortrait
427  anchors {
428  left: parent.left
429  bottom: parent.bottom
430  }
431 
432  onClicked: {
433  setCurrentDate()
434  }
435 
436  text: xi18n("Now")
437  }
438 
439  GroupBox {
440  id: timeGB
441  anchors.right: parent.right
442 
443  RowLayout {
444  Layout.fillWidth: true
445  ColumnLayout {
446  KSLabel {
447  id: labelHour
448  text: xi18n("Hour")
449  }
450 
451  Tumbler {
452  id: tumblerHour
453  model: 24
454  implicitHeight: tumblersGrid.tumblerHeight
455  implicitWidth: tumblersGrid.tumblerWidth
456  anchors.horizontalCenter: labelHour.horizontalCenter
457 
458  delegate: KSText {
459  text: modelData < 10 ? "0" + modelData : modelData
460  font: tumblerHour.font
461  horizontalAlignment: Text.AlignHCenter
462  verticalAlignment: Text.AlignVCenter
463  opacity: 1.0 - Math.abs(Tumbler.displacement) / (tumblerHour.visibleItemCount / 2)
464  }
465 
466  Component.onCompleted: {
467  setHour(currentDate.getHours())
468  }
469 
470  function setHour(hour) {
471  currentIndex = hour
472  }
473  }
474  }
475 
476  ColumnLayout {
477  KSLabel {
478  id:labelMinute
479  text: xi18n("Min.")
480  }
481 
482  Tumbler {
483  id: tumblerMinute
484  model: 60
485  implicitHeight: tumblersGrid.tumblerHeight
486  implicitWidth: tumblersGrid.tumblerWidth
487  anchors.horizontalCenter: labelMinute.horizontalCenter
488 
489  delegate: KSText {
490  text: modelData < 10 ? "0" + modelData : modelData
491  font: tumblerHour.font
492  horizontalAlignment: Text.AlignHCenter
493  verticalAlignment: Text.AlignVCenter
494  opacity: 1.0 - Math.abs(Tumbler.displacement) / (tumblerHour.visibleItemCount / 2)
495  }
496 
497  Component.onCompleted: {
498  setMinute(currentDate.getMinutes())
499  }
500 
501  function setMinute(minutes) {
502  currentIndex = minutes
503  }
504  }
505  }
506  }
507  }
508  }
509  }
510 
511  RowLayout {
512  //height: childrenRect.height
513  Layout.fillWidth: true
514  anchors {
515  left: parent.left
516  right: parent.right
517  }
518 
519  Button {
520  visible: window.isPortrait
521  anchors {
522  left: parent.left
523  }
524 
525  onClicked: {
526  setCurrentDate()
527  console.log(Projector)
528  }
529 
530  text: xi18n("Now")
531  }
532 
533  Button {
534  visible: !window.isPortrait
535  text: xi18n("Ok")
536  anchors {
537  left: parent.left
538  }
539  onClicked: {
540  var date = new Date(userYear, userMonth, userDay, userHour, userMinutes)
541  KStarsLite.slotSetTime(date)
542  skyMapLite.notification.showNotification("Setting time to " + date)
543  stackView.pop()
544  }
545  }
546 
547  Row {
548  anchors.right: parent.right
549  spacing: 5
550 
551  Button {
552  visible: window.isPortrait
553  text: xi18n("Ok")
554  onClicked: {
555  var date = new Date(userYear, userMonth, userDay, userHour, userMinutes)
556  KStarsLite.slotSetTime(date)
557  skyMapLite.notification.showNotification("Setting time to " + date)
558  stackView.pop()
559  }
560  }
561 
562  Button {
563  text: xi18n("Cancel")
564  onClicked: {
565  stackView.pop()
566  }
567  }
568  }
569  }
570 
571  }
572  }
573 }
QTextStream & right(QTextStream &stream)
This class loads QML files and connects SkyMapLite and KStarsData Unlike KStars class it is not a mai...
Definition: kstarslite.h:46
QTextStream & left(QTextStream &stream)
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
QString xi18n(const char *text, const TYPE &arg...)
QTextStream & left(QTextStream &s)
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
LocaleWrapper locale()
QTextStream & right(QTextStream &s)
void slotSetTime(QDateTime time)
sets time and date according to parameter time
Definition: kstarslite.cpp:326
QString right(int n) const const
KJOBWIDGETS_EXPORT QWidget * window(KJob *job)
const QList< QKeySequence > & end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 15 2022 04:04:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.