2
0

PwmFan.qml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. StatusPoint {
  116. id: currentPwm
  117. size: canvas.fontSize
  118. visible: canvas.contains(Coordinates.centerOf(this)) && fan.hasTemp && canvas.drawingEnabled
  119. fan: root.fan
  120. }
  121. PwmPoint {
  122. id: stopPoint
  123. color: fan.hasTemp ? "blue" : Qt.tint(canvas.pal.light, Qt.rgba(0, 0, 1, 0.5))
  124. size: canvas.fontSize
  125. unit: root.unit
  126. enabled: fan.hasTemp
  127. drag.maximumX: Math.min(canvas.scaleX(canvas.scaleTemp(maxPoint.x)-1), maxPoint.x-1)
  128. drag.minimumY: Math.max(canvas.scaleY(canvas.scalePwm(maxPoint.y)-1), maxPoint.y+1)
  129. x: fan.hasTemp ? canvas.scaleX(MoreMath.bound(minTemp, fan.minTemp, maxTemp)) - width/2 : canvas.leftPadding - width/2
  130. y: fan.hasTemp ? canvas.scaleY(fan.minStop) - height/2 : canvas.topPadding -height/2
  131. visible: canvas.contains(Coordinates.centerOf(this)) && canvas.drawingEnabled
  132. drag.onActiveChanged: {
  133. if (!drag.active) {
  134. fan.minStop = Math.round(canvas.scalePwm(centerY));
  135. fan.minTemp = Math.round(canvas.scaleTemp(centerX));
  136. if (!fanOffCheckBox.checked) fan.minPwm = fan.minStop;
  137. }
  138. }
  139. }
  140. PwmPoint {
  141. id: maxPoint
  142. color: fan.hasTemp ? "red" : Qt.tint(canvas.pal.light, Qt.rgba(1, 0, 0, 0.5))
  143. size: canvas.fontSize
  144. unit: root.unit
  145. enabled: fan.hasTemp
  146. drag.minimumX: Math.max(canvas.scaleX(canvas.scaleTemp(stopPoint.x)+1), stopPoint.x+1)
  147. drag.maximumY: Math.min(canvas.scaleY(canvas.scalePwm(stopPoint.y)+1), stopPoint.y-1)
  148. x: fan.hasTemp ? canvas.scaleX(MoreMath.bound(minTemp, fan.maxTemp, maxTemp)) - width/2 : canvas.width - canvas.rightPadding - width/2
  149. y: fan.hasTemp ? canvas.scaleY(fan.maxPwm) - height/2 : canvas.topPadding - height/2
  150. visible: canvas.contains(Coordinates.centerOf(this)) && canvas.drawingEnabled
  151. drag.onActiveChanged: {
  152. if (!drag.active) {
  153. fan.maxPwm = Math.round(canvas.scalePwm(centerY));
  154. fan.maxTemp = Math.round(canvas.scaleTemp(centerX));
  155. }
  156. }
  157. }
  158. function scaleX(temp) {
  159. var scaledX = (temp - minTemp) * plotWidth / (maxTemp - minTemp);
  160. return leftPadding + scaledX;
  161. }
  162. function scaleY(pwm) {
  163. var scaledY = pwm * plotHeight / 255;
  164. return height - bottomPadding - scaledY;
  165. }
  166. function scaleTemp(x) {
  167. var scaledTemp = (x - leftPadding) / plotWidth * (maxTemp - minTemp);
  168. return minTemp + scaledTemp;
  169. }
  170. function scalePwm(y) {
  171. var scaledPwm = (y - topPadding) / plotHeight * 255;
  172. return 255 - scaledPwm;
  173. }
  174. onPaint: {
  175. var c = canvas.getContext("2d");
  176. c.clearRect(0, 0, width, height);
  177. if (canvas.drawingEnabled) {
  178. c.font = canvas.fontSize + "px sans-serif";
  179. c.fillStyle = pal.light;
  180. c.strokeStyle = pal.text;
  181. c.fillRect(leftPadding, topPadding, plotWidth, plotHeight);
  182. var fillGradient = c.createLinearGradient(0, 0, width, 0);
  183. fillGradient.addColorStop(0, "rgb(0, 0, 255)");
  184. fillGradient.addColorStop(1, "rgb(255, 0, 0)");
  185. var strokeGradient = c.createLinearGradient(0, 0, width, 0);
  186. strokeGradient.addColorStop(0, "rgb(0, 0, 255)");
  187. strokeGradient.addColorStop(1, "rgb(255, 0, 0)");
  188. c.fillStyle = fillGradient;
  189. c.strokeStyle = strokeGradient;
  190. c.lineWidth = 2;
  191. c.lineJoin = "round";
  192. c.beginPath();
  193. if (fanOffCheckBox.checked) {
  194. c.moveTo(scaleX(minTemp), scaleY(0));
  195. c.lineTo(stopPoint.centerX, scaleY(0));
  196. } else {
  197. c.moveTo(scaleX(minTemp), stopPoint.centerY);
  198. }
  199. c.lineTo(stopPoint.centerX, stopPoint.centerY);
  200. c.lineTo(maxPoint.centerX, maxPoint.centerY);
  201. c.lineTo(scaleX(maxTemp), maxPoint.centerY);
  202. c.stroke();
  203. c.lineTo(scaleX(maxTemp), height - bottomPadding);
  204. c.lineTo(leftPadding, height - bottomPadding);
  205. c.fill();
  206. fillGradient = c.createLinearGradient(0, 0, 0, height);
  207. fillGradient.addColorStop(0, Colors.setAlpha(canvas.pal.light, 0.6));
  208. fillGradient.addColorStop(1, Colors.setAlpha(canvas.pal.light, 0.9));
  209. c.fillStyle = fillGradient;
  210. c.fill();
  211. c.closePath();
  212. c.textAlign = "right";
  213. c.textBaseline = "middle";
  214. c.strokeStyle = pal.text;
  215. c.fillStyle = pal.text;
  216. c.lineWidth = 1;
  217. c.strokeRect(leftPadding-0.5, topPadding-0.5, plotWidth+0.5, plotHeight+1.5);
  218. for (var i=0; i<=100; i+=20) {
  219. var y = scaleY(i*2.55);
  220. c.fillText(i + '%', leftPadding - 2, y);
  221. if (i != 0 && i != 100) {
  222. for (var j=leftPadding; j<=width-rightPadding; j+=15) {
  223. c.moveTo(j, y);
  224. c.lineTo(Math.min(j+5, width-rightPadding), y);
  225. }
  226. c.stroke();
  227. }
  228. }
  229. c.textAlign = "center";
  230. c.textBaseline = "top";
  231. var convertedMinTemp = Units.fromCelsius(minTemp, unit);
  232. var convertedMaxTemp = Units.fromCelsius(maxTemp, unit);
  233. var suffix = (unit == 0) ? "°C" : (unit == 1) ? "K" : "°F"
  234. var lastTemp;
  235. for (var i=convertedMinTemp; i<convertedMaxTemp; i+= 10) {
  236. lastTemp = i;
  237. var x = scaleX(Units.toCelsius(i, unit));
  238. c.fillText(i + suffix, x, topPadding+plotHeight+fontSize/2);
  239. if (i != convertedMinTemp) {
  240. for (var j=scaleY(255); j<=scaleY(0); j+=20) {
  241. c.moveTo(x, j);
  242. c.lineTo(x, Math.min(j+5, width-rightPadding));
  243. }
  244. c.stroke();
  245. }
  246. }
  247. if ((convertedMaxTemp - lastTemp) > 5)
  248. c.fillText(convertedMaxTemp + suffix, scaleX(maxTemp), topPadding+plotHeight+fontSize/2);
  249. }
  250. }
  251. }
  252. ColumnLayout {
  253. property int padding: 10
  254. id: settingsArea
  255. anchors {
  256. left: parent.left
  257. leftMargin: padding
  258. right: parent.right
  259. rightMargin: padding
  260. bottom: parent.bottom
  261. bottomMargin: padding
  262. }
  263. visible: root.height >= nameField.height + height + 2*margin
  264. opacity: canvas.opacity
  265. clip: true
  266. spacing: 2
  267. RowLayout {
  268. CheckBox {
  269. id: hasTempCheckBox
  270. text: i18n("Controlled by:")
  271. checked: fan.hasTemp
  272. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  273. onCheckedChanged: fan.hasTemp = checked
  274. }
  275. RowLayout {
  276. ComboBox {
  277. id: tempBox
  278. Layout.fillWidth: true
  279. model: ArrayFunctions.namesWithPaths(loader.allTemps)
  280. enabled: hasTempCheckBox.checked
  281. onCurrentIndexChanged: {
  282. if (hasTempCheckBox.checked)
  283. fan.temp = loader.allTemps[currentIndex];
  284. }
  285. }
  286. }
  287. }
  288. CheckBox {
  289. id: fanOffCheckBox
  290. text: i18n("Turn Fan off if temp < MINTEMP")
  291. enabled: hasTempCheckBox.checked
  292. checked: fan.minPwm == 0
  293. onCheckedChanged: {
  294. fan.minPwm = checked ? 0 : fan.minStop;
  295. canvas.requestPaint();
  296. }
  297. }
  298. RowLayout {
  299. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  300. Label {
  301. text: i18n("Pwm value for fan to start:")
  302. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  303. renderType: Text.NativeRendering
  304. }
  305. SpinBox {
  306. id: minStartInput
  307. Layout.fillWidth: true
  308. minimumValue: 1
  309. maximumValue: 100
  310. decimals: 1
  311. value: Math.round(fan.minStart / 2.55)
  312. suffix: i18n("%")
  313. onValueChanged: fan.minStart = Math.round(value * 2.55)
  314. }
  315. }
  316. RowLayout {
  317. visible: systemdCom
  318. Item {
  319. Layout.fillWidth: true
  320. }
  321. Button {
  322. property bool reactivateAfterTesting
  323. id: testButton
  324. text: fan.testing ? i18n("Abort test") : i18n("Test start and stop values")
  325. iconName: "dialog-password"
  326. anchors.right: parent.right
  327. onClicked: {
  328. if (fan.testing) {
  329. fan.abortTest();
  330. systemdCom.serviceActive = true;
  331. } else {
  332. reactivateAfterTesting = systemdCom.serviceActive;
  333. systemdCom.serviceActive = false;
  334. minStartInput.value = Qt.binding(function() { return Math.round(fan.minStart / 2.55) });
  335. if (!fan.test()) {
  336. systemdCom.serviceActive = reactivateAfterTesting;
  337. }
  338. }
  339. }
  340. Connections {
  341. target: fan
  342. onTestingChanged: if (!fan.testing) systemdCom.serviceActive = testButton.reactivateAfterTesting
  343. }
  344. }
  345. }
  346. }
  347. }