void MailTreeDelegate::updateEditorGeometry (QWidget *editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { auto height = GetSubjectFont (index, option).second.height (); qobject_cast<QToolBar*> (editor)->setIconSize ({ height, height }); editor->setMaximumSize (option.rect.size ()); editor->move (option.rect.topRight () - QPoint { editor->width (), 0 }); }
void MailTreeDelegate::updateEditorGeometry (QWidget *editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { const auto& subjFM = GetSubjectFont (index, option).second; auto height = subjFM.boundingRect (GetString (index, MailModel::Column::Subject)).height (); qobject_cast<QToolBar*> (editor)->setIconSize ({ height, height }); editor->setMaximumSize (option.rect.size ()); editor->move (option.rect.topRight () - QPoint { editor->width (), 0 }); }
QSize MailTreeDelegate::sizeHint (const QStyleOptionViewItem& option, const QModelIndex& index) const { const auto& subjFontInfo = GetSubjectFont (index, option); const QFontMetrics plainFM { option.font }; const auto width = View_->viewport ()->width (); const auto height = 2 * Padding + subjFontInfo.second.boundingRect (GetString (index, MailModel::Column::Subject)).height () + plainFM.boundingRect (GetString (index, MailModel::Column::From)).height (); return { width, height }; }
void MailTreeDelegate::paint (QPainter *painter, const QStyleOptionViewItem& stockItem, const QModelIndex& index) const { const bool isEnabled = index.flags () & Qt::ItemIsEnabled; QStyleOptionViewItemV4 option { stockItem }; if (!isEnabled) option.font.setStrikeOut (true); const auto style = GetStyle (option); painter->save (); style->drawPrimitive (QStyle::PE_PanelItemViewItem, &option, painter, option.widget); if (option.state & QStyle::State_Selected) painter->setPen (option.palette.color (QPalette::HighlightedText)); if (Mode_ == MailListMode::MultiSelect) DrawCheckbox (painter, style, option, index); const auto& subject = GetString (index, MailModel::Column::Subject); const auto& subjFontInfo = GetSubjectFont (index, option); const auto subjHeight = subjFontInfo.second.boundingRect (subject).height (); auto y = option.rect.top () + subjHeight; const auto actionsWidth = GetActionsBarWidth (index, option, subjHeight); painter->setFont (subjFontInfo.first); painter->drawText (option.rect.left (), y, subjFontInfo.second.elidedText (subject, Qt::ElideRight, option.rect.width () - actionsWidth)); const QFontMetrics fontFM { option.font }; auto stringHeight = [&fontFM] (const QString& str) { return fontFM.boundingRect (str).height (); }; auto from = GetString (index, MailModel::Column::From); if (const auto childrenCount = index.data (MailModel::MailRole::TotalChildrenCount).toInt ()) { from += " ("; if (const auto unread = index.data (MailModel::MailRole::UnreadChildrenCount).toInt ()) from += QString::number (unread) + "/"; from += QString::number (childrenCount) + ")"; } const auto& date = GetString (index, MailModel::Column::Date); y += std::max ({ stringHeight (from), stringHeight (date) }); painter->setFont (option.font); const auto dateWidth = fontFM.boundingRect (date).width (); painter->drawText (option.rect.right () - dateWidth, y, date); painter->drawText (option.rect.left (), y, fontFM.elidedText (from, Qt::ElideRight, option.rect.width () - dateWidth - 5 * Padding)); painter->restore (); }