source: trunk/doc/html/showimg-example.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 39.7 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/showimg/showimg.doc:4 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Show Image</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<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>
33
34
35<p>
36This example reads and displays an image in any supported image
37format (GIF, BMP, PPM, XMP, etc.).
38<p> <hr>
39<p> Header file:
40<p> <pre>/****************************************************************************
41** $Id: showimg-example.html 2051 2007-02-21 10:04:20Z chehrlic $
42**
43** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
44**
45** This file is part of an example program for Qt. This example
46** program may be used, distributed and modified without limitation.
47**
48*****************************************************************************/
49
50#ifndef SHOWIMG_H
51#define SHOWIMG_H
52
53#include &lt;<a href="qwidget-h.html">qwidget.h</a>&gt;
54#include &lt;<a href="qimage-h.html">qimage.h</a>&gt;
55
56
57class QLabel;
58class QMenuBar;
59class QPopupMenu;
60
61class ImageViewer : public <a href="qwidget.html">QWidget</a>
62{
63 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
64public:
65 ImageViewer( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0, int wFlags=0 );
66 ~ImageViewer();
67 bool loadImage( const <a href="qstring.html">QString</a>&amp; );
68protected:
69 void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
70 void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );
71 void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
72 void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
73 void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
74
75private:
76 void scale();
77 int conversion_flags;
78 bool smooth() const;
79 bool useColorContext() const;
80 int alloc_context;
81 bool convertEvent( <a href="qmouseevent.html">QMouseEvent</a>* e, int&amp; x, int&amp; y );
82 <a href="qstring.html">QString</a> filename;
83 <a href="qimage.html">QImage</a> image; // the loaded image
84 <a href="qpixmap.html">QPixmap</a> pm; // the converted pixmap
85 <a href="qpixmap.html">QPixmap</a> pmScaled; // the scaled pixmap
86
87 <a href="qmenubar.html">QMenuBar</a> *menubar;
88 <a href="qpopupmenu.html">QPopupMenu</a> *file;
89 <a href="qpopupmenu.html">QPopupMenu</a> *saveimage;
90 <a href="qpopupmenu.html">QPopupMenu</a> *savepixmap;
91 <a href="qpopupmenu.html">QPopupMenu</a> *edit;
92 <a href="qpopupmenu.html">QPopupMenu</a> *options;
93
94 <a href="qwidget.html">QWidget</a> *helpmsg;
95 <a href="qlabel.html">QLabel</a> *status;
96 int si, sp, ac, co, mo, fd, bd, // Menu item ids
97 td, ta, ba, fa, au, ad, dd,
98 ss, cc, t1, t8, t32;
99 void updateStatus();
100 void setMenuItemFlags();
101 bool reconvertImage();
102 int pickx, picky;
103 int clickx, clicky;
104 bool may_be_other;
105 static ImageViewer* other;
106 void setImage(const <a href="qimage.html">QImage</a>&amp; newimage);
107
108private slots:
109 void to1Bit();
110 void to8Bit();
111 void to32Bit();
112 void toBitDepth(int);
113
114 void copy();
115 void paste();
116
117 void hFlip();
118 void vFlip();
119 void rot180();
120
121 void editText();
122
123 void newWindow();
124 void openFile();
125 void saveImage(int);
126 void savePixmap(int);
127 void giveHelp();
128 void doOption(int);
129 void copyFrom(ImageViewer*);
130};
131
132
133#endif // SHOWIMG_H
134</pre>
135
136<p> <hr>
137<p> Implementation:
138<p> <pre>/****************************************************************************
139** $Id: showimg-example.html 2051 2007-02-21 10:04:20Z chehrlic $
140**
141** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
142**
143** This file is part of an example program for Qt. This example
144** program may be used, distributed and modified without limitation.
145**
146*****************************************************************************/
147
148#include "showimg.h"
149#include "imagetexteditor.h"
150#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
151#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
152#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
153#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
154#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
155#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
156#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
157#include &lt;<a href="qclipboard-h.html">qclipboard.h</a>&gt;
158
159
160/*
161 In the constructor, we just pass the standard parameters on to
162 QWidget.
163
164 The menu uses a single slot to simplify the process of adding
165 more items to the options menu.
166*/
167<a name="f405"></a>ImageViewer::ImageViewer( <a href="qwidget.html">QWidget</a> *parent, const char *name, int wFlags )
168 : <a href="qwidget.html">QWidget</a>( parent, name, wFlags ),
169 conversion_flags( PreferDither ),
170 helpmsg( 0 )
171{
172 pickx = -1;
173 picky = -1;
174 clickx = -1;
175 clicky = -1;
176 alloc_context = 0;
177
178 menubar = new <a href="qmenubar.html">QMenuBar</a>(this);
179<a name="x1326"></a> menubar-&gt;<a href="qmenubar.html#setSeparator">setSeparator</a>( QMenuBar::InWindowsStyle );
180
181<a name="x1323"></a> <a href="qstrlist.html">QStrList</a> fmt = QImage::<a href="qimage.html#outputFormats">outputFormats</a>();
182 saveimage = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
183 savepixmap = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
184<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>()) {
185<a name="x1327"></a> saveimage-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( f );
186 savepixmap-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( f );
187 }
188<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)) );
189 <a href="qobject.html#connect">connect</a>( savepixmap, SIGNAL(<a href="qpopupmenu.html#activated">activated</a>(int)), this, SLOT(savePixmap(int)) );
190
191 file = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
192 menubar-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;File", file );
193 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;New window", this, SLOT(newWindow()), CTRL+Key_N );
194 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Open...", this, SLOT(openFile()), CTRL+Key_O );
195 si = file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Save image", saveimage );
196 sp = file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Save pixmap", savepixmap );
197<a name="x1328"></a> file-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
198 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "E&amp;xit", qApp, SLOT(<a href="qapplication.html#quit">quit</a>()), CTRL+Key_Q );
199
200 edit = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
201 menubar-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Edit", edit );
202 edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Copy", this, SLOT(copy()), CTRL+Key_C);
203 edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Paste", this, SLOT(paste()), CTRL+Key_V);
204 edit-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
205 edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Horizontal flip", this, SLOT(hFlip()), ALT+Key_H);
206 edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Vertical flip", this, SLOT(vFlip()), ALT+Key_V);
207 edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Rotate 180", this, SLOT(rot180()), ALT+Key_R);
208 edit-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
209 edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Text...", this, SLOT(editText()));
210 edit-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
211 t1 = edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Convert to &amp;1 bit", this, SLOT(to1Bit()) );
212 t8 = edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Convert to &amp;8 bit", this, SLOT(to8Bit()) );
213 t32 = edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Convert to &amp;32 bit", this, SLOT(to32Bit()) );
214
215 options = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
216 menubar-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Options", options );
217 ac = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "AutoColor" );
218 co = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "ColorOnly" );
219 mo = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "MonoOnly" );
220 options-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
221 fd = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "DiffuseDither" );
222 bd = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "OrderedDither" );
223 td = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "ThresholdDither" );
224 options-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
225 ta = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "ThresholdAlphaDither" );
226 ba = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "OrderedAlphaDither" );
227 fa = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "DiffuseAlphaDither" );
228 options-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
229 ad = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "PreferDither" );
230 dd = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "AvoidDither" );
231 options-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
232 ss = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Smooth scaling" );
233 cc = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Use color context" );
234<a name="x1309"></a> if ( QApplication::<a href="qapplication.html#colorSpec">colorSpec</a>() == QApplication::ManyColor )
235<a name="x1331"></a> options-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( cc, FALSE );
236<a name="x1342"></a> options-&gt;<a href="qpopupmenu.html#setCheckable">setCheckable</a>( TRUE );
237 setMenuItemFlags();
238
239 menubar-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
240
241 <a href="qpopupmenu.html">QPopupMenu</a>* help = new <a href="qpopupmenu.html">QPopupMenu</a>( menubar );
242 menubar-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Help", help );
243 help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Help!", this, SLOT(giveHelp()), CTRL+Key_H );
244
245 <a href="qobject.html#connect">connect</a>( options, SIGNAL(<a href="qpopupmenu.html#activated">activated</a>(int)), this, SLOT(doOption(int)) );
246
247 status = new <a href="qlabel.html">QLabel</a>(this);
248 status-&gt;<a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::WinPanel | QFrame::Sunken );
249<a name="x1354"></a> status-&gt;<a href="qwidget.html#setFixedHeight">setFixedHeight</a>( <a href="qwidget.html#fontMetrics">fontMetrics</a>().height() + 4 );
250
251 <a href="qwidget.html#setMouseTracking">setMouseTracking</a>( TRUE );
252}
253
254ImageViewer::~ImageViewer()
255{
256 if ( alloc_context )
257<a name="x1313"></a> QColor::<a href="qcolor.html#destroyAllocContext">destroyAllocContext</a>( alloc_context );
258 if ( other == this )
259 other = 0;
260}
261
262/*
263 This function modifies the conversion_flags when an options menu item
264 is selected, then ensures all menu items are up to date, and reconverts
265 the image if possibly necessary.
266*/
267void <a name="f406"></a>ImageViewer::doOption(int item)
268{
269 if ( item == ss || item == cc ) {
270 // Toggle
271<a name="x1329"></a> bool newbool = !options-&gt;<a href="qmenudata.html#isItemChecked">isItemChecked</a>(item);
272<a name="x1330"></a> options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>(item, newbool);
273 // And reconvert...
274 reconvertImage();
275 <a href="qwidget.html#repaint">repaint</a>(image.hasAlphaBuffer()); // show image in widget
276 return;
277 }
278
279 if ( options-&gt;<a href="qmenudata.html#isItemChecked">isItemChecked</a>( item ) ) return; // They are all radio buttons
280
281 int ocf = conversion_flags;
282
283 if ( item == ac ) {
284 conversion_flags = ( conversion_flags &amp; ~ColorMode_Mask ) | AutoColor;
285 } else if ( item == co ) {
286 conversion_flags = ( conversion_flags &amp; ~ColorMode_Mask ) | ColorOnly;
287 } else if ( item == mo ) {
288 conversion_flags = ( conversion_flags &amp; ~ColorMode_Mask ) | MonoOnly;
289 } else if ( item == fd ) {
290 conversion_flags = ( conversion_flags &amp; ~Dither_Mask ) | DiffuseDither;
291 } else if ( item == bd ) {
292 conversion_flags = ( conversion_flags &amp; ~Dither_Mask ) | OrderedDither;
293 } else if ( item == td ) {
294 conversion_flags = ( conversion_flags &amp; ~Dither_Mask ) | ThresholdDither;
295 } else if ( item == ta ) {
296 conversion_flags = ( conversion_flags &amp; ~AlphaDither_Mask ) | ThresholdAlphaDither;
297 } else if ( item == fa ) {
298 conversion_flags = ( conversion_flags &amp; ~AlphaDither_Mask ) | DiffuseAlphaDither;
299 } else if ( item == ba ) {
300 conversion_flags = ( conversion_flags &amp; ~AlphaDither_Mask ) | OrderedAlphaDither;
301 } else if ( item == ad ) {
302 conversion_flags = ( conversion_flags &amp; ~DitherMode_Mask ) | PreferDither;
303 } else if ( item == dd ) {
304 conversion_flags = ( conversion_flags &amp; ~DitherMode_Mask ) | AvoidDither;
305 }
306
307 if ( ocf != conversion_flags ) {
308 setMenuItemFlags();
309 // And reconvert...
310 reconvertImage();
311 <a href="qwidget.html#repaint">repaint</a>(image.hasAlphaBuffer()); // show image in widget
312 }
313}
314
315/*
316 Set the options menu to reflect the conversion_flags value.
317*/
318void <a name="f407"></a>ImageViewer::setMenuItemFlags()
319{
320 // File
321 bool valid_image = pm.size() != QSize( 0, 0 );
322 file-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( si, valid_image );
323 file-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( sp, valid_image );
324
325 // Edit
326 edit-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( t1, image.depth() != 1 );
327 edit-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( t8, image.depth() != 8 );
328 edit-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( t32, image.depth() != 32 );
329
330 // Options
331 bool may_need_color_dithering =
332 !valid_image
333 || image.depth() == 32 &amp;&amp; QPixmap::<a href="qpixmap.html#defaultDepth">defaultDepth</a>() &lt; 24;
334 bool may_need_dithering = may_need_color_dithering
335 || image.depth() &gt; 1 &amp;&amp; options-&gt;<a href="qmenudata.html#isItemChecked">isItemChecked</a>(mo)
336 || image.depth() &gt; 1 &amp;&amp; QPixmap::<a href="qpixmap.html#defaultDepth">defaultDepth</a>() == 1;
337 bool has_alpha_mask = !valid_image || image.hasAlphaBuffer();
338
339 options-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( fd, may_need_dithering );
340 options-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( bd, may_need_dithering );
341 options-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( td, may_need_dithering );
342
343 options-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( ta, has_alpha_mask );
344 options-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( fa, has_alpha_mask );
345 options-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( ba, has_alpha_mask );
346
347 options-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( ad, may_need_color_dithering );
348 options-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( dd, may_need_color_dithering );
349
350 options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( ac, (conversion_flags &amp; ColorMode_Mask) == AutoColor );
351 options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( co, (conversion_flags &amp; ColorMode_Mask) == ColorOnly );
352 options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( mo, (conversion_flags &amp; ColorMode_Mask) == MonoOnly );
353 options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( fd, (conversion_flags &amp; Dither_Mask) == DiffuseDither );
354 options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( bd, (conversion_flags &amp; Dither_Mask) == OrderedDither );
355 options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( td, (conversion_flags &amp; Dither_Mask) == ThresholdDither );
356 options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( ta, (conversion_flags &amp; AlphaDither_Mask) == ThresholdAlphaDither );
357 options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( fa, (conversion_flags &amp; AlphaDither_Mask) == DiffuseAlphaDither );
358 options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( ba, (conversion_flags &amp; AlphaDither_Mask) == OrderedAlphaDither );
359 options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( ad, (conversion_flags &amp; DitherMode_Mask) == PreferDither );
360 options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( dd, (conversion_flags &amp; DitherMode_Mask) == AvoidDither );
361}
362
363void <a name="f408"></a>ImageViewer::updateStatus()
364{
365 if ( pm.size() == QSize( 0, 0 ) ) {
366 if ( !filename.isEmpty() )
367 status-&gt;<a href="qlabel.html#setText">setText</a>("Could not load image");
368 else
369 status-&gt;<a href="qlabel.html#setText">setText</a>("No image - select Open from File menu.");
370 } else {
371 <a href="qstring.html">QString</a> message, moremsg;
372 message.<a href="qstring.html#sprintf">sprintf</a>("%dx%d", image.width(), image.height());
373 if ( pm.size() != pmScaled.size() ) {
374 moremsg.sprintf(" [%dx%d]", pmScaled.width(),
375 pmScaled.height());
376 message += moremsg;
377 }
378 moremsg.sprintf(", %d bits ", image.depth());
379 message += moremsg;
380 if (image.valid(pickx,picky)) {
381 moremsg.sprintf("(%d,%d)=#%0*x ",
382 pickx, picky,
383 image.hasAlphaBuffer() ? 8 : 6,
384 image.pixel(pickx,picky));
385 message += moremsg;
386 }
387 if ( image.numColors() &gt; 0 ) {
388 if (image.valid(pickx,picky)) {
389 moremsg.sprintf(", %d/%d colors", image.pixelIndex(pickx,picky),
390 image.numColors());
391 } else {
392 moremsg.sprintf(", %d colors", image.numColors());
393 }
394 message += moremsg;
395 }
396 if ( image.hasAlphaBuffer() ) {
397 if ( image.depth() == 8 ) {
398 int i;
399 bool alpha[256];
400 int nalpha=0;
401
402 for (i=0; i&lt;256; i++)
403 alpha[i] = FALSE;
404
405 for (i=0; i&lt;image.numColors(); i++) {
406 int alevel = image.color(i) &gt;&gt; 24;
407 if (!alpha[alevel]) {
408 alpha[alevel] = TRUE;
409 nalpha++;
410 }
411 }
412 moremsg.sprintf(", %d alpha levels", nalpha);
413 } else {
414 // Too many pixels to bother counting.
415 moremsg = ", 8-bit alpha channel";
416 }
417 message += moremsg;
418 }
419 status-&gt;<a href="qlabel.html#setText">setText</a>(message);
420 }
421}
422
423/*
424 This function saves the image.
425*/
426void <a name="f409"></a>ImageViewer::saveImage( int item )
427{
428<a name="x1332"></a> const char* fmt = saveimage-&gt;<a href="qmenudata.html#text">text</a>(item);
429<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>,
430 this, filename);
431 if ( !savefilename.<a href="qstring.html#isEmpty">isEmpty</a>() )
432 if ( !image.save( savefilename, fmt ) )
433<a name="x1333"></a> QMessageBox::<a href="qmessagebox.html#warning">warning</a>( this, "Save failed", "Error saving file" );
434}
435
436/*
437 This function saves the converted image.
438*/
439void <a name="f410"></a>ImageViewer::savePixmap( int item )
440{
441 const char* fmt = savepixmap-&gt;<a href="qmenudata.html#text">text</a>(item);
442 <a href="qstring.html">QString</a> savefilename = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>(QString::null,
443 QString::null, this, filename);
444 if ( !savefilename.<a href="qstring.html#isEmpty">isEmpty</a>() )
445 if ( !pmScaled.save( savefilename, fmt ) )
446 QMessageBox::<a href="qmessagebox.html#warning">warning</a>( this, "Save failed", "Error saving file" );
447}
448
449
450void <a name="f411"></a>ImageViewer::newWindow()
451{
452 ImageViewer* that = new ImageViewer(0, "new window", WDestructiveClose);
453 that-&gt;options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( that-&gt;cc, useColorContext() );
454 that-&gt;<a href="qwidget.html#show">show</a>();
455}
456
457/*
458 This function is the slot for processing the Open menu item.
459*/
460void <a name="f412"></a>ImageViewer::openFile()
461{
462<a name="x1318"></a> <a href="qstring.html">QString</a> newfilename = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( QString::null,
463 QString::null,
464 this );
465 if ( !newfilename.<a href="qstring.html#isEmpty">isEmpty</a>() ) {
466 loadImage( newfilename ) ;
467 <a href="qwidget.html#repaint">repaint</a>(); // show image in widget
468 }
469}
470
471/*
472 This function loads an image from a file and resizes the widget to
473 exactly fit the image size. If the file was not found or the image
474 format was unknown it will resize the widget to fit the errorText
475 message (see above) displayed in the current font.
476
477 Returns TRUE if the image was successfully loaded.
478*/
479
480bool <a name="f413"></a>ImageViewer::loadImage( const <a href="qstring.html">QString</a>&amp; fileName )
481{
482 filename = fileName;
483 bool ok = FALSE;
484 if ( !filename.isEmpty() ) {
485<a name="x1312"></a> QApplication::<a href="qapplication.html#setOverrideCursor">setOverrideCursor</a>( waitCursor ); // this might take time
486 ok = image.load(filename, 0);
487 pickx = -1;
488 clickx = -1;
489 if ( ok )
490 ok = reconvertImage();
491 if ( ok ) {
492 <a href="qwidget.html#setCaption">setCaption</a>( filename ); // set window caption
493 int w = pm.width();
494 int h = pm.height();
495
496 const int reasonable_width = 128;
497 if ( w &lt; reasonable_width ) {
498 // Integer scale up to something reasonable
499 int multiply = ( reasonable_width + w - 1 ) / w;
500 w *= multiply;
501 h *= multiply;
502 }
503
504<a name="x1347"></a><a name="x1325"></a> h += menubar-&gt;<a href="qmenubar.html#heightForWidth">heightForWidth</a>(w) + status-&gt;<a href="qwidget.html#height">height</a>();
505 <a href="qwidget.html#resize">resize</a>( w, h ); // we resize to fit image
506 } else {
507 pm.resize(0,0); // couldn't load image
508 <a href="qwidget.html#update">update</a>();
509 }
510<a name="x1311"></a> QApplication::<a href="qapplication.html#restoreOverrideCursor">restoreOverrideCursor</a>(); // restore original cursor
511 }
512 updateStatus();
513 setMenuItemFlags();
514 return ok;
515}
516
517bool <a name="f414"></a>ImageViewer::reconvertImage()
518{
519 bool success = FALSE;
520
521 if ( image.isNull() ) return FALSE;
522
523 if ( alloc_context ) {
524 QColor::<a href="qcolor.html#destroyAllocContext">destroyAllocContext</a>( alloc_context );
525 alloc_context = 0;
526 }
527 if ( useColorContext() ) {
528<a name="x1314"></a> alloc_context = QColor::<a href="qcolor.html#enterAllocContext">enterAllocContext</a>();
529 // Clear the image to hide flickering palette
530 <a href="qpainter.html">QPainter</a> painter(this);
531<a name="x1338"></a> painter.<a href="qpainter.html#eraseRect">eraseRect</a>(0, menubar-&gt;<a href="qmenubar.html#heightForWidth">heightForWidth</a>( <a href="qwidget.html#width">width</a>() ), width(), height());
532 }
533
534 QApplication::<a href="qapplication.html#setOverrideCursor">setOverrideCursor</a>( waitCursor ); // this might take time
535 if ( pm.convertFromImage(image, conversion_flags) )
536 {
537 pmScaled = QPixmap();
538 scale();
539 <a href="qwidget.html#resize">resize</a>( <a href="qwidget.html#width">width</a>(), height() );
540 success = TRUE; // load successful
541 } else {
542 pm.resize(0,0); // couldn't load image
543 }
544 updateStatus();
545 setMenuItemFlags();
546 QApplication::<a href="qapplication.html#restoreOverrideCursor">restoreOverrideCursor</a>(); // restore original cursor
547
548 if ( useColorContext() )
549<a name="x1315"></a> QColor::<a href="qcolor.html#leaveAllocContext">leaveAllocContext</a>();
550
551 return success; // TRUE if loaded OK
552}
553
554bool <a name="f415"></a>ImageViewer::smooth() const
555{
556 return options-&gt;<a href="qmenudata.html#isItemChecked">isItemChecked</a>(ss);
557}
558
559bool <a name="f416"></a>ImageViewer::useColorContext() const
560{
561 return options-&gt;<a href="qmenudata.html#isItemChecked">isItemChecked</a>(cc);
562}
563
564/*
565 This functions scales the pixmap in the member variable "pm" to fit the
566 widget size and puts the resulting pixmap in the member variable "pmScaled".
567*/
568
569void <a name="f417"></a>ImageViewer::scale()
570{
571 int h = <a href="qwidget.html#height">height</a>() - menubar-&gt;<a href="qmenubar.html#heightForWidth">heightForWidth</a>( <a href="qwidget.html#width">width</a>() ) - status-&gt;<a href="qwidget.html#height">height</a>();
572
573 if ( image.isNull() ) return;
574
575 QApplication::<a href="qapplication.html#setOverrideCursor">setOverrideCursor</a>( waitCursor ); // this might take time
576 if ( <a href="qwidget.html#width">width</a>() == pm.width() &amp;&amp; h == pm.height() )
577 { // no need to scale if widget
578 pmScaled = pm; // size equals pixmap size
579 } else {
580 if (smooth()) {
581 pmScaled.convertFromImage(image.smoothScale(<a href="qwidget.html#width">width</a>(), h),
582 conversion_flags);
583 } else {
584 <a href="qwmatrix.html">QWMatrix</a> m; // <a href="qwmatrix.html#TransformationMode">transformation matrix</a>
585<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
586 ((double)h)/pm.height());
587 pmScaled = pm.xForm( m ); // create scaled pixmap
588 }
589 }
590 QApplication::<a href="qapplication.html#restoreOverrideCursor">restoreOverrideCursor</a>(); // restore original cursor
591}
592
593/*
594 The resize event handler, if a valid pixmap was loaded it will call
595 scale() to fit the pixmap to the new widget size.
596*/
597
598<a name="x1353"></a>void ImageViewer::<a href="qwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> * )
599{
600<a name="x1355"></a> status-&gt;<a href="qwidget.html#setGeometry">setGeometry</a>(0, height() - status-&gt;<a href="qwidget.html#height">height</a>(),
601 <a href="qwidget.html#width">width</a>(), status-&gt;<a href="qwidget.html#height">height</a>());
602
603 if ( pm.size() == QSize( 0, 0 ) ) // we couldn't load the image
604 return;
605
606 int h = <a href="qwidget.html#height">height</a>() - menubar-&gt;<a href="qmenubar.html#heightForWidth">heightForWidth</a>( <a href="qwidget.html#width">width</a>() ) - status-&gt;<a href="qwidget.html#height">height</a>();
607 if ( <a href="qwidget.html#width">width</a>() != pmScaled.width() || h != pmScaled.height())
608 { // if new size,
609 scale(); // scale pmScaled to window
610 updateStatus();
611 }
612 if ( image.hasAlphaBuffer() )
613 <a href="qwidget.html#erase">erase</a>();
614}
615
616bool <a name="f418"></a>ImageViewer::convertEvent( <a href="qmouseevent.html">QMouseEvent</a>* e, int&amp; x, int&amp; y)
617{
618 if ( pm.size() != QSize( 0, 0 ) ) {
619 int h = <a href="qwidget.html#height">height</a>() - menubar-&gt;<a href="qmenubar.html#heightForWidth">heightForWidth</a>( <a href="qwidget.html#width">width</a>() ) - status-&gt;<a href="qwidget.html#height">height</a>();
620<a name="x1335"></a> int nx = e-&gt;<a href="qmouseevent.html#x">x</a>() * image.width() / width();
621<a name="x1336"></a> int ny = (e-&gt;<a href="qmouseevent.html#y">y</a>()-menubar-&gt;<a href="qmenubar.html#heightForWidth">heightForWidth</a>( <a href="qwidget.html#width">width</a>() )) * image.height() / h;
622 if (nx != x || ny != y ) {
623 x = nx;
624 y = ny;
625 updateStatus();
626 return TRUE;
627 }
628 }
629 return FALSE;
630}
631
632void ImageViewer::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
633{
634 may_be_other = convertEvent(e, clickx, clicky);
635}
636
637<a name="x1350"></a>void ImageViewer::<a href="qwidget.html#mouseReleaseEvent">mouseReleaseEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * )
638{
639 if ( may_be_other )
640 other = this;
641}
642
643/*
644 Record the pixel position of interest.
645*/
646void ImageViewer::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
647{
648 if (convertEvent(e,pickx,picky)) {
649 updateStatus();
650<a name="x1334"></a> if ((e-&gt;<a href="qmouseevent.html#state">state</a>()&amp;LeftButton)) {
651 may_be_other = FALSE;
652 if ( clickx &gt;= 0 &amp;&amp; other) {
653 copyFrom(other);
654 }
655 }
656 }
657}
658
659/*
660 Draws the portion of the scaled pixmap that needs to be updated or prints
661 an error message if no legal pixmap has been loaded.
662*/
663
664void ImageViewer::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> *e )
665{
666 if ( pm.size() != QSize( 0, 0 ) ) { // is an image loaded?
667 <a href="qpainter.html">QPainter</a> painter(this);
668<a name="x1340"></a><a name="x1339"></a> painter.<a href="qpainter.html#setClipRect">setClipRect</a>(e-&gt;<a href="qpaintevent.html#rect">rect</a>());
669<a name="x1337"></a> painter.<a href="qpainter.html#drawPixmap">drawPixmap</a>(0, menubar-&gt;<a href="qmenubar.html#heightForWidth">heightForWidth</a>( <a href="qwidget.html#width">width</a>() ), pmScaled);
670 }
671}
672
673
674/*
675 Explain anything that might be confusing.
676*/
677void <a name="f419"></a>ImageViewer::giveHelp()
678{
679 if (!helpmsg) {
680 <a href="qstring.html">QString</a> helptext =
681 "&lt;b&gt;Usage:&lt;/b&gt; &lt;tt&gt;showimg [-m] &lt;i&gt;filename ...&lt;/i&gt;&lt;/tt&gt;"
682 "&lt;blockquote&gt;"
683 "&lt;tt&gt;-m&lt;/tt&gt; - use &lt;i&gt;ManyColor&lt;/i&gt; color spec"
684 "&lt;/blockquote&gt;"
685 "&lt;p&gt;Supported input formats:"
686 "&lt;blockquote&gt;";
687<a name="x1321"></a> helptext += QImage::<a href="qimage.html#inputFormatList">inputFormatList</a>().join(", ");
688 helptext += "&lt;/blockquote&gt;";
689
690 helpmsg = new <a href="qmessagebox.html">QMessageBox</a>( "Help", helptext,
691 QMessageBox::Information, QMessageBox::Ok, 0, 0, 0, 0, FALSE );
692 }
693<a name="x1317"></a> helpmsg-&gt;<a href="qdialog.html#show">show</a>();
694<a name="x1352"></a> helpmsg-&gt;<a href="qwidget.html#raise">raise</a>();
695}
696
697void <a name="f420"></a>ImageViewer::copyFrom(ImageViewer* s)
698{
699 if ( clickx &gt;= 0 ) {
700 int dx = clickx;
701 int dy = clicky;
702 int sx = s-&gt;clickx;
703 int sy = s-&gt;clicky;
704 int sw = QABS(clickx - pickx)+1;
705 int sh = QABS(clicky - picky)+1;
706 if ( clickx &gt; pickx ) {
707 dx = pickx;
708 sx -= sw-1;
709 }
710 if ( clicky &gt; picky ) {
711 dy = picky;
712 sy -= sh-1;
713 }
714 <a href="qimage.html#bitBlt">bitBlt</a>( &amp;image, dx, dy, &amp;s-&gt;image, sx, sy, sw, sh );
715 reconvertImage();
716 <a href="qwidget.html#repaint">repaint</a>( image.hasAlphaBuffer() );
717 }
718}
719ImageViewer* ImageViewer::other = 0;
720
721void <a name="f421"></a>ImageViewer::hFlip()
722{
723 setImage(image.mirror(TRUE,FALSE));
724}
725
726void <a name="f422"></a>ImageViewer::vFlip()
727{
728 setImage(image.mirror(FALSE,TRUE));
729}
730
731void <a name="f423"></a>ImageViewer::rot180()
732{
733 setImage(image.mirror(TRUE,TRUE));
734}
735
736void <a name="f424"></a>ImageViewer::copy()
737{
738#ifndef QT_NO_MIMECLIPBOARD
739<a name="x1308"></a> QApplication::<a href="qapplication.html#clipboard">clipboard</a>()-&gt;setImage(image); // Less information loss
740#endif
741}
742
743void <a name="f425"></a>ImageViewer::paste()
744{
745#ifndef QT_NO_MIMECLIPBOARD
746 <a href="qimage.html">QImage</a> p = QApplication::<a href="qapplication.html#clipboard">clipboard</a>()-&gt;image();
747<a name="x1322"></a> if ( !p.<a href="qimage.html#isNull">isNull</a>() ) {
748 filename = "pasted";
749 setImage(p);
750 }
751#endif
752}
753
754void <a name="f426"></a>ImageViewer::setImage(const <a href="qimage.html">QImage</a>&amp; newimage)
755{
756 image = newimage;
757
758 pickx = -1;
759 clickx = -1;
760 <a href="qwidget.html#setCaption">setCaption</a>( filename ); // set window caption
761 int w = image.width();
762 int h = image.height();
763 if ( !w )
764 return;
765
766 const int reasonable_width = 128;
767 if ( w &lt; reasonable_width ) {
768 // Integer scale up to something reasonable
769 int multiply = ( reasonable_width + w - 1 ) / w;
770 w *= multiply;
771 h *= multiply;
772 }
773
774 h += menubar-&gt;<a href="qmenubar.html#heightForWidth">heightForWidth</a>(w) + status-&gt;<a href="qwidget.html#height">height</a>();
775 <a href="qwidget.html#resize">resize</a>( w, h ); // we resize to fit image
776
777 reconvertImage();
778 <a href="qwidget.html#repaint">repaint</a>( image.hasAlphaBuffer() );
779
780 updateStatus();
781 setMenuItemFlags();
782}
783
784void <a name="f427"></a>ImageViewer::editText()
785{
786 ImageTextEditor editor(image,this);
787 editor.<a href="qdialog.html#exec">exec</a>();
788}
789
790void <a name="f428"></a>ImageViewer::to1Bit()
791{
792 toBitDepth(1);
793}
794
795void <a name="f429"></a>ImageViewer::to8Bit()
796{
797 toBitDepth(8);
798}
799
800void <a name="f430"></a>ImageViewer::to32Bit()
801{
802 toBitDepth(32);
803}
804
805void <a name="f431"></a>ImageViewer::toBitDepth(int d)
806{
807 image = image.convertDepth(d);
808 reconvertImage();
809 <a href="qwidget.html#repaint">repaint</a>( image.hasAlphaBuffer() );
810}
811</pre>
812
813<p> <hr>
814<p> Main:
815<p> <pre>/****************************************************************************
816** $Id: showimg-example.html 2051 2007-02-21 10:04:20Z chehrlic $
817**
818** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
819**
820** This file is part of an example program for Qt. This example
821** program may be used, distributed and modified without limitation.
822**
823*****************************************************************************/
824
825#include "showimg.h"
826#include "imagefip.h"
827#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
828#include &lt;<a href="qimage-h.html">qimage.h</a>&gt;
829
830int main( int argc, char **argv )
831{
832 if ( argc &gt; 1 &amp;&amp; QString(argv[1]) == "-m" ) {
833<a name="x1361"></a> QApplication::<a href="qapplication.html#setColorSpec">setColorSpec</a>( QApplication::ManyColor );
834 argc--;
835 argv++;
836 }
837 else if ( argc &gt; 1 &amp;&amp; QString(argv[1]) == "-n" ) {
838 QApplication::<a href="qapplication.html#setColorSpec">setColorSpec</a>( QApplication::NormalColor );
839 argc--;
840 argv++;
841 }
842 else {
843 QApplication::<a href="qapplication.html#setColorSpec">setColorSpec</a>( QApplication::CustomColor );
844 }
845
846 <a href="qapplication.html">QApplication</a> a( argc, argv );
847
848 ImageIconProvider iip;
849<a name="x1362"></a> QFileDialog::<a href="qfiledialog.html#setIconProvider">setIconProvider</a>( &amp;iip );
850
851 if ( argc &lt;= 1 ) {
852 // Create a window which looks after its own existence.
853 ImageViewer *w =
854 new ImageViewer(0, "new window", Qt::WDestructiveClose | Qt::WResizeNoErase );
855<a name="x1364"></a> w-&gt;<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Image Viewer");
856 w-&gt;<a href="qwidget.html#show">show</a>();
857 } else {
858 for ( int i=1; i&lt;argc; i++ ) {
859 // Create a window which looks after its own existence.
860 ImageViewer *w =
861 new ImageViewer(0, argv[i], Qt::WDestructiveClose | Qt::WResizeNoErase );
862 w-&gt;<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Image Viewer");
863 w-&gt;loadImage( argv[i] );
864 w-&gt;<a href="qwidget.html#show">show</a>();
865 }
866 }
867
868 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>()));
869
870 return a.<a href="qapplication.html#exec">exec</a>();
871}
872</pre>
873
874<p>See also <a href="examples.html">Examples</a>.
875
876<!-- eof -->
877<p><address><hr><div align=center>
878<table width=100% cellspacing=0 border=0><tr>
879<td>Copyright &copy; 2007
880<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
881<td align=right><div align=right>Qt 3.3.8</div>
882</table></div></address></body>
883</html>
Note: See TracBrowser for help on using the repository browser.