def ObjectSelect(self, id): """\ Called when an object is selected. """ # Hide the order panel when no object is selected if id is None: self.Master.Hide() return # Check the object exists object = self.application.cache.objects[self.oid] # Do the clean up first self.Orders.DeleteAllItems() self.Possible.Clear() if object.order_number == 0 and len(object.order_types) == 0: # No orders and no possible orders on this object! self.Master.Hide() else: self.Master.Show() self.Master.Layout() self.Master.Update() self.Orders.SetToolTipDefault(_("Current orders on %s.") % object.name) orders = self.application.cache.orders[self.oid] # Add all the orders to the list for listpos, node in enumerate(orders): self.InsertListItem(listpos, node) # Set which order types can be added to this object self.Possible.SetToolTipDefault(_("Order type to create")) for type in object.order_types: if not objects.OrderDescs().has_key(type): print "WARNING: Unknown order type with id %s" % type continue od = objects.OrderDescs()[type] self.Possible.Append(od._name, type) if hasattr(od, "doc"): desc = od.doc else: desc = od.__doc__ desc = desc.strip() self.Possible.SetToolTipItem(self.Possible.GetCount()-1, desc) # Set the order types to the first selection if len(object.order_types) > 0: self.Possible.Enable() self.Possible.SetSelection(0) else: self.Possible.Disable()
def CheckClipBoard(self): """\ Check if the items in the clipboard could be pasted on the currently selected object. """ if self.clipboard != None: for order in self.clipboard: if not objects.OrderDescs().has_key(order.subtype): return False slot = self.Possible.FindString(objects.OrderDescs()[order.subtype]._name) if slot == wx.NOT_FOUND: return False return True return False
def OnOrderNew(self, evt, after=True): """\ Called to add a new order. """ assert self.oid != None # Figure out what type of new order we are creating type = self.Possible.GetSelection() if type == wx.NOT_FOUND: return type = self.Possible.GetClientData(type) # Build the argument list orderdesc = objects.OrderDescs()[type] # sequence, id, slot, type, turns, resources args = [0, self.oid, -1, type, 0, []] for name, type in orderdesc.names: args += defaults[type] # Create the new order new = objects.Order(*args) new._dirty = True # Insert the new order (after the currently selected) if after: node = self.InsertAfterOrder(new) else: node = self.InsertBeforeOrder(new) self.SelectOrders([node])
def BuildMenu(self, menu): """\ Build a menu containing the order types which could be inserted. """ object = self.application.cache.objects[self.oid] for type in object.order_types: if not objects.OrderDescs().has_key(type): continue od = objects.OrderDescs()[type] if hasattr(od, "doc"): desc = od.doc else: desc = od.__doc__ desc = desc.strip() menu.Append(-1, od._name, desc)
def FromPanel(self, order): orderdesc = objects.OrderDescs()[order.subtype] args = [order.sequence, order.id, -1, order.subtype, 0, []] subpanels = copy.copy(self.ArgumentsChildren) for name, type in orderdesc.names: panel = subpanels.pop(0) args += panel.get_value() return apply(objects.Order, args)
def sendOrder(self, conn, id, type, moreargs): # sequence, id, slot, type, turns, resources args = [0, id, -1, type, 0, []] # get orderdesc so we can get default args for order type orderdesc = objects.OrderDescs()[type] for name, type in orderdesc.names: args += defaults[type] # Create the new order new = objects.Order(*args) new._dirty = True # Insert order after node = self.cache.orders[id].last assert not node is None # Do some sanity checking d = self.cache.orders[id] assert node in d evt = self.cache.apply("orders", "create after", id, node, new) apply(conn, evt, self.cache) self.cache.save()
def build(self): """Build json friendly structure""" return_data = {} for i in self.cache.objects: # Retrives object and orders for that object object = self.cache.objects[i] orders = self.cache.orders[i] # If we have orders for this object or the object can receive orders if object.order_number > 0 or len(object.order_types) > 0: # Build the initialize structure for this object and its orders return_data[i] = {'orders': {}, 'order_type': []} # Go through all orders currently on the object for listpos, node in enumerate(orders): order = node.CurrentOrder orderdesc = objects.OrderDescs()[order.subtype] if hasattr(orderdesc, 'doc'): desc = orderdesc.doc else: desc = orderdesc.__doc__ desc = desc.strip() args = self.get_args(orderdesc, order) return_data[i]['orders'][int(node.id)] = { 'order_id': int(node.id), 'name': safestr(order._name), 'description': safestr(desc), 'type': order.subtype, 'turns': order.turns, 'args': args } # Go through all possible orders this object can receive for type in object.order_types: # If type is not recognized, ignore it if not objects.OrderDescs().has_key(type): continue # Retrive order description orderdesc = objects.OrderDescs()[type] if hasattr(orderdesc, 'doc'): desc = orderdesc.doc else: desc = orderdesc.__doc__ desc = desc.strip() args = self.get_args(orderdesc) return_data[i]['order_type'].append({ 'name': safestr(orderdesc._name), 'description': safestr(desc), 'type': type, 'args': args }) return return_data
def BuildPanel(self): """\ Builds a panel for the entering of orders arguments. """ try: object = self.application.cache.objects[self.oid] nodes = self.nodes if object.order_number == 0 and len(object.order_types) == 0: node = _("No orders avaliable") elif len(nodes) > 1: node = _("Multiple orders selected.") elif len(nodes) < 1: node = None else: node = nodes[0] if node.LastState in ("removing", "removed"): node = _("Order queued for removal.") except KeyError: node = _("No object selected.") self.ColourOrderPanel() # Remove the previous panel and stuff if hasattr(self, "ArgumentsPanel"): self.ArgumentsPanel.Hide() self.DetailsSizer.Remove(self.ArgumentsPanel) self.ArgumentsPanel.Destroy() del self.ArgumentsPanel # Show the details panel DetailsPanelShow = True self.DetailsPanel.Hide() self.ArgumentLine.Hide() self.Message.SetLabel("") self.Message.Hide() if isinstance(node, ChangeNode): order = node.CurrentOrder assert not order is None # Create a new panel self.ArgumentsPanel = wx.Panel(self.DetailsPanel, -1) self.ArgumentsPanel.SetAutoLayout( True ) self.ArgumentsSizer = wx.FlexGridSizer( 0, 1, 0, 0) self.ArgumentsPanel.SetSizer(self.ArgumentsSizer) self.ArgumentsSizer.AddGrowableCol( 0 ) orderdesc = objects.OrderDescs()[order.subtype] # List for the argument subpanels self.ArgumentsChildren = [] for name, subtype in orderdesc.names: # Add there name.. name_text = wx.StaticText( self.ArgumentsPanel, -1, name.title().replace("_","") ) name_text.SetFont(wx.local.tinyFont) # Add the arguments bit namepos = wx.LEFT if subtype == constants.ARG_ABS_COORD: subpanel = PositionArgumentPanel(self.ArgumentsPanel) subpanel.application = self.application subpanel.set_value(list(getattr(order, name))) elif subtype == constants.ARG_LIST: subpanel = ListArgumentPanel(self.ArgumentsPanel) subpanel.set_value(list(getattr(order, name))) elif subtype == constants.ARG_STRING: subpanel = TextArgumentPanel(self.ArgumentsPanel) subpanel.set_value([getattr(order, name)]) elif subtype == constants.ARG_TIME: subpanel = TimeArgumentPanel(self.ArgumentsPanel) subpanel.set_value([getattr(order,name)]) elif subtype == constants.ARG_OBJECT: subpanel = ObjectArgumentPanel(self.ArgumentsPanel) subpanel.application(self.application) subpanel.set_value([getattr(order,name)]) else: return subpanel.SetToolTip(wx.ToolTip(getattr(orderdesc, name+'__doc__'))) subpanel.SetFont(wx.local.normalFont) self.ArgumentsChildren.append( subpanel ) if subpanel.namepos == wx.TOP: self.ArgumentsSizer.Add( name_text, 0, wx.ALIGN_CENTER|wx.RIGHT|wx.LEFT, border=4) self.ArgumentsSizer.Add( subpanel, 1, wx.GROW|wx.EXPAND|wx.ALIGN_CENTER) elif subpanel.namepos == wx.LEFT: ArgumentSubSizer = wx.BoxSizer(wx.HORIZONTAL) ArgumentSubSizer.Add( name_text, 0, wx.ALIGN_CENTER|wx.RIGHT, 4 ) ArgumentSubSizer.Add( subpanel, 1, wx.GROW|wx.EXPAND|wx.ALIGN_CENTER) self.ArgumentsSizer.Add(ArgumentSubSizer, 1, wx.GROW|wx.EXPAND|wx.ALIGN_CENTER) else: raise TypeError('WTF?') self.ArgumentsPanel.Layout() if len(orderdesc.names) == 0: name_text = wx.StaticText( self.ArgumentsPanel, -1, "No arguments" ) name_text.SetFont(wx.local.normalFont) self.ArgumentsSizer.Add( name_text, 0, wx.ALIGN_CENTER|wx.CENTER, 4 ) # self.DetailsPanel.SetClientSize(wx.Size(self.GetBestSize()[0], -1)) self.DetailsSizer.Add( self.ArgumentsPanel, flag=wx.GROW|wx.EXPAND|wx.ALIGN_CENTER|wx.ALL) # Show the Save/Revert/Delete buttons self.Message.Show() self.Save.Show() self.Revert.Show() self.Delete.Show() elif isinstance(node, (unicode, str)): self.Message.SetLabel(node) self.Message.Show() # Hide the Save/Revert buttons self.Save.Hide() self.Revert.Hide() # Delete button should still be valid self.Delete.Show() else: DetailsPanelShow = False self.ArgumentLine.Hide() # Hide the Save/Revert/Delete buttons self.Save.Hide() self.Revert.Hide() self.Delete.Hide() if DetailsPanelShow: self.DetailsPanel.Show() self.ArgumentLine.Show() self.Orders.SetSize((-1,0)) self.Master.Layout() self.DetailsPanel.Layout() self.Layout() self.Orders._doResize()