File: mainwindow.py

package info (click to toggle)
fife 0.3.3%2Br3-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 16,312 kB
  • sloc: cpp: 83,234; python: 16,261; sh: 10,134; xml: 3,435; makefile: 265; objc: 245; ansic: 229
file content (362 lines) | stat: -rw-r--r-- 12,078 bytes parent folder | download
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# -*- coding: utf-8 -*-

# ####################################################################
#  Copyright (C) 2005-2009 by the FIFE team
#  http://www.fifengine.de
#  This file is part of FIFE.
#
#  FIFE is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#
#  This library 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the
#  Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
# ####################################################################

from fife.extensions import pychan
from menubar import MenuBar, Menu
from panel import Panel
from dockarea import DockArea
import toolbar
from toolbar import ToolBar
from statusbar import StatusBar
from fife import fife

DOCKAREA = {
	'left'	: 'left',
	'right' : 'right',
	'top'	: 'top',
	'bottom': 'bottom'
}

class MainWindow(object):

	def __init__(self, *args, **kwargs):
		self._toolbar = None
		self._menubar = None
		self._statusbar = None
	
		self._rootwidget = None
		self._centralwidget = None
		self._dockareas = {
				DOCKAREA['left']:None, 
				DOCKAREA['right']:None, 
				DOCKAREA['top']:None, 
				DOCKAREA['bottom']:None
			}
			
		self._toolbarareas = {
				DOCKAREA['left']:None, 
				DOCKAREA['right']:None, 
				DOCKAREA['top']:None, 
				DOCKAREA['bottom']:None
			}
			
		self.dockareamarker = None
			
	def initGui(self, screen_width, screen_height):
		bar_height = 30

		self._dockareas[DOCKAREA['left']] = DockArea("left")
		self._dockareas[DOCKAREA['right']] = DockArea("right")
		self._dockareas[DOCKAREA['top']] = DockArea("top")
		self._dockareas[DOCKAREA['bottom']] = DockArea("bottom")

		self._toolbarareas[DOCKAREA['left']] = pychan.widgets.VBox(margins=(0,0,0,0))
		self._toolbarareas[DOCKAREA['right']] = pychan.widgets.VBox(margins=(0,0,0,0))
		self._toolbarareas[DOCKAREA['top']] = pychan.widgets.HBox(margins=(0,0,0,0))
		self._toolbarareas[DOCKAREA['bottom']] = pychan.widgets.HBox(margins=(0,0,0,0))
		
		self._statusbar = StatusBar(text=u"", panel_size=bar_height)
		self._toolbar = ToolBar(title=u"Toolbar", button_style=0)
		self._menubar = MenuBar(min_size=(screen_width, bar_height), position=(0, 0))
		
		# Set up root widget
		self._rootwidget = pychan.widgets.VBox(padding=0, vexpand=1, hexpand=1)
		self._rootwidget.min_size = \
		self._rootwidget.max_size = (screen_width, screen_height)
		self._rootwidget.opaque = False
		
		# This is where the map will be displayed
		self._centralwidget = pychan.widgets.VBox(vexpand=1, hexpand=1)
		self._centralwidget.opaque = False
		
		middle = pychan.widgets.HBox(padding=0, vexpand=1, hexpand=1)
		middle.opaque = False
		
		# Pychan bug? Adding a spacer instead of a container creates
		# a gap after the right dockarea
		middle.addChild(self._toolbarareas['left'])
		middle.addChild(self._dockareas['left'])
		middle.addChild(self._centralwidget)
		#middle.addSpacer(pychan.widgets.Spacer())
		middle.addChild(self._dockareas['right'])
		middle.addChild(self._toolbarareas['right'])
		
		self._rootwidget.addChild(self._menubar)
		#self._rootwidget.addChild(self._toolbar)
		self._rootwidget.addChild(self._toolbarareas['top'])
		self._rootwidget.addChild(self._dockareas['top'])
		self._rootwidget.addChild(middle)
		self._rootwidget.addChild(self._dockareas['bottom'])
		self._rootwidget.addChild(self._toolbarareas['bottom'])
		self._rootwidget.addChild(self._statusbar)

		self._toolbar.setDocked(True)
		self.dockWidgetTo(self._toolbar, "top")
		
		self._rootwidget.show()
		
	def getCentralWidget(self):
		return self._centralwidget
	
	def getStatusBar(self): 
		return self._statusbar
		
	def getMenuBar(self):
		return self._menubar
	
	def getToolBar(self): 
		return self._toolbar
	
	def dockWidgetTo(self, widget, dockarea, x=-1, y=-1):
		""" docks a B{Panel} widget to the given dock area
		
		@note:	
			- ToolBar needs special treatment, given x, y coordinates
			  have no effect on this Panel
			- x, y are cursor coordinates, without giving them the
			  widgets are added beneath the already existing ones
			  nice feature, but also makes problems on editor startup
			  (e.g. if a user docked 2 widgets left, on startup they
			   are added in a col instead of a grouped tab)
				
		@todo:	turn x,y documentation into something more useful
		
		@type	widget:	Panel
		@param	widget:	a panel widget instance
		@type	dockarea:	str
		@param	dockarea:	id of the target dock area
		@type	x:	int
		@param	x:	x coordinate
		@type	y:	int
		@param	y:	y coordinate
		"""
		if isinstance(widget, pychan.widgets.Widget) is False:
			print "Argument is not a valid widget"
			return
			
		if widget.parent:
			widgetParent = widget.parent
			widgetParent.removeChild(widget)
			widgetParent.adaptLayout()
			
		# We must hide the widget before adding it to the dockarea,
		# or we will get a duplicate copy of the widget in the top left corner
		# of screen.
		widget.hide() 
		dockareas = self._dockareas
		
		# Panel widgets which provide an implementation for set_orientation()
		# should check if they need to re-align themselves
		if hasattr(widget, 'set_orientation'):
			key = ''
			if dockarea == DOCKAREA['left'] or dockarea == DOCKAREA['right']:
				key = 'Vertical'
			elif dockarea == DOCKAREA['top'] or dockarea == DOCKAREA['bottom']:
				key = 'Horizontal'
			widget.set_orientation(key=key)
	
		if isinstance(widget, ToolBar):
			dockareas = self._toolbarareas
			docked = False
			if dockarea == DOCKAREA['left']:
				docked = True
				dockareas[DOCKAREA['left']].addChild(widget)
				dockareas[DOCKAREA['left']].adaptLayout()
				
			elif dockarea == DOCKAREA['right']:
				docked = True
				dockareas[DOCKAREA['right']].addChild(widget)
				dockareas[DOCKAREA['right']].adaptLayout()
				
			elif dockarea == DOCKAREA['top']:
				docked = True
				dockareas[DOCKAREA['top']].addChild(widget)
				dockareas[DOCKAREA['top']].adaptLayout()
				
			elif dockarea == DOCKAREA['bottom']:
				docked = True
				dockareas[DOCKAREA['bottom']].addChild(widget)
				dockareas[DOCKAREA['bottom']].adaptLayout()
				
			else:
				print "Invalid dockarea"
			
			widget.dockareaname = dockarea
			widget.setDocked(docked)
				
		else:
			if dockarea == DOCKAREA['left']:
				dockareas[DOCKAREA['left']].dockChild(widget, x, y)
				
			elif dockarea == DOCKAREA['right']:
				dockareas[DOCKAREA['right']].dockChild(widget, x, y)
				
			elif dockarea == DOCKAREA['top']:
				dockareas[DOCKAREA['top']].dockChild(widget, x, y)
				
			elif dockarea == DOCKAREA['bottom']:
				dockareas[DOCKAREA['bottom']].dockChild(widget, x, y)
				
			else:
				print "Invalid dockarea"

	def getToolbarAreaAt(self, x, y, mark=False):
		if self.dockareamarker is None:
			self.dockareamarker = pychan.widgets.Container()
			self.dockareamarker.base_color = fife.Color(200, 0, 0, 100)
		if mark is False:
			self.dockareamarker.hide()
	
		# Mouse wasn't over any dockwidgets. See if it is near any edge of the screen instead
		if x <= self._toolbarareas["left"].getAbsolutePos()[0]+10:
			if mark:
				self.dockareamarker.position = self._toolbarareas["left"].getAbsolutePos()
				self.dockareamarker.size = (10, self._toolbarareas["left"].height)
				self.dockareamarker.show()
			return DOCKAREA["left"]
			
		elif x >= self._toolbarareas["right"].getAbsolutePos()[0]-10:
			if mark:
				self.dockareamarker.position = self._toolbarareas["right"].getAbsolutePos()
				self.dockareamarker.size = (10, self._toolbarareas["right"].height)
				self.dockareamarker.x -= 10
				self.dockareamarker.show()
			return DOCKAREA["right"]
			
		elif y <= self._toolbarareas["top"].getAbsolutePos()[1]+10:
			if mark:
				self.dockareamarker.position = self._toolbarareas["top"].getAbsolutePos()
				self.dockareamarker.size = (self._toolbarareas["top"].width, 10)
				self.dockareamarker.show()
			return DOCKAREA["top"]
			
		elif y >= self._toolbarareas["bottom"].getAbsolutePos()[1]-10:
			if mark:
				self.dockareamarker.position = self._toolbarareas["bottom"].getAbsolutePos()
				self.dockareamarker.y -= 10
				self.dockareamarker.size = (self._toolbarareas["bottom"].width, 10)
				self.dockareamarker.show()
			return DOCKAREA["bottom"]

		if mark is True:
			self.dockareamarker.hide()
		return None
			
	def getDockAreaAt(self, x, y, mark=False):
		""" returns the dock area at the given cursor coordinates (if possible)
		
			also used for highlighting the dock area (this method is used
			on drag mouse events of a B{Panel} widget)
		
		@type	x:	int
		@param	x:	cursor x coordinates
		@type	y:	int
		@param	y:	cursor y coordinates
		@type	mark:	bool
		@param	mark:	flag to wether show the dock area marker (red area indicator) or not
		@rtype	side:	str
		@return	side:	dockarea id (e.g. 'right', 'left' ...)
		"""
		side = None
		
		if self.dockareamarker is None:
			self.dockareamarker = pychan.widgets.Container()
			self.dockareamarker.base_color = fife.Color(200, 0, 0, 100)
		if mark is False:
			self.dockareamarker.hide()
	
		for key in DOCKAREA:
			side = DOCKAREA[key]
			
			dockarea = self._dockareas[side]
			#absX, absY = dockarea.getAbsolutePos()
			#if absX <= x and absY <= y \
			#		and absX+dockarea.width >= x and absX+dockarea.height >= y:
			#	return side
			placeIn, placeBefore, placeAfter = dockarea.getDockLocation(x, y)
			if placeIn or placeBefore or placeAfter:
				if mark is True:
					if placeIn:
						self.dockareamarker.position = placeIn.getAbsolutePos()
						self.dockareamarker.size = placeIn.size
					elif placeBefore:
						self.dockareamarker.position = placeBefore.getAbsolutePos()
						if side == "left" or side == "right":
							self.dockareamarker.size = (placeBefore.width, 10)
						else:
							self.dockareamarker.size = (10, placeBefore.height)
					elif placeAfter:
						self.dockareamarker.position = placeAfter.getAbsolutePos()
						
						if side == "left" or side == "right":
							self.dockareamarker.size = (placeAfter.width, 10)
							self.dockareamarker.y += placeAfter.height-10
						else:
							self.dockareamarker.size = (10, placeAfter.height)
							self.dockareamarker.x += placeAfter.width-10
						
					self.dockareamarker.show()
				return side

		# reset side, next attempt to find a new home
		side = None

		# Mouse wasn't over any dockwidgets. See if it is near any edge of the screen instead
		if x <= self._dockareas["left"].getAbsolutePos()[0]+10:
			if mark:
				self.dockareamarker.position = self._dockareas["left"].getAbsolutePos()
				self.dockareamarker.size = (10, self._dockareas["left"].height)
				self.dockareamarker.show()
			side = DOCKAREA["left"]
			return side
			
		elif x >= self._dockareas["right"].getAbsolutePos()[0]-10:
			if mark:
				self.dockareamarker.position = self._dockareas["right"].getAbsolutePos()
				self.dockareamarker.size = (10, self._dockareas["right"].height)
				self.dockareamarker.x -= 10
				self.dockareamarker.show()
			side = DOCKAREA["right"]
			return side
			
		elif y <= self._dockareas["top"].getAbsolutePos()[1]+10:
			if mark:
				self.dockareamarker.position = self._dockareas["top"].getAbsolutePos()
				self.dockareamarker.size = (self._dockareas["top"].width, 10)
				self.dockareamarker.show()
			side = DOCKAREA["top"]
			return side
			
		elif y >= self._dockareas["bottom"].getAbsolutePos()[1]-10:
			if mark:
				self.dockareamarker.position = self._dockareas["bottom"].getAbsolutePos()
				self.dockareamarker.y -= 10
				self.dockareamarker.size = (self._dockareas["bottom"].width, 10)
				self.dockareamarker.show()
			side = DOCKAREA["bottom"]
			return side

		if mark is True:
			self.dockareamarker.hide()
			
		return side