1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| class PhotoDelegate : public QStyledItemDelegate { Q_OBJECT
public: enum ViewMode { ListMode, GridMode }; explicit PhotoDelegate(QListView* parent = nullptr) : QStyledItemDelegate(parent) { listView = parent; }; void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override { painter->save(); painter->setRenderHint(QPainter::Antialiasing); QListView::ViewMode viewMode = listView->viewMode(); QPixmap pixmap(index.data(Qt::UserRole).toString()); QString fileName = index.data(Qt::UserRole + 1).toString(); QString fileReso = index.data(Qt::UserRole + 2).toString(); QString fileDate = index.data(Qt::UserRole + 3).toString(); QString fileSize = index.data(Qt::UserRole + 4).toString();
QRect iconRect = QRect(option.rect.left() + 5, option.rect.top() + 5, 50, 50); QRect nameRect = QRect(iconRect.right() - 50, option.rect.top() + 100, option.rect.width(), 20); QRect dateRect = QRect(iconRect.right() - 50, option.rect.top() + 120, option.rect.width(), 20); QRect sizeRect = QRect(iconRect.right() - 50, option.rect.top() + 140, option.rect.width(), 20);
bool isSelected = option.state & QStyle::State_Selected; bool isHovered = option.state & QStyle::State_MouseOver;
if (viewMode == QListView::ViewMode::IconMode) { QString filePath = index.data(Qt::UserRole).value<QString>(); QRect imageRect = option.rect;
int size = qMin(imageRect.width(), imageRect.height()); QRect squareRect(imageRect.topLeft(), QSize(size, size)); squareRect.moveCenter(imageRect.center());
painter->setRenderHint(QPainter::Antialiasing);
QPainterPath clipPath; clipPath.addRoundedRect(squareRect, 10, 10); painter->setClipPath(clipPath); painter->drawPixmap(squareRect, pixmap, pixmap.rect()); if (!isSelected) { QPixmap checkIcon(":/images/unselect.png"); QRect checkRect(squareRect.topRight() - QPoint(SELECT_ICON_SIZE + 5, -5), QSize(SELECT_ICON_SIZE, SELECT_ICON_SIZE)); painter->drawPixmap(checkRect, checkIcon); }
if (isSelected) { painter->setPen(QPen(QColor(41, 95, 204), 3)); painter->drawRoundedRect(squareRect, 10, 10);
QPixmap checkIcon(":/images/selected.png"); QRect checkRect(squareRect.topRight() - QPoint(SELECT_ICON_SIZE + 5, -5), QSize(SELECT_ICON_SIZE, SELECT_ICON_SIZE)); painter->drawPixmap(checkRect, checkIcon); } if (isHovered) { painter->setPen(QPen(QColor(41, 95, 204), 3)); painter->drawRoundedRect(squareRect, 10, 10);
painter->setPen(Qt::black); painter->drawText(nameRect, Qt::AlignLeft | Qt::AlignVCenter, fileName); painter->drawText(dateRect, Qt::AlignLeft | Qt::AlignVCenter, fileDate); painter->drawText(sizeRect, Qt::AlignLeft | Qt::AlignVCenter, fileSize); } } else if (viewMode == QListView::ViewMode::ListMode) { if (isHovered) { QRect fileRect(option.rect.topLeft(), option.rect.size()); int borderRadius = 10;
painter->setPen(Qt::NoPen); painter->setBrush(QColor(235, 241, 255));
painter->drawRoundedRect(fileRect, borderRadius, borderRadius); }
if (isSelected) { QRect fileRect(option.rect.topLeft(), option.rect.size()); int borderRadius = 10; painter->setPen(Qt::NoPen); painter->setBrush(QColor(235, 241, 255));
painter->drawRoundedRect(fileRect, borderRadius, borderRadius);
QRect checkIconRect = QRect(option.rect.left() + 15, option.rect.top() + 20, SELECT_ICON_SIZE, SELECT_ICON_SIZE); QPixmap checkIcon(":/images/selected.png"); painter->drawPixmap(checkIconRect, checkIcon); } else { QRect checkIconRect = QRect(option.rect.left() + 15, option.rect.top() + 20, SELECT_ICON_SIZE, SELECT_ICON_SIZE); QPixmap checkIcon(":/images/unselect.png"); painter->drawPixmap(checkIconRect, checkIcon); }
QRect iconRect = QRect(option.rect.left() + 45, option.rect.top() + 7, 44, 44); QRect nameRect = QRect(iconRect.right() + 40, option.rect.top() + 20, 200, 20); QRect resoRect = QRect(nameRect.right() + 40, option.rect.top() + 20, 100, 20); QRect dateRect = QRect(resoRect.right() + 40, option.rect.top() + 20, 200, 20); QRect sizeRect = QRect(dateRect.right() + 40, option.rect.top() + 20, 100, 20);
int borderRadius = 10; QPainterPath path; path.addRoundedRect(iconRect, borderRadius, borderRadius);
painter->save(); painter->setRenderHint(QPainter::Antialiasing, true); painter->setClipPath(path); painter->drawPixmap(iconRect, pixmap); painter->restore();
painter->setPen(Qt::black); painter->drawText(nameRect, Qt::AlignLeft | Qt::AlignVCenter, fileName); painter->setPen(Qt::gray); painter->drawText(resoRect, Qt::AlignLeft | Qt::AlignVCenter, fileReso); painter->drawText(dateRect, Qt::AlignLeft | Qt::AlignVCenter, fileDate);
painter->drawText(sizeRect, Qt::AlignLeft | Qt::AlignVCenter, fileSize);
}
painter->restore(); }
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override { Q_UNUSED(index); if (listView->viewMode() == QListView::ViewMode::IconMode) { return QSize(170, 170); } else { return QSize(50, 60); }
} bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) override { if (event->type() == QEvent::MouseButtonPress) { QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); QListView::ViewMode viewMode = listView->viewMode();
QRect checkRect; if (viewMode == QListView::ViewMode::IconMode) { checkRect = QRect(option.rect.topRight() - QPoint(SELECT_ICON_SIZE + 5, -5), QSize(SELECT_ICON_SIZE, SELECT_ICON_SIZE)); } else { checkRect = QRect(option.rect.left() + 15, option.rect.top() + 20, SELECT_ICON_SIZE, SELECT_ICON_SIZE); }
if (checkRect.contains(mouseEvent->pos())) { bool isSelected = index.data(Qt::UserRole + 15).toBool(); model->setData(index, !isSelected, Qt::UserRole + 15); QItemSelectionModel* selectionModel = listView->selectionModel(); if (!isSelected) { selectionModel->select(index, QItemSelectionModel::Select); } else { selectionModel->select(index, QItemSelectionModel::Deselect); }
return true; } } return QStyledItemDelegate::editorEvent(event, model, option, index); } private: QListView* listView; };
|