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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qvfb.h"
#include "qvfbview.h"
#include "qvfbhdr.h"
#ifdef Q_WS_X11
#include "qvfbx11view.h"
#endif
#include "qvfbratedlg.h"
#include "ui_config.h"
#include "qanimationwriter.h"
#include <deviceskin.h>
#include <QMenuBar>
#include <QMenu>
#include <QApplication>
#include <QMessageBox>
#include <QComboBox>
#include <QLabel>
#include <QFileDialog>
#include <QSlider>
#include <QSpinBox>
#include <QLayout>
#include <QRadioButton>
#include <QImage>
#include <QPixmap>
#include <QCheckBox>
#include <QCursor>
#include <QTime>
#include <QScrollArea>
#include <QProgressBar>
#include <QPushButton>
#include <QTextStream>
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
QT_BEGIN_NAMESPACE
// =====================================================================
static const char *red_on_led_xpm[] = {
"11 11 10 1",
" c None",
". c #FF0000",
"+ c #FF4C4C",
"@ c #FF7A7A",
"# c #D30000",
"$ c #FF9393",
"% c #BA0000",
"& c #FFFFFF",
"* c #7F0000",
"= c #000000",
" ",
" .++@@ ",
" .....+@ ",
" ##...$.+@ ",
" %#..$&$.+ ",
" *#...$..+ ",
" *%#...... ",
" =*%#..... ",
" =*%###. ",
" ===*. ",
" "};
static const char *red_off_led_xpm[] = {
"11 11 12 1",
" c None",
". c #CDB7B4",
"+ c #D2BFBD",
"@ c #DBCBCA",
"# c #E5D9D8",
"$ c #BC9E9B",
"% c #E2D6D5",
"& c #AD8986",
"* c #FFFFFF",
"= c #A8817D",
"- c #B2908D",
"; c #6F4D4A",
" ",
" .++@# ",
" .....@# ",
" $$...%.@# ",
" &$..%*%.@ ",
" =-...%..+ ",
" =&-...... ",
" ;==-..... ",
" ;=&-$$. ",
" ;==&$ ",
" "};
static bool copyButtonConfiguration(const QString &prefix, int displayId)
{
const QString destDir = QT_VFB_DATADIR(displayId).append("/");
const QFileInfo src(prefix + QLatin1String("defaultbuttons.conf"));
const QFileInfo dst(destDir + QLatin1String("defaultbuttons.conf"));
unlink(dst.absoluteFilePath().toLatin1().constData());
if (!src.exists())
return false;
const bool rc = QFile::copy(src.absoluteFilePath(), dst.absoluteFilePath());
if (!rc)
qWarning() << "Failed to copy the button configuration file " << src.absoluteFilePath() << " to " << dst.absoluteFilePath() << '.';
return rc;
}
// =====================================================================
class AnimationSaveWidget : public QWidget {
Q_OBJECT
public:
AnimationSaveWidget(QVFbAbstractView *v);
~AnimationSaveWidget();
bool detectPpmtoMpegCommand();
void timerEvent(QTimerEvent *te);
void convertToMpeg(QString filename);
void removeTemporaryFiles();
protected slots:
void toggleRecord();
void reset();
void save();
private:
QVFbAbstractView *view;
QProgressBar *progressBar;
QLabel *statusText;
bool haveMpeg, savingAsMpeg, recording;
QCheckBox *mpegSave;
QAnimationWriter *animation;
QPushButton *recBt, *resetBt, *saveBt;
QLabel *timeDpy, *recLED;
int timerId, progressTimerId;
QPixmap recOn, recOff;
QTime tm;
int elapsed, imageNum;
};
// =====================================================================
Zoomer::Zoomer(QVFb* target) :
qvfb(target)
{
QVBoxLayout *layout = new QVBoxLayout(this);
QSlider *sl = new QSlider(Qt::Horizontal);
sl->setMinimum(10);
sl->setMaximum(64);
sl->setPageStep(1);
sl->setValue(32);
layout->addWidget(sl);
connect(sl,SIGNAL(valueChanged(int)),this,SLOT(zoom(int)));
label = new QLabel();
layout->addWidget(label);
}
void Zoomer::zoom(int z)
{
double d = (double)z/32.0;
qvfb->setZoom(d);
label->setText(QString::number(d,'g',2));
}
// =====================================================================
QVFb::QVFb( int display_id, int w, int h, int d, int r, const QString &skin, DisplayType displayType, QWidget *parent, Qt::WindowFlags flags )
: QMainWindow( parent, flags )
{
this->displayType = displayType;
view = 0;
secondaryView = 0;
scroller = 0;
this->skin = 0;
currentSkinIndex = -1;
findSkins(skin);
zoomer = 0;
QPixmap pix(":/res/images/logo.png");
setWindowIcon( pix );
rateDlg = 0;
refreshRate = 30;
// Create the menu first to avoid scroll bars in the main window
createMenu( menuBar() );
init( display_id, w, h, d, r, skin );
enableCursor( true );
}
QVFb::~QVFb()
{
}
void QVFb::popupMenu()
{
QMenu *pm = new QMenu( this );
createMenu( pm );
pm->exec(QCursor::pos());
}
void QVFb::init( int display_id, int pw, int ph, int d, int r, const QString& skin_name )
{
delete view;
view = 0;
delete secondaryView;
secondaryView = 0;
delete scroller;
scroller = 0;
delete skin;
skin = 0;
skinscaleH = skinscaleV = 1.0;
QVFbView::Rotation rot = ((r == 90) ? QVFbView::Rot90 :
((r == 180) ? QVFbView::Rot180 :
((r == 270) ? QVFbView::Rot270 :
QVFbView::Rot0 )));
if ( !skin_name.isEmpty() ) {
const bool vis = isVisible();
DeviceSkinParameters parameters;
QString readError;
if (parameters.read(skin_name,DeviceSkinParameters::ReadAll, &readError)) {
skin = new DeviceSkin(parameters, this);
connect(skin, SIGNAL(popupMenu()), this, SLOT(popupMenu()));
const int sw = parameters.screenSize().width();
const int sh = parameters.screenSize().height();
const int sd = parameters.screenDepth;
if (!pw) pw = sw;
if (!ph) ph = sh;
if (d < 0) {
if (sd)
d = sd;
else
d = -d;
}
if (vis)
hide();
menuBar()->hide();
scroller = 0;
#ifdef Q_WS_X11
if (displayType == X11)
view = new QVFbX11View( display_id, pw, ph, d, rot, skin );
else
#endif
view = new QVFbView( display_id, pw, ph, d, rot, skin );
skin->setView( view );
view->setContentsMargins( 0, 0, 0, 0 );
view->setTouchscreenEmulation(!parameters.hasMouseHover);
connect(skin, SIGNAL(skinKeyPressEvent(int,QString,bool)), view, SLOT(skinKeyPressEvent(int,QString,bool)));
connect(skin, SIGNAL(skinKeyReleaseEvent(int,QString,bool)), view, SLOT(skinKeyReleaseEvent(int,QString,bool)));
copyButtonConfiguration(skin->prefix(), view->displayId());
setCentralWidget( skin );
adjustSize();
skinscaleH = (double)sw/pw;
skinscaleV = (double)sh/ph;
if ( skinscaleH != 1.0 || skinscaleH != 1.0 )
setZoom(skinscaleH);
view->show();
if (parameters.hasSecondaryScreen()) {
const QSize ssize = parameters.secondaryScreenSize();
// assumes same depth and rotation
#ifdef Q_WS_X11
if (displayType == X11)
secondaryView = new QVFbX11View( display_id+1, ssize.width(), ssize.height(), d, rot, skin );
else
#endif
secondaryView = new QVFbView( display_id+1, ssize.width(), ssize.height(), d, rot, skin );
skin->setSecondaryView(secondaryView);
secondaryView->show();
}
if ( vis ) show();
} else {
qWarning("%s", qPrintable(readError));
}
}
// If we failed to get a skin or we were not supplied
// with one then fallback to a framebuffer without
// a skin
if (!skin){
// Default values
if (!pw)
pw = 240;
if (!ph)
ph = 320;
if (!d)
d = 32;
else if (d < 0)
d = -d;
if (currentSkinIndex != -1) {
clearMask();
setParent( 0, 0 );
move( pos() );
show();
//unset fixed size:
setMinimumSize(0,0);
setMaximumSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX);
}
menuBar()->show();
scroller = new QScrollArea(this);
scroller->setFocusPolicy(Qt::NoFocus); // don't steal key events from the embedded app
#ifdef Q_WS_X11
if (displayType == X11)
view = new QVFbX11View( display_id, pw, ph, d, rot, scroller );
else
#endif
view = new QVFbView( display_id, pw, ph, d, rot, scroller );
scroller->setWidget(view);
view->setContentsMargins( 0, 0, 0, 0 );
setCentralWidget(scroller);
ph += 2; // avoid scrollbar
scroller->show();
// delete defaultbuttons.conf if it was left behind...
unlink(QFileInfo(QT_VFB_DATADIR(view->displayId()).append("/defaultbuttons.conf")).absoluteFilePath().toLatin1().constData());
if (secondaryView)
unlink(QFileInfo(QT_VFB_DATADIR(view->displayId() + 1).append("/defaultbuttons.conf")).absoluteFilePath().toLatin1().constData());
}
view->setRate(refreshRate);
if (secondaryView) {
secondaryView->setRate(refreshRate);
}
// Resize QVFb to the new size
QSize newSize = view->sizeHint();
// ... fudge factor
newSize += QSize(20, 35);
resize(newSize);
setWindowTitle(QString("Virtual framebuffer %1x%2 %3bpp Display :%4 Rotate %5")
.arg(view->displayWidth()).arg(view->displayHeight())
.arg(d).arg(display_id).arg(r));
}
void QVFb::enableCursor( bool e )
{
if ( skin && skin->hasCursor() ) {
view->setCursor( Qt::BlankCursor );
if (secondaryView)
secondaryView->setCursor( Qt::BlankCursor );
} else {
view->setCursor( e ? Qt::ArrowCursor : Qt::BlankCursor );
if (secondaryView)
secondaryView->setCursor( e ? Qt::ArrowCursor : Qt::BlankCursor );
}
cursorAction->setChecked( e );
}
template <typename T>
void QVFb::createMenu(T *menu)
{
menu->addMenu( createFileMenu() );
menu->addMenu( createViewMenu() );
menu->addSeparator();
menu->addMenu( createHelpMenu() );
}
QMenu* QVFb::createFileMenu()
{
QMenu *file = new QMenu( tr("&File"), this );
file->addAction( tr("&Configure..."), this, SLOT(configure()), QKeySequence::Preferences );
file->addSeparator();
file->addAction( tr("&Save image..."), this, SLOT(saveImage()), QKeySequence::Save );
file->addAction( tr("&Animation..."), this, SLOT(toggleAnimation()), 0 );
file->addSeparator();
file->addAction( tr("&Quit"), qApp, SLOT(quit()), QKeySequence::Quit );
return file;
}
QMenu* QVFb::createViewMenu()
{
viewMenu = new QMenu( tr("&View"), this );
cursorAction = viewMenu->addAction( tr("Show &Cursor"), this,
SLOT(toggleCursor()) );
cursorAction->setCheckable(true);
if ( view )
enableCursor(true);
viewMenu->addAction( tr("&Refresh Rate..."), this, SLOT(changeRate()) );
viewMenu->addSeparator();
viewMenu->addAction( tr("&No rotation"), this, SLOT(setRot0()) );
viewMenu->addAction( tr("&90\260 rotation"), this, SLOT(setRot90()) );
viewMenu->addAction( tr("1&80\260 rotation"), this, SLOT(setRot180()) );
viewMenu->addAction( tr("2&70\260 rotation"), this, SLOT(setRot270()) );
viewMenu->addSeparator();
viewMenu->addAction( tr("Zoom scale &0.5"), this, SLOT(setZoomHalf()) );
viewMenu->addAction( tr("Zoom scale 0.7&5"), this, SLOT(setZoom075()) );
viewMenu->addAction( tr("Zoom scale &1"), this, SLOT(setZoom1()) );
viewMenu->addAction( tr("Zoom scale &2"), this, SLOT(setZoom2()) );
viewMenu->addAction( tr("Zoom scale &3"), this, SLOT(setZoom3()) );
viewMenu->addAction( tr("Zoom scale &4"), this, SLOT(setZoom4()) );
viewMenu->addSeparator();
viewMenu->addAction( tr("Zoom &scale..."), this, SLOT(setZoom()) );
return viewMenu;
}
QMenu* QVFb::createHelpMenu()
{
QMenu *help = new QMenu( tr("&Help"), this );
help->addAction(tr("&About..."), this, SLOT(about()));
return help;
}
void QVFb::setZoom(double z)
{
view->setZoom(z,z*skinscaleV/skinscaleH);
if (secondaryView)
secondaryView->setZoom(z,z*skinscaleV/skinscaleH);
if (skin) {
skin->setTransform(QMatrix().scale(z/skinscaleH,z/skinscaleV).rotate(90*view->displayRotation()));
if (secondaryView)
secondaryView->setFixedSize(
int(secondaryView->displayWidth()*z),
int(secondaryView->displayHeight()*z*skinscaleV/skinscaleH));
}
}
void QVFb::setRotation(QVFbView::Rotation r)
{
view->setRotation(r);
if (secondaryView)
secondaryView->setRotation(r);
setZoom(view->zoomH());
}
void QVFb::setRot0()
{
setRotation(QVFbView::Rot0);
}
void QVFb::setRot90()
{
setRotation(QVFbView::Rot90);
}
void QVFb::setRot180()
{
setRotation(QVFbView::Rot180);
}
void QVFb::setRot270()
{
setRotation(QVFbView::Rot270);
}
void QVFb::setZoomHalf()
{
setZoom(0.5);
}
void QVFb::setZoom075()
{
setZoom(0.75);
}
void QVFb::setZoom1()
{
setZoom(1);
}
void QVFb::setZoom()
{
if ( !zoomer )
zoomer = new Zoomer(this);
zoomer->show();
}
void QVFb::setZoom2()
{
setZoom(2);
}
void QVFb::setZoom3()
{
setZoom(3);
}
void QVFb::setZoom4()
{
setZoom(4);
}
void QVFb::saveImage()
{
QImage img = view->image();
QString filename = QFileDialog::getSaveFileName(this, tr("Save Main Screen image"), tr("snapshot.png"), tr("Portable Network Graphics (*.png)"));
if (!filename.isEmpty()){
if(!img.save(filename,"PNG"))
QMessageBox::critical(this, tr("Save Main Screen Image"), tr("Save failed. Check that you have permission to write to the target directory."));
}
if (secondaryView) {
QImage img = view->image();
QString filename = QFileDialog::getSaveFileName(this, tr("Save Second Screen image"), tr("snapshot.png"), tr("Portable Network Graphics (*.png)"));
if (!filename.isEmpty()) {
if(!img.save(filename,"PNG"))
QMessageBox::critical(this, tr("Save Second Screen Image"), tr("Save failed. Check that you have permission to write to the target directory."));
}
}
}
void QVFb::toggleAnimation()
{
static AnimationSaveWidget *animWidget = 0;
if ( !animWidget )
animWidget = new AnimationSaveWidget(view);
if ( animWidget->isVisible() )
animWidget->hide();
else
animWidget->show();
}
void QVFb::toggleCursor()
{
enableCursor(cursorAction->isChecked());
}
void QVFb::changeRate()
{
if ( !rateDlg ) {
rateDlg = new QVFbRateDialog( refreshRate, this );
connect( rateDlg, SIGNAL(updateRate(int)), this, SLOT(setRate(int)) );
}
rateDlg->show();
}
void QVFb::setRate(int i)
{
refreshRate = i;
view->setRate(i);
if (secondaryView)
secondaryView->setRate(i);
}
void QVFb::about()
{
QMessageBox::about(this, tr("About QVFB"), tr(
"<h2>The Qt for Embedded Linux Virtual X11 Framebuffer</h2>"
"<p>This application runs under Qt for X11, emulating a simple framebuffer, "
"which the Qt for Embedded Linux server and clients can attach to just as if "
"it was a hardware Linux framebuffer. "
"<p>With the aid of this development tool, you can develop Qt for Embedded "
"Linux applications under X11 without having to switch to a virtual console. "
"This means you can comfortably use your other development tools such "
"as GUI profilers and debuggers."
));
}
void QVFb::findSkins(const QString ¤tSkin)
{
skinnames.clear();
skinfiles.clear();
QDir dir(":/skins/","*.skin");
const QFileInfoList l = dir.entryInfoList();
int i = 1; // "None" is already in list at index 0
for (QFileInfoList::const_iterator it = l.begin(); it != l.end(); ++it) {
skinnames.append((*it).baseName()); // should perhaps be in file
skinfiles.append((*it).filePath());
if (((*it).baseName() + ".skin") == currentSkin)
currentSkinIndex = i;
i++;
}
}
class Config : public QDialog, public Ui::Config
{
public:
Config(QWidget *parent)
: QDialog(parent)
{
setupUi(this);
setModal(true);
connect(buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
}
};
void QVFb::configure()
{
config = new Config(this);
int w = view->displayWidth();
int h = view->displayHeight();
// Need to block signals, because we connect to animateClick(),
// since QCheckBox doesn't have setChecked(bool) in 2.x.
chooseSize(QSize(w,h));
config->skin->insertItems(config->skin->count(), skinnames);
if (currentSkinIndex > 0)
config->skin->setCurrentIndex(currentSkinIndex);
config->skin->addItem(tr("Browse..."));
config->touchScreen->setChecked(view->touchScreenEmulation());
config->lcdScreen->setChecked(view->lcdScreenEmulation());
chooseDepth(view->displayDepth(), view->displayFormat());
config->rgbSwapped->setChecked(view->rgbSwapped());
connect(config->skin, SIGNAL(activated(int)), this, SLOT(skinConfigChosen(int)));
if ( view->gammaRed() == view->gammaGreen() && view->gammaGreen() == view->gammaBlue() ) {
config->gammaslider->setValue(int(view->gammaRed()*400));
config->rslider->setValue(100);
config->gslider->setValue(100);
config->bslider->setValue(100);
} else {
config->gammaslider->setValue(100);
config->rslider->setValue(int(view->gammaRed()*400));
config->gslider->setValue(int(view->gammaGreen()*400));
config->bslider->setValue(int(view->gammaBlue()*400));
}
connect(config->gammaslider, SIGNAL(valueChanged(int)), this, SLOT(setGamma400(int)));
connect(config->rslider, SIGNAL(valueChanged(int)), this, SLOT(setR400(int)));
connect(config->gslider, SIGNAL(valueChanged(int)), this, SLOT(setG400(int)));
connect(config->bslider, SIGNAL(valueChanged(int)), this, SLOT(setB400(int)));
updateGammaLabels();
double ogr=view->gammaRed(), ogg=view->gammaGreen(), ogb=view->gammaBlue();
qApp->setQuitOnLastWindowClosed(false);
hide();
if ( config->exec() ) {
int id = view->displayId(); // not settable yet
if ( config->size_176_220->isChecked() ) {
w=176; h=220;
} else if ( config->size_240_320->isChecked() ) {
w=240; h=320;
} else if ( config->size_320_240->isChecked() ) {
w=320; h=240;
} else if ( config->size_640_480->isChecked() ) {
w=640; h=480;
} else if ( config->size_800_480->isChecked() ) {
w=800; h=480;
} else if ( config->size_800_600->isChecked() ) {
w=800; h=600;
} else if ( config->size_1024_768->isChecked() ) {
w=1024; h=768;
} else {
w=config->size_width->value();
h=config->size_height->value();
}
int d;
if ( config->depth_1->isChecked() )
d=1;
else if ( config->depth_2gray->isChecked() )
d=2;
else if ( config->depth_4gray->isChecked() )
d=4;
else if ( config->depth_8->isChecked() )
d=8;
else if ( config->depth_12->isChecked() )
d=12;
else if ( config->depth_15->isChecked() )
d = 15;
else if ( config->depth_16->isChecked() )
d=16;
else if ( config->depth_18->isChecked() )
d=18;
else if ( config->depth_24->isChecked() )
d=24;
else
d=32;
QVFbView::PixelFormat displayFormat = config->depth_32_argb->isChecked()
? QVFbView::ARGBFormat : QVFbView::DefaultFormat;
int skinIndex = config->skin->currentIndex();
if ( w != view->displayWidth() || h != view->displayHeight()
|| d != view->displayDepth() || skinIndex != currentSkinIndex ) {
QVFbView::Rotation rot = view->displayRotation();
int r = ((rot == QVFbView::Rot90) ? 90 :
((rot == QVFbView::Rot180) ? 180 :
((rot == QVFbView::Rot270) ? 270 : 0 )));
currentSkinIndex = skinIndex;
init( id, w, h, d, r, skinIndex > 0 ? skinfiles[skinIndex-1] : QString::null );
}
view->setViewFormat(displayFormat);
view->setTouchscreenEmulation( config->touchScreen->isChecked() );
if (view->rgbSwapped() != config->rgbSwapped->isChecked()) {
//### the windowTitle logic is inside init(), and init isn't always invoked
QString caption = windowTitle();
if (!config->rgbSwapped->isChecked())
caption.replace(QLatin1String(" BGR"), QString());
else
caption.append(QLatin1String(" BGR"));
setWindowTitle(caption);
view->setRgbSwapped(config->rgbSwapped->isChecked());
}
bool lcdEmulation = config->lcdScreen->isChecked();
view->setLcdScreenEmulation( lcdEmulation );
if ( lcdEmulation )
setZoom3();
} else {
view->setGamma(ogr, ogg, ogb);
}
show();
qApp->setQuitOnLastWindowClosed(true);
delete config;
config=0;
}
void QVFb::chooseSize(const QSize& sz)
{
config->size_width->blockSignals(true);
config->size_height->blockSignals(true);
config->size_width->setValue(sz.width());
config->size_height->setValue(sz.height());
config->size_width->blockSignals(false);
config->size_height->blockSignals(false);
config->size_custom->setChecked(true); // unless changed by settings below
config->size_176_220->setChecked(sz == QSize(176,220));
config->size_240_320->setChecked(sz == QSize(240,320));
config->size_320_240->setChecked(sz == QSize(320,240));
config->size_640_480->setChecked(sz == QSize(640,480));
config->size_800_480->setChecked(sz == QSize(800,480));
config->size_800_600->setChecked(sz == QSize(800,600));
config->size_1024_768->setChecked(sz == QSize(1024,768));
}
void QVFb::chooseDepth(int depth, QVFbView::PixelFormat displayFormat)
{
config->depth_1->setChecked(depth==1);
config->depth_2gray->setChecked(depth==2);
config->depth_4gray->setChecked(depth==4);
config->depth_8->setChecked(depth==8);
config->depth_12->setChecked(depth==12);
config->depth_15->setChecked(depth==15);
config->depth_16->setChecked(depth==16);
config->depth_18->setChecked(depth==18);
config->depth_24->setChecked(depth==24);
config->depth_32->setChecked(depth==32 && displayFormat != QVFbView::ARGBFormat);
config->depth_32_argb->setChecked(depth==32 && displayFormat == QVFbView::ARGBFormat);
}
void QVFb::skinConfigChosen(int i)
{
if (i == config->skin->count() - 1) { // Browse... ?
QFileDialog dlg(this);
dlg.setFileMode(QFileDialog::DirectoryOnly);
dlg.setWindowTitle(tr("Load Custom Skin..."));
dlg.setFilter(tr("All QVFB Skins (*.skin)"));
dlg.setDirectory(QDir::current());
if (dlg.exec() && dlg.selectedFiles().count() == 1) {
skinfiles.append(dlg.selectedFiles().first());
i = skinfiles.count();
config->skin->insertItem(i, QFileInfo(skinfiles.last()).baseName());
config->skin->setCurrentIndex(i);
} else {
i = 0;
}
}
if ( i ) {
DeviceSkinParameters parameters;
QString readError;
if (parameters.read(skinfiles[i-1], DeviceSkinParameters::ReadSizeOnly, &readError)) {
chooseSize(parameters.screenSize());
if (parameters.screenDepth)
chooseDepth(parameters.screenDepth,QVFbView::ARGBFormat);
config->touchScreen->setChecked(!parameters.hasMouseHover);
} else {
qWarning("%s", qPrintable(readError));
}
}
}
void QVFb::setGamma400(int n)
{
double g = n/100.0;
view->setGamma(config->rslider->value()/100.0*g,
config->gslider->value()/100.0*g,
config->bslider->value()/100.0*g);
updateGammaLabels();
}
void QVFb::setR400(int n)
{
double g = n/100.0;
view->setGamma(config->rslider->value()/100.0*g,
view->gammaGreen(),
view->gammaBlue());
updateGammaLabels();
}
void QVFb::setG400(int n)
{
double g = n/100.0;
view->setGamma(view->gammaRed(),
config->gslider->value()/100.0*g,
view->gammaBlue());
updateGammaLabels();
}
void QVFb::setB400(int n)
{
double g = n/100.0;
view->setGamma(view->gammaRed(),
view->gammaGreen(),
config->bslider->value()/100.0*g);
updateGammaLabels();
}
void QVFb::updateGammaLabels()
{
config->rlabel->setText(QString::number(view->gammaRed(),'g',2));
config->glabel->setText(QString::number(view->gammaGreen(),'g',2));
config->blabel->setText(QString::number(view->gammaBlue(),'g',2));
}
QSize QVFb::sizeHint() const
{
return QSize(int(view->displayWidth()*view->zoomH()),
int(menuBar()->height()+view->displayHeight()*view->zoomV()));
}
// =====================================================================
AnimationSaveWidget::AnimationSaveWidget(QVFbAbstractView *v) :
QWidget((QWidget*)0,0),
view(v), recording(false), animation(0),
timerId(-1), progressTimerId(-1),
recOn(red_on_led_xpm), recOff(red_off_led_xpm),
imageNum(0)
{
// Create the animation record UI dialog
QVBoxLayout *vlayout = new QVBoxLayout( this );
QWidget *hbox = new QWidget( this );
vlayout->addWidget(hbox);
QHBoxLayout *hlayout = new QHBoxLayout(hbox);
recBt = new QPushButton( tr("Record"), hbox );
hlayout->addWidget(recBt);
resetBt = new QPushButton( tr("Reset"), hbox );
hlayout->addWidget(resetBt);
saveBt = new QPushButton( tr("Save"), hbox );
hlayout->addWidget(saveBt);
recBt->setFixedWidth( 100 );
resetBt->setFixedWidth( 100 );
saveBt->setFixedWidth( 100 );
timeDpy = new QLabel( "00:00", hbox );
hlayout->addWidget(timeDpy);
recLED = new QLabel( hbox );
hlayout->addWidget(recLED);
recLED->setPixmap( recOff );
timeDpy->setMargin( 5 );
connect( recBt, SIGNAL(clicked()), this, SLOT(toggleRecord()) );
connect( resetBt, SIGNAL(clicked()), this, SLOT(reset()) );
connect( saveBt, SIGNAL(clicked()), this, SLOT(save()) );
elapsed = 0;
vlayout->setMargin( 5 );
vlayout->setSpacing( 5 );
haveMpeg = detectPpmtoMpegCommand();
mpegSave = new QCheckBox( tr("Save in MPEG format (requires netpbm package installed)"), this );
vlayout->addWidget(mpegSave);
mpegSave->setChecked( haveMpeg );
mpegSave->setEnabled( haveMpeg );
savingAsMpeg = haveMpeg;
QWidget *hbox2 = new QWidget( this );
vlayout->addWidget(hbox2);
QHBoxLayout *hlayout2 = new QHBoxLayout( hbox2 );
statusText = new QLabel( tr("Click record to begin recording."), hbox2 );
hlayout2->addWidget(statusText);
progressBar = new QProgressBar( hbox2 );
progressBar->setValue( 0 );
hlayout2->addWidget(progressBar);
progressBar->hide();
}
AnimationSaveWidget::~AnimationSaveWidget()
{
// clean up
removeTemporaryFiles();
delete animation;
}
// returns true if we have ppmtompeg command, else returns false
bool AnimationSaveWidget::detectPpmtoMpegCommand()
{
// search the PATH for the ppmtompeg command to test we can record to mpeg
QStringList paths = QString(::getenv("PATH")).split(":");
for ( int i = 0; i < paths.count(); i++ )
if ( QFile::exists( paths[i] + "/" + "ppmtompeg" ) )
return true;
return false;
}
void AnimationSaveWidget::timerEvent( QTimerEvent *te )
{
QString str;
// Recording timer
if ( te->timerId() == timerId ) {
// Add a frame to the animation
if ( savingAsMpeg && view )
view->image().save( str.sprintf("/tmp/qvfb_tmp_image_%04d.ppm", imageNum), "PPM");
else if ( animation && view )
animation->appendFrame(view->image());//QPoint(0,0));
imageNum++;
// Update the display of number of seconds that have been recorded.
int tmMsec = tm.elapsed();
timeDpy->setText( str.sprintf("%02d:%02d", tmMsec/60000, (tmMsec%60000)/1000) );
QObject::timerEvent( te );
// Make the recording LED blink
static int tick = 0;
static bool on = false;
if ( tick > 10 ) {
tick = 0;
if ( on )
recLED->setPixmap( recOff );
else
recLED->setPixmap( recOn );
on = !on;
}
tick++;
}
// Saving progress timer
if ( te->timerId() == progressTimerId ) {
// Parse output log file to work out the encoding progress.
QFile f("/tmp/qvfb_tmp_output.log");
f.open(QIODevice::ReadOnly);
int largestNum = 0;
bool done = false;
char buffer[1024];
while ( !f.atEnd() ) {
// example of the output log entries
// During each frame:
// "FRAME 764 (B): I BLOCKS: 0......
// When complete:
// "======FRAMES READ: 766"
f.readLine(buffer, 1024);
str = QString(buffer);
if ( str.left(6) == "FRAME " ) {
int num = str.mid(6, str.indexOf(QChar(' '), 6) - 6).toInt();
if ( num > largestNum )
largestNum = num;
} else if ( str.left(18) == "======FRAMES READ:" ) {
done = true;
}
}
f.close();
// Update the progress bar with the frame we are up to
progressBar->setValue( largestNum );
// Finished saving
if ( done ) {
progressBar->hide();
statusText->setText( tr("Finished saving."));
removeTemporaryFiles();
killTimer( progressTimerId );
progressTimerId = -1;
reset();
}
}
}
// Takes the saved ppm files and converts them to a mpeg file named filename
void AnimationSaveWidget::convertToMpeg(QString filename)
{
recLED->setPixmap( recOff );
killTimer( timerId );
progressBar->show();
progressBar->setRange( 0, imageNum );
progressBar->setValue( 0 );
// Build parameter file required by ppmtompeg
QFile file("/tmp/qvfb_tmp_ppmtompeg.params");
if ( file.open( QIODevice::WriteOnly ) ) {
QTextStream t( &file );
t << "PATTERN IBBPBBPBBPBBPBB\n";
t << "OUTPUT " << filename << "\n";
t << "INPUT_DIR /tmp\n";
t << "INPUT\n";
QString str;
str = str.sprintf("%04d", imageNum - 1);
t << "qvfb_tmp_image_*.ppm [0000-" << str << "]\n";
t << "END_INPUT\n";
t << "BASE_FILE_FORMAT PPM\n";
t << "INPUT_CONVERT *\n";
t << "GOP_SIZE 15\n";
t << "SLICES_PER_FRAME 1\n";
t << "PIXEL HALF\n";
t << "RANGE 5\n";
t << "PSEARCH_ALG LOGARITHMIC\n";
t << "BSEARCH_ALG SIMPLE\n";
t << "IQSCALE 1\n";
t << "PQSCALE 1\n";
t << "BQSCALE 1\n";
t << "REFERENCE_FRAME DECODED\n";
t << "ASPECT_RATIO 1\n";
t << "FRAME_RATE 24\n";
t << "BIT_RATE 64000\n"; // Quality
t << "BUFFER_SIZE 2048\n";
}
file.close();
// ### can't use QProcess, not in Qt 2.3
// ### but it's certainly in Qt 4! use it?
// Execute the ppmtompeg command as a separate process to do the encoding
pid_t pid = ::fork();
if ( !pid ) {
// Child process
// redirect stdout to log file
freopen("/tmp/qvfb_tmp_output.log", "w", stdout);
// ppmtompeg tool is from the netpbm package
::execlp("ppmtompeg", "ppmtompeg", "/tmp/qvfb_tmp_ppmtompeg.params", (void *)0);
exit(0);
}
// Update the saving progress bar every 200ms
progressTimerId = startTimer( 200 );
}
// Cleanup temporary files created during creating a mpeg file
void AnimationSaveWidget::removeTemporaryFiles()
{
QString str;
for ( int i = 0; i < imageNum; i++ )
QFile::remove( str.sprintf("/tmp/qvfb_tmp_image_%04d.ppm", i) );
QFile::remove("/tmp/qvfb_tmp_ppmtompeg.params");
QFile::remove("/tmp/qvfb_tmp_output.log");
imageNum = 0;
}
// toggles between recording and paused (usually when record button clicked)
void AnimationSaveWidget::toggleRecord()
{
if ( recording ) {
recLED->setPixmap( recOff );
recBt->setText( tr("Record") );
statusText->setText( tr("Paused. Click record to resume, or save if done."));
killTimer( timerId );
timerId = -1;
elapsed = tm.elapsed();
} else {
recLED->setPixmap( recOn );
recBt->setText( tr("Pause") );
statusText->setText( tr("Recording..."));
tm.start();
if ( elapsed == 0 ) {
savingAsMpeg = mpegSave->isChecked();
if ( !savingAsMpeg ) {
delete animation;
animation = new QAnimationWriter("/tmp/qvfb_tmp_animation.mng","MNG");
animation->setFrameRate(24);
if ( view )
animation->appendFrame(view->image());
}
}
tm = tm.addMSecs(-elapsed);
elapsed = 0;
timerId = startTimer(1000 / 24);
}
recording = !recording;
}
// Reset everything to initial state of not recording
void AnimationSaveWidget::reset()
{
if ( recording ) {
toggleRecord();
statusText->setText( tr("Click record to begin recording."));
removeTemporaryFiles();
}
progressBar->setValue( 0 );
timeDpy->setText( "00:00" );
elapsed = 0;
imageNum = 0;
delete animation;
animation = 0;
}
// Prompt for filename to save to and put animation in that file
void AnimationSaveWidget::save()
{
if ( recording )
toggleRecord(); // pauses
statusText->setText( tr("Saving... "));
QString filename;
if ( savingAsMpeg ) {
filename = QFileDialog::getSaveFileName(this, tr("Save animation..."), "", "*.mpg");
if ( !filename.isNull() )
convertToMpeg(filename);
} else {
filename = QFileDialog::getSaveFileName(this, tr("Save animation..."), "", "*.mng");
if (filename.isNull()) {
statusText->setText(tr("Save canceled."));
} else {
QFile::remove(filename);
bool success = QFile::rename(QLatin1String("/tmp/qvfb_tmp_animation.mng"),
filename);
if (success) {
statusText->setText(tr("Finished saving."));
reset();
} else {
statusText->setText(tr("Save failed!"));
}
}
}
}
QT_END_NAMESPACE
#include "qvfb.moc"
|