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 __init__(self, parent=None, lines=None, **kwargs): super().__init__(parent, **kwargs) self._lines = self._heightHint = None self.doc = NTextDocument(self) self.doc.setDocumentMargin(0) self.doc.setUndoRedoEnabled(False) self.setLines(lines if lines else 4) self.doc.documentLayout().setPaintDevice(self) # make difference on high DPI
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._doc = NTextDocument(self) self.setDocument(self._doc) # remove highlight color's alpha to avoid alpha loss in copy&paste. # NTextDocument should use this color too. hl, bg = self.HlColor, self.palette().base().color() fac = hl.alpha() / 255 self.HlColor = QColor(round(hl.red()*fac + bg.red()*(1-fac)), round(hl.green()*fac + bg.green()*(1-fac)), round(hl.blue()*fac + bg.blue()*(1-fac))) self.autoIndent = False # used by tab indent shortcut if QLocale().language() in (QLocale.Chinese, QLocale.Japanese): self._indent = ' ' # 2 full width spaces else: self._indent = ' ' # 4 spaces # setup format menu onHLAct = lambda: super(NTextEdit, self).setHL(self.hlAct.isChecked()) onBDAct = lambda: super(NTextEdit, self).setBD(self.bdAct.isChecked()) onSOAct = lambda: super(NTextEdit, self).setSO(self.soAct.isChecked()) onULAct = lambda: super(NTextEdit, self).setUL(self.ulAct.isChecked()) onItaAct = lambda: super(NTextEdit, self).setIta(self.itaAct.isChecked()) self.fmtMenu = QMenu(self.tr('Format'), self) # shortcuts of format actions only used to display shortcut-hint in menu self.hlAct = QAction(makeQIcon(':/menu/highlight.png'), self.tr('Highlight'), self, triggered=onHLAct, shortcut=QKeySequence('Ctrl+H')) self.bdAct = QAction(makeQIcon(':/menu/bold.png'), self.tr('Bold'), self, triggered=onBDAct, shortcut=QKeySequence.Bold) self.soAct = QAction(makeQIcon(':/menu/strikeout.png'), self.tr('Strike out'), self, triggered=onSOAct, shortcut=QKeySequence('Ctrl+T')) self.ulAct = QAction(makeQIcon(':/menu/underline.png'), self.tr('Underline'), self, triggered=onULAct, shortcut=QKeySequence.Underline) self.itaAct = QAction(makeQIcon(':/menu/italic.png'), self.tr('Italic'), self, triggered=onItaAct, shortcut=QKeySequence.Italic) self.clrAct = QAction(self.tr('Clear format'), self, shortcut=QKeySequence('Ctrl+D'), triggered=self.clearFormat) self.acts = (self.hlAct, self.bdAct, self.soAct, self.ulAct, self.itaAct) # excluding uncheckable clrAct for a in self.acts: self.fmtMenu.addAction(a) a.setCheckable(True) self.fmtMenu.addSeparator() self.addAction(self.clrAct) self.fmtMenu.addAction(self.clrAct) self.key2act = { Qt.Key_H: self.hlAct, Qt.Key_B: self.bdAct, Qt.Key_T: self.soAct, Qt.Key_U: self.ulAct, Qt.Key_I: self.itaAct}
def test_overlap(self): test_str = 'This is something, string, string.\nparagraph, paragraph!\n method?' test_fmt = [(0, 2, 1), (0, 2, 2), (0, 10, 3), (5, 15, 4), (34, 3, 5)] # output formats may be duplicated, because Qt store format this way true_result = [ (0, 2, 1), (0, 2, 2), (0, 2, 3), (2, 3, 3), (5, 5, 3), # from (0, 10, 3), broken into three parts (5, 5, 4), (10, 10, 4), # from (5, 15, 4) (35, 2, 5) # 34 is \n ] doc = NTextDocument() doc.setText(test_str, test_fmt) result = NTextDocument.getFormats(doc) self.assertEqual(true_result, result)