source: orange/Orange/utils/debugging.py @ 10651:4f6fcf57db06

Revision 10651:4f6fcf57db06, 4.4 KB checked in by markotoplak, 2 years ago (diff)

Moved caching, collections, debugging, fileutil, r, testing from misc to utils.

Line 
1from __future__ import absolute_import
2# a simple module used in orange to see whether debugging is enabled, where to write the output and how much output do you want to see
3
4orngDebuggingEnabled = 0                            # do we want to enable debugging
5orngDebuggingFileName = "debuggingOutput.txt"       # where do we want to write output to
6orngVerbosity = 1                                   # what's the level of verbosity
7
8import weakref
9
10from collections import defaultdict
11
12import PyQt4.QtGui
13from PyQt4 import QtCore, QtGui
14from PyQt4.QtTest import QTest
15from PyQt4.QtCore import Qt
16
17import random
18class debug(object):
19    elements_list = defaultdict(list)
20    random = random
21
22    @classmethod
23    def registerQItemView(cls, widget, view):
24        cls.elements_list[widget].append(view)
25
26    @classmethod
27    def regiseterQGraphicsView(cls, widget, view):
28        cls.elements_list[widget].append(view)
29
30    @classmethod
31    def registerQwtPlot(cls, widget, graph):
32        cls.elements_list[widget].append(graph)
33
34    @classmethod
35    def register(cls, widget, element):
36        from PyQt4.QtGui import QGraphicsView, QAbstractItemView
37        from OWGraph import OWGraph
38        if isinstance(element, QGraphicsView):
39            cls.regiseterQGraphicsView(widget, element)
40        elif isinstance(element, QAbstractItemView):
41            cls.registerQItemView(widget, element)
42        elif isinstance(element, OWGraph):
43            cls.registerOWGraph(widget, element)
44        else:
45            print "Unhandled type", element
46
47    @classmethod
48    def scrollAreaInteract(cls, area):
49    #        print "scrollAreaInteract", area
50        from PyQt4.QtTest import QTest
51        geom = area.geometry()
52        randpos = lambda: geom.topLeft() + QtCore.QPoint(geom.width() * random.random(), geom.height() * random.random())
53        QTest.mouseMove(area, randpos(), 2)
54        QTest.mouseClick(area, Qt.LeftButton, pos=randpos(), delay=2)
55        QTest.mouseDClick(area, Qt.LeftButton, pos=randpos(), delay=2)
56        QTest.mousePress(area, Qt.LeftButton, pos=randpos(), delay=2)
57        QTest.mouseRelease(area, Qt.LeftButton, pos=randpos(), delay=2)
58
59    #        area.scrollContentsBy(random.randint(-10, 10), random.randint(-10, 10))
60
61
62    @classmethod
63    def itemViewInteract(cls, view):
64        cls.scrollAreaInteract(view)
65
66    @classmethod
67    def graphicsViewInteract(cls, view):
68        cls.scrollAreaInteract(view)
69
70    @classmethod
71    def graphInteract(cls, view):
72        cls.scrollAreaInteract(view)
73
74    @classmethod
75    def interact(cls, widget):
76        from PyQt4.QtGui import QGraphicsView, QAbstractItemView
77        from OWGraph import OWGraph
78        if isinstance(widget, QGraphicsView):
79            cls.graphicsViewInteract(widget)
80        elif isinstance(widget, QAbstractItemView):
81            cls.itemViewInteract(widget)
82        elif isinstance(widget, OWGraph):
83            cls.graphInteract(widget)
84        else:
85            print "Unhandled widget interaction", widget
86
87    @classmethod
88    def interactWithOWWidget(cls, widget):
89        views = cls.candidateDebugWidgets(widget)
90        for view in views:
91            if view.isEnabled() and getattr(view, "debuggingEnabled", True):
92                cls.interact(view)
93
94    @classmethod
95    def candidateDebugWidgets(cls, widget):
96        from PyQt4.QtGui import QGraphicsView, QAbstractItemView
97        from OWGraph import QwtPlot
98
99        gviews_list = widget.findChildren(QGraphicsView)
100        iviews_list = widget.findChildren(QAbstractItemView)
101        pviews_list = widget.findChildren(QwtPlot)
102
103        return gviews_list + iviews_list + pviews_list
104
105#    @classmethod
106#    def candidateDebugWidgets(cls, widget, cls_list=None):
107#        if cls_list is None:
108#            from PyQt4.QtGui import QGraphicsView, QAbstractItemView
109#            from OWGraph import QwtPlot
110#            cls_list = [QGraphicsView, QAbstractItemView, QwtPlot]
111#        candidates = []
112#        for child in widget.children():
113#            print [(child, isinstance(child, db_cls)) for db_cls in cls_list]
114#            if any(isinstance(child, db_cls) for db_cls in cls_list):
115#                if getattr(child, "debuggingEnabled", True):
116#                    candidates.append(child)
117#                    candidates.extend(cls.candidateWidgets(child, cls_list))
118#            else:
119#                candidates.extend(cls.candidateWidgets(child, cls_list))
120#        return candidates
121
122
123
Note: See TracBrowser for help on using the repository browser.