MainPage.qml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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: mainPage
  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: mainPage.width
  15. height: mainPage.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: mainPage.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 * 1.6 : 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: mainPage.width - x
  76. text: qsTr("Plus/Minus Time")
  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: launchDialogToToday()
  92. }
  93. Text {
  94. id: txtSelectedDate
  95. color: "#666666"
  96. anchors.left: btnSelectDate.right
  97. anchors.leftMargin: 20
  98. anchors.top: btnSelectDate.top
  99. anchors.topMargin: 10
  100. font.pointSize: 18
  101. }
  102. }
  103. Row {
  104. id: rowTimePicker
  105. x: 10
  106. y: 150
  107. Button{
  108. id: btnSelectTime
  109. width: 150
  110. text: qsTr("Time")
  111. opacity: 0.5
  112. onClicked: launchDialogToTime()
  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: 220
  128. width: parent.width
  129. ButtonRow {
  130. width: 150
  131. checkedButton: btnPlus
  132. Button {
  133. id: btnPlus
  134. text: "+"
  135. opacity: 0.8
  136. onClicked: {
  137. calcResults()
  138. }
  139. }
  140. Button {
  141. id: btnMinus
  142. text: "-"
  143. opacity: 0.8
  144. onClicked: {
  145. calcResults()
  146. }
  147. }
  148. }
  149. Text {
  150. // Separator
  151. width: 10
  152. }
  153. TextField {
  154. id: tfNumber
  155. font.pointSize: 18
  156. // inputMask: "9"
  157. maximumLength: 5
  158. width: 150
  159. inputMethodHints: Qt.ImhDigitsOnly | Qt.ImhNoPredictiveText
  160. platformSipAttributes: customSipAttributes
  161. onTextChanged: {
  162. calcResults()
  163. }
  164. Keys.onReturnPressed: {
  165. calcResults()
  166. tfNumber.closeSoftwareInputPanel()
  167. }
  168. platformStyle: TextFieldStyle { paddingRight: clearButton.width }
  169. Image {
  170. id: clearButton
  171. anchors.right: parent.right
  172. anchors.verticalCenter: parent.verticalCenter
  173. source: "image://theme/icon-m-input-clear"
  174. MouseArea {
  175. anchors.fill: parent
  176. onClicked: {
  177. inputContext.reset();
  178. tfNumber.text = "";
  179. }
  180. }
  181. }
  182. }
  183. Button {
  184. id: btnSelectUnit
  185. width: 130
  186. text: sdUnit.model.get(sdUnit.selectedIndex).name
  187. opacity: 0.5
  188. onClicked: {
  189. sdUnit.open();
  190. }
  191. }
  192. }
  193. // Create a selection dialog with a title and list elements to choose from.
  194. SelectionDialog {
  195. id: sdUnit
  196. titleText: qsTr("Unit")
  197. selectedIndex: 3
  198. model: unitModel
  199. onAccepted: {
  200. calcResults()
  201. }
  202. }
  203. ListModel {
  204. id: unitModel
  205. }
  206. Row {
  207. id: rowCalcButtonAndResults
  208. x: 80
  209. y: 290
  210. Text {
  211. opacity: 0.5
  212. width: 30
  213. text: qsTr("=")
  214. font.pointSize: 18
  215. font.bold: true
  216. }
  217. Text {
  218. id: txtResults
  219. font.pointSize: 18
  220. font.bold: true
  221. color: "#a20407"
  222. width: 200
  223. }
  224. }
  225. Row {
  226. x: 10
  227. y: 360
  228. Button {
  229. id: btnClear
  230. width: 130
  231. text: qsTr( "Clear" )
  232. opacity: 0.5
  233. onClicked: {
  234. clear()
  235. }
  236. }
  237. }
  238. Row {
  239. x: mainPage.width - 320
  240. y: 430
  241. Image {
  242. id: imgCalcTimeDiff
  243. x: 0; y: 0
  244. width: 320;
  245. source: qsTr("qrc:/icon_timediff")
  246. MouseArea {
  247. anchors.fill: parent
  248. onClicked: {
  249. appWindow.pageStack.clear()
  250. appWindow.pageStack.push(timeDiffPage)
  251. }
  252. }
  253. }
  254. }
  255. }
  256. DatePickerDialog {
  257. id: dlgDate
  258. titleText: qsTr("Select a date")
  259. minimumYear: 1900
  260. maximumYear: 2099
  261. acceptButtonText: qsTr("OK")
  262. rejectButtonText: qsTr("Cancel")
  263. onAccepted: selectDateCallbackFunction()
  264. }
  265. }
  266. function createUnitMenuItems() {
  267. // console.log("createUnitMenuItems() executed.")
  268. if (unitModel.count==0) {
  269. unitModel.append({"name":qsTr("Year(s)")})
  270. unitModel.append({"name":qsTr("Month(s)")})
  271. unitModel.append({"name":qsTr("Week(s)")})
  272. unitModel.append({"name":qsTr("Day(s)")})
  273. unitModel.append({"name":qsTr("Business Day(s)")})
  274. unitModel.append({"name":qsTr("Hour(s)")})
  275. unitModel.append({"name":qsTr("Minute(s)")})
  276. unitModel.append({"name":qsTr("Second(s)")})
  277. btnSelectUnit.text = sdUnit.model.get(sdUnit.selectedIndex).name
  278. }
  279. }
  280. function launchDialogToToday() {
  281. if (txtSelectedDate.text=="") {
  282. var dToday = new Date()
  283. dlgDate.year = dToday.getFullYear()
  284. dlgDate.month = dToday.getMonth()+1
  285. dlgDate.day = dToday.getDate()
  286. }
  287. dlgDate.open()
  288. }
  289. function selectDateCallbackFunction() {
  290. txtSelectedDate.text = Qt.formatDateTime(new Date(dlgDate.year, dlgDate.month-1, dlgDate.day), "yyyy/MM/dd ddd")
  291. calcResults()
  292. // txtResults.text = ""
  293. }
  294. function clear() {
  295. tfNumber.text = ""
  296. txtSelectedDate.text = ""
  297. txtSelectedTime.text = ""
  298. txtResults.text = ""
  299. dlgTime.hour = 0
  300. dlgTime.minute = 0
  301. dlgTime.second = 0
  302. }
  303. function calcResults() {
  304. var dSelectedDate = new Date()
  305. var dResult = new Date()
  306. btnSelectUnit.text = sdUnit.model.get(sdUnit.selectedIndex).name
  307. if (txtSelectedDate.text == "") {
  308. dlgDate.year = dResult.getFullYear()
  309. dlgDate.month = dResult.getMonth() + 1
  310. dlgDate.day = dResult.getDate()
  311. txtSelectedDate.text = Qt.formatDateTime(dResult, "yyyy/MM/dd ddd")
  312. }
  313. if (txtSelectedTime.text != "") {
  314. dSelectedDate = new Date(dlgDate.year, dlgDate.month-1, dlgDate.day, dlgTime.hour, dlgTime.minute, dlgTime.second )
  315. dResult = dSelectedDate
  316. }
  317. else {
  318. dSelectedDate = new Date(dlgDate.year, dlgDate.month-1, dlgDate.day)
  319. dResult = dSelectedDate
  320. }
  321. var iPlusOrMinus = 1
  322. if (btnPlus.checked)
  323. iPlusOrMinus = 1
  324. else if (btnMinus.checked)
  325. iPlusOrMinus = -1
  326. var fDecimals = parseFloat(tfNumber.text) - Math.floor(parseFloat(tfNumber.text))
  327. if (btnSelectUnit.text==qsTr("Year(s)")) {
  328. if (iPlusOrMinus==1 || (dlgDate.year + (iPlusOrMinus * parseFloat(tfNumber.text)))>0 ) {
  329. if (fDecimals > 0)
  330. dResult = new Date(dlgDate.year + ( iPlusOrMinus * parseFloat(tfNumber.text)), dlgDate.month-1, dlgDate.day, dlgTime.hour, dlgTime.minute, dlgTime.second + (fDecimals*365.25*24*60*60) )
  331. else
  332. dResult = new Date(dlgDate.year + ( iPlusOrMinus * parseFloat(tfNumber.text)), dlgDate.month-1, dlgDate.day, dlgTime.hour, dlgTime.minute, dlgTime.second)
  333. }
  334. else { // handle B.C. years --- need to minus extra 1 year because there is no Year 0 by definition
  335. if (fDecimals > 0)
  336. dResult = new Date(dlgDate.year + ( (iPlusOrMinus * (parseFloat(tfNumber.text) + 1) )), dlgDate.month-1, dlgDate.day, dlgTime.hour, dlgTime.minute, dlgTime.second - (fDecimals*365.25*24*60*60) )
  337. else
  338. dResult = new Date(dlgDate.year + ( (iPlusOrMinus * (parseFloat(tfNumber.text) + 1) )), dlgDate.month-1, dlgDate.day, dlgTime.hour, dlgTime.minute, dlgTime.second)
  339. }
  340. // console.log("iPlusOrMinus = " + iPlusOrMinus + ", tfNumber = " + tfNumber.text + ", dResult = " + dResult)
  341. }
  342. if (btnSelectUnit.text==qsTr("Month(s)"))
  343. if (fDecimals > 0)
  344. dResult = new Date(dlgDate.year, dlgDate.month - 1 + ( iPlusOrMinus * parseFloat(tfNumber.text)), dlgDate.day, dlgTime.hour, dlgTime.minute, dlgTime.second + (fDecimals*30.436875*24*60*60) )
  345. else
  346. dResult = new Date(dlgDate.year, dlgDate.month - 1 + ( iPlusOrMinus * parseFloat(tfNumber.text)), dlgDate.day, dlgTime.hour, dlgTime.minute, dlgTime.second)
  347. if (btnSelectUnit.text==qsTr("Week(s)"))
  348. if (fDecimals > 0)
  349. dResult = new Date(dlgDate.year, dlgDate.month - 1, dlgDate.day + ( iPlusOrMinus * parseFloat(tfNumber.text) * 7), dlgTime.hour, dlgTime.minute, dlgTime.second + (fDecimals*7*24*60*60) )
  350. else
  351. dResult = new Date(dlgDate.year, dlgDate.month - 1, dlgDate.day + ( iPlusOrMinus * parseFloat(tfNumber.text) * 7), dlgTime.hour, dlgTime.minute, dlgTime.second)
  352. if (btnSelectUnit.text==qsTr("Day(s)"))
  353. if (fDecimals > 0)
  354. dResult = new Date(dlgDate.year, dlgDate.month - 1, dlgDate.day + ( iPlusOrMinus * parseFloat(tfNumber.text)), dlgTime.hour, dlgTime.minute, dlgTime.second + (fDecimals*24*60*60) )
  355. else
  356. dResult = new Date(dlgDate.year, dlgDate.month - 1, dlgDate.day + ( iPlusOrMinus * parseFloat(tfNumber.text)), dlgTime.hour, dlgTime.minute, dlgTime.second)
  357. if (btnSelectUnit.text==qsTr("Business Day(s)")) {
  358. if (fDecimals > 0) {
  359. dResult = CalcJSLib.addBusinessDays(dSelectedDate, (iPlusOrMinus * parseFloat(tfNumber.text)) )
  360. dResult = new Date(dResult.getFullYear(), dResult.getMonth(), dResult.getDate(), dlgTime.hour, dlgTime.minute, dlgTime.second + (fDecimals*24*60*60))
  361. }
  362. else
  363. dResult = CalcJSLib.addBusinessDays(dSelectedDate, (iPlusOrMinus * parseFloat(tfNumber.text)) )
  364. }
  365. if (btnSelectUnit.text==qsTr("Hour(s)"))
  366. if (fDecimals > 0)
  367. dResult = new Date(dlgDate.year, dlgDate.month - 1, dlgDate.day, dlgTime.hour + ( iPlusOrMinus * parseFloat(tfNumber.text)), dlgTime.minute, dlgTime.second + (fDecimals*60*60) )
  368. else
  369. dResult = new Date(dlgDate.year, dlgDate.month - 1, dlgDate.day, dlgTime.hour + ( iPlusOrMinus * parseFloat(tfNumber.text)), dlgTime.minute, dlgTime.second )
  370. if (btnSelectUnit.text==qsTr("Minute(s)"))
  371. if (fDecimals > 0)
  372. dResult = new Date(dlgDate.year, dlgDate.month - 1, dlgDate.day, dlgTime.hour, dlgTime.minute + ( iPlusOrMinus * parseFloat(tfNumber.text)), dlgTime.second )
  373. else
  374. dResult = new Date(dlgDate.year, dlgDate.month - 1, dlgDate.day, dlgTime.hour, dlgTime.minute + ( iPlusOrMinus * parseFloat(tfNumber.text)), dlgTime.second + (fDecimals*60))
  375. if (btnSelectUnit.text==qsTr("Second(s)"))
  376. if (fDecimals > 0)
  377. dResult = new Date(dlgDate.year, dlgDate.month - 1, dlgDate.day, dlgTime.hour, dlgTime.minute, dlgTime.second + ( iPlusOrMinus * parseFloat(tfNumber.text)) )
  378. else
  379. dResult = new Date(dlgDate.year, dlgDate.month - 1, dlgDate.day, dlgTime.hour, dlgTime.minute, dlgTime.second + ( iPlusOrMinus * parseFloat(tfNumber.text)) + fDecimals )
  380. var sResults = ""
  381. if (txtSelectedTime.text != "")
  382. sResults = Qt.formatDateTime(dResult, "yyyy/MM/dd HH:mm:ss ddd").toString()
  383. else
  384. sResults = Qt.formatDateTime(dResult, "yyyy/MM/dd ddd").toString()
  385. if ( ( ( btnSelectUnit.text==qsTr("Year(s)") && (parseFloat(tfNumber.text) >= 1801) ) || ( btnSelectUnit.text==qsTr("Month(s)") && (parseFloat(tfNumber.text) >= 21612) ) )
  386. && iPlusOrMinus==-1 && dResult.getFullYear()>="1900" && dResult.getFullYear()<="1999" )
  387. sResults = "00" + sResults.substr(2,sResults.length-2) // to cater the case where QML treats YY as 19YY
  388. if (sResults.substr(0,5) == "-4713") {
  389. dlgBefore4713BC.open()
  390. tfNumber.text = ""
  391. }
  392. if (tfNumber.text!="" && (!CalcJSLib.isValidDate(dResult) || sResults == "" ) ) {
  393. dlgInvalidValue.open()
  394. tfNumber.text = ""
  395. }
  396. if (sResults.substr(0,1)!="-")
  397. txtResults.text = sResults
  398. else
  399. txtResults.text = sResults.substr(1, sResults.length-1).replace(" ", qsTr(" B.C. "))
  400. }
  401. QueryDialog {
  402. id: dlgInvalidValue
  403. titleText: qsTr("Error")
  404. acceptButtonText: qsTr("OK")
  405. message: qsTr("Please specify a proper value.")
  406. }
  407. QueryDialog {
  408. id: dlgBefore4713BC
  409. titleText: qsTr("Reminder")
  410. acceptButtonText: qsTr("OK")
  411. message: qsTr("The earliest date that Time Calculator supports is 4713-01-02 B.C., i.e. the next day of the Julian Day.")
  412. }
  413. TimePickerDialog {
  414. id: dlgTime
  415. titleText: qsTr("Select a time")
  416. acceptButtonText: qsTr("OK")
  417. rejectButtonText: qsTr("Cancel")
  418. onAccepted: selectTimeCallbackFunction()
  419. }
  420. function launchDialogToTime(hour, minute) {
  421. if (txtSelectedTime.text=="") {
  422. var d = new Date()
  423. dlgTime.hour = d.getHours()
  424. dlgTime.minute = d.getMinutes()
  425. dlgTime.second = d.getSeconds()
  426. }
  427. dlgTime.hourMode = DateTime.TwentyFourHours
  428. dlgTime.fields = DateTime.Hours | DateTime.Minutes | DateTime.Seconds;
  429. dlgTime.open();
  430. }
  431. function selectTimeCallbackFunction() {
  432. txtSelectedTime.text = Qt.formatDateTime(new Date(dlgDate.year, dlgDate.month, dlgDate.day, dlgTime.hour, dlgTime.minute, dlgTime.second), "HH:mm:ss")
  433. calcResults()
  434. // txtResults.text = ""
  435. }
  436. Component.onCompleted: createUnitMenuItems()
  437. }