00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "markedscrollbar.h"
00020
00021 #include <QPainter>
00022 #include <QStyle>
00023 #include <QStyleOptionSlider>
00024
00048 MarkedScrollBar::MarkedScrollBar(QWidget *parent)
00049 : QScrollBar(parent), m_isClipped(true)
00050 {
00051 }
00052
00061 void MarkedScrollBar::addMark(int position, const QColor& colour,
00062 const QString& identifier)
00063 {
00064 markData mark = { position, colour, identifier };
00065 m_marks.append(mark);
00066 }
00067
00073 void MarkedScrollBar::removeMark(int position)
00074 {
00075 QMutableListIterator<markData> i(m_marks);
00076 while (i.hasNext())
00077 {
00078 if (i.next().pos == position)
00079 {
00080 i.remove();
00081 break;
00082 }
00083 }
00084 }
00085
00091 void MarkedScrollBar::removeMark(const QString& identifier)
00092 {
00093 QMutableListIterator<markData> i(m_marks);
00094 while (i.hasNext())
00095 {
00096 if (i.next().identifier == identifier)
00097 {
00098 i.remove();
00099 break;
00100 }
00101 }
00102 }
00103
00107 void MarkedScrollBar::removeAllMarks()
00108 {
00109 m_marks.clear();
00110 }
00111
00117 void MarkedScrollBar::removeMarksLessThan(int position)
00118 {
00119 QMutableListIterator<markData> i(m_marks);
00120 while (i.hasNext())
00121 {
00122 int pos = i.next().pos;
00123 if (pos < position)
00124 {
00125 i.remove();
00126 }
00127 }
00128 }
00129
00135 void MarkedScrollBar::removeMarksGreaterThan(int position)
00136 {
00137 QMutableListIterator<markData> i(m_marks);
00138 while (i.hasNext())
00139 {
00140 int pos = i.next().pos;
00141 if (pos > position)
00142 {
00143 i.remove();
00144 }
00145 }
00146 }
00147
00154 void MarkedScrollBar::removeMarksBetween(int start, int end)
00155 {
00156 if (start > end)
00157 {
00158 qWarning("MarkedScrollBar::removeMarksBetween: start > end");
00159 }
00160
00161 QMutableListIterator<markData> i(m_marks);
00162 while (i.hasNext())
00163 {
00164 int pos = i.next().pos;
00165 if (pos > start && pos < end)
00166 {
00167 i.remove();
00168 }
00169 }
00170 }
00171
00180 void MarkedScrollBar::addShade(int startPos, int endPos, const QColor& colour,
00181 const QString& identifier)
00182 {
00183 if (startPos > endPos)
00184 {
00185 qWarning("MarkedScrollBar::addShade: bad argument, startPos > endPos");
00186 }
00187
00188 shadeData shade = { startPos, endPos, colour, identifier };
00189 m_shades.append(shade);
00190 }
00191
00197 void MarkedScrollBar::removeShade(const QString &identifier)
00198 {
00199 QMutableListIterator<shadeData> i(m_shades);
00200 while (i.hasNext())
00201 {
00202 if (i.next().identifier == identifier)
00203 {
00204 i.remove();
00205 break;
00206 }
00207 }
00208 }
00209
00213 void MarkedScrollBar::removeAllShades()
00214 {
00215 m_shades.clear();
00216 }
00217
00222 bool MarkedScrollBar::isClipped() const
00223 {
00224 return m_isClipped;
00225 }
00226
00231 void MarkedScrollBar::enableClipping(bool clip)
00232 {
00233 m_isClipped = clip;
00234 }
00235
00236 void MarkedScrollBar::paintEvent(QPaintEvent *event)
00237 {
00238
00239 QScrollBar::paintEvent(event);
00240
00241
00242 QStyleOptionSlider styleOption;
00243 initStyleOption(&styleOption);
00244
00245
00246 QRect addPage = style()->subControlRect(QStyle::CC_ScrollBar, &styleOption,
00247 QStyle::SC_ScrollBarAddPage, this);
00248 QRect subPage = style()->subControlRect(QStyle::CC_ScrollBar, &styleOption,
00249 QStyle::SC_ScrollBarSubPage, this);
00250 QRect slider = style()->subControlRect(QStyle::CC_ScrollBar, &styleOption,
00251 QStyle::SC_ScrollBarSlider, this);
00252
00253
00254
00255 QPainter p(this);
00256
00257
00258 if (isClipped())
00259 {
00260 p.setClipRegion(QRegion(addPage) + QRegion(subPage));
00261 }
00262
00263
00264 qreal sf;
00265
00266
00267 if (orientation() == Qt::Horizontal)
00268 {
00269 p.translate(qMin(subPage.left(), addPage.left()), 0.0);
00270
00271 sf = (addPage.width() + subPage.width() + slider.width())
00272 / ((qreal) maximum() - minimum());
00273 }
00274 else
00275 {
00276 p.translate(0.0, qMin(subPage.top(), addPage.top()));
00277
00278 sf = (addPage.height() + subPage.height() + slider.height())
00279 / ((qreal) maximum() - minimum());
00280 }
00281
00282 foreach (markData mark, m_marks)
00283 {
00284 p.setPen(mark.colour);
00285
00286 if (orientation() == Qt::Horizontal)
00287 {
00288 p.drawLine(QPoint(mark.pos * sf, 2),
00289 QPoint(mark.pos * sf, slider.height() - 3));
00290 }
00291 else if (orientation() == Qt::Vertical)
00292 {
00293 p.drawLine(QPoint(2, mark.pos * sf),
00294 QPoint(slider.width() - 3, mark.pos * sf));
00295 }
00296 }
00297
00298 foreach (shadeData shade, m_shades)
00299 {
00300 QColor penColour = shade.colour;
00301 QColor brushColour = shade.colour;
00302 brushColour.setAlphaF(brushColour.alphaF() * 0.45);
00303
00304 p.setPen(penColour);
00305 p.setBrush(brushColour);
00306
00307 if (orientation() == Qt::Horizontal)
00308 {
00309 p.drawRect(QRect(QPoint(shade.start * sf, 2),
00310 QPoint(shade.end * sf, slider.height() - 4)));
00311
00312 }
00313 else if (orientation() == Qt::Vertical)
00314 {
00315 p.drawRect(QRect(QPoint(2, shade.start * sf),
00316 QPoint(slider.width() - 4, shade.end * sf)));
00317 }
00318 }
00319 }
00320