PwmFan.qml 15 KB

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