From bd8fc05725b84f23800a443f38c670737c265613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marta=20Marczykowska-G=C3=B3recka?= Date: Wed, 29 Jan 2020 19:27:17 +0100 Subject: [PATCH] Changed display of image sizes in VM settings to GB when sensible For sizes above 1 GB, the sizes of private image and root image will be displayed as %.1f GB; smaller sizes will be displayed as %d MB. fixes QubesOS/qubes-issues#5592 --- qubesmanager/utils.py | 35 ++++++++++++++++++++++++++++++++--- ui/settingsdlg.ui | 23 +++++++++++++++-------- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/qubesmanager/utils.py b/qubesmanager/utils.py index 52c4bd6..e8d8ea5 100644 --- a/qubesmanager/utils.py +++ b/qubesmanager/utils.py @@ -30,8 +30,7 @@ import sys import quamash from qubesadmin import events -from PyQt5 import QtWidgets, QtCore # pylint: disable=import-error -from PyQt5.QtGui import QIcon # pylint: disable=import-error +from PyQt5 import QtWidgets, QtCore, QtGui # pylint: disable=import-error def _filter_internal(vm): @@ -39,6 +38,36 @@ def _filter_internal(vm): and not vm.features.get('internal', False)) +class SizeSpinBox(QtWidgets.QSpinBox): + # pylint: disable=invalid-name, no-self-use + def __init__(self, *args, **kwargs): + super(SizeSpinBox, self).__init__(*args, **kwargs) + + self.pattern = r'(\d+\.?\d?) ?(GB|MB)' + self.regex = re.compile(self.pattern) + self.validator = QtGui.QRegExpValidator(QtCore.QRegExp( + self.pattern), self) + + def textFromValue(self, v: int) -> str: + if v > 1024: + return '{:.1f} GB'.format(v / 1024) + + return '{} MB'.format(v) + + def validate(self, text: str, pos: int): + return self.validator.validate(text, pos) + + def valueFromText(self, text: str) -> int: + value, unit = self.regex.fullmatch(text.strip()).groups() + + if unit == 'GB': + multiplier = 1024 + else: + multiplier = 1 + + return int(float(value) * multiplier) + + def prepare_choice(widget, holder, propname, choice, default, filter_function=None, *, icon_getter=None, allow_internal=None, allow_default=False, @@ -171,7 +200,7 @@ def prepare_label_choice(widget, holder, propname, default, *args, **kwargs): sorted(app.labels.values(), key=lambda l: l.index), default, *args, icon_getter=(lambda label: - QIcon.fromTheme(label.icon)), + QtGui.QIcon.fromTheme(label.icon)), **kwargs) diff --git a/ui/settingsdlg.ui b/ui/settingsdlg.ui index bb29417..f020ebd 100644 --- a/ui/settingsdlg.ui +++ b/ui/settingsdlg.ui @@ -110,16 +110,16 @@ 15 - + true + + <html><head/><body><p>Values displayed using the binary definition of gigabyte and megabyte, i.e. 1024<span style=" vertical-align:super;">3</span> and 1024<span style=" vertical-align:super;">2 </span>bytes respectively.</p></body></html> + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - MiB - 1048576 @@ -129,16 +129,16 @@ - + true + + <html><head/><body><p>Values displayed using the binary definition of gigabyte and megabyte, i.e. 1024<span style=" vertical-align:super;">3</span> and 1024<span style=" vertical-align:super;">2 </span>bytes respectively.</p></body></html> + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - MiB - 0 @@ -1556,6 +1556,13 @@ The qube must be running to disable seamless mode; this setting is not persisten + + + SizeSpinBox + QSpinBox +
qubesmanager/utils.h
+
+
tabWidget vmname