PwmFan.qml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. implicitWidth: 800
  38. implicitHeight: 600
  39. color: "transparent"
  40. border.color: "black"
  41. border.width: 2
  42. radius: 10
  43. clip: false
  44. state: fan.active ? "" : minimizable ? "minimized" : ""
  45. function update() {
  46. if (fan) {
  47. hasTempCheckBox.checked = Qt.binding(function() { return fan.hasTemp; })
  48. fanOffCheckBox.checked = Qt.binding(function() { return (fan.minPwm == 0); })
  49. minStartInput.text = Qt.binding(function() { return fan.minStart; })
  50. if (fan.hasTemp && loader) {
  51. tempBox.currentIndex = loader.allTemps.indexOf(fan.temp);
  52. }
  53. }
  54. canvas.requestPaint();
  55. }
  56. onFanChanged: update()
  57. onLoaderChanged: update()
  58. onUnitChanged: if (fan) canvas.requestPaint()
  59. onMinTempChanged: if (fan) canvas.requestPaint()
  60. onMaxTempChanged: if (fan) canvas.requestPaint()
  61. Connections {
  62. target: loader
  63. onConfigUrlChanged: update()
  64. }
  65. states: [
  66. State {
  67. name: "minimized"
  68. PropertyChanges {
  69. target: root
  70. height: header.height + 2*margin
  71. }
  72. },
  73. State {
  74. name: "maximized"
  75. // PropertyChanges {
  76. // target: root
  77. // height:
  78. // }
  79. }
  80. ]
  81. transitions: Transition {
  82. NumberAnimation {
  83. target: root
  84. property: "height"
  85. easing.amplitude: 1.5
  86. easing.type: Easing.InOutQuad
  87. duration: minimizeDuration
  88. }
  89. }
  90. SystemPalette {
  91. id: palette
  92. }
  93. RowLayout {
  94. id: header
  95. anchors {
  96. left: parent.left
  97. leftMargin: margin
  98. right: parent.right
  99. rightMargin: margin
  100. top: parent.top
  101. topMargin: margin
  102. }
  103. z: -1
  104. clip: true
  105. spacing: margin
  106. TextEdit {
  107. id: nameField
  108. Layout.alignment: Qt.AlignTop
  109. text: fan.name
  110. onTextChanged: fan.name = text;
  111. horizontalAlignment: TextEdit.AlignLeft
  112. wrapMode: TextEdit.Wrap
  113. font.bold: true
  114. font.pointSize: 14
  115. selectByMouse: true
  116. Layout.fillWidth: true
  117. MouseArea {
  118. anchors.fill: parent
  119. cursorShape: Qt.IBeamCursor
  120. acceptedButtons: Qt.NoButton
  121. }
  122. }
  123. Rectangle {
  124. id: collapseButton
  125. height: 16
  126. width: 16
  127. Layout.alignment: Qt.AlignTop
  128. color: collapseMouseArea.containsMouse ? "red" : "transparent"
  129. radius: width / 2
  130. visible: minimizable
  131. enabled: minimizable
  132. Label {
  133. anchors.fill: parent
  134. text: root.state == "minimized" ? "-" : "X"
  135. color: collapseMouseArea.containsMouse ? "black" : "red"
  136. horizontalAlignment: Text.AlignHCenter
  137. verticalAlignment: Text.AlignVCenter
  138. }
  139. MouseArea {
  140. id: collapseMouseArea
  141. anchors.fill: parent
  142. hoverEnabled: true
  143. acceptedButtons: Qt.LeftButton
  144. onClicked: fan.active = fan.active ? false : true
  145. }
  146. }
  147. }
  148. Canvas {
  149. property int fontSize: Math.max(9, Math.min(height / 25, 20))
  150. property int leftPadding: fontSize * 4
  151. property int rightPadding: fontSize * 2
  152. property int topPadding: fontSize
  153. property int bottomPadding: fontSize * 2
  154. property int plotWidth: width - leftPadding - rightPadding
  155. property int plotHeight: height - topPadding - bottomPadding
  156. property alias minTemp: root.minTemp
  157. property alias maxTemp: root.maxTemp
  158. id: canvas
  159. renderTarget: Canvas.FramebufferObject
  160. anchors {
  161. left: parent.left
  162. right: parent.right
  163. top: header.bottom
  164. bottom: settingsArea.top
  165. }
  166. opacity: state == "minimized" ? 0 : 1
  167. Behavior on opacity {
  168. NumberAnimation { duration: minimizeDuration / 2 }
  169. }
  170. Rectangle {
  171. property real unscaledTemp: fan.temp ? fan.temp.value : minTemp
  172. property real unscaledPwm: fan.pwm
  173. id: currentPwm
  174. x: parent.scaleX(unscaledTemp) - width/2
  175. y: parent.scaleY(unscaledPwm) - height/2
  176. width: canvas.fontSize
  177. height: width
  178. radius: width / 2
  179. color: "black"
  180. visible: parent.contains(Coordinates.centerOf(this)) && fan.hasTemp
  181. Behavior on unscaledTemp {
  182. SpringAnimation {
  183. // duration: 2000
  184. epsilon: 0.1
  185. spring: 1.0
  186. damping: 0.5
  187. }
  188. }
  189. Behavior on unscaledPwm {
  190. SpringAnimation {
  191. // duration: 2000
  192. epsilon: 0.1
  193. spring: 1.0
  194. damping: 0.5
  195. }
  196. }
  197. }
  198. PwmPoint {
  199. id: stopPoint
  200. color: "blue"
  201. size: canvas.fontSize
  202. drag.maximumX: Math.min(canvas.scaleX(canvas.scaleTemp(maxPoint.x)-1), maxPoint.x-1)
  203. drag.minimumY: Math.max(canvas.scaleY(canvas.scalePwm(maxPoint.y)-1), maxPoint.y+1)
  204. x: canvas.scaleX(MoreMath.bound(minTemp, fan.minTemp, maxTemp)) - width/2
  205. y: canvas.scaleY(fan.minStop) - height/2
  206. visible: parent.contains(Coordinates.centerOf(this)) && parent.height > 0
  207. drag.onActiveChanged: {
  208. if (!drag.active) {
  209. fan.minStop = canvas.scalePwm(centerY);
  210. fan.minTemp = canvas.scaleTemp(centerX);
  211. if (!fanOffCheckBox.checked) fan.minPwm = fan.minStop;
  212. }
  213. }
  214. }
  215. PwmPoint {
  216. id: maxPoint
  217. color: "red"
  218. size: canvas.fontSize
  219. drag.minimumX: stopPoint.x
  220. drag.maximumY: stopPoint.y
  221. x: canvas.scaleX(MoreMath.bound(minTemp, fan.maxTemp, maxTemp)) - width/2
  222. y: canvas.scaleY(fan.maxPwm) - height/2
  223. visible: parent.contains(Coordinates.centerOf(this)) && parent.height > 0
  224. drag.onActiveChanged: {
  225. if (!drag.active) {
  226. fan.maxPwm = canvas.scalePwm(centerY);
  227. fan.maxTemp = canvas.scaleTemp(centerX);
  228. }
  229. }
  230. }
  231. function scaleX(temp) {
  232. var scaledX = (temp - minTemp) * plotWidth / (maxTemp - minTemp);
  233. return leftPadding + scaledX;
  234. }
  235. function scaleY(pwm) {
  236. var scaledY = pwm * plotHeight / 255;
  237. return height - bottomPadding - scaledY;
  238. }
  239. function scaleTemp(x) {
  240. var scaledTemp = (x - leftPadding) / plotWidth * (maxTemp - minTemp);
  241. return minTemp + scaledTemp;
  242. }
  243. function scalePwm(y) {
  244. var scaledPwm = (y - topPadding) / plotHeight * 255;
  245. return 255 - scaledPwm;
  246. }
  247. onPaint: {
  248. var c = canvas.getContext("2d");
  249. c.font = canvas.fontSize + "px sans-serif";
  250. c.clearRect(0, 0, width, height);
  251. c.fillStyle = palette.base;
  252. c.fillRect(leftPadding, topPadding, plotWidth, plotHeight);
  253. var fillGradient = c.createLinearGradient(0, 0, width, 0);
  254. fillGradient.addColorStop(0, "rgb(0, 0, 255)");
  255. fillGradient.addColorStop(1, "rgb(255, 0, 0)");
  256. var strokeGradient = c.createLinearGradient(0, 0, width, 0);
  257. strokeGradient.addColorStop(0, "rgb(0, 0, 255)");
  258. strokeGradient.addColorStop(1, "rgb(255, 0, 0)");
  259. c.fillStyle = fillGradient;
  260. c.strokeStyle = strokeGradient;
  261. c.lineWidth = 2;
  262. c.lineJoin = "round";
  263. c.beginPath();
  264. if (fanOffCheckBox.checked) {
  265. c.moveTo(scaleX(minTemp), scaleY(0));
  266. c.lineTo(stopPoint.centerX, scaleY(0));
  267. } else {
  268. c.moveTo(scaleX(minTemp), stopPoint.centerY);
  269. }
  270. c.lineTo(stopPoint.centerX, stopPoint.centerY);
  271. c.lineTo(maxPoint.centerX, maxPoint.centerY);
  272. c.lineTo(scaleX(maxTemp), maxPoint.centerY);
  273. c.stroke();
  274. c.lineTo(scaleX(maxTemp), height - bottomPadding);
  275. c.lineTo(leftPadding, height - bottomPadding);
  276. c.fill();
  277. fillGradient = c.createLinearGradient(0, 0, 0, height);
  278. fillGradient.addColorStop(0, "rgba(127, 127, 127, 0.6)");
  279. fillGradient.addColorStop(1, "rgba(127, 127, 127, 0.9)");
  280. c.fillStyle = fillGradient;
  281. c.fill();
  282. c.closePath();
  283. c.textAlign = "right";
  284. c.textBaseline = "middle";
  285. c.strokeStyle = palette.dark;
  286. c.fillStyle = palette.dark;
  287. c.lineWidth = 1;
  288. c.strokeRect(leftPadding-0.5, topPadding-0.5, plotWidth+0.5, plotHeight+1.5);
  289. for (var i=0; i<=100; i+=20) {
  290. var y = scaleY(i*2.55);
  291. c.fillText(i + '%', leftPadding - 2, y);
  292. if (i != 0 && i != 100) {
  293. for (var j=leftPadding; j<=width-rightPadding; j+=15) {
  294. c.moveTo(j, y);
  295. c.lineTo(Math.min(j+5, width-rightPadding), y);
  296. }
  297. c.stroke();
  298. }
  299. }
  300. c.textAlign = "center";
  301. c.textBaseline = "top";
  302. var convertedMinTemp = Units.fromCelsius(minTemp, unit);
  303. var convertedMaxTemp = Units.fromCelsius(maxTemp, unit);
  304. for (i=convertedMinTemp; i<convertedMaxTemp; i+= 10) {
  305. var x = scaleX(Units.toCelsius(i, unit));
  306. c.fillText(i + '°', x, topPadding+plotHeight+fontSize/2);
  307. if (i != convertedMinTemp) {
  308. for (var j=scaleY(255); j<=scaleY(0); j+=20) {
  309. c.moveTo(x, j);
  310. c.lineTo(x, Math.min(j+5, width-rightPadding));
  311. }
  312. c.stroke();
  313. }
  314. }
  315. c.fillText(convertedMaxTemp + '°', 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. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  381. Label {
  382. text: i18n("Test start and stop values")
  383. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  384. renderType: Text.NativeRendering
  385. }
  386. Item {
  387. Layout.fillWidth: true
  388. }
  389. Button {
  390. property bool reactivateAfterTesting
  391. id: testButton
  392. text: fan.testing? i18n("Abort") : i18n("Test")
  393. anchors.right: parent.right
  394. onClicked: {
  395. if (fan.testing) {
  396. fan.abortTest();
  397. systemdCom.serviceActive = true;
  398. } else {
  399. reactivateAfterTesting = systemdCom.serviceActive;
  400. systemdCom.serviceActive = false;
  401. minStartInput.text = Qt.binding(function() { return fan.minStart });
  402. fan.test();
  403. }
  404. }
  405. Connections {
  406. target: fan
  407. onTestingChanged: if (!fan.testing) systemdCom.serviceActive = testButton.reactivateAfterTesting
  408. }
  409. }
  410. }
  411. }
  412. }