VirtualBox

Changeset 21194 in vbox for trunk/src


Ignore:
Timestamp:
Jul 3, 2009 12:24:28 PM (16 years ago)
Author:
vboxsync
Message:

FE/Qt4: Colored slider for memory/CPU. Better snapping handling.

Location:
trunk/src/VBox/Frontends/VirtualBox
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/include/QIAdvancedSlider.h

    r21120 r21194  
    2626/* Qt includes */
    2727#include <QSlider>
     28
     29class CPrivateSlider;
    2830
    2931class QIAdvancedSlider: public QWidget
     
    6264    bool isSnappingEnabled() const;
    6365
     66    void setOptimalHint (int aMin, int aMax);
     67    void setWarningHint (int aMin, int aMax);
     68    void setErrorHint (int aMin, int aMax);
     69
    6470public slots:
    6571
     
    8389
    8490    /* Private member vars */
    85     QSlider *mSlider;
     91    CPrivateSlider *mSlider;
    8692    bool mSnappingEnabled;
    8793};
  • trunk/src/VBox/Frontends/VirtualBox/include/VBoxVMSettingsSystem.h

    r21132 r21194  
    8282    uint mMinGuestCPU;
    8383    uint mMaxGuestCPU;
     84
     85    int mMaxRAMAllowed;
     86    int mMaxRAMOptimum;
    8487};
    8588
  • trunk/src/VBox/Frontends/VirtualBox/src/QIAdvancedSlider.cpp

    r21124 r21194  
    2222
    2323#include "QIAdvancedSlider.h"
     24#include "VBoxGlobal.h"
    2425
    2526/* Qt includes */
    2627#include <QVBoxLayout>
     28#include <QPainter>
     29#include <QStyle>
     30#include <QStyleOptionSlider>
    2731
    2832/* System includes */
    2933#include <math.h>
    3034
     35class CPrivateSlider: public QSlider
     36{
     37public:
     38    CPrivateSlider (Qt::Orientation aOrientation, QWidget *aParent = 0)
     39      : QSlider (aOrientation, aParent)
     40      , mOptColor (0x0, 0xff, 0x0, 0x3c)
     41      , mWrnColor (0xff, 0x54, 0x0, 0x3c)
     42      , mErrColor (0xff, 0x0, 0x0, 0x3c)
     43      , mMinOpt (-1)
     44      , mMaxOpt (-1)
     45      , mMinWrn (-1)
     46      , mMaxWrn (-1)
     47      , mMinErr (-1)
     48      , mMaxErr (-1)
     49    {}
     50
     51    int positionForValue (int aVal) const
     52    {
     53        QStyleOptionSlider opt;
     54        initStyleOption (&opt);
     55        opt.subControls = QStyle::SC_All;
     56        int available = style()->pixelMetric (QStyle::PM_SliderSpaceAvailable, &opt, this);
     57        return QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, aVal, available);
     58    }
     59
     60    virtual void paintEvent ( QPaintEvent * event )
     61    {
     62        QPainter p(this);
     63
     64        QStyleOptionSlider opt;
     65        initStyleOption (&opt);
     66        opt.subControls = QStyle::SC_All;
     67
     68        int available = style()->pixelMetric (QStyle::PM_SliderSpaceAvailable, &opt, this);
     69        QSize s = size();
     70
     71        QRect ticks = style()->subControlRect (QStyle::CC_Slider, &opt, QStyle::SC_SliderTickmarks, this);
     72        if (ticks.isNull() || ticks.isEmpty())
     73        {
     74            ticks = style()->subControlRect (QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this) | style()->subControlRect (QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
     75            ticks.setRect ((s.width() - available) / 2, ticks.bottom() + 1, available, s.height() - ticks.bottom() - 1);
     76        }
     77        if (mMinOpt != -1 &&
     78            mMaxOpt != -1)
     79        {
     80            int posMinOpt = QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, mMinOpt, available);
     81            int posMaxOpt = QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, mMaxOpt, available);
     82            p.fillRect (ticks.x() + posMinOpt, ticks.y(), posMaxOpt - posMinOpt + 1, ticks.height(), mOptColor);
     83        }
     84        if (mMinWrn != -1 &&
     85            mMaxWrn != -1)
     86        {
     87            int posMinWrn = QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, mMinWrn, available);
     88            int posMaxWrn = QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, mMaxWrn, available);
     89            p.fillRect (ticks.x() + posMinWrn, ticks.y(), posMaxWrn - posMinWrn + 1, ticks.height(), mWrnColor);
     90        }
     91        if (mMinErr != -1 &&
     92            mMaxErr != -1)
     93        {
     94            int posMinErr = QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, mMinErr, available);
     95            int posMaxErr = QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, mMaxErr, available);
     96            p.fillRect (ticks.x() + posMinErr, ticks.y(), posMaxErr - posMinErr + 1, ticks.height(), mErrColor);
     97        }
     98
     99        QSlider::paintEvent (event);
     100    }
     101
     102    QColor mOptColor;
     103    QColor mWrnColor;
     104    QColor mErrColor;
     105
     106    int mMinOpt;
     107    int mMaxOpt;
     108    int mMinWrn;
     109    int mMaxWrn;
     110    int mMinErr;
     111    int mMaxErr;
     112};
    31113
    32114QIAdvancedSlider::QIAdvancedSlider (QWidget *aParent /* = 0 */)
     
    125207{
    126208    return mSnappingEnabled;
     209}
     210
     211void QIAdvancedSlider::setOptimalHint (int aMin, int aMax)
     212{
     213    mSlider->mMinOpt = aMin;
     214    mSlider->mMaxOpt = aMax;
     215}
     216
     217void QIAdvancedSlider::setWarningHint (int aMin, int aMax)
     218{
     219    mSlider->mMinWrn = aMin;
     220    mSlider->mMaxWrn = aMax;
     221}
     222
     223void QIAdvancedSlider::setErrorHint (int aMin, int aMax)
     224{
     225    mSlider->mMinErr = aMin;
     226    mSlider->mMaxErr = aMax;
    127227}
    128228
     
    146246void QIAdvancedSlider::init (Qt::Orientation aOrientation /* = Qt::Horizontal */)
    147247{
    148     mSnappingEnabled = true;
     248    mSnappingEnabled = false;
    149249
    150250    QVBoxLayout *pMainLayout = new QVBoxLayout (this);
    151     mSlider = new QSlider (aOrientation, this);
     251    VBoxGlobal::setLayoutMargin (pMainLayout, 0);
     252    mSlider = new CPrivateSlider (aOrientation, this);
    152253    pMainLayout->addWidget (mSlider);
    153254
     
    164265    {
    165266        float l2 = log ((float)val)/log (2.0);
    166         float snap = 1.0/l2 * 2; /* How much border around the current snaping value (%) */
    167267        int newVal = pow ((float)2, (int)qRound (l2)); /* The value to snap on */
    168         if (abs (newVal - val) < (snap * newVal)) /* Snap only if we are in a defined array */
     268        int pos = mSlider->positionForValue (val); /* Get the relative screen pos for the original value */
     269        int newPos = mSlider->positionForValue (newVal); /* Get the relative screen pos for the snap value */
     270        if (abs (newPos - pos) < 5) /* 10 pixel snapping range */
    169271        {
    170272            val = newVal;
     
    177279    return val;
    178280}
     281
  • trunk/src/VBox/Frontends/VirtualBox/src/VBoxVMSettingsDisplay.cpp

    r20085 r21194  
    7474    mSlMemory->setMinimum ((MinVRAM / mSlMemory->pageStep()) * mSlMemory->pageStep());
    7575    mSlMemory->setMaximum (MaxVRAM);
     76    mSlMemory->setSnappingEnabled (true);
     77    quint64 needMBytes = VBoxGlobal::requiredVideoMemory (&mMachine) / _1M;
     78    mSlMemory->setErrorHint (0, 1);
     79    mSlMemory->setWarningHint (1, needMBytes);
     80    mSlMemory->setOptimalHint (needMBytes, MaxVRAM);
    7681    /* Limit min/max. size of QLineEdit */
    7782    mLeMemory->setFixedWidthByText (QString().fill ('8', 4));
  • trunk/src/VBox/Frontends/VirtualBox/src/VBoxVMSettingsSystem.cpp

    r21130 r21194  
    5757    mMinGuestRAM = sys.GetMinGuestRAM();
    5858    mMaxGuestRAM = RT_MIN (RT_ALIGN (vboxGlobal().virtualBox().GetHost().GetMemorySize(), _1G / _1M), sys.GetMaxGuestRAM());
     59    uint hostCPUs = vboxGlobal().virtualBox().GetHost().GetProcessorCount();
    5960    mMinGuestCPU = sys.GetMinGuestCPUCount();
    60     mMaxGuestCPU = RT_MIN (2 * vboxGlobal().virtualBox().GetHost().GetProcessorCount(), sys.GetMaxGuestCPUCount());
     61    mMaxGuestCPU = RT_MIN (2 * hostCPUs, sys.GetMaxGuestCPUCount());
     62
     63    /* Come up with some nice round percent boundraries relative to
     64     * the system memory. A max of 75% on a 256GB config is ridiculous,
     65     * even on an 8GB rig reserving 2GB for the OS is way to conservative.
     66     * The max numbers can be estimated using the following program:
     67     *
     68     *      double calcMaxPct(uint64_t cbRam)
     69     *      {
     70     *          double cbRamOverhead = cbRam * 0.0390625; // 160 bytes per page.
     71     *          double cbRamForTheOS = RT_MAX(RT_MIN(_512M, cbRam * 0.25), _64M);
     72     *          double OSPct  = (cbRamOverhead + cbRamForTheOS) * 100.0 / cbRam;
     73     *          double MaxPct = 100 - OSPct;
     74     *          return MaxPct;
     75     *      }
     76     *
     77     *      int main()
     78     *      {
     79     *          uint64_t cbRam = _1G;
     80     *          for (; !(cbRam >> 33); cbRam += _1G)
     81     *              printf("%8lluGB %.1f%% %8lluKB\n", cbRam >> 30, calcMaxPct(cbRam),
     82     *                     (uint64_t)(cbRam * calcMaxPct(cbRam) / 100.0) >> 20);
     83     *          for (; !(cbRam >> 51); cbRam <<= 1)
     84     *              printf("%8lluGB %.1f%% %8lluKB\n", cbRam >> 30, calcMaxPct(cbRam),
     85     *                     (uint64_t)(cbRam * calcMaxPct(cbRam) / 100.0) >> 20);
     86     *          return 0;
     87     *      }
     88     *
     89     * Note. We might wanna put these calculations somewhere global later. */
     90
     91    /* System RAM amount test */
     92    ulong fullSize = vboxGlobal().virtualBox().GetHost().GetMemorySize();
     93    mMaxRAMAllowed  = 0.75 * fullSize;
     94    mMaxRAMOptimum  = 0.50 * fullSize;
     95    if (fullSize < 3072)
     96        /* done */;
     97    else if (fullSize < 4096)   /* 3GB */
     98        mMaxRAMAllowed = 0.80 * fullSize;
     99    else if (fullSize < 6144)   /* 4-5GB */
     100    {
     101        mMaxRAMAllowed = 0.84 * fullSize;
     102        mMaxRAMOptimum = 0.60 * fullSize;
     103    }
     104    else if (fullSize < 8192)   /* 6-7GB */
     105    {
     106        mMaxRAMAllowed = 0.88 * fullSize;
     107        mMaxRAMOptimum = 0.65 * fullSize;
     108    }
     109    else if (fullSize < 16384)  /* 8-15GB */
     110    {
     111        mMaxRAMAllowed = 0.90 * fullSize;
     112        mMaxRAMOptimum = 0.70 * fullSize;
     113    }
     114    else if (fullSize < 32768)  /* 16-31GB */
     115    {
     116        mMaxRAMAllowed = 0.93 * fullSize;
     117        mMaxRAMOptimum = 0.75 * fullSize;
     118    }
     119    else if (fullSize < 65536)  /* 32-63GB */
     120    {
     121        mMaxRAMAllowed = 0.94 * fullSize;
     122        mMaxRAMOptimum = 0.80 * fullSize;
     123    }
     124    else if (fullSize < 131072) /* 64-127GB */
     125    {
     126        mMaxRAMAllowed = 0.95 * fullSize;
     127        mMaxRAMOptimum = 0.85 * fullSize;
     128    }
     129    else                        /* 128GB- */
     130    {
     131        mMaxRAMAllowed = 0.96 * fullSize;
     132        mMaxRAMOptimum = 0.90 * fullSize;
     133    }
    61134
    62135    /* Setup validators */
     
    103176    mSlMemory->setMinimum ((mMinGuestRAM / mSlMemory->pageStep()) * mSlMemory->pageStep());
    104177    mSlMemory->setMaximum (mMaxGuestRAM);
     178    mSlMemory->setSnappingEnabled (true);
     179    mSlMemory->setOptimalHint (1, mMaxRAMOptimum);
     180    mSlMemory->setWarningHint (mMaxRAMOptimum, mMaxRAMAllowed);
     181    mSlMemory->setErrorHint (mMaxRAMAllowed, mMaxGuestRAM);
    105182    /* Limit min/max. size of QLineEdit */
    106183    mLeMemory->setFixedWidthByText (QString().fill ('8', 5));
     
    115192    mSlCPU->setMinimum (mMinGuestCPU);
    116193    mSlCPU->setMaximum (mMaxGuestCPU);
     194    mSlCPU->setOptimalHint (1, hostCPUs);
     195    mSlCPU->setWarningHint (hostCPUs, mMaxGuestCPU);
    117196    /* Limit min/max. size of QLineEdit */
    118197    mLeCPU->setFixedWidthByText (QString().fill ('8', 3));
     
    274353bool VBoxVMSettingsSystem::revalidate (QString &aWarning, QString & /* aTitle */)
    275354{
    276     /* Come up with some nice round percent boundraries relative to
    277      * the system memory. A max of 75% on a 256GB config is ridiculous,
    278      * even on an 8GB rig reserving 2GB for the OS is way to conservative.
    279      * The max numbers can be estimated using the following program:
    280      *
    281      *      double calcMaxPct(uint64_t cbRam)
    282      *      {
    283      *          double cbRamOverhead = cbRam * 0.0390625; // 160 bytes per page.
    284      *          double cbRamForTheOS = RT_MAX(RT_MIN(_512M, cbRam * 0.25), _64M);
    285      *          double OSPct  = (cbRamOverhead + cbRamForTheOS) * 100.0 / cbRam;
    286      *          double MaxPct = 100 - OSPct;
    287      *          return MaxPct;
    288      *      }
    289      *
    290      *      int main()
    291      *      {
    292      *          uint64_t cbRam = _1G;
    293      *          for (; !(cbRam >> 33); cbRam += _1G)
    294      *              printf("%8lluGB %.1f%% %8lluKB\n", cbRam >> 30, calcMaxPct(cbRam),
    295      *                     (uint64_t)(cbRam * calcMaxPct(cbRam) / 100.0) >> 20);
    296      *          for (; !(cbRam >> 51); cbRam <<= 1)
    297      *              printf("%8lluGB %.1f%% %8lluKB\n", cbRam >> 30, calcMaxPct(cbRam),
    298      *                     (uint64_t)(cbRam * calcMaxPct(cbRam) / 100.0) >> 20);
    299      *          return 0;
    300      *      }
    301      *
    302      * Note. We might wanna put these calculations somewhere global later. */
    303 
    304     /* System RAM amount test */
    305355    ulong fullSize = vboxGlobal().virtualBox().GetHost().GetMemorySize();
    306     double maxPct  = 0.75;
    307     double warnPct = 0.50;
    308     if (fullSize < 3072)
    309         /* done */;
    310     else if (fullSize < 4096)   /* 3GB */
    311         maxPct  = 0.80;
    312     else if (fullSize < 6144)   /* 4-5GB */
    313     {
    314         maxPct  = 0.84;
    315         warnPct = 0.60;
    316     }
    317     else if (fullSize < 8192)   /* 6-7GB */
    318     {
    319         maxPct  = 0.88;
    320         warnPct = 0.65;
    321     }
    322     else if (fullSize < 16384)  /* 8-15GB */
    323     {
    324         maxPct  = 0.90;
    325         warnPct = 0.70;
    326     }
    327     else if (fullSize < 32768)  /* 16-31GB */
    328     {
    329         maxPct  = 0.93;
    330         warnPct = 0.75;
    331     }
    332     else if (fullSize < 65536)  /* 32-63GB */
    333     {
    334         maxPct  = 0.94;
    335         warnPct = 0.80;
    336     }
    337     else if (fullSize < 131072) /* 64-127GB */
    338     {
    339         maxPct  = 0.95;
    340         warnPct = 0.85;
    341     }
    342     else                        /* 128GB- */
    343     {
    344         maxPct  = 0.96;
    345         warnPct = 0.90;
    346     }
    347 
    348     if (mSlMemory->value() > maxPct * fullSize)
     356    if (mSlMemory->value() > mMaxRAMAllowed)
    349357    {
    350358        aWarning = tr (
     
    352360            "(<b>%2</b>) to the virtual machine. Not enough memory is left "
    353361            "for your host operating system. Please select a smaller amount.")
    354             .arg ((unsigned)(maxPct * 100))
     362            .arg ((unsigned)qRound ((double)mMaxRAMAllowed / fullSize * 100.0))
    355363            .arg (vboxGlobal().formatSize ((uint64_t)fullSize * _1M));
    356364        return false;
    357365    }
    358     if (mSlMemory->value() > warnPct * fullSize)
     366    if (mSlMemory->value() > mMaxRAMOptimum)
    359367    {
    360368        aWarning = tr (
     
    362370            "(<b>%2</b>) to the virtual machine. Not enough memory might be "
    363371            "left for your host operating system. Continue at your own risk.")
    364             .arg ((unsigned)(warnPct * 100))
     372            .arg ((unsigned)qRound ((double)mMaxRAMOptimum / fullSize * 100.0))
    365373            .arg (vboxGlobal().formatSize ((uint64_t)fullSize * _1M));
    366374        return true;
  • trunk/src/VBox/Frontends/VirtualBox/ui/VBoxVMSettingsSystem.ui

    r21119 r21194  
    397397         </property>
    398398         <item>
    399           <widget class="QSlider" name="mSlCPU">
     399          <widget class="QIAdvancedSlider" name="mSlCPU">
    400400           <property name="whatsThis">
    401401            <string>Controls the number of virtual CPUs in the virtual machine.</string>
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette