PwmFan.qml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. import "../scripts/colors.js" as Colors
  27. Rectangle {
  28. property QtObject fan
  29. property QtObject loader
  30. property QtObject systemdCom
  31. property real minTemp: 40.0
  32. property real maxTemp: 90.0
  33. property int margin: 5
  34. property int minimizeDuration: 400
  35. property int unit: 0
  36. readonly property alias name: nameField.text
  37. id: root
  38. color: "transparent"
  39. border.color: palette.windowText
  40. border.width: 2
  41. radius: 10
  42. clip: false
  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. if (fan.hasTemp && loader) {
  48. tempBox.currentIndex = loader.allTemps.indexOf(fan.temp);
  49. }
  50. }
  51. canvas.requestPaint();
  52. }
  53. onFanChanged: update();
  54. onLoaderChanged: update()
  55. onUnitChanged: if (fan) canvas.requestPaint()
  56. onMinTempChanged: if (fan) canvas.requestPaint()
  57. onMaxTempChanged: if (fan) canvas.requestPaint()
  58. Connections {
  59. target: loader
  60. onConfigUrlChanged: update()
  61. }
  62. SystemPalette {
  63. id: palette
  64. colorGroup: SystemPalette.Active
  65. }
  66. SystemPalette {
  67. id: disabledPalette
  68. colorGroup: SystemPalette.Disabled
  69. }
  70. TextEdit {
  71. id: nameField
  72. anchors {
  73. left: parent.left
  74. leftMargin: margin
  75. right: parent.right
  76. rightMargin: margin
  77. top: parent.top
  78. topMargin: margin
  79. }
  80. visible: root.height >= height + margin*2
  81. text: fan.name
  82. onTextChanged: fan.name = text
  83. color: palette.text
  84. horizontalAlignment: TextEdit.AlignLeft
  85. wrapMode: TextEdit.Wrap
  86. font.bold: true
  87. font.pointSize: 14
  88. selectByMouse: true
  89. MouseArea {
  90. anchors.fill: parent
  91. cursorShape: Qt.IBeamCursor
  92. acceptedButtons: Qt.NoButton
  93. }
  94. }
  95. Canvas {
  96. property int fontSize: MoreMath.bound(9, height / 20 + 1, 18)
  97. property int leftPadding: fontSize * 4
  98. property int rightPadding: fontSize * 2
  99. property int topPadding: fontSize
  100. property int bottomPadding: fontSize * 2
  101. property bool drawingEnabled: height > bottomPadding + topPadding && width > leftPadding + rightPadding
  102. property int plotWidth: width - leftPadding - rightPadding
  103. property int plotHeight: height - topPadding - bottomPadding
  104. property alias minTemp: root.minTemp //needed for pwmPoints
  105. property alias maxTemp: root.maxTemp //needed for pwmPoints
  106. property QtObject pal: fan.hasTemp ? palette : disabledPalette
  107. id: canvas
  108. renderTarget: Canvas.FramebufferObject
  109. anchors {
  110. left: parent.left
  111. right: parent.right
  112. top: nameField.bottom
  113. bottom: settingsArea.top
  114. }
  115. Rectangle {
  116. property real unscaledTemp: fan.temp ? fan.temp.value : minTemp
  117. property real unscaledPwm: fan.pwm
  118. id: currentPwm
  119. x: parent.scaleX(unscaledTemp) - width/2
  120. y: parent.scaleY(unscaledPwm) - height/2
  121. width: canvas.fontSize
  122. height: width
  123. radius: width / 2
  124. color: "black"
  125. visible: canvas.contains(Coordinates.centerOf(this)) && fan.hasTemp && canvas.drawingEnabled
  126. Behavior on unscaledTemp {
  127. SpringAnimation {
  128. epsilon: 0.1
  129. spring: 1.0
  130. damping: 0.4
  131. }
  132. }
  133. Behavior on unscaledPwm {
  134. SpringAnimation {
  135. epsilon: 0.1
  136. spring: 1.0
  137. damping: 0.4
  138. }
  139. }
  140. }
  141. PwmPoint {
  142. id: stopPoint
  143. color: fan.hasTemp ? "blue" : Qt.tint(canvas.pal.light, Qt.rgba(0, 0, 1, 0.5))
  144. size: canvas.fontSize
  145. unit: root.unit
  146. enabled: fan.hasTemp
  147. drag.maximumX: Math.min(canvas.scaleX(canvas.scaleTemp(maxPoint.x)-1), maxPoint.x-1)
  148. drag.minimumY: Math.max(canvas.scaleY(canvas.scalePwm(maxPoint.y)-1), maxPoint.y+1)
  149. x: fan.hasTemp ? canvas.scaleX(MoreMath.bound(minTemp, fan.minTemp, maxTemp)) - width/2 : canvas.leftPadding - width/2
  150. y: fan.hasTemp ? canvas.scaleY(fan.minStop) - height/2 : canvas.topPadding -height/2
  151. visible: canvas.contains(Coordinates.centerOf(this)) && canvas.drawingEnabled
  152. drag.onActiveChanged: {
  153. if (!drag.active) {
  154. fan.minStop = Math.round(canvas.scalePwm(centerY));
  155. fan.minTemp = Math.round(canvas.scaleTemp(centerX));
  156. if (!fanOffCheckBox.checked) fan.minPwm = fan.minStop;
  157. }
  158. }
  159. }
  160. PwmPoint {
  161. id: maxPoint
  162. color: fan.hasTemp ? "red" : Qt.tint(canvas.pal.light, Qt.rgba(1, 0, 0, 0.5))
  163. size: canvas.fontSize
  164. unit: root.unit
  165. enabled: fan.hasTemp
  166. drag.minimumX: Math.max(canvas.scaleX(canvas.scaleTemp(stopPoint.x)+1), stopPoint.x+1)
  167. drag.maximumY: Math.min(canvas.scaleY(canvas.scalePwm(stopPoint.y)+1), stopPoint.y-1)
  168. x: fan.hasTemp ? canvas.scaleX(MoreMath.bound(minTemp, fan.maxTemp, maxTemp)) - width/2 : canvas.width - canvas.rightPadding - width/2
  169. y: fan.hasTemp ? canvas.scaleY(fan.maxPwm) - height/2 : canvas.topPadding - height/2
  170. visible: canvas.contains(Coordinates.centerOf(this)) && canvas.drawingEnabled
  171. drag.onActiveChanged: {
  172. if (!drag.active) {
  173. fan.maxPwm = Math.round(canvas.scalePwm(centerY));
  174. fan.maxTemp = Math.round(canvas.scaleTemp(centerX));
  175. }
  176. }
  177. }
  178. function scaleX(temp) {
  179. var scaledX = (temp - minTemp) * plotWidth / (maxTemp - minTemp);
  180. return leftPadding + scaledX;
  181. }
  182. function scaleY(pwm) {
  183. var scaledY = pwm * plotHeight / 255;
  184. return height - bottomPadding - scaledY;
  185. }
  186. function scaleTemp(x) {
  187. var scaledTemp = (x - leftPadding) / plotWidth * (maxTemp - minTemp);
  188. return minTemp + scaledTemp;
  189. }
  190. function scalePwm(y) {
  191. var scaledPwm = (y - topPadding) / plotHeight * 255;
  192. return 255 - scaledPwm;
  193. }
  194. onPaint: {
  195. var c = canvas.getContext("2d");
  196. c.clearRect(0, 0, width, height);
  197. if (canvas.drawingEnabled) {
  198. c.font = canvas.fontSize + "px sans-serif";
  199. c.fillStyle = pal.light;
  200. c.strokeStyle = pal.text;
  201. c.fillRect(leftPadding, topPadding, plotWidth, plotHeight);
  202. var fillGradient = c.createLinearGradient(0, 0, width, 0);
  203. fillGradient.addColorStop(0, "rgb(0, 0, 255)");
  204. fillGradient.addColorStop(1, "rgb(255, 0, 0)");
  205. var strokeGradient = c.createLinearGradient(0, 0, width, 0);
  206. strokeGradient.addColorStop(0, "rgb(0, 0, 255)");
  207. strokeGradient.addColorStop(1, "rgb(255, 0, 0)");
  208. c.fillStyle = fillGradient;
  209. c.strokeStyle = strokeGradient;
  210. c.lineWidth = 2;
  211. c.lineJoin = "round";
  212. c.beginPath();
  213. if (fanOffCheckBox.checked) {
  214. c.moveTo(scaleX(minTemp), scaleY(0));
  215. c.lineTo(stopPoint.centerX, scaleY(0));
  216. } else {
  217. c.moveTo(scaleX(minTemp), stopPoint.centerY);
  218. }
  219. c.lineTo(stopPoint.centerX, stopPoint.centerY);
  220. c.lineTo(maxPoint.centerX, maxPoint.centerY);
  221. c.lineTo(scaleX(maxTemp), maxPoint.centerY);
  222. c.stroke();
  223. c.lineTo(scaleX(maxTemp), height - bottomPadding);
  224. c.lineTo(leftPadding, height - bottomPadding);
  225. c.fill();
  226. fillGradient = c.createLinearGradient(0, 0, 0, height);
  227. fillGradient.addColorStop(0, Colors.setAlpha(canvas.pal.light, 0.6));
  228. fillGradient.addColorStop(1, Colors.setAlpha(canvas.pal.light, 0.9));
  229. c.fillStyle = fillGradient;
  230. c.fill();
  231. c.closePath();
  232. c.textAlign = "right";
  233. c.textBaseline = "middle";
  234. c.strokeStyle = pal.text;
  235. c.fillStyle = pal.text;
  236. c.lineWidth = 1;
  237. c.strokeRect(leftPadding-0.5, topPadding-0.5, plotWidth+0.5, plotHeight+1.5);
  238. for (var i=0; i<=100; i+=20) {
  239. var y = scaleY(i*2.55);
  240. c.fillText(i + '%', leftPadding - 2, y);
  241. if (i != 0 && i != 100) {
  242. for (var j=leftPadding; j<=width-rightPadding; j+=15) {
  243. c.moveTo(j, y);
  244. c.lineTo(Math.min(j+5, width-rightPadding), y);
  245. }
  246. c.stroke();
  247. }
  248. }
  249. c.textAlign = "center";
  250. c.textBaseline = "top";
  251. var convertedMinTemp = Units.fromCelsius(minTemp, unit);
  252. var convertedMaxTemp = Units.fromCelsius(maxTemp, unit);
  253. var suffix = (unit == 0) ? "°C" : (unit == 1) ? "K" : "°F"
  254. var lastTemp;
  255. for (var i=convertedMinTemp; i<convertedMaxTemp; i+= 10) {
  256. lastTemp = i;
  257. var x = scaleX(Units.toCelsius(i, unit));
  258. c.fillText(i + suffix, x, topPadding+plotHeight+fontSize/2);
  259. if (i != convertedMinTemp) {
  260. for (var j=scaleY(255); j<=scaleY(0); j+=20) {
  261. c.moveTo(x, j);
  262. c.lineTo(x, Math.min(j+5, width-rightPadding));
  263. }
  264. c.stroke();
  265. }
  266. }
  267. if ((convertedMaxTemp - lastTemp) > 5)
  268. c.fillText(convertedMaxTemp + suffix, scaleX(maxTemp), topPadding+plotHeight+fontSize/2);
  269. }
  270. }
  271. }
  272. ColumnLayout {
  273. property int padding: 10
  274. id: settingsArea
  275. anchors {
  276. left: parent.left
  277. leftMargin: padding
  278. right: parent.right
  279. rightMargin: padding
  280. bottom: parent.bottom
  281. bottomMargin: padding
  282. }
  283. visible: root.height >= nameField.height + height + 2*margin
  284. opacity: canvas.opacity
  285. clip: true
  286. spacing: 2
  287. RowLayout {
  288. CheckBox {
  289. id: hasTempCheckBox
  290. text: i18n("Controlled by:")
  291. checked: fan.hasTemp
  292. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  293. onCheckedChanged: fan.hasTemp = checked
  294. }
  295. RowLayout {
  296. ComboBox {
  297. id: tempBox
  298. Layout.fillWidth: true
  299. model: ArrayFunctions.namesWithPaths(loader.allTemps)
  300. enabled: hasTempCheckBox.checked
  301. onCurrentIndexChanged: {
  302. if (hasTempCheckBox.checked)
  303. fan.temp = loader.allTemps[currentIndex];
  304. }
  305. }
  306. }
  307. }
  308. CheckBox {
  309. id: fanOffCheckBox
  310. text: i18n("Turn Fan off if temp < MINTEMP")
  311. enabled: hasTempCheckBox.checked
  312. checked: fan.minPwm == 0
  313. onCheckedChanged: {
  314. fan.minPwm = checked ? 0 : fan.minStop;
  315. canvas.requestPaint();
  316. }
  317. }
  318. RowLayout {
  319. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  320. Label {
  321. text: i18n("Pwm value for fan to start:")
  322. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  323. renderType: Text.NativeRendering
  324. }
  325. SpinBox {
  326. id: minStartInput
  327. Layout.fillWidth: true
  328. minimumValue: 1
  329. maximumValue: 100
  330. decimals: 1
  331. value: Math.round(fan.minStart / 2.55)
  332. suffix: i18n("%")
  333. onValueChanged: fan.minStart = Math.round(value * 2.55)
  334. }
  335. }
  336. RowLayout {
  337. visible: systemdCom
  338. Item {
  339. Layout.fillWidth: true
  340. }
  341. Button {
  342. property bool reactivateAfterTesting
  343. id: testButton
  344. text: fan.testing ? i18n("Abort test") : i18n("Test start and stop values")
  345. iconName: "dialog-password"
  346. anchors.right: parent.right
  347. onClicked: {
  348. if (fan.testing) {
  349. fan.abortTest();
  350. systemdCom.serviceActive = true;
  351. } else {
  352. reactivateAfterTesting = systemdCom.serviceActive;
  353. systemdCom.serviceActive = false;
  354. minStartInput.value = Qt.binding(function() { return Math.round(fan.minStart / 2.55) });
  355. if (!fan.test()) {
  356. systemdCom.serviceActive = reactivateAfterTesting;
  357. }
  358. }
  359. }
  360. Connections {
  361. target: fan
  362. onTestingChanged: if (!fan.testing) systemdCom.serviceActive = testButton.reactivateAfterTesting
  363. }
  364. }
  365. }
  366. }
  367. }