summaryrefslogtreecommitdiff
path: root/src/VBox/Frontends/VirtualBox/src/widgets/UIMediumSizeEditor.cpp
blob: 259e521cadc75ef915a4e6c37a5c4832139393be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* $Id$ */
/** @file
 * VBox Qt GUI - UIMediumSizeEditor class implementation.
 */

/*
 * Copyright (C) 2006-2022 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

/* Qt includes: */
#include <QGridLayout>
#include <QLabel>
#include <QRegularExpressionValidator>
#include <QSlider>

/* GUI includes: */
#include "QILineEdit.h"
#include "UICommon.h"
#include "UIConverter.h"
#include "UIMediumSizeEditor.h"
#include "UITranslator.h"

/* COM includes: */
#include "CSystemProperties.h"


const qulonglong UIMediumSizeEditor::m_uSectorSize = 512;

UIMediumSizeEditor::UIMediumSizeEditor(QWidget *pParent, qulonglong uMinimumSize /* = _4M */)
    : QIWithRetranslateUI<QWidget>(pParent)
    , m_uSizeMin(uMinimumSize)
    , m_uSizeMax(uiCommon().virtualBox().GetSystemProperties().GetInfoVDSize())
    , m_iSliderScale(calculateSliderScale(m_uSizeMax))
    , m_uSize(0)
    , m_enmSizeSuffix(SizeSuffix_Byte)
    , m_pSlider(0)
    , m_pLabelMinSize(0)
    , m_pLabelMaxSize(0)
    , m_pEditor(0)
{
    /* Prepare: */
    prepare();
    QString strRegEx = QString("[^\\d%1]").arg(UITranslator::decimalSep());
    m_regExNonDigitOrSeparator = QRegularExpression(strRegEx);
}

void UIMediumSizeEditor::setMediumSize(qulonglong uSize)
{
    /* Remember the new size: */
    m_uSize = uSize;

    /* And assign it to the slider & editor: */
    m_pSlider->blockSignals(true);
    m_pSlider->setValue(sizeMBToSlider(m_uSize, m_iSliderScale));
    m_pSlider->blockSignals(false);
    m_pEditor->blockSignals(true);
    m_pEditor->setText(UITranslator::formatSize(m_uSize));
    m_enmSizeSuffix = UITranslator::parseSizeSuffix(m_pEditor->text());
    m_pEditor->blockSignals(false);
}

void UIMediumSizeEditor::retranslateUi()
{
    /* Translate labels: */
    m_pLabelMinSize->setText(UITranslator::formatSize(m_uSizeMin));
    m_pLabelMaxSize->setText(UITranslator::formatSize(m_uSizeMax));

    /* Translate fields: */
    m_pSlider->setToolTip(tr("Holds the size of this medium."));
    m_pEditor->setToolTip(tr("Holds the size of this medium."));
    m_pLabelMinSize->setToolTip(tr("Minimum size for this medium."));
    m_pLabelMaxSize->setToolTip(tr("Maximum size for this medium."));
}

void UIMediumSizeEditor::sltSizeSliderChanged(int iValue)
{
    /* Update the current size: */
    m_uSize = sliderToSizeMB(iValue, m_iSliderScale);
    /* Update the other widget: */
    m_pEditor->blockSignals(true);
    m_pEditor->setText(UITranslator::formatSize(m_uSize));
    m_enmSizeSuffix = UITranslator::parseSizeSuffix(m_pEditor->text());
    m_pEditor->blockSignals(false);
    /* Notify the listeners: */
    emit sigSizeChanged(m_uSize);
}

void UIMediumSizeEditor::sltSizeEditorTextChanged()
{
    QString strSizeString = ensureSizeSuffix(m_pEditor->text());


    m_pEditor->blockSignals(true);
    int iCursorPosition = m_pEditor->cursorPosition();
    m_pEditor->setText(strSizeString);
    m_pEditor->setCursorPosition(iCursorPosition);
    m_pEditor->blockSignals(false);

    /* Update the current size: */
    m_uSize = checkSectorSizeAlignment(UITranslator::parseSize(strSizeString));

    /* Update the other widget: */
    m_pSlider->blockSignals(true);
    m_pSlider->setValue(sizeMBToSlider(m_uSize, m_iSliderScale));
    m_pSlider->blockSignals(false);
    /* Notify the listeners: */
    emit sigSizeChanged(m_uSize);
}

QString UIMediumSizeEditor::ensureSizeSuffix(const QString &strSizeString)
{
    /* Try to update the m_enmSizeSuffix: */
    if (UITranslator::hasSizeSuffix(strSizeString))
        m_enmSizeSuffix = UITranslator::parseSizeSuffix(strSizeString);

    QString strOnlyDigits(strSizeString);
    /* Remove any chars from the string except digits and decimal separator and then add a space and size suffix: */
    return QString("%1 %2").arg(strOnlyDigits.remove(m_regExNonDigitOrSeparator)).arg(gpConverter->toString(m_enmSizeSuffix));
}

