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