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