1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the tools applications of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qvfbview.h"
|
---|
43 | #include "qvfbshmem.h"
|
---|
44 | #include "qvfbmmap.h"
|
---|
45 |
|
---|
46 | #include "qanimationwriter.h"
|
---|
47 | #include <QApplication>
|
---|
48 | #include <QPainter>
|
---|
49 | #include <QImage>
|
---|
50 | #include <QBitmap>
|
---|
51 | #include <QMatrix>
|
---|
52 | #include <QPaintEvent>
|
---|
53 | #include <QScrollArea>
|
---|
54 | #include <QFile>
|
---|
55 | #include <QDebug>
|
---|
56 |
|
---|
57 | #ifdef Q_WS_X11
|
---|
58 | #include <QX11EmbedContainer>
|
---|
59 | #include <QHBoxLayout>
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #include <stdlib.h>
|
---|
63 | #include <unistd.h>
|
---|
64 | #include <sys/ipc.h>
|
---|
65 | #include <sys/types.h>
|
---|
66 | #include <sys/shm.h>
|
---|
67 | #include <sys/stat.h>
|
---|
68 | #include <sys/sem.h>
|
---|
69 | #include <fcntl.h>
|
---|
70 | #include <errno.h>
|
---|
71 | #include <math.h>
|
---|
72 |
|
---|
73 | QT_BEGIN_NAMESPACE
|
---|
74 |
|
---|
75 | extern int qvfb_protocol;
|
---|
76 |
|
---|
77 | QVFbAbstractView::QVFbAbstractView( QWidget *parent )
|
---|
78 | #ifdef QVFB_USE_GLWIDGET
|
---|
79 | : QGLWidget( parent )
|
---|
80 | #else
|
---|
81 | : QWidget( parent )
|
---|
82 | #endif
|
---|
83 | {
|
---|
84 | }
|
---|
85 |
|
---|
86 | QVFbAbstractView::~QVFbAbstractView()
|
---|
87 | {
|
---|
88 | }
|
---|
89 |
|
---|
90 | QVFbView::QVFbView(int id, int w, int h, int d, Rotation r, QWidget *parent)
|
---|
91 | : QVFbAbstractView(parent),
|
---|
92 | viewdepth(d), viewFormat(DefaultFormat), rsh(0), gsh(0), bsh(0), rmax(15), gmax(15), bmax(15),
|
---|
93 | contentsWidth(w), contentsHeight(h), gred(1.0), ggreen(1.0), gblue(1.0),
|
---|
94 | gammatable(0), refreshRate(30), animation(0),
|
---|
95 | hzm(0.0), vzm(0.0), mView(0),
|
---|
96 | emulateTouchscreen(false), emulateLcdScreen(false), rotation(r)
|
---|
97 | #ifdef Q_WS_X11
|
---|
98 | , embedContainer(0)
|
---|
99 | #endif
|
---|
100 | {
|
---|
101 | switch(qvfb_protocol) {
|
---|
102 | default:
|
---|
103 | case 0:
|
---|
104 | mView = new QShMemViewProtocol(id, QSize(w, h), d, this);
|
---|
105 | break;
|
---|
106 | case 1:
|
---|
107 | mView = new QMMapViewProtocol(id, QSize(w, h), d, this);
|
---|
108 | break;
|
---|
109 | }
|
---|
110 |
|
---|
111 | connect(mView, SIGNAL(displayDataChanged(const QRect &)),
|
---|
112 | SLOT(refreshDisplay(const QRect &)));
|
---|
113 | #ifdef Q_WS_X11
|
---|
114 | connect(mView, SIGNAL(displayEmbedRequested(WId)),
|
---|
115 | this, SLOT(embedDisplay(WId)));
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | setAttribute(Qt::WA_PaintOnScreen, viewFormat != ARGBFormat);
|
---|
119 | setMouseTracking(true);
|
---|
120 | setFocusPolicy(Qt::StrongFocus);
|
---|
121 | setAttribute(Qt::WA_NoSystemBackground);
|
---|
122 |
|
---|
123 | setZoom(1.0,1.0);
|
---|
124 |
|
---|
125 | setGamma(1.0,1.0,1.0);
|
---|
126 | mView->setRate(30);
|
---|
127 | }
|
---|
128 |
|
---|
129 | QVFbView::~QVFbView()
|
---|
130 | {
|
---|
131 | stopAnimation();
|
---|
132 | sendKeyboardData(0, 0, 0, true, false); // magic die key
|
---|
133 | #ifdef Q_WS_X11
|
---|
134 | delete embedContainer;
|
---|
135 | #endif
|
---|
136 | }
|
---|
137 |
|
---|
138 | QSize QVFbView::sizeHint() const
|
---|
139 | {
|
---|
140 | return QSize(contentsWidth, contentsHeight);
|
---|
141 | }
|
---|
142 |
|
---|
143 | void QVFbView::setRate(int i)
|
---|
144 | {
|
---|
145 | mView->setRate(i);
|
---|
146 | }
|
---|
147 |
|
---|
148 | void QVFbView::setGamma(double gr, double gg, double gb)
|
---|
149 | {
|
---|
150 | gred = gr; ggreen = gg; gblue = gb;
|
---|
151 |
|
---|
152 | switch (viewdepth) {
|
---|
153 | case 12:
|
---|
154 | rsh = 12;
|
---|
155 | gsh = 7;
|
---|
156 | bsh = 1;
|
---|
157 | rmax = 15;
|
---|
158 | gmax = 15;
|
---|
159 | bmax = 15;
|
---|
160 | break;
|
---|
161 | case 15:
|
---|
162 | rsh = 10;
|
---|
163 | gsh = 5;
|
---|
164 | bsh = 0;
|
---|
165 | rmax = 31;
|
---|
166 | gmax = 31;
|
---|
167 | bmax = 31;
|
---|
168 | break;
|
---|
169 | case 16:
|
---|
170 | rsh = 11;
|
---|
171 | gsh = 5;
|
---|
172 | bsh = 0;
|
---|
173 | rmax = 31;
|
---|
174 | gmax = 63;
|
---|
175 | bmax = 31;
|
---|
176 | break;
|
---|
177 | case 18:
|
---|
178 | rsh = 12;
|
---|
179 | gsh = 6;
|
---|
180 | bsh = 0;
|
---|
181 | rmax = 63;
|
---|
182 | gmax = 63;
|
---|
183 | bmax = 63;
|
---|
184 | break;
|
---|
185 | case 24:
|
---|
186 | case 32:
|
---|
187 | rsh = 16;
|
---|
188 | gsh = 8;
|
---|
189 | bsh = 0;
|
---|
190 | rmax = 255;
|
---|
191 | gmax = 255;
|
---|
192 | bmax = 255;
|
---|
193 | }
|
---|
194 | int mm = qMax(rmax,qMax(gmax,bmax))+1;
|
---|
195 | if (gammatable)
|
---|
196 | delete [] gammatable;
|
---|
197 | gammatable = new QRgb[mm];
|
---|
198 | for (int i=0; i<mm; i++) {
|
---|
199 | int r = int(pow(i,gr)*255/rmax);
|
---|
200 | int g = int(pow(i,gg)*255/gmax);
|
---|
201 | int b = int(pow(i,gb)*255/bmax);
|
---|
202 | if (r > 255) r = 255;
|
---|
203 | if (g > 255) g = 255;
|
---|
204 | if (b > 255) b = 255;
|
---|
205 | gammatable[i] = qRgb(r,g,b);
|
---|
206 | //qDebug("%d: %d,%d,%d",i,r,g,b);
|
---|
207 | }
|
---|
208 |
|
---|
209 | mView->flushChanges();
|
---|
210 | }
|
---|
211 |
|
---|
212 | void QVFbView::getGamma(int i, QRgb& rgb)
|
---|
213 | {
|
---|
214 | if (i > 255) i = 255;
|
---|
215 | if (i < 0) i = 0;
|
---|
216 | rgb = qRgb(qRed(gammatable[i*rmax/255]),
|
---|
217 | qGreen(gammatable[i*rmax/255]),
|
---|
218 | qBlue(gammatable[i*rmax/255]));
|
---|
219 | }
|
---|
220 |
|
---|
221 | int QVFbView::displayId() const
|
---|
222 | {
|
---|
223 | return mView->id();
|
---|
224 | }
|
---|
225 |
|
---|
226 | int QVFbView::displayWidth() const
|
---|
227 | {
|
---|
228 | return mView->width();
|
---|
229 | }
|
---|
230 |
|
---|
231 | int QVFbView::displayHeight() const
|
---|
232 | {
|
---|
233 | return mView->height();
|
---|
234 | }
|
---|
235 |
|
---|
236 | int QVFbView::displayDepth() const
|
---|
237 | {
|
---|
238 | return viewdepth;
|
---|
239 | }
|
---|
240 |
|
---|
241 | QVFbView::PixelFormat QVFbView::displayFormat() const
|
---|
242 | {
|
---|
243 | return viewFormat;
|
---|
244 | }
|
---|
245 |
|
---|
246 | QVFbView::Rotation QVFbView::displayRotation() const
|
---|
247 | {
|
---|
248 | return rotation;
|
---|
249 | }
|
---|
250 |
|
---|
251 | void QVFbView::setZoom(double hz, double vz)
|
---|
252 | {
|
---|
253 | if (hzm != hz || vzm != vz) {
|
---|
254 | hzm = hz;
|
---|
255 | vzm = vz;
|
---|
256 | mView->flushChanges();
|
---|
257 |
|
---|
258 | contentsWidth = int(displayWidth()*hz);
|
---|
259 | contentsHeight = int(displayHeight()*vz);
|
---|
260 | if (rotation & 1)
|
---|
261 | qSwap(contentsWidth,contentsHeight);
|
---|
262 | resize(contentsWidth, contentsHeight);
|
---|
263 |
|
---|
264 | if (isVisible()) {
|
---|
265 | updateGeometry();
|
---|
266 | qApp->sendPostedEvents();
|
---|
267 | topLevelWidget()->adjustSize();
|
---|
268 | update();
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | void QVFbView::setRotation(QVFbView::Rotation r)
|
---|
274 | {
|
---|
275 | rotation = r;
|
---|
276 | // Force update...
|
---|
277 | double ohzm = hzm;
|
---|
278 | hzm=0.0;
|
---|
279 | setZoom(ohzm,vzm);
|
---|
280 | }
|
---|
281 |
|
---|
282 | static QRect mapToDevice(const QRect &r, const QSize &s, QVFbView::Rotation rotation)
|
---|
283 | {
|
---|
284 | int x1 = r.x();
|
---|
285 | int y1 = r.y();
|
---|
286 | int x2 = r.right();
|
---|
287 | int y2 = r.bottom();
|
---|
288 | int w = s.width();
|
---|
289 | int h = s.height();
|
---|
290 | switch (rotation) {
|
---|
291 | case QVFbView::Rot90:
|
---|
292 | return QRect(
|
---|
293 | QPoint(y1, w - x1),
|
---|
294 | QPoint(y2, w - x2)).normalized();
|
---|
295 | case QVFbView::Rot180:
|
---|
296 | return QRect(
|
---|
297 | QPoint(w - x1, h - y1),
|
---|
298 | QPoint(w - x2, h - y2)).normalized();
|
---|
299 | case QVFbView::Rot270:
|
---|
300 | return QRect(
|
---|
301 | QPoint(h - y1, x1),
|
---|
302 | QPoint(h - y2, x2)).normalized();
|
---|
303 | default:
|
---|
304 | break;
|
---|
305 | }
|
---|
306 | return r;
|
---|
307 | }
|
---|
308 |
|
---|
309 | static QRect mapFromDevice(const QRect &r, const QSize &s, QVFbView::Rotation rotation)
|
---|
310 | {
|
---|
311 | return mapToDevice(r,s,QVFbView::Rotation(4-(int)rotation));
|
---|
312 | }
|
---|
313 |
|
---|
314 | void QVFbView::sendMouseData(const QPoint &pos, int buttons, int wheel)
|
---|
315 | {
|
---|
316 | QPoint p = mapToDevice(QRect(pos,QSize(1,1)), QSize(int(width()/hzm), int(height()/vzm)), rotation).topLeft();
|
---|
317 | mView->sendMouseData(p, buttons, wheel);
|
---|
318 | }
|
---|
319 |
|
---|
320 | void QVFbView::sendKeyboardData(QString unicode, int keycode, int modifiers,
|
---|
321 | bool press, bool repeat)
|
---|
322 | {
|
---|
323 | mView->sendKeyboardData(unicode, keycode, modifiers, press, repeat);
|
---|
324 | }
|
---|
325 |
|
---|
326 | void QVFbView::refreshDisplay(const QRect &r)
|
---|
327 | {
|
---|
328 | if (animation) {
|
---|
329 | if (r.isEmpty()) {
|
---|
330 | animation->appendBlankFrame();
|
---|
331 | } else {
|
---|
332 | int l;
|
---|
333 | QImage img = getBuffer(r, l);
|
---|
334 | animation->appendFrame(img,QPoint(r.x(),r.y()));
|
---|
335 | }
|
---|
336 | }
|
---|
337 | if (!r.isNull()) {
|
---|
338 | if (hzm == 1.0 && vzm == 1.0) // hw: workaround for 4.3.1
|
---|
339 | update(mapFromDevice(r, QSize(displayWidth(), displayHeight()), rotation));
|
---|
340 | else
|
---|
341 | update();
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | static void dim(QRgb* rgb, int n, int brightness)
|
---|
346 | {
|
---|
347 | uchar* b = (uchar*)rgb;
|
---|
348 | #if Q_BYTE_ORDER == Q_BIG_ENDIAN
|
---|
349 | b++;
|
---|
350 | #endif
|
---|
351 | while (n--) {
|
---|
352 | b[0] = (uint)b[0] * brightness / 255;
|
---|
353 | b[1] = (uint)b[1] * brightness / 255;
|
---|
354 | b[2] = (uint)b[2] * brightness / 255;
|
---|
355 | b += 4;
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | QImage QVFbView::getBuffer(const QRect &r, int &leading) const
|
---|
360 | {
|
---|
361 | const int brightness = mView->brightness();
|
---|
362 | if ( brightness == 0 ) {
|
---|
363 | QImage img(r.size(),QImage::Format_RGB32);
|
---|
364 | img.fill(0);
|
---|
365 | leading = 0;
|
---|
366 | return img;
|
---|
367 | }
|
---|
368 |
|
---|
369 | static QByteArray buffer;
|
---|
370 |
|
---|
371 | const int requiredSize = r.width() * r.height() * 4;
|
---|
372 |
|
---|
373 | QImage img;
|
---|
374 | leading = 0;
|
---|
375 |
|
---|
376 | switch (viewdepth) {
|
---|
377 | case 1: {
|
---|
378 | if (requiredSize > buffer.size())
|
---|
379 | buffer.resize(requiredSize);
|
---|
380 |
|
---|
381 | // XXX: hw: replace by drawhelper functionality
|
---|
382 |
|
---|
383 | const int pixelsPerByte = 8;
|
---|
384 | quint8 *src = reinterpret_cast<quint8*>(mView->data())
|
---|
385 | + r.y() * mView->linestep() + r.x() / pixelsPerByte;
|
---|
386 | const int align = qMin(r.width(), (8 - (r.x() & 7)) & 7);
|
---|
387 | const int doAlign = (align > 0 ? 1 : 0);
|
---|
388 | const int tail = qMin(r.width(), (r.width() - align) & 7);
|
---|
389 | const int doTail = (tail > 0 ? 1 : 0);
|
---|
390 | const int width8 = (r.width() - align) / pixelsPerByte;
|
---|
391 | const int stride = mView->linestep() - (width8 + doAlign);
|
---|
392 |
|
---|
393 | uchar *b = reinterpret_cast<uchar*>(buffer.data());
|
---|
394 | img = QImage(b, r.width(), r.height(), QImage::Format_RGB32);
|
---|
395 | for (int y = 0; y < r.height(); ++y) {
|
---|
396 | quint32 *dest = reinterpret_cast<quint32*>(img.scanLine(y));
|
---|
397 | quint8 c;
|
---|
398 |
|
---|
399 | if (doAlign) {
|
---|
400 | switch (align) {
|
---|
401 | case 7: c = ((*src & 0x40) >> 6) * 0xff;
|
---|
402 | *dest++ = qRgb(c, c, c);
|
---|
403 | case 6: c = ((*src & 0x20) >> 5) * 0xff;
|
---|
404 | *dest++ = qRgb(c, c, c);
|
---|
405 | case 5: c = ((*src & 0x10) >> 4) * 0xff;
|
---|
406 | *dest++ = qRgb(c, c, c);
|
---|
407 | case 4: c = ((*src & 0x08) >> 3) * 0xff;
|
---|
408 | *dest++ = qRgb(c, c, c);
|
---|
409 | case 3: c = ((*src & 0x04) >> 2) * 0xff;
|
---|
410 | *dest++ = qRgb(c, c, c);
|
---|
411 | case 2: c = ((*src & 0x02) >> 1) * 0xff;
|
---|
412 | *dest++ = qRgb(c, c, c);
|
---|
413 | case 1: c = ((*src & 0x01)) * 0xff;
|
---|
414 | *dest++ = qRgb(c, c, c);
|
---|
415 | }
|
---|
416 | ++src;
|
---|
417 | }
|
---|
418 | for (int i = 0; i < width8; ++i) {
|
---|
419 | c = ((*src & 0x80) >> 7) * 0xff;
|
---|
420 | *dest++ = qRgb(c, c, c);
|
---|
421 | c = ((*src & 0x40) >> 6) * 0xff;
|
---|
422 | *dest++ = qRgb(c, c, c);
|
---|
423 | c = ((*src & 0x20) >> 5) * 0xff;
|
---|
424 | *dest++ = qRgb(c, c, c);
|
---|
425 | c = ((*src & 0x10) >> 4) * 0xff;
|
---|
426 | *dest++ = qRgb(c, c, c);
|
---|
427 | c = ((*src & 0x08) >> 3) * 0xff;
|
---|
428 | *dest++ = qRgb(c, c, c);
|
---|
429 | c = ((*src & 0x04) >> 2) * 0xff;
|
---|
430 | *dest++ = qRgb(c, c, c);
|
---|
431 | c = ((*src & 0x02) >> 1) * 0xff;
|
---|
432 | *dest++ = qRgb(c, c, c);
|
---|
433 | c = ((*src & 0x01)) * 0xff;
|
---|
434 | *dest++ = qRgb(c, c, c);
|
---|
435 |
|
---|
436 | ++src;
|
---|
437 | }
|
---|
438 | if (doTail) {
|
---|
439 | switch (tail) {
|
---|
440 | case 7: c = ((*src & 0x02) >> 1) * 0xff;
|
---|
441 | dest[6] = qRgb(c, c, c);
|
---|
442 | case 6: c = ((*src & 0x04) >> 2) * 0xff;
|
---|
443 | dest[5] = qRgb(c, c, c);
|
---|
444 | case 5: c = ((*src & 0x08) >> 3) * 0xff;
|
---|
445 | dest[4] = qRgb(c, c, c);
|
---|
446 | case 4: c = ((*src & 0x10) >> 4) * 0xff;
|
---|
447 | dest[3] = qRgb(c, c, c);
|
---|
448 | case 3: c = ((*src & 0x20) >> 5) * 0xff;
|
---|
449 | dest[2] = qRgb(c, c, c);
|
---|
450 | case 2: c = ((*src & 0x40) >> 6) * 0xff;
|
---|
451 | dest[1] = qRgb(c, c, c);
|
---|
452 | case 1: c = ((*src & 0x80) >> 7) * 0xff;
|
---|
453 | dest[0] = qRgb(c, c, c);
|
---|
454 | }
|
---|
455 | }
|
---|
456 | src += stride;
|
---|
457 | }
|
---|
458 | break;
|
---|
459 | }
|
---|
460 | case 4: {
|
---|
461 | if (requiredSize > buffer.size())
|
---|
462 | buffer.resize(requiredSize);
|
---|
463 |
|
---|
464 | // XXX: hw: replace by drawhelper functionality
|
---|
465 |
|
---|
466 | const int pixelsPerByte = 2;
|
---|
467 | const int doAlign = r.x() & 1;
|
---|
468 | const int doTail = (r.width() - doAlign) & 1;
|
---|
469 | const int width8 = (r.width() - doAlign) / pixelsPerByte;
|
---|
470 |
|
---|
471 | uchar *b = reinterpret_cast<uchar*>(buffer.data());
|
---|
472 | img = QImage(b, r.width(), r.height(), QImage::Format_RGB32);
|
---|
473 | for (int y = 0; y < r.height(); ++y) {
|
---|
474 | const quint8 *sptr = mView->data()
|
---|
475 | + (r.y() + y) * mView->linestep()
|
---|
476 | + r.x() / pixelsPerByte;
|
---|
477 | quint32 *dptr = reinterpret_cast<quint32*>(img.scanLine(y));
|
---|
478 |
|
---|
479 | if (doAlign) {
|
---|
480 | quint8 c = (*sptr++ & 0x0f);
|
---|
481 | c |= (c << 4);
|
---|
482 | *dptr++ = qRgb(c, c, c);
|
---|
483 | }
|
---|
484 |
|
---|
485 | for (int i = 0; i < width8; ++i) {
|
---|
486 | quint8 c1 = (*sptr >> 4);
|
---|
487 | quint8 c2 = (*sptr & 0x0f);
|
---|
488 | c1 |= (c1 << 4);
|
---|
489 | c2 |= (c2 << 4);
|
---|
490 | *dptr++ = qRgb(c1, c1, c1);
|
---|
491 | *dptr++ = qRgb(c2, c2, c2);
|
---|
492 | ++sptr;
|
---|
493 | }
|
---|
494 |
|
---|
495 | if (doTail) {
|
---|
496 | quint8 c = *sptr >> 4;
|
---|
497 | c |= (c << 4);
|
---|
498 | *dptr = qRgb(c, c, c);
|
---|
499 | }
|
---|
500 | }
|
---|
501 | break;
|
---|
502 | }
|
---|
503 | case 12:
|
---|
504 | img = QImage((const uchar*)(mView->data() + r.y() * mView->linestep() + r.x() * 2),
|
---|
505 | r.width(), r.height(), mView->linestep(),
|
---|
506 | QImage::Format_RGB444);
|
---|
507 | break;
|
---|
508 | case 15:
|
---|
509 | img = QImage((const uchar*)(mView->data() + r.y() * mView->linestep() + r.x() * 2),
|
---|
510 | r.width(), r.height(), mView->linestep(),
|
---|
511 | QImage::Format_RGB555);
|
---|
512 | break;
|
---|
513 | case 16:
|
---|
514 | img = QImage((const uchar*)(mView->data() + r.y() * mView->linestep() + r.x() * 2),
|
---|
515 | r.width(), r.height(), mView->linestep(),
|
---|
516 | QImage::Format_RGB16);
|
---|
517 | break;
|
---|
518 | case 18:
|
---|
519 | img = QImage((const uchar*)(mView->data() + r.y() * mView->linestep() + r.x() * 3),
|
---|
520 | r.width(), r.height(), mView->linestep(),
|
---|
521 | QImage::Format_RGB666);
|
---|
522 | break;
|
---|
523 | case 24:
|
---|
524 | img = QImage((const uchar*)(mView->data() + r.y() * mView->linestep() + r.x() * 3),
|
---|
525 | r.width(), r.height(), mView->linestep(),
|
---|
526 | QImage::Format_RGB888);
|
---|
527 | break;
|
---|
528 | case 32:
|
---|
529 | img = QImage((const uchar*)(mView->data() + r.y() * mView->linestep() + r.x() * 4),
|
---|
530 | r.width(), r.height(), mView->linestep(),
|
---|
531 | viewFormat == ARGBFormat ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32);
|
---|
532 | break;
|
---|
533 | case 8:
|
---|
534 | img = QImage(mView->data() + r.y() * mView->linestep() + r.x(),
|
---|
535 | r.width(), r.height(), mView->linestep(),
|
---|
536 | QImage::Format_Indexed8);
|
---|
537 | img.setColorTable(mView->clut());
|
---|
538 | if (img.numColors() <= 0)
|
---|
539 | img = QImage();
|
---|
540 | break;
|
---|
541 | }
|
---|
542 |
|
---|
543 | if ( brightness != 255 ) {
|
---|
544 | if (img.format() == QImage::Format_Indexed8) {
|
---|
545 | QVector<QRgb> c = img.colorTable();
|
---|
546 | dim(c.data(),c.count(),brightness);
|
---|
547 | img.setColorTable(c);
|
---|
548 | } else {
|
---|
549 | if ( img.format() != QImage::Format_ARGB32_Premultiplied )
|
---|
550 | img = img.convertToFormat(QImage::Format_RGB32);
|
---|
551 |
|
---|
552 | // NOTE: calling bits() may change numBytes(), so do not
|
---|
553 | // pass them as parameters (which are evaluated right-to-left).
|
---|
554 | QRgb *b = (QRgb*)img.bits();
|
---|
555 | int n = img.numBytes()/4;
|
---|
556 | dim(b,n,brightness);
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | return img;
|
---|
561 | }
|
---|
562 |
|
---|
563 | static int findMultiple(int start, double m, int limit, int step)
|
---|
564 | {
|
---|
565 | int r = start;
|
---|
566 | while (r != limit) {
|
---|
567 | if (int(int(r * m)/m) == r)
|
---|
568 | break;
|
---|
569 | r += step;
|
---|
570 | }
|
---|
571 | return r;
|
---|
572 | }
|
---|
573 |
|
---|
574 | void QVFbView::drawScreen(const QRect &rect)
|
---|
575 | {
|
---|
576 | QRect r = QRect(0, 0, mView->width(), mView->height());
|
---|
577 |
|
---|
578 | if (hzm == 1.0 && vzm == 1.0) // hw: workaround for 4.3.1
|
---|
579 | r &= rect;
|
---|
580 |
|
---|
581 | if (int(hzm) != hzm || int(vzm) != vzm) {
|
---|
582 | r.setLeft(findMultiple(r.left(),hzm,0,-1));
|
---|
583 | r.setTop(findMultiple(r.top(),vzm,0,-1));
|
---|
584 | int w = findMultiple(r.width(),hzm,mView->width(),1);
|
---|
585 | int h = findMultiple(r.height(),vzm,mView->height(),1);
|
---|
586 | r.setRight(r.left()+w-1);
|
---|
587 | r.setBottom(r.top()+h-1);
|
---|
588 | }
|
---|
589 |
|
---|
590 | int leading;
|
---|
591 | const QImage img = getBuffer(r, leading);
|
---|
592 |
|
---|
593 | QPixmap pm;
|
---|
594 | if (hzm == 1.0 && vzm == 1.0) {
|
---|
595 | pm = QPixmap::fromImage(img);
|
---|
596 | } else if (emulateLcdScreen && hzm == 3.0 && vzm == 3.0) {
|
---|
597 | QImage img2(img.width()*3, img.height(), QImage::Format_RGB32);
|
---|
598 | for (int row = 0; row < img2.height(); row++) {
|
---|
599 | QRgb *dptr = (QRgb*)img2.scanLine(row);
|
---|
600 | QRgb *sptr = (QRgb*)img.scanLine(row);
|
---|
601 | for (int col = 0; col < img.width(); col++) {
|
---|
602 | QRgb s = *sptr++;
|
---|
603 | *dptr++ = qRgb(qRed(s),0,0);
|
---|
604 | *dptr++ = qRgb(0,qGreen(s),0);
|
---|
605 | *dptr++ = qRgb(0,0,qBlue(s));
|
---|
606 | }
|
---|
607 | }
|
---|
608 | QMatrix m;
|
---|
609 | m.scale(1.0, 3.0);
|
---|
610 | pm = QPixmap::fromImage(img2);
|
---|
611 | pm = pm.transformed(m);
|
---|
612 | } else if (int(hzm) == hzm && int(vzm) == vzm) {
|
---|
613 | QMatrix m;
|
---|
614 | m.scale(hzm,vzm);
|
---|
615 | pm = QPixmap::fromImage(img);
|
---|
616 | pm = pm.transformed(m);
|
---|
617 | } else {
|
---|
618 | pm = QPixmap::fromImage(img.scaled(int(img.width()*hzm),int(img.height()*vzm), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
|
---|
619 | }
|
---|
620 |
|
---|
621 | int x1 = r.x();
|
---|
622 | int y1 = r.y();
|
---|
623 | int leadingX = leading;
|
---|
624 | int leadingY = 0;
|
---|
625 |
|
---|
626 | // Do the rotation thing
|
---|
627 | int rotX1 = mView->width() - x1 - img.width();
|
---|
628 | int rotY1 = mView->height() - y1 - img.height();
|
---|
629 | int rotLeadingX = (leading) ? mView->width() - leadingX - img.width() : 0;
|
---|
630 | int rotLeadingY = 0;
|
---|
631 | switch (rotation) {
|
---|
632 | case Rot0:
|
---|
633 | break;
|
---|
634 | case Rot90:
|
---|
635 | leadingY = leadingX;
|
---|
636 | leadingX = rotLeadingY;
|
---|
637 | y1 = x1;
|
---|
638 | x1 = rotY1;
|
---|
639 | break;
|
---|
640 | case Rot180:
|
---|
641 | leadingX = rotLeadingX;
|
---|
642 | leadingY = leadingY;
|
---|
643 | x1 = rotX1;
|
---|
644 | y1 = rotY1;
|
---|
645 | break;
|
---|
646 | case Rot270:
|
---|
647 | leadingX = leadingY;
|
---|
648 | leadingY = rotLeadingX;
|
---|
649 | x1 = y1;
|
---|
650 | y1 = rotX1;
|
---|
651 | break;
|
---|
652 | default:
|
---|
653 | break;
|
---|
654 | }
|
---|
655 | x1 = int(x1*hzm);
|
---|
656 | y1 = int(y1*vzm);
|
---|
657 | leadingX = int(leadingX*hzm);
|
---|
658 | leadingY = int(leadingY*vzm);
|
---|
659 | if (rotation != 0) {
|
---|
660 | QMatrix m;
|
---|
661 | m.rotate(rotation * 90.0);
|
---|
662 | pm = pm.transformed(m);
|
---|
663 | }
|
---|
664 |
|
---|
665 | QPainter p(this);
|
---|
666 | if (viewFormat == ARGBFormat) {
|
---|
667 | QPixmap bg(":/res/images/logo-nt.png");
|
---|
668 | p.fillRect(x1,y1,pm.width(), pm.height(), QBrush(bg));
|
---|
669 | }
|
---|
670 | p.drawPixmap(x1, y1, pm, leadingX, leadingY, pm.width(), pm.height());
|
---|
671 | }
|
---|
672 |
|
---|
673 | //bool QVFbView::eventFilter(QObject *obj, QEvent *e)
|
---|
674 | //{
|
---|
675 | // if (obj == this &&
|
---|
676 | // (e->type() == QEvent::FocusIn || e->type() == QEvent::FocusOut))
|
---|
677 | // return true;
|
---|
678 | //
|
---|
679 | // return QWidgetView::eventFilter(obj, e);
|
---|
680 | //}
|
---|
681 |
|
---|
682 | void QVFbView::paintEvent(QPaintEvent *e)
|
---|
683 | {
|
---|
684 | drawScreen(mapToDevice(e->rect(),QSize(int(width()/hzm), int(height()/vzm)),rotation));
|
---|
685 | }
|
---|
686 |
|
---|
687 | void QVFbView::mousePressEvent(QMouseEvent *e)
|
---|
688 | {
|
---|
689 | sendMouseData(QPoint(int(e->x()/hzm),int(e->y()/vzm)), e->buttons(), 0);
|
---|
690 | }
|
---|
691 |
|
---|
692 | void QVFbView::contextMenuEvent(QContextMenuEvent*)
|
---|
693 | {
|
---|
694 |
|
---|
695 | }
|
---|
696 |
|
---|
697 | void QVFbView::mouseDoubleClickEvent(QMouseEvent *e)
|
---|
698 | {
|
---|
699 | sendMouseData(QPoint(int(e->x()/hzm),int(e->y()/vzm)), e->buttons(), 0);
|
---|
700 | }
|
---|
701 |
|
---|
702 | void QVFbView::mouseReleaseEvent(QMouseEvent *e)
|
---|
703 | {
|
---|
704 | sendMouseData(QPoint(int(e->x()/hzm),int(e->y()/vzm)), e->buttons(), 0);
|
---|
705 | }
|
---|
706 |
|
---|
707 | void QVFbView::skinMouseEvent(QMouseEvent *e)
|
---|
708 | {
|
---|
709 | sendMouseData(QPoint(int(e->x()/hzm),int(e->y()/vzm)), e->buttons(), 0);
|
---|
710 | }
|
---|
711 |
|
---|
712 | void QVFbView::mouseMoveEvent(QMouseEvent *e)
|
---|
713 | {
|
---|
714 | if (!emulateTouchscreen || (e->buttons() & Qt::MouseButtonMask))
|
---|
715 | sendMouseData(QPoint(int(e->x()/hzm),int(e->y()/vzm)), e->buttons(), 0);
|
---|
716 | }
|
---|
717 |
|
---|
718 | void QVFbView::wheelEvent(QWheelEvent *e)
|
---|
719 | {
|
---|
720 | if (!e)
|
---|
721 | return;
|
---|
722 | sendMouseData(QPoint(int(e->x()/hzm),int(e->y()/vzm)), e->buttons(), e->delta());
|
---|
723 | }
|
---|
724 |
|
---|
725 | void QVFbView::setTouchscreenEmulation(bool b)
|
---|
726 | {
|
---|
727 | emulateTouchscreen = b;
|
---|
728 | }
|
---|
729 |
|
---|
730 | void QVFbView::setLcdScreenEmulation(bool b)
|
---|
731 | {
|
---|
732 | emulateLcdScreen = b;
|
---|
733 | }
|
---|
734 |
|
---|
735 | void QVFbView::setViewFormat(PixelFormat f)
|
---|
736 | {
|
---|
737 | if (viewFormat == f)
|
---|
738 | return;
|
---|
739 | viewFormat = f;
|
---|
740 | setAttribute(Qt::WA_PaintOnScreen, viewFormat != ARGBFormat);
|
---|
741 | }
|
---|
742 |
|
---|
743 | #ifdef Q_WS_X11
|
---|
744 | void QVFbView::embedDisplay(WId windowId)
|
---|
745 | {
|
---|
746 | if (windowId == 0) {
|
---|
747 | delete embedContainer;
|
---|
748 | embedContainer = 0;
|
---|
749 | return;
|
---|
750 | }
|
---|
751 |
|
---|
752 | if (!embedContainer) {
|
---|
753 | embedContainer = new QX11EmbedContainer(this);
|
---|
754 | embedContainer->setGeometry(rect());
|
---|
755 | embedContainer->show();
|
---|
756 | }
|
---|
757 | embedContainer->embedClient(windowId);
|
---|
758 | }
|
---|
759 | #endif
|
---|
760 |
|
---|
761 | bool QVFbView::event(QEvent *e)
|
---|
762 | {
|
---|
763 | if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease) {
|
---|
764 | QKeyEvent *ke = static_cast<QKeyEvent*>(e);
|
---|
765 | sendKeyboardData(ke->text(), ke->key(),
|
---|
766 | ke->modifiers()&(Qt::ShiftModifier|Qt::ControlModifier|Qt::AltModifier),
|
---|
767 | ke->type() == QEvent::KeyPress, ke->isAutoRepeat());
|
---|
768 | ke->accept();
|
---|
769 | return true;
|
---|
770 | }
|
---|
771 | return QVFbAbstractView::event(e);
|
---|
772 | }
|
---|
773 |
|
---|
774 | void QVFbView::keyPressEvent(QKeyEvent *e)
|
---|
775 | {
|
---|
776 | sendKeyboardData(e->text(), e->key(),
|
---|
777 | e->modifiers()&(Qt::ShiftModifier|Qt::ControlModifier|Qt::AltModifier),
|
---|
778 | true, e->isAutoRepeat());
|
---|
779 | }
|
---|
780 |
|
---|
781 | void QVFbView::keyReleaseEvent(QKeyEvent *e)
|
---|
782 | {
|
---|
783 | sendKeyboardData(e->text(), e->key(),
|
---|
784 | e->modifiers()&(Qt::ShiftModifier|Qt::ControlModifier|Qt::AltModifier),
|
---|
785 | false, e->isAutoRepeat());
|
---|
786 | }
|
---|
787 |
|
---|
788 |
|
---|
789 | QImage QVFbView::image() const
|
---|
790 | {
|
---|
791 | int l;
|
---|
792 | QImage r = getBuffer(QRect(0, 0, mView->width(), mView->height()), l).copy();
|
---|
793 | return r;
|
---|
794 | }
|
---|
795 |
|
---|
796 | void QVFbView::startAnimation(const QString& filename)
|
---|
797 | {
|
---|
798 | delete animation;
|
---|
799 | animation = new QAnimationWriter(filename,"MNG");
|
---|
800 | animation->setFrameRate(refreshRate);
|
---|
801 | animation->appendFrame(QImage(mView->data(),
|
---|
802 | mView->width(), mView->height(), QImage::Format_RGB32));
|
---|
803 | }
|
---|
804 |
|
---|
805 | void QVFbView::stopAnimation()
|
---|
806 | {
|
---|
807 | delete animation;
|
---|
808 | animation = 0;
|
---|
809 | }
|
---|
810 |
|
---|
811 |
|
---|
812 | void QVFbView::skinKeyPressEvent(int code, const QString& text, bool autorep)
|
---|
813 | {
|
---|
814 | QKeyEvent e(QEvent::KeyPress,code,0,text,autorep);
|
---|
815 | keyPressEvent(&e);
|
---|
816 | }
|
---|
817 |
|
---|
818 | void QVFbView::skinKeyReleaseEvent(int code, const QString& text, bool autorep)
|
---|
819 | {
|
---|
820 | QKeyEvent e(QEvent::KeyRelease,code,0,text,autorep);
|
---|
821 | keyReleaseEvent(&e);
|
---|
822 | }
|
---|
823 |
|
---|
824 | QT_END_NAMESPACE
|
---|