OSDN Git Service

Ver0.26
[gefu/Gefu.git] / thumbnaildelegate.cpp
1 #include "foldermodel.h"
2 #include "thumbnaildelegate.h"
3
4 #include <QApplication>
5 #include <QDebug>
6 #include <QPainter>
7 #include <QStylePainter>
8
9 ///////////////////////////////////////////////////////////////////////////////
10 /// \brief ThumbnailDelegate::ThumbnailDelegate
11 /// \param parent   親オブジェクト
12 ///
13 /// コンストラクタ
14 ///
15 ThumbnailDelegate::ThumbnailDelegate(QObject *parent) :
16     QStyledItemDelegate(parent)
17 {
18 }
19
20 ///////////////////////////////////////////////////////////////////////////////
21 /// \brief ThumbnailDelegate::paint
22 /// \param painter  ペインターオブジェクト
23 /// \param option   オプション
24 /// \param index    インデックス
25 ///
26 /// 描画を行います
27 ///
28 void ThumbnailDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
29 {
30     const FolderModel *model = static_cast<const FolderModel*>(index.model());
31     bool checked = model->data(index, Qt::CheckStateRole).toInt() == Qt::Checked;
32
33     // マーク背景
34     if (checked) {
35         painter->fillRect(option.rect, model->data(index, Qt::BackgroundRole).value<QBrush>());
36     }
37     // ハイライト背景
38     if (option.state & QStyle::State_Selected) {
39         painter->fillRect(option.rect, option.palette.highlight());
40     }
41
42     // ファイル名
43     QFont font = model->data(index, Qt::FontRole).value<QFont>();
44     QFontMetrics fm(font);
45     QSize padding(20, fm.height());
46     QRect rc(option.rect);
47     rc.setLeft(rc.left() + padding.width());
48     QString name = fm.elidedText(model->fileName(index), Qt::ElideMiddle, rc.width());
49     painter->setFont(font);
50     painter->setPen(model->data(index, Qt::ForegroundRole).value<QBrush>().color());
51     painter->drawText(rc, name, Qt::AlignCenter | Qt::AlignBottom);
52
53     // チェックボックス
54     if (model->flags(index) & Qt::ItemIsUserCheckable) {
55         QStyleOptionButton optBtn;
56         optBtn.rect = option.rect;
57         optBtn.state = option.state;
58         if (checked)
59             optBtn.state |= QStyle::State_On;
60         qApp->style()->drawControl(QStyle::CE_CheckBox, &optBtn, painter);
61     }
62
63     // アイコンまたは画像
64     QSize size(option.rect.size() - padding);
65     QPixmap pixmap = model->pixmap(index, size);
66     painter->drawPixmap(padding.width() + option.rect.left() + (size.width() - pixmap.width()) / 2,
67                         option.rect.top() + (size.height() - pixmap.height()) / 2,
68                         pixmap);
69 }
70
71 ///////////////////////////////////////////////////////////////////////////////
72 /// \brief ThumbnailDelegate::sizeHint
73 /// \param option   オプション
74 /// \param index    インデックス
75 /// \return サイズを返します。
76 ///
77 QSize ThumbnailDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
78 {
79     Q_UNUSED(option);
80
81     const FolderModel *model = static_cast<const FolderModel*>(index.model());
82     int pointSize = model->data(index, Qt::FontRole).value<QFont>().pointSize();
83
84     return QSize(pointSize * 10, pointSize * 10 * 3 / 4) * m_scaleFactor;
85 }