PwmFan.qml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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.3
  21. import QtQuick.Controls.Styles 1.2
  22. import QtQuick.Layouts 1.1
  23. import QtQuick.Window 2.2
  24. import "../javascript/arrayfunctions.js" as ArrayFunctions
  25. import "../javascript/math.js" as MoreMath
  26. import "../javascript/units.js" as Units
  27. import "../javascript/coordinates.js" as Coordinates
  28. Rectangle {
  29. property var fan
  30. property url config: loader.configUrl
  31. property real minTemp: 30.0
  32. property real maxTemp: 90.0
  33. property int margin: 5
  34. property int minimizeDuration: 400
  35. property real hwRatio
  36. property string unit: "Celsius"
  37. id: root
  38. height: width * hwRatio
  39. color: "transparent"
  40. border.color: "black"
  41. border.width: 2
  42. radius: 10
  43. clip: false
  44. function update() {
  45. hasTempCheckBox.checked = fan.hasTemp;
  46. fanOffCheckBox.checked = (fan.minPwm == 0);
  47. minStartInput.text = fan.minStart;
  48. hwmonBox.currentIndex = fan.temp ? fan.temp.parent.index : 0;
  49. tempBox.currentIndex = fan.temp ? fan.temp.index - 1 : 0;
  50. canvas.requestPaint();
  51. }
  52. onFanChanged: update();
  53. onConfigChanged: update();
  54. states: [
  55. State {
  56. name: "minimized"
  57. PropertyChanges {
  58. target: root
  59. height: header.height + 2*margin
  60. }
  61. },
  62. State {
  63. name: "maximized"
  64. // PropertyChanges {
  65. // target: root
  66. // height:
  67. // }
  68. }
  69. ]
  70. transitions: Transition {
  71. NumberAnimation {
  72. target: root
  73. property: "height"
  74. easing.amplitude: 1.5
  75. easing.type: Easing.InOutQuad
  76. duration: minimizeDuration
  77. }
  78. }
  79. SystemPalette {
  80. id: palette
  81. }
  82. SystemPalette {
  83. id: disabledPalette
  84. colorGroup: SystemPalette.Disabled
  85. }
  86. RowLayout {
  87. id: header
  88. anchors {
  89. left: parent.left
  90. leftMargin: margin
  91. right: parent.right
  92. rightMargin: margin
  93. top: parent.top
  94. topMargin: margin
  95. }
  96. z: -1
  97. clip: true
  98. spacing: margin
  99. TextEdit {
  100. id: nameField
  101. Layout.alignment: Qt.AlignTop
  102. text: fan.name
  103. onTextChanged: fan.name = text;
  104. horizontalAlignment: TextEdit.AlignLeft
  105. wrapMode: TextEdit.Wrap
  106. selectByMouse: true
  107. Layout.fillWidth: true
  108. MouseArea {
  109. anchors.fill: parent
  110. cursorShape: Qt.IBeamCursor
  111. acceptedButtons: Qt.NoButton
  112. }
  113. }
  114. Rectangle {
  115. id: collapseButton
  116. height: 16
  117. width: 16
  118. Layout.alignment: Qt.AlignTop
  119. color: collapseMouseArea.containsMouse ? "red" : "transparent"
  120. radius: width / 2
  121. Text {
  122. anchors.fill: parent
  123. text: root.state == "minimized" ? "-" : "X"
  124. color: collapseMouseArea.containsMouse ? "black" : "red"
  125. horizontalAlignment: Text.AlignHCenter
  126. verticalAlignment: Text.AlignVCenter
  127. }
  128. MouseArea {
  129. id: collapseMouseArea
  130. anchors.fill: parent
  131. hoverEnabled: true
  132. acceptedButtons: Qt.LeftButton
  133. onClicked: root.state = (root.state != "minimized") ? "minimized" : ""
  134. }
  135. }
  136. }
  137. Canvas {
  138. property int leftPadding: 40 * Screen.devicePixelRatio
  139. property int rightPadding: 20 * Screen.devicePixelRatio
  140. property int topPadding: 10 * Screen.devicePixelRatio
  141. property int bottomPadding: 20 * Screen.devicePixelRatio
  142. property int plotWidth: width - leftPadding - rightPadding
  143. property int plotHeight: height - topPadding - bottomPadding
  144. property alias minTemp: root.minTemp
  145. property alias maxTemp: root.maxTemp
  146. id: canvas
  147. renderTarget: Canvas.FramebufferObject
  148. anchors {
  149. left: parent.left
  150. right: parent.right
  151. top: header.bottom
  152. bottom: settingsArea.top
  153. }
  154. opacity: state == "minimized" ? 0 : 1
  155. Behavior on opacity {
  156. NumberAnimation { duration: minimizeDuration / 2 }
  157. }
  158. Rectangle {
  159. id: currentPwm
  160. x: parent.scaleX(fan.temp ? fan.temp.value : minTemp) - width/2
  161. y: parent.scaleY(fan.pwm) - height/2
  162. width: 7
  163. height: width
  164. radius: width / 2
  165. color: "black"
  166. visible: parent.contains(Coordinates.centerOf(this))
  167. }
  168. PwmPoint {
  169. id: stopPoint
  170. color: "blue"
  171. drag.maximumX: Math.min(canvas.scaleX(canvas.scaleTemp(maxPoint.x)-1), maxPoint.x-1)
  172. drag.minimumY: Math.max(canvas.scaleY(canvas.scalePwm(maxPoint.y)-1), maxPoint.y+1)
  173. x: canvas.scaleX(MoreMath.bound(minTemp, fan.minTemp, maxTemp)) - width/2
  174. y: canvas.scaleY(fan.minStop) - height/2
  175. visible: parent.contains(Coordinates.centerOf(this))
  176. drag.onActiveChanged: {
  177. if (!drag.active) {
  178. fan.minStop = canvas.scalePwm(centerY);
  179. fan.minTemp = canvas.scaleTemp(centerX);
  180. if (!fanOffCheckBox.checked) fan.minPwm = fan.minStop;
  181. }
  182. }
  183. }
  184. PwmPoint {
  185. id: maxPoint
  186. color: "red"
  187. drag.minimumX: stopPoint.x
  188. drag.maximumY: stopPoint.y
  189. x: canvas.scaleX(MoreMath.bound(minTemp, fan.maxTemp, maxTemp)) - width/2
  190. y: canvas.scaleY(fan.maxPwm) - height/2
  191. visible: parent.contains(Coordinates.centerOf(this))
  192. drag.onActiveChanged: {
  193. if (!drag.active) {
  194. fan.maxPwm = canvas.scalePwm(centerY);
  195. fan.maxTemp = canvas.scaleTemp(centerX);
  196. }
  197. }
  198. }
  199. function scaleX(temp) {
  200. var scaledX = (temp - minTemp) * plotWidth / (maxTemp - minTemp);
  201. return leftPadding + scaledX;
  202. }
  203. function scaleY(pwm) {
  204. var scaledY = pwm * plotHeight / 255;
  205. return height - bottomPadding - scaledY;
  206. }
  207. function scaleTemp(x) {
  208. var scaledTemp = (x - leftPadding) / plotWidth * (maxTemp - minTemp);
  209. return minTemp + scaledTemp;
  210. }
  211. function scalePwm(y) {
  212. var scaledPwm = (y - topPadding) / plotHeight * 255;
  213. return 255 - scaledPwm;
  214. }
  215. onPaint: {
  216. var c = canvas.getContext("2d");
  217. c.clearRect(0, 0, width, height);
  218. c.fillStyle = palette.base;
  219. c.fillRect(leftPadding, topPadding, plotWidth, plotHeight);
  220. var fillGradient = c.createLinearGradient(0, 0, width, 0);
  221. fillGradient.addColorStop(0, "rgb(0, 0, 255)");
  222. fillGradient.addColorStop(1, "rgb(255, 0, 0)");
  223. var strokeGradient = c.createLinearGradient(0, 0, width, 0);
  224. strokeGradient.addColorStop(0, "rgb(0, 0, 255)");
  225. strokeGradient.addColorStop(1, "rgb(255, 0, 0)");
  226. c.fillStyle = fillGradient;
  227. c.strokeStyle = strokeGradient;
  228. c.lineWidth = 2;
  229. c.lineJoin = "round";
  230. c.beginPath();
  231. if (fanOffCheckBox.checked) {
  232. c.moveTo(scaleX(minTemp), scaleY(0));
  233. c.lineTo(stopPoint.centerX, scaleY(0));
  234. } else {
  235. c.moveTo(scaleX(minTemp), stopPoint.centerY);
  236. }
  237. c.lineTo(stopPoint.centerX, stopPoint.centerY);
  238. c.lineTo(maxPoint.centerX, maxPoint.centerY);
  239. c.lineTo(scaleX(maxTemp), maxPoint.centerY);
  240. c.stroke();
  241. c.lineTo(scaleX(maxTemp), height - bottomPadding);
  242. c.lineTo(leftPadding, height - bottomPadding);
  243. c.fill();
  244. fillGradient = c.createLinearGradient(0, 0, 0, height);
  245. fillGradient.addColorStop(0, "rgba(127, 127, 127, 0.6)");
  246. fillGradient.addColorStop(1, "rgba(127, 127, 127, 0.9)");
  247. c.fillStyle = fillGradient;
  248. c.fill();
  249. c.closePath();
  250. c.textAlign = "right";
  251. c.textBaseline = "middle";
  252. c.strokeStyle = palette.dark;
  253. c.fillStyle = palette.dark;
  254. c.lineWidth = 1;
  255. c.strokeRect(leftPadding-0.5, topPadding-0.5, plotWidth+0.5, plotHeight+1.5);
  256. for (var i=0; i<=100; i+=20) {
  257. var y = scaleY(i*2.55);
  258. c.fillText(i + '%', leftPadding - 2, y);
  259. if (i != 0 && i != 100) {
  260. for (var j=leftPadding; j<=width-rightPadding; j+=15) {
  261. c.moveTo(j, y);
  262. c.lineTo(Math.min(j+5, width-rightPadding), y);
  263. }
  264. c.stroke();
  265. }
  266. }
  267. c.textAlign = "center";
  268. c.textBaseline = "top";
  269. var convertedMinTemp = Units.fromCelsius(minTemp, unit);
  270. var convertedMaxTemp = Units.fromCelsius(maxTemp, unit);
  271. for (i=convertedMinTemp; i<convertedMaxTemp; i+= 10) {
  272. var x = scaleX(Units.toCelsius(i, unit));
  273. c.fillText(i + '°', x, height-15);
  274. if (i != convertedMinTemp) {
  275. for (var j=scaleY(255); j<=scaleY(0); j+=20) {
  276. c.moveTo(x, j);
  277. c.lineTo(x, Math.min(j+5, width-rightPadding));
  278. }
  279. c.stroke();
  280. }
  281. }
  282. c.fillText(convertedMaxTemp + '°', scaleX(maxTemp), height-15);
  283. }
  284. }
  285. ColumnLayout {
  286. property int padding: 10
  287. id: settingsArea
  288. anchors {
  289. left: parent.left
  290. leftMargin: padding
  291. right: parent.right
  292. rightMargin: padding
  293. bottom: parent.bottom
  294. bottomMargin: padding
  295. }
  296. visible: root.height >= header.height + height + 2*margin
  297. opacity: canvas.opacity
  298. clip: true
  299. spacing: 0
  300. RowLayout {
  301. spacing: 0
  302. height: hwmonBox.height
  303. anchors.left: parent.left
  304. anchors.right: parent.right
  305. CheckBox {
  306. id: hasTempCheckBox
  307. text: "Controlled by:"
  308. checked: fan.hasTemp
  309. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  310. onCheckedChanged: fan.hasTemp = checked
  311. }
  312. Item {
  313. Layout.fillWidth: true
  314. }
  315. ComboBox {
  316. property var hwmon: loader.hwmons[currentIndex]
  317. id: hwmonBox
  318. Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
  319. model: ArrayFunctions.names(loader.hwmons)
  320. enabled: hasTempCheckBox.checked
  321. }
  322. Text {
  323. text: "/"
  324. color: enabled ? palette.text : disabledPalette.text
  325. Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
  326. verticalAlignment: Text.AlignVCenter
  327. enabled: hasTempCheckBox.checked
  328. renderType: Text.NativeRendering
  329. }
  330. ComboBox {
  331. id: tempBox
  332. Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
  333. model: ArrayFunctions.labels(hwmonBox.hwmon.temps)
  334. enabled: hasTempCheckBox.checked
  335. onCurrentIndexChanged: {
  336. if (hasTempCheckBox.checked)
  337. fan.temp = hwmonBox.hwmon.temps[currentIndex];
  338. }
  339. onModelChanged: {
  340. if (hasTempCheckBox.checked)
  341. fan.temp = hwmonBox.hwmon.temps[currentIndex];
  342. }
  343. }
  344. }
  345. CheckBox {
  346. id: fanOffCheckBox
  347. text: "Turn Fan off if temp < MINTEMP"
  348. enabled: hasTempCheckBox.checked
  349. checked: fan.minPwm == 0
  350. onCheckedChanged: {
  351. fan.minPwm = checked ? 0 : fan.minStop;
  352. canvas.requestPaint();
  353. }
  354. }
  355. RowLayout {
  356. anchors.left: parent.left
  357. anchors.right: parent.right
  358. Text {
  359. text: "Pwm value for fan to start:"
  360. Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
  361. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  362. color: enabled ? palette.text : disabledPalette.text
  363. renderType: Text.NativeRendering
  364. }
  365. NumberOption {
  366. id: minStartInput
  367. anchors.right: parent.right
  368. width: 50
  369. enabled: fanOffCheckBox.checked && fanOffCheckBox.enabled
  370. text: fan.minStart
  371. onTextChanged: fan.minStart = text
  372. }
  373. }
  374. }
  375. }