00001 /**************************************************************** 00002 * Vidalia is distributed under the following license: 00003 * 00004 * Copyright (C) 2006, Matt Edman, Justin Hipple 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 ****************************************************************/ 00021 00022 /** 00023 * \file zimageview.h 00024 * \version $Id: netviewer.cpp 699 2006-04-15 03:12:22Z hipplej $ 00025 * \brief Displays an image and allows zooming and panning 00026 */ 00027 00028 #ifndef ZIMAGEVIEW_H 00029 #define ZIMAGEVIEW_H 00030 00031 #include <QImage> 00032 #include <QFrame> 00033 #include <QPixmap> 00034 #include <QWidget> 00035 00036 00037 class ZImageView : public QWidget 00038 { 00039 Q_OBJECT 00040 00041 public: 00042 /** Default constructor. */ 00043 ZImageView(QWidget *parent = 0); 00044 /** Sets the displayed image. */ 00045 void setImage(QImage& pixmap); 00046 00047 public slots: 00048 /** Resets the center zoom point back to the center of the viewport. */ 00049 void resetZoomPoint(); 00050 /** Sets the current zoom level to the given percent. */ 00051 void zoom(float pct); 00052 /** Sets the current zoom level to the given percent and scrolls the window 00053 * to place the specified point in the middle. */ 00054 void zoom(QPoint zoomAt, float pct); 00055 /** Zooms into the displayed image by 5% */ 00056 void zoomIn(); 00057 /** Zooms away from the displayed image by 5% */ 00058 void zoomOut(); 00059 00060 protected: 00061 /** Virtual method to let subclasses paint on the image before it's scaled. */ 00062 virtual void paintImage(QPainter *painter) { Q_UNUSED(painter); } 00063 /** Updates the viewport and repaints the displayed image. */ 00064 virtual void paintEvent(QPaintEvent*); 00065 /** Handles the user pressing a mouse button. */ 00066 virtual void mousePressEvent(QMouseEvent* e); 00067 /** Handles the user releasing a mouse button. */ 00068 virtual void mouseReleaseEvent(QMouseEvent* e); 00069 /** Handles the user moving the mouse. */ 00070 virtual void mouseMoveEvent(QMouseEvent* e); 00071 /** Handles events where the widget is resized. */ 00072 virtual void resizeEvent(QResizeEvent* e); 00073 00074 /** Update the viewport. This will set _view to a region that, 00075 * when copied from the image and scaled to the screen size, will 00076 * show what is expected. The _view may be larger in one or more 00077 * directions than the image, and you must deal with the 00078 * non-overlapping regions. 00079 * 00080 * Returns the _zoom==0.0 viewport rect (the max) and the 00081 * _zoom==1.0 viewport rect (the min). */ 00082 QPair<QRect, QRect> updateViewport(int screendx=0, int screendy=0); 00083 /** Redraws the scaled image in the viewport. */ 00084 void drawScaledImage(); 00085 00086 private: 00087 float _zoom; /**< The current zoom level. */ 00088 QImage _image; /**< The displayed image. */ 00089 float _padding; /**< Amount of padding to use on the side of the image. */ 00090 float _maxZoomFactor; /**< Maximum amount to zoom into the image. */ 00091 00092 bool _mouseDown; /**< Set to true when a mouse button is depressed. */ 00093 int _mouseX; /**< The x-coordinate of the current mouse position. */ 00094 int _mouseY; /**< The y-coordinate of the current mouse position. */ 00095 00096 QRect _view; /**< The displayed viewport. */ 00097 float _desiredX; /**< The X value we desire (???). */ 00098 float _desiredY; /**< The Y value we desire (???). */ 00099 }; 00100 00101 #endif 00102