PwmFan.qml 17 KB

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