PwmFan.qml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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: 30.0
  31. property real maxTemp: 90.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. implicitWidth: 800
  38. implicitHeight: 600
  39. color: "transparent"
  40. border.color: "black"
  41. border.width: 2
  42. radius: 10
  43. clip: false
  44. state: fan.active ? "" : minimizable ? "minimized" : ""
  45. function update() {
  46. if (fan) {
  47. hasTempCheckBox.checked = Qt.binding(function() { return fan.hasTemp; })
  48. fanOffCheckBox.checked = Qt.binding(function() { return (fan.minPwm == 0); })
  49. minStartInput.text = Qt.binding(function() { return fan.minStart; })
  50. if (fan.hasTemp) {
  51. hwmonBox.currentIndex = fan.temp.parent.index;
  52. tempBox.currentIndex = fan.temp.index - 1;
  53. }
  54. }
  55. canvas.requestPaint();
  56. }
  57. onFanChanged: update()
  58. onUnitChanged: update()
  59. onMinTempChanged: update()
  60. onMaxTempChanged: update()
  61. Connections {
  62. target: loader
  63. onConfigUrlChanged: update()
  64. }
  65. states: [
  66. State {
  67. name: "minimized"
  68. PropertyChanges {
  69. target: root
  70. height: header.height + 2*margin
  71. }
  72. },
  73. State {
  74. name: "maximized"
  75. // PropertyChanges {
  76. // target: root
  77. // height:
  78. // }
  79. }
  80. ]
  81. transitions: Transition {
  82. NumberAnimation {
  83. target: root
  84. property: "height"
  85. easing.amplitude: 1.5
  86. easing.type: Easing.InOutQuad
  87. duration: minimizeDuration
  88. }
  89. }
  90. SystemPalette {
  91. id: palette
  92. }
  93. RowLayout {
  94. id: header
  95. anchors {
  96. left: parent.left
  97. leftMargin: margin
  98. right: parent.right
  99. rightMargin: margin
  100. top: parent.top
  101. topMargin: margin
  102. }
  103. z: -1
  104. clip: true
  105. spacing: margin
  106. TextEdit {
  107. id: nameField
  108. Layout.alignment: Qt.AlignTop
  109. text: fan.name
  110. onTextChanged: fan.name = text;
  111. horizontalAlignment: TextEdit.AlignLeft
  112. wrapMode: TextEdit.Wrap
  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: Math.max(9, Math.min(height / 20, 20))
  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. id: currentPwm
  170. x: parent.scaleX(fan.temp ? fan.temp.value : minTemp) - width/2
  171. y: parent.scaleY(fan.pwm) - height/2
  172. width: canvas.fontSize
  173. height: width
  174. radius: width / 2
  175. color: "black"
  176. visible: parent.contains(Coordinates.centerOf(this))
  177. }
  178. PwmPoint {
  179. id: stopPoint
  180. color: "blue"
  181. size: canvas.fontSize
  182. drag.maximumX: Math.min(canvas.scaleX(canvas.scaleTemp(maxPoint.x)-1), maxPoint.x-1)
  183. drag.minimumY: Math.max(canvas.scaleY(canvas.scalePwm(maxPoint.y)-1), maxPoint.y+1)
  184. x: canvas.scaleX(MoreMath.bound(minTemp, fan.minTemp, maxTemp)) - width/2
  185. y: canvas.scaleY(fan.minStop) - height/2
  186. visible: parent.contains(Coordinates.centerOf(this)) && parent.height > 0
  187. drag.onActiveChanged: {
  188. if (!drag.active) {
  189. fan.minStop = canvas.scalePwm(centerY);
  190. fan.minTemp = canvas.scaleTemp(centerX);
  191. if (!fanOffCheckBox.checked) fan.minPwm = fan.minStop;
  192. }
  193. }
  194. }
  195. PwmPoint {
  196. id: maxPoint
  197. color: "red"
  198. size: canvas.fontSize
  199. drag.minimumX: stopPoint.x
  200. drag.maximumY: stopPoint.y
  201. x: canvas.scaleX(MoreMath.bound(minTemp, fan.maxTemp, maxTemp)) - width/2
  202. y: canvas.scaleY(fan.maxPwm) - height/2
  203. visible: parent.contains(Coordinates.centerOf(this)) && parent.height > 0
  204. drag.onActiveChanged: {
  205. if (!drag.active) {
  206. fan.maxPwm = canvas.scalePwm(centerY);
  207. fan.maxTemp = canvas.scaleTemp(centerX);
  208. }
  209. }
  210. }
  211. function scaleX(temp) {
  212. var scaledX = (temp - minTemp) * plotWidth / (maxTemp - minTemp);
  213. return leftPadding + scaledX;
  214. }
  215. function scaleY(pwm) {
  216. var scaledY = pwm * plotHeight / 255;
  217. return height - bottomPadding - scaledY;
  218. }
  219. function scaleTemp(x) {
  220. var scaledTemp = (x - leftPadding) / plotWidth * (maxTemp - minTemp);
  221. return minTemp + scaledTemp;
  222. }
  223. function scalePwm(y) {
  224. var scaledPwm = (y - topPadding) / plotHeight * 255;
  225. return 255 - scaledPwm;
  226. }
  227. onPaint: {
  228. var c = canvas.getContext("2d");
  229. c.font = canvas.fontSize + "px sans-serif";
  230. c.clearRect(0, 0, width, height);
  231. c.fillStyle = palette.base;
  232. c.fillRect(leftPadding, topPadding, plotWidth, plotHeight);
  233. var fillGradient = c.createLinearGradient(0, 0, width, 0);
  234. fillGradient.addColorStop(0, "rgb(0, 0, 255)");
  235. fillGradient.addColorStop(1, "rgb(255, 0, 0)");
  236. var strokeGradient = c.createLinearGradient(0, 0, width, 0);
  237. strokeGradient.addColorStop(0, "rgb(0, 0, 255)");
  238. strokeGradient.addColorStop(1, "rgb(255, 0, 0)");
  239. c.fillStyle = fillGradient;
  240. c.strokeStyle = strokeGradient;
  241. c.lineWidth = 2;
  242. c.lineJoin = "round";
  243. c.beginPath();
  244. if (fanOffCheckBox.checked) {
  245. c.moveTo(scaleX(minTemp), scaleY(0));
  246. c.lineTo(stopPoint.centerX, scaleY(0));
  247. } else {
  248. c.moveTo(scaleX(minTemp), stopPoint.centerY);
  249. }
  250. c.lineTo(stopPoint.centerX, stopPoint.centerY);
  251. c.lineTo(maxPoint.centerX, maxPoint.centerY);
  252. c.lineTo(scaleX(maxTemp), maxPoint.centerY);
  253. c.stroke();
  254. c.lineTo(scaleX(maxTemp), height - bottomPadding);
  255. c.lineTo(leftPadding, height - bottomPadding);
  256. c.fill();
  257. fillGradient = c.createLinearGradient(0, 0, 0, height);
  258. fillGradient.addColorStop(0, "rgba(127, 127, 127, 0.6)");
  259. fillGradient.addColorStop(1, "rgba(127, 127, 127, 0.9)");
  260. c.fillStyle = fillGradient;
  261. c.fill();
  262. c.closePath();
  263. c.textAlign = "right";
  264. c.textBaseline = "middle";
  265. c.strokeStyle = palette.dark;
  266. c.fillStyle = palette.dark;
  267. c.lineWidth = 1;
  268. c.strokeRect(leftPadding-0.5, topPadding-0.5, plotWidth+0.5, plotHeight+1.5);
  269. for (var i=0; i<=100; i+=20) {
  270. var y = scaleY(i*2.55);
  271. c.fillText(i + '%', leftPadding - 2, y);
  272. if (i != 0 && i != 100) {
  273. for (var j=leftPadding; j<=width-rightPadding; j+=15) {
  274. c.moveTo(j, y);
  275. c.lineTo(Math.min(j+5, width-rightPadding), y);
  276. }
  277. c.stroke();
  278. }
  279. }
  280. c.textAlign = "center";
  281. c.textBaseline = "top";
  282. var convertedMinTemp = Units.fromCelsius(minTemp, unit);
  283. var convertedMaxTemp = Units.fromCelsius(maxTemp, unit);
  284. for (i=convertedMinTemp; i<convertedMaxTemp; i+= 10) {
  285. var x = scaleX(Units.toCelsius(i, unit));
  286. c.fillText(i + '°', x, topPadding+plotHeight+fontSize/2);
  287. if (i != convertedMinTemp) {
  288. for (var j=scaleY(255); j<=scaleY(0); j+=20) {
  289. c.moveTo(x, j);
  290. c.lineTo(x, Math.min(j+5, width-rightPadding));
  291. }
  292. c.stroke();
  293. }
  294. }
  295. c.fillText(convertedMaxTemp + '°', scaleX(maxTemp), topPadding+plotHeight+fontSize/2);
  296. }
  297. }
  298. ColumnLayout {
  299. property int padding: 10
  300. id: settingsArea
  301. anchors {
  302. left: parent.left
  303. leftMargin: padding
  304. right: parent.right
  305. rightMargin: padding
  306. bottom: parent.bottom
  307. bottomMargin: padding
  308. }
  309. visible: root.height >= header.height + height + 2*margin
  310. opacity: canvas.opacity
  311. clip: true
  312. spacing: 2
  313. RowLayout {
  314. CheckBox {
  315. id: hasTempCheckBox
  316. text: i18n("Controlled by:")
  317. checked: fan.hasTemp
  318. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  319. onCheckedChanged: fan.hasTemp = checked
  320. }
  321. Row {
  322. Layout.fillWidth: true
  323. ComboBox {
  324. property QtObject hwmon: loader.hwmons[currentIndex]
  325. id: hwmonBox
  326. width: (parent.width-slash.width) / 2
  327. anchors.verticalCenter: parent.verticalCenter
  328. model: ArrayFunctions.names(loader.hwmons)
  329. enabled: hasTempCheckBox.checked
  330. }
  331. Label {
  332. id: slash
  333. text: "/"
  334. anchors.verticalCenter: parent.verticalCenter
  335. verticalAlignment: Text.AlignVCenter
  336. enabled: hasTempCheckBox.checked
  337. renderType: Text.NativeRendering
  338. }
  339. ComboBox {
  340. id: tempBox
  341. width: (parent.width-slash.width) / 2
  342. anchors.verticalCenter: parent.verticalCenter
  343. model: ArrayFunctions.names(hwmonBox.hwmon.temps)
  344. enabled: hasTempCheckBox.checked
  345. onCurrentIndexChanged: {
  346. if (hasTempCheckBox.checked && hwmonBox.hwmon)
  347. fan.temp = hwmonBox.hwmon.temps[currentIndex];
  348. }
  349. onModelChanged: {
  350. if (hasTempCheckBox.checked && hwmonBox.hwmon)
  351. fan.temp = hwmonBox.hwmon.temps[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. Label {
  368. text: i18n("Pwm value for fan to start:")
  369. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  370. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  371. renderType: Text.NativeRendering
  372. }
  373. OptionInput {
  374. id: minStartInput
  375. Layout.fillWidth: true
  376. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  377. text: fan.minStart
  378. onTextChanged: fan.minStart = parseInt(text)
  379. }
  380. }
  381. RowLayout {
  382. visible: systemdCom
  383. Label {
  384. text: i18n("Test start and stop values")
  385. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  386. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  387. renderType: Text.NativeRendering
  388. }
  389. Item {
  390. Layout.fillWidth: true
  391. }
  392. Button {
  393. id: testButton
  394. property bool reactivateAfterTesting
  395. text: fan.testing? i18n("Abort") : i18n("Test")
  396. anchors.right: parent.right
  397. height: hwmonBox.height
  398. onClicked: {
  399. if (fan.testing) {
  400. fan.abortTesting();
  401. systemdCom.serviceActive = true;
  402. } else {
  403. reactivateAfterTesting = systemdCom.serviceActive;
  404. systemdCom.serviceActive = false;
  405. minStartInput.text = Qt.binding(function() { return fan.minStart });
  406. fan.test();
  407. }
  408. }
  409. Connections {
  410. target: fan
  411. onTestingChanged: if (!fan.testing) systemdCom.serviceActive = testButton.reactivateAfterTesting
  412. }
  413. }
  414. }
  415. }
  416. }