source: trunk/tools/qvfb/qvfb.cpp@ 1039

Last change on this file since 1039 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 34.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the tools applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qvfb.h"
43#include "qvfbview.h"
44#include "qvfbhdr.h"
45#ifdef Q_WS_X11
46#include "qvfbx11view.h"
47#endif
48#include "qvfbratedlg.h"
49#include "ui_config.h"
50#include "qanimationwriter.h"
51
52#include <deviceskin.h>
53
54#include <QMenuBar>
55#include <QMenu>
56#include <QApplication>
57#include <QMessageBox>
58#include <QComboBox>
59#include <QLabel>
60#include <QFileDialog>
61#include <QSlider>
62#include <QSpinBox>
63#include <QLayout>
64#include <QRadioButton>
65#include <QImage>
66#include <QPixmap>
67#include <QCheckBox>
68#include <QCursor>
69#include <QTime>
70#include <QScrollArea>
71#include <QProgressBar>
72#include <QPushButton>
73#include <QTextStream>
74#include <QFile>
75#include <QFileInfo>
76#include <QDebug>
77
78#include <unistd.h>
79#include <stdlib.h>
80#include <sys/types.h>
81
82QT_BEGIN_NAMESPACE
83
84// =====================================================================
85
86static const char *red_on_led_xpm[] = {
87"11 11 10 1",
88" c None",
89". c #FF0000",
90"+ c #FF4C4C",
91"@ c #FF7A7A",
92"# c #D30000",
93"$ c #FF9393",
94"% c #BA0000",
95"& c #FFFFFF",
96"* c #7F0000",
97"= c #000000",
98" ",
99" .++@@ ",
100" .....+@ ",
101" ##...$.+@ ",
102" %#..$&$.+ ",
103" *#...$..+ ",
104" *%#...... ",
105" =*%#..... ",
106" =*%###. ",
107" ===*. ",
108" "};
109
110static const char *red_off_led_xpm[] = {
111"11 11 12 1",
112" c None",
113". c #CDB7B4",
114"+ c #D2BFBD",
115"@ c #DBCBCA",
116"# c #E5D9D8",
117"$ c #BC9E9B",
118"% c #E2D6D5",
119"& c #AD8986",
120"* c #FFFFFF",
121"= c #A8817D",
122"- c #B2908D",
123"; c #6F4D4A",
124" ",
125" .++@# ",
126" .....@# ",
127" $$...%.@# ",
128" &$..%*%.@ ",
129" =-...%..+ ",
130" =&-...... ",
131" ;==-..... ",
132" ;=&-$$. ",
133" ;==&$ ",
134" "};
135
136static bool copyButtonConfiguration(const QString &prefix, int displayId)
137{
138 const QString destDir = QT_VFB_DATADIR(displayId).append("/");
139 const QFileInfo src(prefix + QLatin1String("defaultbuttons.conf"));
140 const QFileInfo dst(destDir + QLatin1String("defaultbuttons.conf"));
141 unlink(dst.absoluteFilePath().toLatin1().constData());
142 if (!src.exists())
143 return false;
144 const bool rc = QFile::copy(src.absoluteFilePath(), dst.absoluteFilePath());
145 if (!rc)
146 qWarning() << "Failed to copy the button configuration file " << src.absoluteFilePath() << " to " << dst.absoluteFilePath() << '.';
147 return rc;
148}
149
150// =====================================================================
151
152class AnimationSaveWidget : public QWidget {
153 Q_OBJECT
154public:
155 AnimationSaveWidget(QVFbAbstractView *v);
156 ~AnimationSaveWidget();
157 bool detectPpmtoMpegCommand();
158 void timerEvent(QTimerEvent *te);
159 void convertToMpeg(QString filename);
160 void removeTemporaryFiles();
161protected slots:
162 void toggleRecord();
163 void reset();
164 void save();
165private:
166 QVFbAbstractView *view;
167 QProgressBar *progressBar;
168 QLabel *statusText;
169 bool haveMpeg, savingAsMpeg, recording;
170 QCheckBox *mpegSave;
171 QAnimationWriter *animation;
172 QPushButton *recBt, *resetBt, *saveBt;
173 QLabel *timeDpy, *recLED;
174 int timerId, progressTimerId;
175 QPixmap recOn, recOff;
176 QTime tm;
177 int elapsed, imageNum;
178};
179
180// =====================================================================
181
182Zoomer::Zoomer(QVFb* target) :
183 qvfb(target)
184{
185 QVBoxLayout *layout = new QVBoxLayout(this);
186 QSlider *sl = new QSlider(Qt::Horizontal);
187 sl->setMinimum(10);
188 sl->setMaximum(64);
189 sl->setPageStep(1);
190 sl->setValue(32);
191 layout->addWidget(sl);
192 connect(sl,SIGNAL(valueChanged(int)),this,SLOT(zoom(int)));
193 label = new QLabel();
194 layout->addWidget(label);
195}
196
197void Zoomer::zoom(int z)
198{
199 double d = (double)z/32.0;
200 qvfb->setZoom(d);
201 label->setText(QString::number(d,'g',2));
202}
203
204// =====================================================================
205
206QVFb::QVFb( int display_id, int w, int h, int d, int r, const QString &skin, DisplayType displayType, QWidget *parent, Qt::WindowFlags flags )
207 : QMainWindow( parent, flags )
208{
209 this->displayType = displayType;
210 view = 0;
211 secondaryView = 0;
212 scroller = 0;
213 this->skin = 0;
214 currentSkinIndex = -1;
215 findSkins(skin);
216 zoomer = 0;
217 QPixmap pix(":/res/images/logo.png");
218 setWindowIcon( pix );
219 rateDlg = 0;
220 refreshRate = 30;
221 // Create the menu first to avoid scroll bars in the main window
222 createMenu( menuBar() );
223 init( display_id, w, h, d, r, skin );
224 enableCursor( true );
225}
226
227QVFb::~QVFb()
228{
229}
230
231void QVFb::popupMenu()
232{
233 QMenu *pm = new QMenu( this );
234 createMenu( pm );
235 pm->exec(QCursor::pos());
236}
237
238void QVFb::init( int display_id, int pw, int ph, int d, int r, const QString& skin_name )
239{
240 delete view;
241 view = 0;
242 delete secondaryView;
243 secondaryView = 0;
244 delete scroller;
245 scroller = 0;
246 delete skin;
247 skin = 0;
248
249 skinscaleH = skinscaleV = 1.0;
250 QVFbView::Rotation rot = ((r == 90) ? QVFbView::Rot90 :
251 ((r == 180) ? QVFbView::Rot180 :
252 ((r == 270) ? QVFbView::Rot270 :
253 QVFbView::Rot0 )));
254 if ( !skin_name.isEmpty() ) {
255 const bool vis = isVisible();
256 DeviceSkinParameters parameters;
257 QString readError;
258 if (parameters.read(skin_name,DeviceSkinParameters::ReadAll, &readError)) {
259 skin = new DeviceSkin(parameters, this);
260 connect(skin, SIGNAL(popupMenu()), this, SLOT(popupMenu()));
261 const int sw = parameters.screenSize().width();
262 const int sh = parameters.screenSize().height();
263 const int sd = parameters.screenDepth;
264 if (!pw) pw = sw;
265 if (!ph) ph = sh;
266 if (d < 0) {
267 if (sd)
268 d = sd;
269 else
270 d = -d;
271 }
272 if (vis)
273 hide();
274 menuBar()->hide();
275 scroller = 0;
276#ifdef Q_WS_X11
277 if (displayType == X11)
278 view = new QVFbX11View( display_id, pw, ph, d, rot, skin );
279 else
280#endif
281 view = new QVFbView( display_id, pw, ph, d, rot, skin );
282 skin->setView( view );
283 view->setContentsMargins( 0, 0, 0, 0 );
284 view->setTouchscreenEmulation(!parameters.hasMouseHover);
285 connect(skin, SIGNAL(skinKeyPressEvent(int,QString,bool)), view, SLOT(skinKeyPressEvent(int,QString,bool)));
286 connect(skin, SIGNAL(skinKeyReleaseEvent(int,QString,bool)), view, SLOT(skinKeyReleaseEvent(int,QString,bool)));
287
288 copyButtonConfiguration(skin->prefix(), view->displayId());
289
290 setCentralWidget( skin );
291 adjustSize();
292 skinscaleH = (double)sw/pw;
293 skinscaleV = (double)sh/ph;
294 if ( skinscaleH != 1.0 || skinscaleH != 1.0 )
295 setZoom(skinscaleH);
296 view->show();
297
298 if (parameters.hasSecondaryScreen()) {
299 const QSize ssize = parameters.secondaryScreenSize();
300 // assumes same depth and rotation
301#ifdef Q_WS_X11
302 if (displayType == X11)
303 secondaryView = new QVFbX11View( display_id+1, ssize.width(), ssize.height(), d, rot, skin );
304 else
305#endif
306 secondaryView = new QVFbView( display_id+1, ssize.width(), ssize.height(), d, rot, skin );
307 skin->setSecondaryView(secondaryView);
308 secondaryView->show();
309 }
310
311 if ( vis ) show();
312 } else {
313 qWarning("%s", qPrintable(readError));
314 }
315 }
316
317 // If we failed to get a skin or we were not supplied
318 // with one then fallback to a framebuffer without
319 // a skin
320 if (!skin){
321 // Default values
322 if (!pw)
323 pw = 240;
324 if (!ph)
325 ph = 320;
326 if (!d)
327 d = 32;
328 else if (d < 0)
329 d = -d;
330
331 if (currentSkinIndex != -1) {
332 clearMask();
333 setParent( 0, 0 );
334 move( pos() );
335 show();
336 //unset fixed size:
337 setMinimumSize(0,0);
338 setMaximumSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX);
339 }
340 menuBar()->show();
341 scroller = new QScrollArea(this);
342 scroller->setFocusPolicy(Qt::NoFocus); // don't steal key events from the embedded app
343#ifdef Q_WS_X11
344 if (displayType == X11)
345 view = new QVFbX11View( display_id, pw, ph, d, rot, scroller );
346 else
347#endif
348 view = new QVFbView( display_id, pw, ph, d, rot, scroller );
349 scroller->setWidget(view);
350 view->setContentsMargins( 0, 0, 0, 0 );
351 setCentralWidget(scroller);
352 ph += 2; // avoid scrollbar
353 scroller->show();
354 // delete defaultbuttons.conf if it was left behind...
355 unlink(QFileInfo(QT_VFB_DATADIR(view->displayId()).append("/defaultbuttons.conf")).absoluteFilePath().toLatin1().constData());
356 if (secondaryView)
357 unlink(QFileInfo(QT_VFB_DATADIR(view->displayId() + 1).append("/defaultbuttons.conf")).absoluteFilePath().toLatin1().constData());
358 }
359 view->setRate(refreshRate);
360 if (secondaryView) {
361 secondaryView->setRate(refreshRate);
362 }
363 // Resize QVFb to the new size
364 QSize newSize = view->sizeHint();
365
366 // ... fudge factor
367 newSize += QSize(20, 35);
368
369 resize(newSize);
370
371 setWindowTitle(QString("Virtual framebuffer %1x%2 %3bpp Display :%4 Rotate %5")
372 .arg(view->displayWidth()).arg(view->displayHeight())
373 .arg(d).arg(display_id).arg(r));
374}
375
376void QVFb::enableCursor( bool e )
377{
378 if ( skin && skin->hasCursor() ) {
379 view->setCursor( Qt::BlankCursor );
380 if (secondaryView)
381 secondaryView->setCursor( Qt::BlankCursor );
382 } else {
383 view->setCursor( e ? Qt::ArrowCursor : Qt::BlankCursor );
384 if (secondaryView)
385 secondaryView->setCursor( e ? Qt::ArrowCursor : Qt::BlankCursor );
386 }
387 cursorAction->setChecked( e );
388}
389
390template <typename T>
391void QVFb::createMenu(T *menu)
392{
393 menu->addMenu( createFileMenu() );
394 menu->addMenu( createViewMenu() );
395 menu->addSeparator();
396 menu->addMenu( createHelpMenu() );
397}
398
399QMenu* QVFb::createFileMenu()
400{
401 QMenu *file = new QMenu( tr("&File"), this );
402 file->addAction( tr("&Configure..."), this, SLOT(configure()), QKeySequence::Preferences );
403 file->addSeparator();
404 file->addAction( tr("&Save image..."), this, SLOT(saveImage()), QKeySequence::Save );
405 file->addAction( tr("&Animation..."), this, SLOT(toggleAnimation()), 0 );
406 file->addSeparator();
407 file->addAction( tr("&Quit"), qApp, SLOT(quit()), QKeySequence::Quit );
408 return file;
409}
410
411QMenu* QVFb::createViewMenu()
412{
413 viewMenu = new QMenu( tr("&View"), this );
414 cursorAction = viewMenu->addAction( tr("Show &Cursor"), this,
415 SLOT(toggleCursor()) );
416 cursorAction->setCheckable(true);
417 if ( view )
418 enableCursor(true);
419 viewMenu->addAction( tr("&Refresh Rate..."), this, SLOT(changeRate()) );
420 viewMenu->addSeparator();
421 viewMenu->addAction( tr("&No rotation"), this, SLOT(setRot0()) );
422 viewMenu->addAction( tr("&90\260 rotation"), this, SLOT(setRot90()) );
423 viewMenu->addAction( tr("1&80\260 rotation"), this, SLOT(setRot180()) );
424 viewMenu->addAction( tr("2&70\260 rotation"), this, SLOT(setRot270()) );
425 viewMenu->addSeparator();
426 viewMenu->addAction( tr("Zoom scale &0.5"), this, SLOT(setZoomHalf()) );
427 viewMenu->addAction( tr("Zoom scale 0.7&5"), this, SLOT(setZoom075()) );
428 viewMenu->addAction( tr("Zoom scale &1"), this, SLOT(setZoom1()) );
429 viewMenu->addAction( tr("Zoom scale &2"), this, SLOT(setZoom2()) );
430 viewMenu->addAction( tr("Zoom scale &3"), this, SLOT(setZoom3()) );
431 viewMenu->addAction( tr("Zoom scale &4"), this, SLOT(setZoom4()) );
432 viewMenu->addSeparator();
433 viewMenu->addAction( tr("Zoom &scale..."), this, SLOT(setZoom()) );
434 return viewMenu;
435}
436
437
438QMenu* QVFb::createHelpMenu()
439{
440 QMenu *help = new QMenu( tr("&Help"), this );
441 help->addAction(tr("&About..."), this, SLOT(about()));
442 return help;
443}
444
445void QVFb::setZoom(double z)
446{
447 view->setZoom(z,z*skinscaleV/skinscaleH);
448 if (secondaryView)
449 secondaryView->setZoom(z,z*skinscaleV/skinscaleH);
450
451 if (skin) {
452 skin->setTransform(QMatrix().scale(z/skinscaleH,z/skinscaleV).rotate(90*view->displayRotation()));
453 if (secondaryView)
454 secondaryView->setFixedSize(
455 int(secondaryView->displayWidth()*z),
456 int(secondaryView->displayHeight()*z*skinscaleV/skinscaleH));
457 }
458}
459
460void QVFb::setRotation(QVFbView::Rotation r)
461{
462 view->setRotation(r);
463 if (secondaryView)
464 secondaryView->setRotation(r);
465 setZoom(view->zoomH());
466}
467
468void QVFb::setRot0()
469{
470 setRotation(QVFbView::Rot0);
471}
472
473void QVFb::setRot90()
474{
475 setRotation(QVFbView::Rot90);
476}
477
478void QVFb::setRot180()
479{
480 setRotation(QVFbView::Rot180);
481}
482
483void QVFb::setRot270()
484{
485 setRotation(QVFbView::Rot270);
486}
487
488void QVFb::setZoomHalf()
489{
490 setZoom(0.5);
491}
492
493void QVFb::setZoom075()
494{
495 setZoom(0.75);
496}
497
498void QVFb::setZoom1()
499{
500 setZoom(1);
501}
502
503void QVFb::setZoom()
504{
505 if ( !zoomer )
506 zoomer = new Zoomer(this);
507 zoomer->show();
508}
509
510void QVFb::setZoom2()
511{
512 setZoom(2);
513}
514
515void QVFb::setZoom3()
516{
517 setZoom(3);
518}
519
520void QVFb::setZoom4()
521{
522 setZoom(4);
523}
524
525void QVFb::saveImage()
526{
527 QImage img = view->image();
528 QString filename = QFileDialog::getSaveFileName(this, tr("Save Main Screen image"), tr("snapshot.png"), tr("Portable Network Graphics (*.png)"));
529 if (!filename.isEmpty()){
530 if(!img.save(filename,"PNG"))
531 QMessageBox::critical(this, tr("Save Main Screen Image"), tr("Save failed. Check that you have permission to write to the target directory."));
532 }
533 if (secondaryView) {
534 QImage img = view->image();
535 QString filename = QFileDialog::getSaveFileName(this, tr("Save Second Screen image"), tr("snapshot.png"), tr("Portable Network Graphics (*.png)"));
536 if (!filename.isEmpty()) {
537 if(!img.save(filename,"PNG"))
538 QMessageBox::critical(this, tr("Save Second Screen Image"), tr("Save failed. Check that you have permission to write to the target directory."));
539 }
540 }
541}
542
543void QVFb::toggleAnimation()
544{
545 static AnimationSaveWidget *animWidget = 0;
546 if ( !animWidget )
547 animWidget = new AnimationSaveWidget(view);
548 if ( animWidget->isVisible() )
549 animWidget->hide();