diskUsage.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ########################################################################
  2. # Wiizard - A Wii games manager
  3. # Copyright (C) 2023 CYBERDEViL
  4. #
  5. # This file is part of Wiizard.
  6. #
  7. # Wiizard is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Wiizard is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. from PyQt5.QtWidgets import QWidget, QSizePolicy
  22. from PyQt5.QtCore import Qt, QRectF
  23. from PyQt5.QtGui import QBrush, QColor, QPainter
  24. class DiskUsageWidget(QWidget):
  25. def __init__(self, used=-1, capacity=-1, futureUsed=-1, free=-1, parent=None):
  26. QWidget.__init__(self, parent=parent)
  27. self.__used = used
  28. self.__capacity = capacity
  29. self.__futureUsed = futureUsed
  30. self.__free = free
  31. self.text = ""
  32. self.textRect = None
  33. self.fmt = "{} GB / {} GB ({} GB free)"
  34. # Automatic expand width of self
  35. sizePolicy = self.sizePolicy()
  36. sizePolicy.setHorizontalPolicy(QSizePolicy.MinimumExpanding)
  37. self.setSizePolicy(sizePolicy)
  38. self.setMinimumHeight(24)
  39. self.remakeText()
  40. def remakeText(self):
  41. gbCapacity = round(self.__capacity / 1024, 2)
  42. gbUsed = round(self.__used / 1024, 2)
  43. #gbFutureUsed = round(self.__futureUsed / 1024, 2)
  44. gbFree = round(self.__free / 1024, 2)
  45. self.text = self.fmt.format(gbUsed, gbCapacity, gbFree)
  46. fontMetrics = self.fontMetrics()
  47. self.textRect = fontMetrics.tightBoundingRect(self.text) # better but slower?
  48. def updateSizes(self, used, capacity, futureUsed, free):
  49. self.__used = used
  50. self.__capacity = capacity
  51. self.__futureUsed = futureUsed
  52. self.__free = free
  53. if self.__capacity != -1:
  54. self.remakeText()
  55. self.update()
  56. def paintEvent(self, event):
  57. painter = QPainter(self)
  58. rounding = (3, 3)
  59. # Draw background
  60. painter.setPen(Qt.green)
  61. brush = QBrush(QColor(Qt.green), Qt.SolidPattern)
  62. painter.setBrush(brush)
  63. rect = QRectF(0.0, 0.0, self.width() - 1, self.height() - 1);
  64. painter.drawRoundedRect(rect, *rounding)
  65. if self.__capacity == -1:
  66. return
  67. # Draw future used
  68. if self.__futureUsed:
  69. width = (self.width() - 1) * ((self.__futureUsed / (self.__capacity / 100)) / 100)
  70. painter.setPen(Qt.yellow)
  71. brush = QBrush(QColor(Qt.yellow), Qt.Dense5Pattern)
  72. painter.setBrush(brush)
  73. rect = QRectF(0.0, 0.0, width, self.height() - 1);
  74. painter.drawRoundedRect(rect, *rounding)
  75. # Draw used
  76. if self.__used:
  77. width = (self.width() - 1) * ((self.__used / (self.__capacity / 100)) / 100)
  78. painter.setPen(Qt.red)
  79. brush = QBrush(QColor(Qt.red), Qt.Dense2Pattern)
  80. painter.setBrush(brush)
  81. rect = QRectF(0.0, 0.0, width, self.height() - 1)
  82. painter.drawRoundedRect(rect, *rounding)
  83. # Draw text background
  84. x = int(((self.width() - 1) / 2) - ((self.textRect.width() + 1) / 2))
  85. y = int(((self.height() - 1) / 2) + ((self.textRect.height() + 1) / 2))
  86. y -= 2
  87. painter.setPen(Qt.white)
  88. brush = QBrush(QColor(Qt.white), Qt.Dense4Pattern)
  89. painter.setBrush(brush)
  90. painter.drawRect(x, int(y/2) - 2, self.textRect.width(), self.textRect.height())
  91. # Draw text
  92. painter.setPen(Qt.black)
  93. painter.drawText(x, y, self.text)