1 | #include "CheckBoxDelegate.h"
|
---|
2 |
|
---|
3 | #include <QtGui/QPainter>
|
---|
4 | #include <QtGui/QApplication>
|
---|
5 |
|
---|
6 | void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
---|
7 | {
|
---|
8 | //--- QColumnViewDelegate
|
---|
9 | const bool reverse = (option.direction == Qt::RightToLeft);
|
---|
10 | const int width = (option.rect.height() * 2) / 3;
|
---|
11 |
|
---|
12 |
|
---|
13 | // Modify the options to give us room to add an arrow
|
---|
14 | QStyleOptionViewItemV4 opt = option;
|
---|
15 | if (reverse)
|
---|
16 | opt.rect.adjust(width,0,0,0);
|
---|
17 | else
|
---|
18 | opt.rect.adjust(0,0,-width,0);
|
---|
19 |
|
---|
20 | if (!(index.model()->flags(index) & Qt::ItemIsEnabled))
|
---|
21 | {
|
---|
22 | opt.showDecorationSelected = true;
|
---|
23 | opt.state |= QStyle::State_Selected;
|
---|
24 | }
|
---|
25 |
|
---|
26 |
|
---|
27 | QStyledItemDelegate::paint(painter, opt, index);
|
---|
28 |
|
---|
29 |
|
---|
30 | if (reverse)
|
---|
31 | opt.rect = QRect(option.rect.x(), option.rect.y(),
|
---|
32 | width, option.rect.height());
|
---|
33 | else
|
---|
34 | opt.rect = QRect(option.rect.x() + option.rect.width() - width, option.rect.y(),
|
---|
35 | width, option.rect.height());
|
---|
36 |
|
---|
37 | // Draw >
|
---|
38 | if (index.model()->hasChildren(index))
|
---|
39 | {
|
---|
40 | const QWidget *view = opt.widget;
|
---|
41 |
|
---|
42 | QStyle *style = view ? view->style() : qApp->style();
|
---|
43 | style->drawPrimitive(QStyle::PE_IndicatorColumnViewArrow, &opt,
|
---|
44 | painter, view);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | bool CheckBoxDelegate::editorEvent(QEvent *evt, QAbstractItemModel *model, const QStyleOptionViewItem &option,
|
---|
50 | const QModelIndex &index)
|
---|
51 | {
|
---|
52 | QStandardItemModel *it = dynamic_cast<QStandardItemModel*>(model);
|
---|
53 | if (!it)
|
---|
54 | return QStyledItemDelegate::editorEvent(evt, model, option, index);
|
---|
55 |
|
---|
56 | const QStandardItem *item = it->itemFromIndex(index);
|
---|
57 | if (!item)
|
---|
58 | return QStyledItemDelegate::editorEvent(evt, model, option, index);
|
---|
59 |
|
---|
60 | const Qt::CheckState before = item->checkState();
|
---|
61 |
|
---|
62 | const bool rc = QStyledItemDelegate::editorEvent(evt, model, option, index);
|
---|
63 |
|
---|
64 | const Qt::CheckState after = item->checkState();
|
---|
65 |
|
---|
66 | if (before!=after)
|
---|
67 | QApplication::sendEvent(it->parent(), new CheckBoxEvent(*item));
|
---|
68 |
|
---|
69 | return rc;
|
---|
70 | }
|
---|