PwmFan.qml 15 KB

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