summaryrefslogtreecommitdiff
path: root/df10ch_setup_pkg/areas_dlg.py
blob: 3eeb8c7cab22011fa5f4dda985d69ea5112f0393 (plain)
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
229
230
231
232
233
234
235
#
# Copyright (C) 2010 Andreas Auras
#
# This file is part of the DF10CH Atmolight controller project.
#
# DF10CH Atmolight controller is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# DF10CH Atmolight controller is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
#
# This file is part of the DF10CH setup program
#

from Tkinter import *
import device_drv

class AreasDialog:
    def __init__(self, master=None, **args):
        self.root = Canvas(master, **args)
        self.selectedArea = None
        self.selectCallbacks = list()
        
    def configAreas(self, numAreas):
        nTopLeft = numAreas[device_drv.AreaIndex("TopLeft")]
        nTopRight = numAreas[device_drv.AreaIndex("TopRight")]
        nBottomLeft = numAreas[device_drv.AreaIndex("BottomLeft")]
        nBottomRight = numAreas[device_drv.AreaIndex("BottomRight")]
        nCenter = numAreas[device_drv.AreaIndex("Center")]
        nTop = numAreas[device_drv.AreaIndex("Top")]
        nBottom = numAreas[device_drv.AreaIndex("Bottom")]
        nLeft = numAreas[device_drv.AreaIndex("Left")]
        nRight = numAreas[device_drv.AreaIndex("Right")]

        cv = self.root
        size = 0
        width = int(cv["width"])
        height = int(cv["height"])
        
        sTop = nTop + nTopLeft + nTopRight
        if sTop:
            topSize = int(width / sTop)
            topSizeEnd = width - topSize * (sTop - 1)
            if not size or topSize < size:
                size = topSize
            
        sBottom = nBottom + nBottomLeft + nBottomRight
        if sBottom:
            bottomSize = int(width / sBottom)
            bottomSizeEnd = width - bottomSize * (sBottom - 1)
            if not size or bottomSize < size:
                size = bottomSize
        
        sLeft = nLeft + nTopLeft + nBottomLeft
        if sLeft:
            leftSize = int(height / sLeft)
            leftSizeEnd = height - leftSize * (sLeft - 1)
            if not size or leftSize < size:
                size = leftSize

        sRight = nRight + nTopRight + nBottomRight
        if sRight:
            rightSize = int(height / sRight)
            rightSizeEnd = height - rightSize * (sRight - 1)
            if not size or rightSize < size:
                size = rightSize

        if not size or int(width / 2) < size:
            size = int(width / 2)
        if not size or int(height / 2) < size:
            size = int(height / 2)
            
        pTop = [ None ] * nTop
        x = 0
        
            # top left corner
        if nTopLeft:
            pTopLeft = [ 0, 0, topSize, 0, topSize, size, size, size, size, leftSize, 0, leftSize ]
            x = topSize
        elif nTop == 1:
            pTop[0] = [ 0, 0, width, 0, width - size, size, size, size ]
        elif nTop > 1:
            pTop[0] = [ 0, 0, topSize, 0, topSize, size, size, size ]

            # top right corner
        if nTopRight:
            pTopRight = [ width, 0, width, rightSize, width - size, rightSize, width - size, size, width - topSizeEnd, size, width - topSizeEnd, 0 ]
        elif nTop > 1:
            pTop[nTop - 1] = [ width, 0, width - size, size, width - topSizeEnd, size, width - topSizeEnd, 0 ]

            # top
        for i in range(nTop):
            if pTop[i] == None:
                pTop[i] = [ x, 0, x + topSize, 0, x + topSize, size, x, size ]
            x = x + topSize

        pBottom = [ None ] * nBottom
        x = 0
        
            # bottom left corner
        if nBottomLeft:
            pBottomLeft = [ 0, height, 0, height - leftSizeEnd, size, height - leftSizeEnd, size, height - size, bottomSize, height - size, bottomSize, height ]
            x = bottomSize
        elif nBottom == 1:
            pBottom[0] = [ 0, height, size, height - size, width - size, height - size, width, height ]
        elif nBottom > 1:
            pBottom[0] = [ 0, height, size, height - size, bottomSize, height - size, bottomSize, height ]

            # bottom right corner
        if nBottomRight:
            pBottomRight = [ width, height, width - bottomSizeEnd, height, width - bottomSizeEnd, height - size, width - size, height - size, width - size, height - rightSizeEnd, width, height - rightSizeEnd ]
        elif nBottom > 1:
            pBottom[nBottom - 1] = [ width, height, width - bottomSizeEnd, height, width - bottomSizeEnd, height - size, width - size, height - size ]

            # bottom
        for i in range(nBottom):
            if pBottom[i] == None:
                pBottom[i] = [ x, height, x, height - size, x + bottomSize, height - size, x + bottomSize, height ]
            x = x + bottomSize

        pLeft = [ None ] * nLeft
        y = 0

            # left top corner
        if nTopLeft:
            y = leftSize
        elif nLeft == 1:
            pLeft[0] = [ 0, 0, size, size, size, height - size, 0, height ]
        elif nLeft > 1:
            pLeft[0] = [ 0, 0, size, size, size, leftSize, 0, leftSize ]

            # left bottom corner
        if not nBottomLeft and nLeft > 1:
            pLeft[nLeft - 1] = [ 0, height, 0, height - leftSizeEnd, size, height - leftSizeEnd, size, height - size ]

            # left
        for i in range(nLeft):    
            if pLeft[i] == None:
                pLeft[i] = [ 0, y, size, y, size, y + leftSize, 0, y + leftSize ]
            y = y + leftSize
            
        pRight = [ None ] * nRight
        y = 0

            # right top corner
        if nTopRight:
            y = rightSize
        elif nRight == 1:
            pRight[0] = [ width, 0, width, height, width - size, height - size, width - size, size ]
        elif nRight > 1:
            pRight[0] = [ width, 0, width, rightSize, width - size, rightSize, width - size, size ]

            # right bottom corner
        if not nBottomRight and nRight > 1:
            pRight[nRight - 1] = [ width, height, width - size, height - size, width - size, height - rightSizeEnd, width, height - rightSizeEnd ]

            # right
        for i in range(nRight):    
            if pRight[i] == None:
                pRight[i] = [ width, y, width, y + rightSize, width - size, y + rightSize, width - size, y ]
            y = y + rightSize

            # center
        if nCenter:
            pCenter = [ size, size, width - size, size, width - size, height - size, size, height - size ]

            # Build canvas items
        cv.delete(ALL)
        
        if nTopLeft:
            cv.create_polygon(pTopLeft, tags="TopLeft")
        if nTopRight:
            cv.create_polygon(pTopRight, tags="TopRight")
        if nBottomLeft:
            cv.create_polygon(pBottomLeft, tags="BottomLeft")
        if nBottomRight:
            cv.create_polygon(pBottomRight, tags="BottomRight")
        if nCenter:
            cv.create_polygon(pCenter, tags="Center")
        
        for i in range(nTop):
            cv.create_polygon(pTop[i], tags="Top-{0}".format(i + 1))
        for i in range(nBottom):
            cv.create_polygon(pBottom[i], tags="Bottom-{0}".format(i + 1))
        for i in range(nLeft):
            cv.create_polygon(pLeft[i], tags="Left-{0}".format(i + 1))
        for i in range(nRight):
            cv.create_polygon(pRight[i], tags="Right-{0}".format(i + 1))
            
        for id in cv.find_all():
            cv.itemconfigure(id, outline="#808080", width=3)
            cv.tag_bind(id, "<Button-1>", self.cbSelectArea)

    def initAreas(self, color):
        self.root.configure(background=color)
        for id in self.root.find_all():
            self.root.itemconfigure(id, fill=color, outline="#808080")
        self.selectedArea = None
        
    def setAreaColor(self, area, color):
        self.root.itemconfigure(area, fill=color)

    def selectArea(self, area):
        if self.selectedArea != area:
            cv = self.root
            if self.selectedArea:
                cv.itemconfigure(self.selectedArea, outline="#808080")
            idList = cv.find_withtag(area)
            if len(idList):
                id = idList[0]
                cv.tag_raise(id)
                cv.itemconfigure(id, outline="yellow")
                self.selectedArea = area

    def cbSelectArea(self, event):
        cv = self.root
        idList = cv.find_closest(cv.canvasx(event.x), cv.canvasx(event.y))
        if len(idList):
            id = idList[0]
            if self.selectedArea:
                cv.itemconfigure(self.selectedArea, outline="#808080")
            self.selectedArea = cv.gettags(id)[0]
            cv.tag_raise(id)
            cv.itemconfigure(id, outline="yellow")

        for cb in self.selectCallbacks:
            cb.cbAreaSelected()