source: orange/Orange/OrangeCanvas/orngDlgs.py @ 11018:b7cbf2b86522

Revision 11018:b7cbf2b86522, 57.1 KB checked in by Matija Polajnar <matija.polajnar@…>, 17 months ago (diff)

Rewrite the add-on support modules and GUI to support the new properly packed add-ons, published on PyPI.

Line 
1# Author: Gregor Leban (gregor.leban@fri.uni-lj.si)
2# Description:
3#    signal dialog, canvas options dialog
4
5from PyQt4.QtCore import *
6from PyQt4.QtGui import *
7from orngCanvasItems import MyCanvasText
8import OWGUI, sys, os
9
10# this class is needed by signalDialog to show widgets and lines
11class SignalCanvasView(QGraphicsView):
12    def __init__(self, dlg, canvasDlg, *args):
13        apply(QGraphicsView.__init__,(self,) + args)
14        self.dlg = dlg
15        self.canvasDlg = canvasDlg
16        self.bMouseDown = False
17        self.tempLine = None
18        self.inWidget = None
19        self.outWidget = None
20        self.inWidgetIcon = None
21        self.outWidgetIcon = None
22        self.lines = []
23        self.outBoxes = []
24        self.inBoxes = []
25        self.texts = []
26        self.ensureVisible(0,0,1,1)
27        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
28        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
29        self.setAlignment(Qt.AlignLeft | Qt.AlignTop)
30        self.setRenderHint(QPainter.Antialiasing)
31
32    def addSignalList(self, outWidget, inWidget):
33        self.scene().clear()
34        outputs, inputs = outWidget.widgetInfo.outputs, inWidget.widgetInfo.inputs
35        outIcon, inIcon = self.canvasDlg.getWidgetIcon(outWidget.widgetInfo), self.canvasDlg.getWidgetIcon(inWidget.widgetInfo)
36        self.lines = []
37        self.outBoxes = []
38        self.inBoxes = []
39        self.texts = []
40        xSpaceBetweenWidgets = 100  # space between widgets
41        xWidgetOff = 10             # offset for widget position
42        yWidgetOffTop = 10          # offset for widget position
43        yWidgetOffBottom = 30       # offset for widget position
44        ySignalOff = 10             # space between the top of the widget and first signal
45        ySignalSpace = 50           # space between two neighbouring signals
46        ySignalSize = 20            # height of the signal box
47        xSignalSize = 20            # width of the signal box
48        xIconOff = 10
49        iconSize = 48
50
51        count = max(len(inputs), len(outputs))
52        height = max ((count)*ySignalSpace, 70)
53
54        # calculate needed sizes of boxes to show text
55        maxLeft = 0
56        for i in range(len(inputs)):
57            maxLeft = max(maxLeft, self.getTextWidth("("+inputs[i].name+")", 1))
58            maxLeft = max(maxLeft, self.getTextWidth(inputs[i].type))
59
60        maxRight = 0
61        for i in range(len(outputs)):
62            maxRight = max(maxRight, self.getTextWidth("("+outputs[i].name+")", 1))
63            maxRight = max(maxRight, self.getTextWidth(outputs[i].type))
64
65        width = max(maxLeft, maxRight) + 70 # we add 70 to show icons beside signal names
66
67        # show boxes
68        pen = QPen(QBrush(QColor(125, 162, 206, 192)), 1, Qt.SolidLine, Qt.RoundCap)
69        brush = QBrush(QColor(217, 232, 252, 192))
70        self.outWidget = QGraphicsRectItem(xWidgetOff, yWidgetOffTop, width, height, None, self.dlg.canvas)
71        self.outWidget.setBrush(brush)
72        self.outWidget.setPen(pen)
73        self.outWidget.setZValue(-100)
74
75        self.inWidget = QGraphicsRectItem(xWidgetOff + width + xSpaceBetweenWidgets, yWidgetOffTop, width, height, None, self.dlg.canvas)
76        self.inWidget.setBrush(brush)
77        self.inWidget.setPen(pen)
78        self.inWidget.setZValue(-100)
79       
80        canvasPicsDir  = os.path.join(self.canvasDlg.canvasDir, "icons")
81        if os.path.exists(os.path.join(canvasPicsDir, "frame.png")):
82            widgetBack = QPixmap(os.path.join(canvasPicsDir, "frame.png"))
83        else:
84            widgetBack = outWidget.imageFrame
85
86        # if icons -> show them
87        if outIcon:
88#            frame = QGraphicsPixmapItem(widgetBack, None, self.dlg.canvas)
89#            frame.setPos(xWidgetOff + xIconOff, yWidgetOffTop + height/2.0 - frame.pixmap().width()/2.0)
90            self.outWidgetIcon = QGraphicsPixmapItem(outIcon.pixmap(iconSize, iconSize), None, self.dlg.canvas)
91#            self.outWidgetIcon.setPos(xWidgetOff + xIconOff, yWidgetOffTop + height/2.0 - self.outWidgetIcon.pixmap().width()/2.0)
92            self.outWidgetIcon.setPos(xWidgetOff + xIconOff, yWidgetOffTop + xIconOff)
93       
94        if inIcon:
95#            frame = QGraphicsPixmapItem(widgetBack, None, self.dlg.canvas)
96#            frame.setPos(xWidgetOff + xSpaceBetweenWidgets + 2*width - xIconOff - frame.pixmap().width(), yWidgetOffTop + height/2.0 - frame.pixmap().width()/2.0)
97            self.inWidgetIcon = QGraphicsPixmapItem(inIcon.pixmap(iconSize, iconSize), None, self.dlg.canvas)
98#            self.inWidgetIcon.setPos(xWidgetOff + xSpaceBetweenWidgets + 2*width - xIconOff - self.inWidgetIcon.pixmap().width(), yWidgetOffTop + height/2.0 - self.inWidgetIcon.pixmap().width()/2.0)
99            self.inWidgetIcon.setPos(xWidgetOff + xSpaceBetweenWidgets + 2*width - xIconOff - self.inWidgetIcon.pixmap().width(), yWidgetOffTop + xIconOff)
100
101        # show signal boxes and text labels
102        #signalSpace = (count)*ySignalSpace
103        signalSpace = height
104        for i in range(len(outputs)):
105            y = yWidgetOffTop + ((i+1)*signalSpace)/float(len(outputs)+1)
106            box = QGraphicsRectItem(xWidgetOff + width, y - ySignalSize/2.0, xSignalSize, ySignalSize, None, self.dlg.canvas)
107            box.setBrush(QBrush(QColor(0,0,255)))
108            box.setZValue(200)
109            self.outBoxes.append((outputs[i].name, box))
110
111            self.texts.append(MyCanvasText(self.dlg.canvas, outputs[i].name, xWidgetOff + width - 8, y - 7, Qt.AlignRight | Qt.AlignVCenter, bold =1, show=1))
112            self.texts.append(MyCanvasText(self.dlg.canvas, outputs[i].type, xWidgetOff + width - 8, y + 7, Qt.AlignRight | Qt.AlignVCenter, bold =0, show=1))
113
114        for i in range(len(inputs)):
115            y = yWidgetOffTop + ((i+1)*signalSpace)/float(len(inputs)+1)
116            box = QGraphicsRectItem(xWidgetOff + width + xSpaceBetweenWidgets - xSignalSize, y - ySignalSize/2.0, xSignalSize, ySignalSize, None, self.dlg.canvas)
117            box.setBrush(QBrush(QColor(0,0,255)))
118            box.setZValue(200)
119            self.inBoxes.append((inputs[i].name, box))
120
121            self.texts.append(MyCanvasText(self.dlg.canvas, inputs[i].name, xWidgetOff + width + xSpaceBetweenWidgets + 8, y - 7, Qt.AlignLeft | Qt.AlignVCenter, bold =1, show=1))
122            self.texts.append(MyCanvasText(self.dlg.canvas, inputs[i].type, xWidgetOff + width + xSpaceBetweenWidgets + 8, y + 7, Qt.AlignLeft | Qt.AlignVCenter, bold =0, show=1))
123
124        self.texts.append(MyCanvasText(self.dlg.canvas, outWidget.caption, xWidgetOff + width/2.0, yWidgetOffTop + height + 5, Qt.AlignHCenter | Qt.AlignTop, bold =1, show=1))
125        self.texts.append(MyCanvasText(self.dlg.canvas, inWidget.caption, xWidgetOff + width* 1.5 + xSpaceBetweenWidgets, yWidgetOffTop + height + 5, Qt.AlignHCenter | Qt.AlignTop, bold =1, show=1))
126
127        return (2*xWidgetOff + 2*width + xSpaceBetweenWidgets, yWidgetOffTop + height + yWidgetOffBottom)
128
129    def getTextWidth(self, text, bold = 0):
130        temp = QGraphicsSimpleTextItem(text, None, self.dlg.canvas)
131        if bold:
132            font = temp.font()
133            font.setBold(1)
134            temp.setFont(font)
135        temp.hide()
136        return temp.boundingRect().width()
137
138    # ###################################################################
139    # mouse button was pressed
140    def mousePressEvent(self, ev):
141        self.bMouseDown = 1
142        point = self.mapToScene(ev.pos())
143        activeItem = self.scene().itemAt(QPointF(ev.pos()))
144        if type(activeItem) == QGraphicsRectItem and activeItem not in [self.outWidget, self.inWidget]:
145            self.tempLine = QGraphicsLineItem(None, self.dlg.canvas)
146            self.tempLine.setLine(point.x(), point.y(), point.x(), point.y())
147            self.tempLine.setPen(QPen(QColor(200, 200, 200), 1))
148            self.tempLine.setZValue(-300)
149           
150        elif type(activeItem) == QGraphicsLineItem:
151            for (line, outName, inName, outBox, inBox) in self.lines:
152                if line == activeItem:
153                    self.dlg.removeLink(outName, inName)
154                    return
155
156    # ###################################################################
157    # mouse button was released #########################################
158    def mouseMoveEvent(self, ev):
159        if self.tempLine:
160            curr = self.mapToScene(ev.pos())
161            start = self.tempLine.line().p1()
162            self.tempLine.setLine(start.x(), start.y(), curr.x(), curr.y())
163            self.scene().update()
164
165    # ###################################################################
166    # mouse button was released #########################################
167    def mouseReleaseEvent(self, ev):
168        if self.tempLine:
169            activeItem = self.scene().itemAt(QPointF(ev.pos()))
170            if type(activeItem) == QGraphicsRectItem:
171                activeItem2 = self.scene().itemAt(self.tempLine.line().p1())
172                if activeItem.x() < activeItem2.x(): outBox = activeItem; inBox = activeItem2
173                else:                                outBox = activeItem2; inBox = activeItem
174                outName = None; inName = None
175                for (name, box) in self.outBoxes:
176                    if box == outBox: outName = name
177                for (name, box) in self.inBoxes:
178                    if box == inBox: inName = name
179                if outName != None and inName != None:
180                    self.dlg.addLink(outName, inName)
181
182            self.tempLine.hide()
183            self.tempLine = None
184            self.scene().update()
185
186
187    def addLink(self, outName, inName):
188        outBox = None; inBox = None
189        for (name, box) in self.outBoxes:
190            if name == outName: outBox = box
191        for (name, box) in self.inBoxes:
192            if name == inName : inBox  = box
193        if outBox == None or inBox == None:
194            print "error adding link. Data = ", outName, inName
195            return
196        line = QGraphicsLineItem(None, self.dlg.canvas)
197        outRect = outBox.rect()
198        inRect = inBox.rect()
199        line.setLine(outRect.x() + outRect.width()-2, outRect.y() + outRect.height()/2.0, inRect.x()+2, inRect.y() + inRect.height()/2.0)
200        line.setPen(QPen(QColor(160, 160, 160), 5))
201        line.setZValue(100)
202        self.scene().update()
203        self.lines.append((line, outName, inName, outBox, inBox))
204
205
206    def removeLink(self, outName, inName):
207        for (line, outN, inN, outBox, inBox) in self.lines:
208            if outN == outName and inN == inName:
209                line.hide()
210                self.lines.remove((line, outN, inN, outBox, inBox))
211                self.scene().update()
212                return
213
214
215# #######################################
216# # Signal dialog - let the user select active signals between two widgets
217# #######################################
218class SignalDialog(QDialog):
219    def __init__(self, canvasDlg, *args):
220        QDialog.__init__(self, *args)
221        self.setAttribute(Qt.WA_DeleteOnClose, True)
222        self.canvasDlg = canvasDlg
223
224        self.signals = []
225        self._links = []
226        self.allSignalsTaken = 0
227        self.signalManager = self.canvasDlg.schema.signalManager
228
229        # GUI
230        self.setWindowTitle('Connect Signals')
231        self.setLayout(QVBoxLayout())
232
233        self.canvasGroup = OWGUI.widgetBox(self, 1)
234        self.canvas = QGraphicsScene(0,0,1000,1000)
235        self.canvasView = SignalCanvasView(self, self.canvasDlg, self.canvas, self.canvasGroup)
236        self.canvasGroup.layout().addWidget(self.canvasView)
237
238        buttons = OWGUI.widgetBox(self, orientation = "horizontal", sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed))
239
240        self.buttonHelp = OWGUI.button(buttons, self, "&Help")
241        buttons.layout().addStretch(1)
242        self.buttonClearAll = OWGUI.button(buttons, self, "Clear &All", callback = self.clearAll)
243        self.buttonOk = OWGUI.button(buttons, self, "&OK", callback = self.accept)
244        self.buttonOk.setAutoDefault(1)
245        self.buttonOk.setDefault(1)
246        self.buttonCancel = OWGUI.button(buttons, self, "&Cancel", callback = self.reject)
247
248    def clearAll(self):
249        while self._links != []:
250            self.removeLink(self._links[0][0], self._links[0][1])
251
252    def setOutInWidgets(self, outWidget, inWidget):
253        self.outWidget = outWidget
254        self.inWidget = inWidget
255        (width, height) = self.canvasView.addSignalList(outWidget, inWidget)
256        self.canvas.setSceneRect(0, 0, width, height)
257        self.resize(width+50, height+80)
258
259    def countCompatibleConnections(self, outputs, inputs, outInstance, inInstance, outType, inType):
260        count = 0
261        for outS in outputs:
262            if outInstance.getOutputType(outS.name) == None: continue  # ignore if some signals don't exist any more, since we will report error somewhere else
263            if not issubclass(outInstance.getOutputType(outS.name), outType): continue
264            for inS in inputs:
265                if inInstance.getOutputType(inS.name) == None: continue  # ignore if some signals don't exist any more, since we will report error somewhere else
266                if not issubclass(inType, inInstance.getInputType(inS.name)): continue
267                if issubclass(outInstance.getOutputType(outS.name), inInstance.getInputType(inS.name)): count+= 1
268
269        return count
270
271    def existsABetterLink(self, outSignal, inSignal, outSignals, inSignals):
272        existsBetter = 0
273
274        betterOutSignal = None; betterInSignal = None
275        for outS in outSignals:
276            for inS in inSignals:
277                if (outS.name != outSignal.name and outS.name == inSignal.name and outS.type == inSignal.type) or (inS.name != inSignal.name and inS.name == outSignal.name and inS.type == outSignal.type):
278                    existsBetter = 1
279                    betterOutSignal = outS
280                    betterInSignal = inS
281        return existsBetter, betterOutSignal, betterInSignal
282
283
284    def getPossibleConnections(self, outputs, inputs):
285        possibleLinks = []
286        for outS in outputs:
287            outType = self.outWidget.instance.getOutputType(outS.name)
288            if outType == None:     #print "Unable to find signal type for signal %s. Check the definition of the widget." % (outS.name)
289                continue
290            for inS in inputs:
291                inType = self.inWidget.instance.getInputType(inS.name)
292                if inType == None:
293                    continue        #print "Unable to find signal type for signal %s. Check the definition of the widget." % (inS.name)
294                if issubclass(outType, inType):
295                    possibleLinks.append((outS.name, inS.name))
296                if issubclass(inType, outType):
297                    possibleLinks.append((outS.name, inS.name))
298        return possibleLinks
299
300    def addDefaultLinks(self):
301        canConnect = 0
302        addedInLinks = []
303        addedOutLinks = []
304        self.multiplePossibleConnections = 0    # can we connect some signal with more than one widget
305
306        minorInputs = [signal for signal in self.inWidget.widgetInfo.inputs if not signal.default]
307        majorInputs = [signal for signal in self.inWidget.widgetInfo.inputs if signal.default]
308        minorOutputs = [signal for signal in self.outWidget.widgetInfo.outputs if not signal.default]
309        majorOutputs = [signal for signal in self.outWidget.widgetInfo.outputs if signal.default]
310
311        inConnected = self.inWidget.getInConnectedSignalNames()
312        outConnected = self.outWidget.getOutConnectedSignalNames()
313
314        # input connections that can be simultaneously connected to multiple outputs are not to be considered as already connected
315        for i in inConnected[::-1]:
316            if not self.inWidget.instance.signalIsOnlySingleConnection(i):
317                inConnected.remove(i)
318
319        for s in majorInputs + minorInputs:
320            if not self.inWidget.instance.hasInputName(s.name):
321                return -1
322        for s in majorOutputs + minorOutputs:
323            if not self.outWidget.instance.hasOutputName(s.name):
324                return -1
325
326        pl1 = self.getPossibleConnections(majorOutputs, majorInputs)
327        pl2 = self.getPossibleConnections(majorOutputs, minorInputs)
328        pl3 = self.getPossibleConnections(minorOutputs, majorInputs)
329        pl4 = self.getPossibleConnections(minorOutputs, minorInputs)
330
331        all = pl1 + pl2 + pl3 + pl4
332
333        if not all: return 0
334
335        # try to find a link to any inputs that hasn't been previously connected
336        self.allSignalsTaken = 1
337        for (o,i) in all:
338            if i not in inConnected:
339                all.remove((o,i))
340                all.insert(0, (o,i))
341                self.allSignalsTaken = 0       # we found an unconnected link. no need to show the signal dialog
342                break
343        self.addLink(all[0][0], all[0][1])  # add only the best link
344
345        # there are multiple possible connections if we have in the same priority class more than one possible unconnected link
346        for pl in [pl1, pl2, pl3, pl4]:
347            #if len(pl) > 1 and sum([i not in inConnected for (o,i) in pl]) > 1: # if we have more than one valid
348            if len(pl) > 1:     # if we have more than one valid
349                self.multiplePossibleConnections = 1
350            if len(pl) > 0:     # when we find a first non-empty list we stop searching
351                break
352        return len(all) > 0
353   
354    def addDefaultLinks(self):
355        self.multiplePossibleConnections = 0
356        candidates = self.signalManager.proposePossibleLinks(self.outWidget.instance, self.inWidget.instance)
357        if candidates:
358            ## If there are more candidates with max weights
359            maxW = max([w for _,_,w in candidates])
360            maxCandidates = [c for c in candidates if c[-1] == maxW]
361            if len(maxCandidates) > 1:
362                self.multiplePossibleConnections = 1
363            best = maxCandidates[0]
364            self.addLink(best[0].name, best[1].name) # add the best to the view
365            return True
366        else:
367            return 0
368           
369
370    def addLink(self, outName, inName):
371        if (outName, inName) in self._links: return 1
372
373        # check if correct types
374        outType = self.outWidget.instance.getOutputType(outName)
375        inType = self.inWidget.instance.getInputType(inName)
376        if not issubclass(outType, inType) and not issubclass(inType, outType): return 0 #TODO check this with signalManager.canConnect
377
378        inSignal = None
379        inputs = self.inWidget.widgetInfo.inputs
380        for i in range(len(inputs)):
381            if inputs[i].name == inName: inSignal = inputs[i]
382
383        # if inName is a single signal and connection already exists -> delete it
384        for (outN, inN) in self._links:
385            if inN == inName and inSignal.single:
386                self.removeLink(outN, inN)
387
388        self._links.append((outName, inName))
389        self.canvasView.addLink(outName, inName)
390        return 1
391
392
393    def removeLink(self, outName, inName):
394        if (outName, inName) in self._links:
395            self._links.remove((outName, inName))
396            self.canvasView.removeLink(outName, inName)
397
398    def getLinks(self):
399        return self._links
400
401
402class ColorIcon(QToolButton):
403    def __init__(self, parent, color):
404        QToolButton.__init__(self, parent)
405        self.color = color
406        self.setMaximumSize(20,20)
407        self.connect(self, SIGNAL("clicked()"), self.showColorDialog)
408        self.updateColor()
409
410    def updateColor(self):
411        pixmap = QPixmap(16,16)
412        painter = QPainter()
413        painter.begin(pixmap)
414        painter.setPen(QPen(self.color))
415        painter.setBrush(QBrush(self.color))
416        painter.drawRect(0, 0, 16, 16);
417        painter.end()
418        self.setIcon(QIcon(pixmap))
419        self.setIconSize(QSize(16,16))
420
421
422    def drawButtonLabel(self, painter):
423        painter.setBrush(QBrush(self.color))
424        painter.setPen(QPen(self.color))
425        painter.drawRect(3, 3, self.width()-6, self.height()-6)
426
427    def showColorDialog(self):
428        color = QColorDialog.getColor(self.color, self)
429        if color.isValid():
430            self.color = color
431            self.updateColor()
432            self.repaint()
433
434# canvas dialog
435class CanvasOptionsDlg(QDialog):
436    def __init__(self, canvasDlg, *args):
437        apply(QDialog.__init__,(self,) + args)
438        self.canvasDlg = canvasDlg
439        self.settings = dict(canvasDlg.settings)        # create a copy of the settings dict. in case we accept the dialog, we update the canvasDlg.settings with this dict
440        if sys.platform == "darwin":
441            self.setWindowTitle("Preferences")
442        else:
443            self.setWindowTitle("Canvas Options")
444        self.topLayout = QVBoxLayout(self)
445        self.topLayout.setSpacing(0)
446        self.resize(300,300)
447#        self.toAdd = []
448#        self.toRemove = []
449
450        self.tabs = QTabWidget(self)
451        GeneralTab = OWGUI.widgetBox(self.tabs, margin = 4)
452        ExceptionsTab = OWGUI.widgetBox(self.tabs, margin = 4)
453        TabOrderTab = OWGUI.widgetBox(self.tabs, margin = 4)
454
455        self.tabs.addTab(GeneralTab, "General")
456        self.tabs.addTab(ExceptionsTab, "Exception handling")
457        self.tabs.addTab(TabOrderTab, "Widget tab order")
458
459        # #################################################################
460        # GENERAL TAB
461        generalBox = OWGUI.widgetBox(GeneralTab, "General Options")
462        self.snapToGridCB = OWGUI.checkBox(generalBox, self.settings, "snapToGrid", "Snap widgets to grid", debuggingEnabled = 0)
463        self.enableCanvasDropShadowsCB = OWGUI.checkBox(generalBox, self.settings, "enableCanvasDropShadows", "Enable drop shadows in canvas", debuggingEnabled = 0)
464        self.writeLogFileCB  = OWGUI.checkBox(generalBox, self.settings, "writeLogFile", "Save content of the Output window to a log file", debuggingEnabled = 0)
465        self.showSignalNamesCB = OWGUI.checkBox(generalBox, self.settings, "showSignalNames", "Show signal names between widgets", debuggingEnabled = 0)
466        self.dontAskBeforeCloseCB= OWGUI.checkBox(generalBox, self.settings, "dontAskBeforeClose", "Don't ask to save schema before closing", debuggingEnabled = 0)
467        self.saveWidgetsPositionCB = OWGUI.checkBox(generalBox, self.settings, "saveWidgetsPosition", "Save size and position of widgets", debuggingEnabled = 0)
468        self.useContextsCB = OWGUI.checkBox(generalBox, self.settings, "useContexts", "Use context settings")
469
470        validator = QIntValidator(self)
471        validator.setRange(0,10000)
472
473        hbox1 = OWGUI.widgetBox(GeneralTab, orientation = "horizontal")
474        hbox2 = OWGUI.widgetBox(GeneralTab, orientation = "horizontal")
475        canvasDlgSettings = OWGUI.widgetBox(hbox1, "Canvas Dialog Settings")
476#        schemeSettings = OWGUI.widgetBox(hbox1, "Scheme Settings")
477         
478#        self.widthSlider = OWGUI.qwtHSlider(canvasDlgSettings, self.settings, "canvasWidth", minValue = 300, maxValue = 1200, label = "Canvas width:  ", step = 50, precision = " %.0f px", debuggingEnabled = 0)
479#        self.heightSlider = OWGUI.qwtHSlider(canvasDlgSettings, self.settings, "canvasHeight", minValue = 300, maxValue = 1200, label = "Canvas height:  ", step = 50, precision = " %.0f px", debuggingEnabled = 0)
480#        OWGUI.separator(canvasDlgSettings)
481       
482        items = [str(n) for n in QStyleFactory.keys()]
483        itemsLower = [s.lower() for s in items]
484        ind = itemsLower.index(self.settings.get("style", "Windows").lower())
485        self.settings["style"] = items[ind]
486        OWGUI.comboBox(canvasDlgSettings, self.settings, "style", label = "Window style:", orientation = "horizontal", items = [str(n) for n in QStyleFactory.keys()], sendSelectedValue = 1, debuggingEnabled = 0)
487        OWGUI.checkBox(canvasDlgSettings, self.settings, "useDefaultPalette", "Use style's standard palette", debuggingEnabled = 0)
488       
489        OWGUI.separator(canvasDlgSettings)
490        OWGUI.comboBox(canvasDlgSettings, self.settings, "widgetListType", label="Toolbox style:", orientation="horizontal",
491                       items = ["Tool box", "Tree view", "Tree view (no icons)", "Tabs without labels", "Tabs with labels"], debuggingEnabled=0)
492       
493#        if canvasDlg:
494#            selectedWidgetBox = OWGUI.widgetBox(schemeSettings, orientation = "horizontal")
495#            self.selectedWidgetIcon = ColorIcon(selectedWidgetBox, canvasDlg.widgetSelectedColor)
496#            selectedWidgetBox.layout().addWidget(self.selectedWidgetIcon)
497#            selectedWidgetLabel = OWGUI.widgetLabel(selectedWidgetBox, " Selected widget")
498#
499#            activeWidgetBox = OWGUI.widgetBox(schemeSettings, orientation = "horizontal")
500#            self.activeWidgetIcon = ColorIcon(activeWidgetBox, canvasDlg.widgetActiveColor)
501#            activeWidgetBox.layout().addWidget(self.activeWidgetIcon)
502#            selectedWidgetLabel = OWGUI.widgetLabel(activeWidgetBox, " Active widget")
503#
504#            lineBox = OWGUI.widgetBox(schemeSettings, orientation = "horizontal")
505#            self.lineIcon = ColorIcon(lineBox, canvasDlg.lineColor)
506#            lineBox.layout().addWidget(self.lineIcon)
507#            selectedWidgetLabel = OWGUI.widgetLabel(lineBox, " Lines")
508#           
509#        OWGUI.separator(schemeSettings)
510#        items = ["%d x %d" % (v,v) for v in self.canvasDlg.schemeIconSizeList]
511#        val = min(len(items)-1, self.settings['schemeIconSize'])
512#        self.schemeIconSizeCombo = OWGUI.comboBoxWithCaption(schemeSettings, self.settings, 'schemeIconSize', "Scheme icon size:", items = items, tooltip = "Set the size of the widget icons on the scheme", debuggingEnabled = 0)
513       
514        GeneralTab.layout().addStretch(1)
515
516        # #################################################################
517        # EXCEPTION TAB
518        exceptions = OWGUI.widgetBox(ExceptionsTab, "Exceptions")
519        #self.catchExceptionCB = QCheckBox('Catch exceptions', exceptions)
520        self.focusOnCatchExceptionCB = OWGUI.checkBox(exceptions, self.settings, "focusOnCatchException", 'Show output window on exception')
521        self.printExceptionInStatusBarCB = OWGUI.checkBox(exceptions, self.settings, "printExceptionInStatusBar", 'Print last exception in status bar')
522
523        output = OWGUI.widgetBox(ExceptionsTab, "System output")
524        #self.catchOutputCB = QCheckBox('Catch system output', output)
525        self.focusOnCatchOutputCB = OWGUI.checkBox(output, self.settings, "focusOnCatchOutput", 'Focus output window on system output')
526        self.printOutputInStatusBarCB = OWGUI.checkBox(output, self.settings, "printOutputInStatusBar", 'Print last system output in status bar')
527
528        hboxExc = OWGUI.widgetBox(ExceptionsTab, orientation="horizontal")
529        outputCanvas = OWGUI.widgetBox(hboxExc, "Canvas Info Handling")
530        outputWidgets = OWGUI.widgetBox(hboxExc, "Widget Info Handling")
531        self.ocShow = OWGUI.checkBox(outputCanvas, self.settings, "ocShow", 'Show icon above widget for...')
532        indent = OWGUI.checkButtonOffsetHint(self.ocShow)
533        self.ocInfo = OWGUI.checkBox(OWGUI.indentedBox(outputCanvas, indent), self.settings, "ocInfo", 'Information')
534        self.ocWarning = OWGUI.checkBox(OWGUI.indentedBox(outputCanvas, indent), self.settings, "ocWarning", 'Warnings')
535        self.ocError = OWGUI.checkBox(OWGUI.indentedBox(outputCanvas, indent), self.settings, "ocError", 'Errors')
536
537        self.owShow = OWGUI.checkBox(outputWidgets, self.settings, "owShow", 'Show statusbar info for...')
538        self.owInfo = OWGUI.checkBox(OWGUI.indentedBox(outputWidgets, indent), self.settings, "owInfo", 'Information')
539        self.owWarning = OWGUI.checkBox(OWGUI.indentedBox(outputWidgets, indent), self.settings, "owWarning", 'Warnings')
540        self.owError = OWGUI.checkBox(OWGUI.indentedBox(outputWidgets, indent), self.settings, "owError", 'Errors')
541
542        verbosityBox = OWGUI.widgetBox(ExceptionsTab, "Verbosity", orientation = "horizontal")
543        verbosityBox.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum))
544        self.verbosityCombo = OWGUI.comboBox(verbosityBox, self.settings, "outputVerbosity", label = "Set level of widget output: ", orientation='horizontal', items=["Small", "Medium", "High"])
545        ExceptionsTab.layout().addStretch(1)
546
547        # #################################################################
548        # TAB ORDER TAB
549        tabOrderBox = OWGUI.widgetBox(TabOrderTab, "Set Order of Widget Categories", orientation = "horizontal", sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum))
550        self.tabOrderList = QListWidget(tabOrderBox)
551        self.tabOrderList.setAcceptDrops(True)
552
553        tabOrderBox.layout().addWidget(self.tabOrderList)
554        self.tabOrderList.setSelectionMode(QListWidget.SingleSelection)
555       
556        ind = 0
557        for (name, show) in self.settings["WidgetTabs"]:
558            if self.canvasDlg.widgetRegistry.has_key(name):
559                self.tabOrderList.addItem(name)
560                self.tabOrderList.item(ind).setCheckState(show and Qt.Checked or Qt.Unchecked)
561                ind+=1
562
563        w = OWGUI.widgetBox(tabOrderBox, sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding))
564        self.upButton = OWGUI.button(w, self, "Up", callback = self.moveUp)
565        self.downButton = OWGUI.button(w, self, "Down", callback = self.moveDown)
566#        w.layout().addSpacing(20)
567#        self.addButton = OWGUI.button(w, self, "Add", callback = self.addCategory)
568#        self.removeButton = OWGUI.button(w, self, "Remove", callback = self.removeCategory)
569#        self.removeButton.setEnabled(0)
570        w.layout().addStretch(1)
571
572        # OK, Cancel buttons
573        hbox = OWGUI.widgetBox(self, orientation = "horizontal", sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed))
574        hbox.layout().addStretch(1)
575        self.okButton = OWGUI.button(hbox, self, "OK", callback = self.accept)
576        self.cancelButton = OWGUI.button(hbox, self, "Cancel", callback = self.reject)
577        self.connect(self.tabOrderList, SIGNAL("currentRowChanged(int)"), self.enableDisableButtons)
578
579        self.topLayout.addWidget(self.tabs)
580        self.topLayout.addWidget(hbox)
581
582
583    def accept(self):
584#        self.settings["widgetSelectedColor"] = self.selectedWidgetIcon.color.getRgb()
585#        self.settings["widgetActiveColor"]   = self.activeWidgetIcon.color.getRgb()
586#        self.settings["lineColor"]           = self.lineIcon.color.getRgb()
587        QDialog.accept(self)
588       
589       
590
591    # move selected widget category up
592    def moveUp(self):
593        for i in range(1, self.tabOrderList.count()):
594            if self.tabOrderList.item(i).isSelected():
595                item = self.tabOrderList.takeItem(i)
596                for j in range(self.tabOrderList.count()): self.tabOrderList.item(j).setSelected(0)
597                self.tabOrderList.insertItem(i-1, item)
598                item.setSelected(1)
599
600    # move selected widget category down
601    def moveDown(self):
602        for i in range(self.tabOrderList.count()-2,-1,-1):
603            if self.tabOrderList.item(i).isSelected():
604                item = self.tabOrderList.takeItem(i)
605                for j in range(self.tabOrderList.count()): self.tabOrderList.item(j).setSelected(0)
606                self.tabOrderList.insertItem(i+1, item)
607                item.setSelected(1)
608
609    def enableDisableButtons(self, itemIndex):
610        self.upButton.setEnabled(itemIndex > 0)
611        self.downButton.setEnabled(itemIndex < self.tabOrderList.count()-1)
612        catName = str(self.tabOrderList.currentItem().text())
613        if not self.canvasDlg.widgetRegistry.has_key(catName): return
614#        self.removeButton.setEnabled( all([os.path.normpath(self.canvasDlg.widgetDir) not in os.path.normpath(x.directory) and
615#                                           os.path.normpath(self.canvasDlg.addOnsDir) not in os.path.normpath(x.directory)
616#                                             for x in self.canvasDlg.widgetRegistry[catName].values()]))
617        #self.removeButton.setEnabled(1)
618
619#    def addCategory(self):
620#        dir = str(QFileDialog.getExistingDirectory(self, "Select the folder that contains the add-on:"))
621#        if dir != "":
622#            if os.path.split(dir)[1] == "widgets":     # register a dir above the dir that contains the widget folder
623#                dir = os.path.split(dir)[0]
624#            if os.path.exists(os.path.join(dir, "widgets")):
625#                name = os.path.split(dir)[1]
626#                self.toAdd.append((name, dir))
627#                self.tabOrderList.addItem(name)
628#                self.tabOrderList.item(self.tabOrderList.count()-1).setCheckState(Qt.Checked)
629#            else:
630#                QMessageBox.information( None, "Information", 'The specified folder does not seem to contain an Orange add-on.', QMessageBox.Ok + QMessageBox.Default)
631#           
632#       
633#    def removeCategory(self):
634#        curCat = str(self.tabOrderList.item(self.tabOrderList.currentRow()).text())
635#        if QMessageBox.warning(self,'Orange Canvas', "Unregister widget category '%s' from Orange canvas?\nThis will not remove any files." % curCat, QMessageBox.Ok , QMessageBox.Cancel | QMessageBox.Default | QMessageBox.Escape) == QMessageBox.Ok:
636#            self.toRemove.append((curCat, self.canvasDlg.widgetRegistry[curCat]))
637#            item = self.tabOrderList.takeItem(self.tabOrderList.row(self.tabOrderList.currentItem()))
638#            #if item: item.setHidden(1)
639
640class KeyEdit(QLineEdit):
641    def __init__(self, parent, key, invdict, widget, invInvDict):
642        QLineEdit.__init__(self, parent)
643        self.setText(key)
644        #self.setReadOnly(True)
645        self.invdict = invdict
646        self.widget = widget
647        self.invInvDict = invInvDict
648
649    def keyPressEvent(self, e):
650        if e.key() == Qt.Key_Delete or e.key() == Qt.Key_Backspace:
651            pressed = "<none>"
652            self.setText(pressed)
653            prevkey = self.invdict.get(self.widget)
654            if prevkey:
655                del self.invdict[self.widget]
656                del self.invInvDict[prevkey]
657            return
658
659        if e.key() not in range(32, 128): # + range(Qt.Key_F1, Qt.Key_F35+1): -- this wouldn't work, see the line below, and also writing to file etc.
660            e.ignore()
661            return
662
663        pressed = "-".join(filter(None, [e.modifiers() & x and y for x, y in [(Qt.ControlModifier, "Ctrl"), (Qt.AltModifier, "Alt")]]) + [chr(e.key())])
664
665        assigned = self.invInvDict.get(pressed, None)
666        if assigned and assigned != self and QMessageBox.question(self, "Confirmation", "'%(pressed)s' is already assigned to '%(assigned)s'. Override?" % {"pressed": pressed, "assigned": assigned.widget.name}, QMessageBox.Yes | QMessageBox.Default, QMessageBox.No | QMessageBox.Escape) == QMessageBox.No:
667            return
668       
669        if assigned:
670            assigned.setText("<none>")
671            del self.invdict[assigned.widget]
672        self.setText(pressed)
673        self.invdict[self.widget] = pressed
674        self.invInvDict[pressed] = self
675
676class AddOnManagerSummary(QDialog):
677    def __init__(self, add, remove, upgrade, *args):
678        apply(QDialog.__init__,(self,) + args)
679        self.setWindowTitle("Pending Actions")
680        self.topLayout = QVBoxLayout(self)
681        self.topLayout.setSpacing(10)
682        self.resize(200, 0)
683       
684        OWGUI.label(self, self, "If you confirm, the following actions will take place:")
685
686        self.memo = memo = QTextEdit(self)
687        self.layout().addWidget(memo)
688        memo.setReadOnly(True)
689        memo.setFrameStyle(QFrame.NoFrame)
690        pal = QPalette()
691        pal.setColor(QPalette.Base, Qt.transparent)
692        memo.setPalette(pal)
693        memo.setLineWrapMode(QTextEdit.WidgetWidth)
694        memo.setWordWrapMode(QTextOption.WordWrap)
695        QObject.connect(memo.document().documentLayout(),
696                       SIGNAL("documentSizeChanged(const QSizeF &)"),
697                        lambda docSize: self.updateMinSize(docSize))
698        actions = []
699        for ao in add:
700            actions.append("Install %s." % ao)
701        for ao in remove:
702            actions.append("Remove %s." % ao)
703        for ao in upgrade:
704            actions.append("Upgrade %s." % ao)
705        memo.setText("\n".join(actions))
706       
707        self.layout().addStretch(1)
708        self.setSizePolicy( QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Minimum) )
709
710        hbox = OWGUI.widgetBox(self, orientation = "horizontal", sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed))
711        hbox.layout().addStretch(1)
712        self.okButton = OWGUI.button(hbox, self, "OK", callback = self.accept)
713        self.cancelButton = OWGUI.button(hbox, self, "Cancel", callback = self.reject)
714        self.okButton.setDefault(True)
715
716    def updateMinSize(self, documentSize):
717        self.memo.update()
718        self.memo.setMinimumHeight(min(300, documentSize.height() + 2 * self.memo.frameWidth()))
719
720class AddOnManagerDialog(QDialog):
721    def __init__(self, canvasDlg, *args):
722        apply(QDialog.__init__,(self,) + args)
723        self.canvasDlg = canvasDlg
724        self.setWindowTitle("Add-on Management")
725        self.topLayout = QVBoxLayout(self)
726        self.topLayout.setSpacing(0)
727        self.resize(600,500)
728        self.layout().setSizeConstraint(QLayout.SetMinimumSize)
729       
730        mainBox = OWGUI.widgetBox(self, orientation="vertical", sizePolicy=QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
731       
732        # View settings & Search
733       
734        self.groupByRepo = True
735        self.searchStr = ""
736        self.to_upgrade = set()
737
738        searchBox = OWGUI.widgetBox(mainBox, orientation = "horizontal", sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum))
739
740        self.eSearch = self.lineEditSearch(searchBox, self, "searchStr", None, 0, tooltip = "Type in to filter (search) add-ons.", callbackOnType=True, callback=self.searchCallback)
741       
742        # Repository & Add-on tree
743       
744        repos = OWGUI.widgetBox(mainBox, "Add-ons", orientation = "horizontal", sizePolicy=QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred))
745        repos.layout().setSizeConstraint(QLayout.SetMinimumSize)
746        self.lst = lst = QListWidget(repos)
747        lst.setMinimumWidth(200)
748        lst.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred))
749        repos.layout().addWidget(lst)
750        QObject.connect(lst, SIGNAL("itemChanged(QListWidgetItem *)"), self.cbToggled)
751        QObject.connect(lst, SIGNAL("currentItemChanged(QListWidgetItem *, QListWidgetItem *)"), self.currentItemChanged)
752
753        import Orange.utils.addons
754
755        # Bottom info pane
756       
757        self.infoPane = infoPane = OWGUI.widgetBox(mainBox, orientation="vertical", sizePolicy=QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred))
758        infoPane.layout().setSizeConstraint(QLayout.SetMinimumSize)
759
760        pVerInstalled = OWGUI.widgetBox(infoPane, orientation="horizontal")
761        lblVerInstalled = OWGUI.label(pVerInstalled, self, "Installed version:", 150)
762        boldFont = lblVerInstalled.font()
763        boldFont.setWeight(QFont.Bold)
764        lblVerInstalled.setFont(boldFont)
765        self.lblVerInstalledValue = OWGUI.label(pVerInstalled, self, "-")
766        self.lblVerInstalledValue.setSizePolicy(QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum))
767        pVerInstalled.layout().addSpacing(10)
768        self.lblStatus = OWGUI.label(pVerInstalled, self, "")
769        self.lblStatus.setFont(boldFont)
770        pVerInstalled.layout().addStretch(1)
771
772        pVerAvail = OWGUI.widgetBox(infoPane, orientation="horizontal")
773        lblVerAvail = OWGUI.label(pVerAvail, self, "Available version:", 150)
774        lblVerAvail.setFont(boldFont)
775        self.lblVerAvailValue = OWGUI.label(pVerAvail, self, "")
776        pVerAvail.layout().addSpacing(10)
777        self.upgradeButton = OWGUI.button(pVerAvail, self, "Upgrade", callback = self.upgrade)
778        self.upgradeButton.setFixedHeight(lblVerAvail.height())
779        self.donotUpgradeButton = OWGUI.button(pVerAvail, self, "Do not upgrade", callback = self.donotUpgrade)
780        self.donotUpgradeButton.setFixedHeight(lblVerAvail.height())
781        pVerAvail.layout().addStretch(1)
782       
783        pInfoBtns = OWGUI.widgetBox(infoPane, orientation="horizontal", sizePolicy=QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed))
784        self.webButton = OWGUI.button(pInfoBtns, self, "Open webpage", callback = self.openWebPage)
785        self.docButton = OWGUI.button(pInfoBtns, self, "Open documentation", callback = self.openDocsPage)
786        self.listWidgetsButton = OWGUI.button(pInfoBtns, self, "List widgets", callback = self.listWidgets)
787        pInfoBtns.layout().addStretch(1)
788
789        self.lblDescription = lblDescription = QTextEdit(infoPane)
790        infoPane.layout().addWidget(lblDescription)
791        lblDescription.setReadOnly(True)
792        lblDescription.setFrameStyle(QFrame.NoFrame)
793        pal = QPalette()
794        pal.setColor(QPalette.Base, Qt.transparent)
795        lblDescription.setPalette(pal)
796        lblDescription.setLineWrapMode(QTextEdit.WidgetWidth)
797        lblDescription.setWordWrapMode(QTextOption.WordWrap)
798        QObject.connect(lblDescription.document().documentLayout(),
799                        SIGNAL("documentSizeChanged(const QSizeF &)"),
800                        lambda docSize: self.updateDescMinSize(docSize))
801
802        # Bottom info pane for registered add-ons
803        self.regiInfoPane = regiInfoPane = OWGUI.widgetBox(mainBox, orientation="vertical", sizePolicy=QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum))
804        regiInfoPane.layout().setSizeConstraint(QLayout.SetMinimumSize)
805        self.lblRegisteredAddOnInfo = OWGUI.label(regiInfoPane, self, "-")
806        self.lblRegisteredAddOnInfo.setSizePolicy(QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum))
807
808        # Right panel
809       
810        rightPanel = OWGUI.widgetBox(repos, orientation = "vertical", sizePolicy=QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding))
811        rightPanel.layout().setSizeConstraint(QLayout.SetMinimumSize)
812        self.reloadRepoButton = OWGUI.button(rightPanel, self, "Refresh list", callback = self.reloadRepo)
813        rightPanel.layout().addSpacing(15)
814        self.upgradeAllButton = OWGUI.button(rightPanel, self, "Upgrade All", callback = self.upgradeAll)
815        rightPanel.layout().addStretch(1)
816        for btn in rightPanel.children():
817            if btn.__class__ is QPushButton:
818                btn.setMinimumHeight(btn.height())
819       
820        # Close button
821        hbox = OWGUI.widgetBox(mainBox, orientation = "horizontal", sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum))
822        hbox.layout().addStretch(1)
823        self.okButton = OWGUI.button(hbox, self, "OK", callback = self.accept)
824        self.cancelButton = OWGUI.button(hbox, self, "Cancel", callback = self.reject)
825        self.okButton.setDefault(True)
826
827        self.refreshView()
828       
829    def updateDescMinSize(self, documentSize):
830        self.lblDescription.update()
831        self.lblDescription.setMinimumHeight(min(300, documentSize.height() + 2 * self.lblDescription.frameWidth()))
832        pass
833   
834    def accept(self):
835        self.to_upgrade.difference_update(self.to_remove())
836        import Orange.utils.addons
837        add, remove, upgrade = self.to_install(), self.to_remove(), self.to_upgrade
838        if len(add) + len(remove) + len(upgrade) > 0:
839            summary = AddOnManagerSummary(add, remove, upgrade, self)
840            if summary.exec_() == QDialog.Rejected:
841                return
842        QDialog.accept(self)
843       
844    def reloadRepo(self):
845        # Reload add-on list.
846        # TODO: This can take some time - show some progress to user!
847        import Orange.utils.addons
848        try:
849            Orange.utils.addons.refresh_available_addons()
850        except Exception, e:  # Maybe gather all exceptions (for all repositories) and show them in the end?
851            QMessageBox.critical(self, "Error", "Could not reload repository '%s': %s." % (repo.name, e))
852        # Finally, refresh the tree on GUI.
853        self.refreshView()
854           
855    def upgradeCandidates(self):
856        result = []
857        import Orange.utils.addons
858        for ao in Orange.utils.addons.addons.values():
859            if ao.installed_version and ao.available_version and ao.installed_version != ao.available_version:
860                result.append(ao.name)
861        return result
862   
863    def upgradeAll(self):
864        for candidate in self.upgradeCandidates():
865            self.upgrade(candidate, refresh=False)
866        self.refreshInfoPane()
867
868    def upgrade(self, name=None, refresh=True):
869        if not name:
870            name = self.getAddOnIdFromItem(self.lst.currentItem())
871        self.to_upgrade.add(name)
872        if refresh:
873            self.refreshInfoPane()
874
875    def openWebPage(self):
876        addon = self.getAddOnFromItem(self.lst.currentItem())
877        if addon and addon.homepage:
878            import webbrowser
879            webbrowser.open(addon.homepage)
880
881    def openDocsPage(self):
882        addon = self.getAddOnFromItem(self.lst.currentItem())
883        if addon and addon.docs_url:
884            import webbrowser
885            webbrowser.open(addon.docs_url)
886
887    def listWidgets(self):
888        addOn = self.getAddOnFromItem(self.lst.currentItem())
889        if not addOn: return
890        import Orange.utils.addons
891        if addOn.__class__ is not Orange.utils.addons.OrangeAddOnInRepo: return
892        if not addOn.repository.has_web_script: return
893        self.canvasDlg.helpWindow.open("%s/addOnServer.py/%s/doc/widgets/" % (addOn.repository.url, addOn.filename), modal=True)
894       
895       
896    def donotUpgrade(self):
897        id = self.getAddOnIdFromItem(self.lst.currentItem())
898        self.to_upgrade.remove(id)
899        self.refreshInfoPane()
900
901    def cbToggled(self, item):
902        self.refreshInfoPane(item)
903
904    def lineEditSearch(self, *args, **props):
905        return OWGUI.lineEdit(*args, **props)
906
907    def getAddOnFromItem(self, item):
908        return getattr(item, "addon", None)
909
910    def getAddOnIdFromItem(self, item):
911        addon = self.getAddOnFromItem(item)
912        return addon.name if addon else None
913       
914    def refreshInfoPane(self, item=None):
915        if not item:
916            item = self.lst.currentItem()
917        addon = None
918        if item:
919            import Orange.utils.addons
920            import orngEnviron
921            addon = self.getAddOnFromItem(item)
922        if addon:
923            self.lblDescription.setText(addon.summary.strip() +"\n"+ addon.description.strip())
924            self.lblVerAvailValue.setText(addon.available_version)
925
926            self.lblVerInstalledValue.setText(addon.installed_version if addon.installed_version else "-") #TODO Tell whether it's a system-wide installation
927            self.upgradeButton.setVisible(bool(addon.installed_version and addon.installed_version!=addon.available_version) and addon.name not in self.to_upgrade) #TODO Disable if it's a system-wide installation
928            self.donotUpgradeButton.setVisible(addon.name in self.to_upgrade)
929            self.webButton.setVisible(bool(addon.homepage))
930            self.docButton.setVisible(bool(addon.docs_url))
931            self.listWidgetsButton.setVisible(False) #TODO A list of widgets is not available
932
933            if not addon.installed_version and item.checkState()==Qt.Checked:
934                self.lblStatus.setText("marked for installation")
935            elif addon.installed_version and item.checkState()!=Qt.Checked:
936                self.lblStatus.setText("marked for removal")
937            elif addon.name in self.to_upgrade:
938                self.lblStatus.setText("marked for upgrade")
939            else:
940                self.lblStatus.setText("")
941
942            self.infoPane.setVisible(True)
943            self.regiInfoPane.setVisible(False)
944        else:
945            self.infoPane.setVisible(False)
946            self.regiInfoPane.setVisible(False)
947        self.enableDisableButtons()
948
949    def enableDisableButtons(self):
950        import Orange.utils.addons
951        aos = Orange.utils.addons.addons.values()
952        self.upgradeAllButton.setEnabled(any(ao.installed_version and ao.available_version and
953                                             ao.installed_version != ao.available_version and
954                                             ao.name not in self.to_upgrade for ao in aos))
955       
956    def currentItemChanged(self, new, previous):
957        # Refresh info pane & button states
958        self.refreshInfoPane(new)
959
960    def addAddOnsToTree(self, addon_dict, selected=None, to_install=[], to_remove=[]):
961        # Sort alphabetically
962        addons = sorted(list(addon_dict.items()),
963                        key = lambda (name, ao): name)
964
965        for (i, (name, ao)) in enumerate(addons):
966            item = QListWidgetItem()
967            self.lst.addItem(item)
968            item.setText(name)
969            item.setCheckState(Qt.Checked if ao.installed_version and not name in to_remove or name in to_install else Qt.Unchecked)
970            item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled)
971            item.addon = ao
972            if name == selected:
973                self.lst.setCurrentItem(item)
974
975            item.disableToggleSignal = False
976
977    def lst_items(self):
978        for i in xrange(self.lst.count()):
979            yield self.lst.item(i)
980
981    def to_install(self):
982        return set([item.addon.name for item in self.lst_items()
983                    if item.checkState()==Qt.Checked and not item.addon.installed_version])
984
985    def to_remove(self):
986        return set([item.addon.name for item in self.lst_items()
987                    if item.checkState()!=Qt.Checked and item.addon.installed_version])
988
989    def refreshView(self, selectedRegisteredAddOnId=None):
990        import Orange
991        # Save current item selection
992        selected_addon = self.getAddOnIdFromItem(self.lst.currentItem())
993        to_install = self.to_install()
994        to_remove = self.to_remove()
995        #TODO: Save the next repository selection too, in case the current one was deleted
996
997        # Clear the tree
998        self.lst.clear()
999       
1000        # Add repositories and add-ons
1001        addons = {}
1002        for name in Orange.utils.addons.search_index(self.searchStr):
1003            addons[name] = Orange.utils.addons.addons[name]
1004        self.addAddOnsToTree(addons, selected = selected_addon, to_install=to_install, to_remove=to_remove)
1005        self.refreshInfoPane()
1006
1007        #TODO Should we somehow show the legacy registered addons?
1008
1009    def searchCallback(self):
1010        self.refreshView()
1011   
1012# widget shortcuts dialog
1013class WidgetShortcutDlg(QDialog):
1014    def __init__(self, canvasDlg, *args):
1015        import orngTabs
1016
1017        apply(QDialog.__init__,(self,) + args)
1018        self.canvasDlg = canvasDlg
1019        self.setWindowTitle("Widget Shortcuts")
1020        self.setLayout(QVBoxLayout())
1021        self.layout().setSpacing(10)
1022        self.resize(700,500)
1023
1024        self.invDict = dict([(y, x) for x, y in canvasDlg.widgetShortcuts.items()])
1025        invInvDict = {}
1026
1027        self.tabs = QTabWidget(self)
1028       
1029        extraTabs = [(name, 1) for name in canvasDlg.widgetRegistry.keys() if name not in [tab for (tab, s) in canvasDlg.settings["WidgetTabs"]]]
1030        for tabName, show in canvasDlg.settings["WidgetTabs"] + extraTabs:
1031            if not canvasDlg.widgetRegistry.has_key(tabName):
1032                continue
1033            scrollArea = QScrollArea()
1034            self.tabs.addTab(scrollArea, tabName)
1035            #scrollArea.setWidgetResizable(1)       # you have to use this or set size to wtab manually - otherwise nothing gets shown
1036
1037            wtab = QWidget(self.tabs)
1038            scrollArea.setWidget(wtab)
1039
1040            widgets = [(int(widgetInfo.priority), name, widgetInfo) for (name, widgetInfo) in canvasDlg.widgetRegistry[tabName].items()]
1041            widgets.sort()
1042            rows = (len(widgets)+2) / 3
1043            layout = QGridLayout(wtab)
1044
1045            for i, (priority, name, widgetInfo) in enumerate(widgets):
1046                x = i / rows
1047                y = i % rows
1048
1049                hlayout = QHBoxLayout()
1050                mainBox = QWidget(wtab)
1051                mainBox.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed))
1052                mainBox.setLayout(hlayout)
1053                layout.addWidget(mainBox, y, x, Qt.AlignTop | Qt.AlignLeft)
1054                label = QLabel(wtab)
1055                label.setPixmap(canvasDlg.getWidgetIcon(widgetInfo).pixmap(40))
1056                hlayout.addWidget(label)
1057
1058                optionsw = QWidget(self)
1059                optionsw.setLayout(QVBoxLayout())
1060                hlayout.addWidget(optionsw)
1061                optionsw.layout().addStretch(1)
1062
1063                OWGUI.widgetLabel(optionsw, name)
1064                key = self.invDict.get(widgetInfo, "<none>")
1065                le = KeyEdit(optionsw, key, self.invDict, widgetInfo, invInvDict)
1066                optionsw.layout().addWidget(le)
1067                invInvDict[key] = le
1068                le.setFixedWidth(60)
1069
1070            wtab.resize(wtab.sizeHint())
1071
1072        # OK, Cancel buttons
1073        hbox = OWGUI.widgetBox(self, orientation = "horizontal", sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed))
1074        hbox.layout().addStretch(1)
1075        self.okButton = OWGUI.button(hbox, self, "OK", callback = self.accept)
1076        self.cancelButton = OWGUI.button(hbox, self, "Cancel", callback = self.reject)
1077        self.okButton.setDefault(True)
1078
1079        self.layout().addWidget(self.tabs)
1080        self.layout().addWidget(hbox)
1081
1082
1083class AboutDlg(QDialog):
1084    def __init__(self, *args):
1085        QDialog.__init__(self, *args)
1086        self.topLayout = QVBoxLayout(self)
1087#        self.setWindowFlags(Qt.Popup)       # Commented out, because it makes the window appear in the top-left corner on Linux
1088        self.setWindowTitle("About Orange")
1089       
1090        import orngEnviron
1091        import Orange
1092        import re
1093        logoImage = QPixmap(os.path.join(orngEnviron.directoryNames["canvasDir"], "icons", "splash.png"))
1094        logo = OWGUI.widgetLabel(self, "")
1095        logo.setPixmap(logoImage)
1096       
1097        OWGUI.widgetLabel(self, '<p align="center"><h2>Orange</h2></p>') 
1098       
1099        default_version_str = Orange.__version__
1100        built_on = re.findall("\((.*?)\)", Orange.orange.version)
1101        if built_on:
1102            built_on_str = " (built on " + built_on[0].split(",")[-1] + ")"
1103        else:
1104            built_on_str = ""
1105        try:
1106            import Orange.version as version
1107            short_version = version.short_version
1108            hg_revision = version.hg_revision
1109            OWGUI.widgetLabel(self, '<p align="center">version %s</p>' % (short_version + built_on_str))
1110            if not version.release:
1111                OWGUI.widgetLabel(self, '<p align="center">(hg revision %s)</p>' % (hg_revision))
1112        except ImportError:
1113            OWGUI.widgetLabel(self, '<p align="center">version %s</p>' % (default_version_str + built_on_str))
1114        OWGUI.widgetLabel(self, "" )
1115        #OWGUI.button(self, self, "Close", callback = self.accept)
1116        b = QDialogButtonBox(self)
1117        b.setCenterButtons(1)
1118        self.layout().addWidget(b)
1119        butt = b.addButton(QDialogButtonBox.Close)
1120        self.connect(butt, SIGNAL("clicked()"), self.accept)
1121       
1122
1123class saveApplicationDlg(QDialog):
1124    def __init__(self, *args):
1125        import Orange.utils.addons
1126       
1127        apply(QDialog.__init__,(self,) + args)
1128        self.setWindowTitle("Set Widget Order")
1129        self.setLayout(QVBoxLayout())
1130
1131        listbox = OWGUI.widgetBox(self, 1, orientation = "horizontal", sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum))
1132        self.tabOrderList = QListWidget(listbox)
1133        self.tabOrderList.setSelectionMode(QListWidget.SingleSelection)
1134        listbox.layout().addWidget(self.tabOrderList)
1135
1136        w = OWGUI.widgetBox(listbox, sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding))
1137        self.upButton = OWGUI.button(w, self, "Up", callback = self.moveUp)
1138        self.downButton = OWGUI.button(w, self, "Down", callback = self.moveDown)
1139        w.layout().addStretch(1)
1140
1141        hbox = OWGUI.widgetBox(self, orientation = "horizontal", sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed))
1142        OWGUI.button(hbox, self, "Add Separator", callback = self.insertSeparator)
1143        hbox.layout().addStretch(1)
1144        OWGUI.button(hbox, self, "&OK", callback = self.accept)
1145        OWGUI.button(hbox, self, "&Cancel", callback = self.reject)
1146
1147        self.resize(200,250)
1148
1149    def accept(self):
1150        self.shownWidgetList = [str(self.tabOrderList.item(i).text()) for i in range(self.tabOrderList.count())]
1151        QDialog.accept(self)
1152
1153    def insertSeparator(self):
1154        curr = self.tabOrderList.indexFromItem(self.tabOrderList.currentItem()).row()
1155        self.insertWidgetName("[Separator]", curr)
1156
1157    def insertWidgetName(self, name, index = -1):
1158        if index == -1:
1159            self.tabOrderList.addItem(name)
1160        else:
1161            self.tabOrderList.insertItem(index, name)
1162
1163    # move selected widget category up
1164    def moveUp(self):
1165        for i in range(1, self.tabOrderList.count()):
1166            if self.tabOrderList.item(i).isSelected():
1167                item = self.tabOrderList.takeItem(i)
1168                for j in range(self.tabOrderList.count()): self.tabOrderList.item(j).setSelected(0)
1169                self.tabOrderList.insertItem(i-1, item)
1170                item.setSelected(1)
1171
1172
1173    # move selected widget category down
1174    def moveDown(self):
1175        for i in range(self.tabOrderList.count()-2,-1,-1):
1176            if self.tabOrderList.item(i).isSelected():
1177                item = self.tabOrderList.takeItem(i)
1178                for j in range(self.tabOrderList.count()): self.tabOrderList.item(j).setSelected(0)
1179                self.tabOrderList.insertItem(i+1, item)
1180                item.setSelected(1)
1181
1182
1183if __name__=="__main__":
1184    import sys
1185    app = QApplication(sys.argv)
1186    #dlg = saveApplicationDlg(None)
1187    dlg = AboutDlg(None)
1188    dlg.show()
1189    app.exec_()
Note: See TracBrowser for help on using the repository browser.