rock_widget_collection  0.1
BoundarySlider.cc
Go to the documentation of this file.
1 #include "BoundarySlider.h"
2 #include "SlideBarItem.h"
3 
4 #include <iostream>
5 
6 #include <base-logging/Logging.hpp>
7 
9 
10 }
11 
12 BoundarySlider::BoundarySlider(QGraphicsItem* parent, unsigned initIdx) : Slider(parent, initIdx) {
13  setPixmap(QPixmap(":/timeline/triangle_slider"));
14 
15  /* Set item coordinate origin to the x_center and let the slider tip overlap the slidebar a little.
16  * So the origin is a little above the y_center. */
17  setOffset( -0.5 * width() - 1, 0.2 * height() );
18 }
19 
21  if(this->getInitIdx() > otherMarker->getInitIdx()) {
22  return RIGHT;
23  }
24  else {
25  return LEFT;
26  }
27 }
28 
29 void BoundarySlider::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
30  /* Sliding bounds in parent's coords */
31  qreal leftBoundary = parentItem()->boundingRect().left();
32  qreal rightBoundary = parentItem()->boundingRect().right();
33 
34  QPointF newPos = boundarySnapPos(event->pos(), getClickPosOffsetX(event), leftBoundary, rightBoundary);
35 
36  // Check for marker overlap with new position
37  setPos(newPos);
38  if( BoundarySlider *overlappingMarker = ((SlideBarItem*) parentItem())->markerOverlap(this) ) {
39  /* Collision detected! Do not move further in this direction! */
40 
41  LOG_DEBUG_S << "Overlapping time markers!";
42 
43  qreal overlapLeftBoundary;
44  qreal overlapRightBoundary;
45  switch(leftRight(overlappingMarker)) {
46  case LEFT:
47  // the overlapping marker is right hand side
48  overlapLeftBoundary = leftBoundary;
49  overlapRightBoundary = overlappingMarker->pos().x();
50  break;
51  case RIGHT:
52  // the overlapping marker is left hand side
53  overlapLeftBoundary = overlappingMarker->pos().x();
54  overlapRightBoundary = rightBoundary;
55  break;
56  default :
57  std::cerr << "Unknown direction!" << std::endl;
58  exit(EXIT_FAILURE);
59  }
60 
61  /* Update position */
62  setPos(boundarySnapPos(event->pos(), getClickPosOffsetX(event), overlapLeftBoundary, overlapRightBoundary));
63 
64  } else {
65  // No overlap, new position is safe to use.
66  }
67  unsigned index = ((SlideBarItem*)parentItem())->markerIndex(this);
68  setLastIndex(index);
69 
70 // std::cout << "Logical marker position: " << index << std::endl;
71  //std::cout << "Computed real position for that index: " << ((SlideBarItem*)parentItem())->markerPositionForIndex(index) << std::endl;
72 // std::cout << "boundary slider pos.x: " << pos().x() << std::endl;
73  parentItem()->update(); // updates index slider as well
74 }
qreal getClickPosOffsetX(QGraphicsSceneMouseEvent *event)
Definition: Slider.cc:38
Definition: Slider.h:7
Direction
Definition: BoundarySlider.h:6
QPointF boundarySnapPos(QPointF eventPos, qreal clickPosOffsetX, qreal leftBoundary, qreal rightBoundary)
Definition: Slider.cc:18
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
unsigned getInitIdx()
Definition: Slider.cc:14
unsigned initIdx
Definition: Slider.h:34
Direction leftRight(BoundarySlider *otherMarker)
virtual int width() const
Definition: Slider.cc:47
void setLastIndex(unsigned index)
Definition: Slider.cc:55
virtual int height() const
Definition: Slider.cc:43