home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / showimg / showimg.cpp < prev    next >
C/C++ Source or Header  |  2001-10-11  |  19KB  |  673 lines

  1. /****************************************************************************
  2. ** $Id:  qt/showimg.cpp   3.0.0   edited Jul 30 17:10 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include "showimg.h"
  12. #include "imagetexteditor.h"
  13. #include <qmenubar.h>
  14. #include <qfiledialog.h>
  15. #include <qmessagebox.h>
  16. #include <qpopupmenu.h>
  17. #include <qlabel.h>
  18. #include <qpainter.h>
  19. #include <qapplication.h>
  20. #include <qclipboard.h>
  21.  
  22.  
  23. /*
  24.   In the constructor, we just pass the standard parameters on to
  25.   QWidget.
  26.  
  27.   The menu uses a single slot to simplify the process of adding
  28.   more items to the options menu.
  29. */
  30. ImageViewer::ImageViewer( QWidget *parent, const char *name, int wFlags )
  31.     : QWidget( parent, name, wFlags ),
  32.       conversion_flags( PreferDither ),
  33.       filename( 0 ),
  34.       helpmsg( 0 )
  35. {
  36.     pickx = -1;
  37.     picky = -1;
  38.     clickx = -1;
  39.     clicky = -1;
  40.     alloc_context = 0;
  41.  
  42.     menubar = new QMenuBar(this);
  43.     menubar->setSeparator( QMenuBar::InWindowsStyle );
  44.  
  45.     QStrList fmt = QImage::outputFormats();
  46.     saveimage = new QPopupMenu( menubar );
  47.     savepixmap = new QPopupMenu( menubar );
  48.     for (const char* f = fmt.first(); f; f = fmt.next()) {
  49.     saveimage->insertItem( f );
  50.     savepixmap->insertItem( f );
  51.     }
  52.     connect( saveimage, SIGNAL(activated(int)), this, SLOT(saveImage(int)) );
  53.     connect( savepixmap, SIGNAL(activated(int)), this, SLOT(savePixmap(int)) );
  54.  
  55.     file = new QPopupMenu( menubar );
  56.     menubar->insertItem( "&File", file );
  57.     file->insertItem( "&New window", this,  SLOT(newWindow()), CTRL+Key_N );
  58.     file->insertItem( "&Open...", this,  SLOT(openFile()), CTRL+Key_O );
  59.     si = file->insertItem( "Save image", saveimage );
  60.     sp = file->insertItem( "Save pixmap", savepixmap );
  61.     file->insertSeparator();
  62.     file->insertItem( "E&xit", qApp,  SLOT(quit()), CTRL+Key_Q );
  63.  
  64.     edit =  new QPopupMenu( menubar );
  65.     menubar->insertItem( "&Edit", edit );
  66.     edit->insertItem("&Copy", this, SLOT(copy()), CTRL+Key_C);
  67.     edit->insertItem("&Paste", this, SLOT(paste()), CTRL+Key_V);
  68.     edit->insertSeparator();
  69.     edit->insertItem("&Horizontal flip", this, SLOT(hFlip()), ALT+Key_H);
  70.     edit->insertItem("&Vertical flip", this, SLOT(vFlip()), ALT+Key_V);
  71.     edit->insertItem("&Rotate 180", this, SLOT(rot180()), ALT+Key_R);
  72.     edit->insertSeparator();
  73.     edit->insertItem("&Text...", this, SLOT(editText()));
  74.     edit->insertSeparator();
  75.     t1 = edit->insertItem( "Convert to &1 bit", this, SLOT(to1Bit()) );
  76.     t8 = edit->insertItem( "Convert to &8 bit", this, SLOT(to8Bit()) );
  77.     t32 = edit->insertItem( "Convert to &32 bit", this, SLOT(to32Bit()) );
  78.  
  79.     options =  new QPopupMenu( menubar );
  80.     menubar->insertItem( "&Options", options );
  81.     ac = options->insertItem( "AutoColor" );
  82.     co = options->insertItem( "ColorOnly" );
  83.     mo = options->insertItem( "MonoOnly" );
  84.     options->insertSeparator();
  85.     fd = options->insertItem( "DiffuseDither" );
  86.     bd = options->insertItem( "OrderedDither" );
  87.     td = options->insertItem( "ThresholdDither" );
  88.     options->insertSeparator();
  89.     ta = options->insertItem( "ThresholdAlphaDither" );
  90.     ba = options->insertItem( "OrderedAlphaDither" );
  91.     fa = options->insertItem( "DiffuseAlphaDither" );
  92.     options->insertSeparator();
  93.     ad = options->insertItem( "PreferDither" );
  94.     dd = options->insertItem( "AvoidDither" );
  95.     options->insertSeparator();
  96.     ss = options->insertItem( "Smooth scaling" );
  97.     cc = options->insertItem( "Use color context" );
  98.     if ( QApplication::colorSpec() == QApplication::ManyColor )
  99.     options->setItemEnabled( cc, FALSE );
  100.     options->setCheckable( TRUE );
  101.     setMenuItemFlags();
  102.  
  103.     menubar->insertSeparator();
  104.  
  105.     QPopupMenu* help = new QPopupMenu( menubar );
  106.     menubar->insertItem( "&Help", help );
  107.     help->insertItem( "Help!", this, SLOT(giveHelp()), CTRL+Key_H );
  108.  
  109.     connect( options, SIGNAL(activated(int)), this, SLOT(doOption(int)) );
  110.  
  111.     status = new QLabel(this);
  112.     status->setFrameStyle( QFrame::WinPanel | QFrame::Sunken );
  113.     status->setFixedHeight( fontMetrics().height() + 4 );
  114.  
  115.     setMouseTracking( TRUE );
  116. }
  117.  
  118. ImageViewer::~ImageViewer()
  119. {
  120.     if ( alloc_context )
  121.     QColor::destroyAllocContext( alloc_context );
  122.     if ( other == this )
  123.     other = 0;
  124. }
  125.  
  126. /*
  127.   This function modifies the conversion_flags when an options menu item
  128.   is selected, then ensures all menu items are up to date, and reconverts
  129.   the image if possibly necessary.
  130. */
  131. void ImageViewer::doOption(int item)
  132. {
  133.     if ( item == ss || item == cc ) {
  134.     // Toggle
  135.     bool newbool = !options->isItemChecked(item);
  136.     options->setItemChecked(item, newbool);
  137.     // And reconvert...
  138.     reconvertImage();
  139.     repaint(image.hasAlphaBuffer());    // show image in widget
  140.     return;
  141.     }
  142.  
  143.     if ( options->isItemChecked( item ) ) return; // They are all radio buttons
  144.  
  145.     int ocf = conversion_flags;
  146.  
  147.     if ( item == ac ) {
  148.     conversion_flags = conversion_flags & ~ColorMode_Mask | AutoColor;
  149.     } else if ( item == co ) {
  150.     conversion_flags = conversion_flags & ~ColorMode_Mask | ColorOnly;
  151.     } else if ( item == mo ) {
  152.     conversion_flags = conversion_flags & ~ColorMode_Mask | MonoOnly;
  153.     } else if ( item == fd ) {
  154.     conversion_flags = conversion_flags & ~Dither_Mask | DiffuseDither;
  155.     } else if ( item == bd ) {
  156.     conversion_flags = conversion_flags & ~Dither_Mask | OrderedDither;
  157.     } else if ( item == td ) {
  158.     conversion_flags = conversion_flags & ~Dither_Mask | ThresholdDither;
  159.     } else if ( item == ta ) {
  160.     conversion_flags = conversion_flags & ~AlphaDither_Mask | ThresholdAlphaDither;
  161.     } else if ( item == fa ) {
  162.     conversion_flags = conversion_flags & ~AlphaDither_Mask | DiffuseAlphaDither;
  163.     } else if ( item == ba ) {
  164.     conversion_flags = conversion_flags & ~AlphaDither_Mask | OrderedAlphaDither;
  165.     } else if ( item == ad ) {
  166.     conversion_flags = conversion_flags & ~DitherMode_Mask | PreferDither;
  167.     } else if ( item == dd ) {
  168.     conversion_flags = conversion_flags & ~DitherMode_Mask | AvoidDither;
  169.     }
  170.  
  171.     if ( ocf != conversion_flags ) {
  172.     setMenuItemFlags();
  173.     // And reconvert...
  174.     reconvertImage();
  175.     repaint(image.hasAlphaBuffer());    // show image in widget
  176.     }
  177. }
  178.  
  179. /*
  180.   Set the options menu to reflect the conversion_flags value.
  181. */
  182. void ImageViewer::setMenuItemFlags()
  183. {
  184.     // File
  185.     bool valid_image = pm.size() != QSize( 0, 0 );
  186.     file->setItemEnabled( si, valid_image );
  187.     file->setItemEnabled( sp, valid_image );
  188.  
  189.     // Edit
  190.     edit->setItemEnabled( t1, image.depth() != 1 );
  191.     edit->setItemEnabled( t8, image.depth() != 8 );
  192.     edit->setItemEnabled( t32, image.depth() != 32 );
  193.  
  194.     // Options
  195.     bool may_need_color_dithering =
  196.         !valid_image
  197.     || image.depth() == 32 && QPixmap::defaultDepth() < 24;
  198.     bool may_need_dithering = may_need_color_dithering
  199.      || image.depth() > 1 && options->isItemChecked(mo)
  200.      || image.depth() > 1 && QPixmap::defaultDepth() == 1;
  201.     bool has_alpha_mask = !valid_image || image.hasAlphaBuffer();
  202.  
  203.     options->setItemEnabled( fd, may_need_dithering );
  204.     options->setItemEnabled( bd, may_need_dithering );
  205.     options->setItemEnabled( td, may_need_dithering );
  206.  
  207.     options->setItemEnabled( ta, has_alpha_mask );
  208.     options->setItemEnabled( fa, has_alpha_mask );
  209.     options->setItemEnabled( ba, has_alpha_mask );
  210.  
  211.     options->setItemEnabled( ad, may_need_color_dithering );
  212.     options->setItemEnabled( dd, may_need_color_dithering );
  213.  
  214.     options->setItemChecked( ac, (conversion_flags & ColorMode_Mask) == AutoColor );
  215.     options->setItemChecked( co, (conversion_flags & ColorMode_Mask) == ColorOnly );
  216.     options->setItemChecked( mo, (conversion_flags & ColorMode_Mask) == MonoOnly );
  217.     options->setItemChecked( fd, (conversion_flags & Dither_Mask) == DiffuseDither );
  218.     options->setItemChecked( bd, (conversion_flags & Dither_Mask) == OrderedDither );
  219.     options->setItemChecked( td, (conversion_flags & Dither_Mask) == ThresholdDither );
  220.     options->setItemChecked( ta, (conversion_flags & AlphaDither_Mask) == ThresholdAlphaDither );
  221.     options->setItemChecked( fa, (conversion_flags & AlphaDither_Mask) == DiffuseAlphaDither );
  222.     options->setItemChecked( ba, (conversion_flags & AlphaDither_Mask) == OrderedAlphaDither );
  223.     options->setItemChecked( ad, (conversion_flags & DitherMode_Mask) == PreferDither );
  224.     options->setItemChecked( dd, (conversion_flags & DitherMode_Mask) == AvoidDither );
  225. }
  226.  
  227. void ImageViewer::updateStatus()
  228. {
  229.     if ( pm.size() == QSize( 0, 0 ) ) {
  230.     if ( filename )
  231.         status->setText("Could not load image");
  232.     else
  233.         status->setText("No image - select Open from File menu.");
  234.     } else {
  235.     QString message, moremsg;
  236.     message.sprintf("%dx%d", image.width(), image.height());
  237.     if ( pm.size() != pmScaled.size() ) {
  238.         moremsg.sprintf(" [%dx%d]", pmScaled.width(),
  239.         pmScaled.height());
  240.         message += moremsg;
  241.     }
  242.     moremsg.sprintf(", %d bits ", image.depth());
  243.     message += moremsg;
  244.     if (image.valid(pickx,picky)) {
  245.         moremsg.sprintf("(%d,%d)=#%0*x ",
  246.               pickx, picky,
  247.               image.hasAlphaBuffer() ? 8 : 6,
  248.               image.pixel(pickx,picky));
  249.         message += moremsg;
  250.     }
  251.     if ( image.numColors() > 0 ) {
  252.         if (image.valid(pickx,picky)) {
  253.         moremsg.sprintf(", %d/%d colors", image.pixelIndex(pickx,picky),
  254.             image.numColors());
  255.         } else {
  256.         moremsg.sprintf(", %d colors", image.numColors());
  257.         }
  258.         message += moremsg;
  259.     }
  260.     if ( image.hasAlphaBuffer() ) {
  261.         if ( image.depth() == 8 ) {
  262.         int i;
  263.         bool alpha[256];
  264.         int nalpha=0;
  265.  
  266.         for (i=0; i<256; i++)
  267.             alpha[i] = FALSE;
  268.  
  269.         for (i=0; i<image.numColors(); i++) {
  270.             int alevel = image.color(i) >> 24;
  271.             if (!alpha[alevel]) {
  272.             alpha[alevel] = TRUE;
  273.             nalpha++;
  274.             }
  275.         }
  276.         moremsg.sprintf(", %d alpha levels", nalpha);
  277.         } else {
  278.         // Too many pixels to bother counting.
  279.         moremsg = ", 8-bit alpha channel";
  280.         }
  281.         message += moremsg;
  282.     }
  283.     status->setText(message);
  284.     }
  285. }
  286.  
  287. /*
  288.   This function saves the image.
  289. */
  290. void ImageViewer::saveImage( int item )
  291. {
  292.     const char* fmt = saveimage->text(item);
  293.     QString savefilename = QFileDialog::getSaveFileName(QString::null, QString::null,
  294.                     this, filename);
  295.     if ( !savefilename.isEmpty() )
  296.     if ( !image.save( savefilename, fmt ) )
  297.         QMessageBox::warning( this, "Save failed", "Error saving file" );
  298. }
  299.  
  300. /*
  301.   This function saves the converted image.
  302. */
  303. void ImageViewer::savePixmap( int item )
  304. {
  305.     const char* fmt = savepixmap->text(item);
  306.     QString savefilename = QFileDialog::getSaveFileName(QString::null,
  307.                     QString::null, this, filename);
  308.     if ( !savefilename.isEmpty() )
  309.     if ( !pmScaled.save( savefilename, fmt ) )
  310.         QMessageBox::warning( this, "Save failed", "Error saving file" );
  311. }
  312.  
  313.  
  314. void ImageViewer::newWindow()
  315. {
  316.     ImageViewer* that = new ImageViewer(0, "new window", WDestructiveClose);
  317.     that->options->setItemChecked( that->cc, useColorContext() );
  318.     that->show();
  319. }
  320.  
  321. /*
  322.   This function is the slot for processing the Open menu item.
  323. */
  324. void ImageViewer::openFile()
  325. {
  326.     QString newfilename = QFileDialog::getOpenFileName();
  327.     if ( !newfilename.isEmpty() ) {
  328.     loadImage( newfilename ) ;
  329.     repaint();            // show image in widget
  330.     }
  331. }
  332.  
  333. /*
  334.   This function loads an image from a file and resizes the widget to
  335.   exactly fit the image size. If the file was not found or the image
  336.   format was unknown it will resize the widget to fit the errorText
  337.   message (see above) displayed in the current font.
  338.  
  339.   Returns TRUE if the image was successfully loaded.
  340. */
  341.  
  342. bool ImageViewer::loadImage( const char *fileName )
  343. {
  344.     filename = fileName;
  345.     bool ok = FALSE;
  346.     if ( filename ) {
  347.     QApplication::setOverrideCursor( waitCursor ); // this might take time
  348.     ok = image.load(filename, 0);
  349.     pickx = -1;
  350.     clickx = -1;
  351.     if ( ok )
  352.         ok = reconvertImage();
  353.     if ( ok ) {
  354.         setCaption( filename );            // set window caption
  355.         int w = pm.width();
  356.         int h = pm.height();
  357.  
  358.         const int reasonable_width = 128;
  359.         if ( w < reasonable_width ) {
  360.         // Integer scale up to something reasonable
  361.         int multiply = ( reasonable_width + w - 1 ) / w;
  362.         w *= multiply;
  363.         h *= multiply;
  364.         }
  365.  
  366.         h += menubar->heightForWidth(w) + status->height();
  367.         resize( w, h );                // we resize to fit image
  368.     } else {
  369.         pm.resize(0,0);                // couldn't load image
  370.         update();
  371.     }
  372.     QApplication::restoreOverrideCursor();    // restore original cursor
  373.     }
  374.     updateStatus();
  375.     setMenuItemFlags();
  376.     return ok;
  377. }
  378.  
  379. bool ImageViewer::reconvertImage()
  380. {
  381.     bool success = FALSE;
  382.  
  383.     if ( image.isNull() ) return FALSE;
  384.  
  385.     if ( alloc_context ) {
  386.     QColor::destroyAllocContext( alloc_context );
  387.     alloc_context = 0;
  388.     }
  389.     if ( useColorContext() ) {
  390.     alloc_context = QColor::enterAllocContext();
  391.     // Clear the image to hide flickering palette
  392.     QPainter painter(this);
  393.     painter.eraseRect(0, menubar->heightForWidth( width() ), width(), height());
  394.     }
  395.  
  396.     QApplication::setOverrideCursor( waitCursor ); // this might take time
  397.     if ( pm.convertFromImage(image, conversion_flags) )
  398.     {
  399.     pmScaled = QPixmap();
  400.     scale();
  401.     resize( width(), height() );
  402.     success = TRUE;                // load successful
  403.     } else {
  404.     pm.resize(0,0);                // couldn't load image
  405.     }
  406.     updateStatus();
  407.     setMenuItemFlags();
  408.     QApplication::restoreOverrideCursor();    // restore original cursor
  409.  
  410.     if ( useColorContext() )
  411.     QColor::leaveAllocContext();
  412.  
  413.     return success;                // TRUE if loaded OK
  414. }
  415.  
  416. bool ImageViewer::smooth() const
  417. {
  418.     return options->isItemChecked(ss);
  419. }
  420.  
  421. bool ImageViewer::useColorContext() const
  422. {
  423.     return options->isItemChecked(cc);
  424. }
  425.  
  426. /*
  427.   This functions scales the pixmap in the member variable "pm" to fit the
  428.   widget size and  puts the resulting pixmap in the member variable "pmScaled".
  429. */
  430.  
  431. void ImageViewer::scale()
  432. {
  433.     int h = height() - menubar->heightForWidth( width() ) - status->height();
  434.  
  435.     if ( image.isNull() ) return;
  436.  
  437.     QApplication::setOverrideCursor( waitCursor ); // this might take time
  438.     if ( width() == pm.width() && h == pm.height() )
  439.     {                        // no need to scale if widget
  440.     pmScaled = pm;                // size equals pixmap size
  441.     } else {
  442.     if (smooth()) {
  443.         pmScaled.convertFromImage(image.smoothScale(width(), h),
  444.         conversion_flags);
  445.     } else {
  446.         QWMatrix m;                // transformation matrix
  447.         m.scale(((double)width())/pm.width(),// define scale factors
  448.             ((double)h)/pm.height());
  449.         pmScaled = pm.xForm( m );        // create scaled pixmap
  450.     }
  451.     }
  452.     QApplication::restoreOverrideCursor();    // restore original cursor
  453. }
  454.  
  455. /*
  456.   The resize event handler, if a valid pixmap was loaded it will call
  457.   scale() to fit the pixmap to the new widget size.
  458. */
  459.  
  460. void ImageViewer::resizeEvent( QResizeEvent * )
  461. {
  462.     status->setGeometry(0, height() - status->height(),
  463.             width(), status->height());
  464.  
  465.     if ( pm.size() == QSize( 0, 0 ) )        // we couldn't load the image
  466.     return;
  467.  
  468.     int h = height() - menubar->heightForWidth( width() ) - status->height();
  469.     if ( width() != pmScaled.width() || h != pmScaled.height())
  470.     {                        // if new size,
  471.     scale();                // scale pmScaled to window
  472.     updateStatus();
  473.     }
  474.     if ( image.hasAlphaBuffer() )
  475.     erase();
  476. }
  477.  
  478. bool ImageViewer::convertEvent( QMouseEvent* e, int& x, int& y)
  479. {
  480.     if ( pm.size() != QSize( 0, 0 ) ) {
  481.     int h = height() - menubar->heightForWidth( width() ) - status->height();
  482.     int nx = e->x() * image.width() / width();
  483.     int ny = (e->y()-menubar->heightForWidth( width() )) * image.height() / h;
  484.     if (nx != x || ny != y ) {
  485.         x = nx;
  486.         y = ny;
  487.         updateStatus();
  488.         return TRUE;
  489.     }
  490.     }
  491.     return FALSE;
  492. }
  493.  
  494. void ImageViewer::mousePressEvent( QMouseEvent *e )
  495. {
  496.     may_be_other = convertEvent(e, clickx, clicky);
  497. }
  498.  
  499. void ImageViewer::mouseReleaseEvent( QMouseEvent * )
  500. {
  501.     if ( may_be_other )
  502.     other = this;
  503. }
  504.  
  505. /*
  506.   Record the pixel position of interest.
  507. */
  508. void ImageViewer::mouseMoveEvent( QMouseEvent *e )
  509. {
  510.     if (convertEvent(e,pickx,picky)) {
  511.     updateStatus();
  512.     if ((e->state()&LeftButton)) {
  513.         may_be_other = FALSE;
  514.         if ( clickx >= 0 && other) {
  515.         copyFrom(other);
  516.         }
  517.     }
  518.     }
  519. }
  520.  
  521. /*
  522.   Draws the portion of the scaled pixmap that needs to be updated or prints
  523.   an error message if no legal pixmap has been loaded.
  524. */
  525.  
  526. void ImageViewer::paintEvent( QPaintEvent *e )
  527. {
  528.     if ( pm.size() != QSize( 0, 0 ) ) {        // is an image loaded?
  529.     QPainter painter(this);
  530.     painter.setClipRect(e->rect());
  531.     painter.drawPixmap(0, menubar->heightForWidth( width() ), pmScaled);
  532.     }
  533. }
  534.  
  535.  
  536. /*
  537.   Explain anything that might be confusing.
  538. */
  539. void ImageViewer::giveHelp()
  540. {
  541.     if (!helpmsg) {
  542.     QString helptext =
  543.         "<b>Usage:</b> <tt>showimg [-m] <i>filename ...</i></tt>"
  544.         "<blockquote>"
  545.         "<tt>-m</tt> - use <i>ManyColor</i> color spec"
  546.         "</blockquote>"
  547.         "<p>Supported input formats:"
  548.         "<blockquote>";
  549.     helptext += QImage::inputFormatList().join(", ");
  550.     helptext += "</blockquote>";
  551.  
  552.     helpmsg = new QMessageBox( "Help", helptext,
  553.         QMessageBox::Information, QMessageBox::Ok, 0, 0, 0, 0, FALSE );
  554.     }
  555.     helpmsg->show();
  556.     helpmsg->raise();
  557. }
  558.  
  559. void ImageViewer::copyFrom(ImageViewer* s)
  560. {
  561.     if ( clickx >= 0 ) {
  562.     int dx = clickx;
  563.     int dy = clicky;
  564.     int sx = s->clickx;
  565.     int sy = s->clicky;
  566.     int sw = QABS(clickx - pickx)+1;
  567.     int sh = QABS(clicky - picky)+1;
  568.     if ( clickx > pickx ) {
  569.         dx = pickx;
  570.         sx -= sw-1;
  571.     }
  572.     if ( clicky > picky ) {
  573.         dy = picky;
  574.         sy -= sh-1;
  575.     }
  576.     bitBlt( &image, dx, dy, &s->image, sx, sy, sw, sh );
  577.     reconvertImage();
  578.     repaint( image.hasAlphaBuffer() );
  579.     }
  580. }
  581. ImageViewer* ImageViewer::other = 0;
  582.  
  583. void ImageViewer::hFlip()
  584. {
  585.     setImage(image.mirror(TRUE,FALSE));
  586. }
  587.  
  588. void ImageViewer::vFlip()
  589. {
  590.     setImage(image.mirror(FALSE,TRUE));
  591. }
  592.  
  593. void ImageViewer::rot180()
  594. {
  595.     setImage(image.mirror(TRUE,TRUE));
  596. }
  597.  
  598. void ImageViewer::copy()
  599. {
  600. #ifndef QT_NO_MIMECLIPBOARD
  601.     QApplication::clipboard()->setImage(image); // Less information loss
  602. #endif
  603. }
  604.  
  605. void ImageViewer::paste()
  606. {
  607. #ifndef QT_NO_MIMECLIPBOARD
  608.     QImage p = QApplication::clipboard()->image();
  609.     if ( !p.isNull() ) {
  610.     filename = "pasted";
  611.     setImage(p);
  612.     }
  613. #endif
  614. }
  615.  
  616. void ImageViewer::setImage(const QImage& newimage)
  617. {
  618.     image = newimage;
  619.  
  620.     pickx = -1;
  621.     clickx = -1;
  622.     setCaption( filename );            // set window caption
  623.     int w = image.width();
  624.     int h = image.height();
  625.     if ( !w )
  626.     return;
  627.  
  628.     const int reasonable_width = 128;
  629.     if ( w < reasonable_width ) {
  630.     // Integer scale up to something reasonable
  631.     int multiply = ( reasonable_width + w - 1 ) / w;
  632.     w *= multiply;
  633.     h *= multiply;
  634.     }
  635.  
  636.     h += menubar->heightForWidth(w) + status->height();
  637.     resize( w, h );                // we resize to fit image
  638.  
  639.     reconvertImage();
  640.     repaint( image.hasAlphaBuffer() );
  641.  
  642.     updateStatus();
  643.     setMenuItemFlags();
  644. }
  645.  
  646. void ImageViewer::editText()
  647. {
  648.     ImageTextEditor editor(image,this);
  649.     editor.exec();
  650. }
  651.  
  652. void ImageViewer::to1Bit()
  653. {
  654.     toBitDepth(1);
  655. }
  656.  
  657. void ImageViewer::to8Bit()
  658. {
  659.     toBitDepth(8);
  660. }
  661.  
  662. void ImageViewer::to32Bit()
  663. {
  664.     toBitDepth(32);
  665. }
  666.  
  667. void ImageViewer::toBitDepth(int d)
  668. {
  669.     image = image.convertDepth(d);
  670.     reconvertImage();
  671.     repaint( image.hasAlphaBuffer() );
  672. }
  673.