Yingdi Yu | 8dacdf2 | 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" |
| 18 | #include "exception.h" |
| 19 | #endif |
| 20 | |
| 21 | INIT_LOGGER("EndorseComboBoxDelegate"); |
| 22 | |
| 23 | EndorseComboBoxDelegate::EndorseComboBoxDelegate(QObject *parent) |
| 24 | : QItemDelegate(parent) |
| 25 | { |
| 26 | m_items.push_back("Not Endorsed"); |
| 27 | m_items.push_back("Endorsed"); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | QWidget* |
| 32 | EndorseComboBoxDelegate::createEditor(QWidget *parent, |
| 33 | const QStyleOptionViewItem &/* option */, |
| 34 | const QModelIndex &/* index */) const |
| 35 | { |
| 36 | QComboBox* editor = new QComboBox(parent); |
| 37 | for(unsigned int i = 0; i < m_items.size(); ++i) |
| 38 | editor->addItem(m_items[i].c_str()); |
| 39 | return editor; |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | EndorseComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const |
| 44 | { |
| 45 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
| 46 | int value = index.model()->data(index, Qt::EditRole).toUInt(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 47 | comboBox->setCurrentIndex(value); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | EndorseComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const |
| 52 | { |
| 53 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 54 | model->setData(index, comboBox->currentIndex(), Qt::EditRole); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | EndorseComboBoxDelegate::updateEditorGeometry(QWidget *editor, |
| 59 | const QStyleOptionViewItem &option, |
| 60 | const QModelIndex &/* index */) const |
| 61 | { |
| 62 | editor->setGeometry(option.rect); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | EndorseComboBoxDelegate::paint(QPainter *painter, |
| 67 | const QStyleOptionViewItem &option, |
| 68 | const QModelIndex &index) const |
| 69 | { |
| 70 | QStyleOptionViewItemV4 myOption = option; |
| 71 | QString text = m_items[index.model()->data(index, Qt::EditRole).toUInt()].c_str(); |
| 72 | |
| 73 | myOption.text = text; |
| 74 | |
| 75 | QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter); |
| 76 | } |
| 77 | |
| 78 | #if WAF |
| 79 | #include "endorse-combobox-delegate.moc" |
| 80 | #include "endorse-combobox-delegate.cpp.moc" |
| 81 | #endif |