/usr/lib/python2.7/dist-packages/pipedviewer/cmndhelperpq.py is in python-ferret 7.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | '''
CmndHelperPQ is a helper class for dealing with commands
sent to a PyQt piped viewer.
This package was developed by the Thermal Modeling and Analysis
Project (TMAP) of the National Oceanographic and Atmospheric
Administration's (NOAA) Pacific Marine Environmental Lab (PMEL).
'''
import sys
# First try to import PyQt5, then try PyQt4 if that fails
try:
import PyQt5
QT_VERSION = 5
except ImportError:
import PyQt4
QT_VERSION = 4
# Now that the PyQt version is determined, import the parts
# allowing any import errors to propagate out
if QT_VERSION == 5:
from PyQt5.QtCore import Qt, QPointF, QSizeF
from PyQt5.QtGui import QBrush, QColor, QFont, QPainterPath, QPen
else:
from PyQt4.QtCore import Qt, QPointF, QSizeF
from PyQt4.QtGui import QBrush, QColor, QFont, QPainterPath, QPen
class SidesRectF(object):
'''
Trivial helper class for defining a rectangle with floating point
values for the left-x, top-y, right-x, and bottom-y edges.
'''
def __init__(self, left, top, right, bottom):
'''
Create a SidesRectF with the given left, top, right,
and bottom as float values.
'''
super(SidesRectF, self).__init__()
self.__left = float(left)
self.__top = float(top)
self.__right = float(right)
self.__bottom = float(bottom)
def left(self):
'''
Return the left value as a float.
'''
return self.__left
def setLeft(self, val):
'''
Set the SidesRectF left as a float value of the argument.
'''
self.__left = float(val)
def top(self):
'''
Return the top value as a float.
'''
return self.__top
def setTop(self, val):
'''
Set the SidesRectF top as a float value of the argument.
'''
self.__top = float(val)
def right(self):
'''
Return the right value as a float.
'''
return self.__right
def setRight(self, val):
'''
Set the SidesRectF right as a float value of the argument.
'''
self.__right = float(val)
def bottom(self):
'''
Return the bottom value as a float.
'''
return self.__bottom
def setBottom(self, val):
'''
Set the SidesRectF bottom as a float value of the argument.
'''
self.__bottom = float(val)
class SymbolPath(object):
'''
Trivial helper class for defining a symbol
'''
def __init__(self, painterpath, isfilled):
'''
Create a SymbolPath representing a symbol.
Arguments:
painterpath: the QPainterPath representing this symbol
isfilled: if True, the symbol should be drawn with a
solid brush; if False, the symbol should be
drawn with a solid pen
'''
super(SymbolPath, self).__init__()
self.__painterpath = painterpath
self.__isfilled = isfilled
def painterPath(self):
'''
Return the QPainterPath for this symbol
'''
return self.__painterpath
def isFilled(self):
'''
Return True if the symbol should be drawn with a solid brush;
return False if the symbol should be drawn with a solid pen.
'''
return self.__isfilled
class CmndHelperPQ(object):
'''
Helper class of static methods for dealing with commands
sent to a PyQt piped viewer.
'''
def __init__(self, viewer):
'''
Creates a cmndpipe command helper. The widget viewer
is only used for determining the default font and for
translation of error messages.
'''
super(CmndHelperPQ, self).__init__()
self.__viewer = viewer
self.__symbolpaths = { }
def getFontFromCmnd(self, fontinfo):
'''
Returns a QFont based on the information in the dictionary
fontinfo.
Recognized keys in the font dictionary are:
"family": font family name (string)
"size": text size in points (1/72 inches)
"italic": italicize? (False/True)
"bold": make bold? (False/True)
"underline": underline? (False/True)
'''
try:
myfont = QFont(fontinfo["family"])
except KeyError:
myfont = self.__viewer.font()
try:
myfont.setPointSizeF(fontinfo["size"])
except KeyError:
pass
try:
myfont.setItalic(fontinfo["italic"])
except KeyError:
pass
try:
myfont.setBold(fontinfo["bold"])
except KeyError:
pass
try:
myfont.setUnderline(fontinfo["underline"])
except KeyError:
pass
return myfont
def getBrushFromCmnd(self, brushinfo):
'''
Returns a QBrush based on the information in the dictionary
brushinfo. A ValueError is raised if the value for the
"style" key, if given, is not recognized.
Recognized keys in the fill dictionary are:
"color": color name or 24-bit RGB integer value
(eg, 0xFF0088)
"alpha": alpha value from 0 (transparent) to 255 (opaque)
"style": brush style name ("solid", "dense1" to "dense7",
"none", "hor", "ver", "cross",
"bdiag", "fdiag", "diagcross")
'''
try:
mycolor = self.getColorFromCmnd(brushinfo)
mybrush = QBrush(mycolor)
except KeyError:
mybrush = QBrush()
try:
mystyle = brushinfo["style"]
if mystyle == "solid":
mystyle = Qt.SolidPattern
elif mystyle == "dense1":
mystyle = Qt.Dense1Pattern
elif mystyle == "dense2":
mystyle = Qt.Dense2Pattern
elif mystyle == "dense3":
mystyle = Qt.Dense3Pattern
elif mystyle == "dense4":
mystyle = Qt.Dense4Pattern
elif mystyle == "dense5":
mystyle = Qt.Dense5Pattern
elif mystyle == "dense6":
mystyle = Qt.Dense6Pattern
elif mystyle == "dense7":
mystyle = Qt.Dense7Pattern
elif mystyle == "none":
mystyle = Qt.NoBrush
elif mystyle == "hor":
mystyle = Qt.HorPattern
elif mystyle == "ver":
mystyle = Qt.VerPattern
elif mystyle == "cross":
mystyle = Qt.CrossPattern
elif mystyle == "bdiag":
mystyle = Qt.BDiagPattern
elif mystyle == "fdiag":
mystyle = Qt.FDiagPattern
elif mystyle == "diagcross":
mystyle = Qt.DiagCrossPattern
else:
raise ValueError("Unknown brush style '%s'" % str(mystyle))
mybrush.setStyle(mystyle)
except KeyError:
pass
return mybrush
def getPenFromCmnd(self, peninfo):
'''
Returns a QPen based on the information in the dictionary
peninfo. A ValueError is raised if the value for the
"style", "capstyle", or "joinstyle" key, if given, is not
recognized.
Recognized keys in the outline dictionary are:
"color": color name or 24-bit RGB integer value
(eg, 0xFF0088)
"alpha": alpha value from 0 (transparent) to 255 (opaque)
"width": pen width in points (1/72 inches); possibly
further scaled by the width scaling factor
"style": pen style name ("solid", "dash", "dot", "dashdot",
"dashdotdot")
"capstyle": pen cap style name ("square", "flat", "round")
"joinstyle": pen join style name ("bevel", "miter", "round")
'''
try:
mycolor = self.getColorFromCmnd(peninfo)
mypen = QPen(mycolor)
except KeyError:
mypen = QPen()
try:
penwidth = float(peninfo["width"])
penwidth *= self.__viewer.widthScalingFactor()
mypen.setWidthF(penwidth)
except KeyError:
pass
try:
mystyle = peninfo["style"]
if mystyle == "solid":
mystyle = Qt.SolidLine
elif mystyle == "dash":
mystyle = Qt.DashLine
elif mystyle == "dot":
mystyle = Qt.DotLine
elif mystyle == "dashdot":
mystyle = Qt.DashDotLine
elif mystyle == "dashdotdot":
mystyle = Qt.DashDotDotLine
else:
raise ValueError("Unknown pen style '%s'" % str(mystyle))
mypen.setStyle(mystyle)
except KeyError:
pass
try:
mystyle = peninfo["capstyle"]
if mystyle == "square":
mystyle = Qt.SquareCap
elif mystyle == "flat":
mystyle = Qt.FlatCap
elif mystyle == "round":
mystyle = Qt.RoundCap
else:
raise ValueError("Unknown pen cap style '%s'" % str(mystyle))
mypen.setCapStyle(mystyle)
except KeyError:
pass
try:
mystyle = peninfo["joinstyle"]
if mystyle == "bevel":
mystyle = Qt.BevelJoin
elif mystyle == "miter":
mystyle = Qt.MiterJoin
elif mystyle == "round":
mystyle = Qt.RoundJoin
else:
raise ValueError("Unknown pen join style '%s'" % str(mystyle))
mypen.setJoinStyle(mystyle)
except KeyError:
pass
return mypen
def getSymbolFromCmnd(self, symbol):
'''
Returns a SymbolPath of the specified symbol.
Recognized symbols are:
'.' (period): filled circle
'o' (lowercase oh): unfilled circle
'+': plus mark
'x' (lowercase ex): x mark
'*': asterisk
'^': triangle
"#": square
The path is drawn for a 100 x 100 unit square where
the origin is in the center of the square.
'''
# check if this symbol has already been created
try:
sympath = self.__symbolpaths[symbol]
return sympath
except KeyError:
pass
# new symbol - create a SymbolPath for it
if symbol == '.':
path = QPainterPath()
path.addEllipse(-10.0, -10.0, 20.0, 20.0)
sympath = SymbolPath(path, True)
elif symbol == 'o':
path = QPainterPath()
path.addEllipse(-40.0, -40.0, 80.0, 80.0)
sympath = SymbolPath(path, False)
elif symbol == 'x':
path = QPainterPath( QPointF(-30.0, -30.0) )
path.lineTo( 30.0, 30.0)
path.moveTo(-30.0, 30.0)
path.lineTo( 30.0, -30.0)
sympath = SymbolPath(path, False)
elif symbol == '+':
path = QPainterPath( QPointF(0.0, -40.0) )
path.lineTo( 0.0, 40.0)
path.moveTo(-40.0, 0.0)
path.lineTo( 40.0, 0.0)
sympath = SymbolPath(path, False)
elif symbol == '*':
path = QPainterPath( QPointF(0.0, -40.0) )
path.lineTo( 0.0, 40.0)
path.moveTo(-34.641, -20.0)
path.lineTo( 34.641, 20.0)
path.moveTo(-34.641, 20.0)
path.lineTo( 34.641, -20.0)
sympath = SymbolPath(path, False)
elif symbol == '^':
path = QPainterPath( QPointF(-40.0, 30.0) )
path.lineTo( 0.0, -39.282)
path.lineTo(40.0, 30.0)
path.closeSubpath()
sympath = SymbolPath(path, False)
elif symbol == '#':
path = QPainterPath()
path.addRect(-35.0, -35.0, 70.0, 70.0)
sympath = SymbolPath(path, False)
else:
raise ValueError("Unrecognized symbol '%s'" % str(symbol))
# save and return the SymbolPath
self.__symbolpaths[symbol] = sympath
return sympath
def getSizeFromCmnd(self, sizeinfo):
'''
Returns a QSizeF based on the information in the dictionary
sizeinfo. Recognized keys are "width" and "height", and
correspond to those float values in the QSizeF. Values not
given in sizeinfo are assigned as zero in the returned QSizeF.
'''
myrect = QSizeF(0.0, 0.0)
try:
myrect.setWidth(float(sizeinfo["width"]))
except KeyError:
pass
try:
myrect.setHeight(float(sizeinfo["height"]))
except KeyError:
pass
return myrect
def getSidesFromCmnd(self, rectinfo):
'''
Returns a SidesQRectF based on the information in the dictionary
rectinfo. Recognized keys are "left", "top", "right", and "bottom",
and correspond to those float values in the SidesQRectF. Default
values: "left": 0.0, "top": 0.0, "right":1.0, "bottom":1.0
'''
myrect = SidesRectF(left=0.0, top=0.0, right=1.0, bottom=1.0)
try:
myrect.setLeft(float(rectinfo["left"]))
except KeyError:
pass
try:
myrect.setTop(float(rectinfo["top"]))
except KeyError:
pass
try:
myrect.setRight(float(rectinfo["right"]))
except KeyError:
pass
try:
myrect.setBottom(float(rectinfo["bottom"]))
except KeyError:
pass
return myrect
def getColorFromCmnd(self, colorinfo):
'''
Returns a QColor based on the information in the dictionary
colorinfo. Raises a KeyError if the "color" key is not given.
Recognized keys are:
"color": color name or 24-bit RGB integer value
(eg, 0xFF0088)
"alpha": alpha value from 0 (transparent) to 255 (opaque)
if viewer.ignoreAlpha True, this value is ignored
'''
colordata = colorinfo["color"]
mycolor = QColor(colordata)
if not mycolor.isValid():
raise ValueError("Invalid color '%s'" % str(colordata))
if not self.__viewer.ignoreAlpha():
try:
mycolor.setAlpha(int(colorinfo["alpha"]))
except KeyError:
pass
return mycolor
def computeARGB32PreMultInt(self, color):
'''
Returns the Format_ARGB32_Premultiplied integer value
of the given QColor.
'''
(redint, greenint, blueint, alphaint) = color.getRgb()
if self.__viewer.ignoreAlpha():
alphaint = 255
elif (alphaint < 255):
# Scale the RGB values by the alpha value
alphafactor = alphaint / 255.0
redint = int( redint * alphafactor + 0.5 )
if redint > alphaint:
redint = alphaint
greenint = int( greenint * alphafactor + 0.5 )
if greenint > alphaint:
greenint = alphaint
blueint = int( blueint * alphafactor + 0.5 )
if blueint > alphaint:
blueint = alphaint
fillint = ((alphaint * 256 + redint) * 256 + \
greenint) * 256 + blueint
return fillint
|