summaryrefslogtreecommitdiffstats
path: root/src/VBox/Frontends/VirtualBox/src/extensions/QIAdvancedSlider.cpp
blob: 3f3fde11f59ce46868ef6656991f229456a5c9e9 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* $Id: QIAdvancedSlider.cpp $ */
/** @file
 * VBox Qt GUI - Qt extensions: QIAdvancedSlider class implementation.
 */

/*
 * Copyright (C) 2009-2023 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 <QPainter>
#include <QSlider>
#include <QStyle>
#include <QVBoxLayout>

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

/* Qt includes: */
#include <QStyleOptionSlider>

/* External includes: */
#include <math.h>


/** QSlider subclass for our private needs. */
class UIPrivateSlider : public QSlider
{
    Q_OBJECT;

public:

    /** Constructs private-slider passing @a pParent and @a enmOrientation to the base-class. */
    UIPrivateSlider(Qt::Orientation enmOrientation, QWidget *pParent = 0);

    /** Returns suitable position for @a iValue. */
    int positionForValue(int iValue) const;

    /** @todo encapsulate below stuff accordingly.. */

    /** Holds the minimum optimal border. */
    int m_minOpt;
    /** Holds the maximum optimal border. */
    int m_maxOpt;
    /** Holds the minimum warning border. */
    int m_minWrn;
    /** Holds the maximum warning border. */
    int m_maxWrn;
    /** Holds the minimum error border. */
    int m_minErr;
    /** Holds the maximum error border. */
    int m_maxErr;

protected:

    /** Handles paint @a pEvent. */
    virtual void paintEvent(QPaintEvent *pEvent);

private:

    /** Holds the optimal color. */
    QColor m_optColor;
    /** Holds the warning color. */
    QColor m_wrnColor;
    /** Holds the error color. */
    QColor m_errColor;
};


/*********************************************************************************************************************************
*   Class UIPrivateSlider implementation.                                                                                        *
*********************************************************************************************************************************/

UIPrivateSlider::UIPrivateSlider(Qt::Orientation enmOrientation, QWidget *pParent /* = 0 */)
    : QSlider(enmOrientation, pParent)
    , m_minOpt(-1)
    , m_maxOpt(-1)
    , m_minWrn(-1)
    , m_maxWrn(-1)
    , m_minErr(-1)
    , m_maxErr(-1)
    , m_optColor(0x0, 0xff, 0x0, 0x3c)
    , m_wrnColor(0xff, 0x54, 0x0, 0x3c)
    , m_errColor(0xff, 0x0, 0x0, 0x3c)
{
    /* Make sure ticks *always* positioned below: */
    setTickPosition(QSlider::TicksBelow);
}

int UIPrivateSlider::positionForValue(int iValue) const
{
    QStyleOptionSlider opt;
    initStyleOption(&opt);
    opt.subControls = QStyle::SC_All;
    int iAvailable = opt.rect.width() - style()->pixelMetric(QStyle::PM_SliderLength, &opt, this);
    return QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, iValue, iAvailable);
}

void UIPrivateSlider::paintEvent(QPaintEvent *pEvent)
{
    QPainter p(this);

    QStyleOptionSlider opt;
    initStyleOption(&opt);
    opt.subControls = QStyle::SC_All;

    int iAvailable = opt.rect.width() - style()->pixelMetric(QStyle::PM_SliderLength, &opt, this);
    QSize s = size();

    /* We want to acquire SC_SliderTickmarks sub-control rectangle
     * and fill it with necessary background colors: */
#ifdef VBOX_WS_MAC
    // WORKAROUND:
    // Under MacOS X SC_SliderTickmarks is not fully reliable
    // source of the information we need, providing us with incorrect width.
    // So we have to calculate tickmarks rectangle ourself.
    QRect ticks = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderTickmarks, this);
    ticks.setRect((s.width() - iAvailable) / 2, s.height() - ticks.y(), iAvailable, ticks.height());
#else /* VBOX_WS_MAC */
    // WORKAROUND:
    // Under Windows SC_SliderTickmarks is fully unreliable
    // source of the information we need, providing us with empty rectangle.
    // Under X11 SC_SliderTickmarks is not fully reliable
    // source of the information we need, providing us with different rectangles
    // (correct or incorrect) under different look&feel styles.
    // So we have to calculate tickmarks rectangle ourself.
    QRect ticks = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this) |
                  style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
    ticks.setRect((s.width() - iAvailable) / 2, ticks.bottom() + 1, iAvailable, s.height() - ticks.bottom() - 1);
#endif /* VBOX_WS_MAC */

    if ((m_minOpt != -1 &&
         m_maxOpt != -1) &&
        m_minOpt != m_maxOpt)
    {
        int iPosMinOpt = QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, m_minOpt, iAvailable);
        int iPosMaxOpt = QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, m_maxOpt, iAvailable);
        p.fillRect(ticks.x() + iPosMinOpt, ticks.y(), iPosMaxOpt - iPosMinOpt + 1, ticks.height(), m_optColor);
    }
    if ((m_minWrn != -1 &&
         m_maxWrn != -1) &&
        m_minWrn != m_maxWrn)
    {
        int iPosMinWrn = QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, m_minWrn, iAvailable);
        int iPosMaxWrn = QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, m_maxWrn, iAvailable);
        p.fillRect(ticks.x() + iPosMinWrn, ticks.y(), iPosMaxWrn - iPosMinWrn + 1, ticks.height(), m_wrnColor);
    }
    if ((m_minErr != -1 &&
         m_maxErr != -1) &&
        m_minErr != m_maxErr)
    {
        int iPosMinErr = QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, m_minErr, iAvailable);
        int iPosMaxErr = QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, m_maxErr, iAvailable);
        p.fillRect(ticks.x() + iPosMinErr, ticks.y(), iPosMaxErr - iPosMinErr + 1, ticks.height(), m_errColor);
    }
    p.end();

    /* Call to base-class: */
    QSlider::paintEvent(pEvent);
}


