Hi,
I've got a small view (AsaNavigationView) and a big view. The small view displays a rectangle which shows the current position of the big view compared to the big scene. Just like in Photoshop or any other progam with such a navigationarea.
#1 is the navigationview, #2 and #3 are the scrollbars of the big view.
So there are some (related?) problems:
If I click and drag the red rectangle all the way up or to the left side, it stops at the views border, but the scrollbars are not at the top or the left (#2).
If I click and drag the rectange to the right or to the bottom it does not stop at the borders but about 5pix behind. The horizontal scrollbar reaches its end when the rectangle is at its "new" border, the vertical scrollbar reaches its end when the rectange is at the actual broder (but u are still able to drag ne rectange 5px further, but it takes no visible effect).
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
void AsaNavigationView::mouseMoveEvent(QMouseEvent *event){
QGraphicsView::mouseMoveEvent(event);
PositionFrame->setX(event->x()-FrameWidth/2);
PositionFrame->setY(event->y()-FrameHeight/2);
checkPositionValidity();
QPointF middle( (PositionFrame->x()+FrameWidth/2)*SizeFactor*ScaleFactor, (PositionFrame->y()+FrameHeight/2)*SizeFactor*ScaleFactor);
emit movePositionFrame(middle); //send the centerPosition of the red rectangle to the big View
}
void AsaNavigationView::setSize(qreal w,qreal h){ //this is called in the beginnin or if the size of the big scene has changed
const int FixedMaxWidth = 150;
const int FixedMaxHeight = 100;
SizeFactor = w/FixedMaxWidth;
if( int(h/SizeFactor) < FixedMaxWidth ){
this->setFixedSize( 150, h/SizeFactor );
}
else{
SizeFactor = h/FixedMaxHeight;
this->setFixedSize( w/SizeFactor, 100);
}
}
void AsaNavigationView::setFrameSize(qreal w, qreal h){
InitialFrameWidth = int(w/SizeFactor); //since there are a lot of multiplications width float and integer there would be a great loss due to rounding so I keep a initial size
InitialFrameHeight= int(h/SizeFactor);
FrameWidth = InitialFrameWidth/ScaleFactor;
FrameHeight= InitialFrameHeight/ScaleFactor;
drawFrame();
}
void AsaNavigationView::setScaleFactor(float s){
ScaleFactor=s;
FrameWidth = InitialFrameWidth/ScaleFactor;
FrameHeight= InitialFrameHeight/ScaleFactor;
drawFrame();
}
void AsaNavigationView::setFramePosition(const QPointF &pos){
QPointF MiddlePosition;
MiddlePosition.setX(pos.x()/SizeFactor/ScaleFactor);
MiddlePosition.setY(pos.y()/SizeFactor/ScaleFactor);
QPointF TopRightCorner;
TopRightCorner.setX(MiddlePosition.x()-FrameWidth/2);
TopRightCorner.setY(MiddlePosition.y()-FrameHeight/2);
PositionFrame->setPos(TopRightCorner);
}
void AsaNavigationView::checkPositionValidity(){ ////////////here is probably the cause of the errors
QPointF TopLeftFrameCorner = this->mapFromScene( PositionFrame->x(), PositionFrame->y() );
QPointF BottomRightFrameCorner = this->mapFromScene( PositionFrame->x()+FrameWidth, PositionFrame->y()+FrameHeight );
QPointF TopLeftSceneCorner = this->mapToScene(0,0);
QPointF BottomRightSceneCorner = this->mapToScene( this->width(), this->height() );
if( TopLeftFrameCorner.x() <= TopLeftSceneCorner.x() )
PositionFrame->setX( TopLeftSceneCorner.x() );
if(TopLeftFrameCorner.y() <= TopLeftSceneCorner.y())
PositionFrame->setY(TopLeftSceneCorner.y() );
if( BottomRightFrameCorner.x() >= BottomRightSceneCorner.x() ){
PositionFrame->setX( BottomRightSceneCorner.x()-FrameWidth);
}
if( BottomRightFrameCorner.y() >= BottomRightSceneCorner.y() )
PositionFrame->setY( BottomRightSceneCorner.y()-FrameHeight );
}
void AsaNavigationView::drawFrame(){
QPixmap TempRect(FrameWidth, FrameHeight);
QPainter painter(&TempRect);
painter.setPen( QPen(Qt::red, 1) );
painter.fillRect(0,0,FrameWidth,FrameHeight, Qt::green);
painter.drawRect(0,0,FrameWidth-1,FrameHeight-1);
PositionFrame->setPixmap(TempRect);
checkPositionValidity();
}
|
Hope you can help me