-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 | |||
@@ -8,12 +8,14 @@ using namespace Opie::Core; | |||
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(); |
@@ -22,5 +24,7 @@ ImageScrollView::ImageScrollView (const QString&img, QWidget * parent, const cha | |||
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 | } |
@@ -30,5 +34,8 @@ 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 | ||
@@ -37,4 +44,163 @@ ImageScrollView::~ImageScrollView() | |||
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 | { |
@@ -45,4 +211,8 @@ void ImageScrollView::drawContents(QPainter * p, int clipx, int clipy, int clipw | |||
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(); |
@@ -59,5 +229,5 @@ void ImageScrollView::drawContents(QPainter * p, int clipx, int clipy, int clipw | |||
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 | } |
@@ -99,4 +269,5 @@ ImageDlg::ImageDlg(const QString&fname,QWidget * parent, const char * name) | |||
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 | ||
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 | |||
@@ -13,9 +13,17 @@ class ImageScrollView:public QScrollView | |||
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 ); |
@@ -23,11 +31,22 @@ protected: | |||
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 | ||