OpenCores
URL https://opencores.org/ocsvn/bw_tiff_compression/bw_tiff_compression/trunk

Subversion Repositories bw_tiff_compression

[/] [bw_tiff_compression/] [trunk/] [client_application/] [src/] [GUI/] [CMyGraphicsView.cpp] - Blame information for rev 16

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 amulder
/*
2
 * Thanks to the autor of http://www.qtcentre.org/wiki/index.php?title=QGraphicsView:_Smooth_Panning_and_Zooming
3
 */
4
 
5
//Our includes
6
#include "CMyGraphicsView.h"
7
#include "stddefs.h"
8
 
9
//Qt includes
10
#include <QGraphicsTextItem>
11
#include <QTextStream>
12
#include <QScrollBar>
13
#include <QMouseEvent>
14
#include <QWheelEvent>
15
#include <QDebug>
16
#include <QBrush>
17
 
18
/**
19
* Sets up the subclassed QGraphicsView
20
*/
21
CMyGraphicsView::CMyGraphicsView(QWidget* parent) : QGraphicsView(parent) {
22
 
23
        setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
24
 
25
        //Set-up the scene
26
        Scene = new QGraphicsScene(this);
27
    QBrush oBrush;
28
    oBrush.setColor(Qt::lightGray);
29
    oBrush.setStyle(Qt::SolidPattern);
30
    Scene->setBackgroundBrush(oBrush);
31
        setScene(Scene);
32
        setCursor(Qt::OpenHandCursor);
33
//    setBackgroundBrush();
34
}
35
 
36
/**
37
  * Sets the current centerpoint.  Also updates the scene's center point.
38
  * Unlike centerOn, which has no way of getting the floating point center
39
  * back, SetCenter() stores the center point.  It also handles the special
40
  * sidebar case.  This function will claim the centerPoint to sceneRec ie.
41
  * the centerPoint must be within the sceneRec.
42
  */
43
//Set the current centerpoint in the
44
void CMyGraphicsView::SetCenter(const QPointF& centerPoint) {
45
        //Get the rectangle of the visible area in scene coords
46
        QRectF visibleArea = mapToScene(rect()).boundingRect();
47
 
48
        //Get the scene area
49
        QRectF sceneBounds = sceneRect();
50
 
51
        double boundX = visibleArea.width() / 2.0;
52
        double boundY = visibleArea.height() / 2.0;
53
        double boundWidth = sceneBounds.width() - 2.0 * boundX;
54
        double boundHeight = sceneBounds.height() - 2.0 * boundY;
55
 
56
        //The max boundary that the centerPoint can be to
57
        QRectF bounds(boundX, boundY, boundWidth, boundHeight);
58
 
59
        if(bounds.contains(centerPoint)) {
60
                //We are within the bounds
61
                CurrentCenterPoint = centerPoint;
62
        } else {
63
                //We need to clamp or use the center of the screen
64
                if(visibleArea.contains(sceneBounds)) {
65
                        //Use the center of scene ie. we can see the whole scene
66
                        CurrentCenterPoint = sceneBounds.center();
67
                } else {
68
 
69
                        CurrentCenterPoint = centerPoint;
70
 
71
                        //We need to clamp the center. The centerPoint is too large
72
                        if(centerPoint.x() > bounds.x() + bounds.width()) {
73
                                CurrentCenterPoint.setX(bounds.x() + bounds.width());
74
                        } else if(centerPoint.x() < bounds.x()) {
75
                                CurrentCenterPoint.setX(bounds.x());
76
                        }
77
 
78
                        if(centerPoint.y() > bounds.y() + bounds.height()) {
79
                                CurrentCenterPoint.setY(bounds.y() + bounds.height());
80
                        } else if(centerPoint.y() < bounds.y()) {
81
                                CurrentCenterPoint.setY(bounds.y());
82
                        }
83
 
84
                }
85
        }
86
 
87
        //Update the scrollbars
88
        centerOn(CurrentCenterPoint);
89
}
90
 
91
/**
92
  * Handles when the mouse button is pressed
93
  */
94
void CMyGraphicsView::mousePressEvent(QMouseEvent* event) {
95
        //For panning the view
96
        LastPanPoint = event->pos();
97
        setCursor(Qt::ClosedHandCursor);
98
}
99
 
100
/**
101
  * Handles when the mouse button is released
102
  */
103
void CMyGraphicsView::mouseReleaseEvent(QMouseEvent* event)
104
{
105
        REFER(event);
106
        setCursor(Qt::OpenHandCursor);
107
        LastPanPoint = QPoint();
108
}
109
 
110
/**
111
*Handles the mouse move event
112
*/
113
void CMyGraphicsView::mouseMoveEvent(QMouseEvent* event) {
114
        if(!LastPanPoint.isNull()) {
115
                //Get how much we panned
116
                QPointF delta = mapToScene(LastPanPoint) - mapToScene(event->pos());
117
                LastPanPoint = event->pos();
118
 
119
                //Update the center ie. do the pan
120
                SetCenter(GetCenter() + delta);
121
        }
122
}
123
 
124
/**
125
  * Zoom the view in and out.
126
  */
127
void CMyGraphicsView::wheelEvent(QWheelEvent* event) {
128
 
129
        //Get the position of the mouse before scaling, in scene coords
130
        QPointF pointBeforeScale(mapToScene(event->pos()));
131
 
132
        //Get the original screen centerpoint
133
        QPointF screenCenter = GetCenter(); //CurrentCenterPoint; //(visRect.center());
134
 
135
        //Scale the view ie. do the zoom
136
        double scaleFactor = 1.15; //How fast we zoom
137
        if(event->delta() > 0) {
138
                //Zoom in
139
                scale(scaleFactor, scaleFactor);
140
        } else {
141
                //Zooming out
142
                scale(1.0 / scaleFactor, 1.0 / scaleFactor);
143
        }
144
 
145
        //Get the position after scaling, in scene coords
146
        QPointF pointAfterScale(mapToScene(event->pos()));
147
 
148
        //Get the offset of how the screen moved
149
        QPointF offset = pointBeforeScale - pointAfterScale;
150
 
151
        //Adjust to the new center for correct zooming
152
        QPointF newCenter = screenCenter + offset;
153
        SetCenter(newCenter);
154
}
155
 
156
/**
157
  * Need to update the center so there is no jolt in the
158
  * interaction after resizing the widget.
159
  */
160
void CMyGraphicsView::resizeEvent(QResizeEvent* event) {
161
        //Get the rectangle of the visible area in scene coords
162
        QRectF visibleArea = mapToScene(rect()).boundingRect();
163
        SetCenter(visibleArea.center());
164
 
165
        //Call the subclass resize so the scrollbars are updated correctly
166
        QGraphicsView::resizeEvent(event);
167
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.