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
|
#include "imagescrollview.h"
#include <qimage.h>
#include <qlayout.h>
ImageScrollView::ImageScrollView (const QImage&img, QWidget * parent, const char * name, WFlags f)
:QScrollView(parent,name,f|Qt::WRepaintNoErase),_image_data(img)
{
init();
}
ImageScrollView::ImageScrollView (const QString&img, QWidget * parent, const char * name, WFlags f)
:QScrollView(parent,name,f/*|Qt::WRepaintNoErase*/),_image_data(img)
{
init();
}
void ImageScrollView::setImage(const QImage&img)
{
_image_data = img;
init();
}
/* should be called every time the QImage changed it content */
void ImageScrollView::init()
{
viewport()->setBackgroundColor(white);
resizeContents(_image_data.width(),_image_data.height());
}
ImageScrollView::~ImageScrollView()
{
}
void ImageScrollView::drawContents(QPainter * p, int clipx, int clipy, int clipw, int cliph)
{
int w = clipw;
int h = cliph;
int x = clipx;
int y = clipy;
bool erase = false;
if (w>_image_data.width()) {
w=_image_data.width();
x = 0;
erase = true;
} else if (x+w>_image_data.width()){
x = _image_data.width()-w;
}
if (h>_image_data.height()) {
h=_image_data.height();
y = 0;
erase = true;
} else if (y+h>_image_data.height()){
y = _image_data.height()-h;
}
if (erase) {
p->fillRect(clipx,clipy,clipw,cliph,white);
}
p->drawImage(clipx,clipy,_image_data,x,y,w,h);
}
void ImageScrollView::viewportMouseMoveEvent(QMouseEvent* e)
{
int mx, my;
viewportToContents( e->x(), e->y(), mx, my );
scrollBy(_mouseStartPosX-mx,my-_mouseStartPosY);
_mouseStartPosX=mx;
_mouseStartPosY=my;
}
void ImageScrollView::contentsMouseReleaseEvent ( QMouseEvent * e)
{
viewportToContents( e->x(), e->y(), _mouseStartPosX,_mouseStartPosY );
}
void ImageScrollView::contentsMousePressEvent ( QMouseEvent * e)
{
viewportToContents( e->x(), e->y(), _mouseStartPosX,_mouseStartPosY );
}
/* for testing */
ImageDlg::ImageDlg(const QString&fname,QWidget * parent, const char * name)
:QDialog(parent,name,true,WStyle_ContextHelp)
{
QVBoxLayout*dlglayout = new QVBoxLayout(this);
dlglayout->setSpacing(2);
dlglayout->setMargin(1);
ImageScrollView*inf = new ImageScrollView(fname,this);
dlglayout->addWidget(inf);
}
ImageDlg::~ImageDlg()
{
}
|