📄 showimg.cpp
字号:
/****************************************************************************** $Id: qt/showimg.cpp 3.3.6 edited Aug 31 2005 $**** Copyright (C) 1992-2005 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 <qlabel.h>#include <qpainter.h>#include <qapplication.h>#include <qmessagebox.h>ImageViewer::ImageViewer( QWidget *parent, const char *name, int wFlags ) : QWidget( parent, name, wFlags ), conversion_flags( PreferDither ){ pwidget = (QSplitter*)parentWidget(); alloc_context = 0; isFullScreen = false; screenToggled = false; newImage = true; status = new QLabel(this); status->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); status->setFixedHeight( fontMetrics().height() + 4 ); status->setBackgroundColor( white ); setMouseTracking( TRUE );}ImageViewer::~ImageViewer(){ if ( alloc_context ) QColor::destroyAllocContext( alloc_context ); if ( other == this ) other = 0;}/* 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 ImageViewer::loadImage( const QString& fileName ){ newImage = 1; screenToggled = false; filename = fileName; bool ok = FALSE; if ( !filename.isEmpty() ) { QApplication::setOverrideCursor( waitCursor ); // this might take time ok = image.load(filename, 0); if ( ok ) { ok = reconvertImage(); } if ( ok ) { scale(); setCaption( filename ); // set window caption } else { pm.resize(0,0); // couldn't load image update(); } QApplication::restoreOverrideCursor(); // restore original cursor } return ok;}void ImageViewer::updateStatus(){ QString message, moremsg; if ( pm.size() != QSize( 0, 0 ) ) { message.sprintf("%dx%d", image.width(), image.height()); if ( screenToggled && !isFullScreen ) { if ( pm.size() != pmScaledBackup.size() ) { moremsg.sprintf(" [%dx%d]", pmScaledBackup.width(), pmScaledBackup.height()); message += moremsg; } } else if ( !isFullScreen && pm.size() != pmScaled.size() ) { moremsg.sprintf(" [%dx%d]", pmScaled.width(), pmScaled.height()); message += moremsg; } else if ( isFullScreen ) { moremsg.sprintf(" [%dx%d]", pmFullScreen.width(), pmFullScreen.height()); message += moremsg; } moremsg.sprintf(", %d bits ", image.depth()); message += moremsg; } status->setText( message );} bool ImageViewer::reconvertImage(){ bool success = FALSE; if ( image.isNull() ) return FALSE; if ( alloc_context ) { QColor::destroyAllocContext( alloc_context ); alloc_context = 0; } QApplication::setOverrideCursor( waitCursor ); // this might take time if ( pm.convertFromImage(image, conversion_flags) ) { success = TRUE; // load successful } else { pm.resize(0,0); } // couldn't load image QApplication::restoreOverrideCursor(); // restore original cursor return success; // TRUE if loaded OK}/* 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 ImageViewer::scale(){ int h = height() - status->height(); if ( image.isNull() ) return; QApplication::setOverrideCursor( waitCursor ); // this might take time if ( width() >= pm.width() && h >= pm.height() ) { // no need to scale if widget pmScaled = pm; // size equals pixmap size if ( isFullScreen ) pmFullScreen = pmScaled; else pmScaledBackup = pmScaled; } else { QWMatrix m; // transformation matrix if ( pm.width() == pm.height() ) { if ( width() >= h ) { m.scale( ( (double)h ) / pm.width(),// define scale factors ( (double)h ) / pm.width() ); } else { m.scale( ( (double)width() ) / pm.width(),// define scale factors ( (double)width() ) / pm.width() ); } } if ( pm.width() > pm.height() ) { if ( ( (double)pm.width() / width() ) >= ( (double)pm.height() / h ) ) { m.scale( ( (double)width() ) / pm.width(),// define scale factors ( (double)width() ) / pm.width() ); } else { m.scale( ( (double)h ) / pm.height(),// define scale factors ( (double)h ) / pm.height() ); } } if ( pm.width() < pm.height() ) { if ( ( (double)pm.width() / width() ) >= ( (double)pm.height() / h ) ) { m.scale( ( (double)width() ) / pm.width(),// define scale factors ( (double)width() ) / pm.width() ); } else { m.scale( ( (double)h ) / pm.height(),// define scale factors ( (double)h ) / pm.height() ); } } pmScaled = pm.xForm( m ); // create scaled pixmap if ( isFullScreen ) pmFullScreen = pmScaled; else pmScaledBackup = pmScaled; } screenToggled = false; updateStatus(); QApplication::restoreOverrideCursor(); // 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.*/void ImageViewer::resizeEvent( QResizeEvent * ){ int h = height() - status->height(); status->setGeometry(0, h, width(), status->height()); if ( pm.size() == QSize( 0, 0 ) ) // we couldn't load the image return; if ( !screenToggled || newImage ) scale();// scale pmScaled to window newImage = false;}/* 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::paintEvent( QPaintEvent *e ){ int h = height() - status->height(); if ( pm.size() != QSize( 0, 0 ) ) { // is an image loaded? QPainter painter(this); painter.setClipRect(e->rect()); if ( isFullScreen ) { updateStatus(); painter.drawPixmap( ( width() - pmFullScreen.width() ) / 2, ( h - pmFullScreen.height() ) / 2, pmFullScreen ); } else if ( !isFullScreen && screenToggled ) { updateStatus(); painter.drawPixmap( ( width() - pmScaledBackup.width() ) / 2, ( h - pmScaledBackup.height() ) / 2, pmScaledBackup ); } else { updateStatus(); painter.drawPixmap( ( width() - pmScaled.width() ) / 2, ( h - pmScaled.height() ) / 2, pmScaled ); } }}ImageViewer* ImageViewer::other = 0;void ImageViewer::mouseReleaseEvent( QMouseEvent * ){ emit clicked();} void ImageViewer::fullScreen(){ isFullScreen = true; oriPoint = pos(); reparent( 0, QPoint( 0, 0 ) ); showFullScreen();}void ImageViewer::toggleFullScreen(){ screenToggled = true; if ( isFullScreen ) normalView(); else fullScreen();}void ImageViewer::normalView(){ isFullScreen = false; showNormal(); reparent( pwidget, oriPoint ); show();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -