-rw-r--r-- | noncore/graphics/opie-eye/lib/oimagezoomer.cpp | 90 | ||||
-rw-r--r-- | noncore/graphics/opie-eye/lib/oimagezoomer.h | 84 |
2 files changed, 166 insertions, 8 deletions
diff --git a/noncore/graphics/opie-eye/lib/oimagezoomer.cpp b/noncore/graphics/opie-eye/lib/oimagezoomer.cpp index ffa3c0c..4df5dcc 100644 --- a/noncore/graphics/opie-eye/lib/oimagezoomer.cpp +++ b/noncore/graphics/opie-eye/lib/oimagezoomer.cpp | |||
@@ -1,104 +1,194 @@ | |||
1 | #include "oimagezoomer.h" | 1 | #include "oimagezoomer.h" |
2 | 2 | ||
3 | #include <qimage.h> | 3 | #include <qimage.h> |
4 | #include <qpixmap.h> | 4 | #include <qpixmap.h> |
5 | #include <qpainter.h> | 5 | #include <qpainter.h> |
6 | #include <qrect.h> | 6 | #include <qrect.h> |
7 | #include <qpoint.h> | 7 | #include <qpoint.h> |
8 | #include <qsize.h> | 8 | #include <qsize.h> |
9 | 9 | ||
10 | namespace Opie { | 10 | namespace Opie { |
11 | namespace MM { | 11 | namespace MM { |
12 | |||
13 | /** | ||
14 | * \brief The most simple c'tor | ||
15 | * The main c'tor. You still need to set a QPixmap/QIMage, | ||
16 | * setImageSize,setViewPortSize,setVisiblePoint | ||
17 | * | ||
18 | * @param parent The parent widget | ||
19 | * @param name A name for this widget | ||
20 | * @param fl The widget flags | ||
21 | * | ||
22 | */ | ||
12 | OImageZoomer::OImageZoomer( QWidget* parent, const char* name, WFlags fl ) | 23 | OImageZoomer::OImageZoomer( QWidget* parent, const char* name, WFlags fl ) |
13 | : QFrame( parent, name, fl ) { | 24 | : QFrame( parent, name, fl ) { |
14 | init(); | 25 | init(); |
15 | } | 26 | } |
16 | 27 | ||
28 | |||
29 | /** | ||
30 | * \brief This c'tor takes a QPixmap additional | ||
31 | * | ||
32 | * You initially set the QPixmap but you still need to provide | ||
33 | * the additional data to make this widget useful | ||
34 | * | ||
35 | * @param pix A Pixmap it'll be converted to a QImage later! | ||
36 | * @param par The parent widget | ||
37 | * @param name The name of this widget | ||
38 | * @param fl The widget flags | ||
39 | */ | ||
17 | OImageZoomer::OImageZoomer( const QPixmap& pix, QWidget* par, const char* name, WFlags fl ) | 40 | OImageZoomer::OImageZoomer( const QPixmap& pix, QWidget* par, const char* name, WFlags fl ) |
18 | : QFrame( par, name, fl ) { | 41 | : QFrame( par, name, fl ) { |
19 | init(); | 42 | init(); |
20 | setImage( pix ); | 43 | setImage( pix ); |
21 | } | 44 | } |
22 | 45 | ||
46 | |||
47 | /** | ||
48 | * \brief This c'tor takes a QImage instead | ||
49 | * You just provide a QImage which is saved. It behaves the same as the others. | ||
50 | * | ||
51 | * @param img A Image which will be used for the zoomer content | ||
52 | * @param par The parent of the widget | ||
53 | * @param name The name of the widget | ||
54 | * @param fl The widgets flags | ||
55 | */ | ||
56 | OImageZoomer::OImageZoomer( const QImage& img, QWidget* par, const char* name, WFlags fl) | ||
57 | : QFrame( par, name, fl ) { | ||
58 | init(); | ||
59 | setImage( img ); | ||
60 | } | ||
61 | |||
62 | |||
63 | /** | ||
64 | * \brief overloaded c'tor | ||
65 | * | ||
66 | * This differs only in the arguments it takes | ||
67 | * | ||
68 | * | ||
69 | * @param pSize The size of the Page you show | ||
70 | * @param vSize The size of the viewport. The size of the visible part of the widget | ||
71 | * @param par The parent of the widget | ||
72 | * @param name The name | ||
73 | * @param fl The window flags | ||
74 | */ | ||
23 | OImageZoomer::OImageZoomer( const QSize& pSize, const QSize& vSize, QWidget* par, | 75 | OImageZoomer::OImageZoomer( const QSize& pSize, const QSize& vSize, QWidget* par, |
24 | const char* name, WFlags fl ) | 76 | const char* name, WFlags fl ) |
25 | : QFrame( par, name, fl ), m_imgSize( pSize ),m_visSize( vSize ) { | 77 | : QFrame( par, name, fl ), m_imgSize( pSize ),m_visSize( vSize ) { |
26 | init(); | 78 | init(); |
27 | } | 79 | } |
28 | 80 | ||
81 | /** | ||
82 | * d'tor | ||
83 | */ | ||
29 | OImageZoomer::~OImageZoomer() { | 84 | OImageZoomer::~OImageZoomer() { |
30 | 85 | ||
31 | } | 86 | } |
32 | 87 | ||
33 | void OImageZoomer::init() { | 88 | void OImageZoomer::init() { |
34 | setFrameStyle( Panel | Sunken ); | 89 | setFrameStyle( Panel | Sunken ); |
35 | } | 90 | } |
36 | 91 | ||
92 | |||
93 | /** | ||
94 | * \brief set the page/image size | ||
95 | * Tell us the QSize of the Data you show to the user. We need this | ||
96 | * to do the calculations | ||
97 | * | ||
98 | * @param size The size of the stuff you want to zoom on | ||
99 | */ | ||
37 | void OImageZoomer::setImageSize( const QSize& size ) { | 100 | void OImageZoomer::setImageSize( const QSize& size ) { |
38 | m_imgSize = size; | 101 | m_imgSize = size; |
39 | repaint(); | 102 | repaint(); |
40 | } | 103 | } |
104 | |||
105 | /** | ||
106 | * \brief Set the size of the viewport | ||
107 | * Tell us the QSize of the viewport. The viewport is the part | ||
108 | * of the widget which is exposed on the screen | ||
109 | * | ||
110 | * @param size Te size of the viewport | ||
111 | * | ||
112 | * @see QScrollView::viewport() | ||
113 | */ | ||
41 | void OImageZoomer::setViewPortSize( const QSize& size ) { | 114 | void OImageZoomer::setViewPortSize( const QSize& size ) { |
42 | m_visSize = size; | 115 | m_visSize = size; |
43 | repaint(); | 116 | repaint(); |
44 | } | 117 | } |
45 | 118 | ||
119 | /** | ||
120 | * \brief the point in the topleft corner which is currently visible | ||
121 | * Set the visible point. This most of the times relate to QScrollView::contentsX() | ||
122 | * and QScrollView::contentsY() | ||
123 | * | ||
124 | * @see setVisiblePoint(int,int) | ||
125 | */ | ||
46 | void OImageZoomer::setVisiblePoint( const QPoint& pt ) { | 126 | void OImageZoomer::setVisiblePoint( const QPoint& pt ) { |
47 | m_visPt = pt; | 127 | m_visPt = pt; |
48 | repaint(); | 128 | repaint(); |
49 | } | 129 | } |
50 | 130 | ||
131 | |||
132 | /** | ||
133 | * Set the Image. The image will be resized on resizeEvent | ||
134 | * and it'll set the QPixmap background | ||
135 | * | ||
136 | * @param img The image will be stored internally and used as the background | ||
137 | */ | ||
51 | void OImageZoomer::setImage( const QImage& img) { | 138 | void OImageZoomer::setImage( const QImage& img) { |
52 | m_img = img; | 139 | m_img = img; |
53 | resizeEvent( 0 ); | 140 | resizeEvent( 0 ); |
54 | repaint(); | 141 | repaint(); |
55 | } | 142 | } |
56 | 143 | ||
144 | /** | ||
145 | * overloaded function it calls the QImage version | ||
146 | */ | ||
57 | void OImageZoomer::setImage( const QPixmap& pix) { | 147 | void OImageZoomer::setImage( const QPixmap& pix) { |
58 | setImage( pix.convertToImage() ); | 148 | setImage( pix.convertToImage() ); |
59 | } | 149 | } |
60 | 150 | ||
61 | void OImageZoomer::resizeEvent( QResizeEvent* ev ) { | 151 | void OImageZoomer::resizeEvent( QResizeEvent* ev ) { |
62 | QFrame::resizeEvent( ev ); | 152 | QFrame::resizeEvent( ev ); |
63 | setBackgroundOrigin( QWidget::WidgetOrigin ); | 153 | setBackgroundOrigin( QWidget::WidgetOrigin ); |
64 | // TODO Qt3 use PalettePixmap and use size | 154 | // TODO Qt3 use PalettePixmap and use size |
65 | QPixmap pix; pix.convertFromImage( m_img.smoothScale( size().width(), size().height() ) ); | 155 | QPixmap pix; pix.convertFromImage( m_img.smoothScale( size().width(), size().height() ) ); |
66 | setBackgroundPixmap( pix); | 156 | setBackgroundPixmap( pix); |
67 | } | 157 | } |
68 | 158 | ||
69 | void OImageZoomer::drawContents( QPainter* p ) { | 159 | void OImageZoomer::drawContents( QPainter* p ) { |
70 | /* | 160 | /* |
71 | * if the page size | 161 | * if the page size |
72 | */ | 162 | */ |
73 | if ( m_imgSize.isEmpty() ) | 163 | if ( m_imgSize.isEmpty() ) |
74 | return; | 164 | return; |
75 | 165 | ||
76 | /* | 166 | /* |
77 | * paint a red rect which represents the visible size | 167 | * paint a red rect which represents the visible size |
78 | * | 168 | * |
79 | * We need to recalculate x,y and width and height of the | 169 | * We need to recalculate x,y and width and height of the |
80 | * rect. So image size relates to contentRect | 170 | * rect. So image size relates to contentRect |
81 | * | 171 | * |
82 | */ | 172 | */ |
83 | QRect c( contentsRect() ); | 173 | QRect c( contentsRect() ); |
84 | p->setPen( Qt::red ); | 174 | p->setPen( Qt::red ); |
85 | 175 | ||
86 | int len = m_imgSize.width(); | 176 | int len = m_imgSize.width(); |
87 | int x = (c.width()*m_visPt.x())/len + c.x(); | 177 | int x = (c.width()*m_visPt.x())/len + c.x(); |
88 | int w = (c.width()*m_visSize.width() )/len + c.x(); | 178 | int w = (c.width()*m_visSize.width() )/len + c.x(); |
89 | if ( w > c.width() ) w = c.width(); | 179 | if ( w > c.width() ) w = c.width(); |
90 | 180 | ||
91 | len = m_imgSize.height(); | 181 | len = m_imgSize.height(); |
92 | int y = (c.height()*m_visPt.y() )/len + c.y(); | 182 | int y = (c.height()*m_visPt.y() )/len + c.y(); |
93 | int h = (c.height()*m_visSize.height() )/len + c.y(); | 183 | int h = (c.height()*m_visSize.height() )/len + c.y(); |
94 | if ( h > c.height() ) h = c.height(); | 184 | if ( h > c.height() ) h = c.height(); |
95 | 185 | ||
96 | p->drawRect( x, y, w, h ); | 186 | p->drawRect( x, y, w, h ); |
97 | } | 187 | } |
98 | 188 | ||
99 | void OImageZoomer::mousePressEvent( QMouseEvent* ) { | 189 | void OImageZoomer::mousePressEvent( QMouseEvent* ) { |
100 | m_mouseX = m_mouseY = -1; | 190 | m_mouseX = m_mouseY = -1; |
101 | } | 191 | } |
102 | 192 | ||
103 | void OImageZoomer::mouseMoveEvent( QMouseEvent* ev ) { | 193 | void OImageZoomer::mouseMoveEvent( QMouseEvent* ev ) { |
104 | int mx, my; | 194 | int mx, my; |
diff --git a/noncore/graphics/opie-eye/lib/oimagezoomer.h b/noncore/graphics/opie-eye/lib/oimagezoomer.h index 605416c..2516c61 100644 --- a/noncore/graphics/opie-eye/lib/oimagezoomer.h +++ b/noncore/graphics/opie-eye/lib/oimagezoomer.h | |||
@@ -1,66 +1,134 @@ | |||
1 | #ifndef OPIE_ODP_IMAGE_ZOOMER_H | 1 | #ifndef OPIE_ODP_IMAGE_ZOOMER_H |
2 | #define OPIE_ODP_IMAGE_ZOOMER_H | 2 | #define OPIE_ODP_IMAGE_ZOOMER_H |
3 | 3 | ||
4 | #include <qframe.h> | 4 | #include <qframe.h> |
5 | #include <qimage.h> | 5 | #include <qimage.h> |
6 | 6 | ||
7 | class QPixmap; | 7 | class QPixmap; |
8 | class QRect; | 8 | class QRect; |
9 | class QPoint; | 9 | class QPoint; |
10 | 10 | ||
11 | 11 | ||
12 | namespace Opie { | 12 | namespace Opie { |
13 | namespace MM { | 13 | namespace MM { |
14 | 14 | ||
15 | /** | 15 | /** |
16 | * \brief small class to zoom over a widget | 16 | * \brief small class to zoom over a Page |
17 | * This class takes a QImage or QPixmap | 17 | * |
18 | * and gets the size of the original image | 18 | * This class represents your page but smaller. |
19 | * and provides depending of this widgets size | 19 | * It can draw a Rect on top of an Image/Pixmap you supply |
20 | * a zoomer and emits the region which should | 20 | * and you can allow the user easily zooming/moving |
21 | * be shown / zoomed to | 21 | * over your widget. |
22 | * All you need to do is to supply a image/pixmap, the visible size | ||
23 | * and the original image/pixmap size and the current visible top/left | ||
24 | * position. | ||
25 | * | ||
26 | * This Image works perfectly with QScrollView as you can connect | ||
27 | * QScrollView::contentsMoving to setVisiblePoint slot and the zoomAreRel | ||
28 | * to the QScrollView::scrollBy slot. Now you would only need to watch | ||
29 | * the resize event anf give us the new information about QScrollView::viewport | ||
30 | * | ||
31 | * You need to position and set the size of this widget! using setFixedSize() is quite | ||
32 | * a good idea for this widget | ||
33 | * | ||
34 | * @see QScrollView | ||
35 | * @see QScrollView::viewport() | ||
36 | * | ||
37 | * @since 1.2 | ||
38 | * | ||
22 | */ | 39 | */ |
23 | class OImageZoomer : public QFrame { | 40 | class OImageZoomer : public QFrame { |
24 | Q_OBJECT | 41 | Q_OBJECT |
25 | public: | 42 | public: |
26 | OImageZoomer( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); | 43 | OImageZoomer( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); |
27 | OImageZoomer( const QPixmap&,QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); | 44 | OImageZoomer( const QPixmap&,QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); |
28 | OImageZoomer( const QImage&, QWidget* parent = 0, const char* name= 0, WFlags fl = 0 ); | 45 | OImageZoomer( const QImage&, QWidget* parent = 0, const char* name= 0, WFlags fl = 0 ); |
29 | OImageZoomer( const QSize&, const QSize&, QWidget* par, const char*, WFlags fl ); | 46 | OImageZoomer( const QSize&, const QSize&, QWidget* par, const char*, WFlags fl ); |
30 | ~OImageZoomer(); | 47 | ~OImageZoomer(); |
31 | 48 | ||
32 | public slots: | 49 | public slots: |
33 | void setImageSize( const QSize& ); | 50 | void setImageSize( const QSize& ); |
34 | void setViewPortSize( const QSize& ); | 51 | void setViewPortSize( const QSize& ); |
35 | void setVisiblePoint( const QPoint& ); | 52 | void setVisiblePoint( const QPoint& ); |
36 | void setVisiblePoint( int x, int y ); | 53 | void setVisiblePoint( int x, int y ); |
37 | void setImage( const QImage& ); | 54 | void setImage( const QImage& ); |
38 | void setImage( const QPixmap& ); | 55 | void setImage( const QPixmap& ); |
39 | 56 | ||
40 | signals: | 57 | signals: |
41 | void zoomAreaRel( int,int ); | 58 | /** |
42 | void zoomArea( int,int ); | 59 | * Relative movement in the coordinates of the viewport |
60 | * This signal can easily be connected to QScrollView::scrollBy. | ||
61 | * This signal is emitted from within the mouseMoveEvent of this widget | ||
62 | * | ||
63 | * | ||
64 | * @param x The way to move relative on the X-Axis | ||
65 | * @param y The way to move relative on the Y-Axis | ||
66 | * | ||
67 | * @see setVisiblePoint | ||
68 | * @see QScrollView::scrollBy | ||
69 | */ | ||
70 | void zoomAreaRel( int x,int y); | ||
71 | |||
72 | /** | ||
73 | * Here you get absolute coordinates. | ||
74 | * This slot will be emitted from within the mouseMoveEvent of this widget. | ||
75 | * So you may not delete this widget | ||
76 | * | ||
77 | * @param x The absolute X Coordinate to scroll to. | ||
78 | * @param y The absolute Y Coordinate to scroll to. | ||
79 | * | ||
80 | */ | ||
81 | void zoomArea( int x,int y); | ||
43 | 82 | ||
44 | public: | 83 | public: |
84 | /** | ||
85 | * make sure to call these if you reimplement | ||
86 | * @internal | ||
87 | */ | ||
45 | void resizeEvent( QResizeEvent* ); | 88 | void resizeEvent( QResizeEvent* ); |
46 | 89 | ||
47 | protected: | 90 | protected: |
91 | /** | ||
92 | * make sure to call these if you reimplement | ||
93 | * @internal | ||
94 | */ | ||
48 | void drawContents( QPainter* p ); | 95 | void drawContents( QPainter* p ); |
96 | |||
97 | /** | ||
98 | * make sure to call these if you reimplememt | ||
99 | * @internal | ||
100 | */ | ||
49 | void mousePressEvent( QMouseEvent* ev ); | 101 | void mousePressEvent( QMouseEvent* ev ); |
102 | /** | ||
103 | * make sure to call these if you reimplement | ||
104 | * @internal | ||
105 | */ | ||
50 | void mouseMoveEvent( QMouseEvent* ev ); | 106 | void mouseMoveEvent( QMouseEvent* ev ); |
51 | 107 | ||
52 | private: | 108 | private: |
109 | /** | ||
110 | * @internal | ||
111 | */ | ||
53 | void init(); | 112 | void init(); |
54 | QImage m_img; | 113 | QImage m_img; |
55 | QSize m_imgSize, m_visSize; | 114 | QSize m_imgSize, m_visSize; |
56 | QPoint m_visPt; | 115 | QPoint m_visPt; |
57 | int m_mouseX, m_mouseY; | 116 | int m_mouseX, m_mouseY; |
58 | }; | 117 | }; |
59 | 118 | ||
119 | /** | ||
120 | * This slot is present for convience. You can connect the | ||
121 | * QScrollView::contentsMoved to this slot and it calls the QPoint | ||
122 | * version for you | ||
123 | * This realtes to QScrollView::contentsX() and QScrollView::contentsY() | ||
124 | * | ||
125 | * @param x The top left x coordinate | ||
126 | * @param y The top left y coorisnate | ||
127 | */ | ||
60 | inline void OImageZoomer::setVisiblePoint( int x, int y ) { | 128 | inline void OImageZoomer::setVisiblePoint( int x, int y ) { |
61 | setVisiblePoint( QPoint( x, y ) ); | 129 | setVisiblePoint( QPoint( x, y ) ); |
62 | } | 130 | } |
63 | 131 | ||
64 | } | 132 | } |
65 | } | 133 | } |
66 | #endif | 134 | #endif |