File: tooltips.py

package info (click to toggle)
python-qt4 4.0.1-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 18,632 kB
  • ctags: 2,639
  • sloc: python: 29,409; sh: 5,646; cpp: 3,168; xml: 149; makefile: 109
file content (228 lines) | stat: -rwxr-xr-x 8,498 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python

#############################################################################
##
## Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
##
## This file is part of the example classes of the Qt Toolkit.
##
## This file may be used under the terms of the GNU General Public
## License version 2.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of
## this file.  Please review the following information to ensure GNU
## General Public Licensing requirements will be met:
## http://www.trolltech.com/products/qt/opensource.html
##
## If you are unsure which license is appropriate for your use, please
## review the following information:
## http://www.trolltech.com/products/qt/licensing.html or contact the
## sales department at sales@trolltech.com.
##
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
##
#############################################################################

import sys
import random
from PyQt4 import QtCore, QtGui

import tooltips_rc


class ShapeItem:
    def __init__(self):    
        self.myPath = QtGui.QPainterPath()
        self.myPosition = QtCore.QPoint()
        self.myColor  = QtGui.QColor()
        self.myToolTip = QtCore.QString ()
    
    def path(self):
        return self.myPath
    
    def position(self):
        return self.myPosition
    
    def color(self):
        return self.myColor
    
    def toolTip(self):
        return self.myToolTip
    
    def setPath(self, path):
        self.myPath = path
    
    def setToolTip(self, toolTip):
        self.myToolTip = toolTip
    
    def setPosition(self, position):
        self.myPosition = position
    
    def setColor(self, color):
        self.myColor = color


