TimeDiffPage.qml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. import QtQuick 1.1
  2. import com.nokia.meego 1.0
  3. import com.nokia.extras 1.0
  4. import "calcJSLib.js" as CalcJSLib
  5. Page {
  6. id: timeDiffPage
  7. tools: commonTools
  8. Rectangle {
  9. id: container
  10. color: "#4c0094"
  11. anchors.left: parent.left
  12. anchors.leftMargin: 0
  13. anchors.top: parent.top
  14. width: timeDiffPage.width
  15. height: timeDiffPage.height
  16. gradient: Gradient {
  17. GradientStop {
  18. position: 0.00;
  19. color: "#c7b8fa";
  20. }
  21. GradientStop {
  22. position: 0.60;
  23. color: "#ffffff";
  24. }
  25. }
  26. SipAttributes {
  27. id:customSipAttributes
  28. actionKeyEnabled: true
  29. actionKeyHighlighted: true
  30. actionKeyLabel: qsTr("Calculate")
  31. }
  32. Row {
  33. id: rowAppLogoName
  34. Image {
  35. id: appLogo
  36. x: 0; y: 0
  37. width: 64; height: 64
  38. source: "qrc:/icon_withoutBG"
  39. MouseArea {
  40. anchors.fill: parent
  41. onClicked: appWindow.pageStack.push(aboutPage)
  42. }
  43. }
  44. Text {
  45. id: txtAppName
  46. x: 80
  47. y: 10
  48. width: timeDiffPage.width - x
  49. text: qsTr("Time Calculator")
  50. font.pixelSize: 30
  51. font.bold: true
  52. // horizontalAlignment: Text.AlignHCenter
  53. color: "#3f0064"
  54. MouseArea {
  55. anchors.fill: parent
  56. onClicked: appWindow.pageStack.push(aboutPage)
  57. }
  58. }
  59. }
  60. Flickable {
  61. id: flick
  62. anchors.left: parent.left
  63. anchors.leftMargin: 0
  64. anchors.top: rowAppLogoName.bottom
  65. contentHeight: container.width >= 800 ? container.height * 2.2 : container.height * 1
  66. width: container.width
  67. height: container.height
  68. clip: true
  69. flickableDirection: Flickable.VerticalFlick
  70. boundsBehavior: Flickable.DragOverBounds
  71. Row {
  72. x: 10
  73. y: 30
  74. Text {
  75. width: timeDiffPage.width - x
  76. text: qsTr("Get the time difference between")
  77. font.pixelSize: 25
  78. font.bold: true
  79. color: "#800078"
  80. }
  81. }
  82. Row {
  83. id: rowDatePicker
  84. x: 10
  85. y: 80
  86. Button{
  87. id: btnSelectDate
  88. width: 150
  89. text: qsTr("Date")
  90. opacity: 0.5
  91. onClicked: launchDialog1ToToday()
  92. }
  93. Text {
  94. id: txtSelectedDate
  95. color: "#666666"
  96. anchors.left: btnSelectDate.right
  97. anchors.leftMargin: 20
  98. anchors.top: Date.top
  99. anchors.topMargin: 10
  100. font.pointSize: 18
  101. }
  102. }
  103. Row {
  104. id: rowTimePicker
  105. x: 10
  106. y: 140
  107. Button{
  108. id: btnSelectTime
  109. width: 150
  110. text: qsTr("Time")
  111. opacity: 0.5
  112. onClicked: launchDialog1ToTime()
  113. }
  114. Text {
  115. id: txtSelectedTime
  116. color: "#666666"
  117. anchors.left: btnSelectTime.right
  118. anchors.leftMargin: 20
  119. anchors.top: btnSelectTime.top
  120. anchors.topMargin: 10
  121. font.pointSize: 18
  122. }
  123. }
  124. Row {
  125. id: rowDateOperation
  126. x: 10
  127. y: 200
  128. width: parent.width
  129. Text {
  130. font.pixelSize: 25
  131. font.bold: true
  132. color: "#800078"
  133. text: qsTr("and")
  134. }
  135. }
  136. Row {
  137. id: rowDatePicker2
  138. x: 10
  139. y: 250
  140. Button {
  141. id: btnSelectDate2
  142. width: 150
  143. text: qsTr("Date")
  144. opacity: 0.5
  145. onClicked: launchDialog2ToToday()
  146. }
  147. Text {
  148. id: txtSelectedDate2
  149. color: "#666666"
  150. anchors.left: btnSelectDate2.right
  151. anchors.leftMargin: 20
  152. anchors.top: btnSelectDate2.top
  153. anchors.topMargin: 10
  154. font.pointSize: 18
  155. }
  156. }
  157. Row {
  158. id: rowTimePicker2
  159. x: 10
  160. y: 310
  161. Button{
  162. id: btnSelectTime2
  163. width: 150
  164. text: qsTr("Time")
  165. opacity: 0.5
  166. onClicked: launchDialog2ToTime()
  167. }
  168. Text {
  169. id: txtSelectedTime2
  170. color: "#666666"
  171. anchors.left: btnSelectTime2.right
  172. anchors.leftMargin: 20
  173. anchors.top: btnSelectTime2.top
  174. anchors.topMargin: 10
  175. font.pointSize: 18
  176. }
  177. }
  178. // Create a selection dialog with a title and list elements to choose from.
  179. SelectionDialog {
  180. id: sdUnit
  181. titleText: qsTr("Unit")
  182. selectedIndex: 3
  183. model: unitModel
  184. onAccepted: {
  185. calcResults()
  186. }
  187. }
  188. ListModel {
  189. id: unitModel
  190. }
  191. Row {
  192. id: rowResults
  193. x: 10
  194. y: 370
  195. Text {
  196. opacity: 0.5
  197. width: 30
  198. text: qsTr("=")
  199. font.pointSize: 18
  200. font.bold: true
  201. }
  202. Text {
  203. id: txtResults
  204. font.pointSize: 18
  205. font.bold: true
  206. color: "#a20407"
  207. width: 220
  208. }
  209. Button {
  210. id: btnSelectUnit
  211. width: 150
  212. text: sdUnit.model.get(sdUnit.selectedIndex).name
  213. opacity: 0.5
  214. onClicked: {
  215. sdUnit.open()
  216. }
  217. }
  218. }
  219. Row {
  220. x: 10
  221. y: 430
  222. Text {
  223. id: txtAltResultsEqual
  224. opacity: 0.5
  225. width: 30
  226. text: qsTr("=")
  227. font.pointSize: 18
  228. font.bold: true
  229. visible: false
  230. }
  231. Text {
  232. id: txtAltResults
  233. width: 450
  234. text: qsTr( "" )
  235. font.pointSize: 18
  236. font.bold: false
  237. color: "#a20407"
  238. }
  239. }
  240. Row {
  241. x: 10
  242. y: 490
  243. Button {
  244. id: btnClear
  245. width: 130
  246. text: qsTr( "Clear" )
  247. opacity: 0.5
  248. onClicked: {
  249. clear()
  250. }
  251. }
  252. }
  253. Image {
  254. id: imgCalcPlusMinus
  255. x: timeDiffPage.width - 320; y: 550
  256. width: 320;
  257. source: qsTr("qrc:/icon_plusminus")
  258. MouseArea {
  259. anchors.fill: parent
  260. onClicked: {
  261. appWindow.pageStack.clear()
  262. appWindow.pageStack.push(mainPage)
  263. }
  264. }
  265. }
  266. }
  267. DatePickerDialog {
  268. id: dlgDate
  269. titleText: qsTr("Select a date")
  270. minimumYear: 1900
  271. maximumYear: 2099
  272. acceptButtonText: qsTr("OK")
  273. rejectButtonText: qsTr("Cancel")
  274. onAccepted: selectDateCallbackFunction()
  275. }
  276. DatePickerDialog {
  277. id: dlgDate2
  278. titleText: qsTr("Select a date")
  279. minimumYear: 1900
  280. maximumYear: 2099
  281. acceptButtonText: qsTr("OK")
  282. rejectButtonText: qsTr("Cancel")
  283. onAccepted: selectDate2CallbackFunction()
  284. }
  285. }
  286. function createUnitMenuItems() {
  287. // console.log("createUnitMenuItems() executed.")
  288. if (unitModel.count==0) {
  289. unitModel.append({"name":qsTr("Year(s)")})
  290. unitModel.append({"name":qsTr("Month(s)")})
  291. unitModel.append({"name":qsTr("Week(s)")})
  292. unitModel.append({"name":qsTr("Day(s)")})
  293. unitModel.append({"name":qsTr("Business Day(s)")})
  294. unitModel.append({"name":qsTr("Hour(s)")})
  295. unitModel.append({"name":qsTr("Minute(s)")})
  296. unitModel.append({"name":qsTr("Second(s)")})
  297. btnSelectUnit.text = sdUnit.model.get(sdUnit.selectedIndex).name
  298. }
  299. }
  300. function launchDialog1ToToday() {
  301. if (txtSelectedDate.text=="") {
  302. var dToday = new Date()
  303. dlgDate.year = dToday.getFullYear()
  304. dlgDate.month = dToday.getMonth()+1
  305. dlgDate.day = dToday.getDate()
  306. }
  307. dlgDate.open()
  308. }
  309. function launchDialog2ToToday() {
  310. if (txtSelectedDate2.text=="") {
  311. var dToday = new Date()
  312. dlgDate2.year = dToday.getFullYear()
  313. dlgDate2.month = dToday.getMonth()+1
  314. dlgDate2.day = dToday.getDate()
  315. }
  316. dlgDate2.open()
  317. }
  318. function selectDateCallbackFunction() {
  319. txtSelectedDate.text = Qt.formatDateTime(new Date(dlgDate.year, dlgDate.month-1, dlgDate.day), "yyyy/MM/dd ddd")
  320. calcResults()
  321. }
  322. function selectDate2CallbackFunction() {
  323. txtSelectedDate2.text = Qt.formatDateTime(new Date(dlgDate2.year, dlgDate2.month-1, dlgDate2.day), "yyyy/MM/dd ddd")
  324. calcResults()
  325. }
  326. function calcResults() {
  327. var dResult = new Date()
  328. btnSelectUnit.text = sdUnit.model.get(sdUnit.selectedIndex).name
  329. if (txtSelectedDate.text == "") {
  330. dlgDate.year = dResult.getFullYear()
  331. dlgDate.month = dResult.getMonth() + 1
  332. dlgDate.day = dResult.getDate()
  333. txtSelectedDate.text = Qt.formatDateTime(dResult, "yyyy/MM/dd ddd")
  334. }
  335. if (txtSelectedDate2.text == "") {
  336. dlgDate2.year = dResult.getFullYear()
  337. dlgDate2.month = dResult.getMonth() + 1
  338. dlgDate2.day = dResult.getDate()
  339. txtSelectedDate2.text = Qt.formatDateTime(dResult, "yyyy/MM/dd ddd")
  340. }
  341. var dtDate = new Date(dlgDate.year, dlgDate.month-1, dlgDate.day, dlgTime.hour, dlgTime.minute, dlgTime.second, 0)
  342. var dtDate2 = new Date(dlgDate2.year, dlgDate2.month-1, dlgDate2.day, dlgTime2.hour, dlgTime2.minute, dlgTime2.second, 0)
  343. // console.log("dtDate:" + dtDate + " dtDate.getDay: " + dtDate.getDay() + "\ndtDate2: " + dtDate2 + " dtDate2.getDay: " + dtDate2.getDay())
  344. var fsResult = Math.abs( ( dtDate - dtDate2 ) / 1000 ) // in second
  345. var fYResult = fsResult / (60*60*24*365.25)
  346. var fYDecimals = fYResult - Math.floor(fYResult)
  347. var fMResult = fsResult / (60*60*24*30.4375)
  348. var fMDecimals = fMResult - Math.floor(fMResult)
  349. var fWResult = fsResult / (60*60*24*7)
  350. var fWDecimals = fWResult - Math.floor(fWResult)
  351. var fDResult = fsResult / (60*60*24)
  352. var fDDecimals = fDResult - Math.floor(fDResult)
  353. var fBDResult = dtDate <= dtDate2 ? CalcJSLib.calcBusinessDaysDiff(dtDate,dtDate2) : CalcJSLib.calcBusinessDaysDiff(dtDate2,dtDate)
  354. var fBDDecimals = fDResult - Math.floor(fDResult) // use the Day difference decimal part intentionally
  355. var fHResult = fsResult / (60*60)
  356. var fHDecimals = fHResult - Math.floor(fHResult)
  357. var fmResult = fsResult / 60
  358. var fmDecimals = fmResult - Math.floor(fmResult)
  359. var fResult = 0
  360. if (btnSelectUnit.text==qsTr("Year(s)")) {
  361. fResult = fYResult
  362. if (fYDecimals > 0) {
  363. var fYMResult = fYDecimals * 12
  364. var fYMDecimals = fYMResult - Math.floor(fYMResult)
  365. var fYMDResult = fYMDecimals * 30.4375
  366. var fYMDDecimals = fYMDResult - Math.floor(fYMDResult)
  367. var fYMDHResult = fYMDDecimals * 24
  368. var fYMDHDecimals = fYMDHResult - Math.floor(fYMDHResult)
  369. var fYMDHmResult = fYMDHDecimals * 60
  370. var fYMDHmDecimals = fYMDHmResult - Math.floor(fYMDHmResult)
  371. var fYMDHmsResult = fYMDHmDecimals * 60
  372. var fYMDHmsDecimals = fYMDHmsDecimals - Math.floor(fYMDHmsDecimals)
  373. txtAltResults.text = Math.floor(fYResult) + qsTr(" Yr ") + Math.floor(fYMResult) + qsTr(" Mth ") + Math.floor(fYMDResult) + qsTr(" Day ") + Math.floor(fYMDHResult) + qsTr(" Hr ") + Math.floor(fYMDHmResult) + qsTr(" Min ") + fYMDHmsResult.toFixed(0) + qsTr(" Sec")
  374. txtAltResultsEqual.visible = true
  375. }
  376. else {
  377. txtAltResults.text = ""
  378. txtAltResultsEqual.visible = false
  379. }
  380. }
  381. if (btnSelectUnit.text==qsTr("Month(s)")) {
  382. fResult = fMResult
  383. if (fMDecimals > 0) {
  384. var fMDResult = fMDecimals * 30.4375
  385. var fMDDecimals = fMDResult - Math.floor(fMDResult)
  386. var fMDHResult = fMDDecimals * 24
  387. var fMDHDecimals = fMDHResult - Math.floor(fMDHResult)
  388. var fMDHmResult = fMDHDecimals * 60
  389. var fMDHmDecimals = fMDHmResult - Math.floor(fMDHmResult)
  390. var fMDHmsResult = fMDHmDecimals * 60
  391. var fMDHmsDecimals = fMDHmsDecimals - Math.floor(fMDHmsDecimals)
  392. txtAltResults.text = Math.floor(fMResult) + qsTr(" Mth ") + Math.floor(fMDResult) + qsTr(" Day ") + Math.floor(fMDHResult) + qsTr(" Hr ") + Math.floor(fMDHmResult) + qsTr(" Min ") + fMDHmsResult.toFixed(0) + qsTr(" Sec")
  393. txtAltResultsEqual.visible = true
  394. }
  395. else {
  396. txtAltResults.text = ""
  397. txtAltResultsEqual.visible = false
  398. }
  399. }
  400. if (btnSelectUnit.text==qsTr("Week(s)")) {
  401. fResult = fWResult
  402. if (fWDecimals > 0) {
  403. var fWDResult = fWDecimals * 7
  404. var fWDDecimals = fWDResult - Math.floor(fWDResult)
  405. var fWDHResult = fWDDecimals * 24
  406. var fWDHDecimals = fWDHResult - Math.floor(fWDHResult)
  407. var fWDHmResult = fWDHDecimals * 60
  408. var fWDHmDecimals = fWDHmResult - Math.floor(fWDHmResult)
  409. var fWDHmsResult = fWDHmDecimals * 60
  410. var fWDHmsDecimals = fWDHmsDecimals - Math.floor(fWDHmsDecimals)
  411. txtAltResults.text = Math.floor(fWResult) + qsTr(" Wk ") + Math.floor(fWDResult) + qsTr(" Day ") + Math.floor(fWDHResult) + qsTr(" Hr ") + Math.floor(fWDHmResult) + qsTr(" Min ") + fWDHmsResult.toFixed(0) + qsTr(" Sec")
  412. txtAltResultsEqual.visible = true
  413. }
  414. else {
  415. txtAltResults.text = ""
  416. txtAltResultsEqual.visible = false
  417. }
  418. }
  419. if (btnSelectUnit.text==qsTr("Day(s)")) {
  420. fResult = fDResult
  421. if (fDDecimals > 0) {
  422. var fDHResult = fDDecimals * 24
  423. var fDHDecimals = fDHResult - Math.floor(fDHResult)
  424. var fDHmResult = fDHDecimals * 60
  425. var fDHmDecimals = fDHmResult - Math.floor(fDHmResult)
  426. var fDHmsResult = fDHmDecimals * 60
  427. var fDHmsDecimals = fDHmsDecimals - Math.floor(fDHmsDecimals)
  428. txtAltResults.text = Math.floor(fDResult) + qsTr(" Day ") + Math.floor(fDHResult) + qsTr(" Hr ") + Math.floor(fDHmResult) + qsTr(" Min ") + fDHmsResult.toFixed(0) + qsTr(" Sec")
  429. txtAltResultsEqual.visible = true
  430. }
  431. else {
  432. txtAltResults.text = ""
  433. txtAltResultsEqual.visible = false
  434. }
  435. }
  436. if (btnSelectUnit.text==qsTr("Business Day(s)")) {
  437. fResult = fBDResult + fDDecimals // use the Day difference decimal part intentionally
  438. if (fBDDecimals > 0) {
  439. var fBDHResult = fBDDecimals * 24
  440. var fBDHDecimals = fBDHResult - Math.floor(fBDHResult)
  441. var fBDHmResult = fBDHDecimals * 60
  442. var fBDHmDecimals = fBDHmResult - Math.floor(fBDHmResult)
  443. var fBDHmsResult = fBDHmDecimals * 60
  444. var fBDHmsDecimals = fBDHmsDecimals - Math.floor(fBDHmsDecimals)
  445. txtAltResults.text = Math.floor(fBDResult) + qsTr(" Day ") + Math.floor(fBDHResult) + qsTr(" Hr ") + Math.floor(fBDHmResult) + qsTr(" Min ") + fBDHmsResult.toFixed(0) + qsTr(" Sec")
  446. txtAltResultsEqual.visible = true
  447. }
  448. else {
  449. txtAltResults.text = ""
  450. txtAltResultsEqual.visible = false
  451. }
  452. }
  453. if (btnSelectUnit.text==qsTr("Hour(s)")) {
  454. fResult = fHResult
  455. if (fHDecimals > 0) {
  456. var fHmResult = fHDecimals * 60
  457. var fHmDecimals = fHmResult - Math.floor(fHmResult)
  458. var fHmsResult = fHmDecimals * 60
  459. var fHmsDecimals = fHmsDecimals - Math.floor(fHmsDecimals)
  460. txtAltResults.text = Math.floor(fHResult) + qsTr(" Hr ") + Math.floor(fHmResult) + qsTr(" Min ") + fHmsResult.toFixed(0) + qsTr(" Sec")
  461. txtAltResultsEqual.visible = true
  462. }
  463. else {
  464. txtAltResults.text = ""
  465. txtAltResultsEqual.visible = false
  466. }
  467. }
  468. if (btnSelectUnit.text==qsTr("Minute(s)")) {
  469. fResult = fmResult
  470. if (fmDecimals > 0) {
  471. var fmsResult = fmDecimals * 60
  472. var fmsDecimals = fmsDecimals - Math.floor(fmsDecimals)
  473. txtAltResults.text = Math.floor(fmResult) + qsTr(" Min ") + fmsResult.toFixed(0) + qsTr(" Sec")
  474. txtAltResultsEqual.visible = true
  475. }
  476. else {
  477. txtAltResults.text = ""
  478. txtAltResultsEqual.visible = false
  479. }
  480. }
  481. if (btnSelectUnit.text==qsTr("Second(s)")) {
  482. fResult = fsResult
  483. txtAltResults.text = ""
  484. txtAltResultsEqual.visible = false
  485. }
  486. txtResults.text = fResult.toFixed(4)
  487. }
  488. TimePickerDialog {
  489. id: dlgTime
  490. titleText: qsTr("Select a time")
  491. acceptButtonText: qsTr("OK")
  492. rejectButtonText: qsTr("Cancel")
  493. onAccepted: selectTimeCallbackFunction()
  494. }
  495. TimePickerDialog {
  496. id: dlgTime2
  497. titleText: qsTr("Select a time")
  498. acceptButtonText: qsTr("OK")
  499. rejectButtonText: qsTr("Cancel")
  500. onAccepted: selectTime2CallbackFunction()
  501. }
  502. function clear() {
  503. txtSelectedDate.text = ""
  504. txtSelectedTime.text = ""
  505. txtSelectedDate2.text = ""
  506. txtSelectedTime2.text = ""
  507. dlgTime.hour = 0
  508. dlgTime.minute = 0
  509. dlgTime.second = 0
  510. dlgTime2.hour = 0
  511. dlgTime2.minute = 0
  512. dlgTime2.second = 0
  513. txtResults.text = ""
  514. txtAltResults.text = ""
  515. txtAltResultsEqual.visible = false
  516. }
  517. function launchDialog1ToTime(hour, minute) {
  518. if (txtSelectedTime.text=="") {
  519. var d = new Date()
  520. dlgTime.hour = d.getHours()
  521. dlgTime.minute = d.getMinutes()
  522. dlgTime.second = d.getSeconds()
  523. }
  524. dlgTime.hourMode = DateTime.TwentyFourHours
  525. dlgTime.fields = DateTime.Hours | DateTime.Minutes | DateTime.Seconds;
  526. dlgTime.open();
  527. }
  528. function launchDialog2ToTime(hour, minute) {
  529. if (txtSelectedTime2.text=="") {
  530. var d = new Date()
  531. dlgTime2.hour = d.getHours()
  532. dlgTime2.minute = d.getMinutes()
  533. dlgTime2.second = d.getSeconds()
  534. }
  535. dlgTime2.hourMode = DateTime.TwentyFourHours
  536. dlgTime2.fields = DateTime.Hours | DateTime.Minutes | DateTime.Seconds;
  537. dlgTime2.open();
  538. }
  539. function selectTimeCallbackFunction() {
  540. txtSelectedTime.text = Qt.formatDateTime(new Date(dlgDate.year, dlgDate.month, dlgDate.day, dlgTime.hour, dlgTime.minute, dlgTime.second), "HH:mm:ss")
  541. calcResults()
  542. }
  543. function selectTime2CallbackFunction() {
  544. txtSelectedTime2.text = Qt.formatDateTime(new Date(dlgDate2.year, dlgDate2.month, dlgDate2.day, dlgTime2.hour, dlgTime2.minute, dlgTime2.second), "HH:mm:ss")
  545. calcResults()
  546. }
  547. Component.onCompleted: createUnitMenuItems()
  548. }