PwmFan.qml 15 KB

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