class SortingBox(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        
        self.circlePath = QtGui.QPainterPath()
        self.squarePath = QtGui.QPainterPath()
        self.trianglePath = QtGui.QPainterPath()
        self.shapeItems = []        
    
        self.previousPosition = QtCore.QPoint()
        self.itemInMotion = ShapeItem()
        
        self.setAttribute(QtCore.Qt.WA_StaticContents)
        self.setMouseTracking(True)
        self.setBackgroundRole(QtGui.QPalette.Base)
    
        self.itemInMotion = 0
    
        self.newCircleButton = self.createToolButton(self.tr("New Circle"), QtGui.QIcon(":/images/circle.png"), self.createNewCircle)
        self.newSquareButton = self.createToolButton(self.tr("New Square"), QtGui.QIcon(":/images/square.png"), self.createNewSquare)
        self.newTriangleButton = self.createToolButton(self.tr("New Triangle"), QtGui.QIcon(":/images/triangle.png"), self.createNewTriangle)
    
        self.circlePath.addEllipse(0, 0, 100, 100)
        self.squarePath.addRect(0, 0, 100, 100)
    
        x = self.trianglePath.currentPosition().x()
        y = self.trianglePath.currentPosition().y()
        self.trianglePath.moveTo(x + 120 / 2, y)
        self.trianglePath.lineTo(0, 100)
        self.trianglePath.lineTo(120, 100)
        self.trianglePath.lineTo(x + 120 / 2, y)
    
        self.setWindowTitle(self.tr("Tooltips"))
        self.resize(500, 300)
    
        self.createShapeItem(self.circlePath, self.tr("Circle"), self.initialItemPosition(self.circlePath), self.initialItemColor())
        self.createShapeItem(self.squarePath, self.tr("Square"), self.initialItemPosition(self.squarePath), self.initialItemColor())
        self.createShapeItem(self.trianglePath, self.tr("Triangle"), self.initialItemPosition(self.trianglePath), self.initialItemColor())

    def event(self, event):
        if event.type() == QtCore.QEvent.ToolTip:
            helpEvent = event
            index = self.itemAt(helpEvent.pos())
            if index != -1:
                QtGui.QToolTip.showText(helpEvent.globalPos(), self.shapeItems[index].toolTip())
        
        return QtGui.QWidget.event(self, event)
    
    def resizeEvent(self, event):
        margin = self.style().pixelMetric(QtGui.QStyle.PM_DefaultTopLevelMargin)
        x = self.width() - margin
        y = self.height() - margin
    
        y = self.updateButtonGeometry(self.newCircleButton, x, y)
        y = self.updateButtonGeometry(self.newSquareButton, x, y)
        self.updateButtonGeometry(self.newTriangleButton, x, y)
    
    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        for shapeItem in self.shapeItems:
            painter.translate(shapeItem.position())
            painter.setBrush(shapeItem.color())
            painter.drawPath(shapeItem.path())
            painter.translate(-shapeItem.position())
    
    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            index = self.itemAt(event.pos())
            if index != -1:
                self.itemInMotion = self.shapeItems[index]
                self.previousPosition = event.pos()
                
                # hack to substitute the QList.move()
                value = self.shapeItems[index]
                del self.shapeItems[index]
                self.shapeItems.insert(len(self.shapeItems) - 1, value)

                self.update()
    
    def mouseMoveEvent(self, event):
        if (event.buttons() & QtCore.Qt.LeftButton) and self.itemInMotion:
            self.moveItemTo(event.pos())
    
    def mouseReleaseEvent(self, event):
        if (event.button() == QtCore.Qt.LeftButton) and self.itemInMotion:
            self.moveItemTo(event.pos())
            self.itemInMotion = 0

    def createNewCircle(self):
        self.createShapeItem(self.circlePath, self.tr("Circle"), self.randomItemPosition(), self.randomItemColor())
    
    def createNewSquare(self):
        self.createShapeItem(self.squarePath, self.tr("Square"), self.randomItemPosition(), self.randomItemColor())
    
    def createNewTriangle(self):
        self.createShapeItem(self.trianglePath, self.tr("Triangle"), self.randomItemPosition(), self.randomItemColor())
    
    def itemAt(self, pos):
        for i in range(len(self.shapeItems) - 1, -1, -1):
            item = self.shapeItems[i]
            if item.path().contains(QtCore.QPointF(pos - item.position())):
                return i
                
        return -1
    
    def moveItemTo(self, pos):
        offset = pos - self.previousPosition
        self.itemInMotion.setPosition(self.itemInMotion.position() + offset)
        self.previousPosition = QtCore.QPoint(pos)
        self.update()
    
    def updateButtonGeometry(self, button, x, y):
        size = button.sizeHint()
        button.setGeometry(x - size.width(), y - size.height(), size.width(), size.height())
    
        return y - size.height() - self.style().pixelMetric(QtGui.QStyle.PM_DefaultLayoutSpacing)
    
    def createShapeItem(self, path, toolTip, pos, color):
        shapeItem = ShapeItem()
        shapeItem.setPath(path)
        shapeItem.setToolTip(toolTip)
        shapeItem.setPosition(pos)
        shapeItem.setColor(color)
        self.shapeItems.append(shapeItem)
        self.update()
    
    def createToolButton(self, toolTip, icon, member):
        button = QtGui.QToolButton(self)
        button.setToolTip(toolTip)
        button.setIcon(icon)
        button.setIconSize(QtCore.QSize(32, 32))
        self.connect(button, QtCore.SIGNAL("clicked()"), member)
    
        return button
    
    def initialItemPosition(self, path):
        y = (self.height() - path.controlPointRect().height()) / 2

        if len(self.shapeItems) == 0:
            x = ((3 * self.width()) / 2 - path.controlPointRect().width()) / 2
        else:
            x = (self.width() / len(self.shapeItems) - path.controlPointRect().width()) / 2
    
        return QtCore.QPoint(x, y)
    
    def randomItemPosition(self):
        x = random.randint(0, self.width() - 120)
        y = random.randint(0, self.height() - 120)
        
        return QtCore.QPoint(x, y)
    
    def initialItemColor(self):
        hue = ((len(self.shapeItems) + 1) * 85) % 256
        return QtGui.QColor.fromHsv(hue, 255, 190)

    def randomItemColor(self):
        return QtGui.QColor.fromHsv(random.randint(0, 256), 255, 190)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    sortingBox = SortingBox()
    sortingBox.show()
    sys.exit(app.exec_())