1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
#-------------------------------------------------------------------------------
#
# Copyright (c) 2009, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
#
# Author: Evan Patterson
# Date: 07/21/2009
#
#-------------------------------------------------------------------------------
""" Traits UI 'display only' image editor.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
from pyface.qt.QtGui import QFrame, QPainter, QPalette
from pyface.image_resource \
import ImageResource
from traitsui.ui_traits \
import convert_bitmap
# FIXME: ImageEditor is a proxy class defined here just for backward
# compatibility. The class has been moved to the
# traitsui.editors.image_editor file.
from traitsui.editors.image_editor \
import ImageEditor
from editor \
import Editor
#-------------------------------------------------------------------------------
# 'QImageView' class:
#-------------------------------------------------------------------------------
# backported and modified from Enaml ImageView
class QImageView(QFrame):
""" A custom QFrame that will paint a QPixmap as an image. The
api is similar to QLabel, but with a few more options to control
how the image scales.
"""
def __init__(self, parent=None):
""" Initialize a QImageView.
Parameters
----------
parent : QWidget or None, optional
The parent widget of this image viewer.
"""
super(QImageView, self).__init__(parent)
self._pixmap = None
self._scaled_contents = False
self._allow_upscaling = False
self._preserve_aspect_ratio = False
self._allow_clipping = False
self.setBackgroundRole(QPalette.Window)
#--------------------------------------------------------------------------
# Private API
#--------------------------------------------------------------------------
def paintEvent(self, event):
""" A custom paint event handler which draws the image according
to the current size constraints.
"""
pixmap = self._pixmap
if pixmap is None:
return
pm_size = pixmap.size()
pm_width = pm_size.width()
pm_height = pm_size.height()
if pm_width == 0 or pm_height == 0:
return
width = self.size().width()
height = self.size().height()
if not self._scaled_contents:
# If the image isn't scaled, it is centered if possible.
# Otherwise, it's painted at the origin and clipped.
paint_x = max(0, int(width / 2. - pm_width / 2.))
paint_y = max(0, int(height / 2. - pm_height / 2.))
paint_width = pm_width
paint_height = pm_height
else:
# If the image *is* scaled, it's scaled size depends on the
# size of the paint area as well as the other scaling flags.
if self._preserve_aspect_ratio:
pm_ratio = float(pm_width) / pm_height
ratio = float(width) / height
if ratio >= pm_ratio:
if self._allow_upscaling:
paint_height = height
else:
paint_height = min(pm_height, height)
paint_width = int(paint_height * pm_ratio)
else:
if self._allow_upscaling:
paint_width = width
else:
paint_width = min(pm_width, width)
paint_height = int(paint_width / pm_ratio)
else:
if self._allow_upscaling:
paint_height = height
paint_width = width
else:
paint_height = min(pm_height, height)
paint_width = min(pm_width, width)
# In all cases of scaling, we know that the scaled image is
# no larger than the paint area, and can thus be centered.
paint_x = int(width / 2. - paint_width / 2.)
paint_y = int(height / 2. - paint_height / 2.)
# Finally, draw the pixmap into the calculated rect.
painter = QPainter(self)
painter.setRenderHint(QPainter.SmoothPixmapTransform)
painter.drawPixmap(paint_x, paint_y, paint_width, paint_height, pixmap)
#--------------------------------------------------------------------------
# Public API
#--------------------------------------------------------------------------
def sizeHint(self):
""" Returns a appropriate size hint for the image based on the
underlying QPixmap.
"""
pixmap = self._pixmap
if pixmap is not None:
return pixmap.size()
return super(QImageView, self).sizeHint()
def minimumSizeHint(self):
""" Returns a appropriate minimum size hint for the image based on the
underlying QPixmap.
"""
pixmap = self._pixmap
if pixmap is not None and not self._allow_clipping and not self._scaled_contents:
return pixmap.size()
return super(QImageView, self).sizeHint()
def pixmap(self):
""" Returns the underlying pixmap for the image view.
"""
return self._pixmap
def setPixmap(self, pixmap):
""" Set the pixmap to use as the image in the widget.
Parameters
----------
pixamp : QPixmap
The QPixmap to use as the image in the widget.
"""
self._pixmap = pixmap
self.update()
def scaledContents(self):
""" Returns whether or not the contents scale with the widget
size.
"""
return self._scaled_contents
def setScaledContents(self, scaled):
""" Set whether the contents scale with the widget size.
Parameters
----------
scaled : bool
If True, the image will be scaled to fit the widget size,
subject to the other sizing constraints in place. If False,
the image will not scale and will be clipped as required.
"""
self._scaled_contents = scaled
self.update()
def allowUpscaling(self):
""" Returns whether or not the image can be scaled greater than
its natural size.
"""
return self._allow_upscaling
def setAllowUpscaling(self, allow):
""" Set whether or not to allow the image to be scaled beyond
its natural size.
Parameters
----------
allow : bool
If True, then the image may be scaled larger than its
natural if it is scaled to fit. If False, the image will
never be scaled larger than its natural size. In either
case, the image may be scaled smaller.
"""
self._allow_upscaling = allow
self.update()
def preserveAspectRatio(self):
""" Returns whether or not the aspect ratio of the image is
maintained during a resize.
"""
return self._preserve_aspect_ratio
def setPreserveAspectRatio(self, preserve):
""" Set whether or not to preserve the image aspect ratio.
Parameters
----------
preserve : bool
If True then the aspect ratio of the image will be preserved
if it is scaled to fit. Otherwise, the aspect ratio will be
ignored.
"""
self._preserve_aspect_ratio = preserve
self.update()
def allowClipping(self):
""" Returns whether or not the image should be clipped in the view.
"""
return self._preserve_aspect_ratio
def setAllowClipping(self, allow):
""" Set whether or not the image should be clipped in the view.
Parameters
----------
allow : bool
If True then clipping will be allowed. Otherwise the minimum
size hint will be the image size.
"""
self._allow_clipping = allow
self.update()
#-------------------------------------------------------------------------------
# '_ImageEditor' class:
#-------------------------------------------------------------------------------
class _ImageEditor ( Editor ):
""" Traits UI 'display only' image editor.
"""
#---------------------------------------------------------------------------
# Finishes initializing the editor by creating the underlying toolkit
# widget:
#---------------------------------------------------------------------------
def init ( self, parent ):
""" Finishes initializing the editor by creating the underlying toolkit
widget.
"""
image = self.factory.image
if image is None:
image = self.value
self.control = QImageView()
self.control.setPixmap( convert_bitmap( image ) )
self.control.setScaledContents(self.factory.scale)
self.control.setAllowUpscaling(self.factory.allow_upscaling)
self.control.setPreserveAspectRatio(self.factory.preserve_aspect_ratio)
self.control.setAllowClipping(self.factory.allow_clipping)
self.set_tooltip()
#---------------------------------------------------------------------------
# Updates the editor when the object trait changes external to the editor:
#---------------------------------------------------------------------------
def update_editor ( self ):
""" Updates the editor when the object trait changes externally to the
editor.
"""
if self.factory.image is None:
value = self.value
if isinstance( value, ImageResource ):
self.control.setPixmap( convert_bitmap( value ) )
self.control.setScaledContents(self.factory.scale)
self.control.setAllowUpscaling(self.factory.allow_upscaling)
self.control.setPreserveAspectRatio(self.factory.preserve_aspect_ratio)
self.control.setAllowClipping(self.factory.allow_clipping)
|