def __init__(self, stylesheet, data, preview, snippet, parent=None): QtGui.QWidget.__init__(self, parent) self.scale = .3 self.data = data self.ui = Ui_pagesetup() self.ui.setupUi(self) self.stylesheet = stylesheet self.ui.preview = preview self.ui.snippet = snippet ft = self.stylesheet.firstTemplate for template in self.stylesheet.pageTemplates: if ft == template: continue self.ui.firstTemplate.addItem(template) if 'size' in self.stylesheet.page: self.ui.size.insertItem(0, self.stylesheet.psname) self.ui.height.setEnabled(False) self.ui.width.setEnabled(False) self.ui.height.setText('') self.ui.width.setText('') else: self.ui.height.setEnabled(True) self.ui.width.setEnabled(True) self.ui.height.setText(self.stylesheet.page['height']) self.ui.width.setText(self.stylesheet.page['width']) self.ui.size.setCurrentIndex(0) self.ui.firstTemplate.insertItem(0, ft) self.ui.firstTemplate.setCurrentIndex(0) self.ui.margin_top.setText(unicode(self.stylesheet.page['margin-top'])) self.ui.margin_bottom.setText( unicode(self.stylesheet.page['margin-bottom'])) self.ui.margin_left.setText( unicode(self.stylesheet.page['margin-left'])) self.ui.margin_right.setText( unicode(self.stylesheet.page['margin-right'])) self.ui.margin_gutter.setText( unicode(self.stylesheet.page['margin-gutter'])) self.ui.spacing_header.setText( unicode(self.stylesheet.page['spacing-header'])) self.ui.spacing_footer.setText( unicode(self.stylesheet.page['spacing-footer'])) self.pageImage = None self.applyChanges()
def __init__(self, stylesheet, data, preview, snippet, parent=None): QtGui.QWidget.__init__(self,parent) self.scale = .3 self.data = data self.ui=Ui_pagesetup() self.ui.setupUi(self) self.stylesheet=stylesheet self.ui.preview=preview self.ui.snippet=snippet ft=self.stylesheet.firstTemplate for template in self.stylesheet.pageTemplates: if ft == template: continue self.ui.firstTemplate.addItem(template) if 'size' in self.stylesheet.page: self.ui.size.insertItem(0,self.stylesheet.psname) self.ui.height.setEnabled(False) self.ui.width.setEnabled(False) self.ui.height.setText('') self.ui.width.setText('') else: self.ui.height.setEnabled(True) self.ui.width.setEnabled(True) self.ui.height.setText(self.stylesheet.page['height']) self.ui.width.setText(self.stylesheet.page['width']) self.ui.size.setCurrentIndex(0) self.ui.firstTemplate.insertItem(0,ft) self.ui.firstTemplate.setCurrentIndex(0) self.ui.margin_top.setText(str(self.stylesheet.page['margin-top'])) self.ui.margin_bottom.setText(str(self.stylesheet.page['margin-bottom'])) self.ui.margin_left.setText(str(self.stylesheet.page['margin-left'])) self.ui.margin_right.setText(str(self.stylesheet.page['margin-right'])) self.ui.margin_gutter.setText(str(self.stylesheet.page['margin-gutter'])) self.ui.spacing_header.setText(str(self.stylesheet.page['spacing-header'])) self.ui.spacing_footer.setText(str(self.stylesheet.page['spacing-footer'])) self.pageImage=None self.applyChanges()
class PageSetup(QtGui.QWidget): def __init__(self, stylesheet, data, preview, snippet, parent=None): QtGui.QWidget.__init__(self,parent) self.scale = .3 self.data = data self.ui=Ui_pagesetup() self.ui.setupUi(self) self.stylesheet=stylesheet self.ui.preview=preview self.ui.snippet=snippet ft=self.stylesheet.firstTemplate for template in self.stylesheet.pageTemplates: if ft == template: continue self.ui.firstTemplate.addItem(template) if 'size' in self.stylesheet.page: self.ui.size.insertItem(0,self.stylesheet.psname) self.ui.height.setEnabled(False) self.ui.width.setEnabled(False) self.ui.height.setText('') self.ui.width.setText('') else: self.ui.height.setEnabled(True) self.ui.width.setEnabled(True) self.ui.height.setText(self.stylesheet.page['height']) self.ui.width.setText(self.stylesheet.page['width']) self.ui.size.setCurrentIndex(0) self.ui.firstTemplate.insertItem(0,ft) self.ui.firstTemplate.setCurrentIndex(0) self.ui.margin_top.setText(str(self.stylesheet.page['margin-top'])) self.ui.margin_bottom.setText(str(self.stylesheet.page['margin-bottom'])) self.ui.margin_left.setText(str(self.stylesheet.page['margin-left'])) self.ui.margin_right.setText(str(self.stylesheet.page['margin-right'])) self.ui.margin_gutter.setText(str(self.stylesheet.page['margin-gutter'])) self.ui.spacing_header.setText(str(self.stylesheet.page['spacing-header'])) self.ui.spacing_footer.setText(str(self.stylesheet.page['spacing-footer'])) self.pageImage=None self.applyChanges() def applyChanges(self): if str(self.ui.size.currentText())=='Custom': # FIXME: % makes no sense for page size self.ui.width.setEnabled(True) self.ui.height.setEnabled(True) self.pw=self.stylesheet.adjustUnits(str(self.ui.width.text()),1000) or 0 self.ph=self.stylesheet.adjustUnits(str(self.ui.height.text()),1000) or 0 else: self.ui.width.setEnabled(False) self.ui.height.setEnabled(False) self.size=str(self.ui.size.currentText()) self.pw=pagesizes.__dict__[self.size.upper()][0] self.ph=pagesizes.__dict__[self.size.upper()][1] self.lm=self.stylesheet.adjustUnits(str(self.ui.margin_left.text()),self.pw) or 0 self.rm=self.stylesheet.adjustUnits(str(self.ui.margin_right.text()),self.pw) or 0 self.tm=self.stylesheet.adjustUnits(str(self.ui.margin_top.text()),self.ph) or 0 self.bm=self.stylesheet.adjustUnits(str(self.ui.margin_bottom.text()),self.ph) or 0 self.ts=self.stylesheet.adjustUnits(str(self.ui.spacing_header.text()),self.ph) or 0 self.bs=self.stylesheet.adjustUnits(str(self.ui.spacing_footer.text()),self.ph) or 0 self.gm=self.stylesheet.adjustUnits(str(self.ui.margin_gutter.text()),self.pw) or 0 self.pageImage=QtGui.QImage(int(self.pw), int(self.ph), QtGui.QImage.Format_RGB32) self.updatePreview() def updatePreview(self): pm=QtGui.QPixmap(self.pageImage) p=QtGui.QPainter(pm) pen = QtGui.QPen() pen.setWidth(1/self.scale) # Make it be 1px wide when scaled p.setPen(pen) # Draw white page p.setBrush(QtGui.QBrush(QtGui.QColor("white"))) p.drawRect(-1,-1,pm.width()+2,pm.height()+2) for x in (self.gm, self.gm+self.lm, self.pw-self.rm): p.drawLine(x,0,x,pm.height()) for y in (self.tm, self.tm+self.ts, self.ph-self.bm, self.ph-self.bm-self.bs, ): p.drawLine(0,y,pm.width(),y) p.end() self.ui.preview.setPixmap(pm.scaled(self.pw*self.scale,self.ph*self.scale)) self.data["pageSetup"]= { "size": str(self.ui.size.currentText()).lower(), "width": str(self.ui.width.text()), "height": str(self.ui.height.text()), "margin-top": str(self.ui.margin_top.text()), "margin-bottom": str(self.ui.margin_bottom.text()), "margin-left": str(self.ui.margin_left.text()), "margin-right": str(self.ui.margin_right.text()), "margin-gutter": str(self.ui.margin_gutter.text()), "spacing-header": str(self.ui.spacing_header.text()), "spacing-footer": str(self.ui.spacing_footer.text()), "firstTemplate": str(self.ui.firstTemplate.currentText()) } if self.data['pageSetup']['size']=='custom': del(self.data['pageSetup']['size']) else: del(self.data['pageSetup']['width']) del(self.data['pageSetup']['height']) body=highlight(json.dumps(self.data, indent=2), JavascriptLexer(),HtmlFormatter()) head=HtmlFormatter().get_style_defs('.highlight') self.ui.snippet.setHtml( '''<HEAD> <STYLE type="text/css"> %s </STYLE> </HEAD> <BODY> %s </BODY> '''%(head,body))