class ItemWidget(QFrame): """Widget that used to draw an item in paint method.""" def __init__(self, parent=None): super().__init__(parent, objectName='DiaryListItem') self.title = NElideLabel(self, objectName='DiaryListItemTitle') self.title.setFont(font.title) self.title.setAlignment(Qt.AlignVCenter | Qt.AlignRight) self.title.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) self.datetime = QLabel(self, objectName='DiaryListItemDt') self.datetime.setFont(font.datetime) self.datetime.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Preferred) self.text = MultiLineElideLabel(self, objectName='DiaryListItemText', forceHeightHint=True) self.text.setMaximumLineCount( settings['Main'].getint('previewLines')) self.text.setFont(font.text) self.tag = NElideLabel(self, objectName='DiaryListItemTag') # use QToolButton to display icons self.datetimeIco = QToolButton(self, objectName='DiaryListItemDtIco') minSz = max(font.datetime_m.ascent(), 12 * scaleRatio) self.datetimeIco.setIconSize(QSize(minSz, minSz)) self.datetimeIco.setIcon(QIcon(':/calendar.png')) self.tagIco = QToolButton(self, objectName='DiaryListItemTagIco') minSz = max(font.default_m.ascent(), 12 * scaleRatio) self.tagIco.setIconSize(QSize(minSz, minSz)) self.tagIco.setIcon(QIcon(':/tag.png')) self.vLayout0 = QVBoxLayout(self) self.hLayout0 = QHBoxLayout() self.hLayout1 = QHBoxLayout() for i in [self.vLayout0, self.hLayout0, self.hLayout1]: i.setContentsMargins(0, 0, 0, 0) i.setSpacing(0) for i in [self.datetimeIco, self.datetime, self.title]: self.hLayout0.addWidget(i) self.hLayout0.insertSpacing(2, 10) for i in [self.tagIco, self.tag]: self.hLayout1.addWidget(i) self.vLayout0.addLayout(self.hLayout0) self.vLayout0.addWidget(self.text) self.vLayout0.addLayout(self.hLayout1)
class ItemWidget(QFrame): """Widget that used to draw an item in paint method.""" def __init__(self, parent=None): super().__init__(parent, objectName='DiaryListItem') self.title = NElideLabel(self, objectName='DiaryListItemTitle') self.title.setFont(font.title) self.title.setAlignment(Qt.AlignVCenter | Qt.AlignRight) self.title.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) self.datetime = QLabel(self, objectName='DiaryListItemDt') self.datetime.setFont(font.datetime) self.datetime.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Preferred) self.text = MultiLineElideLabel(self, objectName='DiaryListItemText', forceHeightHint=True) self.text.setMaximumLineCount(settings['Main'].getint('previewLines')) self.text.setFont(font.text) self.tag = NElideLabel(self, objectName='DiaryListItemTag') # use QToolButton to display icons self.datetimeIco = QToolButton(self, objectName='DiaryListItemDtIco') minSz = max(font.datetime_m.ascent(), 12*scaleRatio) self.datetimeIco.setIconSize(QSize(minSz, minSz)) self.datetimeIco.setIcon(QIcon(':/calendar.png')) self.tagIco = QToolButton(self, objectName='DiaryListItemTagIco') minSz = max(font.default_m.ascent(), 12*scaleRatio) self.tagIco.setIconSize(QSize(minSz, minSz)) self.tagIco.setIcon(QIcon(':/tag.png')) self.vLayout0 = QVBoxLayout(self) self.hLayout0 = QHBoxLayout() self.hLayout1 = QHBoxLayout() for i in [self.vLayout0, self.hLayout0, self.hLayout1]: i.setContentsMargins(0, 0, 0, 0) i.setSpacing(0) for i in [self.datetimeIco, self.datetime, self.title]: self.hLayout0.addWidget(i) self.hLayout0.insertSpacing(2, 10) for i in [self.tagIco, self.tag]: self.hLayout1.addWidget(i) self.vLayout0.addLayout(self.hLayout0) self.vLayout0.addWidget(self.text) self.vLayout0.addLayout(self.hLayout1)
class ItemWidget(QFrame): """Widget that used to draw an item in ItemDelegate.paint method. This widget's height is 'fixed'(two possible height) because delegate's sizeHint method is called very often. So font fallback will cause problem. """ def __init__(self, parent=None): super().__init__(parent, objectName='DiaryListItem') self.heightWithTag = self.heightNoTag = None self.title = NElideLabel(self, objectName='DiaryListItemTitle') self.title.setFont(font.title) self.title.setAlignment(Qt.AlignVCenter | Qt.AlignRight) self.title.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) self.datetime = QLabel(self, objectName='DiaryListItemDt') self.datetime.setFont(font.datetime) self.datetime.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Preferred) self.text = MultiLineElideLabel(self, objectName='DiaryListItemText') self.text.setMaximumLineCount(settings['Main'].getint('previewLines')) self.text.setFont(font.text) self.tag = NElideLabel(self, objectName='DiaryListItemTag') # use QToolButton to display icons self.datetimeIco = QToolButton(self, objectName='DiaryListItemDtIco') minSz = max(font.datetime_m.ascent(), 12*scaleRatio) self.datetimeIco.setIconSize(QSize(minSz, minSz)) self.datetimeIco.setIcon(QIcon(':/calendar.png')) self.tagIco = QToolButton(self, objectName='DiaryListItemTagIco') minSz = max(font.default_m.ascent(), 12*scaleRatio) self.tagIco.setIconSize(QSize(minSz, minSz)) self.tagIco.setIcon(QIcon(':/tag.png')) self._vLayout0 = QVBoxLayout(self) self._hLayout0 = QHBoxLayout() self._hLayout1 = QHBoxLayout() for i in [self._vLayout0, self._hLayout0, self._hLayout1]: i.setContentsMargins(0, 0, 0, 0) i.setSpacing(0) for i in [self.datetimeIco, self.datetime, self.title]: self._hLayout0.addWidget(i) self._hLayout0.insertSpacing(2, 10) for i in [self.tagIco, self.tag]: self._hLayout1.addWidget(i) self._vLayout0.addLayout(self._hLayout0) self._vLayout0.addWidget(self.text) self._vLayout0.addLayout(self._hLayout1) def refreshStyle(self): """Must be called after dynamic property changed""" refreshStyle(self) # no need to call self.update here 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 refreshHeightInfo(self): self.heightWithTag = self.sizeHint().height() self.heightNoTag = self.heightWithTag - self._hLayout1.sizeHint().height()