PwmFan.qml 15 KB

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