author | alwin <alwin> | 2004-04-05 14:50:26 (UTC) |
---|---|---|
committer | alwin <alwin> | 2004-04-05 14:50:26 (UTC) |
commit | 35c73e46c62f60596037c85a5545e13e4488b81f (patch) (unidiff) | |
tree | 06de71fd29cee295b5cfccefd69ef94767b5f186 | |
parent | f44b1d4abe82cfb74db68bffcaf240f6f6134708 (diff) | |
download | opie-35c73e46c62f60596037c85a5545e13e4488b81f.zip opie-35c73e46c62f60596037c85a5545e13e4488b81f.tar.gz opie-35c73e46c62f60596037c85a5545e13e4488b81f.tar.bz2 |
integrated first implementation of rotation/scaling
-rw-r--r-- | noncore/graphics/opie-eye/gui/imagescrollview.cpp | 185 | ||||
-rw-r--r-- | noncore/graphics/opie-eye/gui/imagescrollview.h | 23 |
2 files changed, 199 insertions, 9 deletions
diff --git a/noncore/graphics/opie-eye/gui/imagescrollview.cpp b/noncore/graphics/opie-eye/gui/imagescrollview.cpp index 019f376..0d35354 100644 --- a/noncore/graphics/opie-eye/gui/imagescrollview.cpp +++ b/noncore/graphics/opie-eye/gui/imagescrollview.cpp | |||
@@ -7,21 +7,25 @@ using namespace Opie::Core; | |||
7 | #include <qimage.h> | 7 | #include <qimage.h> |
8 | #include <qlayout.h> | 8 | #include <qlayout.h> |
9 | 9 | ||
10 | ImageScrollView::ImageScrollView (const QImage&img, QWidget * parent, const char * name, WFlags f) | 10 | ImageScrollView::ImageScrollView (const QImage&img, QWidget * parent, const char * name, WFlags f,bool always_scale,bool rfit) |
11 | :QScrollView(parent,name,f|Qt::WRepaintNoErase),_image_data(img) | 11 | :QScrollView(parent,name,f|Qt::WRepaintNoErase),_image_data(),_original_data(img),scale_to_fit(always_scale), |
12 | rotate_to_fit(rfit),first_resize_done(false) | ||
12 | { | 13 | { |
13 | init(); | 14 | init(); |
14 | } | 15 | } |
15 | 16 | ||
16 | ImageScrollView::ImageScrollView (const QString&img, QWidget * parent, const char * name, WFlags f) | 17 | ImageScrollView::ImageScrollView (const QString&img, QWidget * parent, const char * name, WFlags f,bool always_scale,bool rfit) |
17 | :QScrollView(parent,name,f|Qt::WRepaintNoErase),_image_data(img) | 18 | :QScrollView(parent,name,f|Qt::WRepaintNoErase),_image_data(),_original_data(img),scale_to_fit(always_scale), |
19 | rotate_to_fit(rfit),first_resize_done(false) | ||
18 | { | 20 | { |
19 | init(); | 21 | init(); |
20 | } | 22 | } |
21 | 23 | ||
22 | void ImageScrollView::setImage(const QImage&img) | 24 | void ImageScrollView::setImage(const QImage&img) |
23 | { | 25 | { |
24 | _image_data = img; | 26 | _image_data = QImage(); |
27 | _original_data=img; | ||
28 | first_resize_done = false; | ||
25 | init(); | 29 | init(); |
26 | } | 30 | } |
27 | 31 | ||
@@ -29,13 +33,175 @@ void ImageScrollView::setImage(const QImage&img) | |||
29 | void ImageScrollView::init() | 33 | void ImageScrollView::init() |
30 | { | 34 | { |
31 | viewport()->setBackgroundColor(white); | 35 | viewport()->setBackgroundColor(white); |
32 | resizeContents(_image_data.width(),_image_data.height()); | 36 | if (_original_data.size().isValid()) { |
37 | resizeContents(_original_data.width(),_original_data.height()); | ||
38 | } | ||
39 | last_rot = Rotate0; | ||
33 | } | 40 | } |
34 | 41 | ||
35 | ImageScrollView::~ImageScrollView() | 42 | ImageScrollView::~ImageScrollView() |
36 | { | 43 | { |
37 | } | 44 | } |
38 | 45 | ||
46 | void ImageScrollView::rescaleImage(int w, int h) | ||
47 | { | ||
48 | if (_image_data.width()==w && _image_data.height()==h) { | ||
49 | return; | ||
50 | } | ||
51 | double hs = (double)h / (double)_image_data.height() ; | ||
52 | double ws = (double)w / (double)_image_data.width() ; | ||
53 | double scaleFactor = (hs > ws) ? ws : hs; | ||
54 | int smoothW = (int)(scaleFactor * _image_data.width()); | ||
55 | int smoothH = (int)(scaleFactor * _image_data.height()); | ||
56 | _image_data = _image_data.smoothScale(smoothW,smoothH); | ||
57 | } | ||
58 | |||
59 | void ImageScrollView::rotate_into_data(Rotation r) | ||
60 | { | ||
61 | /* realy - we must do this that way, 'cause when acting direct on _image_data the app will | ||
62 | segfault :( */ | ||
63 | QImage dest; | ||
64 | int x, y; | ||
65 | if ( _original_data.depth() > 8 ) | ||
66 | { | ||
67 | unsigned int *srcData, *destData; | ||
68 | switch ( r ) | ||
69 | { | ||
70 | case Rotate90: | ||
71 | dest.create(_original_data.height(), _original_data.width(), _original_data.depth()); | ||
72 | for ( y=0; y < _original_data.height(); ++y ) | ||
73 | { | ||
74 | srcData = (unsigned int *)_original_data.scanLine(y); | ||
75 | for ( x=0; x < _original_data.width(); ++x ) | ||
76 | { | ||
77 | destData = (unsigned int *)dest.scanLine(x); | ||
78 | destData[_original_data.height()-y-1] = srcData[x]; | ||
79 | } | ||
80 | } | ||
81 | break; | ||
82 | case Rotate180: | ||
83 | dest.create(_original_data.width(), _original_data.height(), _original_data.depth()); | ||
84 | for ( y=0; y < _original_data.height(); ++y ) | ||
85 | { | ||
86 | srcData = (unsigned int *)_original_data.scanLine(y); | ||
87 | destData = (unsigned int *)dest.scanLine(_original_data.height()-y-1); | ||
88 | for ( x=0; x < _original_data.width(); ++x ) | ||
89 | destData[_original_data.width()-x-1] = srcData[x]; | ||
90 | } | ||
91 | break; | ||
92 | case Rotate270: | ||
93 | dest.create(_original_data.height(), _original_data.width(), _original_data.depth()); | ||
94 | for ( y=0; y < _original_data.height(); ++y ) | ||
95 | { | ||
96 | srcData = (unsigned int *)_original_data.scanLine(y); | ||
97 | for ( x=0; x < _original_data.width(); ++x ) | ||
98 | { | ||
99 | destData = (unsigned int *)dest.scanLine(_original_data.width()-x-1); | ||
100 | destData[y] = srcData[x]; | ||
101 | } | ||
102 | } | ||
103 | break; | ||
104 | default: | ||
105 | dest = _original_data; | ||
106 | break; | ||
107 | } | ||
108 | } | ||
109 | else | ||
110 | { | ||
111 | unsigned char *srcData, *destData; | ||
112 | unsigned int *srcTable, *destTable; | ||
113 | switch ( r ) | ||
114 | { | ||
115 | case Rotate90: | ||
116 | dest.create(_original_data.height(), _original_data.width(), _original_data.depth()); | ||
117 | dest.setNumColors(_original_data.numColors()); | ||
118 | srcTable = (unsigned int *)_original_data.colorTable(); | ||
119 | destTable = (unsigned int *)dest.colorTable(); | ||
120 | for ( x=0; x < _original_data.numColors(); ++x ) | ||
121 | destTable[x] = srcTable[x]; | ||
122 | for ( y=0; y < _original_data.height(); ++y ) | ||
123 | { | ||
124 | srcData = (unsigned char *)_original_data.scanLine(y); | ||
125 | for ( x=0; x < _original_data.width(); ++x ) | ||
126 | { | ||
127 | destData = (unsigned char *)dest.scanLine(x); | ||
128 | destData[_original_data.height()-y-1] = srcData[x]; | ||
129 | } | ||
130 | } | ||
131 | break; | ||
132 | case Rotate180: | ||
133 | dest.create(_original_data.width(), _original_data.height(), _original_data.depth()); | ||
134 | dest.setNumColors(_original_data.numColors()); | ||
135 | srcTable = (unsigned int *)_original_data.colorTable(); | ||
136 | destTable = (unsigned int *)dest.colorTable(); | ||
137 | for ( x=0; x < _original_data.numColors(); ++x ) | ||
138 | destTable[x] = srcTable[x]; | ||
139 | for ( y=0; y < _original_data.height(); ++y ) | ||
140 | { | ||
141 | srcData = (unsigned char *)_original_data.scanLine(y); | ||
142 | destData = (unsigned char *)dest.scanLine(_original_data.height()-y-1); | ||
143 | for ( x=0; x < _original_data.width(); ++x ) | ||
144 | destData[_original_data.width()-x-1] = srcData[x]; | ||
145 | } | ||
146 | break; | ||
147 | case Rotate270: | ||
148 | dest.create(_original_data.height(), _original_data.width(), _original_data.depth()); | ||
149 | dest.setNumColors(_original_data.numColors()); | ||
150 | srcTable = (unsigned int *)_original_data.colorTable(); | ||
151 | destTable = (unsigned int *)dest.colorTable(); | ||
152 | for ( x=0; x < _original_data.numColors(); ++x ) | ||
153 | destTable[x] = srcTable[x]; | ||
154 | for ( y=0; y < _original_data.height(); ++y ) | ||
155 | { | ||
156 | srcData = (unsigned char *)_original_data.scanLine(y); | ||
157 | for ( x=0; x < _original_data.width(); ++x ) | ||
158 | { | ||
159 | destData = (unsigned char *)dest.scanLine(_original_data.width()-x-1); | ||
160 | destData[y] = srcData[x]; | ||
161 | } | ||
162 | } | ||
163 | break; | ||
164 | default: | ||
165 | dest = _original_data; | ||
166 | break; | ||
167 | } | ||
168 | |||
169 | } | ||
170 | _image_data = dest; | ||
171 | } | ||
172 | |||
173 | void ImageScrollView::resizeEvent(QResizeEvent * e) | ||
174 | { | ||
175 | odebug << "ImageScrollView resizeEvent" << oendl; | ||
176 | QScrollView::resizeEvent(e); | ||
177 | Rotation r = Rotate0; | ||
178 | if (width()>height()&&_original_data.width()<_original_data.height() || | ||
179 | width()<height()&&_original_data.width()>_original_data.height()) { | ||
180 | if (rotate_to_fit) r = Rotate90; | ||
181 | } | ||
182 | odebug << " r = " << r << oendl; | ||
183 | if (scale_to_fit) { | ||
184 | if (!_image_data.size().isValid()||width()>_image_data.width()||height()>_image_data.height()) { | ||
185 | if (r==Rotate0) { | ||
186 | _image_data = _original_data; | ||
187 | } else { | ||
188 | rotate_into_data(r); | ||
189 | } | ||
190 | } | ||
191 | rescaleImage(width(),height()); | ||
192 | resizeContents(width()-10,height()-10); | ||
193 | } else if (!first_resize_done||r!=last_rot) { | ||
194 | if (r==Rotate0) { | ||
195 | _image_data = _original_data; | ||
196 | } else { | ||
197 | rotate_into_data(r); | ||
198 | } | ||
199 | last_rot = r; | ||
200 | resizeContents(_image_data.width(),_image_data.height()); | ||
201 | } | ||
202 | first_resize_done = true; | ||
203 | } | ||
204 | |||
39 | void ImageScrollView::drawContents(QPainter * p, int clipx, int clipy, int clipw, int cliph) | 205 | void ImageScrollView::drawContents(QPainter * p, int clipx, int clipy, int clipw, int cliph) |
40 | { | 206 | { |
41 | int w = clipw; | 207 | int w = clipw; |
@@ -44,6 +210,10 @@ void ImageScrollView::drawContents(QPainter * p, int clipx, int clipy, int clipw | |||
44 | int y = clipy; | 210 | int y = clipy; |
45 | bool erase = false; | 211 | bool erase = false; |
46 | 212 | ||
213 | if (!_image_data.size().isValid()) { | ||
214 | p->fillRect(clipx,clipy,clipw,cliph,white); | ||
215 | return; | ||
216 | } | ||
47 | if (w>_image_data.width()) { | 217 | if (w>_image_data.width()) { |
48 | w=_image_data.width(); | 218 | w=_image_data.width(); |
49 | x = 0; | 219 | x = 0; |
@@ -58,7 +228,7 @@ void ImageScrollView::drawContents(QPainter * p, int clipx, int clipy, int clipw | |||
58 | } else if (y+h>_image_data.height()){ | 228 | } else if (y+h>_image_data.height()){ |
59 | y = _image_data.height()-h; | 229 | y = _image_data.height()-h; |
60 | } | 230 | } |
61 | if (erase) { | 231 | if (erase||_image_data.hasAlphaBuffer()) { |
62 | p->fillRect(clipx,clipy,clipw,cliph,white); | 232 | p->fillRect(clipx,clipy,clipw,cliph,white); |
63 | } | 233 | } |
64 | p->drawImage(clipx,clipy,_image_data,x,y,w,h); | 234 | p->drawImage(clipx,clipy,_image_data,x,y,w,h); |
@@ -98,6 +268,7 @@ ImageDlg::ImageDlg(const QString&fname,QWidget * parent, const char * name) | |||
98 | dlglayout->setMargin(1); | 268 | dlglayout->setMargin(1); |
99 | ImageScrollView*inf = new ImageScrollView(fname,this); | 269 | ImageScrollView*inf = new ImageScrollView(fname,this); |
100 | dlglayout->addWidget(inf); | 270 | dlglayout->addWidget(inf); |
271 | odebug << "Imagedlg constructor end" << oendl; | ||
101 | } | 272 | } |
102 | 273 | ||
103 | ImageDlg::~ImageDlg() | 274 | ImageDlg::~ImageDlg() |
diff --git a/noncore/graphics/opie-eye/gui/imagescrollview.h b/noncore/graphics/opie-eye/gui/imagescrollview.h index edea235..e25f955 100644 --- a/noncore/graphics/opie-eye/gui/imagescrollview.h +++ b/noncore/graphics/opie-eye/gui/imagescrollview.h | |||
@@ -12,23 +12,42 @@ class ImageScrollView:public QScrollView | |||
12 | { | 12 | { |
13 | Q_OBJECT | 13 | Q_OBJECT |
14 | public: | 14 | public: |
15 | ImageScrollView (const QImage&, QWidget * parent=0, const char * name=0, WFlags f=0 ); | 15 | ImageScrollView (const QImage&, QWidget * parent=0, const char * name=0, WFlags f=0,bool always_scale=false,bool rfit=false ); |
16 | ImageScrollView (const QString&, QWidget * parent=0, const char * name=0, WFlags f=0 ); | 16 | ImageScrollView (const QString&, QWidget * parent=0, const char * name=0, WFlags f=0,bool always_scale=false,bool rfit=false ); |
17 | virtual ~ImageScrollView(); | 17 | virtual ~ImageScrollView(); |
18 | 18 | ||
19 | void setImage(const QImage&); | 19 | void setImage(const QImage&); |
20 | |||
21 | enum Rotation { | ||
22 | Rotate0, | ||
23 | Rotate90, | ||
24 | Rotate180, | ||
25 | Rotate270 | ||
26 | }; | ||
27 | |||
20 | protected: | 28 | protected: |
21 | virtual void drawContents ( QPainter * p, int clipx, int clipy, int clipw, int cliph ); | 29 | virtual void drawContents ( QPainter * p, int clipx, int clipy, int clipw, int cliph ); |
22 | void init(); | 30 | void init(); |
23 | 31 | ||
24 | QImage _image_data; | 32 | QImage _image_data; |
33 | QImage _original_data; | ||
25 | 34 | ||
26 | int _mouseStartPosX,_mouseStartPosY; | 35 | int _mouseStartPosX,_mouseStartPosY; |
27 | 36 | ||
37 | bool scale_to_fit; | ||
38 | bool rotate_to_fit; | ||
39 | bool first_resize_done; | ||
40 | Rotation last_rot; | ||
41 | |||
42 | void rescaleImage(int w, int h); | ||
43 | |||
44 | void rotate_into_data(Rotation r); | ||
45 | |||
28 | protected slots: | 46 | protected slots: |
29 | virtual void viewportMouseMoveEvent(QMouseEvent* e); | 47 | virtual void viewportMouseMoveEvent(QMouseEvent* e); |
30 | virtual void contentsMousePressEvent ( QMouseEvent * e); | 48 | virtual void contentsMousePressEvent ( QMouseEvent * e); |
31 | virtual void contentsMouseReleaseEvent ( QMouseEvent * e); | 49 | virtual void contentsMouseReleaseEvent ( QMouseEvent * e); |
50 | virtual void resizeEvent(QResizeEvent * e); | ||
32 | }; | 51 | }; |
33 | 52 | ||
34 | /* for testing */ | 53 | /* for testing */ |