|
| 1 | +#include "noteitemdelegate.h" |
| 2 | +#include <QPainter> |
| 3 | +#include <QDebug> |
| 4 | +#include <QApplication> |
| 5 | +#include <QMouseEvent> |
| 6 | +#include <QAbstractItemModel> |
| 7 | +#include "../items/notelistitem.h" |
| 8 | + |
| 9 | +NoteItemDelegate::NoteItemDelegate(QListView *view, QSortFilterProxyModel *proxyModel) : |
| 10 | + m_view(view), |
| 11 | + m_proxyModel(proxyModel) |
| 12 | +{ |
| 13 | + |
| 14 | +} |
| 15 | + |
| 16 | +QRect NoteItemDelegate::getStarRect(const QStyleOptionViewItem &option) const |
| 17 | +{ |
| 18 | + return QRect(option.rect.x()+option.rect.width()-35, |
| 19 | + option.rect.y()+option.rect.height()/2-12, |
| 20 | + 25,25); |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +bool NoteItemDelegate::editorEvent(QEvent *event, |
| 25 | + QAbstractItemModel *model, |
| 26 | + const QStyleOptionViewItem &option, |
| 27 | + const QModelIndex &index) |
| 28 | +{ |
| 29 | + if (event->type() == QEvent::MouseButtonRelease) |
| 30 | + { |
| 31 | + QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); |
| 32 | + if(mouseEvent->button() == Qt::LeftButton) |
| 33 | + { |
| 34 | + QRect checkboxRect = getStarRect(option); |
| 35 | + QPoint mousePoint = mouseEvent->pos(); |
| 36 | + if ( checkboxRect.contains(mousePoint) ) { |
| 37 | + QModelIndex realIndex = m_proxyModel->mapToSource(index); |
| 38 | + NoteListItem *item = static_cast<NoteListItem*>( realIndex.internalPointer() ); |
| 39 | + item->note()->setFavorited( !item->note()->favorited() ); |
| 40 | + return true; |
| 41 | + } |
| 42 | + else |
| 43 | + m_view->setCurrentIndex(index); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return QStyledItemDelegate::editorEvent(event, model, option, index); |
| 48 | +} |
| 49 | + |
| 50 | +void NoteItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 51 | +{ |
| 52 | + if (index.column() == 0 ) { |
| 53 | + QModelIndex realIndex = m_proxyModel->mapToSource(index); |
| 54 | + NoteListItem *item = static_cast<NoteListItem*>( realIndex.internalPointer() ); |
| 55 | + |
| 56 | + // Set the background color |
| 57 | + QBrush background = |
| 58 | + (option.state & QStyle::State_Selected) ? |
| 59 | + option.palette.highlight() : |
| 60 | + option.palette.base(); |
| 61 | + painter->fillRect(option.rect, background); |
| 62 | + |
| 63 | + if (option.state & QStyle::State_Selected) |
| 64 | + painter->setPen(option.palette.highlightedText().color()); |
| 65 | + else |
| 66 | + painter->setPen(option.palette.text().color()); |
| 67 | + |
| 68 | + QRect titleRect = option.rect; |
| 69 | + titleRect.setX(titleRect.x()+5); |
| 70 | + titleRect.setWidth(titleRect.x()-5); |
| 71 | + |
| 72 | + QFont font=painter->font() ; |
| 73 | + font.setPointSize(10); |
| 74 | + |
| 75 | + // Title |
| 76 | + font.setWeight(QFont::Bold); |
| 77 | + painter->setFont(font); |
| 78 | + painter->drawText(QPoint(option.rect.x()+10, option.rect.y()+23), item->note()->title()); |
| 79 | + |
| 80 | + // Date |
| 81 | + font.setWeight(QFont::Normal); |
| 82 | + painter->setFont(font); |
| 83 | + painter->drawText(QPoint(option.rect.x()+10, option.rect.y()+43), item->note()->date_created_str()); |
| 84 | + |
| 85 | + |
| 86 | + // The Excerpt |
| 87 | + QRect excerptRect = option.rect; |
| 88 | + excerptRect.setX( excerptRect.x()+10 ); |
| 89 | + excerptRect.setY( excerptRect.y()+50 ); |
| 90 | + excerptRect.setWidth( excerptRect.width()-50 ); |
| 91 | + |
| 92 | + QString excerpt = item->note()->text(); |
| 93 | + if (excerpt.length() > 50) { |
| 94 | + excerpt = excerpt.mid(0, 50) + "..."; |
| 95 | + } |
| 96 | + excerpt.replace("\n", " "); |
| 97 | + painter->drawText(excerptRect, excerpt); |
| 98 | + |
| 99 | + painter->fillRect(QRect( |
| 100 | + option.rect.x() + option.rect.width() - 60, |
| 101 | + option.rect.y(), |
| 102 | + 60, |
| 103 | + option.rect.height()), background); |
| 104 | + |
| 105 | + QIcon favoriteIcon; |
| 106 | + if (item->note()->favorited()) |
| 107 | + favoriteIcon = QIcon::fromTheme("vibrato-draw-star-solid"); |
| 108 | + else |
| 109 | + favoriteIcon = QIcon::fromTheme("vibrato-draw-star"); |
| 110 | + |
| 111 | + favoriteIcon.paint(painter, getStarRect(option)); |
| 112 | + |
| 113 | + } else { |
| 114 | + QStyledItemDelegate::paint(painter, option, index); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +QSize NoteItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 119 | +{ |
| 120 | + return QSize(200,100); |
| 121 | +} |
0 commit comments