PwmFan.qml 15 KB

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