PwmFan.qml 15 KB

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