def extendedColorToQColor(s): if extendedcolor_re.match(s): col = qt4.QColor(s[:-2]) col.setAlpha(int(s[-2:], 16)) return col else: return qt4.QColor(s)
def apply(self, ifc, fields): """Do the color search and replace.""" col1 = qt4.QColor(fields['color1']) col2 = qt4.QColor(fields['color2']) def walkNodes(node): """Walk nodes, changing values.""" if node.type == 'setting' and node.settingtype == 'color': # only follow references if requested if node.isreference: if fields['follow']: node = node.resolveReference() else: return # evaluate into qcolor to make sure is a true match if qt4.QColor(node.val) == col1: node.val = fields['color2'] elif qt4.QColor(node.val) == col2: node.val = fields['color1'] else: for c in node.children: walkNodes(c) fromwidget = ifc.Root.fromPath(fields['widget']) walkNodes(fromwidget)
def brushExtFillPath(painter, extbrush, path, ignorehide=False, stroke=None): """Use an BrushExtended settings object to fill a path on painter. If ignorehide is True, ignore the hide setting on the brush object. stroke is an optional QPen for stroking outline of path """ if extbrush.hide and not ignorehide: if stroke is not None: painter.strokePath(path, stroke) return style = extbrush.style if style in _fillcnvt: # standard fill: use Qt styles for painting color = qt4.QColor(extbrush.color) color.setAlphaF((100 - extbrush.transparency) / 100.) brush = qt4.QBrush(color, _fillcnvt[style]) if stroke is None: painter.fillPath(path, brush) else: painter.save() painter.setPen(stroke) painter.setBrush(brush) painter.drawPath(path) painter.restore() elif style in _hatchmap: # fill with hatching if not extbrush.backhide: # background brush color = qt4.QColor(extbrush.backcolor) color.setAlphaF((100 - extbrush.backtransparency) / 100.) brush = qt4.QBrush(color) painter.fillPath(path, brush) color = qt4.QColor(extbrush.color) color.setAlphaF((100 - extbrush.transparency) / 100.) width = extbrush.get('linewidth').convert(painter) lstyle, dashpattern = extbrush.get('linestyle')._linecnvt[ extbrush.linestyle] pen = qt4.QPen(color, width, lstyle) if dashpattern: pen.setDashPattern(dashpattern) # do hatching with spacing spacing = extbrush.get('patternspacing').convert(painter) if spacing > 0: _hatcher(painter, pen, path, spacing, _hatchmap[style]) if stroke is not None: painter.strokePath(path, stroke)
def color(self, name): """Get a color setting as a QColor.""" val = self.database['color_' + name] if val[0]: default = self.color_defaults[name] if default == 'LightBase': base = qt4.qApp.palette().color(qt4.QPalette.Base) if base.value() < 127: base = qt4.QColor(qt4.Qt.white) return base return qt4.QColor(default) else: return qt4.QColor(val[1])
def render(self): """Render the text.""" if self.calcbounds is None: self.getBounds() p = self.painter p.save() if self.mmldoc is not None: p.translate(self.xi, self.yi) p.rotate(self.angle) # is drawn from bottom of box, not top p.translate(0, -self.size.height()) p.scale(self.drawscale, self.drawscale) self.record.play(p) else: # display an error - must be a better way to do this p.setFont(qt4.QFont()) p.setPen(qt4.QPen(qt4.QColor("red"))) p.drawText( qt4.QRectF(self.xi, self.yi, 200, 200), qt4.Qt.AlignLeft | qt4.Qt.AlignTop | qt4.Qt.TextWordWrap, self.error) p.restore() return self.calcbounds
def walkNodes(node): """Walk nodes, changing values.""" if node.type == 'setting' and node.settingtype == 'color': # only follow references if requested if node.isreference: if fields['follow']: node = node.resolveReference() else: return # evaluate into qcolor to make sure is a true match if qt4.QColor(node.val) == col1: node.val = fields['color2'] elif qt4.QColor(node.val) == col2: node.val = fields['color1'] else: for c in node.children: walkNodes(c)
def apply(self, ifc, fields): """Do the randomizing.""" fromwidget = ifc.Root.fromPath(fields['widget']) col1 = qt4.QColor(fields['color1']) col2 = qt4.QColor(fields['color2']) if fields['randxy']: for node in fromwidget.WalkWidgets(widgettype='xy'): col = self.getRandomColor(col1, col2) node.PlotLine.color.val = col node.MarkerFill.color.val = col node.ErrorBarLine.color.val = col if fields['randfunc']: for node in fromwidget.WalkWidgets(widgettype='function'): node.Line.color.val = self.getRandomColor(col1, col2)
def render(self, state): painter = state.painter pen = painter.pen() oldcolor = pen.color() pen.setColor(qt4.QColor(self.colorname)) painter.setPen(pen) Part.render(self, state) pen.setColor(oldcolor) painter.setPen(pen)
def apply(self, ifc, fields): """Do the randomizing.""" fromwidget = ifc.Root.fromPath(fields['widget']) col1 = qt4.QColor(fields['color1']) col2 = qt4.QColor(fields['color2']) H1, H2 = col1.hue(), col2.hue() S1, S2 = col1.saturation(), col2.saturation() V1, V2 = col1.value(), col2.value() # add up total number of widgets numwidgets = (len(list(fromwidget.WalkWidgets(widgettype='xy'))) + len(list(fromwidget.WalkWidgets(widgettype='function')))) def colatidx(i): """Get color in range 0...numwidgets-1.""" div = float(max(numwidgets - 1, 1)) H = i * (H2 - H1) / div + H1 S = i * (S2 - S1) / div + S1 V = i * (V2 - V1) / div + V1 return str(qt4.QColor.fromHsv(H, S, V).name()) idx = 0 for node in fromwidget.WalkWidgets(): t = node.widgettype if fields['randxy'] and t == 'xy': col = colatidx(idx) idx += 1 node.PlotLine.color.val = col node.MarkerFill.color.val = col node.ErrorBarLine.color.val = col if fields['randfunc'] and t == 'function': node.Line.color.val = colatidx(idx) idx += 1
def makeQPen(self, painthelper): '''Make a QPen from the description. This currently ignores the hide attribute ''' color = qt4.QColor(self.color) color.setAlphaF((100 - self.transparency) / 100.) width = self.get('width').convert(painthelper) style, dashpattern = setting.LineStyle._linecnvt[self.style] pen = qt4.QPen(color, width, style) if dashpattern: pen.setDashPattern(dashpattern) return pen
def identifyWidgetAtPoint(self, x, y, antialias=True): """What widget has drawn at the point x,y? Returns the widget drawn last on the point, or None if it is an empty part of the page. root is the root widget to recurse from if antialias is true, do test for antialiased drawing """ # make a small image filled with a specific color box = 3 specialcolor = qt4.QColor(254, 255, 254) origpix = qt4.QPixmap(2*box+1, 2*box+1) origpix.fill(specialcolor) origimg = origpix.toImage() # store most recent widget here lastwidget = [None] def rendernextstate(state): """Recursively draw painter. Checks whether drawing a widgetchanges the small image around the point given. """ pixmap = qt4.QPixmap(origpix) painter = qt4.QPainter(pixmap) painter.setRenderHint(qt4.QPainter.Antialiasing, antialias) painter.setRenderHint(qt4.QPainter.TextAntialiasing, antialias) # this makes the small image draw from x-box->x+box, y-box->y+box # translate would get overriden by coordinate system playback painter.setWindow(x-box,y-box,box*2+1,box*2+1) state.record.play(painter) painter.end() newimg = pixmap.toImage() if newimg != origimg: lastwidget[0] = state.widget for child in state.children: rendernextstate(child) rendernextstate(self.rootstate) return lastwidget[0]
def setupColorTab(self): """Initialise color tab this makes a grid of controls for each color consisting of label, isdefault check and change color button.""" self.chosencolors = {} self.colorbutton = {} self.colordefaultcheck = {} layout = qt4.QGridLayout() setdb = setting.settingdb for row, colname in enumerate(setdb.colors): isdefault, colval = setting.settingdb['color_%s' % colname] self.chosencolors[colname] = qt4.QColor(colval) # label name, tooltip = color_names[colname] label = qt4.QLabel(name) label.setToolTip(tooltip) layout.addWidget(label, row, 0) # is default check defcheck = qt4.QCheckBox(_("Default")) defcheck.setToolTip( _("Use the default color instead of the one chosen here")) layout.addWidget(defcheck, row, 1) self.colordefaultcheck[colname] = defcheck defcheck.setChecked(isdefault) # button button = qt4.QPushButton() # connect button to method to change color def clicked(color=colname): self.colorButtonClickedSlot(color) self.connect(button, qt4.SIGNAL('clicked()'), clicked) layout.addWidget(button, row, 2) self.colorbutton[colname] = button self.colorGroup.setLayout(layout) self.updateButtonColors()
def makeQPen(self): """ Return a qt4.QPen object for the font pen """ return qt4.QPen(qt4.QColor(self.color))
def makeQBrush(self): '''Make a qbrush from the settings.''' color = qt4.QColor(self.color) color.setAlphaF((100 - self.transparency) / 100.) return qt4.QBrush(color, self.get('style').qtStyle())