Package arcmap :: Module tilepalette
[hide private]
[frames] | no frames]

Source Code for Module arcmap.tilepalette

  1  ################################################################################ 
  2  # Authors: Brian Schott (Sir Alaran) 
  3  # Copyright: Brian Schott (Sir Alaran) 
  4  # Date: Sep 29 2009 
  5  # License: 
  6  # 
  7  # This program is free software: you can redistribute it and/or modify 
  8  # it under the terms of the GNU General Public License as published by 
  9  # the Free Software Foundation, either version 3 of the License, or 
 10  # (at your option) any later version. 
 11  # 
 12  # This program is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 15  # GNU General Public License for more details. 
 16  # 
 17  # You should have received a copy of the GNU General Public License 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 19  ################################################################################ 
 20   
 21   
 22   
 23  """ 
 24  This module contains classes that allow the user to select tiles from tile sets 
 25  """ 
 26   
 27  __docformat__ = "epytext" 
 28   
 29  import logging 
 30  import os 
 31   
 32  import gtk 
 33  import cairo 
 34   
 35  import mapcontroller 
 36  import tilegrid 
 37  import editortools 
 38  import graphics 
 39   
40 -class PaletteManager(mapcontroller.MapListener):
41 """ 42 Manages TilePalette instances. 43 Outside code should use the widget returned from getWidget 44 """ 45
46 - def __init__(self, controller):
47 """ 48 @type controller: MapController 49 @param controller: the map controller 50 """ 51 mapcontroller.MapListener.__init__(self, controller) 52 53 # Palettes are displayed in a notebook 54 self.tileNotebook = gtk.Notebook() 55 self.tileNotebook.set_scrollable(True) 56 57 # Keep references to these around so that they can have the grid toggled 58 self.__palettes = [] 59 60 # Code for allowing new tile sets to be opened 61 # This is an open button that goes in the rightmost tab of the notebook 62 self.openButton = gtk.Button() 63 self.openButton.set_image(gtk.image_new_from_stock(gtk.STOCK_OPEN, 64 gtk.ICON_SIZE_MENU)) 65 self.openButton.set_relief(gtk.RELIEF_NONE) 66 self.openButton.connect("clicked", self.tilePalleteOpen) 67 self.openButton.set_sensitive(False) 68 69 # Create an empty scrolled window as a placeholder for the open button 70 # tab. TODO: think of a better widget for this? 71 scrolledWindow = gtk.ScrolledWindow() 72 scrolledWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) 73 self.tileNotebook.append_page(scrolledWindow, self.openButton)
74
75 - def listenFileOpened(self):
76 """ 77 Gets the PaletteManager ready for use. This is called when a file 78 is opened 79 sel = the selection to use 80 """ 81 self.openButton.set_sensitive(True)
82
83 - def listenFileClosed(self):
84 """ 85 The inverse of initialize. This is called when the map file is closed. 86 """ 87 self.openButton.set_sensitive(False) 88 self.closeAll()
89
90 - def listenAddTileSet(self, fileName):
91 self.addTileSet(fileName)
92
93 - def getWidget(self):
94 """ 95 @rtype: gtk.Widget 96 @return: Return the widget that should be added to the toplevel window 97 """ 98 self.tileNotebook.show_all() 99 return self.tileNotebook
100
101 - def toggleGrid(self):
102 for palette in self.__palettes: 103 palette.toggleGrid()
104
105 - def closeTilePalette(self, widget, tilePalette):
106 """ Closes a tab in the notebook """ 107 index = self.tileNotebook.page_num(tilePalette) 108 self.tileNotebook.remove_page(index)
109
110 - def tilePalleteOpen(self, widget):
111 """ This is called when self.openButton is clicked """ 112 #~ Create the dialog 113 dialog = gtk.FileChooserDialog("Open Tileset", 114 self.tileNotebook.get_toplevel(), gtk.FILE_CHOOSER_ACTION_OPEN, 115 (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, 116 gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT)) 117 118 # Only allow images to be opened 119 # These should be installed... 120 # TODO: Query GDK for this? 121 filter = graphics.getFileFilter() 122 dialog.add_filter(filter) 123 124 response = dialog.run() 125 if response == gtk.RESPONSE_ACCEPT: 126 self.addTileSet(dialog.get_filename()) 127 dialog.destroy()
128
129 - def closeAll(self):
130 """Closes all the palettes """ 131 # Except for page 0, that has the open button on it. 132 for i in range(self.tileNotebook.get_n_pages() - 1): 133 self.tileNotebook.remove_page(i) 134 self.__palettes = []
135
136 - def addTileSet(self, fileName = None):
137 """ Opens the file specified in fileName in a TilePalette """ 138 # Add the label and close buttons to the tab 139 tabCloseButton = gtk.Button() 140 tabCloseButton.set_image(gtk.image_new_from_stock(gtk.STOCK_CLOSE, 141 gtk.ICON_SIZE_MENU)) 142 tabCloseButton.set_relief(gtk.RELIEF_NONE) 143 tabLabel = gtk.Label(os.path.basename(fileName)[0:6] + "..." ) 144 tabBox = gtk.HBox() 145 tabBox.pack_start(tabLabel) 146 tabBox.pack_end(tabCloseButton) 147 tabBox.show_all() 148 # Create the tile palette and add it to a scrolled window 149 palette = TilePalette(self.getController(), fileName) 150 self.__palettes.append(palette) 151 scrolledWindow = gtk.ScrolledWindow(None, None) 152 scrolledWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) 153 scrolledWindow.add_with_viewport(palette.getWidget()) 154 self.tileNotebook.prepend_page(scrolledWindow, tabBox) 155 tabCloseButton.connect("clicked", self.closeTilePalette, 156 scrolledWindow) 157 self.tileNotebook.show_all() 158 # Show the newly-opened file 159 self.tileNotebook.set_current_page(0)
160 161
162 -class TilePalette(tilegrid.TileGrid, mapcontroller.MapListener):
163 """ 164 Class for allowing images to be used as tile sets. Handles selection 165 and other related tasks 166 """ 167
168 - def __init__(self, controller, fileName):
169 """ 170 fileName = the name of the file to load 171 selection = the selection to set 172 tileSizet = the dimensions (in pixels) of the tiles 173 """ 174 mapcontroller.MapListener.__init__(self, controller) 175 self.tileImage, self.index = controller.openTileSet(fileName) 176 tilegrid.TileGrid.__init__(self, self.tileImage.get_width(), 177 self.tileImage.get_height(), controller.mapTileSize()) 178 self.toggleGrid() 179 180 self.addTool(editortools.TileSelectTool(controller, self.index), 0) 181 182 self.fileName = fileName 183 self.showGrid = True 184 self.queue_draw() 185 self.redrawBuffer()
186
187 - def specialRedraw(self, context):
188 """ See TileGrid.specialRedraw """ 189 context.set_source_surface(self.tileImage) 190 context.paint()
191