PwmFan.qml 15 KB

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