summaryrefslogtreecommitdiff
path: root/noncore/graphics/opie-eye/gui/imagescrollview.cpp
blob: ad192195838c7a965c0ecaa6c45c9986bffabebb (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "imagescrollview.h"

#include <opie2/odebug.h>

using namespace Opie::Core;

#include <qimage.h>
#include <qlayout.h>

ImageScrollView::ImageScrollView( QWidget* parent, const char* name,  WFlags f )
    :QScrollView(parent,name,f|Qt::WRepaintNoErase ),_image_data(),_original_data(),scale_to_fit(true),
     rotate_to_fit(true),first_resize_done(false),m_lastName("")
{
    init();
}

ImageScrollView::ImageScrollView (const QImage&img, QWidget * parent, const char * name, WFlags f,bool always_scale,bool rfit)
    :QScrollView(parent,name,f|Qt::WRepaintNoErase),_image_data(),_original_data(img),scale_to_fit(always_scale),
    rotate_to_fit(rfit),first_resize_done(false),m_lastName("")
{
    _original_data.convertDepth(QPixmap::defaultDepth());
    _original_data.setAlphaBuffer(false);
    init();
}

ImageScrollView::ImageScrollView (const QString&img, QWidget * parent, const char * name, WFlags f,bool always_scale,bool rfit)
    :QScrollView(parent,name,f|Qt::WRepaintNoErase),_image_data(),_original_data(),scale_to_fit(always_scale),
    rotate_to_fit(rfit),first_resize_done(false),m_lastName("")
{
    init();
    setImage(img);
}

void ImageScrollView::setImage(const QImage&img)
{
    _image_data = QImage();
    _original_data=img;
    _original_data.convertDepth(QPixmap::defaultDepth());
    _original_data.setAlphaBuffer(false);
    m_lastName = "";
    if (first_resize_done) {
        generateImage();
    }
}

void ImageScrollView::setImage( const QString& path ) {
    odebug << "load new image " << oendl;
    if (m_lastName == path) return;
    m_lastName = path;
    _original_data.load(path);
    _original_data.convertDepth(QPixmap::defaultDepth());
    _original_data.setAlphaBuffer(false);
    _image_data = QImage();
    if (first_resize_done) {
        generateImage();
    }
}

/* should be called every time the QImage changed it content */
void ImageScrollView::init()
{
    odebug << "init " << oendl;
    viewport()->setBackgroundColor(white);
    setFocusPolicy(QWidget::StrongFocus);
    if (first_resize_done) {
        last_rot = Rotate0;
        generateImage();
        odebug << "reinit display " << oendl;
    } else if (_original_data.size().isValid()) {
        resizeContents(_original_data.width(),_original_data.height());
    }
}

void ImageScrollView::setAutoRotate(bool how)
{
    /* to avoid double repaints */
    if (rotate_to_fit != how) {
        rotate_to_fit = how;
        _image_data = QImage();
        generateImage();
    }
}

void ImageScrollView::setAutoScale(bool how)
{
    scale_to_fit = how;
    if (!how) {
        rotate_to_fit = false;
    }
    _image_data = QImage();
    generateImage();
}

ImageScrollView::~ImageScrollView()
{
}

void ImageScrollView::rescaleImage(int w, int h)
{
    if (_image_data.width()==w && _image_data.height()==h) {
        return;
    }
    double hs = (double)h / (double)_image_data.height() ;
    double ws = (double)w / (double)_image_data.width() ;
    double scaleFactor = (hs > ws) ? ws : hs;
    int smoothW = (int)(scaleFactor * _image_data.width());
    int smoothH = (int)(scaleFactor * _image_data.height());
    _image_data = _image_data.smoothScale(smoothW,smoothH);
}

void ImageScrollView::rotate_into_data(Rotation r)
{
    /* realy - we must do this that way, 'cause when acting direct on _image_data the app will
       segfault :( */
    QImage dest;
    int x, y;
    if ( _original_data.depth() > 8 )
    {
        unsigned int *srcData, *destData;
        switch ( r )
        {
            case Rotate90:
                dest.create(_original_data.height(), _original_data.width(), _original_data.depth());
                for ( y=0; y < _original_data.height(); ++y )
                {
                    srcData = (unsigned int *)_original_data.scanLine(y);
                    for ( x=0; x < _original_data.width(); ++x )
                    {
                        destData = (unsigned int *)dest.scanLine(x);
                        destData[_original_data.height()-y-1] = srcData[x];
                    }
                }
                break;
            case Rotate180:
                dest.create(_original_data.width(), _original_data.height(), _original_data.depth());
                for ( y=0; y < _original_data.height(); ++y )
                {
                    srcData = (unsigned int *)_original_data.scanLine(y);
                    destData = (unsigned int *)dest.scanLine(_original_data.height()-y-1);
                    for ( x=0; x < _original_data.width(); ++x )
                        destData[_original_data.width()-x-1] = srcData[x];
                }
                break;
            case Rotate270:
                dest.create(_original_data.height(), _original_data.width(), _original_data.depth());
                for ( y=0; y < _original_data.height(); ++y )
                {
                    srcData = (unsigned int *)_original_data.scanLine(y);
                    for ( x=0; x < _original_data.width(); ++x )
                    {
                        destData = (unsigned int *)dest.scanLine(_original_data.width()-x-1);
                        destData[y] = srcData[x];
                    }
                }
                break;
            default:
                dest = _original_data;
                break;
        }
    }
    else
    {
        unsigned char *srcData, *destData;
        unsigned int *srcTable, *destTable;
        switch ( r )
        {
            case Rotate90:
                dest.create(_original_data.height(), _original_data.width(), _original_data.depth());
                dest.setNumColors(_original_data.numColors());
                srcTable = (unsigned int *)_original_data.colorTable();
                destTable = (unsigned int *)dest.colorTable();
                for ( x=0; x < _original_data.numColors(); ++x )
                    destTable[x] = srcTable[x];
                for ( y=0; y < _original_data.height(); ++y )
                {
                    srcData = (unsigned char *)_original_data.scanLine(y);
                    for ( x=0; x < _original_data.width(); ++x )
                    {
                        destData = (unsigned char *)dest.scanLine(x);
                        destData[_original_data.height()-y-1] = srcData[x];
                    }
                }
                break;
            case Rotate180:
                dest.create(_original_data.width(), _original_data.height(), _original_data.depth());
                dest.setNumColors(_original_data.numColors());
                srcTable = (unsigned int *)_original_data.colorTable();
                destTable = (unsigned int *)dest.colorTable();
                for ( x=0; x < _original_data.numColors(); ++x )
                    destTable[x] = srcTable[x];
                for ( y=0; y < _original_data.height(); ++y )
                {
                    srcData = (unsigned char *)_original_data.scanLine(y);
                    destData = (unsigned char *)dest.scanLine(_original_data.height()-y-1);
                    for ( x=0; x < _original_data.width(); ++x )
                        destData[_original_data.width()-x-1] = srcData[x];
                }
                break;
            case Rotate270:
                dest.create(_original_data.height(), _original_data.width(), _original_data.depth());
                dest.setNumColors(_original_data.numColors());
                srcTable = (unsigned int *)_original_data.colorTable();
                destTable = (unsigned int *)dest.colorTable();
                for ( x=0; x < _original_data.numColors(); ++x )
                    destTable[x] = srcTable[x];
                for ( y=0; y < _original_data.height(); ++y )
                {
                    srcData = (unsigned char *)_original_data.scanLine(y);
                    for ( x=0; x < _original_data.width(); ++x )
                    {
                        destData = (unsigned char *)dest.scanLine(_original_data.width()-x-1);
                        destData[y] = srcData[x];
                    }
                }
                break;
            default:
                dest = _original_data;
                break;
        }

    }
    _image_data = dest;
}

void ImageScrollView::generateImage()
{
    Rotation r = Rotate0;
    if (width()>height()&&_original_data.width()<_original_data.height() ||
        width()<height()&&_original_data.width()>_original_data.height()) {
        if (rotate_to_fit) r = Rotate90;
    }
    odebug << " r = " << r << oendl;
    if (scale_to_fit) {
        if (!_image_data.size().isValid()||width()>_image_data.width()||height()>_image_data.height()) {
            odebug << "Rescaling data" << oendl;
            if (r==Rotate0) {
                _image_data = _original_data;
            } else {
                rotate_into_data(r);
            }
        }
        rescaleImage(width(),height());
        resizeContents(_image_data.width(),_image_data.height());
    }  else if (!first_resize_done||r!=last_rot||_image_data.width()==0) {
        if (r==Rotate0) {
            _image_data = _original_data;
        } else {
            rotate_into_data(r);
        }
        last_rot = r;
        resizeContents(_image_data.width(),_image_data.height());
    }
    _pdata.convertFromImage(_image_data);
    _image_data=QImage();
}

void ImageScrollView::resizeEvent(QResizeEvent * e)
{
    odebug << "ImageScrollView resizeEvent" << oendl;
    QScrollView::resizeEvent(e);
    generateImage();
    first_resize_done = true;
}

void ImageScrollView::keyPressEvent(QKeyEvent * e)
{
    if (!e) return;
    int dx = horizontalScrollBar()->lineStep();
    int dy = verticalScrollBar()->lineStep();
    if (e->key()==Qt::Key_Right) {
        scrollBy(dx,0);
        e->accept();
    } else if (e->key()==Qt::Key_Left) {
        scrollBy(0-dx,0);
        e->accept();
    } else if (e->key()==Qt::Key_Up) {
        scrollBy(0,0-dy);
        e->accept();
    } else if (e->key()==Qt::Key_Down) {
        scrollBy(0,dy);
        e->accept();
    } else {
        e->ignore();
    }
    QScrollView::keyPressEvent(e);
}

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 (!_pdata.size().isValid()) {
        p->fillRect(clipx,clipy,clipw,cliph,white);
        return;
    }
    if (w>_pdata.width()) {
        w=_pdata.width();
        x = 0;
        erase = true;
    } else if (x+w>_pdata.width()){
        x = _pdata.width()-w;
    }
    if (h>_pdata.height()) {
        h=_pdata.height();
        y = 0;
        erase = true;
    } else if (y+h>_pdata.height()){
        y = _pdata.height()-h;
    }
    if (erase||_original_data.hasAlphaBuffer()) {
        p->fillRect(clipx,clipy,clipw,cliph,white);
    }
    p->drawPixmap(clipx,clipy,_pdata,x,y,w,h);
}