/*********************************************************************************************************************************
*   Class QIAdvancedSlider implementation.                                                                                       *
*********************************************************************************************************************************/

QIAdvancedSlider::QIAdvancedSlider(QWidget *pParent /* = 0 */)
  : QWidget(pParent)
{
    prepare();
}

QIAdvancedSlider::QIAdvancedSlider(Qt::Orientation enmOrientation, QWidget *pParent /* = 0 */)
  : QWidget(pParent)
{
    prepare(enmOrientation);
}

int QIAdvancedSlider::value() const
{
    return m_pSlider->value();
}

void QIAdvancedSlider::setRange(int iMin, int iMax)
{
    m_pSlider->setRange(iMin, iMax);
}

void QIAdvancedSlider::setMaximum(int iValue)
{
    m_pSlider->setMaximum(iValue);
}

int QIAdvancedSlider::maximum() const
{
    return m_pSlider->maximum();
}

void QIAdvancedSlider::setMinimum(int iValue)
{
    m_pSlider->setMinimum(iValue);
}

int QIAdvancedSlider::minimum() const
{
    return m_pSlider->minimum();
}

void QIAdvancedSlider::setPageStep(int iValue)
{
    m_pSlider->setPageStep(iValue);
}

int QIAdvancedSlider::pageStep() const
{
    return m_pSlider->pageStep();
}

void QIAdvancedSlider::setSingleStep(int iValue)
{
    m_pSlider->setSingleStep(iValue);
}

int QIAdvancedSlider::singelStep() const
{
    return m_pSlider->singleStep();
}

void QIAdvancedSlider::setTickInterval(int iValue)
{
    m_pSlider->setTickInterval(iValue);
}

int QIAdvancedSlider::tickInterval() const
{
    return m_pSlider->tickInterval();
}

Qt::Orientation QIAdvancedSlider::orientation() const
{
    return m_pSlider->orientation();
}

void QIAdvancedSlider::setSnappingEnabled(bool fOn)
{
    m_fSnappingEnabled = fOn;
}

bool QIAdvancedSlider::isSnappingEnabled() const
{
    return m_fSnappingEnabled;
}

void QIAdvancedSlider::setOptimalHint(int iMin, int iMax)
{
    m_pSlider->m_minOpt = iMin;
    m_pSlider->m_maxOpt = iMax;

    update();
}

void QIAdvancedSlider::setWarningHint(int iMin, int iMax)
{
    m_pSlider->m_minWrn = iMin;
    m_pSlider->m_maxWrn = iMax;

    update();
}

void QIAdvancedSlider::setErrorHint(int iMin, int iMax)
{
    m_pSlider->m_minErr = iMin;
    m_pSlider->m_maxErr = iMax;

    update();
}

void QIAdvancedSlider::setToolTip(const QString &strToolTip)
{
    m_pSlider->setToolTip(strToolTip);
}

void QIAdvancedSlider::setOrientation(Qt::Orientation enmOrientation)
{
    m_pSlider->setOrientation(enmOrientation);
}

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

void QIAdvancedSlider::sltSliderMoved(int iValue)
{
    iValue = snapValue(iValue);
    m_pSlider->setValue(iValue);
    emit sliderMoved(iValue);
}

void QIAdvancedSlider::prepare(Qt::Orientation enmOrientation /* = Qt::Horizontal */)
{
    m_fSnappingEnabled = false;

    /* Create layout: */
    QVBoxLayout *pMainLayout = new QVBoxLayout(this);
    if (pMainLayout)
    {
        /* Configure layout: */
        pMainLayout->setContentsMargins(0, 0, 0, 0);

        /* Create private-slider: */
        m_pSlider = new UIPrivateSlider(enmOrientation, this);
        if (m_pSlider)
        {
            connect(m_pSlider, &UIPrivateSlider::sliderMoved,    this, &QIAdvancedSlider::sltSliderMoved);
            connect(m_pSlider, &UIPrivateSlider::valueChanged,   this, &QIAdvancedSlider::valueChanged);
            connect(m_pSlider, &UIPrivateSlider::sliderPressed,  this, &QIAdvancedSlider::sliderPressed);
            connect(m_pSlider, &UIPrivateSlider::sliderReleased, this, &QIAdvancedSlider::sliderReleased);

            /* Add into layout: */
            pMainLayout->addWidget(m_pSlider);
        }
    }
}

int QIAdvancedSlider::snapValue(int iValue)
{
    if (m_fSnappingEnabled &&
        iValue > 2)
    {
        float l2 = log((float)iValue)/log(2.0);
        int iNewVal = (int)pow((float)2, (int)qRound(l2)); /* The value to snap on */
        int iPos = m_pSlider->positionForValue(iValue); /* Get the relative screen pos for the original value */
        int iNewPos = m_pSlider->positionForValue(iNewVal); /* Get the relative screen pos for the snap value */
        if (abs(iNewPos - iPos) < 5) /* 10 pixel snapping range */
        {
            iValue = iNewVal;
            if (iValue > m_pSlider->maximum())
                iValue = m_pSlider->maximum();
            else if (iValue < m_pSlider->minimum())
                iValue = m_pSlider->minimum();
        }
    }
    return iValue;
}


#include "QIAdvancedSlider.moc"