Changeset 8202:7cfd9af8bb2b in orange


Ignore:
Timestamp:
08/17/11 21:10:39 (3 years ago)
Author:
Noughmad <Noughmad@…>
Branch:
default
Convert:
78077b04fabf4126a8aa28d75ecfab16037bcbde
Message:

Make Qwt dependency in OWDlgs optional, add support for QGraphicsView-based graphs there, and fill the background according to the plot's background color.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeWidgets/OWDlgs.py

    r8181 r8202  
    22from OWBaseWidget import * 
    33import OWGUI 
    4 from plot.owplot import * 
     4 
     5_have_qwt = True 
     6try: 
     7    from PyQt4.Qwt5 import * 
     8except ImportError: 
     9    _have_qwt = False 
     10 
    511from PyQt4.QtSvg import * 
    612from ColorPalette import * 
     
    2935 
    3036        box = OWGUI.widgetBox(self.space, "Image Size") 
    31         if isinstance(graph, OWPlot): 
     37 
     38        global _have_qwt 
     39        if _have_qwt and isinstance(graph, QwtPlot): 
    3240            size = OWGUI.radioButtonsInBox(box, self, "selectedSize", ["Current size", "400 x 400", "600 x 600", "800 x 800", "Custom:"], callback = self.updateGUI) 
    3341            self.customXEdit = OWGUI.lineEdit(OWGUI.indentedBox(box), self, "customX", "Width: ", orientation = "horizontal", valueType = int) 
    3442            self.customYEdit = OWGUI.lineEdit(OWGUI.indentedBox(box), self, "customY", "Height:", orientation = "horizontal", valueType = int) 
    3543            OWGUI.comboBoxWithCaption(self.space, self, "penWidthFactor", label = 'Factor:   ', box = " Pen width multiplication factor ",  tooltip = "Set the pen width factor for all curves in the plot\n(Useful for example when the lines in the plot look to thin)\nDefault: 1", sendSelectedValue = 1, valueType = int, items = range(1,20)) 
    36         elif isinstance(graph, QGraphicsScene): 
     44        elif isinstance(graph, QGraphicsScene) or isinstance(graph, QGraphicsView): 
    3745            OWGUI.widgetLabel(box, "Image size will be set automatically.") 
    3846 
     
    5866            ext = ".png"                                        # if no format was specified, we choose png 
    5967        filename = fil + ext 
     68         
     69        real_graph = self.graph if isinstance(self.graph, QGraphicsView) else None 
     70        if real_graph: 
     71            self.graph = self.graph.scene()             
    6072 
    6173        if isinstance(self.graph, QGraphicsScene): 
    6274            source = self.getSceneBoundingRect().adjusted(-15, -15, 15, 15) 
     75            size = source.size() 
     76        elif isinstance(self.graph, QGraphicsView): 
     77            source = self.graph.sceneRect() 
    6378            size = source.size() 
    6479        elif not size: 
     
    7590        painter.setRenderHint(QPainter.Antialiasing) 
    7691        if not filename.lower().endswith(".svg"): 
    77             painter.fillRect(buffer.rect(), QBrush(Qt.white)) # make background same color as the widget's background 
    78  
    79         if isinstance(self.graph, OWPlot): 
     92            if isinstance(self.graph, QGraphicsScene) or isinstance(self.graph, QGraphicsViev): 
     93                # make background same color as the widget's background 
     94                brush = self.graph.backgroundBrush() 
     95                if brush.style() == Qt.NoBrush: 
     96                    brush = QBrush(self.graph.palette().color(QPalette.Base)) 
     97                painter.fillRect(buffer.rect(), brush) 
     98            else: 
     99                painter.fillRect(buffer.rect(), QBrush(Qt.white)) 
     100 
     101        # qwt plot 
     102        global _have_qwt 
     103        if _have_qwt and isinstance(self.graph, QwtPlot): 
     104 
    80105            if self.penWidthFactor != 1: 
    81106                for curve in self.graph.itemList(): 
     
    89114 
    90115        # QGraphicsScene 
    91         elif isinstance(self.graph, QGraphicsScene): 
     116        elif isinstance(self.graph, QGraphicsScene) or isinstance(self.graph, QGraphicsView): 
    92117            target = QRectF(0,0, source.width(), source.height()) 
    93118            self.graph.render(painter, target, source) 
     
    98123        if closeDialog: 
    99124            QDialog.accept(self) 
    100  
    101125 
    102126    def getSceneBoundingRect(self): 
     
    110134        filename = self.getFileName(self.defaultName, "Python Script (*.py)", ".py") 
    111135        if filename: 
    112             if isinstance(self.graph, OWPlot): 
     136            global _have_qwt 
     137            if _have_qwt and isinstance(self.graph, QwtPlot): 
    113138                self.graph.saveToMatplotlib(filename, self.getSize()) 
    114139            else: 
     
    118143                f.write("# This Python file uses the following encoding: utf-8\n") 
    119144                f.write("from pylab import *\nfrom matplotlib.patches import Rectangle\n\n#constants\nx1 = %f; x2 = %f\ny1 = 0.0; y2 = %f\ndpi = 80\nxsize = %d\nysize = %d\nedgeOffset = 0.01\n\nfigure(facecolor = 'w', figsize = (xsize/float(dpi), ysize/float(dpi)), dpi = dpi)\na = gca()\nhold(True)\n" % (minx, maxx, maxy, maxx-minx, maxy-miny)) 
    120  
    121                 sortedList = [(item.zValue(), item) for item in self.graph.items()] 
     145                 
     146                if isinstance(self.graph, QGraphicsView): 
     147                    scene = self.graph.scene() 
     148                else: 
     149                    scene = self.graph 
     150                 
     151                sortedList = [(item.zValue(), item) for item in scene.items()] 
    122152                sortedList.sort()   # sort items by z value 
    123153 
     
    187217 
    188218    def updateGUI(self): 
    189         if isinstance(self.graph, OWPlot): 
     219        global _have_qwt 
     220        if _have_qwt and isinstance(self.graph, QwtPlot): 
    190221            self.customXEdit.setEnabled(self.selectedSize == 4) 
    191222            self.customYEdit.setEnabled(self.selectedSize == 4) 
     
    197228        col = tuple([v/float(255) for v in col]) 
    198229        return col, obj.color().alpha()/float(255) 
    199  
    200  
    201  
    202  
Note: See TracChangeset for help on using the changeset viewer.