Changeset 26712 in vbox for trunk/src/VBox/Frontends
- Timestamp:
- Feb 23, 2010 2:39:23 PM (15 years ago)
- Location:
- trunk/src/VBox/Frontends/VirtualBox/src/extensions
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIAdvancedSlider.cpp
r25177 r26712 1 /* $Id$ */ 1 2 /** @file 2 3 * … … 6 7 7 8 /* 8 * Copyright (C) 2009 Sun Microsystems, Inc.9 * Copyright (C) 2009-2010 Sun Microsystems, Inc. 9 10 * 10 11 * This file is part of VirtualBox Open Source Edition (OSE), as … … 36 37 { 37 38 public: 38 CPrivateSlider (Qt::Orientation aOrientation, QWidget *aParent = 0)39 : QSlider (aOrientation, aParent)40 , m OptColor(0x0, 0xff, 0x0, 0x3c)41 , m WrnColor(0xff, 0x54, 0x0, 0x3c)42 , m ErrColor(0xff, 0x0, 0x0, 0x3c)43 , m MinOpt(-1)44 , m MaxOpt(-1)45 , m MinWrn(-1)46 , m MaxWrn(-1)47 , m MinErr(-1)48 , m MaxErr(-1)39 CPrivateSlider(Qt::Orientation fOrientation, QWidget *pParent = 0) 40 : QSlider(fOrientation, pParent) 41 , m_optColor(0x0, 0xff, 0x0, 0x3c) 42 , m_wrnColor(0xff, 0x54, 0x0, 0x3c) 43 , m_errColor(0xff, 0x0, 0x0, 0x3c) 44 , m_minOpt(-1) 45 , m_maxOpt(-1) 46 , m_minWrn(-1) 47 , m_maxWrn(-1) 48 , m_minErr(-1) 49 , m_maxErr(-1) 49 50 {} 50 51 51 int positionForValue (int aVal) const52 int positionForValue(int val) const 52 53 { 53 54 QStyleOptionSlider opt; 54 initStyleOption 55 initStyleOption(&opt); 55 56 opt.subControls = QStyle::SC_All; 56 int available = opt.rect.width() - style()->pixelMetric 57 return QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, aVal, available);57 int available = opt.rect.width() - style()->pixelMetric(QStyle::PM_SliderLength, &opt, this); 58 return QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, val, available); 58 59 } 59 60 60 virtual void paintEvent (QPaintEvent *aEvent)61 virtual void paintEvent(QPaintEvent *pEvent) 61 62 { 62 63 QPainter p(this); 63 64 64 65 QStyleOptionSlider opt; 65 initStyleOption 66 initStyleOption(&opt); 66 67 opt.subControls = QStyle::SC_All; 67 68 68 int available = opt.rect.width() - style()->pixelMetric 69 int available = opt.rect.width() - style()->pixelMetric(QStyle::PM_SliderLength, &opt, this); 69 70 QSize s = size(); 70 71 71 QRect ticks = style()->subControlRect 72 QRect ticks = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderTickmarks, this); 72 73 #ifdef Q_WS_MAC 73 ticks.setRect 74 ticks.setRect((s.width() - available) / 2, s.height() - ticks.y(), available, ticks.height()); 74 75 #else /* Q_WS_MAC */ 75 76 if (ticks.isNull() || ticks.isEmpty()) 76 77 { 77 ticks = style()->subControlRect (QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this) | style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);78 ticks.setRect 78 ticks = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this) | style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this); 79 ticks.setRect((s.width() - available) / 2, ticks.bottom() + 1, available, s.height() - ticks.bottom() - 1); 79 80 } 80 81 #endif /* Q_WS_MAC */ 81 if ((m MinOpt != -1 &&82 m MaxOpt != -1) &&83 m MinOpt != mMaxOpt)84 { 85 int posMinOpt = QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, mMinOpt, available);86 int posMaxOpt = QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, mMaxOpt, available);87 p.fillRect (ticks.x() + posMinOpt, ticks.y(), posMaxOpt - posMinOpt + 1, ticks.height(), mOptColor);88 } 89 if ((m MinWrn != -1 &&90 m MaxWrn != -1) &&91 m MinWrn != mMaxWrn)92 { 93 int posMinWrn = QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, mMinWrn, available);94 int posMaxWrn = QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, mMaxWrn, available);95 p.fillRect (ticks.x() + posMinWrn, ticks.y(), posMaxWrn - posMinWrn + 1, ticks.height(), mWrnColor);96 } 97 if ((m MinErr != -1 &&98 m MaxErr != -1) &&99 m MinErr != mMaxErr)100 { 101 int posMinErr = QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, mMinErr, available);102 int posMaxErr = QStyle::sliderPositionFromValue (opt.minimum, opt.maximum, mMaxErr, available);103 p.fillRect (ticks.x() + posMinErr, ticks.y(), posMaxErr - posMinErr + 1, ticks.height(), mErrColor);82 if ((m_minOpt != -1 && 83 m_maxOpt != -1) && 84 m_minOpt != m_maxOpt) 85 { 86 int posMinOpt = QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, m_minOpt, available); 87 int posMaxOpt = QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, m_maxOpt, available); 88 p.fillRect(ticks.x() + posMinOpt, ticks.y(), posMaxOpt - posMinOpt + 1, ticks.height(), m_optColor); 89 } 90 if ((m_minWrn != -1 && 91 m_maxWrn != -1) && 92 m_minWrn != m_maxWrn) 93 { 94 int posMinWrn = QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, m_minWrn, available); 95 int posMaxWrn = QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, m_maxWrn, available); 96 p.fillRect(ticks.x() + posMinWrn, ticks.y(), posMaxWrn - posMinWrn + 1, ticks.height(), m_wrnColor); 97 } 98 if ((m_minErr != -1 && 99 m_maxErr != -1) && 100 m_minErr != m_maxErr) 101 { 102 int posMinErr = QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, m_minErr, available); 103 int posMaxErr = QStyle::sliderPositionFromValue(opt.minimum, opt.maximum, m_maxErr, available); 104 p.fillRect(ticks.x() + posMinErr, ticks.y(), posMaxErr - posMinErr + 1, ticks.height(), m_errColor); 104 105 } 105 106 p.end(); 106 107 107 QSlider::paintEvent (aEvent);108 QSlider::paintEvent(pEvent); 108 109 } 109 110 110 QColor m OptColor;111 QColor m WrnColor;112 QColor m ErrColor;113 114 int m MinOpt;115 int m MaxOpt;116 int m MinWrn;117 int m MaxWrn;118 int m MinErr;119 int m MaxErr;111 QColor m_optColor; 112 QColor m_wrnColor; 113 QColor m_errColor; 114 115 int m_minOpt; 116 int m_maxOpt; 117 int m_minWrn; 118 int m_maxWrn; 119 int m_minErr; 120 int m_maxErr; 120 121 }; 121 122 122 QIAdvancedSlider::QIAdvancedSlider (QWidget *aParent /* = 0 */)123 : QWidget (aParent)123 QIAdvancedSlider::QIAdvancedSlider(QWidget *pParent /* = 0 */) 124 : QWidget(pParent) 124 125 { 125 126 init(); 126 127 } 127 128 128 QIAdvancedSlider::QIAdvancedSlider (Qt::Orientation aOrientation, QWidget *aParent /* = 0 */)129 : QWidget (aParent)130 { 131 init (aOrientation);129 QIAdvancedSlider::QIAdvancedSlider(Qt::Orientation fOrientation, QWidget *pParent /* = 0 */) 130 : QWidget(pParent) 131 { 132 init(fOrientation); 132 133 } 133 134 134 135 int QIAdvancedSlider::value() const 135 136 { 136 return m Slider->value();137 } 138 139 void QIAdvancedSlider::setRange (int aMinV, int aMaxV)140 { 141 m Slider->setRange (aMinV, aMaxV);142 } 143 144 void QIAdvancedSlider::setMaximum (int aVal)145 { 146 m Slider->setMaximum (aVal);137 return m_pSlider->value(); 138 } 139 140 void QIAdvancedSlider::setRange(int minV, int maxV) 141 { 142 m_pSlider->setRange(minV, maxV); 143 } 144 145 void QIAdvancedSlider::setMaximum(int val) 146 { 147 m_pSlider->setMaximum(val); 147 148 } 148 149 149 150 int QIAdvancedSlider::maximum() const 150 151 { 151 return m Slider->maximum();152 } 153 154 void QIAdvancedSlider::setMinimum (int aVal)155 { 156 m Slider->setMinimum (aVal);152 return m_pSlider->maximum(); 153 } 154 155 void QIAdvancedSlider::setMinimum(int val) 156 { 157 m_pSlider->setMinimum(val); 157 158 } 158 159 159 160 int QIAdvancedSlider::minimum() const 160 161 { 161 return m Slider->minimum();162 } 163 164 void QIAdvancedSlider::setPageStep (int aVal)165 { 166 m Slider->setPageStep (aVal);162 return m_pSlider->minimum(); 163 } 164 165 void QIAdvancedSlider::setPageStep(int val) 166 { 167 m_pSlider->setPageStep(val); 167 168 } 168 169 169 170 int QIAdvancedSlider::pageStep() const 170 171 { 171 return m Slider->pageStep();172 } 173 174 void QIAdvancedSlider::setSingleStep (int aVal)175 { 176 m Slider->setSingleStep (aVal);172 return m_pSlider->pageStep(); 173 } 174 175 void QIAdvancedSlider::setSingleStep(int val) 176 { 177 m_pSlider->setSingleStep(val); 177 178 } 178 179 179 180 int QIAdvancedSlider::singelStep() const 180 181 { 181 return m Slider->singleStep();182 } 183 184 void QIAdvancedSlider::setTickInterval (int aVal)185 { 186 m Slider->setTickInterval (aVal);182 return m_pSlider->singleStep(); 183 } 184 185 void QIAdvancedSlider::setTickInterval(int val) 186 { 187 m_pSlider->setTickInterval(val); 187 188 } 188 189 189 190 int QIAdvancedSlider::tickInterval() const 190 191 { 191 return m Slider->tickInterval();192 } 193 194 void QIAdvancedSlider::setTickPosition (QSlider::TickPosition aPos)195 { 196 m Slider->setTickPosition (aPos);192 return m_pSlider->tickInterval(); 193 } 194 195 void QIAdvancedSlider::setTickPosition(QSlider::TickPosition fPos) 196 { 197 m_pSlider->setTickPosition(fPos); 197 198 } 198 199 199 200 QSlider::TickPosition QIAdvancedSlider::tickPosition() const 200 201 { 201 return m Slider->tickPosition();202 return m_pSlider->tickPosition(); 202 203 } 203 204 204 205 Qt::Orientation QIAdvancedSlider::orientation() const 205 206 { 206 return m Slider->orientation();207 } 208 209 void QIAdvancedSlider::setSnappingEnabled (bool aOn)210 { 211 m SnappingEnabled = aOn;207 return m_pSlider->orientation(); 208 } 209 210 void QIAdvancedSlider::setSnappingEnabled(bool fOn) 211 { 212 m_fSnappingEnabled = fOn; 212 213 } 213 214 214 215 bool QIAdvancedSlider::isSnappingEnabled() const 215 216 { 216 return m SnappingEnabled;217 } 218 219 void QIAdvancedSlider::setOptimalHint (int aMin, int aMax)220 { 221 m Slider->mMinOpt = aMin;222 m Slider->mMaxOpt = aMax;223 } 224 225 void QIAdvancedSlider::setWarningHint (int aMin, int aMax)226 { 227 m Slider->mMinWrn = aMin;228 m Slider->mMaxWrn = aMax;229 } 230 231 void QIAdvancedSlider::setErrorHint (int aMin, int aMax)232 { 233 m Slider->mMinErr = aMin;234 m Slider->mMaxErr = aMax;235 } 236 237 void QIAdvancedSlider::setOrientation (Qt::Orientation aOrientation)238 { 239 m Slider->setOrientation (aOrientation);240 } 241 242 void QIAdvancedSlider::setValue (int aVal)243 { 244 m Slider->setValue (aVal);245 } 246 247 void QIAdvancedSlider:: prvSliderMoved(int val)217 return m_fSnappingEnabled; 218 } 219 220 void QIAdvancedSlider::setOptimalHint(int min, int max) 221 { 222 m_pSlider->m_minOpt = min; 223 m_pSlider->m_maxOpt = max; 224 } 225 226 void QIAdvancedSlider::setWarningHint(int min, int max) 227 { 228 m_pSlider->m_minWrn = min; 229 m_pSlider->m_maxWrn = max; 230 } 231 232 void QIAdvancedSlider::setErrorHint(int min, int max) 233 { 234 m_pSlider->m_minErr = min; 235 m_pSlider->m_maxErr = max; 236 } 237 238 void QIAdvancedSlider::setOrientation(Qt::Orientation fOrientation) 239 { 240 m_pSlider->setOrientation(fOrientation); 241 } 242 243 void QIAdvancedSlider::setValue (int val) 244 { 245 m_pSlider->setValue(val); 246 } 247 248 void QIAdvancedSlider::sltSliderMoved(int val) 248 249 { 249 250 val = snapValue(val); 250 m Slider->setValue(val);251 m_pSlider->setValue(val); 251 252 emit sliderMoved(val); 252 253 } 253 254 254 void QIAdvancedSlider::init (Qt::Orientation aOrientation /* = Qt::Horizontal */)255 { 256 m SnappingEnabled = false;257 258 QVBoxLayout *pMainLayout = new QVBoxLayout 259 VBoxGlobal::setLayoutMargin 260 m Slider = new CPrivateSlider (aOrientation, this);261 pMainLayout->addWidget (mSlider);262 263 connect(m Slider, SIGNAL (sliderMoved(int)), this, SLOT (prvSliderMoved(int)));264 connect(m Slider, SIGNAL (valueChanged(int)), this, SIGNAL(valueChanged(int)));265 connect(m Slider, SIGNAL (sliderPressed()), this, SIGNAL(sliderPressed()));266 connect(m Slider, SIGNAL (sliderReleased()), this, SIGNAL(sliderReleased()));255 void QIAdvancedSlider::init(Qt::Orientation fOrientation /* = Qt::Horizontal */) 256 { 257 m_fSnappingEnabled = false; 258 259 QVBoxLayout *pMainLayout = new QVBoxLayout(this); 260 VBoxGlobal::setLayoutMargin(pMainLayout, 0); 261 m_pSlider = new CPrivateSlider(fOrientation, this); 262 pMainLayout->addWidget(m_pSlider); 263 264 connect(m_pSlider, SIGNAL(sliderMoved(int)), this, SLOT(sltSliderMoved(int))); 265 connect(m_pSlider, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int))); 266 connect(m_pSlider, SIGNAL(sliderPressed()), this, SIGNAL(sliderPressed())); 267 connect(m_pSlider, SIGNAL(sliderReleased()), this, SIGNAL(sliderReleased())); 267 268 } 268 269 269 270 int QIAdvancedSlider::snapValue(int val) 270 271 { 271 if (m SnappingEnabled &&272 if (m_fSnappingEnabled && 272 273 val > 2) 273 274 { 274 float l2 = log ((float)val)/log(2.0);275 int newVal = (int) pow ((float)2, (int)qRound(l2)); /* The value to snap on */276 int pos = m Slider->positionForValue(val); /* Get the relative screen pos for the original value */277 int newPos = m Slider->positionForValue(newVal); /* Get the relative screen pos for the snap value */278 if (abs 275 float l2 = log((float)val)/log(2.0); 276 int newVal = (int)pow((float)2, (int)qRound(l2)); /* The value to snap on */ 277 int pos = m_pSlider->positionForValue(val); /* Get the relative screen pos for the original value */ 278 int newPos = m_pSlider->positionForValue(newVal); /* Get the relative screen pos for the snap value */ 279 if (abs(newPos - pos) < 5) /* 10 pixel snapping range */ 279 280 { 280 281 val = newVal; 281 if (val > m Slider->maximum())282 val = m Slider->maximum();283 else if (val < m Slider->minimum())284 val = m Slider->minimum();282 if (val > m_pSlider->maximum()) 283 val = m_pSlider->maximum(); 284 else if (val < m_pSlider->minimum()) 285 val = m_pSlider->minimum(); 285 286 } 286 287 } -
trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIAdvancedSlider.h
r26079 r26712 35 35 36 36 public: 37 QIAdvancedSlider (QWidget *aParent = 0);38 QIAdvancedSlider (Qt::Orientation aOrientation, QWidget *aParent = 0);37 QIAdvancedSlider(QWidget *pParent = 0); 38 QIAdvancedSlider(Qt::Orientation fOrientation, QWidget *pParent = 0); 39 39 40 40 int value() const; 41 41 42 void setRange (int aMinV, int aMaxV);42 void setRange(int minV, int maxV); 43 43 44 void setMaximum (int aVal);44 void setMaximum(int val); 45 45 int maximum() const; 46 46 47 void setMinimum (int aVal);47 void setMinimum(int val); 48 48 int minimum() const; 49 49 50 void setPageStep (int aVal);50 void setPageStep(int val); 51 51 int pageStep() const; 52 52 53 void setSingleStep (int aVal);53 void setSingleStep(int val); 54 54 int singelStep() const; 55 55 56 void setTickInterval (int aVal);56 void setTickInterval(int val); 57 57 int tickInterval() const; 58 58 59 void setTickPosition (QSlider::TickPosition aPos);59 void setTickPosition(QSlider::TickPosition fPos); 60 60 QSlider::TickPosition tickPosition() const; 61 61 62 62 Qt::Orientation orientation() const; 63 63 64 void setSnappingEnabled (bool aOn);64 void setSnappingEnabled(bool fOn); 65 65 bool isSnappingEnabled() const; 66 66 67 void setOptimalHint (int aMin, int aMax);68 void setWarningHint (int aMin, int aMax);69 void setErrorHint (int aMin, int aMax);67 void setOptimalHint(int min, int max); 68 void setWarningHint(int min, int max); 69 void setErrorHint(int min, int max); 70 70 71 71 public slots: 72 72 73 void setOrientation (Qt::Orientation aOrientation);74 void setValue (int aVal);73 void setOrientation(Qt::Orientation fOrientation); 74 void setValue(int val); 75 75 76 76 signals: 77 void valueChanged 78 void sliderMoved 77 void valueChanged(int); 78 void sliderMoved(int); 79 79 void sliderPressed(); 80 80 void sliderReleased(); … … 82 82 private slots: 83 83 84 void prvSliderMoved(int val);84 void sltSliderMoved(int val); 85 85 86 86 private: 87 87 88 void init (Qt::Orientation aOrientation = Qt::Horizontal);88 void init(Qt::Orientation fOrientation = Qt::Horizontal); 89 89 int snapValue(int val); 90 90 91 91 /* Private member vars */ 92 CPrivateSlider *m Slider;93 bool m SnappingEnabled;92 CPrivateSlider *m_pSlider; 93 bool m_fSnappingEnabled; 94 94 }; 95 95
Note:
See TracChangeset
for help on using the changeset viewer.