<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>Vidalia: ZImageView.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="navpath"><a class="el" href="dir_b47abd1aad201a152869566145babb61.html">src</a> » <a class="el" href="dir_695ba8f8357372e02d38c40b3c6dbe81.html">vidalia</a> » <a class="el" href="dir_3776330d706700e272fa8df738fecea6.html">network</a> </div> </div> <div class="contents"> <h1>ZImageView.cpp</h1><a href="_z_image_view_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment">** This file is part of Vidalia, and is subject to the license terms in the</span> <a name="l00003"></a>00003 <span class="comment">** LICENSE file, found in the top level directory of this distribution. If you</span> <a name="l00004"></a>00004 <span class="comment">** did not receive the LICENSE file with this file, you may obtain it from the</span> <a name="l00005"></a>00005 <span class="comment">** Vidalia source package distributed by the Vidalia Project at</span> <a name="l00006"></a>00006 <span class="comment">** http://www.vidalia-project.net/. No part of Vidalia, including this file,</span> <a name="l00007"></a>00007 <span class="comment">** may be copied, modified, propagated, or distributed except according to the</span> <a name="l00008"></a>00008 <span class="comment">** terms described in the LICENSE file.</span> <a name="l00009"></a>00009 <span class="comment">*/</span> <a name="l00010"></a>00010 <a name="l00011"></a>00011 <span class="comment">/*</span> <a name="l00012"></a>00012 <span class="comment">** \file ZImageView.cpp</span> <a name="l00013"></a>00013 <span class="comment">** \version $Id: ZImageView.cpp 3735 2009-04-28 20:28:01Z edmanm $</span> <a name="l00014"></a>00014 <span class="comment">** \brief Displays an image and allows zooming and panning</span> <a name="l00015"></a>00015 <span class="comment">*/</span> <a name="l00016"></a>00016 <a name="l00017"></a>00017 <span class="preprocessor">#include "<a class="code" href="_z_image_view_8h.html">ZImageView.h</a>"</span> <a name="l00018"></a>00018 <a name="l00019"></a>00019 <span class="preprocessor">#include <QPainter></span> <a name="l00020"></a>00020 <span class="preprocessor">#include <QMouseEvent></span> <a name="l00021"></a>00021 <a name="l00022"></a>00022 <span class="preprocessor">#include <cmath></span> <a name="l00023"></a>00023 <a name="l00024"></a>00024 <span class="preprocessor">#if QT_VERSION >= 0x040200</span> <a name="l00025"></a>00025 <span class="preprocessor"></span><span class="preprocessor">#define CURSOR_NORMAL QCursor(Qt::OpenHandCursor)</span> <a name="l00026"></a>00026 <span class="preprocessor"></span><span class="preprocessor">#define CURSOR_MOUSE_PRESS QCursor(Qt::ClosedHandCursor)</span> <a name="l00027"></a>00027 <span class="preprocessor"></span><span class="preprocessor">#else</span> <a name="l00028"></a><a class="code" href="_z_image_view_8cpp.html#a23768cc008ce16cba666aed372f8ceb8">00028</a> <span class="preprocessor"></span><span class="preprocessor">#define CURSOR_NORMAL QCursor(Qt::CrossCursor)</span> <a name="l00029"></a><a class="code" href="_z_image_view_8cpp.html#af9c60cf5bfb74c44713c5520b483ec02">00029</a> <span class="preprocessor"></span><span class="preprocessor">#define CURSOR_MOUSE_PRESS QCursor(Qt::SizeAllCursor)</span> <a name="l00030"></a>00030 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00031"></a>00031 <span class="preprocessor"></span> <a name="l00032"></a>00032 <span class="comment"></span> <a name="l00033"></a>00033 <span class="comment">/** Constructor. */</span> <a name="l00034"></a><a class="code" href="class_z_image_view.html#a20c2efec70e55e72cfa555fd6a5a5348">00034</a> <a class="code" href="class_z_image_view.html#a20c2efec70e55e72cfa555fd6a5a5348">ZImageView::ZImageView</a>(QWidget *parent) <a name="l00035"></a>00035 : QWidget(parent) <a name="l00036"></a>00036 { <a name="l00037"></a>00037 <span class="comment">/* Initialize members */</span> <a name="l00038"></a>00038 <a class="code" href="class_z_image_view.html#a09aa4727f102d675b068417a4584711d">_zoom</a> = 0.0; <a name="l00039"></a>00039 <a class="code" href="class_z_image_view.html#af54b58163cc045b9893b7df9040882c5">_desiredX</a> = 0.0; <a name="l00040"></a>00040 <a class="code" href="class_z_image_view.html#a9489f1183fec0ad2f4e741c0eaa87116">_desiredY</a> = 0.0; <a name="l00041"></a>00041 <a class="code" href="class_z_image_view.html#a4a6356de7396bfec67c00218d85c1bf0">_maxZoomFactor</a> = 2.0; <a name="l00042"></a>00042 <a class="code" href="class_z_image_view.html#a6126731c5cbd59f87e859d043e38d44a">_padding</a> = 60; <a name="l00043"></a>00043 <a name="l00044"></a>00044 setCursor(<a class="code" href="_z_image_view_8cpp.html#a23768cc008ce16cba666aed372f8ceb8">CURSOR_NORMAL</a>); <a name="l00045"></a>00045 <a class="code" href="class_z_image_view.html#abfebb20951fdb6a4a634ea50e8530269">updateViewport</a>(); <a name="l00046"></a>00046 <a class="code" href="class_z_image_view.html#abc80734053ec3e8f310d0668c97384cc">resetZoomPoint</a>(); <a name="l00047"></a>00047 repaint(); <a name="l00048"></a>00048 } <a name="l00049"></a>00049 <span class="comment"></span> <a name="l00050"></a>00050 <span class="comment">/** Sets the displayed image. */</span> <a name="l00051"></a>00051 <span class="keywordtype">void</span> <a name="l00052"></a><a class="code" href="class_z_image_view.html#a83f5953309ba8f95e6ca69c71df78b57">00052</a> <a class="code" href="class_z_image_view.html#a83f5953309ba8f95e6ca69c71df78b57">ZImageView::setImage</a>(QImage& img) <a name="l00053"></a>00053 { <a name="l00054"></a>00054 <a class="code" href="class_z_image_view.html#a6e57d445585f1cf7f6f95d716cbad0db">_image</a> = img.copy(); <a name="l00055"></a>00055 <a class="code" href="class_z_image_view.html#abfebb20951fdb6a4a634ea50e8530269">updateViewport</a>(); <a name="l00056"></a>00056 <a class="code" href="class_z_image_view.html#abc80734053ec3e8f310d0668c97384cc">resetZoomPoint</a>(); <a name="l00057"></a>00057 <a name="l00058"></a>00058 <span class="keywordflow">if</span> (isVisible()) { <a name="l00059"></a>00059 repaint(); <a name="l00060"></a>00060 } <a name="l00061"></a>00061 } <a name="l00062"></a>00062 <span class="comment"></span> <a name="l00063"></a>00063 <span class="comment">/** Draws the scaled image on the widget. */</span> <a name="l00064"></a>00064 <span class="keywordtype">void</span> <a name="l00065"></a><a class="code" href="class_z_image_view.html#ac62a97f3e68350811bd49f20923ec3c4">00065</a> <a class="code" href="class_z_image_view.html#ac62a97f3e68350811bd49f20923ec3c4">ZImageView::drawScaledImage</a>() <a name="l00066"></a>00066 { <a name="l00067"></a>00067 <span class="keywordflow">if</span> (!isVisible()) { <a name="l00068"></a>00068 <span class="keywordflow">return</span>; <a name="l00069"></a>00069 } <a name="l00070"></a>00070 <a name="l00071"></a>00071 QBrush background(QColor(<span class="stringliteral">"#fdfdfd"</span>)); <a name="l00072"></a>00072 <span class="keywordflow">if</span> (<a class="code" href="class_z_image_view.html#a6e57d445585f1cf7f6f95d716cbad0db">_image</a>.isNull()) { <a name="l00073"></a>00073 QPainter <a class="code" href="html_8cpp.html#a6859bd9e7f8c6a4ec0150749bac76c1d">p</a>(<span class="keyword">this</span>); <a name="l00074"></a>00074 p.fillRect(rect(), background); <a name="l00075"></a>00075 <span class="keywordflow">return</span>; <a name="l00076"></a>00076 } <a name="l00077"></a>00077 <a name="l00078"></a>00078 QRect sRect = rect(); <a name="l00079"></a>00079 QRect iRect = <a class="code" href="class_z_image_view.html#a6e57d445585f1cf7f6f95d716cbad0db">_image</a>.rect(); <a name="l00080"></a>00080 QRect r = <a class="code" href="class_z_image_view.html#a0098b990223e7a2a6a8b18636ae5d522">_view</a>; <a name="l00081"></a>00081 <a name="l00082"></a>00082 <span class="comment">// Think of the _view as being overlaid on the image. The _view has the same</span> <a name="l00083"></a>00083 <span class="comment">// aspect ratio as the screen, so we cut the _view region out of the _image</span> <a name="l00084"></a>00084 <span class="comment">// and scale it to the screen dimensions and paint it.</span> <a name="l00085"></a>00085 <a name="l00086"></a>00086 <span class="comment">// There is a slight catch in that the _view may be larger than the image in </span> <a name="l00087"></a>00087 <span class="comment">// one or both directions. In that case, we need to reduce the _view region</span> <a name="l00088"></a>00088 <span class="comment">// to lie within the image, then paint the background around it. Copying</span> <a name="l00089"></a>00089 <span class="comment">// a region from an image where the region is bigger than the image results</span> <a name="l00090"></a>00090 <span class="comment">// in the parts outside the image being black, which is not what we want.</span> <a name="l00091"></a>00091 <a name="l00092"></a>00092 <span class="comment">// The view has the same aspect ratio as the screen, so the vertical and </span> <a name="l00093"></a>00093 <span class="comment">// horizontal scale factors will be equal.</span> <a name="l00094"></a>00094 <a name="l00095"></a>00095 <span class="keywordtype">double</span> scaleFactor = double(sRect.width()) / <span class="keywordtype">double</span>(<a class="code" href="class_z_image_view.html#a0098b990223e7a2a6a8b18636ae5d522">_view</a>.width()); <a name="l00096"></a>00096 <a name="l00097"></a>00097 <span class="comment">// Constrain r to lie entirely within the image.</span> <a name="l00098"></a>00098 <span class="keywordflow">if</span> (r.top() < 0) { <a name="l00099"></a>00099 r.setTop(0); <a name="l00100"></a>00100 } <a name="l00101"></a>00101 <span class="keywordflow">if</span> (iRect.bottom() < r.bottom()) { <a name="l00102"></a>00102 r.setBottom(iRect.bottom()); <a name="l00103"></a>00103 } <a name="l00104"></a>00104 <span class="keywordflow">if</span> (r.left() < 0) { <a name="l00105"></a>00105 r.setLeft(0); <a name="l00106"></a>00106 } <a name="l00107"></a>00107 <span class="keywordflow">if</span> (iRect.right() < r.right()) { <a name="l00108"></a>00108 r.setRight(iRect.right()); <a name="l00109"></a>00109 } <a name="l00110"></a>00110 <a name="l00111"></a>00111 <span class="comment">// Figure out the size that the 'r' region will be when drawn to the screen.</span> <a name="l00112"></a>00112 QSize scaleTo(<span class="keywordtype">int</span>(<span class="keywordtype">double</span>(r.width()) * scaleFactor), <a name="l00113"></a>00113 int(<span class="keywordtype">double</span>(r.height()) * scaleFactor)); <a name="l00114"></a>00114 <span class="comment"></span> <a name="l00115"></a>00115 <span class="comment"> /** Make a copy of the image so we don't ruin the original */</span> <a name="l00116"></a>00116 QImage <a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a> = <a class="code" href="class_z_image_view.html#a6e57d445585f1cf7f6f95d716cbad0db">_image</a>.copy(); <a name="l00117"></a>00117 <span class="comment"></span> <a name="l00118"></a>00118 <span class="comment"> /** Create a QPainter that draws directly on the copied image and call the</span> <a name="l00119"></a>00119 <span class="comment"> * virtual function to draw whatever the subclasses need to on the image. */</span> <a name="l00120"></a>00120 QPainter painter; <a name="l00121"></a>00121 painter.begin(&i); <a name="l00122"></a>00122 <a class="code" href="class_z_image_view.html#ade3ed53ffad6fbd8b3de18a166aa8b0e">paintImage</a>(&painter); <a name="l00123"></a>00123 painter.end(); <a name="l00124"></a>00124 <span class="comment"></span> <a name="l00125"></a>00125 <span class="comment"> /** Rescale the image copy */</span> <a name="l00126"></a>00126 i = i.copy(r).scaled(scaleTo, <a name="l00127"></a>00127 Qt::KeepAspectRatioByExpanding, <a name="l00128"></a>00128 Qt::SmoothTransformation); <a name="l00129"></a>00129 <a name="l00130"></a>00130 <span class="keywordtype">int</span> extraWidth = int(<span class="keywordtype">double</span>(sRect.width() - i.width()) / 2.0); <a name="l00131"></a>00131 <span class="keywordtype">int</span> extraHeight = int(<span class="keywordtype">double</span>(sRect.height() - i.height()) / 2.0); <a name="l00132"></a>00132 <a name="l00133"></a>00133 <span class="comment">// We don't want to paint the background</span> <a name="l00134"></a>00134 <span class="comment">// because this isn't double buffered and that would flicker.</span> <a name="l00135"></a>00135 <span class="comment">// We could double buffer it, but that would cost ~3 MB of memory.</span> <a name="l00136"></a>00136 <a name="l00137"></a>00137 QPainter <a class="code" href="html_8cpp.html#a6859bd9e7f8c6a4ec0150749bac76c1d">p</a>(<span class="keyword">this</span>); <a name="l00138"></a>00138 <span class="keywordflow">if</span> (extraWidth > 0) { <a name="l00139"></a>00139 p.fillRect(0, 0, extraWidth, sRect.height(), background); <a name="l00140"></a>00140 p.fillRect(sRect.width() - extraWidth, 0, <a name="l00141"></a>00141 sRect.width(), sRect.height(), background); <a name="l00142"></a>00142 } <a name="l00143"></a>00143 <a name="l00144"></a>00144 <span class="keywordflow">if</span> (extraHeight > 0) { <a name="l00145"></a>00145 p.fillRect(0, 0, sRect.width(), extraHeight, background); <a name="l00146"></a>00146 p.fillRect(0, sRect.height() - extraHeight, <a name="l00147"></a>00147 sRect.width(), sRect.height(), background); <a name="l00148"></a>00148 } <a name="l00149"></a>00149 <a name="l00150"></a>00150 <span class="comment">// Finally, paint the image copy.</span> <a name="l00151"></a>00151 p.drawImage(extraWidth, extraHeight, i); <a name="l00152"></a>00152 } <a name="l00153"></a>00153 <span class="comment"></span> <a name="l00154"></a>00154 <span class="comment">/** Updates the displayed viewport. */</span> <a name="l00155"></a>00155 <span class="keywordtype">void</span> <a name="l00156"></a><a class="code" href="class_z_image_view.html#abfebb20951fdb6a4a634ea50e8530269">00156</a> <a class="code" href="class_z_image_view.html#abfebb20951fdb6a4a634ea50e8530269">ZImageView::updateViewport</a>(<span class="keywordtype">int</span> screendx, <span class="keywordtype">int</span> screendy) <a name="l00157"></a>00157 { <a name="l00158"></a>00158 <span class="comment">/* The gist of this is to find the biggest and smallest possible viewports,</span> <a name="l00159"></a>00159 <span class="comment"> * then use the _zoom factor to interpolate between them. Also pan the </span> <a name="l00160"></a>00160 <span class="comment"> * viewport, but constrain each dimension to lie within the image or to be </span> <a name="l00161"></a>00161 <span class="comment"> * centered if the image is too small in that direction. */</span> <a name="l00162"></a>00162 <a name="l00163"></a>00163 QRect sRect = rect(); <a name="l00164"></a>00164 QRect iRect = <a class="code" href="class_z_image_view.html#a6e57d445585f1cf7f6f95d716cbad0db">_image</a>.rect(); <a name="l00165"></a>00165 <a name="l00166"></a>00166 <span class="keywordtype">float</span> sw = float(sRect.width()); <a name="l00167"></a>00167 <span class="keywordtype">float</span> sh = float(sRect.height()); <a name="l00168"></a>00168 <span class="keywordtype">float</span> iw = float(iRect.width()); <a name="l00169"></a>00169 <span class="keywordtype">float</span> ih = float(iRect.height()); <a name="l00170"></a>00170 <a name="l00171"></a>00171 <span class="comment">// Get the initial max and min sizes for the viewport. These won't have the </span> <a name="l00172"></a>00172 <span class="comment">// correct aspect ratio. They will actually be the least upper bound and </span> <a name="l00173"></a>00173 <span class="comment">// greatest lower bound of the set containing the screen and image rects.</span> <a name="l00174"></a>00174 <span class="keywordtype">float</span> maxw = float(std::max<int>(sRect.width(), iRect.width())) + <a class="code" href="class_z_image_view.html#a6126731c5cbd59f87e859d043e38d44a">_padding</a>; <a name="l00175"></a>00175 <span class="keywordtype">float</span> maxh = float(std::max<int>(sRect.height(), iRect.height())) + <a class="code" href="class_z_image_view.html#a6126731c5cbd59f87e859d043e38d44a">_padding</a>; <a name="l00176"></a>00176 <span class="keywordtype">float</span> minw = std::ceil(<span class="keywordtype">float</span>(sRect.width()) / <a class="code" href="class_z_image_view.html#a4a6356de7396bfec67c00218d85c1bf0">_maxZoomFactor</a>); <a name="l00177"></a>00177 <span class="keywordtype">float</span> minh = std::ceil(<span class="keywordtype">float</span>(sRect.height()) / <a class="code" href="class_z_image_view.html#a4a6356de7396bfec67c00218d85c1bf0">_maxZoomFactor</a>); <a name="l00178"></a>00178 <a name="l00179"></a>00179 <span class="comment">// Now that we have the glb and the lub, we expand/shrink them until</span> <a name="l00180"></a>00180 <span class="comment">// the aspect ratio is that of the screen.</span> <a name="l00181"></a>00181 <span class="keywordtype">float</span> aspect = sw / sh; <a name="l00182"></a>00182 <a name="l00183"></a>00183 <span class="comment">// Fix the max rect.</span> <a name="l00184"></a>00184 <span class="keywordtype">float</span> newmaxh = maxh; <a name="l00185"></a>00185 <span class="keywordtype">float</span> newmaxw = aspect * newmaxh; <a name="l00186"></a>00186 <span class="keywordflow">if</span> (newmaxw < maxw) { <a name="l00187"></a>00187 newmaxw = maxw; <a name="l00188"></a>00188 newmaxh = maxw / aspect; <a name="l00189"></a>00189 } <a name="l00190"></a>00190 <a name="l00191"></a>00191 <span class="comment">// Fix the min rect.</span> <a name="l00192"></a>00192 <span class="keywordtype">float</span> newminh = minh; <a name="l00193"></a>00193 <span class="keywordtype">float</span> newminw = aspect * newminh; <a name="l00194"></a>00194 <span class="keywordflow">if</span> (minw < newminw) { <a name="l00195"></a>00195 newminw = minw; <a name="l00196"></a>00196 newminh = newminw / aspect; <a name="l00197"></a>00197 } <a name="l00198"></a>00198 <a name="l00199"></a>00199 <span class="comment">// Now interpolate between max and min.</span> <a name="l00200"></a>00200 <span class="keywordtype">float</span> vw = (1.0f - <a class="code" href="class_z_image_view.html#a09aa4727f102d675b068417a4584711d">_zoom</a>) * (newmaxw - newminw) + newminw; <a name="l00201"></a>00201 <span class="keywordtype">float</span> vh = (1.0f - <a class="code" href="class_z_image_view.html#a09aa4727f102d675b068417a4584711d">_zoom</a>) * (newmaxh - newminh) + newminh; <a name="l00202"></a>00202 <a name="l00203"></a>00203 <a class="code" href="class_z_image_view.html#a0098b990223e7a2a6a8b18636ae5d522">_view</a>.setWidth(<span class="keywordtype">int</span>(vw)); <a name="l00204"></a>00204 <a class="code" href="class_z_image_view.html#a0098b990223e7a2a6a8b18636ae5d522">_view</a>.setHeight(<span class="keywordtype">int</span>(vh)); <a name="l00205"></a>00205 <a name="l00206"></a>00206 <span class="comment">// Now pan the view</span> <a name="l00207"></a>00207 <a name="l00208"></a>00208 <span class="comment">// Convert the pan delta from screen coordinates to view coordinates.</span> <a name="l00209"></a>00209 <span class="keywordtype">float</span> vdx = vw * (float(screendx) / sw); <a name="l00210"></a>00210 <span class="keywordtype">float</span> vdy = vh * (float(screendy) / sh); <a name="l00211"></a>00211 <a name="l00212"></a>00212 <span class="comment">// Constrain the center of the viewport to the image rect.</span> <a name="l00213"></a>00213 <a class="code" href="class_z_image_view.html#af54b58163cc045b9893b7df9040882c5">_desiredX</a> = qBound(0.0f, _desiredX + vdx, iw); <a name="l00214"></a>00214 <a class="code" href="class_z_image_view.html#a9489f1183fec0ad2f4e741c0eaa87116">_desiredY</a> = qBound(0.0f, <a class="code" href="class_z_image_view.html#a9489f1183fec0ad2f4e741c0eaa87116">_desiredY</a> + vdy, ih); <a name="l00215"></a>00215 <a class="code" href="class_z_image_view.html#a0098b990223e7a2a6a8b18636ae5d522">_view</a>.moveCenter(QPoint(<span class="keywordtype">int</span>(_desiredX), <span class="keywordtype">int</span>(<a class="code" href="class_z_image_view.html#a9489f1183fec0ad2f4e741c0eaa87116">_desiredY</a>))); <a name="l00216"></a>00216 <a name="l00217"></a>00217 QPoint viewCenter = <a class="code" href="class_z_image_view.html#a0098b990223e7a2a6a8b18636ae5d522">_view</a>.center(); <a name="l00218"></a>00218 <span class="keywordtype">float</span> vx = viewCenter.x(); <a name="l00219"></a>00219 <span class="keywordtype">float</span> vy = viewCenter.y(); <a name="l00220"></a>00220 <a name="l00221"></a>00221 <span class="comment">// The viewport may be wider than the height and/or width. In that case,</span> <a name="l00222"></a>00222 <span class="comment">// center the view over the image in the appropriate directions.</span> <a name="l00223"></a>00223 <span class="comment">//</span> <a name="l00224"></a>00224 <span class="comment">// If the viewport is smaller than the image in either direction, then make</span> <a name="l00225"></a>00225 <span class="comment">// sure the edge of the viewport isn't past the edge of the image.</span> <a name="l00226"></a>00226 <a name="l00227"></a>00227 vdx = 0; <a name="l00228"></a>00228 vdy = 0; <a name="l00229"></a>00229 <a name="l00230"></a>00230 <span class="keywordflow">if</span> (iw <= vw) { <a name="l00231"></a>00231 vdx = (iw / 2.0f) - vx; <span class="comment">// Center horizontally.</span> <a name="l00232"></a>00232 } <span class="keywordflow">else</span> { <a name="l00233"></a>00233 <span class="comment">// Check that the edge of the view isn't past the edge of the image.</span> <a name="l00234"></a>00234 <span class="keywordtype">float</span> vl = float(<a class="code" href="class_z_image_view.html#a0098b990223e7a2a6a8b18636ae5d522">_view</a>.left()); <a name="l00235"></a>00235 <span class="keywordtype">float</span> vr = float(<a class="code" href="class_z_image_view.html#a0098b990223e7a2a6a8b18636ae5d522">_view</a>.right()); <a name="l00236"></a>00236 <span class="keywordflow">if</span> (vl < 0) { <a name="l00237"></a>00237 vdx = -vl; <a name="l00238"></a>00238 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (vr > iw) { <a name="l00239"></a>00239 vdx = iw - vr; <a name="l00240"></a>00240 } <a name="l00241"></a>00241 } <a name="l00242"></a>00242 <a name="l00243"></a>00243 <span class="keywordflow">if</span> (ih <= vh) { <a name="l00244"></a>00244 vdy = (ih / 2.0f) - vy; <span class="comment">// Center vertically.</span> <a name="l00245"></a>00245 } <span class="keywordflow">else</span> { <a name="l00246"></a>00246 <span class="comment">// Check that the edge of the view isn't past the edge of the image.</span> <a name="l00247"></a>00247 <span class="keywordtype">float</span> vt = float(<a class="code" href="class_z_image_view.html#a0098b990223e7a2a6a8b18636ae5d522">_view</a>.top()); <a name="l00248"></a>00248 <span class="keywordtype">float</span> vb = float(<a class="code" href="class_z_image_view.html#a0098b990223e7a2a6a8b18636ae5d522">_view</a>.bottom()); <a name="l00249"></a>00249 <span class="keywordflow">if</span> (vt < 0) { <a name="l00250"></a>00250 vdy = -vt; <a name="l00251"></a>00251 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (vb > ih) { <a name="l00252"></a>00252 vdy = ih - vb; <a name="l00253"></a>00253 } <a name="l00254"></a>00254 } <a name="l00255"></a>00255 <a name="l00256"></a>00256 <a class="code" href="class_z_image_view.html#a0098b990223e7a2a6a8b18636ae5d522">_view</a>.translate(<span class="keywordtype">int</span>(vdx), <span class="keywordtype">int</span>(vdy)); <a name="l00257"></a>00257 } <a name="l00258"></a>00258 <span class="comment"></span> <a name="l00259"></a>00259 <span class="comment">/** Resets the zoom point back to the center of the viewport. */</span> <a name="l00260"></a>00260 <span class="keywordtype">void</span> <a name="l00261"></a><a class="code" href="class_z_image_view.html#abc80734053ec3e8f310d0668c97384cc">00261</a> <a class="code" href="class_z_image_view.html#abc80734053ec3e8f310d0668c97384cc">ZImageView::resetZoomPoint</a>() <a name="l00262"></a>00262 { <a name="l00263"></a>00263 QPoint viewCenter = <a class="code" href="class_z_image_view.html#a0098b990223e7a2a6a8b18636ae5d522">_view</a>.center(); <a name="l00264"></a>00264 <a class="code" href="class_z_image_view.html#af54b58163cc045b9893b7df9040882c5">_desiredX</a> = viewCenter.x(); <a name="l00265"></a>00265 <a class="code" href="class_z_image_view.html#a9489f1183fec0ad2f4e741c0eaa87116">_desiredY</a> = viewCenter.y(); <a name="l00266"></a>00266 } <a name="l00267"></a>00267 <span class="comment"></span> <a name="l00268"></a>00268 <span class="comment">/** Handles repainting this widget by updating the viewport and drawing the</span> <a name="l00269"></a>00269 <span class="comment"> * scaled image. */</span> <a name="l00270"></a>00270 <span class="keywordtype">void</span> <a name="l00271"></a><a class="code" href="class_z_image_view.html#aff831b1cf2eddbbf7209287455afb82b">00271</a> <a class="code" href="class_z_image_view.html#aff831b1cf2eddbbf7209287455afb82b">ZImageView::paintEvent</a>(QPaintEvent*) <a name="l00272"></a>00272 { <a name="l00273"></a>00273 <a class="code" href="class_z_image_view.html#abfebb20951fdb6a4a634ea50e8530269">updateViewport</a>(); <a name="l00274"></a>00274 <a class="code" href="class_z_image_view.html#ac62a97f3e68350811bd49f20923ec3c4">drawScaledImage</a>(); <a name="l00275"></a>00275 } <a name="l00276"></a>00276 <span class="comment"></span> <a name="l00277"></a>00277 <span class="comment">/** Sets the current zoom percentage to the given value and scrolls the</span> <a name="l00278"></a>00278 <span class="comment"> * viewport to center the given point. */</span> <a name="l00279"></a>00279 <span class="keywordtype">void</span> <a name="l00280"></a><a class="code" href="class_z_image_view.html#a37efcd3b50cf79f7caa5c1007dc44d0d">00280</a> <a class="code" href="class_z_image_view.html#a3e0fb8ec2586bc3bbcd4791022534fd5">ZImageView::zoom</a>(QPoint zoomAt, <span class="keywordtype">float</span> pct) <a name="l00281"></a>00281 { <a name="l00282"></a>00282 <a class="code" href="class_z_image_view.html#af54b58163cc045b9893b7df9040882c5">_desiredX</a> = zoomAt.x(); <a name="l00283"></a>00283 <a class="code" href="class_z_image_view.html#a9489f1183fec0ad2f4e741c0eaa87116">_desiredY</a> = zoomAt.y(); <a name="l00284"></a>00284 <a class="code" href="class_z_image_view.html#a3e0fb8ec2586bc3bbcd4791022534fd5">zoom</a>(pct); <a name="l00285"></a>00285 } <a name="l00286"></a>00286 <span class="comment"></span> <a name="l00287"></a>00287 <span class="comment">/** Sets the current zoom percentage to the given value. */</span> <a name="l00288"></a>00288 <span class="keywordtype">void</span> <a name="l00289"></a><a class="code" href="class_z_image_view.html#a3e0fb8ec2586bc3bbcd4791022534fd5">00289</a> <a class="code" href="class_z_image_view.html#a3e0fb8ec2586bc3bbcd4791022534fd5">ZImageView::zoom</a>(<span class="keywordtype">float</span> pct) <a name="l00290"></a>00290 { <a name="l00291"></a>00291 <a class="code" href="class_z_image_view.html#a09aa4727f102d675b068417a4584711d">_zoom</a> = qBound(0.0f, pct, 1.0f); <a name="l00292"></a>00292 repaint(); <a name="l00293"></a>00293 } <a name="l00294"></a>00294 <span class="comment"></span> <a name="l00295"></a>00295 <span class="comment">/** Zooms into the image by 10% */</span> <a name="l00296"></a>00296 <span class="keywordtype">void</span> <a name="l00297"></a><a class="code" href="class_z_image_view.html#a68c4dcf1561fa920240db2843525d4f6">00297</a> <a class="code" href="class_z_image_view.html#a68c4dcf1561fa920240db2843525d4f6">ZImageView::zoomIn</a>() <a name="l00298"></a>00298 { <a name="l00299"></a>00299 <a class="code" href="class_z_image_view.html#a3e0fb8ec2586bc3bbcd4791022534fd5">zoom</a>(<a class="code" href="class_z_image_view.html#a09aa4727f102d675b068417a4584711d">_zoom</a> + .1); <a name="l00300"></a>00300 } <a name="l00301"></a>00301 <span class="comment"></span> <a name="l00302"></a>00302 <span class="comment">/** Zooms away from the image by 10% */</span> <a name="l00303"></a>00303 <span class="keywordtype">void</span> <a name="l00304"></a><a class="code" href="class_z_image_view.html#ac75ff5e4efacd591a4d40e0b00ec66ef">00304</a> <a class="code" href="class_z_image_view.html#ac75ff5e4efacd591a4d40e0b00ec66ef">ZImageView::zoomOut</a>() <a name="l00305"></a>00305 { <a name="l00306"></a>00306 <a class="code" href="class_z_image_view.html#a3e0fb8ec2586bc3bbcd4791022534fd5">zoom</a>(<a class="code" href="class_z_image_view.html#a09aa4727f102d675b068417a4584711d">_zoom</a> - .1); <a name="l00307"></a>00307 } <a name="l00308"></a>00308 <span class="comment"></span> <a name="l00309"></a>00309 <span class="comment">/** Responds to the user pressing a mouse button. */</span> <a name="l00310"></a>00310 <span class="keywordtype">void</span> <a name="l00311"></a><a class="code" href="class_z_image_view.html#aa188bc90ca38d51d133abfa1fef6b3e7">00311</a> <a class="code" href="class_z_image_view.html#aa188bc90ca38d51d133abfa1fef6b3e7">ZImageView::mousePressEvent</a>(QMouseEvent *e) <a name="l00312"></a>00312 { <a name="l00313"></a>00313 e->accept(); <a name="l00314"></a>00314 setCursor(<a class="code" href="_z_image_view_8cpp.html#af9c60cf5bfb74c44713c5520b483ec02">CURSOR_MOUSE_PRESS</a>); <a name="l00315"></a>00315 <a class="code" href="class_z_image_view.html#a47ff80bede59e6c486a426d56049f785">_mouseX</a> = e->x(); <a name="l00316"></a>00316 <a class="code" href="class_z_image_view.html#ac795b670066c66dd0272e79e140f2757">_mouseY</a> = e->y(); <a name="l00317"></a>00317 } <a name="l00318"></a>00318 <span class="comment"></span> <a name="l00319"></a>00319 <span class="comment">/** Responds to the user releasing a mouse button. */</span> <a name="l00320"></a>00320 <span class="keywordtype">void</span> <a name="l00321"></a><a class="code" href="class_z_image_view.html#a29561f7d44a1bd07e10e0ec0db3f5a5b">00321</a> <a class="code" href="class_z_image_view.html#a29561f7d44a1bd07e10e0ec0db3f5a5b">ZImageView::mouseReleaseEvent</a>(QMouseEvent *e) <a name="l00322"></a>00322 { <a name="l00323"></a>00323 e->accept(); <a name="l00324"></a>00324 setCursor(<a class="code" href="_z_image_view_8cpp.html#a23768cc008ce16cba666aed372f8ceb8">CURSOR_NORMAL</a>); <a name="l00325"></a>00325 <a class="code" href="class_z_image_view.html#abfebb20951fdb6a4a634ea50e8530269">updateViewport</a>(); <a name="l00326"></a>00326 <a class="code" href="class_z_image_view.html#abc80734053ec3e8f310d0668c97384cc">resetZoomPoint</a>(); <a name="l00327"></a>00327 } <a name="l00328"></a>00328 <span class="comment"></span> <a name="l00329"></a>00329 <span class="comment">/** Responds to the user double-clicking a mouse button on the image. A left</span> <a name="l00330"></a>00330 <span class="comment"> * double-click zooms in on the image and a right double-click zooms out.</span> <a name="l00331"></a>00331 <span class="comment"> * Zooming is centered on the location of the double-click. */</span> <a name="l00332"></a>00332 <span class="keywordtype">void</span> <a name="l00333"></a><a class="code" href="class_z_image_view.html#a9892c980c0fcf30fad3e892e63903af1">00333</a> <a class="code" href="class_z_image_view.html#a9892c980c0fcf30fad3e892e63903af1">ZImageView::mouseDoubleClickEvent</a>(QMouseEvent *e) <a name="l00334"></a>00334 { <a name="l00335"></a>00335 e->accept(); <a name="l00336"></a>00336 <a name="l00337"></a>00337 QPoint center = rect().center(); <a name="l00338"></a>00338 <span class="keywordtype">int</span> dx = e->x() - center.x(); <a name="l00339"></a>00339 <span class="keywordtype">int</span> dy = e->y() - center.y(); <a name="l00340"></a>00340 <a class="code" href="class_z_image_view.html#abfebb20951fdb6a4a634ea50e8530269">updateViewport</a>(dx, dy); <a name="l00341"></a>00341 <a class="code" href="class_z_image_view.html#abc80734053ec3e8f310d0668c97384cc">resetZoomPoint</a>(); <a name="l00342"></a>00342 <a name="l00343"></a>00343 Qt::MouseButton btn = e->button(); <a name="l00344"></a>00344 <span class="keywordflow">if</span> (btn == Qt::LeftButton) <a name="l00345"></a>00345 <a class="code" href="class_z_image_view.html#a68c4dcf1561fa920240db2843525d4f6">zoomIn</a>(); <a name="l00346"></a>00346 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (btn == Qt::RightButton) <a name="l00347"></a>00347 <a class="code" href="class_z_image_view.html#ac75ff5e4efacd591a4d40e0b00ec66ef">zoomOut</a>(); <a name="l00348"></a>00348 } <a name="l00349"></a>00349 <span class="comment"></span> <a name="l00350"></a>00350 <span class="comment">/** Responds to the user moving the mouse. */</span> <a name="l00351"></a>00351 <span class="keywordtype">void</span> <a name="l00352"></a><a class="code" href="class_z_image_view.html#ab7d6c2663d2c443e053ca61545c0e28b">00352</a> <a class="code" href="class_z_image_view.html#ab7d6c2663d2c443e053ca61545c0e28b">ZImageView::mouseMoveEvent</a>(QMouseEvent *e) <a name="l00353"></a>00353 { <a name="l00354"></a>00354 e->accept(); <a name="l00355"></a>00355 <span class="keywordtype">int</span> dx = <a class="code" href="class_z_image_view.html#a47ff80bede59e6c486a426d56049f785">_mouseX</a> - e->x(); <a name="l00356"></a>00356 <span class="keywordtype">int</span> dy = <a class="code" href="class_z_image_view.html#ac795b670066c66dd0272e79e140f2757">_mouseY</a> - e->y(); <a name="l00357"></a>00357 <a class="code" href="class_z_image_view.html#a47ff80bede59e6c486a426d56049f785">_mouseX</a> = e->x(); <a name="l00358"></a>00358 <a class="code" href="class_z_image_view.html#ac795b670066c66dd0272e79e140f2757">_mouseY</a> = e->y(); <a name="l00359"></a>00359 <a name="l00360"></a>00360 <a class="code" href="class_z_image_view.html#abfebb20951fdb6a4a634ea50e8530269">updateViewport</a>(dx, dy); <a name="l00361"></a>00361 <span class="keywordflow">if</span> (0.001 <= <a class="code" href="class_z_image_view.html#a09aa4727f102d675b068417a4584711d">_zoom</a>) { <a name="l00362"></a>00362 repaint(); <a name="l00363"></a>00363 } <a name="l00364"></a>00364 } <a name="l00365"></a>00365 <a name="l00366"></a>00366 <span class="keywordtype">void</span> <a name="l00367"></a><a class="code" href="class_z_image_view.html#ad988031ebc9e8677f939359db47934b6">00367</a> <a class="code" href="class_z_image_view.html#ad988031ebc9e8677f939359db47934b6">ZImageView::wheelEvent</a>(QWheelEvent *e) <a name="l00368"></a>00368 { <a name="l00369"></a>00369 <span class="keywordflow">if</span> (e->delta() > 0) { <a name="l00370"></a>00370 <a class="code" href="class_z_image_view.html#a68c4dcf1561fa920240db2843525d4f6">zoomIn</a>(); <a name="l00371"></a>00371 } <span class="keywordflow">else</span> { <a name="l00372"></a>00372 <a class="code" href="class_z_image_view.html#ac75ff5e4efacd591a4d40e0b00ec66ef">zoomOut</a>(); <a name="l00373"></a>00373 } <a name="l00374"></a>00374 } </pre></div></div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Mon Aug 30 22:58:55 2010 for Vidalia by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>