summaryrefslogtreecommitdiff
path: root/src/VBox/Frontends/VirtualBox/src/settings/editors/UIExecutionCapEditor.cpp
blob: f599c8d176ecbde7b931310d4eafc616a35240cc (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
/* $Id$ */
/** @file
 * VBox Qt GUI - UIExecutionCapEditor class implementation.
 */

/*
 * Copyright (C) 2019-2022 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

/* Qt includes: */
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QSpinBox>
#include <QVBoxLayout>

/* GUI includes: */
#include "QIAdvancedSlider.h"
#include "UICommon.h"
#include "UIExecutionCapEditor.h"

UIExecutionCapEditor::UIExecutionCapEditor(QWidget *pParent /* = 0 */)
    : QIWithRetranslateUI<QWidget>(pParent)
    , m_uMinExecCap(1)
    , m_uMedExecCap(40)
    , m_uMaxExecCap(100)
    , m_pLabelExecCap(0)
    , m_pSlider(0)
    , m_pSpinBox(0)
    , m_pLabelExecCapMin(0)
    , m_pLabelExecCapMax(0)
{
    prepare();
}

int UIExecutionCapEditor::medExecCap() const
{
    return (int)m_uMedExecCap;
}

void UIExecutionCapEditor::setValue(int iValue)
{
    if (m_pSlider)
        m_pSlider->setValue(iValue);
}

int UIExecutionCapEditor::value() const
{
    return m_pSlider ? m_pSlider->value() : 0;
}

int UIExecutionCapEditor::minimumLabelHorizontalHint() const
{
    return m_pLabelExecCap->minimumSizeHint().width();
}

void UIExecutionCapEditor::setMinimumLayoutIndent(int iIndent)
{
    if (m_pLayout)
        m_pLayout->setColumnMinimumWidth(0, iIndent);
}

void UIExecutionCapEditor::retranslateUi()
{
    if (m_pLabelExecCap)
        m_pLabelExecCap->setText(tr("&Execution Cap:"));

    const QString strToolTip(tr("Limits the amount of time that each virtual CPU is allowed to run for. "
                                "Each virtual CPU will be allowed to use up to this percentage of the processing "
                                "time available on one physical CPU."));
    if (m_pSlider)
        m_pSlider->setToolTip(strToolTip);
    if (m_pSpinBox)
    {
        m_pSpinBox->setSuffix(QString("%"));
        m_pSpinBox->setToolTip(strToolTip);
    }

    if (m_pLabelExecCapMin)
    {
        m_pLabelExecCapMin->setText(QString("%1%").arg(m_uMinExecCap));
        m_pLabelExecCapMin->setToolTip(tr("Minimum possible execution cap."));
    }
    if (m_pLabelExecCapMax)
    {
        m_pLabelExecCapMax->setText(QString("%1%").arg(m_uMaxExecCap));
        m_pLabelExecCapMax->setToolTip(tr("Maximum possible virtual CPU count."));
    }
}

void UIExecutionCapEditor::sltHandleSliderChange()
{
    /* Apply spin-box value keeping it's signals disabled: */
    if (m_pSpinBox && m_pSlider)
    {
        m_pSpinBox->blockSignals(true);
        m_pSpinBox->setValue(m_pSlider->value());
        m_pSpinBox->blockSignals(false);
    }

    /* Send signal to listener: */
    emit sigValueChanged();
}

void UIExecutionCapEditor::sltHandleSpinBoxChange()
{
    /* Apply slider value keeping it's signals disabled: */
    if (m_pSpinBox && m_pSlider)
    {
        m_pSlider->blockSignals(true);
        m_pSlider->setValue(m_pSpinBox->value());
        m_pSlider->blockSignals(false);
    }

    /* Send signal to listener: */
    emit sigValueChanged();
}

void UIExecutionCapEditor::prepare()
{
    /* Create main layout: */
    m_pLayout = new QGridLayout(this);
    if (m_pLayout)
    {
        m_pLayout->setContentsMargins(0, 0, 0, 0);

        /* Create main label: */
        m_pLabelExecCap = new QLabel(this);
        if (m_pLabelExecCap)
        {
            m_pLabelExecCap->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
            m_pLayout->addWidget(m_pLabelExecCap, 0, 0);
        }

        /* Create slider layout: */
        QVBoxLayout *pSliderLayout = new QVBoxLayout;
        if (pSliderLayout)
        {
            pSliderLayout->setContentsMargins(0, 0, 0, 0);

            /* Create VCPU slider: */
            m_pSlider = new QIAdvancedSlider(this);
            if (m_pSlider)
            {
                m_pSlider->setOrientation(Qt::Horizontal);
                m_pSlider->setPageStep(10);
                m_pSlider->setSingleStep(1);
                m_pSlider->setTickInterval(10);
                m_pSlider->setMinimum(m_uMinExecCap);
                m_pSlider->setMaximum(m_uMaxExecCap);
                m_pSlider->setWarningHint(m_uMinExecCap, m_uMedExecCap);
                m_pSlider->setOptimalHint(m_uMedExecCap, m_uMaxExecCap);
                connect(m_pSlider, &QIAdvancedSlider::valueChanged,
                        this, &UIExecutionCapEditor::sltHandleSliderChange);
                pSliderLayout->addWidget(m_pSlider);
            }

            /* Create legend layout: */
            QHBoxLayout *pLegendLayout = new QHBoxLayout;
            if (pLegendLayout)
            {
                pLegendLayout->setContentsMargins(0, 0, 0, 0);

                /* Create min label: */
                m_pLabelExecCapMin = new QLabel(this);
                if (m_pLabelExecCapMin)
                    pLegendLayout->addWidget(m_pLabelExecCapMin);

                /* Push labels from each other: */
                pLegendLayout->addStretch();

                /* Create max label: */
                m_pLabelExecCapMax = new QLabel(this);
                if (m_pLabelExecCapMax)
                    pLegendLayout->addWidget(m_pLabelExecCapMax);

                /* Add legend layout to slider layout: */
                pSliderLayout->addLayout(pLegendLayout);
            }

            /* Add slider layout to main layout: */
            m_pLayout->addLayout(pSliderLayout, 0, 1, 2, 1);
        }

        /* Create VCPU spin-box: */
        m_pSpinBox = new QSpinBox(this);
        if (m_pSpinBox)
        {
            setFocusProxy(m_pSpinBox);
            if (m_pLabelExecCap)
                m_pLabelExecCap->setBuddy(m_pSpinBox);
            m_pSpinBox->setMinimum(m_uMinExecCap);
            m_pSpinBox->setMaximum(m_uMaxExecCap);
            uiCommon().setMinimumWidthAccordingSymbolCount(m_pSpinBox, 4);
            connect(m_pSpinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
                    this, &UIExecutionCapEditor::sltHandleSpinBoxChange);
            m_pLayout->addWidget(m_pSpinBox, 0, 2);
        }
    }

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