| 10 | ProjectItemDelegate::ProjectItemDelegate(QWidget* parent) : QStyledItemDelegate(parent) {} |
| 11 | |
| 12 | void ProjectItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const |
| 13 | { |
| 14 | painter->save(); |
| 15 | |
| 16 | QStyleOptionViewItem opt(option); |
| 17 | initStyleOption(&opt, index); |
| 18 | |
| 19 | auto rect = opt.rect; |
| 20 | |
| 21 | if (opt.state & QStyle::State_Selected) { |
| 22 | painter->fillRect(rect, opt.palette.highlight()); |
| 23 | painter->setPen(opt.palette.highlightedText().color()); |
| 24 | } else if (opt.state & QStyle::State_MouseOver) { |
| 25 | painter->fillRect(rect, opt.palette.window()); |
| 26 | } |
| 27 | |
| 28 | // The default icon size will be a square (and height is usually the lower value). |
| 29 | auto icon_width = rect.height(), icon_height = rect.height(); |
| 30 | int icon_x_margin = (rect.height() - icon_width) / 2; |
| 31 | int icon_y_margin = (rect.height() - icon_height) / 2; |
| 32 | |
| 33 | if (!opt.icon.isNull()) { // Icon painting |
| 34 | { |
| 35 | auto icon_size = opt.decorationSize; |
| 36 | icon_width = icon_size.width(); |
| 37 | icon_height = icon_size.height(); |
| 38 | |
| 39 | float desired_dim = rect.height() - 10; |
| 40 | |
| 41 | auto scaleRatio = icon_width > icon_height ? desired_dim / icon_width : desired_dim / icon_height; |
| 42 | |
| 43 | icon_width *= scaleRatio; |
| 44 | icon_height *= scaleRatio; |
| 45 | |
| 46 | icon_x_margin = (rect.height() - icon_width) / 2; |
| 47 | icon_y_margin = (rect.height() - icon_height) / 2; |
| 48 | } |
| 49 | |
| 50 | // Centralize icon with a margin to separate from the other elements |
| 51 | int x = rect.x() + icon_x_margin; |
| 52 | int y = rect.y() + icon_y_margin; |
| 53 | |
| 54 | // Prevent 'scaling null pixmap' warnings |
| 55 | if (icon_width > 0 && icon_height > 0) |
| 56 | opt.icon.paint(painter, x, y, icon_width, icon_height); |
| 57 | } |
| 58 | |
| 59 | // Change the rect so that further painting is easier |
| 60 | rect.setTopLeft(QPoint(rect.x() + icon_width + 2 * icon_x_margin, rect.y() + 4)); |
| 61 | |
| 62 | { // Title painting |
| 63 | auto title = index.data(UserDataTypes::TITLE).toString(); |
| 64 | |
| 65 | painter->save(); |
| 66 | |
| 67 | auto font = opt.font; |
| 68 | if (index.data(UserDataTypes::SELECTED).toBool()) { |
| 69 | // Set nice font |
no test coverage detected