PwmFan.qml 15 KB

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