summaryrefslogtreecommitdiff
path: root/noncore/graphics/opie-eye/lib/oimagezoomer.cpp
blob: ffa3c0c7fe44dcad916e9f5e08f683ff3acf686b (plain)
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
112
113
114
115
116
117
118
119
120
#include "oimagezoomer.h"

#include <qimage.h>
#include <qpixmap.h>
#include <qpainter.h>
#include <qrect.h>
#include <qpoint.h>
#include <qsize.h>

namespace Opie {
namespace MM {
OImageZoomer::OImageZoomer( QWidget* parent,  const char* name,  WFlags fl )
    :  QFrame( parent, name, fl ) {
    init();
}

OImageZoomer::OImageZoomer( const QPixmap& pix, QWidget* par, const char* name, WFlags fl )
    : QFrame( par, name, fl ) {
    init();
    setImage( pix );
}

OImageZoomer::OImageZoomer( const QSize& pSize, const QSize& vSize, QWidget* par,
                            const char* name, WFlags fl )
    : QFrame( par, name, fl ), m_imgSize( pSize ),m_visSize( vSize ) {
    init();
}

OImageZoomer::~OImageZoomer() {

}

void OImageZoomer::init() {
    setFrameStyle( Panel | Sunken );
}

void OImageZoomer::setImageSize( const QSize& size ) {
    m_imgSize = size;
    repaint();
}
void OImageZoomer::setViewPortSize( const QSize& size ) {
    m_visSize = size;
    repaint();
}

void OImageZoomer::setVisiblePoint( const QPoint& pt ) {
    m_visPt = pt;
    repaint();
}

void OImageZoomer::setImage( const QImage& img) {
    m_img = img;
    resizeEvent( 0 );
    repaint();
}

void OImageZoomer::setImage( const QPixmap& pix) {
    setImage( pix.convertToImage() );
}

void OImageZoomer::resizeEvent( QResizeEvent* ev ) {
    QFrame::resizeEvent( ev );
    setBackgroundOrigin(  QWidget::WidgetOrigin );
    // TODO Qt3 use PalettePixmap and use size
    QPixmap pix; pix.convertFromImage( m_img.smoothScale( size().width(), size().height() ) );
    setBackgroundPixmap( pix);
}

void OImageZoomer::drawContents( QPainter* p ) {
    /*
     * if the page size
     */
    if ( m_imgSize.isEmpty() )
        return;

   /*
    * paint a red rect which represents the visible size
    *
    * We need to recalculate x,y and width and height of the
    * rect. So image size relates to contentRect
    *
    */
    QRect c( contentsRect() );
    p->setPen( Qt::red );

    int len = m_imgSize.width();
    int x = (c.width()*m_visPt.x())/len        + c.x();
    int w = (c.width()*m_visSize.width() )/len + c.x();
    if ( w > c.width() ) w = c.width();

    len = m_imgSize.height();
    int y = (c.height()*m_visPt.y() )/len          + c.y();
    int h = (c.height()*m_visSize.height() )/len + c.y();
    if ( h > c.height() ) h = c.height();

    p->drawRect( x, y, w, h );
}

void OImageZoomer::mousePressEvent( QMouseEvent*  ) {
    m_mouseX = m_mouseY = -1;
}

void OImageZoomer::mouseMoveEvent( QMouseEvent* ev ) {
    int mx, my;
    mx = ev->x();
    my = ev->y();

    if ( m_mouseX != -1 && m_mouseY != -1 ) {
        int diffx = ( mx - m_mouseX ) * m_imgSize.width() / width();
        int diffy = ( my - m_mouseY ) * m_imgSize.height() / height();
        emit zoomAreaRel( diffx, diffy );
        emit zoomArea(m_visPt.x()+diffx, m_visPt.y()+diffy );
    }
    m_mouseX = mx;
    m_mouseY = my;
}


}
}