Advertisement
Guest User

CalendarView.qml

a guest
Oct 13th, 2017
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.66 KB | None | 0 0
  1. /*
  2. * Copyright 2013 Sebastian Kügler <sebas@kde.org>
  3. * Copyright 2015 Martin Klapetek <mklapetek@kde.org>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import QtQuick 2.4
  19. import QtQuick.Layouts 1.1
  20. import org.kde.plasma.core 2.0 as PlasmaCore
  21. import org.kde.plasma.calendar 2.0 as PlasmaCalendar
  22. import org.kde.plasma.components 2.0 as PlasmaComponents
  23. import org.kde.plasma.extras 2.0 as PlasmaExtras
  24.  
  25. Item {
  26. id: calendar
  27.  
  28. Layout.minimumWidth: _minimumWidth
  29. Layout.minimumHeight: _minimumHeight
  30.  
  31. // The "sensible" values
  32. property int _minimumWidth: (showAgenda ? agendaViewWidth : 0) + monthViewWidth
  33. property int _minimumHeight: units.gridUnit * 14
  34. Layout.preferredWidth: _minimumWidth
  35. Layout.preferredHeight: _minimumHeight * 1.5
  36.  
  37. readonly property bool showAgenda: PlasmaCalendar.EventPluginsManager.enabledPlugins.length > 0
  38.  
  39. readonly property int agendaViewWidth: _minimumHeight * 1.5
  40. readonly property int monthViewWidth: monthView.showWeekNumbers ? Math.round(_minimumHeight * 1.75) : Math.round(_minimumHeight * 1.5)
  41.  
  42. property int boxWidth: (agendaViewWidth + monthViewWidth - ((showAgenda ? 3 : 4) * spacing)) / 2
  43.  
  44. property int spacing: units.largeSpacing
  45. property alias borderWidth: monthView.borderWidth
  46. property alias monthView: monthView
  47.  
  48. property bool debug: false
  49.  
  50. property bool isExpanded: plasmoid.expanded
  51.  
  52. onIsExpandedChanged: {
  53. // clear all the selections when the plasmoid is showing/hiding
  54. monthView.resetToToday();
  55. }
  56.  
  57. PlasmaCore.DataSource {
  58. id: executable
  59. engine: "executable"
  60. connectedSources: []
  61. onNewData: {
  62. var exitCode = data["exit code"]
  63. var exitStatus = data["exit status"]
  64. var stdout = data["stdout"]
  65. var stderr = data["stderr"]
  66. exited(exitCode, exitStatus, stdout, stderr)
  67. disconnectSource(sourceName) // cmd finished
  68. }
  69. function exec(cmd) {
  70. connectSource(cmd)
  71. }
  72. signal exited(int exitCode, int exitStatus, string stdout, string stderr)
  73. }
  74.  
  75. Image {
  76. fillMode: Image.Stretch
  77. anchors.fill: parent
  78. source: "images/" + monthView.currentDate.getMonth()
  79.  
  80. MouseArea {
  81. anchors.fill: parent
  82. acceptedButtons: Qt.MidButton
  83. onClicked: {
  84. organize()
  85. }
  86. function organize() {
  87. executable.exec('korganizer')
  88. }
  89. }
  90. }
  91. Image {
  92. fillMode: Image.Stretch
  93. anchors.fill: parent
  94. source: "images/Matte.png"
  95. }
  96.  
  97. Item {
  98. id: agenda
  99. visible: calendar.showAgenda
  100.  
  101. width: boxWidth
  102. anchors {
  103. top: parent.top
  104. left: parent.left
  105. bottom: parent.bottom
  106. leftMargin: spacing
  107. topMargin: spacing
  108. bottomMargin: spacing
  109. }
  110.  
  111. function dateString(format) {
  112. return Qt.formatDate(monthView.currentDate, format);
  113. }
  114.  
  115. function formatDateWithoutYear(date) {
  116. // Unfortunatelly Qt overrides ECMA's Date.toLocaleDateString(),
  117. // which is able to return locale-specific date-and-month-only date
  118. // formats, with its dumb version that only supports Qt::DateFormat
  119. // enum subset. So to get a day-and-month-only date format string we
  120. // must resort to this magic and hope there are no locales that use
  121. // other separators...
  122. var format = Qt.locale().dateFormat(Locale.ShortFormat).replace(/[./ ]*Y{2,4}[./ ]*/i, '');
  123. return Qt.formatDate(date, format);
  124. }
  125.  
  126. Connections {
  127. target: monthView
  128.  
  129. onCurrentDateChanged: {
  130. // Apparently this is needed because this is a simple QList being
  131. // returned and if the list for the current day has 1 event and the
  132. // user clicks some other date which also has 1 event, QML sees the
  133. // sizes match and does not update the labels with the content.
  134. // Resetting the model to null first clears it and then correct data
  135. // are displayed.
  136. holidaysList.model = null;
  137. holidaysList.model = monthView.daysModel.eventsForDate(monthView.currentDate);
  138. }
  139. }
  140.  
  141. Connections {
  142. target: monthView.daysModel
  143.  
  144. onAgendaUpdated: {
  145. // Checks if the dates are the same, comparing the date objects
  146. // directly won't work and this does a simple integer subtracting
  147. // so should be fastest. One of the JS weirdness.
  148. if (updatedDate - monthView.currentDate === 0) {
  149. holidaysList.model = null;
  150. holidaysList.model = monthView.daysModel.eventsForDate(monthView.currentDate);
  151. }
  152. }
  153. }
  154.  
  155. Connections {
  156. target: plasmoid.configuration
  157.  
  158. onEnabledCalendarPluginsChanged: {
  159. PlasmaCalendar.EventPluginsManager.enabledPlugins = plasmoid.configuration.enabledCalendarPlugins;
  160. }
  161. }
  162.  
  163. PlasmaComponents.Label {
  164. id: dayLabel
  165. anchors.left: parent.left
  166. height: dayHeading.height + dateHeading.height
  167. width: paintedWidth
  168. font.pixelSize: height
  169. font.weight: Font.Light
  170. text: agenda.dateString("dd")
  171. opacity: 0.6
  172. }
  173.  
  174. PlasmaExtras.Heading {
  175. id: dayHeading
  176. anchors {
  177. top: parent.top
  178. left: dayLabel.right
  179. right: parent.right
  180. leftMargin: spacing / 2
  181. }
  182. level: 1
  183. elide: Text.ElideRight
  184. text: agenda.dateString("dddd")
  185. }
  186. PlasmaComponents.Label {
  187. id: dateHeading
  188. anchors {
  189. top: dayHeading.bottom
  190. left: dayLabel.right
  191. right: parent.right
  192. leftMargin: spacing / 2
  193. }
  194. elide: Text.ElideRight
  195. text: Qt.locale().standaloneMonthName(monthView.currentDate.getMonth())
  196. + agenda.dateString(" yyyy")
  197. }
  198.  
  199. TextMetrics {
  200. id: dateLabelMetrics
  201.  
  202. // Date/time are arbitrary values with all parts being two-digit
  203. readonly property string timeString: Qt.formatTime(new Date(2000, 12, 12, 12, 12, 12, 12))
  204. readonly property string dateString: agenda.formatDateWithoutYear(new Date(2000, 12, 12, 12, 12, 12))
  205.  
  206. font: theme.defaultFont
  207. text: timeString.length > dateString.length ? timeString : dateString
  208. }
  209.  
  210. PlasmaExtras.ScrollArea {
  211. id: holidaysView
  212. anchors {
  213. top: dateHeading.bottom
  214. left: parent.left
  215. right: parent.right
  216. bottom: parent.bottom
  217. }
  218. flickableItem.boundsBehavior: Flickable.StopAtBounds
  219.  
  220. ListView {
  221. id: holidaysList
  222.  
  223. delegate: PlasmaComponents.ListItem {
  224. id: eventItem
  225. property bool hasTime: {
  226. // Explicitly all-day event
  227. if (modelData.isAllDay) {
  228. return false;
  229. }
  230. // Multi-day event which does not start or end today (so
  231. // is all-day from today's point of view)
  232. if (modelData.startDateTime - monthView.currentDate < 0 &&
  233. modelData.endDateTime - monthView.currentDate > 86400000) { // 24hrs in ms
  234. return false;
  235. }
  236.  
  237. // Non-explicit all-day event
  238. var startIsMidnight = modelData.startDateTime.getHours() == 0
  239. && modelData.startDateTime.getMinutes() == 0;
  240.  
  241. var endIsMidnight = modelData.endDateTime.getHours() == 0
  242. && modelData.endDateTime.getMinutes() == 0;
  243.  
  244. var sameDay = modelData.startDateTime.getDate() == modelData.endDateTime.getDate()
  245. && modelData.startDateTime.getDay() == modelData.endDateTime.getDay()
  246.  
  247. if (startIsMidnight && endIsMidnight && sameDay) {
  248. return false
  249. }
  250.  
  251. return true;
  252. }
  253.  
  254. GridLayout {
  255. columns: 3
  256. rows: 2
  257. rowSpacing: 0
  258. columnSpacing: 2 * units.smallSpacing
  259.  
  260. width: parent.width
  261.  
  262. Rectangle {
  263. id: eventColor
  264.  
  265. Layout.row: 0
  266. Layout.column: 0
  267. Layout.rowSpan: 2
  268. Layout.fillHeight: true
  269.  
  270. color: modelData.eventColor
  271. width: 5 * units.devicePixelRatio
  272. visible: modelData.eventColor !== ""
  273. }
  274.  
  275. PlasmaComponents.Label {
  276. id: startTimeLabel
  277.  
  278. readonly property bool startsToday: modelData.startDateTime - monthView.currentDate >= 0
  279. readonly property bool startedYesterdayLessThan12HoursAgo: modelData.startDateTime - monthView.currentDate >= -43200000 //12hrs in ms
  280.  
  281. Layout.row: 0
  282. Layout.column: 1
  283. Layout.minimumWidth: dateLabelMetrics.width
  284.  
  285. text: startsToday || startedYesterdayLessThan12HoursAgo
  286. ? Qt.formatTime(modelData.startDateTime)
  287. : agenda.formatDateWithoutYear(modelData.startDateTime)
  288. horizontalAlignment: Qt.AlignRight
  289. visible: eventItem.hasTime
  290. }
  291.  
  292. PlasmaComponents.Label {
  293. id: endTimeLabel
  294.  
  295. readonly property bool endsToday: modelData.endDateTime - monthView.currentDate <= 86400000 // 24hrs in ms
  296. readonly property bool endsTomorrowInLessThan12Hours: modelData.endDateTime - monthView.currentDate <= 86400000 + 43200000 // 36hrs in ms
  297.  
  298. Layout.row: 1
  299. Layout.column: 1
  300. Layout.minimumWidth: dateLabelMetrics.width
  301.  
  302. text: endsToday || endsTomorrowInLessThan12Hours
  303. ? Qt.formatTime(modelData.endDateTime)
  304. : agenda.formatDateWithoutYear(modelData.endDateTime)
  305. horizontalAlignment: Qt.AlignRight
  306. enabled: false
  307.  
  308. visible: eventItem.hasTime
  309. }
  310.  
  311. PlasmaComponents.Label {
  312. id: eventTitle
  313.  
  314. Layout.row: 0
  315. Layout.column: 2
  316. Layout.fillWidth: true
  317.  
  318. font.weight: Font.Bold
  319. elide: Text.ElideRight
  320. text: modelData.title
  321. verticalAlignment: Text.AlignVCenter
  322. }
  323.  
  324. PlasmaComponents.Label {
  325. id: eventDescription
  326.  
  327. Layout.row: 1
  328. Layout.column: 2
  329. Layout.fillWidth: true
  330.  
  331. elide: Text.ElideRight
  332. text: modelData.description
  333. verticalAlignment: Text.AlignVCenter
  334. maximumLineCount: 1
  335. enabled: false
  336.  
  337. visible: text !== ""
  338. }
  339. }
  340. }
  341.  
  342. section.property: "modelData.eventType"
  343. section.delegate: PlasmaExtras.Heading {
  344. level: 3
  345. elide: Text.ElideRight
  346. text: section
  347. }
  348. }
  349. }
  350.  
  351. PlasmaExtras.Heading {
  352. anchors.fill: holidaysView
  353. anchors.leftMargin: units.largeSpacing
  354. anchors.rightMargin: units.largeSpacing
  355. text: monthView.isToday(monthView.currentDate) ? i18n("No events for today")
  356. : i18n("No events for this day");
  357. level: 3
  358. opacity: 0.4
  359. visible: holidaysList.count == 0
  360. }
  361.  
  362. }
  363. Item {
  364. id: cal
  365. width: boxWidth
  366. anchors {
  367. top: parent.top
  368. right: parent.right
  369. bottom: parent.bottom
  370. margins: spacing
  371. }
  372.  
  373. PlasmaCalendar.MonthView {
  374. id: monthView
  375. borderOpacity: 0.25
  376. today: root.tzDate
  377. showWeekNumbers: plasmoid.configuration.showWeekNumbers
  378. anchors.fill: parent
  379. }
  380.  
  381. }
  382.  
  383. // Allows the user to keep the calendar open for reference
  384. PlasmaComponents.ToolButton {
  385. anchors.right: parent.right
  386. width: Math.round(units.gridUnit * 1.25)
  387. height: width
  388. checkable: true
  389. iconSource: "window-pin"
  390. onCheckedChanged: plasmoid.hideOnWindowDeactivate = !checked
  391. }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement