PwmFan.qml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/math.js" as MoreMath
  23. import "../scripts/units.js" as Units
  24. import "../scripts/colors.js" as Colors
  25. Rectangle {
  26. property QtObject fan
  27. property QtObject systemdCom
  28. property QtObject tempModel
  29. property real minTemp: 40.0
  30. property real maxTemp: 90.0
  31. property int margin: 5
  32. property int unit: 0
  33. property real convertedMinTemp: Units.fromCelsius(minTemp, unit)
  34. property real convertedMaxTemp: Units.fromCelsius(maxTemp, unit)
  35. id: root
  36. color: "transparent"
  37. border.color: palette.windowText
  38. border.width: 2
  39. radius: 10
  40. clip: false
  41. function update() {
  42. if (fan) {
  43. hasTempCheckBox.checked = Qt.binding(function() { return fan.hasTemp; })
  44. fanOffCheckBox.checked = Qt.binding(function() { return (fan.minPwm == 0); })
  45. }
  46. bgCanvas.requestPaint();
  47. }
  48. onFanChanged: update();
  49. onMinTempChanged: if (fan) bgCanvas.requestPaint()
  50. onMaxTempChanged: if (fan) bgCanvas.requestPaint()
  51. onUnitChanged: if (fan) bgCanvas.requestRepaint()
  52. SystemPalette {
  53. id: palette
  54. colorGroup: SystemPalette.Active
  55. }
  56. SystemPalette {
  57. id: disabledPalette
  58. colorGroup: SystemPalette.Disabled
  59. }
  60. TextEdit {
  61. id: nameField
  62. anchors {
  63. left: parent.left
  64. leftMargin: margin
  65. right: parent.right
  66. rightMargin: margin
  67. top: parent.top
  68. topMargin: margin
  69. }
  70. visible: root.height >= height + margin*2
  71. text: fan.name
  72. onTextChanged: fan.name = text
  73. color: palette.text
  74. horizontalAlignment: TextEdit.AlignLeft
  75. wrapMode: TextEdit.Wrap
  76. font.bold: true
  77. font.pointSize: 14
  78. selectByMouse: true
  79. MouseArea {
  80. anchors.fill: parent
  81. cursorShape: Qt.IBeamCursor
  82. acceptedButtons: Qt.NoButton
  83. }
  84. }
  85. Item {
  86. id: graph
  87. property int fontSize: MoreMath.bound(8, height / 20 + 1, 16)
  88. property QtObject pal: fan.hasTemp ? palette : disabledPalette
  89. property string suffix: (root.unit == 0) ? "°C" : (root.unit == 1) ? "K" : "°F"
  90. property int verticalScalaCount: 6
  91. property var horIntervals: MoreMath.intervals(root.convertedMinTemp, root.convertedMaxTemp, 10)
  92. anchors {
  93. left: parent.left
  94. right: parent.right
  95. top: nameField.bottom
  96. bottom: settingsArea.top
  97. }
  98. visible: background.height > 0 && background.width > 0
  99. Item {
  100. id: verticalScala
  101. anchors {
  102. top: background.top
  103. bottom: background.bottom
  104. left: parent.left
  105. }
  106. width: graph.fontSize * 3
  107. Repeater {
  108. model: graph.verticalScalaCount
  109. Label {
  110. x: 0
  111. width: verticalScala.width - graph.fontSize / 3
  112. y: background.scaleY(255 / (graph.verticalScalaCount - 1) * index) - graph.fontSize / 2
  113. horizontalAlignment: Text.AlignRight
  114. color: graph.pal.text
  115. text: index * (100 / (graph.verticalScalaCount - 1)) + "%"
  116. font.pixelSize: graph.fontSize
  117. }
  118. }
  119. }
  120. Item {
  121. id: horizontalScala
  122. anchors {
  123. right: background.right
  124. bottom: parent.bottom
  125. left: background.left
  126. }
  127. height: graph.fontSize * 2
  128. Repeater {
  129. model: graph.horIntervals.length;
  130. Label {
  131. x: Math.min(horizontalScala.width, background.scaleX(Units.toCelsius(graph.horIntervals[index]), root.unit)) - width / 2
  132. y: graph.fontSize / 2
  133. color: graph.pal.text
  134. text: graph.horIntervals[index] + graph.suffix
  135. font.pixelSize: graph.fontSize
  136. }
  137. }
  138. }
  139. Rectangle {
  140. id: background
  141. property alias pal: graph.pal
  142. color: pal.light
  143. border.color: pal.text
  144. border.width: 2
  145. radius: 1
  146. anchors {
  147. top: parent.top
  148. left: verticalScala.right
  149. bottom: horizontalScala.top
  150. right: parent.right
  151. topMargin: parent.fontSize
  152. bottomMargin: 0
  153. rightMargin: parent.fontSize * 2
  154. leftMargin: 0
  155. }
  156. function scaleX(temp) {
  157. return (temp - minTemp) * width / (maxTemp - minTemp);
  158. }
  159. function scaleY(pwm) {
  160. return height - pwm * height / 255;
  161. }
  162. function scaleTemp(x) {
  163. return x / width * (maxTemp - minTemp) + minTemp;
  164. }
  165. function scalePwm(y) {
  166. return 255 - y / height * 255;
  167. }
  168. Canvas {
  169. id: bgCanvas
  170. anchors.fill: parent
  171. anchors.margins: parent.border.width
  172. renderStrategy: Canvas.Cooperative
  173. property alias pal: background.pal
  174. onPaint: {
  175. var c = bgCanvas.getContext("2d");
  176. c.clearRect(0, 0, width, height);
  177. var gradient = c.createLinearGradient(0, 0, width, 0);
  178. gradient.addColorStop(0, "rgb(0, 0, 255)");
  179. gradient.addColorStop(1, "rgb(255, 0, 0)");
  180. c.fillStyle = gradient;
  181. c.lineWidth = 2;
  182. c.strokeStyle = gradient;
  183. c.lineJoin = "round";
  184. c.beginPath();
  185. if (fanOffCheckBox.checked) {
  186. c.moveTo(stopPoint.centerX, height);
  187. } else {
  188. c.moveTo(0, stopPoint.centerY);
  189. }
  190. c.lineTo(stopPoint.centerX, stopPoint.centerY);
  191. c.lineTo(maxPoint.centerX, maxPoint.centerY);
  192. c.lineTo(width, maxPoint.centerY);
  193. c.stroke();
  194. c.lineTo(width, height);
  195. if (fanOffCheckBox.checked) {
  196. c.lineTo(stopPoint.centerX, height);
  197. } else {
  198. c.lineTo(0, height);
  199. }
  200. c.fill();
  201. //blend background
  202. gradient = c.createLinearGradient(0, 0, 0, height);
  203. gradient.addColorStop(0, Colors.setAlpha(background.color, 0.5));
  204. gradient.addColorStop(1, Colors.setAlpha(background.color, 0.9));
  205. c.fillStyle = gradient;
  206. c.fill();
  207. //draw mesh
  208. c.strokeStyle = Colors.setAlpha(pal.text, 0.3);
  209. for (var i=0; i<=100; i+=20) {
  210. var y = background.scaleY(i*2.55);
  211. if (i != 0 && i != 100) {
  212. for (var j=0; j<=width; j+=15) {
  213. c.moveTo(j, y);
  214. c.lineTo(Math.min(j+5, width), y);
  215. }
  216. }
  217. }
  218. if (graph.horIntervals.length > 1) {
  219. for (var i=1; i<graph.horIntervals.length; i++) {
  220. var x = background.scaleX(Units.toCelsius(graph.horIntervals[i], unit));
  221. for (var j=0; j<=height; j+=20) {
  222. c.moveTo(x, j);
  223. c.lineTo(x, Math.min(j+5, width));
  224. }
  225. }
  226. }
  227. c.stroke();
  228. }
  229. }
  230. StatusPoint {
  231. id: currentPwm
  232. size: graph.fontSize
  233. visible: background.contains(center) && fan.hasTemp
  234. fan: root.fan
  235. unit: root.unit
  236. }
  237. PwmPoint {
  238. id: stopPoint
  239. color: fan.hasTemp ? "blue" : Qt.tint(graph.pal.light, Qt.rgba(0, 0, 1, 0.5))
  240. size: graph.fontSize
  241. unit: root.unit
  242. enabled: fan.hasTemp
  243. drag.maximumX: Math.min(background.scaleX(background.scaleTemp(maxPoint.x)-1), maxPoint.x-1)
  244. drag.minimumY: Math.max(background.scaleY(background.scalePwm(maxPoint.y)-1), maxPoint.y+1)
  245. x: fan.hasTemp ? background.scaleX(MoreMath.bound(minTemp, fan.minTemp, maxTemp)) - width/2 : -width/2
  246. y: fan.hasTemp ? background.scaleY(fan.minStop) - height/2 : -height/2
  247. drag.onActiveChanged: {
  248. if (!drag.active) {
  249. fan.minStop = Math.round(background.scalePwm(centerY));
  250. fan.minTemp = Math.round(background.scaleTemp(centerX));
  251. if (!fanOffCheckBox.checked) fan.minPwm = fan.minStop;
  252. }
  253. }
  254. onPositionChanged: bgCanvas.requestPaint()
  255. }
  256. PwmPoint {
  257. id: maxPoint
  258. color: fan.hasTemp ? "red" : Qt.tint(graph.pal.light, Qt.rgba(1, 0, 0, 0.5))
  259. size: graph.fontSize
  260. unit: root.unit
  261. enabled: fan.hasTemp
  262. drag.minimumX: Math.max(background.scaleX(background.scaleTemp(stopPoint.x)+1), stopPoint.x+1)
  263. drag.maximumY: Math.min(background.scaleY(background.scalePwm(stopPoint.y)+1), stopPoint.y-1)
  264. x: fan.hasTemp ? background.scaleX(MoreMath.bound(minTemp, fan.maxTemp, maxTemp)) - width/2 : background.width - width/2
  265. y: fan.hasTemp ? background.scaleY(fan.maxPwm) - height/2 : -height/2
  266. drag.onActiveChanged: {
  267. if (!drag.active) {
  268. fan.maxPwm = Math.round(background.scalePwm(centerY));
  269. fan.maxTemp = Math.round(background.scaleTemp(centerX));
  270. }
  271. }
  272. onPositionChanged: bgCanvas.requestPaint()
  273. }
  274. }
  275. }
  276. ColumnLayout {
  277. property int padding: 10
  278. id: settingsArea
  279. anchors {
  280. left: parent.left
  281. leftMargin: padding
  282. right: parent.right
  283. rightMargin: padding
  284. bottom: parent.bottom
  285. bottomMargin: padding
  286. }
  287. visible: root.height >= nameField.height + height + 2*margin
  288. clip: true
  289. spacing: 2
  290. RowLayout {
  291. CheckBox {
  292. id: hasTempCheckBox
  293. text: i18n("Controlled by:")
  294. checked: fan.hasTemp
  295. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  296. onCheckedChanged: {
  297. fan.hasTemp = checked;
  298. if (checked) {
  299. fan.temp = tempModel.temps[tempBox.currentIndex];
  300. }
  301. bgCanvas.requestPaint();
  302. }
  303. }
  304. RowLayout {
  305. ComboBox {
  306. id: tempBox
  307. Layout.fillWidth: true
  308. model: tempModel
  309. textRole: "display"
  310. enabled: hasTempCheckBox.checked
  311. onCurrentIndexChanged: {
  312. if (hasTempCheckBox.checked)
  313. fan.temp = tempModel.temps[currentIndex];
  314. }
  315. }
  316. }
  317. }
  318. CheckBox {
  319. id: fanOffCheckBox
  320. text: i18n("Turn Fan off if temp < MINTEMP")
  321. enabled: hasTempCheckBox.checked
  322. checked: fan.minPwm == 0
  323. onCheckedChanged: {
  324. fan.minPwm = checked ? 0 : fan.minStop;
  325. bgCanvas.requestPaint();
  326. }
  327. }
  328. RowLayout {
  329. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  330. Label {
  331. text: i18n("Pwm value for fan to start:")
  332. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  333. renderType: Text.NativeRendering
  334. }
  335. SpinBox {
  336. id: minStartInput
  337. Layout.fillWidth: true
  338. minimumValue: 1
  339. maximumValue: 100
  340. decimals: 1
  341. value: Math.round(fan.minStart / 2.55)
  342. suffix: i18n("%")
  343. onValueChanged: fan.minStart = Math.round(value * 2.55)
  344. }
  345. }
  346. RowLayout {
  347. visible: systemdCom
  348. Item {
  349. Layout.fillWidth: true
  350. }
  351. Button {
  352. property bool reactivateAfterTesting
  353. id: testButton
  354. text: fan.testing ? i18n("Abort test") : i18n("Test start and stop values")
  355. iconName: "dialog-password"
  356. anchors.right: parent.right
  357. onClicked: {
  358. if (fan.testing) {
  359. systemdCom.serviceActive = reactivateAfterTesting;
  360. fan.abortTest();
  361. } else {
  362. reactivateAfterTesting = systemdCom.serviceActive;
  363. systemdCom.serviceActive = false;
  364. minStartInput.value = Qt.binding(function() { return Math.round(fan.minStart / 2.55) });
  365. fan.test();
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }