def __set_size(self, size): self.__size = size self.set_size_request(size, size) from gtk.gdk import Rectangle rectangle = Rectangle(0, 0, size, size) self.size_allocate(rectangle) self.queue_draw() return
def _redraw_canvas(self): """ Redraw the canvas to make it look as thought the hands are ticking. """ if self.window: dimensions = self.get_allocation() rect = Rectangle(width=dimensions.width, height=dimensions.height) self.window.invalidate_rect(rect, True) self.window.process_updates(True)
def _expose(self, *args): """ Method used to draw on to the canvas. """ self._context = self._draw_area.window.cairo_create() content_area = Rectangle(width=self.allocation.width, height=self.allocation.height) self._context.rectangle(content_area) self._context.clip() self._draw_clock()
def drawComponent(self, ctx): if self.layout == None: self.layout = ctx.create_layout() self.setAlignment() self.layout.set_font_description(self.font) if self.lyrics_need_update: self.updateLyrics() self.clipBounds = Rectangle(*ctx.clip_extents()) if self.anim_fraction == 1.0: self.anim_fraction = 0.0 self.textForAnimation = None self.actualLine += 1 if self.lyrics != None and self.actualLine < len(self.lyrics.entities): print self.anim_fraction center_y = 60 ctx.translate(0, center_y) if self.actualLine > 0: ctx.set_source_rgba(*self.color_normal) ctx.translate(0, -self.lyrics.entities[self.actualLine-1].height) #if self.anim_fraction > 0: ctx.save() ctx.translate(-200*self.anim_fraction, 0) self.drawScaledLyric(ctx, self.lyrics.entities[self.actualLine-1]) ctx.restore() ctx.translate(0, self.lyrics.entities[self.actualLine-1].height) ctx.set_source_rgba(*self.color_highlight) ctx.translate((self.anim_fraction)*200, 0) self.drawScaledLyric(ctx, self.lyrics.entities[self.actualLine]) ctx.translate(-self.anim_fraction*200, 0) if self.actualLine+1 < len(self.lyrics.entities): if self.anim_fraction > 0: ctx.save() ctx.translate((1-self.anim_fraction)*-200, 0) self.drawScaledLyric(ctx, self.lyrics.entities[self.actualLine+1]) ctx.restore() ctx.translate(0, max(self.lyrics.entities[self.actualLine].height, self.lyrics.entities[self.actualLine+1].height)) ctx.set_source_rgba(*self.color_normal) if self.anim_fraction > 0: ctx.translate(-200*self.anim_fraction, 0) self.drawScaledLyric(ctx, self.lyrics.entities[self.actualLine+1])
def get_geometry_rel(window, monitor_geom): # type: (wnck.Window, Rectangle) -> Rectangle """Get window position relative to the monitor rather than the desktop. @param monitor_geom: The rectangle returned by C{gdk.Screen.get_monitor_geometry} @type window: C{wnck.Window} @type monitor_geom: C{gtk.gdk.Rectangle} @rtype: C{gtk.gdk.Rectangle} """ win_geom = Rectangle(*window.get_geometry()) win_geom.x -= monitor_geom.x win_geom.y -= monitor_geom.y return win_geom
def reposition( self, win, # type: wnck.Window geom=None, # type: Optional[Rectangle] monitor=Rectangle(0, 0, 0, 0), # type: Rectangle keep_maximize=False, # type: bool gravity=wnck.WINDOW_GRAVITY_NORTHWEST, geometry_mask=wnck.WINDOW_CHANGE_X | wnck.WINDOW_CHANGE_Y | wnck.WINDOW_CHANGE_WIDTH | wnck.WINDOW_CHANGE_HEIGHT # type: wnck.WindowMoveResizeMask ): # pylint: disable=no-member,too-many-arguments # type: (...) -> None # TODO: Complete MyPy type signature # pylint:disable=line-too-long """ Position and size a window, decorations inclusive, according to the provided target window and monitor geometry rectangles. If no monitor rectangle is specified, position relative to the desktop as a whole. @param win: The C{wnck.Window} to operate on. @param geom: The new geometry for the window. Can be left unspecified if the intent is to move the window to another monitor without repositioning it. @param monitor: The frame relative to which C{geom} should be interpreted. The whole desktop if unspecified. @param keep_maximize: Whether to re-maximize a maximized window after un-maximizing it to move it. @param gravity: A constant specifying which point on the window is referred to by the X and Y coordinates in C{geom}. @param geometry_mask: A set of flags determining which aspects of the requested geometry should actually be applied to the window. (Allows the same geometry definition to easily be shared between operations like move and resize.) @type win: C{wnck.Window} @type geom: C{gtk.gdk.Rectangle} or C{None} @type monitor: C{gtk.gdk.Rectangle} @type keep_maximize: C{bool} @type gravity: U{WnckWindowGravity<https://developer.gnome.org/libwnck/stable/WnckWindow.html#WnckWindowGravity>} or U{GDK Gravity Constant<http://www.pygtk.org/docs/pygtk/gdk-constants.html#gdk-gravity-constants>} @type geometry_mask: U{WnckWindowMoveResizeMask<https://developer.gnome.org/libwnck/2.30/WnckWindow.html#WnckWindowMoveResizeMask>} @todo 1.0.0: Look for a way to accomplish this with a cleaner method signature. This is getting a little hairy. (API-breaking change) """ # NOQA # We need to ensure that ignored values are still present for # gravity calculations. old_geom = self.get_geometry_rel(win, self.get_monitor(win)[1]) if geom: for attr in ('x', 'y', 'width', 'height'): if not geometry_mask & getattr( wnck, 'WINDOW_CHANGE_%s' % attr.upper()): setattr(geom, attr, getattr(old_geom, attr)) else: geom = old_geom with persist_maximization(win, keep_maximize): # Apply gravity and resolve to absolute desktop coordinates. new_x, new_y = self.calc_win_gravity(geom, gravity) new_x += monitor.x new_y += monitor.y logging.debug(" Repositioning to (%d, %d, %d, %d)\n", new_x, new_y, geom.width, geom.height) # See the calc_win_gravity docstring for the rationale here win.set_geometry(wnck.WINDOW_GRAVITY_STATIC, geometry_mask, new_x, new_y, geom.width, geom.height)
def drawScaledLyric(self, ctx, lyric, scale=1, cache=False): backup_layout_width = self.layout.get_width() backup_font_size = self.font.get_size() backup_translationFont_size = self.translationFont.get_size() ctx.save() self.font.set_size(int(backup_font_size * scale * self.gscale)) self.translationFont.set_size( int(backup_translationFont_size * scale * self.gscale)) self.layout.set_font_description(self.font) self.layout.set_width( int(pango.SCALE * self.bounds.width * self.gscale * scale / self.text_scale)) j = 0 for length in reversed(lyric.lengths): if length == 0: j += 1 else: break i = len(lyric.lengths) - j stext = "" for text in lyric.text: stext += text start = 0 #for length in lyric.lengths: linesBounds = [] for l in range(0, i): length = lyric.lengths[l] if l < len(lyric.linesBounds): bounds = lyric.linesBounds[l] intersection = Rectangle(*[int(ceil(v)) for v in bounds]).intersect( self.clipBounds) if intersection.width == 0 or intersection.height == 0: #print "NETREBA" #print intersection #break pass else: #print "TREBA" pass ctx.save() ctx.scale(scale, scale) #ctx.rectangle(*bounds) #ctx.fill() ctx.restore() text = stext[start:start + length].strip() start += length # new start position if cache and False: self.drawText(ctx, text, scale) else: #""" self.layout.set_text(text) ctx.show_layout(self.layout) bounds = self.layout.get_pixel_extents()[1] #print bounds #ctx.rectangle(*bounds) #ctx.fill() #print "____" #print self.layout.get_extents() #print self.layout.get_pixel_extents() #print self.layout.get_pixel_size() linesBounds.append(self.layout.get_pixel_extents()) if l == 0: self.last_text_extents = self.layout.get_pixel_extents() #self.last_text_extents[1] height = self.layout.get_pixel_size()[1] ctx.translate(0, height) #""" """ stext = "" for text in lyric.text: stext += text start = 0 for length in lyric.lengths: text = stext[start : start+length].strip() start += length # new start position self.layout.set_text(text) ctx.show_layout(self.layout) height = self.layout.get_pixel_size()[1] ctx.translate(0, height) """ """ j = 0 for line in reversed(lyric.text): if len(line) == 0: j+=1 else: break i = len(lyric.text) - j #for text in lyric.text: #print "%s %d %d %d" % (lyric.text, len(lyric.text), j, i) for t in range(0, i): text = lyric.text[t] self.layout.set_text(text) ctx.show_layout(self.layout) height = self.layout.get_pixel_size()[1] ctx.translate(0, height) """ if self.lyrics.showTranslation and lyric.translation != None: self.layout.set_font_description(self.translationFont) ttext = "" for text in lyric.translation: ttext += text start = 0 for tl in lyric.tlengths: self.layout.set_text(ttext[start:start + tl]) start += tl #self.layout.set_text(lyric.translation) #self.layout.set_text("[ preklad ]") #break ctx.show_layout(self.layout) height = self.layout.get_pixel_size()[1] ctx.translate(0, height) self.layout.set_font_description(self.font) # empty lines at the end for t in range(0, j): self.layout.set_text("") ctx.show_layout(self.layout) height = self.layout.get_pixel_size()[1] ctx.translate(0, height) ctx.restore() self.font.set_size(backup_font_size) self.translationFont.set_size(backup_translationFont_size) #self.layout.set_font_description(self.font) self.layout.set_width(backup_layout_width)
def drawComponent(self, ctx): #print "redraw Lyrics Panel" #print self.anim_fraction #print self.actualLine t_start = gobject.get_current_time() ctx.save() matrix = ctx.get_matrix() self.text_matrix = cairo.Matrix(1, matrix[1], matrix[2], 1, matrix[4], matrix[5]) lastScale = self.gscale self.gscale = matrix[0] if self.gscale != lastScale: self.resizeBuffers() ctx.set_matrix(self.text_matrix) #ctx.scale(1.0/scale_x, 1.0/scale_y) if self.layout == None: self.layout = ctx.create_layout() self.setAlignment() self.layout.set_font_description(self.font) self.clipBounds = Rectangle( *[int(ceil(v)) for v in ctx.clip_extents()]) #print self.clipBounds #print 'draw: '+str(ctx.clip_extents()) #ctx.set_source_rgba(1, 0, 0, 1) #ctx.rectangle(0, 0, self.bounds.width, self.bounds.height) #ctx.fill() #ctx.set_operator(cairo.OPERATOR_SOURCE) ctx.set_operator(cairo.OPERATOR_OVER) if not self.text_scale or not self.font: # or not self.color_highlight or not self.color_normal: return ######################### ##### RENDER LYRICS ##### ######################### if self.lyrics_need_update: self.updateLyrics() """ if ([self.actualLine, self.animation.fraction] == self.lastState): #TODO add lyrics to state print "EQUAL" #return else: print "NOT EQUAL" self.lastState = [self.actualLine, self.animation.fraction] """ #options = ctx.get_font_options() #options.set_hint_metrics(cairo.HINT_METRICS_OFF) #options.set_hint_style(cairo.HINT_STYLE_FULL) #ctx.set_font_options(options) if self._buffer1 == None: self._buffer1 = PixmapBuffer(self.root.root.window, self.bounds.width * self.gscale * 0.9, 20) if self._buffer2 == None: self._buffer2 = PixmapBuffer(self.root.root.window, self.bounds.width * self.gscale, 20) if self.anim_fraction == 1.0: self.anim_fraction = 0.0 self.textForAnimation = None self.actualLine += 1 if self.render_lyrics and self.lyrics and self.actualLine < len( self.lyrics.entities): #fraction = math.sin(self.animation.fraction*1.57) fraction = math.sin( self.anim_fraction * 1.57) # non-linear interpolation of animation fraction #fraction = self.anim_fraction ############################### ##### Compute positioning ##### ############################### if ([self.actualLine, self.anim_fraction] != self.lastState): #TODO add lyrics to state actual_y = (self.bounds.height - self.lyrics.entities[ self.actualLine].trimmed_height * self.text_scale) / 2.0 if self.actualLine + 1 < len(self.lyrics.entities): next_y = (self.bounds.height - self.lyrics.entities[self.actualLine + 1]. trimmed_height * self.text_scale) / 2.0 # center y position, interpolation between actual and next texts y-center position (if their heights are diffrent) self.center_y = actual_y + (next_y - actual_y) * fraction else: self.center_y = actual_y self.center_y = int(self.center_y) # texts moving animation - actual text must translate about it's height by y-axle self.motion_y = -fraction * ( self.lyrics.entities[self.actualLine].height) # decreasing scale text factor in animation self.dec_scale = self.text_scale - fraction * ( self.text_scale - 1.0) # increasing scale text factor in animation self.inc_scale = 1.0 + fraction * (self.text_scale - 1.0) # must be integer due to nice text rendering on raster if self.textAlign == ALIGN_CENTER: self.center_x = int( ((self.bounds.width - self.bounds.width / self.text_scale) / 2.0) * self.gscale) elif self.textAlign == ALIGN_LEFT: self.center_x = 0 else: self.center_x = int( (self.bounds.width - self.bounds.width / self.text_scale) * self.gscale) #self.center_x = ((self.bounds.width-self.bounds.width/self.text_scale)/2.0)*self.gscale #print (self.center_y + self.motion_y)*self.gscale if self.textForAnimation != self.actualLine: #print "treba to %f" % self.anim_fraction self.textForAnimation = self.actualLine # resize text buffer if needed if self.lyrics.entities[ self. actualLine].height * self.gscale > self._buffer1.height: self._buffer1.resize( self._buffer1.width, self.lyrics.entities[self.actualLine].height * self.gscale) self._buffer1.clear() #options = self._buffer1.ctx.get_font_options() #options.set_hint_metrics(cairo.HINT_METRICS_OFF) #self._buffer1.ctx.set_font_options(options) self.drawScaledLyric(self._buffer1.ctx, self.lyrics.entities[self.actualLine], 1) # create text path for next line for animation if self.actualLine + 1 < len(self.lyrics.entities): #self.next_text_i = self.getScaledText(ctx, self.lyrics.entities[self.actualLine+1], self.text_scale) ctx.set_source_rgba(1, 1, 1, 1) # resize text buffer if needed if self.lyrics.entities[ self.actualLine + 1].height * self.gscale * self.text_scale > self._buffer2.height: self._buffer2.resize( self._buffer2.width, self.lyrics.entities[self.actualLine + 1].height * self.gscale * self.text_scale) self._buffer2.clear() self.drawScaledLyric( self._buffer2.ctx, self.lyrics.entities[self.actualLine + 1], self.text_scale) options = ctx.get_font_options() #else: print "NETREBA" self.lastState = [self.actualLine, self.anim_fraction] self.dec_color = ( self.color_highlight[0] + (self.color_normal[0] - self.color_highlight[0]) * fraction, self.color_highlight[1] + (self.color_normal[1] - self.color_highlight[1]) * fraction, self.color_highlight[2] + (self.color_normal[2] - self.color_highlight[2]) * fraction, self.color_highlight[3] + (self.color_normal[3] - self.color_highlight[3]) * fraction) self.inc_color = ( self.color_normal[0] + (self.color_highlight[0] - self.color_normal[0]) * fraction, self.color_normal[1] + (self.color_highlight[1] - self.color_normal[1]) * fraction, self.color_normal[2] + (self.color_highlight[2] - self.color_normal[2]) * fraction, self.color_normal[3] + (self.color_highlight[3] - self.color_normal[3]) * fraction) ################################# ##### And finally Rendering ##### ################################# ctx.translate(0, (self.center_y + self.motion_y) * self.gscale) #ctx.translate(0, int((self.center_y + self.motion_y)*self.gscale)) self.dec_scale2 = 1.0 - fraction * (1.0 - 1.0 / self.text_scale) ############################## ##### render actual text ##### ############################## if self.anim_fraction == 0.0: # render actual line - scaled at maximum ctx.set_source_rgba(self.color_highlight[0], self.color_highlight[1], self.color_highlight[2], self.color_highlight[3]) self.drawScaledLyric(ctx, self.lyrics.entities[self.actualLine], self.text_scale) else: ctx.save() #ctx.translate(center_x, 0) ctx.translate(self.center_x * fraction, 0) ctx.scale(self.dec_scale, self.dec_scale) """ ctx.set_source_rgba (*self.dec_color) ctx.rectangle(0,0, self.bounds.width, 50) ctx.fill() ctx.set_operator(cairo.OPERATOR_DEST_IN) ctx.set_source_pixmap(self.__buffer, 0, 0) ctx.paint() """ ctx.rectangle(0, 0, self._buffer1.width, self._buffer1.height) ctx.clip() ctx.set_source_pixmap(self._buffer1.pixmap, 0, 0) ctx.paint() ctx.set_operator(cairo.OPERATOR_IN) ctx.set_source_rgba(*self.dec_color) ctx.rectangle(0, 0, self._buffer1.width, self._buffer1.height) ctx.fill() ctx.restore() ############################### ##### render texts before ##### ############################### #""" ctx.translate(self.center_x, 0) #CENTER # set graphics state for text before and after actual lyrics line ctx.set_source_rgba(self.color_normal[0], self.color_normal[1], self.color_normal[2], self.color_normal[3]) ctx.save() i = self.actualLine y = self.center_y + self.motion_y while i > 0 and y > 0: i -= 1 ctx.translate(0, -self.lyrics.entities[i].height * self.gscale) #matrix = ctx.get_matrix() #print matrix #self.drawScaledLyric(ctx, self.lyrics.entities[i], 1, cache = self.anim_fraction != 0.0) self.drawScaledLyric(ctx, self.lyrics.entities[i], 1, cache=True) #extents = self.last_text_extents[1] #ctx.rectangle(*extents) #ctx.stroke() #ctx.fill() y -= self.lyrics.entities[i].height ctx.restore() #""" ############################### ##### render texts behind ##### ############################### # height after text scale operation h = self.lyrics.entities[ self. actualLine].trimmed_height * self.dec_scale + self.lyrics.entities[ self.actualLine].height - self.lyrics.entities[ self.actualLine].trimmed_height h = int(h) ctx.translate(0, h * self.gscale) i = self.actualLine y = self.center_y + self.motion_y + h while i + 1 < len(self.lyrics.entities) and y < self.bounds.height: i += 1 if i == self.actualLine + 1: # next text - need some animation ctx.save() #ctx.set_source_rgba (*self.inc_color) ctx.set_source_rgba(*self.inc_color) if fraction != 0: ctx.save() # align to center, correction after scale operation ctx.translate(-self.center_x * fraction, 0) self.inc_scalei = 1.0 / self.text_scale + ( 1.0 - (1.0 / self.text_scale)) * fraction ctx.scale(self.inc_scalei, self.inc_scalei) #ctx.set_source_rgba (*self.inc_color) #ctx.mask(self.next_text_i) ctx.rectangle(0, 0, self._buffer2.width, self._buffer2.height) ctx.clip() ctx.set_source_pixmap(self._buffer2.pixmap, 0, 0) ctx.paint() ctx.set_operator(cairo.OPERATOR_IN) ctx.set_source_rgba(*self.inc_color) ctx.rectangle(0, 0, self._buffer2.width, self._buffer2.height) ctx.fill() ctx.restore() if fraction == 0: self.drawScaledLyric(ctx, self.lyrics.entities[i], 1) #text = self.getScaledText(ctx, self.lyrics.entities[i], 1);ctx.mask(text);ctx.fill() ctx.restore() # height after text scale operation h = self.lyrics.entities[ i].trimmed_height * self.inc_scale + self.lyrics.entities[ i].height - self.lyrics.entities[i].trimmed_height ctx.translate(0, h * self.gscale) y += h else: #self.drawScaledLyric(ctx, self.lyrics.entities[i], 1, cache = self.anim_fraction != 0) self.drawScaledLyric(ctx, self.lyrics.entities[i], 1, cache=True) ctx.translate(0, self.lyrics.entities[i].height * self.gscale) y += self.lyrics.entities[i].height ctx.restore() # make soft edges ctx.save() ctx.set_operator(cairo.OPERATOR_DEST_OUT) ctx.set_source(self.gradient) ctx.rectangle(0, 0, self.bounds.width, 10) ctx.fill() ctx.scale(1, -1) ctx.translate(0, -self.bounds.height) ctx.set_source(self.gradient) ctx.rectangle(0, 0, self.bounds.width, 10) ctx.fill() ctx.set_source_rgba(1, 0, 1, 1.0 - self.alpha) ctx.rectangle(0, 0, self.bounds.width, self.bounds.height) ctx.fill() ctx.restore() ctx.save() ctx.set_operator(cairo.OPERATOR_DEST_OVER) ctx.scale(self.bounds.width / 300.0, self.bounds.height / 130.0) self.theme.render(ctx, 'background') ctx.restore() """ # render message instead of lyrics if not self.render_lyrics: ctx.translate(center_x, self.bounds.height/2) ctx.set_source_rgba (1, 1, 1, 1) self.layout.set_text(self.message) ctx.show_layout(self.layout) """ #while gtk.events_pending(): # gtk.main_iteration(False) t_end = gobject.get_current_time()