def draw(self): """W.draw () -> None Draws the Window. """ Bin.draw(self) cls = self.__class__ style = base.GlobalStyle st = self.style or style.get_style(cls) dropshadow = style.get_style_entry(cls, st, "shadow") rect = self.image.get_rect() # Create the caption. surface_caption = style.engine.draw_caption(rect.width - dropshadow, self.title, self.state, cls, st) self._captionrect = rect self._captionrect = surface_caption.get_rect() self._captionrect.topleft = self.topleft # Blit the caption. self.image.blit(surface_caption, (0, 0)) # Position and blit the child. if self.child: posx, posy = self.dispose_widget() self.child.topleft = posx, posy self.image.blit(self.child.image, (posx, posy))
def __init__(self, capacity): self.fitness = 0 self.rank = 0 self.bins = [] temp = Bin() temp.capacity = capacity self.bins.append(temp)
def __init__ (self, width, height): Bin.__init__ (self) self._scrolling = SCROLL_AUTO # Scrollbars. self._vscroll = VScrollBar (height, height) self._vscroll.connect_signal (SIG_VALCHANGED, self._scroll_child) self._vscroll.parent = self self._hscroll = HScrollBar (width, width) self._hscroll.connect_signal (SIG_VALCHANGED, self._scroll_child) self._hscroll.parent = self self._vscroll_visible = False self._hscroll_visible = False # Scrolling will be handled directly by the ScrolledWindow. self._hscroll.set_focus = lambda self: False self._vscroll.set_focus = lambda self: False self.controls.append (self._vscroll) self.controls.append (self._hscroll) self._signals[SIG_KEYDOWN] = None # Dummy self._signals[SIG_MOUSEDOWN] = [] # Respect the size. if width < self._hscroll.minsize[0]: width = self._hscroll.minsize[0] if height < self._vscroll.minsize[1]: height = self._vscroll.minsize[1] self.minsize = width, height
def destroy(self): """D.destroy () -> None Destroys the Window and removes it from its event system. """ self.__stopevents = True Bin.destroy(self)
def worst_pack_bins(self, data_array, capacity): capacity = int(capacity) for item in data_array: item = int(item) worstfit = capacity worst_bin = -1 i = 0 # Try to fit item into a bin while i < len(self.bins): if ((capacity - (self.bins[i].sum + item)) > worstfit) and (self.bins[i].sum + item <= capacity): worstfit = capacity - (self.bins[i].sum + item) worst_bin = i i += 1 if worst_bin != -1: self.bins[worst_bin].sum += item self.bins[worst_bin].items.append(item) else: b = Bin() b.capacity = capacity b.sum += item b.items.append(item) self.bins.append(b)
def draw (self): """W.draw () -> None Draws the ScrolledWindow surface. Creates the visible surface of the ScrolledWindow. """ Bin.draw (self) blit = self.image.blit # Update the srollbars. self.hscrollbar.lock () self.vscrollbar.lock () hscroll, vscroll = self._update_scrollbars () self.hscrollbar.unlock () self.vscrollbar.unlock () if hscroll: blit (self.hscrollbar.image, self.hscrollbar.rect) if vscroll: blit (self.vscrollbar.image, self.vscrollbar.rect) if self.child: blit (self.child.image, self.child.rect)
def loading(goods, cap): global goods_ goods_ = goods bins = [] j = 0 global max_weight_sofar while len(goods_): backtracking(1, len(goods_), capacity=cap) bin_ = Bin(cap) bins.append(bin_) #print(perfect, id(perfect)) for ele in perfect: bin_.add(Item(ele)) goods.remove(ele) path.clear() perfect.clear() num_item = len(goods) j += 1 max_weight_sofar = 0 for i in range(len(bins)): print(i + 1, ":", bins[i]) return bins
def __init__ (self, width, height): Bin.__init__ (self) self._scrolling = SCROLL_AUTO # ScrollBars. self._vscroll = VScrollBar (height, height) self._vscroll.connect_signal (SIG_VALCHANGE, self.set_dirty, True) self._vscroll.parent = self self._hscroll = HScrollBar (width, width) self._hscroll.connect_signal (SIG_VALCHANGE, self.set_dirty, True) self._hscroll.parent = self self._hscroll_visible = False self._vscroll_visible = False # Remove the keyboard events from the scrollbars. We use our own. del self._vscroll._signals[SIG_KEYDOWN] del self._hscroll._signals[SIG_KEYDOWN] self.controls.append (self._vscroll) self.controls.append (self._hscroll) # Respect the scrollbar sizes. if width < self._hscroll.size[0]: width = self._hscroll.size[0] if height < self._vscroll.size[1]: height = self._vscroll.size[1] self.size = (width, height) self._signals[SIG_KEYDOWN] = None # Dummy self._signals[SIG_MOUSEDOWN] = []
def draw (self): """W.draw () -> None Draws the Window. """ Bin.draw (self) cls = self.__class__ style = base.GlobalStyle st = self.style or style.get_style (cls) dropshadow = style.get_style_entry (cls, st, "shadow") rect = self.image.get_rect () # Create the caption. surface_caption = style.engine.draw_caption (rect.width - dropshadow, self.title, self.state, cls, st) self._captionrect = rect self._captionrect = surface_caption.get_rect () self._captionrect.topleft = self.topleft # Blit the caption. self.image.blit (surface_caption, (0, 0)) # Position and blit the child. if self.child: posx, posy = self.dispose_widget () self.child.topleft = posx, posy self.image.blit (self.child.image, (posx, posy))
def destroy (self): """D.destroy () -> None Destroys the Window and removes it from its event system. """ self.__stopevents = True Bin.destroy (self)
def notify (self, event): """B.notify (...) -> None Notifies the Button about an event. """ if not self.eventarea or not self.sensitive: # The button does not seem to be completely realized for now or # is not sensitive. return elif event.signal == SIG_MOUSEDOWN: if self.eventarea.collidepoint (event.data.pos): if base.debug: print "Button.MOUSEDOWN" self.focus = True # The button only acts upon left clicks. if event.data.button == 1: self.state = STATE_ACTIVE self.__click = True self.run_signal_handlers (SIG_MOUSEDOWN) elif event.signal == SIG_MOUSEUP: if self.eventarea.collidepoint (event.data.pos): if base.debug: print "Button.MOUSEUP" self.run_signal_handlers (SIG_MOUSEUP) if event.data.button == 1: if self.state == STATE_ACTIVE: self.state = STATE_ENTERED else: self.state = STATE_NORMAL # Check for a previous left click. if self.__click: self.__click = False if base.debug: print "Button.CLICKED" self.run_signal_handlers (SIG_CLICKED) elif (event.data.button == 1) and (self.state == STATE_ACTIVE): # Reset the 'clicked' state for the button, if the mouse # button 1 is released at another location. self.__click = False self.state = STATE_NORMAL elif event.signal == SIG_MOUSEMOVE: if self.eventarea.collidepoint (event.data.pos): if base.debug: print "Button.MOUSEMOVE (inner)" if self.state == STATE_NORMAL: self.state = STATE_ENTERED self.run_signal_handlers (SIG_MOUSEMOVE) elif self.state == STATE_ENTERED: if base.debug: print "Button.MOUSEMOVE (outer)" self.state = STATE_NORMAL elif (event.signal == SIG_KEYDOWN) and self.focus: if event.data.key in (pygame.locals.K_SPACE, pygame.locals.K_KP_ENTER, pygame.locals.K_RETURN): # Activate the focused button, if the user presses # space, return or enter. self.activate () Bin.notify (self, event)
def test_init(): wheel_1 = Wheel() outcome_1 = Outcome("Split-Bet", 17) outcome_2 = Outcome("Split-Bet", 1) outcome_3 = Outcome("Any Craps", 8) bin_1 = Bin([Outcome("0", 35), outcome_2, outcome_3]) bin_2 = Bin([Outcome("Straight-Bet", 12)]) assert outcome_1 == outcome_2 assert outcome_2 in bin_1 and outcome_3 in bin_1
def __init__ (self, widget): Bin.__init__ (self) self._hadjustment = 0 self._vadjustment = 0 if widget: self.minsize = widget.size else: self.minsize = 10, 10 self.child = widget
def __init__(self, widget): Bin.__init__(self) self._hadjustment = 0 self._vadjustment = 0 if widget: self.minsize = widget.size else: self.minsize = 10, 10 self.child = widget
def test_add_bins_to_wheel(): wheel_1 = Wheel() outcome_1 = Outcome("Split-Bet", 17) outcome_2 = Outcome("Split-Bet", 1) outcome_3 = Outcome("Any Craps", 8) bin_1 = Bin([Outcome("0", 35), outcome_2, outcome_3]) bin_2 = Bin([Outcome("Straight-Bet", 12)]) wheel_1.addOutcomes(1, bin_1) wheel_1.addOutcomes(2, bin_2) assert bin_1 in wheel_1.bins[0]
def set_state (self, state): """B.set_state (...) -> None Sets the state of the Button. Sets the state of the Button and causes its child to set its state to the same value. """ Bin.set_state (self, state) if self.child: self.child.state = self.state
def __init__(self): Bin.__init__(self) # Internal click detector. self.__click = False # Signals, the button listens to. self._signals[SIG_MOUSEDOWN] = [] self._signals[SIG_MOUSEUP] = [] self._signals[SIG_MOUSEMOVE] = [] self._signals[SIG_KEYDOWN] = None # Dummy for keyboard activation. self._signals[SIG_CLICKED] = []
def __init__ (self): Bin.__init__ (self) # Internal click detector. self.__click = False # Signals, the button listens to. self._signals[SIG_MOUSEDOWN] = [] self._signals[SIG_MOUSEUP] = [] self._signals[SIG_MOUSEMOVE] = [] self._signals[SIG_KEYDOWN] = None # Dummy for keyboard activation. self._signals[SIG_CLICKED] = []
def packItems(self): for item in self.S: for bin in self.BinList: if item.getWeight() <= bin.getCapacity(): bin.pack(item) item.setTrue() break if item.getMark() == False: newbin = Bin() self.BinList.append(newbin) newbin.pack(item) item.setTrue()
def packItems(self): binl = self.getBinList() for item in self.S: bestfit = binl[0] for bin in binl: if item.getWeight() <= bin.getCapacity() and ( bestfit.getCapacity() > bin.getCapacity() or bestfit.getCapacity() < item.getWeight()): bestfit = bin if bestfit.getCapacity() < item.getWeight(): bestfit = Bin() binl.append(bestfit) bestfit.pack(item)
def packItems(self): binl = self.getBinList() for item in self.S: worstfit = binl[0] for bin in binl: if item.getWeight() <= bin.getCapacity() and ( worstfit.getCapacity() < bin.getCapacity() or worstfit.getCapacity() < item.getWeight()): worstfit = bin if worstfit.getCapacity() < item.getWeight(): worstfit = Bin() binl.append(worstfit) worstfit.pack(item) item.setTrue()
def set_state (self, state): """S.set_state (...) -> None Sets the state of the ScrolledWindow. Sets the state of the ScrolledWindow. The state of the ScrolledWindow is mainly used for the visible or non-visible appearance of the ScrolledWindow, so that the user can determine the state of the ScrolledWindow easier. Usually this method should not be invoked by user code. """ Bin.set_state (self, state) if state in (STATE_NORMAL, STATE_INSENSITIVE): self.vscrollbar.set_state (state) self.hscrollbar.set_state (state)
def rect_to_client (self, rect=None): """V.rect_to_client () -> pygame.Rect Returns the absolute coordinates a rect is located at. If a rect argument is passed, its size and position are modified to match the criteria of the scrolling adjustments of the ViewPort. Besides that it exactly behaves like the original rect_to_client() method. """ if rect: border = base.GlobalStyle.get_border_size \ (self.__class__, self.style, StyleInformation.get ("VIEWPORT_BORDER")) rect.x = self.x + rect.x rect.y = self.y + rect.y if rect.right > self.right - border: rect.width = self.right - border - rect.left if rect.bottom > self.bottom - border: rect.height = self.bottom - border - rect.top if (self.parent != None) and isinstance (self.parent, BaseWidget): return self.parent.rect_to_client (rect) return rect return Bin.rect_to_client (self)
def set_focus (self, focus=True): """W.set_focus (...) -> None Sets the input and action focus of the window. Sets the input and action focus of the window and returns True upon success or False, if the focus could not be set. """ self.lock () if focus: self.state = STATE_ACTIVE elif (not self._keepactive) and (self.state == STATE_ACTIVE): self.state = STATE_NORMAL Bin.set_focus (self, focus) self.unlock () return True
def set_focus(self, focus=True): """W.set_focus (...) -> None Sets the input and action focus of the window. Sets the input and action focus of the window and returns True upon success or False, if the focus could not be set. """ self.lock() if focus: self.state = STATE_ACTIVE elif (not self._keepactive) and (self.state == STATE_ACTIVE): self.state = STATE_NORMAL Bin.set_focus(self, focus) self.unlock() return True
def loading(): bins = [] j = 0 while len(goods) != 0: eles = backtracking(1) bin_ = Bin() bins.append(bin_) for ele in eles: bin_.add(Item(ele)) goods.remove(ele) path.clear() num_item = len(goods) j += 1 for i in range(len(bins)): print(i+1, ":", bins[i])
def rect_to_client(self, rect=None): """V.rect_to_client () -> pygame.Rect Returns the absolute coordinates a rect is located at. If a rect argument is passed, its size and position are modified to match the criteria of the scrolling adjustments of the ViewPort. Besides that it exactly behaves like the original rect_to_client() method. """ if rect: border = base.GlobalStyle.get_border_size \ (self.__class__, self.style, StyleInformation.get ("VIEWPORT_BORDER")) rect.x = self.x + rect.x rect.y = self.y + rect.y if rect.right > self.right - border: rect.width = self.right - border - rect.left if rect.bottom > self.bottom - border: rect.height = self.bottom - border - rect.top if (self.parent != None) and isinstance(self.parent, BaseWidget): return self.parent.rect_to_client(rect) return rect return Bin.rect_to_client(self)
def draw(self): """V.draw () -> None Draws the ViewPort surface and places its child on it. """ border = base.GlobalStyle.get_border_size \ (self.__class__, self.style, StyleInformation.get ("VIEWPORT_BORDER")) Bin.draw(self) if self.child: self.child.topleft = border + self.hadjustment, \ border + self.vadjustment self.image.blit( self.child.image, (border, border), (abs(self.hadjustment), abs(self.vadjustment), self.width - 2 * border, self.height - 2 * border))
def set_child (self, child=None): """B.set_child (...) -> None Sets the child to display in the ScrolledList. Creates a parent-child relationship from the ScrolledList to a widget. If the widget does not support native scrolling, it will be packed into a ViewPort. """ self.lock () if child and not isinstance (child, ViewPort): self.vscrollbar.value = 0 self.hscrollbar.value = 0 child = ViewPort (child) child.minsize = self.minsize Bin.set_child (self, child) self.unlock ()
def draw (self): """V.draw () -> None Draws the ViewPort surface and places its child on it. """ border = base.GlobalStyle.get_border_size \ (self.__class__, self.style, StyleInformation.get ("VIEWPORT_BORDER")) Bin.draw (self) if self.child: self.child.topleft = border + self.hadjustment, \ border + self.vadjustment self.image.blit (self.child.image, (border, border), (abs (self.hadjustment), abs (self.vadjustment), self.width - 2 * border, self.height - 2 * border))
def grabBin(self): """ Creates a new Bin object that will store Items taken from Shelves Returns: new_bin: A Bin object containing nothing """ new_bin = Bin() return new_bin
def notify (self, event): """W.notify (event) -> None Notifies the window about an event. """ if not self.sensitive or not self.eventarea: return if event.signal == SIG_MOUSEDOWN: if self.rect.collidepoint (event.data.pos): if base.debug: print "Window.MOUSEDOWN" self.focus = True self.run_signal_handlers (SIG_MOUSEDOWN) if self._caption_rect.collidepoint (event.data.pos): if event.data.button == 1: # Initiate window movement. self.state = STATE_ACTIVE self.__pressed = True self.__old_pos = event.data.pos elif event.data.button == 2: # Minimize/maximize window. self.__minimized = not self.__minimized self.dirty = True elif event.signal == SIG_MOUSEUP: if self.eventarea.collidepoint (event.data.pos): if base.debug: print "Window.MOUSEUP" self.run_signal_handlers (SIG_MOUSEUP) if event.data.button == 1: if self.__pressed: self.__pressed = False self.state = STATE_NORMAL elif event.signal == SIG_MOUSEMOVE: if self.__pressed: # The window is moved. self._move_to_position (event.data.pos) self.__old_pos = event.data.pos else: if self.eventarea.collidepoint (event.data.pos): if base.debug: print "Window.MOUSEMOVE (inner)" self.run_signal_handlers (SIG_MOUSEMOVE) Bin.notify (self, event)
def update (self, **kwargs): """V.update (...) -> None Updates the ViewPort. Updates the ViewPort and causes its parent to update itself on demand. """ if not self.dirty: border = base.GlobalStyle.get_border_size \ (self.__class__, self.style, StyleInformation.get ("VIEWPORT_BORDER")) resize = kwargs.get ("resize", False) children = kwargs.get ("children", {}) blit = self.image.blit items = children.items () # Clean up the dirty areas on the widget. vals = [] for child, rect in items: blit (self._bg, rect, rect) # r will be the area for the blit. r = Rect (abs (self.hadjustment), abs (self.vadjustment), self.width - 2 * border, self.height - 2 * border) blit (child.image, (border, border), r) vals.append (r) # If a parent's available, reassign the child rects, so that # they point to the absolute position on the widget and build # one matching them all for an update. if self.parent: rect = Rect (self._oldrect) if len (vals) != 0: for r in vals: r.x += self.x r.y += self.y rect.unionall (vals[1:]) self.parent.update (children={ self : rect }, resize=resize) self._lock = max (self._lock - 1, 0) else: Bin.update (self, **kwargs)
def update(self, **kwargs): """V.update (...) -> None Updates the ViewPort. Updates the ViewPort and causes its parent to update itself on demand. """ if not self.dirty: border = base.GlobalStyle.get_border_size \ (self.__class__, self.style, StyleInformation.get ("VIEWPORT_BORDER")) resize = kwargs.get("resize", False) children = kwargs.get("children", {}) blit = self.image.blit items = children.items() # Clean up the dirty areas on the widget. vals = [] for child, rect in items: blit(self._bg, rect, rect) # r will be the area for the blit. r = Rect(abs(self.hadjustment), abs(self.vadjustment), self.width - 2 * border, self.height - 2 * border) blit(child.image, (border, border), r) vals.append(r) # If a parent's available, reassign the child rects, so that # they point to the absolute position on the widget and build # one matching them all for an update. if self.parent: rect = Rect(self._oldrect) if len(vals) != 0: for r in vals: r.x += self.x r.y += self.y rect.unionall(vals[1:]) self.parent.update(children={self: rect}, resize=resize) self._lock = max(self._lock - 1, 0) else: Bin.update(self, **kwargs)
def __init__ (self, title=None): Bin.__init__ (self) self._title = None self.set_title (title) self._align = ALIGN_NONE # Rectangle area for mouse click & movement on the window # caption. self._caption_rect = None # State variables for button pressing and mouse movements. self.__pressed = False self.__old_pos = None self.__minimized = False self._signals[SIG_MOUSEDOWN] = [] self._signals[SIG_MOUSEUP] = [] self._signals[SIG_MOUSEMOVE] = []
def packItems(self): binl = self.getBinList() bin = binl[0] for item in self.S: if item.getWeight() <= bin.getCapacity(): bin.pack(item) else: newBin = Bin() binl.append(newBin) bin = newBin bin.pack(item)
def addBins(self): for i in range(0, self.binsAmount): rightPosition = False while not rightPosition: x = randint(0, self.gridWidth - 1) y = randint(0, self.gridHeight - 1) if self.checkIfPositionIsEmpty([x, y]): rightPosition = True element = Bin(x, y) self.positionsToVisit.append([x, y]) self.mapElements.append(element)
def set_child (self, child=None): """B.set_child (...) -> None Sets the Label to display on the Button. Creates a parent-child relationship from the Button to a Label and causes the Label to set its mnemonic widget to the Button. Raises a TypeError, if the passed argument does not inherit from the Label class. """ if child and not isinstance (child, Label): raise TypeError ("child must inherit from Label") Bin.set_child (self, child) if child: child.set_widget (self) if not child.style: child.style = self.style or \ base.GlobalStyle.get_style (self.__class__) self.dirty = True
def draw (self): """B.draw () -> None Draws the Alignment surface and places its child on it. """ Bin.draw (self) rect = self.image.get_rect () if self.child: self.child.center = rect.center if self.align & ALIGN_TOP == ALIGN_TOP: self.child.top = rect.top + self.padding elif self.align & ALIGN_BOTTOM == ALIGN_BOTTOM: self.child.bottom = rect.bottom - self.padding if self.align & ALIGN_LEFT == ALIGN_LEFT: self.child.left = rect.left + self.padding elif self.align & ALIGN_RIGHT == ALIGN_RIGHT: self.child.right = rect.right - self.padding self.image.blit (self.child.image, self.child.rect)
def run(self): print("summarizer started") dayBin = {i: Bin() for i in range(366)} locationBin = {} self.bins.append(dayBin) self.bins.append(locationBin) while True: while self.queueList.qsize() > 0: record = self.queueList.get() featureList = [ 'AIR_TEMPERATURE', 'PRECIPITATION', 'SOLAR_RADIATION', 'SURFACE_TEMPERATURE', 'RELATIVE_HUMIDITY' ] recordList = [record[i] for i in featureList] fmt = '%Y%m%d' s = str(record['UTC_DATE']) if s is '20180229': continue dt = datetime.datetime.strptime(s, fmt) tt = dt.timetuple() nthDay = tt.tm_yday - 1 dayBin[nthDay].update(recordList) lat = record['LATITUDE'] long = record['LONGITUDE'] geohash = pgh.encode(lat, long) if geohash in self.geoHashList: locationBin[geohash].update(recordList) else: newBin = Bin() locationBin[geohash] = newBin locationBin[geohash].update(recordList) self.geoHashList.add(geohash) self.correlation_matrix.update(record) time.sleep(1)
def __init__(self, title=None): Bin.__init__(self) self.__stopevents = False self._title = None self._align = ALIGN_NONE # Rectangle area for mouse click & movement on the window # caption. self._captionrect = None # State variables for button pressing and mouse movements. self.__pressed = False self.__oldpos = None self._keepactive = False self._signals[SIG_MOUSEDOWN] = [] self._signals[SIG_MOUSEUP] = [] self._signals[SIG_MOUSEMOVE] = [] self.minsize = (100, 20) self.set_title(title)
def loading_(goods, cap): bins = [] j = 0 global max_weight_sofar, path while len(goods): brand_and_branch(goods, cap) bin_ = Bin(cap) bins.append(bin_) #print(perfect, id(perfect)) for ele in perfect: bin_.add(Item(ele)) goods.remove(ele) perfect.clear() num_item = len(goods) j += 1 for i in range(len(bins)): print(i+1, ":", bins[i]) return bins
class Wheel: bins = tuple(Bin() for i in range(38)) binArr = [] rng = random def __init__(self): pass def addOutcomes(self, number, outcome): self.binArr.append([number, outcome]) self.bins = self.binArr def next(self): return (random.choice(self.bins)) def get(self, bin): return self.bins[bin]
def __init__(self, gameDisplay, trashInv, door1 =None, door2 =None): #invoke the super classes' constructor super().__init__("Garbage Room", gameDisplay, door1, door2) self.roomImage = pygame.image.load('Images/garbage_room.jpg') self.inventory = pygame.Rect(0, 0, 800, 80) self.sceneItems = trashInv #The trash bins self.compostBin = Bin('compost', 200, 350, self.gameDisplay) self.landfillBin = Bin('landfill', 400, 350, self.gameDisplay) self.recycleBin = Bin('recycle', 600, 350, self.gameDisplay) #Used to keep track of which trash item is currently allowed for dragging self.trashIndex = 0 self.sceneItems[self.trashIndex].setMove(True)
def nf_binpick(items, cap): num = len(items) bins = [] for i in range(num): bins.append(Bin(cap)) i += 1 form = "第%d个物品(重量为:%d)------->第%d个箱子(剩余容量:%f)" for index in range(num): for ji in range(num): if (items[index] <= bins[ji].remaining): if bins[ji].add(Item(items[index])): print( form % (index + 1, items[index], ji + 1, bins[ji].remaining)) break for i in range(num): if bins[i].empty(): bins[i] = None for i in range(bins.count(None)): bins.remove(None) return bins
def notify (self, event): """S.notify (...) -> None Notifies the ScrolledWindow about an event. """ if not self.eventarea or not self.sensitive: return if event.signal == SIG_MOUSEDOWN: if self.eventarea.collidepoint (event.data.pos): if base.debug: print "ScrolledWindow.MOUSEDOWN" self.focus = True self.run_signal_handlers (SIG_MOUSEDOWN) # Mouse wheel. if self._hscroll_visible and \ (self.hscrollbar.eventarea.collidepoint (event.data.pos)): # Do not scroll vertical, if the mouse cursor is # over the horizontal scrollbar. pass elif self._vscroll_visible: if event.data.button == 4: self.vscrollbar.decrease () elif event.data.button == 5: self.vscrollbar.increase () elif (event.signal == SIG_KEYDOWN) and self.focus: if self._hscroll_visible: # Horizontal scrollbar key movement if event.data.key == pygame.locals.K_RIGHT: self.hscrollbar.increase () elif event.data.key == pygame.locals.K_LEFT: self.hscrollbar.decrease () elif event.data.key == pygame.locals.K_END: self.hscrollbar.value = self.hscrollbar.maximum elif event.data.key == pygame.locals.K_HOME: self.hscrollbar.value = self.hscrollbar.minimum if self._vscroll_visible: # Vertical scrollbar key movement if event.data.key == pygame.locals.K_DOWN: self.vscrollbar.increase () elif event.data.key == pygame.locals.K_UP: self.vscrollbar.decrease () elif event.data.key == pygame.locals.K_PAGEUP: val = self.vscrollbar.value - 10 * self.vscrollbar.step if val > self.vscrollbar.minimum: self.vscrollbar.value = val else: self.vscrollbar.value = self.vscrollbar.minimum elif event.data.key == pygame.locals.K_PAGEDOWN: val = self.vscrollbar.value + 10 * self.vscrollbar.step if val < self.vscrollbar.maximum: self.vscrollbar.value = val else: self.vscrollbar.value = self.vscrollbar.maximum elif event.data.key == pygame.locals.K_END: self.vscrollbar.value = self.vscrollbar.maximum elif event.data.key == pygame.locals.K_HOME: self.vscrollbar.value = self.vscrollbar.minimum Bin.notify (self, event)
def draw (self): """L.draw () -> None Draws the ListViewPort surface and places its items on it. """ Bin.draw (self) style = base.GlobalStyle cls = self.__class__ color = StyleInformation.get ("SELECTION_COLOR") active = StyleInformation.get ("ACTIVE_BORDER") border_active = style.get_border_size (cls, self.style, active) border = style.get_border_size \ (cls, self.style, StyleInformation.get ("VIEWPORT_BORDER")) active_space = StyleInformation.get ("ACTIVE_BORDER_SPACE") st = self.style state = self.state realwidth = self.real_width posy = 0 draw_rect = Draw.draw_rect sdraw_rect = style.engine.draw_rect sdraw_border = style.engine.draw_border spacing = self.scrolledlist.spacing width = self.width - 2 * border height = self.height - 2 * border selheight = 0 surface = None cursor_found = False cursor = self.scrolledlist.cursor items = self.scrolledlist.items focus = self.scrolledlist.focus images = self.images lower = abs (self.vadjustment) - spacing - 2 * border_active upper = abs (self.vadjustment) + height # Overall surface surface_all = sdraw_rect (max (realwidth, width), self.real_height, state, cls, st) blit = surface_all.blit for item in items: image, rect = images[item] # Draw only those which are visible. if (posy + rect.height < lower): posy += rect.height + 2 * border_active + spacing continue elif posy > upper: break selheight = rect.height + 2 * border_active if item.selected: # Highlight the selection. surface = draw_rect (max (width, realwidth), selheight, color) # Show input focus. if focus and (item == cursor): sdraw_border (surface, state, cls, st, active, space=active_space) cursor_found = True surface.blit (image, (border_active, border_active)) blit (surface, (0, posy)) elif focus and not cursor_found and (item == cursor): # Input focus. surface = sdraw_rect (max (width, realwidth), selheight, state, cls, st) sdraw_border (surface, state, cls, st, active, space=active_space) surface.blit (image, (border_active, border_active)) cursor_found = True blit (surface, (0, posy)) else: # Regular image, move by the active border, so that # all items have the correct offset. blit (image, (border_active, posy + border_active)) posy += selheight + spacing self.image.blit (surface_all, (border, border), (abs (self.hadjustment), abs (self.vadjustment), width, height))
def __init__ (self, width, height): Bin.__init__ (self) self._align = ALIGN_NONE self.minsize = width, height
def notify (self, event): """W.notify (event) -> None Notifies the window about an event. """ if not self.sensitive: return # Recursively notify all attached children. if self.child: self._notify_children (self.child, event) if self.__stopevents: return for control in self.controls: if event.handled: return self._notify_children (control, event) if event.signal in SIGNALS_MOUSE: eventarea = self.rect_to_client () if event.signal == SIG_MOUSEDOWN: if eventarea.collidepoint (event.data.pos): if not event.handled: self.focus = True self.run_signal_handlers (SIG_MOUSEDOWN, event.data) if self._captionrect.collidepoint (event.data.pos): if event.data.button == 1: # Initiate window movement. self.state = STATE_ACTIVE self.__pressed = True self.__oldpos = event.data.pos event.handled = True else: self.state = STATE_NORMAL elif event.signal == SIG_MOUSEUP: if eventarea.collidepoint (event.data.pos): self.run_signal_handlers (SIG_MOUSEUP, event.data) if event.data.button == 1: if self.__pressed: self.__pressed = False event.handled = True elif event.signal == SIG_MOUSEMOVE: if self.__pressed: # The window is moved. self._move_to_position (event.data.pos) event.handled = True elif eventarea.collidepoint (event.data.pos): self.run_signal_handlers (SIG_MOUSEMOVE, event.data) event.handled = True elif event.signal == SIG_FOCUSED: # Keep the active state of the window, if it contains the child if self._contains (self, event.data): if self.state != STATE_ACTIVE: self.state = STATE_ACTIVE self._keepactive = True else: self._keepactive = False Bin.notify (self, event)
def notify (self, event): """B.notify (...) -> None Notifies the ButtonBase about an event. """ if not self.sensitive: return if event.signal in SIGNALS_MOUSE: eventarea = self.rect_to_client () if event.signal == SIG_MOUSEDOWN: if eventarea.collidepoint (event.data.pos): self.focus = True # The button only acts upon left clicks. if event.data.button == 1: self.__click = True self.state = STATE_ACTIVE self.run_signal_handlers (SIG_MOUSEDOWN, event.data) event.handled = True elif event.signal == SIG_MOUSEUP: if eventarea.collidepoint (event.data.pos): self.run_signal_handlers (SIG_MOUSEUP, event.data) if event.data.button == 1: if self.state == STATE_ACTIVE: self.state = STATE_ENTERED else: self.state = STATE_NORMAL # Check for a previous left click. if self.__click: self.__click = False self.run_signal_handlers (SIG_CLICKED) event.handled = True elif event.data.button == 1: # Reset the 'clicked' state for the button, if the mouse # button 1 is released at another location. self.__click = False self.state = STATE_NORMAL elif event.signal == SIG_MOUSEMOVE: if eventarea.collidepoint (event.data.pos): if not self.__click: self.state = STATE_ENTERED else: self.state = STATE_ACTIVE self.run_signal_handlers (SIG_MOUSEMOVE, event.data) self.entered = True event.handled = True else: self.entered = False elif (event.signal == SIG_KEYDOWN) and self.focus: if event.data.key in (K_SPACE, K_KP_ENTER, K_RETURN): # Activate the focused button, if the user presses # space, return or enter. self.activate () event.handled = True Bin.notify (self, event)
def notify (self, event): """S.notify (...) -> None Notifies the ScrolledWindow about an event. """ if not self.sensitive: return if event.signal in SIGNALS_MOUSE: eventarea = self.rect_to_client () if event.signal == SIG_MOUSEDOWN: if eventarea.collidepoint (event.data.pos): self.focus = True self.run_signal_handlers (SIG_MOUSEDOWN, event.data) # Mouse wheel. for c in self.controls: c.notify (event) if not event.handled: if self._vscroll_visible: if event.data.button == 4: self.vscrollbar.decrease () elif event.data.button == 5: self.vscrollbar.increase () elif self._hscroll_visible: if event.data.button == 4: self.hscrollbar.decrease () elif event.data.button == 5: self.hscrollbar.increase () event.handled = True elif (event.signal == SIG_KEYDOWN) and self.focus: if self._hscroll_visible: # Horizontal scrollbar key movement if event.data.key == K_RIGHT: self.hscrollbar.increase () event.handled = True elif event.data.key == K_LEFT: self.hscrollbar.decrease () event.handled = True elif event.data.key == K_END: self.hscrollbar.value = self.hscrollbar.maximum event.handled = True elif event.data.key == K_HOME: self.hscrollbar.value = self.hscrollbar.minimum event.handled = True if self._vscroll_visible: # Vertical scrollbar key movement if event.data.key == K_DOWN: self.vscrollbar.increase () event.handled = True elif event.data.key == K_UP: self.vscrollbar.decrease () event.handled = True elif event.data.key == K_PAGEUP: val = self.vscrollbar.value - 10 * self.vscrollbar.step if val > self.vscrollbar.minimum: self.vscrollbar.value = val else: self.vscrollbar.value = self.vscrollbar.minimum event.handled = True elif event.data.key == K_PAGEDOWN: val = self.vscrollbar.value + 10 * self.vscrollbar.step if val < self.vscrollbar.maximum: self.vscrollbar.value = val else: self.vscrollbar.value = self.vscrollbar.maximum event.handled = True elif event.data.key == K_END: self.vscrollbar.value = self.vscrollbar.maximum event.handled = True elif event.data.key == K_HOME: self.vscrollbar.value = self.vscrollbar.minimum event.handled = True Bin.notify (self, event)