PwmFan.qml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 real minTemp: 30.0
  30. property real maxTemp: 90.0
  31. property int margin: 5
  32. property int minimizeDuration: 400
  33. property real hwRatio
  34. property int unit: 0
  35. id: root
  36. height: width * hwRatio
  37. color: "transparent"
  38. border.color: "black"
  39. border.width: 2
  40. radius: 10
  41. clip: false
  42. state: fan.active ? "" : "minimized"
  43. function update() {
  44. hasTempCheckBox.checked = fan.hasTemp;
  45. fanOffCheckBox.checked = (fan.minPwm == 0);
  46. minStartInput.text = fan.minStart;
  47. hwmonBox.currentIndex = fan.temp ? fan.temp.parent.index : 0;
  48. tempBox.currentIndex = fan.temp ? fan.temp.index - 1 : 0;
  49. canvas.requestPaint();
  50. }
  51. onFanChanged: update()
  52. onLoaderChanged: update()
  53. onUnitChanged: 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 fontSize: Math.max(9, Math.min(height / 20, 20))
  143. property int leftPadding: fontSize * 4
  144. property int rightPadding: fontSize * 2
  145. property int topPadding: fontSize
  146. property int bottomPadding: fontSize * 2
  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: canvas.fontSize
  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. size: canvas.fontSize
  177. drag.maximumX: Math.min(canvas.scaleX(canvas.scaleTemp(maxPoint.x)-1), maxPoint.x-1)
  178. drag.minimumY: Math.max(canvas.scaleY(canvas.scalePwm(maxPoint.y)-1), maxPoint.y+1)
  179. x: canvas.scaleX(MoreMath.bound(minTemp, fan.minTemp, maxTemp)) - width/2
  180. y: canvas.scaleY(fan.minStop) - height/2
  181. visible: parent.contains(Coordinates.centerOf(this))
  182. drag.onActiveChanged: {
  183. if (!drag.active) {
  184. fan.minStop = canvas.scalePwm(centerY);
  185. fan.minTemp = canvas.scaleTemp(centerX);
  186. if (!fanOffCheckBox.checked) fan.minPwm = fan.minStop;
  187. }
  188. }
  189. }
  190. PwmPoint {
  191. id: maxPoint
  192. color: "red"
  193. size: canvas.fontSize
  194. drag.minimumX: stopPoint.x
  195. drag.maximumY: stopPoint.y
  196. x: canvas.scaleX(MoreMath.bound(minTemp, fan.maxTemp, maxTemp)) - width/2
  197. y: canvas.scaleY(fan.maxPwm) - height/2
  198. visible: parent.contains(Coordinates.centerOf(this))
  199. drag.onActiveChanged: {
  200. if (!drag.active) {
  201. fan.maxPwm = canvas.scalePwm(centerY);
  202. fan.maxTemp = canvas.scaleTemp(centerX);
  203. }
  204. }
  205. }
  206. function scaleX(temp) {
  207. var scaledX = (temp - minTemp) * plotWidth / (maxTemp - minTemp);
  208. return leftPadding + scaledX;
  209. }
  210. function scaleY(pwm) {
  211. var scaledY = pwm * plotHeight / 255;
  212. return height - bottomPadding - scaledY;
  213. }
  214. function scaleTemp(x) {
  215. var scaledTemp = (x - leftPadding) / plotWidth * (maxTemp - minTemp);
  216. return minTemp + scaledTemp;
  217. }
  218. function scalePwm(y) {
  219. var scaledPwm = (y - topPadding) / plotHeight * 255;
  220. return 255 - scaledPwm;
  221. }
  222. onPaint: {
  223. var c = canvas.getContext("2d");
  224. c.font = canvas.fontSize + "px sans-serif";
  225. c.clearRect(0, 0, width, height);
  226. c.fillStyle = palette.base;
  227. c.fillRect(leftPadding, topPadding, plotWidth, plotHeight);
  228. var fillGradient = c.createLinearGradient(0, 0, width, 0);
  229. fillGradient.addColorStop(0, "rgb(0, 0, 255)");
  230. fillGradient.addColorStop(1, "rgb(255, 0, 0)");
  231. var strokeGradient = c.createLinearGradient(0, 0, width, 0);
  232. strokeGradient.addColorStop(0, "rgb(0, 0, 255)");
  233. strokeGradient.addColorStop(1, "rgb(255, 0, 0)");
  234. c.fillStyle = fillGradient;
  235. c.strokeStyle = strokeGradient;
  236. c.lineWidth = 2;
  237. c.lineJoin = "round";
  238. c.beginPath();
  239. if (fanOffCheckBox.checked) {
  240. c.moveTo(scaleX(minTemp), scaleY(0));
  241. c.lineTo(stopPoint.centerX, scaleY(0));
  242. } else {
  243. c.moveTo(scaleX(minTemp), stopPoint.centerY);
  244. }
  245. c.lineTo(stopPoint.centerX, stopPoint.centerY);
  246. c.lineTo(maxPoint.centerX, maxPoint.centerY);
  247. c.lineTo(scaleX(maxTemp), maxPoint.centerY);
  248. c.stroke();
  249. c.lineTo(scaleX(maxTemp), height - bottomPadding);
  250. c.lineTo(leftPadding, height - bottomPadding);
  251. c.fill();
  252. fillGradient = c.createLinearGradient(0, 0, 0, height);
  253. fillGradient.addColorStop(0, "rgba(127, 127, 127, 0.6)");
  254. fillGradient.addColorStop(1, "rgba(127, 127, 127, 0.9)");
  255. c.fillStyle = fillGradient;
  256. c.fill();
  257. c.closePath();
  258. c.textAlign = "right";
  259. c.textBaseline = "middle";
  260. c.strokeStyle = palette.dark;
  261. c.fillStyle = palette.dark;
  262. c.lineWidth = 1;
  263. c.strokeRect(leftPadding-0.5, topPadding-0.5, plotWidth+0.5, plotHeight+1.5);
  264. for (var i=0; i<=100; i+=20) {
  265. var y = scaleY(i*2.55);
  266. c.fillText(i + '%', leftPadding - 2, y);
  267. if (i != 0 && i != 100) {
  268. for (var j=leftPadding; j<=width-rightPadding; j+=15) {
  269. c.moveTo(j, y);
  270. c.lineTo(Math.min(j+5, width-rightPadding), y);
  271. }
  272. c.stroke();
  273. }
  274. }
  275. c.textAlign = "center";
  276. c.textBaseline = "top";
  277. var convertedMinTemp = Units.fromCelsius(minTemp, unit);
  278. var convertedMaxTemp = Units.fromCelsius(maxTemp, unit);
  279. for (i=convertedMinTemp; i<convertedMaxTemp; i+= 10) {
  280. var x = scaleX(Units.toCelsius(i, unit));
  281. c.fillText(i + '°', x, topPadding+plotHeight+fontSize/2);
  282. if (i != convertedMinTemp) {
  283. for (var j=scaleY(255); j<=scaleY(0); j+=20) {
  284. c.moveTo(x, j);
  285. c.lineTo(x, Math.min(j+5, width-rightPadding));
  286. }
  287. c.stroke();
  288. }
  289. }
  290. c.fillText(convertedMaxTemp + '°', scaleX(maxTemp), topPadding+plotHeight+fontSize/2);
  291. }
  292. }
  293. ColumnLayout {
  294. property int padding: 10
  295. id: settingsArea
  296. anchors {
  297. left: parent.left
  298. leftMargin: padding
  299. right: parent.right
  300. rightMargin: padding
  301. bottom: parent.bottom
  302. bottomMargin: padding
  303. }
  304. visible: root.height >= header.height + height + 2*margin
  305. opacity: canvas.opacity
  306. clip: true
  307. spacing: 2
  308. RowLayout {
  309. spacing: 0
  310. width: parent.width
  311. CheckBox {
  312. id: hasTempCheckBox
  313. text: i18n("Controlled by:")
  314. checked: fan.hasTemp
  315. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  316. onCheckedChanged: fan.hasTemp = checked
  317. }
  318. Row {
  319. Layout.fillWidth: true
  320. ComboBox {
  321. property QtObject hwmon: loader.hwmons[currentIndex]
  322. id: hwmonBox
  323. width: (parent.width-slash.width) / 2
  324. anchors.verticalCenter: parent.verticalCenter
  325. model: ArrayFunctions.names(loader.hwmons)
  326. enabled: hasTempCheckBox.checked
  327. }
  328. Label {
  329. id: slash
  330. text: "/"
  331. color: enabled ? palette.text : disabledPalette.text
  332. anchors.verticalCenter: parent.verticalCenter
  333. verticalAlignment: Text.AlignVCenter
  334. enabled: hasTempCheckBox.checked
  335. renderType: Text.NativeRendering
  336. }
  337. ComboBox {
  338. id: tempBox
  339. width: (parent.width-slash.width) / 2
  340. anchors.verticalCenter: parent.verticalCenter
  341. model: ArrayFunctions.names(hwmonBox.hwmon.temps)
  342. enabled: hasTempCheckBox.checked
  343. onCurrentIndexChanged: {
  344. if (hasTempCheckBox.checked && hwmonBox.hwmon)
  345. fan.temp = hwmonBox.hwmon.temps[currentIndex];
  346. }
  347. onModelChanged: {
  348. if (hasTempCheckBox.checked && hwmonBox.hwmon)
  349. fan.temp = hwmonBox.hwmon.temps[currentIndex];
  350. }
  351. }
  352. }
  353. }
  354. CheckBox {
  355. id: fanOffCheckBox
  356. text: i18n("Turn Fan off if temp < MINTEMP")
  357. enabled: hasTempCheckBox.checked
  358. checked: fan.minPwm == 0
  359. onCheckedChanged: {
  360. fan.minPwm = checked ? 0 : fan.minStop;
  361. canvas.requestPaint();
  362. }
  363. }
  364. RowLayout {
  365. anchors.left: parent.left
  366. anchors.right: parent.right
  367. Label {
  368. text: i18n("Pwm value for fan to start:")
  369. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  370. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  371. color: enabled ? palette.text : disabledPalette.text
  372. renderType: Text.NativeRendering
  373. }
  374. OptionInput {
  375. id: minStartInput
  376. anchors.right: parent.right
  377. width: 50
  378. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  379. text: fan.minStart
  380. onTextChanged: fan.minStart = text
  381. }
  382. }
  383. RowLayout {
  384. anchors.left: parent.left
  385. anchors.right: parent.right
  386. Label {
  387. text: i18n("Test start and stop values")
  388. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  389. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  390. color: enabled ? palette.text : disabledPalette.text
  391. renderType: Text.NativeRendering
  392. }
  393. Button {
  394. text: fan.testing? i18n("Abort") : i18n("Test")
  395. anchors.right: parent.right
  396. height: hwmonBox.height
  397. onClicked: {
  398. if (fan.testing) {
  399. systemdCom.dbusAction("StartUnit", [systemdCom.serviceName + ".service", "replace"]);
  400. fan.abortTesting();
  401. } else {
  402. systemdCom.dbusAction("StopUnit", [systemdCom.serviceName + ".service", "replace"]);
  403. minStartInput.text = Qt.binding(function() { return fan.minStart });
  404. fan.test();
  405. }
  406. }
  407. }
  408. }
  409. }
  410. }