PwmFan.qml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 && loader) {
  51. tempBox.currentIndex = loader.allTemps.indexOf(fan.temp);
  52. }
  53. }
  54. canvas.requestPaint();
  55. }
  56. onFanChanged: update()
  57. onLoaderChanged: update()
  58. onUnitChanged: if (fan) canvas.requestPaint()
  59. onMinTempChanged: if (fan) canvas.requestPaint()
  60. onMaxTempChanged: if (fan) canvas.requestPaint()
  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. font.bold: true
  114. font.pointSize: 14
  115. selectByMouse: true
  116. Layout.fillWidth: true
  117. MouseArea {
  118. anchors.fill: parent
  119. cursorShape: Qt.IBeamCursor
  120. acceptedButtons: Qt.NoButton
  121. }
  122. }
  123. Rectangle {
  124. id: collapseButton
  125. height: 16
  126. width: 16
  127. Layout.alignment: Qt.AlignTop
  128. color: collapseMouseArea.containsMouse ? "red" : "transparent"
  129. radius: width / 2
  130. visible: minimizable
  131. enabled: minimizable
  132. Label {
  133. anchors.fill: parent
  134. text: root.state == "minimized" ? "-" : "X"
  135. color: collapseMouseArea.containsMouse ? "black" : "red"
  136. horizontalAlignment: Text.AlignHCenter
  137. verticalAlignment: Text.AlignVCenter
  138. }
  139. MouseArea {
  140. id: collapseMouseArea
  141. anchors.fill: parent
  142. hoverEnabled: true
  143. acceptedButtons: Qt.LeftButton
  144. onClicked: fan.active = fan.active ? false : true
  145. }
  146. }
  147. }
  148. Canvas {
  149. property int fontSize: Math.max(9, Math.min(height / 20, 20))
  150. property int leftPadding: fontSize * 4
  151. property int rightPadding: fontSize * 2
  152. property int topPadding: fontSize
  153. property int bottomPadding: fontSize * 2
  154. property int plotWidth: width - leftPadding - rightPadding
  155. property int plotHeight: height - topPadding - bottomPadding
  156. property alias minTemp: root.minTemp
  157. property alias maxTemp: root.maxTemp
  158. id: canvas
  159. renderTarget: Canvas.FramebufferObject
  160. anchors {
  161. left: parent.left
  162. right: parent.right
  163. top: header.bottom
  164. bottom: settingsArea.top
  165. }
  166. opacity: state == "minimized" ? 0 : 1
  167. Behavior on opacity {
  168. NumberAnimation { duration: minimizeDuration / 2 }
  169. }
  170. Rectangle {
  171. id: currentPwm
  172. x: parent.scaleX(fan.temp ? fan.temp.value : minTemp) - width/2
  173. y: parent.scaleY(fan.pwm) - height/2
  174. width: canvas.fontSize
  175. height: width
  176. radius: width / 2
  177. color: "black"
  178. visible: parent.contains(Coordinates.centerOf(this))
  179. }
  180. PwmPoint {
  181. id: stopPoint
  182. color: "blue"
  183. size: canvas.fontSize
  184. drag.maximumX: Math.min(canvas.scaleX(canvas.scaleTemp(maxPoint.x)-1), maxPoint.x-1)
  185. drag.minimumY: Math.max(canvas.scaleY(canvas.scalePwm(maxPoint.y)-1), maxPoint.y+1)
  186. x: canvas.scaleX(MoreMath.bound(minTemp, fan.minTemp, maxTemp)) - width/2
  187. y: canvas.scaleY(fan.minStop) - height/2
  188. visible: parent.contains(Coordinates.centerOf(this)) && parent.height > 0
  189. drag.onActiveChanged: {
  190. if (!drag.active) {
  191. fan.minStop = canvas.scalePwm(centerY);
  192. fan.minTemp = canvas.scaleTemp(centerX);
  193. if (!fanOffCheckBox.checked) fan.minPwm = fan.minStop;
  194. }
  195. }
  196. }
  197. PwmPoint {
  198. id: maxPoint
  199. color: "red"
  200. size: canvas.fontSize
  201. drag.minimumX: stopPoint.x
  202. drag.maximumY: stopPoint.y
  203. x: canvas.scaleX(MoreMath.bound(minTemp, fan.maxTemp, maxTemp)) - width/2
  204. y: canvas.scaleY(fan.maxPwm) - height/2
  205. visible: parent.contains(Coordinates.centerOf(this)) && parent.height > 0
  206. drag.onActiveChanged: {
  207. if (!drag.active) {
  208. fan.maxPwm = canvas.scalePwm(centerY);
  209. fan.maxTemp = canvas.scaleTemp(centerX);
  210. }
  211. }
  212. }
  213. function scaleX(temp) {
  214. var scaledX = (temp - minTemp) * plotWidth / (maxTemp - minTemp);
  215. return leftPadding + scaledX;
  216. }
  217. function scaleY(pwm) {
  218. var scaledY = pwm * plotHeight / 255;
  219. return height - bottomPadding - scaledY;
  220. }
  221. function scaleTemp(x) {
  222. var scaledTemp = (x - leftPadding) / plotWidth * (maxTemp - minTemp);
  223. return minTemp + scaledTemp;
  224. }
  225. function scalePwm(y) {
  226. var scaledPwm = (y - topPadding) / plotHeight * 255;
  227. return 255 - scaledPwm;
  228. }
  229. onPaint: {
  230. var c = canvas.getContext("2d");
  231. c.font = canvas.fontSize + "px sans-serif";
  232. c.clearRect(0, 0, width, height);
  233. c.fillStyle = palette.base;
  234. c.fillRect(leftPadding, topPadding, plotWidth, plotHeight);
  235. var fillGradient = c.createLinearGradient(0, 0, width, 0);
  236. fillGradient.addColorStop(0, "rgb(0, 0, 255)");
  237. fillGradient.addColorStop(1, "rgb(255, 0, 0)");
  238. var strokeGradient = c.createLinearGradient(0, 0, width, 0);
  239. strokeGradient.addColorStop(0, "rgb(0, 0, 255)");
  240. strokeGradient.addColorStop(1, "rgb(255, 0, 0)");
  241. c.fillStyle = fillGradient;
  242. c.strokeStyle = strokeGradient;
  243. c.lineWidth = 2;
  244. c.lineJoin = "round";
  245. c.beginPath();
  246. if (fanOffCheckBox.checked) {
  247. c.moveTo(scaleX(minTemp), scaleY(0));
  248. c.lineTo(stopPoint.centerX, scaleY(0));
  249. } else {
  250. c.moveTo(scaleX(minTemp), stopPoint.centerY);
  251. }
  252. c.lineTo(stopPoint.centerX, stopPoint.centerY);
  253. c.lineTo(maxPoint.centerX, maxPoint.centerY);
  254. c.lineTo(scaleX(maxTemp), maxPoint.centerY);
  255. c.stroke();
  256. c.lineTo(scaleX(maxTemp), height - bottomPadding);
  257. c.lineTo(leftPadding, height - bottomPadding);
  258. c.fill();
  259. fillGradient = c.createLinearGradient(0, 0, 0, height);
  260. fillGradient.addColorStop(0, "rgba(127, 127, 127, 0.6)");
  261. fillGradient.addColorStop(1, "rgba(127, 127, 127, 0.9)");
  262. c.fillStyle = fillGradient;
  263. c.fill();
  264. c.closePath();
  265. c.textAlign = "right";
  266. c.textBaseline = "middle";
  267. c.strokeStyle = palette.dark;
  268. c.fillStyle = palette.dark;
  269. c.lineWidth = 1;
  270. c.strokeRect(leftPadding-0.5, topPadding-0.5, plotWidth+0.5, plotHeight+1.5);
  271. for (var i=0; i<=100; i+=20) {
  272. var y = scaleY(i*2.55);
  273. c.fillText(i + '%', leftPadding - 2, y);
  274. if (i != 0 && i != 100) {
  275. for (var j=leftPadding; j<=width-rightPadding; j+=15) {
  276. c.moveTo(j, y);
  277. c.lineTo(Math.min(j+5, width-rightPadding), y);
  278. }
  279. c.stroke();
  280. }
  281. }
  282. c.textAlign = "center";
  283. c.textBaseline = "top";
  284. var convertedMinTemp = Units.fromCelsius(minTemp, unit);
  285. var convertedMaxTemp = Units.fromCelsius(maxTemp, unit);
  286. for (i=convertedMinTemp; i<convertedMaxTemp; i+= 10) {
  287. var x = scaleX(Units.toCelsius(i, unit));
  288. c.fillText(i + '°', x, topPadding+plotHeight+fontSize/2);
  289. if (i != convertedMinTemp) {
  290. for (var j=scaleY(255); j<=scaleY(0); j+=20) {
  291. c.moveTo(x, j);
  292. c.lineTo(x, Math.min(j+5, width-rightPadding));
  293. }
  294. c.stroke();
  295. }
  296. }
  297. c.fillText(convertedMaxTemp + '°', scaleX(maxTemp), topPadding+plotHeight+fontSize/2);
  298. }
  299. }
  300. ColumnLayout {
  301. property int padding: 10
  302. id: settingsArea
  303. anchors {
  304. left: parent.left
  305. leftMargin: padding
  306. right: parent.right
  307. rightMargin: padding
  308. bottom: parent.bottom
  309. bottomMargin: padding
  310. }
  311. visible: root.height >= header.height + height + 2*margin
  312. opacity: canvas.opacity
  313. clip: true
  314. spacing: 2
  315. RowLayout {
  316. CheckBox {
  317. id: hasTempCheckBox
  318. text: i18n("Controlled by:")
  319. checked: fan.hasTemp
  320. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  321. onCheckedChanged: fan.hasTemp = checked
  322. }
  323. RowLayout {
  324. ComboBox {
  325. id: tempBox
  326. Layout.fillWidth: true
  327. model: ArrayFunctions.namesWithPaths(loader.allTemps)
  328. enabled: hasTempCheckBox.checked
  329. onCurrentIndexChanged: {
  330. if (hasTempCheckBox.checked)
  331. fan.temp = loader.allTemps[currentIndex];
  332. }
  333. }
  334. }
  335. }
  336. CheckBox {
  337. id: fanOffCheckBox
  338. text: i18n("Turn Fan off if temp < MINTEMP")
  339. enabled: hasTempCheckBox.checked
  340. checked: fan.minPwm == 0
  341. onCheckedChanged: {
  342. fan.minPwm = checked ? 0 : fan.minStop;
  343. canvas.requestPaint();
  344. }
  345. }
  346. RowLayout {
  347. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  348. Label {
  349. text: i18n("Pwm value for fan to start:")
  350. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  351. renderType: Text.NativeRendering
  352. }
  353. OptionInput {
  354. id: minStartInput
  355. Layout.fillWidth: true
  356. text: fan.minStart
  357. onTextChanged: fan.minStart = parseInt(text)
  358. }
  359. }
  360. RowLayout {
  361. visible: systemdCom
  362. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  363. Label {
  364. text: i18n("Test start and stop values")
  365. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  366. renderType: Text.NativeRendering
  367. }
  368. Item {
  369. Layout.fillWidth: true
  370. }
  371. Button {
  372. property bool reactivateAfterTesting
  373. id: testButton
  374. text: fan.testing? i18n("Abort") : i18n("Test")
  375. anchors.right: parent.right
  376. onClicked: {
  377. if (fan.testing) {
  378. fan.abortTest();
  379. systemdCom.serviceActive = true;
  380. } else {
  381. reactivateAfterTesting = systemdCom.serviceActive;
  382. systemdCom.serviceActive = false;
  383. minStartInput.text = Qt.binding(function() { return fan.minStart });
  384. fan.test();
  385. }
  386. }
  387. Connections {
  388. target: fan
  389. onTestingChanged: if (!fan.testing) systemdCom.serviceActive = testButton.reactivateAfterTesting
  390. }
  391. }
  392. }
  393. }
  394. }