/* using the real geometry points and not the translated points is wanted! */
void ImageScrollView::viewportMouseMoveEvent(QMouseEvent* e)
{
    int mx, my;
    mx = e->x();
    my = e->y();
    if (_mouseStartPosX!=-1 && _mouseStartPosY!=-1) {
        int diffx = _mouseStartPosX-mx;
        int diffy = _mouseStartPosY-my;
#if 0
        QScrollBar*xbar = horizontalScrollBar();
        QScrollBar*ybar = verticalScrollBar();
        if (xbar->value()+diffx>xbar->maxValue()) {
            diffx = xbar->maxValue()-xbar->value();
        } else if (xbar->value()+diffx<0) {
            diffx=0-xbar->value();
        }
        if (ybar->value()+diffy>ybar->maxValue()) {
            diffy = ybar->maxValue()-ybar->value();
        } else if (ybar->value()+diffy<0) {
            diffy=0-ybar->value();
        }
#endif
        scrollBy(diffx,diffy);
    }
    _mouseStartPosX=mx;
    _mouseStartPosY=my;
}

void ImageScrollView::contentsMousePressEvent ( QMouseEvent * )
{
    /* this marks the beginning of a possible mouse move. Due internal reasons of QT
       the geometry values here may real differ from that set in MoveEvent (I don't know
       why). For getting them in real context, we use the first move-event to set the start
       position ;) 
    */
    _mouseStartPosX = -1;
    _mouseStartPosY = -1;
}

void ImageScrollView::setDestructiveClose() {
    WFlags fl = getWFlags();
    /* clear it just in case */
    fl &= ~WDestructiveClose;
    fl |= WDestructiveClose;
    setWFlags( fl );
}