rock_widget_collection  0.1
SlideBarItem.cc
Go to the documentation of this file.
1 #include "SlideBarItem.h"
2 
3 #include <QtGui>
4 
5 #include <iostream>
6 
7 #include <base-logging/Logging.hpp>
8 
9 SlideBarItem::SlideBarItem(unsigned startIndex, unsigned steps, unsigned stepSize, QGraphicsItem *parent) :
10  QGraphicsItem(parent),height(10),width(100), color(QColor(Qt::gray))
11 {
12  reconfigure(startIndex, steps, stepSize);
13  slider = new IndexSlider(this, startIndex);
14  allSliders.append(slider);
15 }
16 
17 void SlideBarItem::reconfigure(unsigned startIndex, unsigned steps, unsigned stepSize) {
18  LOG_DEBUG("Reconfiguring slidebar: startIndex=%d, steps=%d, stepSize=%d\n", startIndex, steps, stepSize);
19  this->startIndex = startIndex;
20  this->steps = steps;
21  this->stepSize = stepSize;
22 }
23 
25  return QRectF(0, 0, getWidth(), getHeight());
26 }
27 
28 void SlideBarItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
29  Q_UNUSED(option);
30  Q_UNUSED(widget);
31  painter->setPen(QPen(Qt::black, 1));
32  painter->setBrush(QBrush(color));
33  int center = getHeight() * 0.5;
34  painter->fillRect(0, center, getWidth(), HEIGHT, color);
35  Q_FOREACH(unsigned bookmark, bookmarks) {
36  int x_pos = markerPositionForIndex(bookmark);
37  int y_top = center-(getBookmarkHeight()/2)+SlideBarItem::HEIGHT/2;
38  int y_bottom = y_top + getBookmarkHeight();
39  painter->drawLine(x_pos, y_top, x_pos, y_bottom);
40  }
41 }
42 
43 unsigned SlideBarItem::getStartIndex() const {
44  return startIndex;
45 }
46 
47 
49  return bookmark_height;
50 }
51 
52 void SlideBarItem::setBookmarkHeight(qreal bookmark_height) {
53  this->bookmark_height = bookmark_height;
54 }
55 
56 void SlideBarItem::setWidth(int width) {
57  this->width = width;
58 }
59 
60 void SlideBarItem::setHeight(int height) {
61  this->height = height;
62 }
63 
65  return width;
66 }
67 
69  return height;
70 }
71 
73  timeMarkers.append(marker);
74  allSliders.append(marker);
75  marker->setLastIndex(markerIndex(marker));
76 }
77 
78 const QList<BoundarySlider*>& SlideBarItem::getTimeMarkers() {
79  return timeMarkers;
80 }
81 
82 const QList<Slider*>& SlideBarItem::getAllSliders() {
83  return allSliders;
84 }
85 
87  Q_FOREACH(BoundarySlider* otherMarker, timeMarkers) {
88  if(otherMarker == marker) {
89  continue;
90  }
91 
92  bool overlap = false;
93 
94  switch(marker->leftRight(otherMarker)) {
95  case RIGHT :
96  // marker is right hand side
97  if(marker->pos().x() < otherMarker->pos().x()) {
98  overlap = true;
99  }
100  break;
101  case LEFT :
102  // marker is left hand side or right on top
103  if(marker->pos().x() >= otherMarker->pos().x()) {
104  overlap = true;
105  }
106  break;
107  default :
108  std::cerr << "Unknown direction!" << std::endl;
109  exit(EXIT_FAILURE);
110  }
111 
112  if(marker->collidesWithItem(otherMarker) || overlap) {
113  return otherMarker;
114  }
115  }
116  return NULL;
117 }
118 
119 bool SlideBarItem::markerContainsPoint(BoundarySlider *marker, const QPointF &point) {
120  Q_FOREACH(BoundarySlider* otherMarker, timeMarkers) {
121  if(otherMarker == marker) {
122  continue;
123  }
124  if(otherMarker->contains(point)) {
125  return true;
126  }
127  }
128  return false;
129 }
130 
132  //std::cout << "slidebar pos.x()" << pos().x() << std::endl;
133  //std::cout << "marker pos.x()" << marker->pos().x()
134  return (unsigned) QStyle::sliderValueFromPosition(startIndex, steps*stepSize, marker->pos().x(), getWidth());
135 }
136 
138  return QStyle::sliderPositionFromValue(startIndex, steps*stepSize, index, getWidth());
139 }
140 
142  return slider;
143 }
144 
145 void SlideBarItem::updateBookmarks(QVector<int> & bookmarks) {
146  this->bookmarks = bookmarks;
147  update();
148 }
149 
150 void SlideBarItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
151  // Check if bookmark was clicked
152  Q_FOREACH(int bookmark, bookmarks) {
153  // Define click region for bookmarks
154  QRectF clickRegion(markerPositionForIndex(bookmark)-2, pos().y() - (getBookmarkHeight()/2.0), 5, getBookmarkHeight());
155  if(clickRegion.contains(event->pos())) {
156  // Bookmark clicked!
157  emit bookmarkClicked(bookmark);
158  break;
159  }
160  }
161 }
Definition: Slider.h:7
const QList< Slider * > & getAllSliders()
Definition: SlideBarItem.cc:82
int markerPositionForIndex(unsigned index)
SlideBarItem(unsigned startIndex, unsigned steps, unsigned stepSize, QGraphicsItem *parent=0)
Definition: SlideBarItem.cc:9
void reconfigure(unsigned startIndex, unsigned steps, unsigned stepSize)
Definition: SlideBarItem.cc:17
unsigned markerIndex(Slider *marker)
qreal getBookmarkHeight() const
Definition: SlideBarItem.cc:48
void setHeight(int width)
Definition: SlideBarItem.cc:60
void updateBookmarks(QVector< int > &bookmarks)
const QList< BoundarySlider * > & getTimeMarkers()
Definition: SlideBarItem.cc:78
static const int HEIGHT
Definition: SlideBarItem.h:15
void addTimeMarker(BoundarySlider *marker)
Definition: SlideBarItem.cc:72
BoundarySlider * markerOverlap(BoundarySlider *marker)
Definition: SlideBarItem.cc:86
void bookmarkClicked(int idx)
Direction leftRight(BoundarySlider *otherMarker)
unsigned getStartIndex() const
Definition: SlideBarItem.cc:43
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: SlideBarItem.cc:28
IndexSlider * getIndexSlider()
int getWidth() const
Definition: SlideBarItem.cc:64
void setBookmarkHeight(qreal height)
Definition: SlideBarItem.cc:52
void mousePressEvent(QGraphicsSceneMouseEvent *event)
int getHeight() const
Definition: SlideBarItem.cc:68
bool markerContainsPoint(BoundarySlider *marker, const QPointF &point)
void setLastIndex(unsigned index)
Definition: Slider.cc:55
void setWidth(int width)
Definition: SlideBarItem.cc:56
QRectF boundingRect() const
Definition: SlideBarItem.cc:24