PwmFan.qml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright (C) 2015 Malte Veerman <maldela@halloarsch.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. import QtQuick 2.4
  20. import QtQuick.Controls 1.4
  21. import QtQuick.Layouts 1.1
  22. import "../scripts/arrayfunctions.js" as ArrayFunctions
  23. import "../scripts/math.js" as MoreMath
  24. import "../scripts/units.js" as Units
  25. import "../scripts/coordinates.js" as Coordinates
  26. Rectangle {
  27. property QtObject fan
  28. property QtObject loader
  29. property QtObject systemdCom
  30. property real minTemp: 20.0
  31. property real maxTemp: 100.0
  32. property int margin: 5
  33. property int minimizeDuration: 400
  34. property int unit: 0
  35. property bool minimizable: true
  36. id: root
  37. color: "transparent"
  38. border.color: "black"
  39. border.width: 2
  40. radius: 10
  41. clip: false
  42. state: fan.active ? "" : minimizable ? "minimized" : ""
  43. function update() {
  44. if (fan) {
  45. hasTempCheckBox.checked = Qt.binding(function() { return fan.hasTemp; })
  46. fanOffCheckBox.checked = Qt.binding(function() { return (fan.minPwm == 0); })
  47. minStartInput.text = Qt.binding(function() { return fan.minStart; })
  48. if (fan.hasTemp && loader) {
  49. tempBox.currentIndex = loader.allTemps.indexOf(fan.temp);
  50. }
  51. }
  52. canvas.requestPaint();
  53. }
  54. onFanChanged: update();
  55. onLoaderChanged: update()
  56. onUnitChanged: if (fan) canvas.requestPaint()
  57. onMinTempChanged: if (fan) canvas.requestPaint()
  58. onMaxTempChanged: if (fan) canvas.requestPaint()
  59. Connections {
  60. target: loader
  61. onConfigUrlChanged: update()
  62. }
  63. states: [
  64. State {
  65. name: "minimized"
  66. PropertyChanges {
  67. target: root
  68. height: header.height + 2*margin
  69. }
  70. },
  71. State {
  72. name: "maximized"
  73. // PropertyChanges {
  74. // target: root
  75. // height:
  76. // }
  77. }
  78. ]
  79. transitions: Transition {
  80. NumberAnimation {
  81. target: root
  82. property: "height"
  83. easing.amplitude: 1.5
  84. easing.type: Easing.InOutQuad
  85. duration: minimizeDuration
  86. }
  87. }
  88. SystemPalette {
  89. id: palette
  90. }
  91. RowLayout {
  92. id: header
  93. anchors {
  94. left: parent.left
  95. leftMargin: margin
  96. right: parent.right
  97. rightMargin: margin
  98. top: parent.top
  99. topMargin: margin
  100. }
  101. z: -1
  102. clip: true
  103. spacing: margin
  104. TextEdit {
  105. id: nameField
  106. Layout.alignment: Qt.AlignTop
  107. text: fan.name
  108. onTextChanged: fan.name = text;
  109. horizontalAlignment: TextEdit.AlignLeft
  110. wrapMode: TextEdit.Wrap
  111. font.bold: true
  112. font.pointSize: 14
  113. selectByMouse: true
  114. Layout.fillWidth: true
  115. MouseArea {
  116. anchors.fill: parent
  117. cursorShape: Qt.IBeamCursor
  118. acceptedButtons: Qt.NoButton
  119. }
  120. }
  121. Rectangle {
  122. id: collapseButton
  123. height: 16
  124. width: 16
  125. Layout.alignment: Qt.AlignTop
  126. color: collapseMouseArea.containsMouse ? "red" : "transparent"
  127. radius: width / 2
  128. visible: minimizable
  129. enabled: minimizable
  130. Label {
  131. anchors.fill: parent
  132. text: root.state == "minimized" ? "-" : "X"
  133. color: collapseMouseArea.containsMouse ? "black" : "red"
  134. horizontalAlignment: Text.AlignHCenter
  135. verticalAlignment: Text.AlignVCenter
  136. }
  137. MouseArea {
  138. id: collapseMouseArea
  139. anchors.fill: parent
  140. hoverEnabled: true
  141. acceptedButtons: Qt.LeftButton
  142. onClicked: fan.active = fan.active ? false : true
  143. }
  144. }
  145. }
  146. Canvas {
  147. property int fontSize: MoreMath.bound(9, height / 20 + 1, 18)
  148. property int leftPadding: fontSize * 4
  149. property int rightPadding: fontSize * 2
  150. property int topPadding: fontSize
  151. property int bottomPadding: fontSize * 2
  152. property int plotWidth: width - leftPadding - rightPadding
  153. property int plotHeight: height - topPadding - bottomPadding
  154. id: canvas
  155. renderTarget: Canvas.FramebufferObject
  156. anchors {
  157. left: parent.left
  158. right: parent.right
  159. top: header.bottom
  160. bottom: settingsArea.top
  161. }
  162. opacity: state == "minimized" ? 0 : 1
  163. Behavior on opacity {
  164. NumberAnimation { duration: minimizeDuration / 2 }
  165. }
  166. Rectangle {
  167. property real unscaledTemp: fan.temp ? fan.temp.value : minTemp
  168. property real unscaledPwm: fan.pwm
  169. id: currentPwm
  170. x: parent.scaleX(unscaledTemp) - width/2
  171. y: parent.scaleY(unscaledPwm) - height/2
  172. width: canvas.fontSize
  173. height: width
  174. radius: width / 2
  175. color: "black"
  176. visible: parent.contains(Coordinates.centerOf(this)) && fan.hasTemp
  177. Behavior on unscaledTemp {
  178. SpringAnimation {
  179. epsilon: 0.1
  180. spring: 1.0
  181. damping: 0.4
  182. }
  183. }
  184. Behavior on unscaledPwm {
  185. SpringAnimation {
  186. epsilon: 0.1
  187. spring: 1.0
  188. damping: 0.4
  189. }
  190. }
  191. }
  192. PwmPoint {
  193. id: stopPoint
  194. color: "blue"
  195. size: canvas.fontSize
  196. unit: root.unit
  197. drag.maximumX: Math.min(canvas.scaleX(canvas.scaleTemp(maxPoint.x)-1), maxPoint.x-1)
  198. drag.minimumY: Math.max(canvas.scaleY(canvas.scalePwm(maxPoint.y)-1), maxPoint.y+1)
  199. x: canvas.scaleX(MoreMath.bound(minTemp, fan.minTemp, maxTemp)) - width/2
  200. y: canvas.scaleY(fan.minStop) - height/2
  201. visible: parent.contains(Coordinates.centerOf(this)) && parent.height > 0
  202. drag.onActiveChanged: {
  203. if (!drag.active) {
  204. fan.minStop = canvas.scalePwm(centerY);
  205. fan.minTemp = canvas.scaleTemp(centerX);
  206. if (!fanOffCheckBox.checked) fan.minPwm = fan.minStop;
  207. }
  208. }
  209. }
  210. PwmPoint {
  211. id: maxPoint
  212. color: "red"
  213. size: canvas.fontSize
  214. unit: root.unit
  215. drag.minimumX: stopPoint.x
  216. drag.maximumY: stopPoint.y
  217. x: canvas.scaleX(MoreMath.bound(minTemp, fan.maxTemp, maxTemp)) - width/2
  218. y: canvas.scaleY(fan.maxPwm) - height/2
  219. visible: parent.contains(Coordinates.centerOf(this)) && parent.height > 0
  220. drag.onActiveChanged: {
  221. if (!drag.active) {
  222. fan.maxPwm = canvas.scalePwm(centerY);
  223. fan.maxTemp = canvas.scaleTemp(centerX);
  224. }
  225. }
  226. }
  227. function scaleX(temp) {
  228. var scaledX = (temp - minTemp) * plotWidth / (maxTemp - minTemp);
  229. return leftPadding + scaledX;
  230. }
  231. function scaleY(pwm) {
  232. var scaledY = pwm * plotHeight / 255;
  233. return height - bottomPadding - scaledY;
  234. }
  235. function scaleTemp(x) {
  236. var scaledTemp = (x - leftPadding) / plotWidth * (maxTemp - minTemp);
  237. return minTemp + scaledTemp;
  238. }
  239. function scalePwm(y) {
  240. var scaledPwm = (y - topPadding) / plotHeight * 255;
  241. return 255 - scaledPwm;
  242. }
  243. onPaint: {
  244. var c = canvas.getContext("2d");
  245. c.font = canvas.fontSize + "px sans-serif";
  246. c.clearRect(0, 0, width, height);
  247. c.fillStyle = palette.base;
  248. c.fillRect(leftPadding, topPadding, plotWidth, plotHeight);
  249. var fillGradient = c.createLinearGradient(0, 0, width, 0);
  250. fillGradient.addColorStop(0, "rgb(0, 0, 255)");
  251. fillGradient.addColorStop(1, "rgb(255, 0, 0)");
  252. var strokeGradient = c.createLinearGradient(0, 0, width, 0);
  253. strokeGradient.addColorStop(0, "rgb(0, 0, 255)");
  254. strokeGradient.addColorStop(1, "rgb(255, 0, 0)");
  255. c.fillStyle = fillGradient;
  256. c.strokeStyle = strokeGradient;
  257. c.lineWidth = 2;
  258. c.lineJoin = "round";
  259. c.beginPath();
  260. if (fanOffCheckBox.checked) {
  261. c.moveTo(scaleX(minTemp), scaleY(0));
  262. c.lineTo(stopPoint.centerX, scaleY(0));
  263. } else {
  264. c.moveTo(scaleX(minTemp), stopPoint.centerY);
  265. }
  266. c.lineTo(stopPoint.centerX, stopPoint.centerY);
  267. c.lineTo(maxPoint.centerX, maxPoint.centerY);
  268. c.lineTo(scaleX(maxTemp), maxPoint.centerY);
  269. c.stroke();
  270. c.lineTo(scaleX(maxTemp), height - bottomPadding);
  271. c.lineTo(leftPadding, height - bottomPadding);
  272. c.fill();
  273. fillGradient = c.createLinearGradient(0, 0, 0, height);
  274. fillGradient.addColorStop(0, "rgba(127, 127, 127, 0.6)");
  275. fillGradient.addColorStop(1, "rgba(127, 127, 127, 0.9)");
  276. c.fillStyle = fillGradient;
  277. c.fill();
  278. c.closePath();
  279. c.textAlign = "right";
  280. c.textBaseline = "middle";
  281. c.strokeStyle = palette.dark;
  282. c.fillStyle = palette.dark;
  283. c.lineWidth = 1;
  284. c.strokeRect(leftPadding-0.5, topPadding-0.5, plotWidth+0.5, plotHeight+1.5);
  285. for (var i=0; i<=100; i+=20) {
  286. var y = scaleY(i*2.55);
  287. c.fillText(i + '%', leftPadding - 2, y);
  288. if (i != 0 && i != 100) {
  289. for (var j=leftPadding; j<=width-rightPadding; j+=15) {
  290. c.moveTo(j, y);
  291. c.lineTo(Math.min(j+5, width-rightPadding), y);
  292. }
  293. c.stroke();
  294. }
  295. }
  296. c.textAlign = "center";
  297. c.textBaseline = "top";
  298. var convertedMinTemp = Units.fromCelsius(minTemp, unit);
  299. var convertedMaxTemp = Units.fromCelsius(maxTemp, unit);
  300. var suffix = (unit == 0) ? "°C" : (unit == 1) ? "K" : "°F"
  301. var lastTemp;
  302. for (var i=convertedMinTemp; i<convertedMaxTemp; i+= 10) {
  303. lastTemp = i;
  304. var x = scaleX(Units.toCelsius(i, unit));
  305. c.fillText(i + suffix, x, topPadding+plotHeight+fontSize/2);
  306. if (i != convertedMinTemp) {
  307. for (var j=scaleY(255); j<=scaleY(0); j+=20) {
  308. c.moveTo(x, j);
  309. c.lineTo(x, Math.min(j+5, width-rightPadding));
  310. }
  311. c.stroke();
  312. }
  313. }
  314. if ((convertedMaxTemp - lastTemp) > 5)
  315. c.fillText(convertedMaxTemp + suffix, scaleX(maxTemp), topPadding+plotHeight+fontSize/2);
  316. }
  317. }
  318. ColumnLayout {
  319. property int padding: 10
  320. id: settingsArea
  321. anchors {
  322. left: parent.left
  323. leftMargin: padding
  324. right: parent.right
  325. rightMargin: padding
  326. bottom: parent.bottom
  327. bottomMargin: padding
  328. }
  329. visible: root.height >= header.height + height + 2*margin
  330. opacity: canvas.opacity
  331. clip: true
  332. spacing: 2
  333. RowLayout {
  334. CheckBox {
  335. id: hasTempCheckBox
  336. text: i18n("Controlled by:")
  337. checked: fan.hasTemp
  338. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  339. onCheckedChanged: fan.hasTemp = checked
  340. }
  341. RowLayout {
  342. ComboBox {
  343. id: tempBox
  344. Layout.fillWidth: true
  345. model: ArrayFunctions.namesWithPaths(loader.allTemps)
  346. enabled: hasTempCheckBox.checked
  347. onCurrentIndexChanged: {
  348. if (hasTempCheckBox.checked)
  349. fan.temp = loader.allTemps[currentIndex];
  350. }
  351. }
  352. }
  353. }
  354. CheckBox {
  355. id: fanOffCheckBox
  356. text: i18n("Turn Fan off if temp < MINTEMP")
  357. enabled: hasTempCheckBox.checked
  358. checked: fan.minPwm == 0
  359. onCheckedChanged: {
  360. fan.minPwm = checked ? 0 : fan.minStop;
  361. canvas.requestPaint();
  362. }
  363. }
  364. RowLayout {
  365. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  366. Label {
  367. text: i18n("Pwm value for fan to start:")
  368. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  369. renderType: Text.NativeRendering
  370. }
  371. OptionInput {
  372. id: minStartInput
  373. Layout.fillWidth: true
  374. text: fan.minStart
  375. onTextChanged: fan.minStart = parseInt(text)
  376. }
  377. }
  378. RowLayout {
  379. visible: systemdCom
  380. Label {
  381. text: i18n("Test start and stop values")
  382. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  383. renderType: Text.NativeRendering
  384. }
  385. Item {
  386. Layout.fillWidth: true
  387. }
  388. Button {
  389. property bool reactivateAfterTesting
  390. id: testButton
  391. text: fan.testing? i18n("Abort") : i18n("Test")
  392. anchors.right: parent.right
  393. onClicked: {
  394. if (fan.testing) {
  395. fan.abortTest();
  396. systemdCom.serviceActive = true;
  397. } else {
  398. reactivateAfterTesting = systemdCom.serviceActive;
  399. systemdCom.serviceActive = false;
  400. minStartInput.text = Qt.binding(function() { return fan.minStart });
  401. fan.test();
  402. }
  403. }
  404. Connections {
  405. target: fan
  406. onTestingChanged: if (!fan.testing) systemdCom.serviceActive = testButton.reactivateAfterTesting
  407. }
  408. }
  409. }
  410. }
  411. }