Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "endorse-combobox-delegate.h" |
| 12 | |
| 13 | #include <QComboBox> |
| 14 | #include <QApplication> |
| 15 | |
| 16 | #ifndef Q_MOC_RUN |
| 17 | #include "logging.h" |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 18 | #endif |
| 19 | |
| 20 | INIT_LOGGER("EndorseComboBoxDelegate"); |
| 21 | |
| 22 | EndorseComboBoxDelegate::EndorseComboBoxDelegate(QObject *parent) |
| 23 | : QItemDelegate(parent) |
| 24 | { |
| 25 | m_items.push_back("Not Endorsed"); |
| 26 | m_items.push_back("Endorsed"); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | QWidget* |
| 31 | EndorseComboBoxDelegate::createEditor(QWidget *parent, |
| 32 | const QStyleOptionViewItem &/* option */, |
| 33 | const QModelIndex &/* index */) const |
| 34 | { |
| 35 | QComboBox* editor = new QComboBox(parent); |
| 36 | for(unsigned int i = 0; i < m_items.size(); ++i) |
| 37 | editor->addItem(m_items[i].c_str()); |
| 38 | return editor; |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | EndorseComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const |
| 43 | { |
| 44 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
| 45 | int value = index.model()->data(index, Qt::EditRole).toUInt(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 46 | comboBox->setCurrentIndex(value); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | EndorseComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const |
| 51 | { |
| 52 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 53 | model->setData(index, comboBox->currentIndex(), Qt::EditRole); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | EndorseComboBoxDelegate::updateEditorGeometry(QWidget *editor, |
| 58 | const QStyleOptionViewItem &option, |
| 59 | const QModelIndex &/* index */) const |
| 60 | { |
| 61 | editor->setGeometry(option.rect); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | EndorseComboBoxDelegate::paint(QPainter *painter, |
| 66 | const QStyleOptionViewItem &option, |
| 67 | const QModelIndex &index) const |
| 68 | { |
| 69 | QStyleOptionViewItemV4 myOption = option; |
| 70 | QString text = m_items[index.model()->data(index, Qt::EditRole).toUInt()].c_str(); |
| 71 | |
| 72 | myOption.text = text; |
| 73 | |
| 74 | QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter); |
| 75 | } |
| 76 | |
| 77 | #if WAF |
| 78 | #include "endorse-combobox-delegate.moc" |
| 79 | #include "endorse-combobox-delegate.cpp.moc" |
| 80 | #endif |