PwmFan.qml 15 KB

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