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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/showimg/showimg.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Show Image</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
<a href="index.html">
<font color="#004faf">Home</font></a>
| <a href="classes.html">
<font color="#004faf">All Classes</font></a>
| <a href="mainclasses.html">
<font color="#004faf">Main Classes</font></a>
| <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
| <a href="groups.html">
<font color="#004faf">Grouped Classes</font></a>
| <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Show Image</h1>
<p>
This example reads and displays an image in any supported image
format (GIF, BMP, PPM, XMP, etc.).
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id: qt/showimg.h 3.3.8 edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#ifndef SHOWIMG_H
#define SHOWIMG_H
#include <<a href="qwidget-h.html">qwidget.h</a>>
#include <<a href="qimage-h.html">qimage.h</a>>
class QLabel;
class QMenuBar;
class QPopupMenu;
class ImageViewer : public <a href="qwidget.html">QWidget</a>
{
<a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
public:
ImageViewer( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0, int wFlags=0 );
~ImageViewer();
bool loadImage( const <a href="qstring.html">QString</a>& );
protected:
void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );
void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
private:
void scale();
int conversion_flags;
bool smooth() const;
bool useColorContext() const;
int alloc_context;
bool convertEvent( <a href="qmouseevent.html">QMouseEvent</a>* e, int& x, int& y );
<a href="qstring.html">QString</a> filename;
<a href="qimage.html">QImage</a> image; // the loaded image
<a href="qpixmap.html">QPixmap</a> pm; // the converted pixmap
<a href="qpixmap.html">QPixmap</a> pmScaled; // the scaled pixmap
<a href="qmenubar.html">QMenuBar</a> *menubar;
<a href="qpopupmenu.html">QPopupMenu</a> *file;
<a href="qpopupmenu.html">QPopupMenu</a> *saveimage;
<a href="qpopupmenu.html">QPopupMenu</a> *savepixmap;
<a href="qpopupmenu.html">QPopupMenu</a> *edit;
<a href="qpopupmenu.html">QPopupMenu</a> *options;
<a href="qwidget.html">QWidget</a> *helpmsg;
<a href="qlabel.html">QLabel</a> *status;
int si, sp, ac, co, mo, fd, bd, // Menu item ids
td, ta, ba, fa, au, ad, dd,
ss, cc, t1, t8, t32;
void updateStatus();
void setMenuItemFlags();
bool reconvertImage();
int pickx, picky;
int clickx, clicky;
bool may_be_other;
static ImageViewer* other;
void setImage(const <a href="qimage.html">QImage</a>& newimage);
private slots:
void to1Bit();
void to8Bit();
void to32Bit();
void toBitDepth(int);
void copy();
void paste();
void hFlip();
void vFlip();
void rot180();
void editText();
void newWindow();
void openFile();
void saveImage(int);
void savePixmap(int);
void giveHelp();
void doOption(int);
void copyFrom(ImageViewer*);
};
#endif // SHOWIMG_H
</pre>
<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/showimg.cpp 3.3.8 edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "showimg.h"
#include "imagetexteditor.h"
#include <<a href="qmenubar-h.html">qmenubar.h</a>>
#include <<a href="qfiledialog-h.html">qfiledialog.h</a>>
#include <<a href="qmessagebox-h.html">qmessagebox.h</a>>
#include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>
#include <<a href="qlabel-h.html">qlabel.h</a>>
#include <<a href="qpainter-h.html">qpainter.h</a>>
#include <<a href="qapplication-h.html">qapplication.h</a>>
#include <<a href="qclipboard-h.html">qclipboard.h</a>>
/*
In the constructor, we just pass the standard parameters on to
QWidget.
The menu uses a single slot to simplify the process of adding
more items to the options menu.
*/
<a name="f405"></a>ImageViewer::ImageViewer( <a href="qwidget.html">QWidget</a> *parent, const char *name, int wFlags )
: <a href="qwidget.html">QWidget</a>( parent, name, wFlags ),
conversion_flags( PreferDither ),
helpmsg( 0 )
{
pickx = -1;
picky = -1;
clickx = -1;
clicky = -1;
alloc_context = 0;
menubar = new <a href="qmenubar.html">QMenuBar</a>(this);
<a name="x1326"></a> menubar-><a href="qmenubar.html#setSeparator">setSeparator</a>( QMenuBar::InWindowsStyle );
<a name="x1323"></a> <a href="qstrlist.html">QStrList</a> fmt = QImage::<a href="qimage.html#outputFormats">outputFormats</a>();
saveimage = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
savepixmap = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
<a name="x1344"></a><a name="x1343"></a> for (const char* f = fmt.<a href="qptrlist.html#first">first</a>(); f; f = fmt.<a href="qptrlist.html#next">next</a>()) {
<a name="x1327"></a> saveimage-><a href="qmenudata.html#insertItem">insertItem</a>( f );
savepixmap-><a href="qmenudata.html#insertItem">insertItem</a>( f );
}
<a name="x1341"></a> <a href="qobject.html#connect">connect</a>( saveimage, SIGNAL(<a href="qpopupmenu.html#activated">activated</a>(int)), this, SLOT(saveImage(int)) );
<a href="qobject.html#connect">connect</a>( savepixmap, SIGNAL(<a href="qpopupmenu.html#activated">activated</a>(int)), this, SLOT(savePixmap(int)) );
file = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
menubar-><a href="qmenudata.html#insertItem">insertItem</a>( "&File", file );
file-><a href="qmenudata.html#insertItem">insertItem</a>( "&New window", this, SLOT(newWindow()), CTRL+Key_N );
file-><a href="qmenudata.html#insertItem">insertItem</a>( "&Open...", this, SLOT(openFile()), CTRL+Key_O );
si = file-><a href="qmenudata.html#insertItem">insertItem</a>( "Save image", saveimage );
sp = file-><a href="qmenudata.html#insertItem">insertItem</a>( "Save pixmap", savepixmap );
<a name="x1328"></a> file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
file-><a href="qmenudata.html#insertItem">insertItem</a>( "E&xit", qApp, SLOT(<a href="qapplication.html#quit">quit</a>()), CTRL+Key_Q );
edit = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
menubar-><a href="qmenudata.html#insertItem">insertItem</a>( "&Edit", edit );
edit-><a href="qmenudata.html#insertItem">insertItem</a>("&Copy", this, SLOT(copy()), CTRL+Key_C);
edit-><a href="qmenudata.html#insertItem">insertItem</a>("&Paste", this, SLOT(paste()), CTRL+Key_V);
edit-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
edit-><a href="qmenudata.html#insertItem">insertItem</a>("&Horizontal flip", this, SLOT(hFlip()), ALT+Key_H);
edit-><a href="qmenudata.html#insertItem">insertItem</a>("&Vertical flip", this, SLOT(vFlip()), ALT+Key_V);
edit-><a href="qmenudata.html#insertItem">insertItem</a>("&Rotate 180", this, SLOT(rot180()), ALT+Key_R);
edit-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
edit-><a href="qmenudata.html#insertItem">insertItem</a>("&Text...", this, SLOT(editText()));
edit-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
t1 = edit-><a href="qmenudata.html#insertItem">insertItem</a>( "Convert to &1 bit", this, SLOT(to1Bit()) );
t8 = edit-><a href="qmenudata.html#insertItem">insertItem</a>( "Convert to &8 bit", this, SLOT(to8Bit()) );
t32 = edit-><a href="qmenudata.html#insertItem">insertItem</a>( "Convert to &32 bit", this, SLOT(to32Bit()) );
options = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
menubar-><a href="qmenudata.html#insertItem">insertItem</a>( "&Options", options );
ac = options-><a href="qmenudata.html#insertItem">insertItem</a>( "AutoColor" );
co = options-><a href="qmenudata.html#insertItem">insertItem</a>( "ColorOnly" );
mo = options-><a href="qmenudata.html#insertItem">insertItem</a>( "MonoOnly" );
options-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
fd = options-><a href="qmenudata.html#insertItem">insertItem</a>( "DiffuseDither" );
bd = options-><a href="qmenudata.html#insertItem">insertItem</a>( "OrderedDither" );
td = options-><a href="qmenudata.html#insertItem">insertItem</a>( "ThresholdDither" );
options-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
ta = options-><a href="qmenudata.html#insertItem">insertItem</a>( "ThresholdAlphaDither" );
ba = options-><a href="qmenudata.html#insertItem">insertItem</a>( "OrderedAlphaDither" );
fa = options-><a href="qmenudata.html#insertItem">insertItem</a>( "DiffuseAlphaDither" );
options-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
ad = options-><a href="qmenudata.html#insertItem">insertItem</a>( "PreferDither" );
dd = options-><a href="qmenudata.html#insertItem">insertItem</a>( "AvoidDither" );
options-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
ss = options-><a href="qmenudata.html#insertItem">insertItem</a>( "Smooth scaling" );
cc = options-><a href="qmenudata.html#insertItem">insertItem</a>( "Use color context" );
<a name="x1309"></a> if ( QApplication::<a href="qapplication.html#colorSpec">colorSpec</a>() == QApplication::ManyColor )
<a name="x1331"></a> options-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( cc, FALSE );
<a name="x1342"></a> options-><a href="qpopupmenu.html#setCheckable">setCheckable</a>( TRUE );
setMenuItemFlags();
menubar-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
<a href="qpopupmenu.html">QPopupMenu</a>* help = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
menubar-><a href="qmenudata.html#insertItem">insertItem</a>( "&Help", help );
help-><a href="qmenudata.html#insertItem">insertItem</a>( "Help!", this, SLOT(giveHelp()), CTRL+Key_H );
<a href="qobject.html#connect">connect</a>( options, SIGNAL(<a href="qpopupmenu.html#activated">activated</a>(int)), this, SLOT(doOption(int)) );
status = new <a href="qlabel.html">QLabel</a>(this);
status-><a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::WinPanel | QFrame::Sunken );
<a name="x1354"></a> status-><a href="qwidget.html#setFixedHeight">setFixedHeight</a>( <a href="qwidget.html#fontMetrics">fontMetrics</a>().height() + 4 );
<a href="qwidget.html#setMouseTracking">setMouseTracking</a>( TRUE );
}
ImageViewer::~ImageViewer()
{
if ( alloc_context )
<a name="x1313"></a> QColor::<a href="qcolor.html#destroyAllocContext">destroyAllocContext</a>( alloc_context );
if ( other == this )
other = 0;
}
/*
This function modifies the conversion_flags when an options menu item
is selected, then ensures all menu items are up to date, and reconverts
the image if possibly necessary.
*/
void <a name="f406"></a>ImageViewer::doOption(int item)
{
if ( item == ss || item == cc ) {
// Toggle
<a name="x1329"></a> bool newbool = !options-><a href="qmenudata.html#isItemChecked">isItemChecked</a>(item);
<a name="x1330"></a> options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>(item, newbool);
// And reconvert...
reconvertImage();
<a href="qwidget.html#repaint">repaint</a>(image.hasAlphaBuffer()); // show image in widget
return;
}
if ( options-><a href="qmenudata.html#isItemChecked">isItemChecked</a>( item ) ) return; // They are all radio buttons
int ocf = conversion_flags;
if ( item == ac ) {
conversion_flags = ( conversion_flags & ~ColorMode_Mask ) | AutoColor;
} else if ( item == co ) {
conversion_flags = ( conversion_flags & ~ColorMode_Mask ) | ColorOnly;
} else if ( item == mo ) {
conversion_flags = ( conversion_flags & ~ColorMode_Mask ) | MonoOnly;
} else if ( item == fd ) {
conversion_flags = ( conversion_flags & ~Dither_Mask ) | DiffuseDither;
} else if ( item == bd ) {
conversion_flags = ( conversion_flags & ~Dither_Mask ) | OrderedDither;
} else if ( item == td ) {
conversion_flags = ( conversion_flags & ~Dither_Mask ) | ThresholdDither;
} else if ( item == ta ) {
conversion_flags = ( conversion_flags & ~AlphaDither_Mask ) | ThresholdAlphaDither;
} else if ( item == fa ) {
conversion_flags = ( conversion_flags & ~AlphaDither_Mask ) | DiffuseAlphaDither;
} else if ( item == ba ) {
conversion_flags = ( conversion_flags & ~AlphaDither_Mask ) | OrderedAlphaDither;
} else if ( item == ad ) {
conversion_flags = ( conversion_flags & ~DitherMode_Mask ) | PreferDither;
} else if ( item == dd ) {
conversion_flags = ( conversion_flags & ~DitherMode_Mask ) | AvoidDither;
}
if ( ocf != conversion_flags ) {
setMenuItemFlags();
// And reconvert...
reconvertImage();
<a href="qwidget.html#repaint">repaint</a>(image.hasAlphaBuffer()); // show image in widget
}
}
/*
Set the options menu to reflect the conversion_flags value.
*/
void <a name="f407"></a>ImageViewer::setMenuItemFlags()
{
// File
bool valid_image = pm.size() != QSize( 0, 0 );
file-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( si, valid_image );
file-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( sp, valid_image );
// Edit
edit-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( t1, image.depth() != 1 );
edit-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( t8, image.depth() != 8 );
edit-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( t32, image.depth() != 32 );
// Options
bool may_need_color_dithering =
!valid_image
|| image.depth() == 32 && QPixmap::<a href="qpixmap.html#defaultDepth">defaultDepth</a>() < 24;
bool may_need_dithering = may_need_color_dithering
|| image.depth() > 1 && options-><a href="qmenudata.html#isItemChecked">isItemChecked</a>(mo)
|| image.depth() > 1 && QPixmap::<a href="qpixmap.html#defaultDepth">defaultDepth</a>() == 1;
bool has_alpha_mask = !valid_image || image.hasAlphaBuffer();
options-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( fd, may_need_dithering );
options-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( bd, may_need_dithering );
options-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( td, may_need_dithering );
options-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( ta, has_alpha_mask );
options-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( fa, has_alpha_mask );
options-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( ba, has_alpha_mask );
options-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( ad, may_need_color_dithering );
options-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( dd, may_need_color_dithering );
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( ac, (conversion_flags & ColorMode_Mask) == AutoColor );
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( co, (conversion_flags & ColorMode_Mask) == ColorOnly );
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( mo, (conversion_flags & ColorMode_Mask) == MonoOnly );
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( fd, (conversion_flags & Dither_Mask) == DiffuseDither );
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( bd, (conversion_flags & Dither_Mask) == OrderedDither );
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( td, (conversion_flags & Dither_Mask) == ThresholdDither );
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( ta, (conversion_flags & AlphaDither_Mask) == ThresholdAlphaDither );
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( fa, (conversion_flags & AlphaDither_Mask) == DiffuseAlphaDither );
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( ba, (conversion_flags & AlphaDither_Mask) == OrderedAlphaDither );
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( ad, (conversion_flags & DitherMode_Mask) == PreferDither );
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( dd, (conversion_flags & DitherMode_Mask) == AvoidDither );
}
void <a name="f408"></a>ImageViewer::updateStatus()
{
if ( pm.size() == QSize( 0, 0 ) ) {
if ( !filename.isEmpty() )
status-><a href="qlabel.html#setText">setText</a>("Could not load image");
else
status-><a href="qlabel.html#setText">setText</a>("No image - select Open from File menu.");
} else {
<a href="qstring.html">QString</a> message, moremsg;
message.<a href="qstring.html#sprintf">sprintf</a>("%dx%d", image.width(), image.height());
if ( pm.size() != pmScaled.size() ) {
moremsg.sprintf(" [%dx%d]", pmScaled.width(),
pmScaled.height());
message += moremsg;
}
moremsg.sprintf(", %d bits ", image.depth());
message += moremsg;
if (image.valid(pickx,picky)) {
moremsg.sprintf("(%d,%d)=#%0*x ",
pickx, picky,
image.hasAlphaBuffer() ? 8 : 6,
image.pixel(pickx,picky));
message += moremsg;
}
if ( image.numColors() > 0 ) {
if (image.valid(pickx,picky)) {
moremsg.sprintf(", %d/%d colors", image.pixelIndex(pickx,picky),
image.numColors());
} else {
moremsg.sprintf(", %d colors", image.numColors());
}
message += moremsg;
}
if ( image.hasAlphaBuffer() ) {
if ( image.depth() == 8 ) {
int i;
bool alpha[256];
int nalpha=0;
for (i=0; i<256; i++)
alpha[i] = FALSE;
for (i=0; i<image.numColors(); i++) {
int alevel = image.color(i) >> 24;
if (!alpha[alevel]) {
alpha[alevel] = TRUE;
nalpha++;
}
}
moremsg.sprintf(", %d alpha levels", nalpha);
} else {
// Too many pixels to bother counting.
moremsg = ", 8-bit alpha channel";
}
message += moremsg;
}
status-><a href="qlabel.html#setText">setText</a>(message);
}
}
/*
This function saves the image.
*/
void <a name="f409"></a>ImageViewer::saveImage( int item )
{
<a name="x1332"></a> const char* fmt = saveimage-><a href="qmenudata.html#text">text</a>(item);
<a name="x1319"></a> <a href="qstring.html">QString</a> savefilename = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>(QString::null, <a href="qstring.html#QString-null">QString::null</a>,
this, filename);
if ( !savefilename.<a href="qstring.html#isEmpty">isEmpty</a>() )
if ( !image.save( savefilename, fmt ) )
<a name="x1333"></a> QMessageBox::<a href="qmessagebox.html#warning">warning</a>( this, "Save failed", "Error saving file" );
}
/*
This function saves the converted image.
*/
void <a name="f410"></a>ImageViewer::savePixmap( int item )
{
const char* fmt = savepixmap-><a href="qmenudata.html#text">text</a>(item);
<a href="qstring.html">QString</a> savefilename = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>(QString::null,
QString::null, this, filename);
if ( !savefilename.<a href="qstring.html#isEmpty">isEmpty</a>() )
if ( !pmScaled.save( savefilename, fmt ) )
QMessageBox::<a href="qmessagebox.html#warning">warning</a>( this, "Save failed", "Error saving file" );
}
void <a name="f411"></a>ImageViewer::newWindow()
{
ImageViewer* that = new ImageViewer(0, "new window", WDestructiveClose);
that->options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( that->cc, useColorContext() );
that-><a href="qwidget.html#show">show</a>();
}
/*
This function is the slot for processing the Open menu item.
*/
void <a name="f412"></a>ImageViewer::openFile()
{
<a name="x1318"></a> <a href="qstring.html">QString</a> newfilename = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( QString::null,
QString::null,
this );
if ( !newfilename.<a href="qstring.html#isEmpty">isEmpty</a>() ) {
loadImage( newfilename ) ;
<a href="qwidget.html#repaint">repaint</a>(); // show image in widget
}
}
/*
This function loads an image from a file and resizes the widget to
exactly fit the image size. If the file was not found or the image
format was unknown it will resize the widget to fit the errorText
message (see above) displayed in the current font.
Returns TRUE if the image was successfully loaded.
*/
bool <a name="f413"></a>ImageViewer::loadImage( const <a href="qstring.html">QString</a>& fileName )
{
filename = fileName;
bool ok = FALSE;
if ( !filename.isEmpty() ) {
<a name="x1312"></a> QApplication::<a href="qapplication.html#setOverrideCursor">setOverrideCursor</a>( waitCursor ); // this might take time
ok = image.load(filename, 0);
pickx = -1;
clickx = -1;
if ( ok )
ok = reconvertImage();
if ( ok ) {
<a href="qwidget.html#setCaption">setCaption</a>( filename ); // set window caption
int w = pm.width();
int h = pm.height();
const int reasonable_width = 128;
if ( w < reasonable_width ) {
// Integer scale up to something reasonable
int multiply = ( reasonable_width + w - 1 ) / w;
w *= multiply;
h *= multiply;
}
<a name="x1347"></a><a name="x1325"></a> h += menubar-><a href="qmenubar.html#heightForWidth">heightForWidth</a>(w) + status-><a href="qwidget.html#height">height</a>();
<a href="qwidget.html#resize">resize</a>( w, h ); // we resize to fit image
} else {
pm.resize(0,0); // couldn't load image
<a href="qwidget.html#update">update</a>();
}
<a name="x1311"></a> QApplication::<a href="qapplication.html#restoreOverrideCursor">restoreOverrideCursor</a>(); // restore original cursor
}
updateStatus();
setMenuItemFlags();
return ok;
}
bool <a name="f414"></a>ImageViewer::reconvertImage()
{
bool success = FALSE;
if ( image.isNull() ) return FALSE;
if ( alloc_context ) {
QColor::<a href="qcolor.html#destroyAllocContext">destroyAllocContext</a>( alloc_context );
alloc_context = 0;
}
if ( useColorContext() ) {
<a name="x1314"></a> alloc_context = QColor::<a href="qcolor.html#enterAllocContext">enterAllocContext</a>();
// Clear the image to hide flickering palette
<a href="qpainter.html">QPainter</a> painter(this);
<a name="x1338"></a> painter.<a href="qpainter.html#eraseRect">eraseRect</a>(0, menubar-><a href="qmenubar.html#heightForWidth">heightForWidth</a>( <a href="qwidget.html#width">width</a>() ), width(), height());
}
QApplication::<a href="qapplication.html#setOverrideCursor">setOverrideCursor</a>( waitCursor ); // this might take time
if ( pm.convertFromImage(image, conversion_flags) )
{
pmScaled = QPixmap();
scale();
<a href="qwidget.html#resize">resize</a>( <a href="qwidget.html#width">width</a>(), height() );
success = TRUE; // load successful
} else {
pm.resize(0,0); // couldn't load image
}
updateStatus();
setMenuItemFlags();
QApplication::<a href="qapplication.html#restoreOverrideCursor">restoreOverrideCursor</a>(); // restore original cursor
if ( useColorContext() )
<a name="x1315"></a> QColor::<a href="qcolor.html#leaveAllocContext">leaveAllocContext</a>();
return success; // TRUE if loaded OK
}
bool <a name="f415"></a>ImageViewer::smooth() const
{
return options-><a href="qmenudata.html#isItemChecked">isItemChecked</a>(ss);
}
bool <a name="f416"></a>ImageViewer::useColorContext() const
{
return options-><a href="qmenudata.html#isItemChecked">isItemChecked</a>(cc);
}
/*
This functions scales the pixmap in the member variable "pm" to fit the
widget size and puts the resulting pixmap in the member variable "pmScaled".
*/
void <a name="f417"></a>ImageViewer::scale()
{
int h = <a href="qwidget.html#height">height</a>() - menubar-><a href="qmenubar.html#heightForWidth">heightForWidth</a>( <a href="qwidget.html#width">width</a>() ) - status-><a href="qwidget.html#height">height</a>();
if ( image.isNull() ) return;
QApplication::<a href="qapplication.html#setOverrideCursor">setOverrideCursor</a>( waitCursor ); // this might take time
if ( <a href="qwidget.html#width">width</a>() == pm.width() && h == pm.height() )
{ // no need to scale if widget
pmScaled = pm; // size equals pixmap size
} else {
if (smooth()) {
pmScaled.convertFromImage(image.smoothScale(<a href="qwidget.html#width">width</a>(), h),
conversion_flags);
} else {
<a href="qwmatrix.html">QWMatrix</a> m; // <a href="qwmatrix.html#TransformationMode">transformation matrix</a>
<a name="x1357"></a> m.<a href="qwmatrix.html#scale">scale</a>(((double)<a href="qwidget.html#width">width</a>())/pm.width(),// define scale factors
((double)h)/pm.height());
pmScaled = pm.xForm( m ); // create scaled pixmap
}
}
QApplication::<a href="qapplication.html#restoreOverrideCursor">restoreOverrideCursor</a>(); // restore original cursor
}
/*
The resize event handler, if a valid pixmap was loaded it will call
scale() to fit the pixmap to the new widget size.
*/
<a name="x1353"></a>void ImageViewer::<a href="qwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> * )
{
<a name="x1355"></a> status-><a href="qwidget.html#setGeometry">setGeometry</a>(0, height() - status-><a href="qwidget.html#height">height</a>(),
<a href="qwidget.html#width">width</a>(), status-><a href="qwidget.html#height">height</a>());
if ( pm.size() == QSize( 0, 0 ) ) // we couldn't load the image
return;
int h = <a href="qwidget.html#height">height</a>() - menubar-><a href="qmenubar.html#heightForWidth">heightForWidth</a>( <a href="qwidget.html#width">width</a>() ) - status-><a href="qwidget.html#height">height</a>();
if ( <a href="qwidget.html#width">width</a>() != pmScaled.width() || h != pmScaled.height())
{ // if new size,
scale(); // scale pmScaled to window
updateStatus();
}
if ( image.hasAlphaBuffer() )
<a href="qwidget.html#erase">erase</a>();
}
bool <a name="f418"></a>ImageViewer::convertEvent( <a href="qmouseevent.html">QMouseEvent</a>* e, int& x, int& y)
{
if ( pm.size() != QSize( 0, 0 ) ) {
int h = <a href="qwidget.html#height">height</a>() - menubar-><a href="qmenubar.html#heightForWidth">heightForWidth</a>( <a href="qwidget.html#width">width</a>() ) - status-><a href="qwidget.html#height">height</a>();
<a name="x1335"></a> int nx = e-><a href="qmouseevent.html#x">x</a>() * image.width() / width();
<a name="x1336"></a> int ny = (e-><a href="qmouseevent.html#y">y</a>()-menubar-><a href="qmenubar.html#heightForWidth">heightForWidth</a>( <a href="qwidget.html#width">width</a>() )) * image.height() / h;
if (nx != x || ny != y ) {
x = nx;
y = ny;
updateStatus();
return TRUE;
}
}
return FALSE;
}
void ImageViewer::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
{
may_be_other = convertEvent(e, clickx, clicky);
}
<a name="x1350"></a>void ImageViewer::<a href="qwidget.html#mouseReleaseEvent">mouseReleaseEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * )
{
if ( may_be_other )
other = this;
}
/*
Record the pixel position of interest.
*/
void ImageViewer::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
{
if (convertEvent(e,pickx,picky)) {
updateStatus();
<a name="x1334"></a> if ((e-><a href="qmouseevent.html#state">state</a>()&LeftButton)) {
may_be_other = FALSE;
if ( clickx >= 0 && other) {
copyFrom(other);
}
}
}
}
/*
Draws the portion of the scaled pixmap that needs to be updated or prints
an error message if no legal pixmap has been loaded.
*/
void ImageViewer::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> *e )
{
if ( pm.size() != QSize( 0, 0 ) ) { // is an image loaded?
<a href="qpainter.html">QPainter</a> painter(this);
<a name="x1340"></a><a name="x1339"></a> painter.<a href="qpainter.html#setClipRect">setClipRect</a>(e-><a href="qpaintevent.html#rect">rect</a>());
<a name="x1337"></a> painter.<a href="qpainter.html#drawPixmap">drawPixmap</a>(0, menubar-><a href="qmenubar.html#heightForWidth">heightForWidth</a>( <a href="qwidget.html#width">width</a>() ), pmScaled);
}
}
/*
Explain anything that might be confusing.
*/
void <a name="f419"></a>ImageViewer::giveHelp()
{
if (!helpmsg) {
<a href="qstring.html">QString</a> helptext =
"<b>Usage:</b> <tt>showimg [-m] <i>filename ...</i></tt>"
"<blockquote>"
"<tt>-m</tt> - use <i>ManyColor</i> color spec"
"</blockquote>"
"<p>Supported input formats:"
"<blockquote>";
<a name="x1321"></a> helptext += QImage::<a href="qimage.html#inputFormatList">inputFormatList</a>().join(", ");
helptext += "</blockquote>";
helpmsg = new <a href="qmessagebox.html">QMessageBox</a>( "Help", helptext,
QMessageBox::Information, QMessageBox::Ok, 0, 0, 0, 0, FALSE );
}
<a name="x1317"></a> helpmsg-><a href="qdialog.html#show">show</a>();
<a name="x1352"></a> helpmsg-><a href="qwidget.html#raise">raise</a>();
}
void <a name="f420"></a>ImageViewer::copyFrom(ImageViewer* s)
{
if ( clickx >= 0 ) {
int dx = clickx;
int dy = clicky;
int sx = s->clickx;
int sy = s->clicky;
int sw = QABS(clickx - pickx)+1;
int sh = QABS(clicky - picky)+1;
if ( clickx > pickx ) {
dx = pickx;
sx -= sw-1;
}
if ( clicky > picky ) {
dy = picky;
sy -= sh-1;
}
<a href="qimage.html#bitBlt">bitBlt</a>( &image, dx, dy, &s->image, sx, sy, sw, sh );
reconvertImage();
<a href="qwidget.html#repaint">repaint</a>( image.hasAlphaBuffer() );
}
}
ImageViewer* ImageViewer::other = 0;
void <a name="f421"></a>ImageViewer::hFlip()
{
setImage(image.mirror(TRUE,FALSE));
}
void <a name="f422"></a>ImageViewer::vFlip()
{
setImage(image.mirror(FALSE,TRUE));
}
void <a name="f423"></a>ImageViewer::rot180()
{
setImage(image.mirror(TRUE,TRUE));
}
void <a name="f424"></a>ImageViewer::copy()
{
#ifndef QT_NO_MIMECLIPBOARD
<a name="x1308"></a> QApplication::<a href="qapplication.html#clipboard">clipboard</a>()->setImage(image); // Less information loss
#endif
}
void <a name="f425"></a>ImageViewer::paste()
{
#ifndef QT_NO_MIMECLIPBOARD
<a href="qimage.html">QImage</a> p = QApplication::<a href="qapplication.html#clipboard">clipboard</a>()->image();
<a name="x1322"></a> if ( !p.<a href="qimage.html#isNull">isNull</a>() ) {
filename = "pasted";
setImage(p);
}
#endif
}
void <a name="f426"></a>ImageViewer::setImage(const <a href="qimage.html">QImage</a>& newimage)
{
image = newimage;
pickx = -1;
clickx = -1;
<a href="qwidget.html#setCaption">setCaption</a>( filename ); // set window caption
int w = image.width();
int h = image.height();
if ( !w )
return;
const int reasonable_width = 128;
if ( w < reasonable_width ) {
// Integer scale up to something reasonable
int multiply = ( reasonable_width + w - 1 ) / w;
w *= multiply;
h *= multiply;
}
h += menubar-><a href="qmenubar.html#heightForWidth">heightForWidth</a>(w) + status-><a href="qwidget.html#height">height</a>();
<a href="qwidget.html#resize">resize</a>( w, h ); // we resize to fit image
reconvertImage();
<a href="qwidget.html#repaint">repaint</a>( image.hasAlphaBuffer() );
updateStatus();
setMenuItemFlags();
}
void <a name="f427"></a>ImageViewer::editText()
{
ImageTextEditor editor(image,this);
editor.<a href="qdialog.html#exec">exec</a>();
}
void <a name="f428"></a>ImageViewer::to1Bit()
{
toBitDepth(1);
}
void <a name="f429"></a>ImageViewer::to8Bit()
{
toBitDepth(8);
}
void <a name="f430"></a>ImageViewer::to32Bit()
{
toBitDepth(32);
}
void <a name="f431"></a>ImageViewer::toBitDepth(int d)
{
image = image.convertDepth(d);
reconvertImage();
<a href="qwidget.html#repaint">repaint</a>( image.hasAlphaBuffer() );
}
</pre>
<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id: qt/main.cpp 3.3.8 edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "showimg.h"
#include "imagefip.h"
#include <<a href="qapplication-h.html">qapplication.h</a>>
#include <<a href="qimage-h.html">qimage.h</a>>
int main( int argc, char **argv )
{
if ( argc > 1 && QString(argv[1]) == "-m" ) {
<a name="x1361"></a> QApplication::<a href="qapplication.html#setColorSpec">setColorSpec</a>( QApplication::ManyColor );
argc--;
argv++;
}
else if ( argc > 1 && QString(argv[1]) == "-n" ) {
QApplication::<a href="qapplication.html#setColorSpec">setColorSpec</a>( QApplication::NormalColor );
argc--;
argv++;
}
else {
QApplication::<a href="qapplication.html#setColorSpec">setColorSpec</a>( QApplication::CustomColor );
}
<a href="qapplication.html">QApplication</a> a( argc, argv );
ImageIconProvider iip;
<a name="x1362"></a> QFileDialog::<a href="qfiledialog.html#setIconProvider">setIconProvider</a>( &iip );
if ( argc <= 1 ) {
// Create a window which looks after its own existence.
ImageViewer *w =
new ImageViewer(0, "new window", Qt::WDestructiveClose | Qt::WResizeNoErase );
<a name="x1364"></a> w-><a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Image Viewer");
w-><a href="qwidget.html#show">show</a>();
} else {
for ( int i=1; i<argc; i++ ) {
// Create a window which looks after its own existence.
ImageViewer *w =
new ImageViewer(0, argv[i], Qt::WDestructiveClose | Qt::WResizeNoErase );
w-><a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Image Viewer");
w->loadImage( argv[i] );
w-><a href="qwidget.html#show">show</a>();
}
}
QObject::<a href="qobject.html#connect">connect</a>(qApp, SIGNAL(<a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()));
return a.<a href="qapplication.html#exec">exec</a>();
}
</pre>
<p>See also <a href="examples.html">Examples</a>.
<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright © 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>
|