def newTextLayout(widget, text="", font=None, maxSize=None, maximizeScale=0.6): """ None return value should be expected and handled, only if maxSize is given """ layout = widget.create_pango_layout("") ## a pango.Layout object if not font: font = ui.getFont() layout.set_font_description(pfontEncode(font)) if text: layout.set_markup(text) if maxSize: maxW, maxH = maxSize maxW = float(maxW) maxH = float(maxH) if maxW <= 0 or maxH <= 0: return ## layoutW, layoutH = layout.get_pixel_size() ## minRat = 1.01 * max(layoutW / maxW, layoutH / maxH) if minRat > 1: font[3] = int(font[3] / minRat) else: minRat /= maximizeScale if minRat < 1: font[3] = int(font[3] / minRat) layout.set_font_description(pfontEncode(font)) return layout
def newTextLayout(widget, text='', font=None, maxSize=None, truncate=False): layout = widget.create_pango_layout('') ## a pango.Layout object if not font: font = ui.getFont() layout.set_font_description(pfontEncode(font)) if text: layout.set_markup(text) if maxSize: (maxW, maxH) = maxSize layoutW, layoutH = layout.get_pixel_size() if layoutH > maxH: font[3] = int(font[3]*maxH//layoutH) layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() if layoutW > maxW: if truncate: char_w = layoutW/len(text) char_num = int(maxW//char_w) while layoutW > maxW: text = cutText(text, char_num) layout.set_markup(text) layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() char_num -= max(int((layoutW-maxW)//char_w), 1) if char_num<0: layout = None break else:## use smaller font, don't truncate text font[3] = int(font[3]*float(maxW)/layoutW) layout.set_font_description(pfontEncode(font)) return layout
def newLimitedWidthTextLayout(widget, text, width, font=None, truncate=True, markup=True): if not font: font = ui.getFont() layout = widget.create_pango_layout("") if markup: layout.set_markup(text) else: layout.set_text(text) layout.set_font_description(pfontEncode(font)) if not layout: return None layoutW, layoutH = layout.get_pixel_size() if layoutW > width: if truncate: char_w = layoutW / len(text) char_num = int(width // char_w) while layoutW > width: text = cutText(text, char_num) layout = widget.create_pango_layout(text) layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() char_num -= max(int((layoutW - width) // char_w), 1) if char_num < 0: layout = None break else: ## use smaller font font2 = list(font) while layoutW > width: font2[3] = 0.9 * font2[3] * width / layoutW layout.set_font_description(pfontEncode(font2)) layoutW, layoutH = layout.get_pixel_size() # print layoutW, width # print return layout
def statusIconUpdateIcon(self, ddate): ## FIXME imagePath = ui.statusIconImageHoli if ui.todayCell.holiday else ui.statusIconImage ext = os.path.splitext(imagePath)[1][1:].lower() loader = gdk.pixbuf_loader_new_with_mime_type('image/%s' % ext) if ui.statusIconFixedSizeEnable: try: width, height = ui.statusIconFixedSizeWH loader.set_size(width, height) except: myRaise() data = open(imagePath).read() if ext == 'svg': dayNum = _(ddate[2]) if ui.statusIconFontFamilyEnable: if ui.statusIconFontFamily: family = ui.statusIconFontFamily else: family = ui.getFont()[0] dayNum = '<tspan style="font-family:%s">%s</tspan>' % (family, dayNum) data = data.replace( 'TX', dayNum, ) loader.write(data) loader.close() pixbuf = loader.get_pixbuf() self.sicon.set_from_pixbuf(pixbuf)
def drawTextList(self, cr, textList, font=None): w = self.allocation.width h = self.allocation.height ### rowH = h/7.0 itemW = w - ui.wcalPadding if font is None: fontName = self.getFontValue() fontSize = ui.getFont()[-1] ## FIXME font = [fontName, False, False, fontSize] if fontName else None for i in range(7): text = textList[i] layout = newTextLayout( self, text=text, font=font, maxSize=(itemW, rowH), maximizeScale=ui.wcalTextSizeScale, ) if layout: layoutW, layoutH = layout.get_pixel_size() layoutX = (w-layoutW)/2.0 layoutY = (i+0.5)*rowH - layoutH/2.0 cr.move_to(layoutX, layoutY) if self.colorizeHolidayText and self.wcal.status[i].holiday: setColor(cr, ui.holidayColor) else: setColor(cr, ui.textColor) cr.show_layout(layout)
def statusIconUpdateIcon(self, ddate):## FIXME imagePath = ui.statusIconImageHoli if ui.todayCell.holiday else ui.statusIconImage ext = os.path.splitext(imagePath)[1][1:].lower() loader = gdk.pixbuf_loader_new_with_mime_type('image/%s'%ext) if ui.statusIconFixedSizeEnable: try: width, height = ui.statusIconFixedSizeWH loader.set_size(width, height) except: myRaise() data = open(imagePath).read() if ext == 'svg': dayNum = _(ddate[2]) if ui.statusIconFontFamilyEnable: if ui.statusIconFontFamily: family = ui.statusIconFontFamily else: family = ui.getFont()[0] dayNum = '<tspan style="font-family:%s">%s</tspan>'%(family, dayNum) data = data.replace( 'TX', dayNum, ) loader.write(data) loader.close() pixbuf = loader.get_pixbuf() self.sicon.set_from_pixbuf(pixbuf)
def newTextLayout( widget, text='', font=None, maxSize=None, maximizeScale=0.6, truncate=False, ): ''' None return value should be expected and handled, only if maxSize is given ''' layout = widget.create_pango_layout('') ## a Pango.Layout object if font: font = list(font) else: font = ui.getFont() layout.set_font_description(pfontEncode(font)) if text: layout.set_markup(text) if maxSize: layoutW, layoutH = layout.get_pixel_size() ## maxW, maxH = maxSize maxW = float(maxW) maxH = float(maxH) if maxW <= 0: return if maxH <= 0: minRat = 1.0 else: minRat = 1.01 * layoutH/maxH ## FIXME if truncate: if minRat > 1: font[3] = int(font[3]/minRat) layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() if layoutW > 0: char_w = float(layoutW)/len(text) char_num = int(maxW//char_w) while layoutW > maxW: text = cutText(text, char_num) if not text: break layout = widget.create_pango_layout(text) layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() char_num -= max(int((layoutW-maxW)//char_w), 1) if char_num<0: layout = None break else: if maximizeScale > 0: minRat = minRat/maximizeScale if minRat < layoutW/maxW: minRat = layoutW/maxW if minRat > 1: font[3] = int(font[3]/minRat) layout.set_font_description(pfontEncode(font)) return layout
def drawBoxText(cr, box, x, y, w, h, widget): ## now draw the text ## how to find the best font size based in the box's width and height, ## and font family? FIXME ## possibly write in many lines? or just in one line and wrap if needed? if box.text: #print(box.text) textW = 0.9 * w textH = 0.9 * h textLen = len(toUnicode(box.text)) #print('textLen=%s'%textLen) avgCharW = float(textW if rotateBoxLabel == 0 else max(textW, textH)) / textLen if avgCharW > 3: ## FIXME font = list(ui.getFont()) layout = widget.create_pango_layout( box.text) ## a pango.Layout object layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() #print('orig font size: %s'%font[3]) normRatio = min( float(textW) / layoutW, float(textH) / layoutH, ) rotateRatio = min( float(textW) / layoutH, float(textH) / layoutW, ) if rotateBoxLabel != 0 and rotateRatio > normRatio: font[3] *= max(normRatio, rotateRatio) layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() fillColor(cr, fgColor) ## before cr.move_to #print('x=%s, y=%s, w=%s, h=%s, layoutW=%s, layoutH=%s'\) # %(x,y,w,h,layoutW,layoutH) cr.move_to( x + (w - rotateBoxLabel * layoutH) / 2.0, y + (h + rotateBoxLabel * layoutW) / 2.0, ) cr.rotate(-rotateBoxLabel * pi / 2) cr.show_layout(layout) try: cr.rotate(rotateBoxLabel * pi / 2) except: print('counld not rotate by %s*pi/2 = %s' % ( rotateBoxLabel, rotateBoxLabel * pi / 2, )) else: font[3] *= normRatio layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() fillColor(cr, fgColor) ## before cr.move_to cr.move_to( x + (w - layoutW) / 2.0, y + (h - layoutH) / 2.0, ) cr.show_layout(layout)
def newTextLayout( widget, text='', font=None, maxSize=None, maximizeScale=0.6, truncate=False, ): ''' None return value should be expected and handled, only if maxSize is given ''' layout = widget.create_pango_layout('') ## a pango.Layout object if not font: font = ui.getFont() layout.set_font_description(pfontEncode(font)) if text: layout.set_markup(text) if maxSize: layoutW, layoutH = layout.get_pixel_size() ## maxW, maxH = maxSize maxW = float(maxW) maxH = float(maxH) if maxW <= 0: return if maxH <= 0: minRat = 1.0 else: minRat = 1.01 * layoutH/maxH ## FIXME if truncate: if minRat > 1: font[3] = int(font[3]/minRat) layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() if layoutW > 0: char_w = float(layoutW)/len(text) char_num = int(maxW//char_w) while layoutW > maxW: text = cutText(text, char_num) if not text: break layout = widget.create_pango_layout(text) layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() char_num -= max(int((layoutW-maxW)//char_w), 1) if char_num<0: layout = None break else: if maximizeScale > 0: minRat = minRat/maximizeScale if minRat < layoutW/maxW: minRat = layoutW/maxW if minRat > 1: font[3] = int(font[3]/minRat) layout.set_font_description(pfontEncode(font)) return layout
def onConfigChange(self, *a, **ka): ui.cellCache.clear() settings.set_property( "gtk-font-name", pfontEncode(ui.getFont()).to_string(), ) #### BaseCalObj.onConfigChange(self, *a, **ka) self.onDateChange()
def onConfigChange(self, *a, **ka): ui.cellCache.clear() settings.set_property( 'gtk-font-name', pfontEncode(ui.getFont()), ) #### IntegratedCalObj.onConfigChange(self, *a, **ka) self.onDateChange()
def drawBoxText(cr, box, x, y, w, h, widget): ## now draw the text ## how to find the best font size based in the box's width and height, ## and font family? FIXME ## possibly write in many lines? or just in one line and wrap if needed? if box.text: #print(box.text) textW = 0.9 * w textH = 0.9 * h textLen = len(toUnicode(box.text)) #print('textLen=%s'%textLen) avgCharW = float(textW if rotateBoxLabel == 0 else max(textW, textH)) / textLen if avgCharW > 3:## FIXME font = list(ui.getFont()) layout = widget.create_pango_layout(box.text) ## a pango.Layout object layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() #print('orig font size: %s'%font[3]) normRatio = min( float(textW)/layoutW, float(textH)/layoutH, ) rotateRatio = min( float(textW)/layoutH, float(textH)/layoutW, ) if rotateBoxLabel != 0 and rotateRatio > normRatio: font[3] *= max(normRatio, rotateRatio) layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() fillColor(cr, fgColor)## before cr.move_to #print('x=%s, y=%s, w=%s, h=%s, layoutW=%s, layoutH=%s'\) # %(x,y,w,h,layoutW,layoutH) cr.move_to( x + (w - rotateBoxLabel*layoutH)/2.0, y + (h + rotateBoxLabel*layoutW)/2.0, ) cr.rotate(-rotateBoxLabel*pi/2) cr.show_layout(layout) try: cr.rotate(rotateBoxLabel*pi/2) except: print('counld not rotate by %s*pi/2 = %s'%( rotateBoxLabel, rotateBoxLabel*pi/2, )) else: font[3] *= normRatio layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() fillColor(cr, fgColor)## before cr.move_to cr.move_to( x + (w-layoutW)/2.0, y + (h-layoutH)/2.0, ) cr.show_layout(layout)
def updateTextWidth(self): ### update width of week days names to understand that should be ### synopsis or no qfont = qfontEncode(ui.getFont()) wm = 0 ## max width for i in xrange(7): w = calcTextWidth(core.weekDayName[i], qfont) #print w, if w > wm: wm = w self.wdaysWidth = wm*7 + ui.mcalLeftMargin
def calcTextWidth(text, font=None): if isinstance(text, str): text = text.decode('utf-8') if font==None: qfont = qfontEncode(ui.getFont()) elif isinstance(font, tuple): qfont = qfontEncode(font) else: qfont = font n = len(text) met = qt.QFontMetrics(font) ## OR met = widget.fontMetrics() w = 0 for i in range(n): w += met.charWidth(text, i) return w
def calcTextWidth(text, arg=None): if isinstance(text, str): text = text.decode('utf-8') if arg==None: qfont = qfontEncode(ui.getFont()) elif isinstance(arg, tuple): qfont = qfontEncode(arg) elif isinstance(arg, qt.QWidget): qfont = arg.font() elif isinstance(arg, qt.QFont): qfont = arg n = len(text) met = qt.QFontMetrics(qfont) ## OR widget.fontMetrics() ## OR app.fontMetrics() w = 0 for i in range(n): w += met.charWidth(text, i) return w
def trayUpdateIcon(self, ddate):## FIXME imagePath = ui.trayImageHoli if ui.todayCell.holiday else ui.trayImage ext = splitext(imagePath)[1][1:].lower() loader = gdk.pixbuf_loader_new_with_mime_type('image/%s'%ext) data = open(imagePath).read() if ext == 'svg': dayNum = _(ddate[2]) if ui.trayFontFamilyEnable: if ui.trayFontFamily: family = ui.trayFontFamily else: family = ui.getFont()[0] dayNum = '<tspan style="font-family:%s">%s</tspan>'%(family, dayNum) data = data.replace( 'TX', dayNum, ) loader.write(data) loader.close() pixbuf = loader.get_pixbuf() self.sicon.set_from_pixbuf(pixbuf)
def drawTextList(self, cr, textData, font=None): w = self.get_allocation().width h = self.get_allocation().height ### rowH = h/7.0 itemW = w - ui.wcalPadding if font is None: fontName = self.getFontValue() fontSize = ui.getFont()[-1] ## FIXME font = [fontName, False, False, fontSize] if fontName else None for i in range(7): data = textData[i] if data: linesN = len(data) lineH = rowH/linesN lineI = 0 if len(data[0]) < 2: print(self._name) for line, color in data: layout = newTextLayout( self, text=line, font=font, maxSize=(itemW, lineH), maximizeScale=ui.wcalTextSizeScale, truncate=self.truncateText, ) if not layout: continue layoutW, layoutH = layout.get_pixel_size() layoutX = (w-layoutW)/2.0 layoutY = i*rowH + (lineI+0.5)*lineH - layoutH/2.0 cr.move_to(layoutX, layoutY) if self.colorizeHolidayText and self.wcal.status[i].holiday: color = ui.holidayColor if not color: color = ui.textColor setColor(cr, color) cr.show_layout(layout) lineI += 1
movingKeyTimeoutFirst = 0.5 movingKeyTimeout = 0.1 ## seconds ## continiouse keyPress delay is about 0.05 sec ############################################# truncateTickLabel = False ## 0: no rotation ## 1: 90 deg CCW (if needed) ## -1: 90 deg CW (if needed) #################################################### fontFamily = ui.getFont()[0] dayLen = 24 * 3600 minYearLenSec = 365 * dayLen avgMonthLen = 30 * dayLen unitSteps = ( (3600, 12), (3600, 6), (3600, 3), (3600, 1), (60, 30), (60, 15), (60, 5), (60, 1), (1, 30),
def drawBox(self, cr, box): d = box.lineW x = box.x w = box.w y = box.y h = box.h ### cr.rectangle(x, y, w, h) if d == 0: fillColor(cr, box.color) else: try: alpha = box.color[3] except IndexError: alpha = 255 try: fillColor(cr, ( box.color[0], box.color[1], box.color[2], int(alpha*boxInnerAlpha), )) except cairo.Error: return ### cr.set_line_width(0) cr.move_to(x, y) cr.line_to(x+w, y) cr.line_to(x+w, y+h) cr.line_to(x, y+h) cr.line_to(x, y) cr.line_to(x+d, y) cr.line_to(x+d, y+h-d) cr.line_to(x+w-d, y+h-d) cr.line_to(x+w-d, y+d) cr.line_to(x+d, y+d) cr.close_path() fillColor(cr, box.color) ######## Draw Move/Resize Border if box.hasBorder: if w > boxMoveBorder*2 and h > boxMoveBorder: b = boxMoveBorder bd = boxMoveLineW #cr.set_line_width(bd) cr.move_to(x+b, y+h) cr.line_to(x+b, y+b) cr.line_to(x+w-b, y+b) cr.line_to(x+w-b, y+h) cr.line_to(x+w-b-bd, y+h) cr.line_to(x+w-b-bd, y+b+bd) cr.line_to(x+b+bd, y+b+bd) cr.line_to(x+b+bd, y+h) cr.close_path() fillColor(cr, box.color) ### bds = 0.7*bd cr.move_to(x, y) cr.line_to(x+bds, y) cr.line_to(x+b+bds, y+b) cr.line_to(x+b, y+b+bds) cr.line_to(x, y+bds) cr.close_path() fillColor(cr, box.color) ## cr.move_to(x+w, y) cr.line_to(x+w-bds, y) cr.line_to(x+w-b-bds, y+b) cr.line_to(x+w-b, y+b+bds) cr.line_to(x+w, y+bds) cr.close_path() fillColor(cr, box.color) else: box.hasBorder = False ######## ## now draw the text ## how to find the best font size based in the box's width and height, and font family? FIXME ## possibly write in many lines? or just in one line and wrap if needed? if box.text: #print box.text textW = 0.9*w textH = 0.9*h textLen = len(toUnicode(box.text)) #print 'textLen=%s'%textLen if rotateBoxLabel == 0: avgCharW = float(textW) / textLen else: avgCharW = float(max(textW, textH)) / textLen #print 'avgCharW=%s'%avgCharW if avgCharW > 3:## FIXME font = list(ui.getFont()) layout = self.create_pango_layout(box.text) ## a pango.Layout object layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() #print 'orig font size: %s'%font[3] normRatio = min( float(textW)/layoutW, float(textH)/layoutH, ) rotateRatio = min( float(textW)/layoutH, float(textH)/layoutW, ) if rotateBoxLabel != 0 and rotateRatio > normRatio: font[3] *= max(normRatio, rotateRatio) layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() fillColor(cr, fgColor)## before cr.move_to #print 'x=%s, y=%s, w=%s, h=%s, layoutW=%s, layoutH=%s'%(x,y,w,h,layoutW,layoutH) cr.move_to( x + (w - rotateBoxLabel*layoutH)/2.0, y + (h + rotateBoxLabel*layoutW)/2.0, ) cr.rotate(-rotateBoxLabel*pi/2) cr.show_layout(layout) try: cr.rotate(rotateBoxLabel*pi/2) except: print 'counld not rotate by %s*pi/2 = %s'%(rotateBoxLabel, rotateBoxLabel*pi/2) else: font[3] *= normRatio layout.set_font_description(pfontEncode(font)) layoutW, layoutH = layout.get_pixel_size() fillColor(cr, fgColor)## before cr.move_to cr.move_to( x + (w-layoutW)/2.0, y + (h-layoutH)/2.0, ) cr.show_layout(layout)
def set(self, data): font = data['font'] self.fontCheck.set_active(bool(font)) if not font: font = ui.getFont() self.fontb.set_font_name(font)
## movingMaxSpeed = movingAccel * 4 ## reach to maximum speed in 4 seconds movingKeyTimeoutFirst = 0.5 movingKeyTimeout = 0.1 ## seconds ## continiouse keyPress delay is about 0.05 sec ############################################# truncateTickLabel = False ## 0: no rotation ## 1: 90 deg CCW (if needed) ## -1: 90 deg CW (if needed) #################################################### fontFamily = ui.getFont()[0] dayLen = 24 * 3600 minYearLenSec = 365 * dayLen avgMonthLen = 30 * dayLen unitSteps = ( (3600, 12), (3600, 6), (3600, 3), (3600, 1), (60, 30), (60, 15), (60, 5), (60, 1), (1, 30),