void UIMediumSizeEditor::prepare()
{
    /* Create layout: */
    QGridLayout *pLayout = new QGridLayout(this);
    if (pLayout)
    {
        /* Configure layout: */
        pLayout->setContentsMargins(0, 0, 0, 0);
        pLayout->setColumnStretch(0, 1);
        pLayout->setColumnStretch(1, 1);
        pLayout->setColumnStretch(2, 0);

        /* Create size slider: */
        m_pSlider = new QSlider;
        if (m_pSlider)
        {
            /* Configure slider: */
            m_pSlider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
            m_pSlider->setOrientation(Qt::Horizontal);
            m_pSlider->setTickPosition(QSlider::TicksBelow);
            m_pSlider->setFocusPolicy(Qt::StrongFocus);
            m_pSlider->setPageStep(m_iSliderScale);
            m_pSlider->setSingleStep(m_iSliderScale / 8);
            m_pSlider->setTickInterval(0);
            m_pSlider->setMinimum(sizeMBToSlider(m_uSizeMin, m_iSliderScale));
            m_pSlider->setMaximum(sizeMBToSlider(m_uSizeMax, m_iSliderScale));
            connect(m_pSlider, &QSlider::valueChanged,
                    this, &UIMediumSizeEditor::sltSizeSliderChanged);

            /* Add into layout: */
            pLayout->addWidget(m_pSlider, 0, 0, 1, 2, Qt::AlignTop);
        }

        /* Create minimum size label: */
        m_pLabelMinSize = new QLabel;
        if (m_pLabelMinSize)
        {
            /* Configure label: */
            m_pLabelMinSize->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

            /* Add into layout: */
            pLayout->addWidget(m_pLabelMinSize, 1, 0);
        }

        /* Create maximum size label: */
        m_pLabelMaxSize = new QLabel;
        if (m_pLabelMaxSize)
        {
            /* Configure label: */
            m_pLabelMaxSize->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

            /* Add into layout: */
            pLayout->addWidget(m_pLabelMaxSize, 1, 1);
        }

        /* Create size editor: */
        m_pEditor = new QILineEdit;
        if (m_pEditor)
        {
            /* Configure editor: */
            m_pEditor->installEventFilter(this);
            m_pEditor->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
            m_pEditor->setFixedWidthByText("88888.88 MB");
            m_pEditor->setAlignment(Qt::AlignRight);
            m_pEditor->setValidator(new QRegularExpressionValidator(QRegularExpression(UITranslator::sizeRegexp()), this));
            connect(m_pEditor, &QILineEdit::textChanged,
                    this, &UIMediumSizeEditor::sltSizeEditorTextChanged);

            /* Add into layout: */
            pLayout->addWidget(m_pEditor, 0, 2, Qt::AlignTop);
        }
    }

    /* Apply language settings: */
    retranslateUi();
}

/* static */
int UIMediumSizeEditor::calculateSliderScale(qulonglong uMaximumMediumSize)
{
    /* Detect how many steps to recognize between adjacent powers of 2
     * to ensure that the last slider step is exactly that we need: */
    int iSliderScale = 0;
    int iPower = log2i(uMaximumMediumSize);
    qulonglong uTickMB = (qulonglong)1 << iPower;
    if (uTickMB < uMaximumMediumSize)
    {
        qulonglong uTickMBNext = (qulonglong)1 << (iPower + 1);
        qulonglong uGap = uTickMBNext - uMaximumMediumSize;
        iSliderScale = (int)((uTickMBNext - uTickMB) / uGap);
#ifdef VBOX_WS_MAC
        // WORKAROUND:
        // There is an issue with Qt5 QSlider under OSX:
        // Slider tick count (maximum - minimum) is limited with some
        // "magical number" - 588351, having it more than that brings
        // unpredictable results like slider token jumping and disappearing,
        // so we are limiting tick count by lowering slider-scale 128 times.
        iSliderScale /= 128;
#endif /* VBOX_WS_MAC */
    }
    return qMax(iSliderScale, 8);
}

/* static */
int UIMediumSizeEditor::log2i(qulonglong uValue)
{
    if (!uValue)
        return 0;
    int iPower = -1;
    while (uValue)
    {
        ++iPower;
        uValue >>= 1;
    }
    return iPower;
}

/* static */
int UIMediumSizeEditor::sizeMBToSlider(qulonglong uValue, int iSliderScale)
{
    /* Make sure *any* slider value is multiple of m_uSectorSize: */
    uValue /= m_uSectorSize;

    /* Calculate result: */
    int iPower = log2i(uValue);
    qulonglong uTickMB = qulonglong (1) << iPower;
    qulonglong uTickMBNext = qulonglong (1) << (iPower + 1);
    int iStep = (uValue - uTickMB) * iSliderScale / (uTickMBNext - uTickMB);
    int iResult = iPower * iSliderScale + iStep;

    /* Return result: */
    return iResult;
}

/* static */
qulonglong UIMediumSizeEditor::sliderToSizeMB(int uValue, int iSliderScale)
{
    /* Calculate result: */
    int iPower = uValue / iSliderScale;
    int iStep = uValue % iSliderScale;
    qulonglong uTickMB = qulonglong (1) << iPower;
    qulonglong uTickMBNext = qulonglong (1) << (iPower + 1);
    qulonglong uResult = uTickMB + (uTickMBNext - uTickMB) * iStep / iSliderScale;

    /* Make sure *any* slider value is multiple of m_uSectorSize: */
    uResult *= m_uSectorSize;

    /* Return result: */
    return uResult;
}

void UIMediumSizeEditor::updateSizeToolTips(qulonglong uSize)
{
    const QString strToolTip = tr("<nobr>%1 (%2 B)</nobr>").arg(UITranslator::formatSize(uSize)).arg(uSize);
    m_pSlider->setToolTip(strToolTip);
    m_pEditor->setToolTip(strToolTip);
}

qulonglong UIMediumSizeEditor::checkSectorSizeAlignment(qulonglong uSize)
{
    if (m_uSectorSize == 0 || uSize % m_uSectorSize == 0)
        return uSize;
    qulonglong uNewSize = (uSize / m_uSectorSize) * m_uSectorSize;
    return uNewSize;
}