PwmFan.qml 15 KB

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