PwmFan.qml 15 KB

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