Debian Codegraph @

samples/qtrecompress/qimagecontrol.cpp

raw
1 #include "qimagecontrol.h"
2
3 #include <qapplication.h>
4 #include <qframe.h>
5 #include <qimage.h>
6 #include <qpainter.h>
7 #include <qpixmap.h>
8
9 #include <stdlib.h> // abort()
10 #include <stdio.h>
11
12 const QEvent::Type Type_LeftPix=QEvent::Type(QEvent::User);
13 const QEvent::Type Type_RightPix=QEvent::Type(QEvent::User+1);
14
15 QImageControl::QImageControl(QWidget* parent, const char* name):QWidget(parent,name),_il(0), _ir(0)
16 {
17 _left = new QFrame( this );
18 _right = new QFrame( this );
19 _left->setFrameShadow( QFrame::Sunken );
20 _left->setFrameShape( QFrame::Box );
21 _right->setFrameShadow( QFrame::Sunken );
22 _right->setFrameShape( QFrame::Box );
23 _il = _ir = 0;
24 //setSize( 192,144 );
25 }
26
27 QImageControl::~QImageControl()
28 {
29 delete _il;
30 delete _ir;
31 }
32
33 void QImageControl::setSize(int width, int height)
34 {
35 if (height<0)
36 height=-height;
37 _left->resize( width, height );
38 _right->resize( width, height );
39
40 int nw = width + 10;
41 int nh = 5;
42
43 if (height < 400)
44 {
45 nw = 5;
46 nh = height + 10;
47 }
48 _left->move( 5, 5 );
49 _right->move( nw, nh );
50 resize( width + 5 + nw, height + 5 + nh );
51 }
52
53 QImage* QImageControl::getQImage(const avm::CImage* im) const
54 {
55 int w = im->Width();
56 int h = im->Height();
57 int bitdepth = 32;
58 //printf("HEI %d\n", _h);
59 QImage* qi = new QImage( w, h, bitdepth );
60 avm::BitmapInfo bi(w, h, bitdepth);
61 avm::CImage ci(&bi, qi->scanLine(0), false);
62 ci.Convert(im);
63 return qi;
64 }
65
66 void QImageControl::setSourcePicture(const avm::CImage* src)
67 {
68 //printf("SRC:\n"); src->GetFmt()->Print();
69 m_Mutex.Lock();
70 QImage* s32 = getQImage(src);
71 delete _il;
72 _il = s32;
73 QEvent* e = new QEvent(Type_LeftPix);
74 qApp->postEvent(this, e);
75 m_Mutex.Unlock();
76 //qApp->sendPostedEvents();
77 }
78
79 void QImageControl::setDestPicture(const avm::CImage* src)
80 {
81 //printf("DST:\n"); src->GetFmt()->Print();
82 m_Mutex.Lock();
83 QImage* s32 = getQImage(src);
84 delete _ir;
85 _ir = s32;
86 QEvent* e = new QEvent(Type_RightPix);
87 qApp->postEvent(this, e);
88 m_Mutex.Unlock();
89 }
90
91 void QImageControl::setPicture(const QImage* qi, QFrame* frame)
92 {
93 if (qi)
94 {
95 QPainter p(frame);
96 p.drawImage( 0, 0, *qi );
97 }
98 }
99
100 void QImageControl::paintEvent(QPaintEvent* e)
101 {
102 avm::Locker locker(m_Mutex);
103 setPicture(_il, _left);
104 setPicture(_ir, _right);
105 }
106
107 bool QImageControl::event(QEvent* ev)
108 {
109 static int lastWidth=0, lastHeight=0;
110
111 if (ev->type()==Type_LeftPix)
112 {
113 avm::Locker locker(m_Mutex);
114 setPicture(_il, _left);
115 return true;
116 }
117 else if (ev->type()==Type_RightPix)
118 {
119 avm::Locker locker(m_Mutex);
120 // first clear the destination frame if the picture size has changed
121 if(_ir->width()!=lastWidth || _ir->height()!=lastHeight){
122 lastWidth=_ir->width();
123 lastHeight=_ir->height();
124 QPainter p(_right);
125 p.eraseRect(0,0,_il->width(),_il->height());
126 }
127 // then set the new picture
128 setPicture(_ir, _right);
129 return true;
130 }
131
132 if (ev->type() == QEvent::Paint)
133 return QWidget::event(ev); // this seems to help with flickering
134 return false;
135 }