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