📄 showimg-main-cpp.html
字号:
} else { <a href="qwmatrix.html">QWMatrix</a> m; // transformation matrix m.<a href="qwmatrix.html#41a356">scale</a>(((double)width())/pm.width(),// define scale factors ((double)h)/pm.height()); pmScaled = pm.xForm( m ); // create scaled pixmap } } <a href="qapplication.html#655095">QApplication::restoreOverrideCursor</a>(); // restore original cursor}/* The resize event handler, if a valid pixmap was loaded it will call <a href=#381>scale</a>() to fit the pixmap to the new widget size.*/void <a name="382"></a>ImageViewer::resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * ){ status->setGeometry(0, <a href="qwidget.html#e3c588">height</a>() - status->height(), <a href="qwidget.html#2edab1">width</a>(), status->height()); if ( pm.size() == QSize( 0, 0 ) ) // we couldn't load the image return; int h = height() - menubar->heightForWidth( <a href="qwidget.html#2edab1">width</a>() ) - status->height(); if ( width() != pmScaled.width() || h != pmScaled.height()) { // if new size, <a href=#381>scale</a>(); // scale pmScaled to window <a href=#371>updateStatus</a>(); } if ( image.hasAlphaBuffer() ) <a href="qwidget.html#10cf79">erase</a>();}bool <a name="383"></a>ImageViewer::convertEvent( <a href="qmouseevent.html">QMouseEvent</a>* e, int& x, int& y){ if ( pm.size() != QSize( 0, 0 ) ) { int h = height() - menubar->heightForWidth( <a href="qwidget.html#2edab1">width</a>() ) - status->height(); int nx = e->x() * image.width() / width(); int ny = (e->y()-menubar->heightForWidth( <a href="qwidget.html#2edab1">width</a>() )) * image.height() / h; if (nx != x || ny != y ) { x = nx; y = ny; <a href=#371>updateStatus</a>(); return TRUE; } } return FALSE;}void <a name="384"></a>ImageViewer::mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ){ may_be_other = convertEvent(e, clickx, clicky);}void <a name="385"></a>ImageViewer::mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * ){ if ( may_be_other ) other = this;}/* Record the pixel position of interest.*/void <a name="386"></a>ImageViewer::mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ){ if (convertEvent(e,pickx,picky)) { <a href=#371>updateStatus</a>(); if ((e->state()&LeftButton)) { may_be_other = FALSE; if ( clickx >= 0 && other) { <a href=#389>copyFrom</a>(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 <a name="387"></a>ImageViewer::paintEvent( <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); painter.<a href="qpainter.html#898839">setClipRect</a>(e-><a href="qpaintevent.html#2d6e18">rect</a>()); painter.<a href="qpainter.html#c64b89">drawPixmap</a>(0, menubar->heightForWidth( <a href="qwidget.html#2edab1">width</a>() ), pmScaled); }}/* Explain anything that might be confusing.*/void <a name="388"></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>"; helptext += QImage::inputFormatList().join(", "); helptext += "</blockquote>"; helpmsg = new <a href="qmessagebox.html">QMessageBox</a>( "Help", helptext, QMessageBox::Information, QMessageBox::Ok, 0, 0, 0, 0, FALSE ); } helpmsg->show(); helpmsg->raise();}void <a name="389"></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="qpaintdevice.html#35ae2e">bitBlt</a>( &image, dx, dy, &s->image, sx, sy, sw, sh ); <a href=#377>reconvertImage</a>(); <a href="qwidget.html#7569b1">repaint</a>( image.hasAlphaBuffer() ); }}ImageViewer* <a name="390"></a>ImageViewer::other = 0;void <a name="391"></a>ImageViewer::hFlip(){ <a href=#396>setImage</a>(image.mirror(TRUE,FALSE));}void <a name="392"></a>ImageViewer::vFlip(){ <a href=#396>setImage</a>(image.mirror(FALSE,TRUE));}void <a name="393"></a>ImageViewer::rot180(){ <a href=#396>setImage</a>(image.mirror(TRUE,TRUE));}void <a name="394"></a>ImageViewer::copy(){#ifndef QT_NO_MIMECLIPBOARD <a href="qapplication.html#1c95b5">QApplication::clipboard</a>()->setImage(image); // Less information loss#endif}void <a name="395"></a>ImageViewer::paste(){#ifndef QT_NO_MIMECLIPBOARD <a href="qimage.html">QImage</a> p = QApplication::clipboard()->image(); if ( !image.isNull() ) { filename = "pasted"; <a href=#396>setImage</a>(p); }#endif}void <a name="396"></a>ImageViewer::setImage(const QImage& newimage){ image = newimage; pickx = -1; clickx = -1; <a href="qwidget.html#d6a291">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->heightForWidth(w) + status->height(); <a href="qwidget.html#8fcbbe">resize</a>( w, h ); // we resize to fit image <a href=#377>reconvertImage</a>(); <a href="qwidget.html#7569b1">repaint</a>( image.hasAlphaBuffer() ); <a href=#371>updateStatus</a>(); <a href=#370>setMenuItemFlags</a>();}void <a name="397"></a>ImageViewer::editText(){ ImageTextEditor editor(image,this); editor.exec();}void <a name="398"></a>ImageViewer::to1Bit(){ <a href=#401>toBitDepth</a>(1);}void <a name="399"></a>ImageViewer::to8Bit(){ <a href=#401>toBitDepth</a>(8);}void <a name="400"></a>ImageViewer::to32Bit(){ <a href=#401>toBitDepth</a>(32);}void <a name="401"></a>ImageViewer::toBitDepth(int d){ image = image.convertDepth(d); <a href=#377>reconvertImage</a>(); <a href="qwidget.html#7569b1">repaint</a>( image.hasAlphaBuffer() );}</pre> <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/showimg/main.cpp 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. 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 name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>#include <<a name="qimage.h"></a><a href="qimage-h.html">qimage.h</a>>int main( int argc, char **argv ){ if ( argc > 1 && <a name="QString"></a><a href="qstring.html">QString</a>(argv[1]) == "-m" ) { <a name="QApplication::setColorSpec"></a><a href="qapplication.html#1ee2d1">QApplication::setColorSpec</a>( QApplication::ManyColor ); argc--; argv++; } else if ( argc > 1 && <a href="qstring.html">QString</a>(argv[1]) == "-n" ) { <a href="qapplication.html#1ee2d1">QApplication::setColorSpec</a>( QApplication::NormalColor ); argc--; argv++; } else { <a href="qapplication.html#1ee2d1">QApplication::setColorSpec</a>( QApplication::CustomColor ); } <a name="QApplication::setFont"></a><a href="qapplication.html#3d926a">QApplication::setFont</a>( <a name="QFont"></a><a href="qfont.html">QFont</a>("Helvetica", 12) ); <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv ); <a name="QFileDialog::setIconProvider"></a><a href="qfiledialog.html#8d2ecf">QFileDialog::setIconProvider</a>(new ImageIconProvider); if ( argc <= 1 ) { // Create a window which looks after its own existence. ImageViewer *w = new ImageViewer(0, "new window", Qt::WDestructiveClose | Qt::WResizeNoErase ); w-><a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - Image Viewer"); w-><a name="show"></a><a href="qwidget.html#200ee5">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#d6a291">setCaption</a>("Qt Example - Image Viewer"); w->loadImage( argv[i] ); w-><a href="qwidget.html#200ee5">show</a>(); } } <a name="QObject::connect"></a><a href="qobject.html#7f8e37">QObject::connect</a>(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit())); return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -