QtMvvm  1.1.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
DateEdit.qml
1 import QtQuick 2.10
2 import QtQuick.Controls 2.3
3 import QtQuick.Layouts 1.3
4 import Qt.labs.calendar 1.0
5 import de.skycoder42.QtMvvm.Quick 1.1
6 
17 ListView {
18  id: _calenderList
19 
31  property date firstDate: new Date(1970, 0, 1)
43  property date lastDate: new Date(9999, 11, 31)
55  property date date: _p.today();
56 
57  QtObject {
58  id: _p
59 
60  property bool _skipNextFocus: false
61 
62  function today() {
63  var cDate = new Date();
64  cDate.setHours(0, 0, 0, 0, 0);
65  return cDate;
66  }
67 
68  function focusDate() {
69  if(_skipNextFocus)
70  _skipNextFocus = false;
71  else {
72  _calenderList.currentIndex = calendarModel.indexOf(_calenderList.date);
73  _calenderList.positionViewAtIndex(_calenderList.currentIndex, ListView.SnapPosition);
74  }
75  }
76  }
77 
78  Component.onCompleted: _p.focusDate()
79  onDateChanged: _p.focusDate()
80 
81  implicitWidth: 300
82  implicitHeight: 200
83 
84  interactive: true
85  keyNavigationWraps: true
86  snapMode: ListView.SnapOneItem
87  orientation: ListView.Horizontal
88  highlightRangeMode: ListView.StrictlyEnforceRange
89  clip: true
90 
91  model: CalendarModel {
92  id: calendarModel
93  from: _calenderList.firstDate
94  to: _calenderList.lastDate
95  }
96 
97  delegate: GridLayout {
98  width: _calenderList.width
99  height: _calenderList.height
100  columns: 4
101 
102  Button {
103  Layout.rowSpan: 3
104  Layout.fillHeight: true
105  Layout.preferredWidth: 36
106  flat: true
107 
108  icon.name: "go-previous"
109  icon.source: "qrc:/de/skycoder42/qtmvvm/quick/icons/ic_navigate_before.svg"
110  icon.width: 24
111  icon.height: 24
112 
113  onClicked: _calenderList.decrementCurrentIndex()
114  }
115 
116  Label {
117  Layout.columnSpan: 2
118  Layout.fillWidth: true
119  horizontalAlignment: Text.AlignHCenter
120  font.bold: true
121  text: grid.title
122  }
123 
124  Button {
125  Layout.rowSpan: 3
126  Layout.fillHeight: true
127  Layout.preferredWidth: 36
128  flat: true
129 
130  icon.name: "go-next"
131  icon.source: "qrc:/de/skycoder42/qtmvvm/quick/icons/ic_navigate_next.svg"
132  icon.width: 24
133  icon.height: 24
134 
135  onClicked: _calenderList.incrementCurrentIndex()
136  }
137 
138  Item {
139  Layout.fillWidth: true
140  } // spacer
141 
142  DayOfWeekRow {
143  Layout.fillWidth: true
144  delegate: Label {
145  text: model.shortName
146  horizontalAlignment: Text.AlignHCenter
147  verticalAlignment: Text.AlignVCenter
148  }
149  }
150 
151  WeekNumberColumn {
152  month: model.month
153  year: model.year
154  Layout.fillHeight: true
155  delegate: Label {
156  text: model.weekNumber
157  horizontalAlignment: Text.AlignHCenter
158  verticalAlignment: Text.AlignVCenter
159  }
160  }
161 
162  MonthGrid {
163  id: grid
164  month: model.month
165  year: model.year
166  Layout.fillWidth: true
167  Layout.fillHeight: true
168 
169  delegate: Label {
170  id: dayDelegate
171 
172  ColorHelper {
173  id: helper
174  }
175 
176  readonly property bool isCurrent: model.day === _calenderList.date.getDate() && model.month === _calenderList.date.getMonth()
177  readonly property alias highlightColor: helper.highlight
178 
179  horizontalAlignment: Text.AlignHCenter
180  verticalAlignment: Text.AlignVCenter
181  opacity: model.month === grid.month ? 1 : 0.5
182  text: model.day
183  font: grid.font
184  color: isCurrent ? QuickPresenter.accentTextColor(highlightColor, helper.text) : helper.text
185 
186  background: Rectangle {
187  readonly property double size: Math.max(dayDelegate.width, dayDelegate.height) * 1.2
188 
189  anchors.centerIn: parent
190  height: size
191  width: size
192 
193  color: dayDelegate.highlightColor
194  opacity: dayDelegate.isCurrent ? 1 : 0
195  radius: size / 2
196 
197  Behavior on opacity {
198  NumberAnimation {
199  duration: 750
200  easing.type: Easing.OutBack
201  }
202  }
203  }
204 
205  MouseArea {
206  anchors.fill: parent
207 
208  onClicked: {
209  if(model.month < grid.month) {
210  _calenderList.decrementCurrentIndex();
211  _p._skipNextFocus = true;
212  } else if(model.month > grid.month) {
213  _calenderList.incrementCurrentIndex();
214  _p._skipNextFocus = true;
215  }
216  _calenderList.date = model.date;
217  }
218  }
219  }
220  }
221 
222  Item {
223  Layout.fillWidth: true
224  Layout.columnSpan: 4
225  Layout.minimumHeight: 0
226  Layout.maximumHeight: 0
227  } // spacer, only needed for the additional padding below the month grid
228  }
229 }
color highlight
The color to use to highlight stuff.
Definition: ColorHelper.qml:35
A helper class to get style-dependant colors.
Definition: ColorHelper.qml:17
QTMVVM_REVISION_1 static Q_INVOKABLE QColor accentTextColor(const QColor &accentColor, const QColor &baseColor) const
Calculates the optimal text color based on the background color.
The QML import for the QtMvvmQuick QML module.
Definition: ActionButton.qml:4
A QML singleton to access common presenter methods globally.