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
This commit is contained in:
Marta Marczykowska-Górecka 2020-01-29 19:27:17 +01:00
parent 8902727e0f
commit bd8fc05725
No known key found for this signature in database
GPG Key ID: 9A752C30B26FD04B
2 changed files with 47 additions and 11 deletions

View File

@ -30,8 +30,7 @@ import sys
import quamash import quamash
from qubesadmin import events from qubesadmin import events
from PyQt5 import QtWidgets, QtCore # pylint: disable=import-error from PyQt5 import QtWidgets, QtCore, QtGui # pylint: disable=import-error
from PyQt5.QtGui import QIcon # pylint: disable=import-error
def _filter_internal(vm): def _filter_internal(vm):
@ -39,6 +38,36 @@ def _filter_internal(vm):
and not vm.features.get('internal', False)) 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, def prepare_choice(widget, holder, propname, choice, default,
filter_function=None, *, filter_function=None, *,
icon_getter=None, allow_internal=None, allow_default=False, 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), sorted(app.labels.values(), key=lambda l: l.index),
default, *args, default, *args,
icon_getter=(lambda label: icon_getter=(lambda label:
QIcon.fromTheme(label.icon)), QtGui.QIcon.fromTheme(label.icon)),
**kwargs) **kwargs)

View File

@ -110,16 +110,16 @@
<number>15</number> <number>15</number>
</property> </property>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QSpinBox" name="root_resize"> <widget class="SizeSpinBox" name="root_resize">
<property name="enabled"> <property name="enabled">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Values displayed using the binary definition of gigabyte and megabyte, i.e. 1024&lt;span style=&quot; vertical-align:super;&quot;&gt;3&lt;/span&gt; and 1024&lt;span style=&quot; vertical-align:super;&quot;&gt;2 &lt;/span&gt;bytes respectively.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="alignment"> <property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property> </property>
<property name="suffix">
<string>MiB</string>
</property>
<property name="maximum"> <property name="maximum">
<number>1048576</number> <number>1048576</number>
</property> </property>
@ -129,16 +129,16 @@
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
<widget class="QSpinBox" name="max_priv_storage"> <widget class="SizeSpinBox" name="max_priv_storage">
<property name="enabled"> <property name="enabled">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Values displayed using the binary definition of gigabyte and megabyte, i.e. 1024&lt;span style=&quot; vertical-align:super;&quot;&gt;3&lt;/span&gt; and 1024&lt;span style=&quot; vertical-align:super;&quot;&gt;2 &lt;/span&gt;bytes respectively.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="alignment"> <property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property> </property>
<property name="suffix">
<string>MiB</string>
</property>
<property name="minimum"> <property name="minimum">
<number>0</number> <number>0</number>
</property> </property>
@ -1556,6 +1556,13 @@ The qube must be running to disable seamless mode; this setting is not persisten
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>SizeSpinBox</class>
<extends>QSpinBox</extends>
<header>qubesmanager/utils.h</header>
</customwidget>
</customwidgets>
<tabstops> <tabstops>
<tabstop>tabWidget</tabstop> <tabstop>tabWidget</tabstop>
<tabstop>vmname</tabstop> <tabstop>vmname</tabstop>