QtSektorivalikko
Tavoite
Komponentti sektorivalikolle.
Satunnaista koodia
Tiedosto aiColors.py
Tiedosto sectormenus.py:
1 class SectorMenu(QGraphicsItem):
2 def __init__(self, position, scene, matrix=QMatrix(),parent=None):
3 super(SectorMenu, self).__init__()
4 self.penW=2
5 self.parent=parent
6 self.setFlags(QGraphicsItem.ItemIsSelectable|
7 QGraphicsItem.ItemIsFocusable)
8 self.innerRadius=16.0
9 self.outerRadius=42.0
10 r=self.innerRadius
11 R=self.outerRadius
12 self.outerRect=QRectF(-R, -R, 2*R, 2*R)
13 self.innerRect=QRectF(-r, -r, 2*r, 2*r)
14 self.sectors=self.calcSectors()
15 self.setPos(position)
16 #self.setPos(QPointF(0.0,0.0))
17 self.setMatrix(matrix)
18 scene.clearSelection()
19 scene.addItem(self)
20 self.setSelected(True)
21 self.setFocus()
22 self.setAcceptsHoverEvents(True)
23
24 def calcSectors(self):
25 sectors=[]
26 for start,angle in [(0,60),(70,60),(140,60),(210,60),(280,60)]:
27 startAngle = start#*16
28 spanAngle = angle#*16
29 # go radians
30 start=pi*start/180
31 angle=pi*angle/180
32 r=self.innerRadius+self.penW/2.0
33 R=self.outerRadius-self.penW/2.0
34 #x=position.x(); y=position.y()
35 x=0.0; y=0.0
36 P1=QPointF(x+r*cos(start),y-r*sin(start))
37 P2=QPointF(x+R*cos(start),y-R*sin(start))
38 P3=QPointF(x+r*cos(start+angle),y-r*sin(start+angle))
39 P4=QPointF(x+R*cos(start+angle),y-R*sin(start+angle))
40 #sectors.append((startAngle, spanAngle, P1,P2,P3,P4))
41 # find the path:
42 path=QPainterPath()
43 path.moveTo(P1)
44 path.lineTo(P2)
45 path.arcTo(self.outerRect, startAngle, spanAngle)
46 #path.arcTo(self.innerRect, startAngle, spanAngle)
47 #path.moveTo(P3)
48 path.lineTo(P4)
49 #path.arcTo(self.outerRect, startAngle, spanAngle)
50 path.arcTo(self.innerRect, startAngle+spanAngle, -spanAngle)
51 sectors.append(path)
52 return sectors
53
54 def shape(self):
55 path=QPainterPath()
56 for s in self.sectors:
57 path.addPath(s)
58 return path
59
60 def boundingRect(self):
61 return self.outerRect
62
63
64 def hoverMoveEvent(self, event):
65 print "hoverMoveEvent ", random.uniform(-100,100)
66
67
68 def paint(self, painter, option, widget):
69 pen = QPen(Qt.SolidLine)
70 pen.setColor(aiColor(idx=22))
71 pen.setWidth(self.penW)
72 painter.setPen(pen)
73 i=0
74 #for startAngle,spanAngle,P1,P2,P3,P4 in self.sectors:
75 for s in self.sectors:
76 i+=3
77 pen.setColor(aiColor(idx=i));painter.setPen(pen)
78 painter.setBrush(aiColor(idx=i))
79 painter.strokePath(s,pen)
80 painter.drawPath(s);
Keskustelua
Snifi: -- Voiko koodin taustaväriä muuttaa vähän iloisemmaksi (esim vaaleanharmaa)?