Changeset 8400:78f258d26776 in orange


Ignore:
Timestamp:
07/20/11 11:13:58 (3 years ago)
Author:
Noughmad <Noughmad@…>
Branch:
default
Convert:
590df03be21a2ffb16791af85c74c1cb3f829773
Message:

Add working tooltips

Location:
orange/OrangeWidgets/plot
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeWidgets/plot/owplot.py

    r8387 r8400  
    117117        # Method aliases, because there are some methods with different names but same functions 
    118118        self.setCanvasBackground = self.setCanvasColor 
     119        self.map_from_widget = self.mapToScene 
    119120         
    120121        # OWScatterPlot needs these: 
     
    125126        self.curveSymbols = self.palette.curve_symbols 
    126127        self.tips = TooltipManager(self) 
     128        self.setMouseTracking(True) 
    127129         
    128130        self.state = NOTHING 
     
    551553        self.static_click = True 
    552554        self._pressed_mouse_button = event.button() 
    553         point = self.map_from_widget(event.pos()) 
     555        point = self.mapToScene(event.pos()) 
    554556        if event.button() == Qt.LeftButton and self.state == SELECT_RECTANGLE and self.graph_area.contains(point): 
    555             self._selection_start_point = self.map_from_widget(event.pos()) 
     557            self._selection_start_point = self.mapToScene(event.pos()) 
    556558            self._current_rs_item = QGraphicsRectItem(parent=self.graph_item, scene=self.scene()) 
    557559             
     
    562564        if event.buttons(): 
    563565            self.static_click = False 
    564         point = self.map_from_widget(event.pos()) 
     566        point = self.mapToScene(event.pos()) 
    565567        if self._pressed_mouse_button == Qt.LeftButton: 
    566568            if self.state == SELECT_RECTANGLE and self._current_rs_item and self.graph_area.contains(point): 
    567569                self._current_rs_item.setRect(QRectF(self._selection_start_point, point).normalized()) 
    568         if not self._pressed_mouse_button and self.state == SELECT_POLYGON and self._current_ps_item: 
     570        elif not self._pressed_mouse_button and self.state == SELECT_POLYGON and self._current_ps_item: 
    569571            self._current_ps_polygon[-1] = point 
    570572            self._current_ps_item.setPolygon(self._current_ps_polygon) 
     
    576578            else: 
    577579                self._current_ps_item.setPen(QPen(Qt.black)) 
    578              
     580        else: 
     581            t, ok = (self.transform_for_axes(xBottom, yLeft) * self.zoom_transform).inverted() 
     582            if ok: 
     583                p = t.map(point) 
     584                text, x, y = self.tips.maybeTip(p.x(), p.y()) 
     585                if type(text) == int:  
     586                    text = self.buildTooltip(text) 
     587                if text and x is not None and y is not None: 
     588                    tp = self.mapFromScene(QPointF(x,y) * self.map_transform) 
     589                    self.showTip(tp.x(), tp.y(), text) 
     590            
    579591    def mouseReleaseEvent(self, event): 
    580592        if self.mouseReleaseEventHandler and self.mouseReleaseEventHandler(event): 
     
    590602     
    591603    def mouseStaticClick(self, event): 
    592         point = self.map_from_widget(event.pos()) 
     604        point = self.mapToScene(event.pos()) 
    593605        if self.state == ZOOMING: 
    594606            t, ok = self.zoom_transform.inverted() 
     
    695707            self._current_ps_item = None 
    696708         
    697     def map_from_widget(self, point): 
    698         return self.mapToScene(point) - self.graph_item.pos() 
    699          
    700709    def get_selected_points(self, xData, yData, validData): 
    701710        region = QRegion() 
     
    816825        else: 
    817826            return None 
     827             
     828    # #################################################################### 
     829    # return string with attribute names and their values for example example 
     830    def getExampleTooltipText(self, example, indices = None, maxIndices = 20): 
     831        if indices and type(indices[0]) == str: 
     832            indices = [self.attributeNameIndex[i] for i in indices] 
     833        if not indices:  
     834            indices = range(len(self.dataDomain.attributes)) 
     835         
     836        # don't show the class value twice 
     837        if example.domain.classVar: 
     838            classIndex = self.attributeNameIndex[example.domain.classVar.name] 
     839            while classIndex in indices: 
     840                indices.remove(classIndex)       
     841       
     842        text = "<b>Attributes:</b><br>" 
     843        for index in indices[:maxIndices]: 
     844            attr = self.attributeNames[index] 
     845            if attr not in example.domain:  text += "&nbsp;"*4 + "%s = ?<br>" % (Qt.escape(attr)) 
     846            elif example[attr].isSpecial(): text += "&nbsp;"*4 + "%s = ?<br>" % (Qt.escape(attr)) 
     847            else:                           text += "&nbsp;"*4 + "%s = %s<br>" % (Qt.escape(attr), Qt.escape(str(example[attr]))) 
     848        if len(indices) > maxIndices: 
     849            text += "&nbsp;"*4 + " ... <br>" 
     850 
     851        if example.domain.classVar: 
     852            text = text[:-4] 
     853            text += "<hr><b>Class:</b><br>" 
     854            if example.getclass().isSpecial(): text += "&nbsp;"*4 + "%s = ?<br>" % (Qt.escape(example.domain.classVar.name)) 
     855            else:                              text += "&nbsp;"*4 + "%s = %s<br>" % (Qt.escape(example.domain.classVar.name), Qt.escape(str(example.getclass()))) 
     856 
     857        if len(example.domain.getmetas()) != 0: 
     858            text = text[:-4] 
     859            text += "<hr><b>Meta attributes:</b><br>" 
     860            # show values of meta attributes 
     861            for key in example.domain.getmetas(): 
     862                try: text += "&nbsp;"*4 + "%s = %s<br>" % (Qt.escape(example.domain[key].name), Qt.escape(str(example[key]))) 
     863                except: pass 
     864        return text[:-4]        # remove the last <br> 
     865 
     866    # show a tooltip at x,y with text. if the mouse will move for more than 2 pixels it will be removed 
     867    def showTip(self, x, y, text): 
     868        QToolTip.showText(self.mapToGlobal(QPoint(x, y)), text, self, QRect(x-3,y-3,6,6)) 
  • orange/OrangeWidgets/plot/owtools.py

    r8388 r8400  
    3939        dists = [max(abs(x-position[0])- position[2],0) + max(abs(y-position[1])-position[3], 0) for position in self.positions] 
    4040        nearestIndex = dists.index(min(dists)) 
    41  
    42         intX = abs(self.graph.transform(self.graph.xBottom, x) - self.graph.transform(self.graph.xBottom, self.positions[nearestIndex][0])) 
    43         intY = abs(self.graph.transform(self.graph.yLeft, y) - self.graph.transform(self.graph.yLeft, self.positions[nearestIndex][1])) 
     41         
     42        intX = abs(self.graph.transform(xBottom, x) - self.graph.transform(xBottom, self.positions[nearestIndex][0])) 
     43        intY = abs(self.graph.transform(yLeft, y) - self.graph.transform(yLeft, self.positions[nearestIndex][1])) 
    4444        if self.positions[nearestIndex][2] == 0 and self.positions[nearestIndex][3] == 0:   # if we specified no custom range then assume 6 pixels 
    4545            if intX + intY < 6:  return (self.texts[nearestIndex], self.positions[nearestIndex][0], self.positions[nearestIndex][1]) 
Note: See TracChangeset for help on using the changeset viewer.