def __init__(self): super().__init__() # don't pass parent because of mem problem # Because some fonts have much more space at top and bottom, we use ascent instead # of height, and add it with a small number. magic = int(4 * scaleRatio) self.title_h = max(font.title_m.ascent(), font.datetime_m.ascent()) + magic self.titleArea_h = self.title_h + 4 self.text_h = font.text_m.lineSpacing() * settings['Main'].getint( 'previewLines') self.tagPath_h = font.default_m.ascent() + magic self.tag_h = self.tagPath_h + 4 self.dt_w = font.datetime_m.width( datetimeTrans('2000-01-01 00:00')) + 40 self.all_h = None # updated in sizeHint before each item being painting # doc is used to draw text(diary's body) self.doc = NTextDocument() self.doc.setDefaultFont(font.text) self.doc.setUndoRedoEnabled(False) self.doc.setDocumentMargin(0) self.doc.documentLayout().setPaintDevice( qApp.desktop()) # refer actual list will cause segfault # setup colors self.c_text = Qt.black self.c_bg = QColor(255, 236, 176) self.c_border = QColor(214, 172, 41) self.c_inActBg = QColor(255, 236, 176, 40) self.c_gray = QColor(93, 73, 57)
def fromDiaryDict(self, dic): self.timeModified = self.tagModified = False self.id = dic['id'] self.datetime = dic.get('datetime') self.dtBtn.setText(datetimeTrans(self.datetime) if self.datetime else '') self.titleEditor.setText(dic.get('title', '')) self.tagEditor.setText(dic.get('tags', '')) self.textEditor.setRichText(dic.get('text', ''), dic.get('formats')) # if title is empty, use datetime instead. if no datetime (new), use "New Diary" t = (dic.get('title') or (datetimeTrans(self.datetime, stripTime=True) if 'datetime' in dic else None) or self.tr('New Diary')) self.setWindowTitle("%s - Hazama" % t) readOnly = (settings['Editor'].getboolean('autoReadOnly') and self.datetime is not None and datetimeToQt(self.datetime).daysTo(QDateTime.currentDateTime()) > 3) self.setReadOnly(readOnly)
def on_dtBtn_clicked(self): """Show datetime edit dialog""" if self.readOnly: return dtStr = currentDatetime() if self.datetime is None else self.datetime newDt = DateTimeDialog.getDateTime(datetimeToQt(dtStr), fullDatetimeFmt, self) if newDt is not None: newDtStr = newDt.toString(DB_DATETIME_FMT_QT) if newDtStr != self.datetime: self.datetime = newDtStr self.dtBtn.setText(datetimeTrans(newDtStr)) self.timeModified = True
def setTexts(self, dt, text, title, tags): # Some layout behaviours are full of mystery, even changing order of # calls will break the UI self.datetime.setText(datetimeTrans(dt)) # without this width of dt will not be updated (for performance reason?) self._hLayout0.activate() # width of title area depends on itemW's width self.title.setText( font.title_m.elidedText(title, Qt.ElideRight, self.title.width())) self.text.setText(text) if tags: tags = ' \u2022 '.join(tags.split()) # use bullet to separate self.tag.setText(tags) self.tag.setVisible(bool(tags)) self.tagIco.setVisible(bool(tags))
def getItemWidget(self, index, row, recycled): w = recycled or self.ItemWidget() if index is not None: dt, text, title, tags = (index.sibling(row, i).data() for i in range(1, 5)) # Some layout behaviours are full of mystery, be careful!!!! w.datetime.setText(datetimeTrans(dt)) w.title.setText(title) w.text.setText(text) if tags: tags = ' \u2022 '.join(tags.split()) # use bullet to separate w.tag.setText(tags) w.tag.setVisible(bool(tags)) w.tagIco.setVisible(bool(tags)) return w
def __init__(self): super().__init__() # don't pass parent because of mem problem # Because some fonts have much more space at top and bottom, we use ascent instead # of height, and add it with a small number. magic = int(4 * scaleRatio) self.title_h = max(font.title_m.ascent(), font.datetime_m.ascent()) + magic self.titleArea_h = self.title_h + 4 self.text_h = font.text_m.lineSpacing() * settings['Main'].getint('previewLines') self.tagPath_h = font.default_m.ascent() + magic self.tag_h = self.tagPath_h + 4 self.dt_w = font.datetime_m.width(datetimeTrans('2000-01-01 00:00')) + 40 self.all_h = None # updated in sizeHint before each item being painting # doc is used to draw text(diary's body) self.doc = NTextDocument() self.doc.setDefaultFont(font.text) self.doc.setUndoRedoEnabled(False) self.doc.setDocumentMargin(0) self.doc.documentLayout().setPaintDevice(qApp.desktop()) # refer actual list will cause segfault # setup colors self.c_text = Qt.black self.c_bg = QColor(255, 236, 176) self.c_border = QColor(214, 172, 41) self.c_inActBg = QColor(255, 236, 176, 40) self.c_gray = QColor(93, 73, 57)
def paint(self, painter, option, index): x, y, w = option.rect.x(), option.rect.y(), option.rect.width()-1 row = index.row() dt, text, title, tags, formats = (index.sibling(row, i).data() for i in range(1, 6)) selected = bool(option.state & QStyle.State_Selected) active = bool(option.state & QStyle.State_Active) # draw border and background painter.setPen(self.c_border) painter.setBrush(self.c_bg if selected and active else self.c_inActBg) painter.drawRect(x+1, y, w-2, self.all_h) # outer border if selected: # draw inner border pen = QPen() pen.setStyle(Qt.DashLine) pen.setColor(self.c_gray) painter.setPen(pen) painter.drawRect(x+2, y+1, w-4, self.all_h-2) # draw datetime and title painter.setPen(self.c_gray) painter.drawLine(x+10, y+self.titleArea_h, x+w-10, y+self.titleArea_h) painter.setPen(self.c_text) painter.setFont(font.datetime) painter.drawText(x+14, y+self.titleArea_h-self.title_h, self.dt_w, self.title_h, Qt.AlignVCenter, datetimeTrans(dt)) if title: painter.setFont(font.title) title_w = w - self.dt_w - 13 title = font.title_m.elidedText(title, Qt.ElideRight, title_w) painter.drawText(x+self.dt_w, y+self.titleArea_h-self.title_h, title_w, self.title_h, Qt.AlignVCenter | Qt.AlignRight, title) # draw text self.doc.setText(text, formats) self.doc.setTextWidth(w-26) painter.translate(x+14, y+self.titleArea_h+2) self.doc.drawContentsColor(painter, QRect(0, 0, w-26, self.text_h), self.c_text) painter.resetTransform() # draw tags if tags: painter.setPen(self.c_gray) painter.setFont(font.default) painter.translate(x + 15, y+self.titleArea_h+6+self.text_h) real_x, max_x = x+15, w-10 for t in tags.split(): oneTag_w = font.default_m.width(t) + 4 real_x += oneTag_w + 15 if real_x > max_x: break tagPath = QPainterPath() tagPath.moveTo(8, 0) tagPath.lineTo(8+oneTag_w, 0) tagPath.lineTo(8+oneTag_w, self.tagPath_h) tagPath.lineTo(8, self.tagPath_h) tagPath.lineTo(0, self.tagPath_h/2) tagPath.closeSubpath() painter.drawPath(tagPath) painter.drawText(8, 0, oneTag_w, self.tagPath_h, Qt.AlignCenter, t) painter.translate(oneTag_w+15, 0) # translate by offset else: painter.resetTransform() return # draw ellipsis if too many tags painter.setPen(Qt.DotLine) painter.drawLine(-4, self.tagPath_h/2, 2, self.tagPath_h/2) painter.resetTransform()
def paint(self, painter, option, index): x, y, w = option.rect.x(), option.rect.y(), option.rect.width() - 1 row = index.row() dt, text, title, tags, formats = (index.sibling(row, i).data() for i in range(1, 6)) selected = bool(option.state & QStyle.State_Selected) active = bool(option.state & QStyle.State_Active) # draw border and background painter.setPen(self.c_border) painter.setBrush(self.c_bg if selected and active else self.c_inActBg) painter.drawRect(x + 1, y, w - 2, self.all_h) # outer border if selected: # draw inner border pen = QPen() pen.setStyle(Qt.DashLine) pen.setColor(self.c_gray) painter.setPen(pen) painter.drawRect(x + 2, y + 1, w - 4, self.all_h - 2) # draw datetime and title painter.setPen(self.c_gray) painter.drawLine(x + 10, y + self.titleArea_h, x + w - 10, y + self.titleArea_h) painter.setPen(self.c_text) painter.setFont(font.datetime) painter.drawText(x + 14, y + self.titleArea_h - self.title_h, self.dt_w, self.title_h, Qt.AlignVCenter, datetimeTrans(dt)) if title: painter.setFont(font.title) title_w = w - self.dt_w - 13 title = font.title_m.elidedText(title, Qt.ElideRight, title_w) painter.drawText(x + self.dt_w, y + self.titleArea_h - self.title_h, title_w, self.title_h, Qt.AlignVCenter | Qt.AlignRight, title) # draw text self.doc.setText(text, formats) self.doc.setTextWidth(w - 26) painter.translate(x + 14, y + self.titleArea_h + 2) self.doc.drawContentsColor(painter, QRect(0, 0, w - 26, self.text_h), self.c_text) painter.resetTransform() # draw tags if tags: painter.setPen(self.c_gray) painter.setFont(font.default) painter.translate(x + 15, y + self.titleArea_h + 6 + self.text_h) real_x, max_x = x + 15, w - 10 for t in tags.split(): oneTag_w = font.default_m.width(t) + 4 real_x += oneTag_w + 15 if real_x > max_x: break tagPath = QPainterPath() tagPath.moveTo(8, 0) tagPath.lineTo(8 + oneTag_w, 0) tagPath.lineTo(8 + oneTag_w, self.tagPath_h) tagPath.lineTo(8, self.tagPath_h) tagPath.lineTo(0, self.tagPath_h / 2) tagPath.closeSubpath() painter.drawPath(tagPath) painter.drawText(8, 0, oneTag_w, self.tagPath_h, Qt.AlignCenter, t) painter.translate(oneTag_w + 15, 0) # translate by offset else: painter.resetTransform() return # draw ellipsis if too many tags painter.setPen(Qt.DotLine) painter.drawLine(-4, self.tagPath_h / 2, 2, self.tagPath_h / 2) painter.resetTransform()