Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 3 | * Copyright (c) 2020, Regents of the University of California |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 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 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 16 | namespace chronochat { |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 17 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 18 | EndorseComboBoxDelegate::EndorseComboBoxDelegate(QObject* parent) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 19 | : QItemDelegate(parent) |
| 20 | { |
| 21 | m_items.push_back("Not Endorsed"); |
| 22 | m_items.push_back("Endorsed"); |
| 23 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 24 | |
| 25 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 26 | QWidget* |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 27 | EndorseComboBoxDelegate::createEditor(QWidget* parent, |
| 28 | const QStyleOptionViewItem& /* option */, |
| 29 | const QModelIndex& /* index */) const |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 30 | { |
| 31 | QComboBox* editor = new QComboBox(parent); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 32 | for (unsigned int i = 0; i < m_items.size(); ++i) |
| 33 | editor->addItem(m_items[i].c_str()); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 34 | return editor; |
| 35 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 36 | |
| 37 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 38 | EndorseComboBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 39 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 40 | QComboBox* comboBox = static_cast<QComboBox*>(editor); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 41 | int value = index.model()->data(index, Qt::EditRole).toUInt(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 42 | comboBox->setCurrentIndex(value); |
| 43 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 44 | |
| 45 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 46 | EndorseComboBoxDelegate::setModelData(QWidget* editor, |
| 47 | QAbstractItemModel* model, |
| 48 | const QModelIndex& index) const |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 49 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 50 | QComboBox* comboBox = static_cast<QComboBox*>(editor); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 51 | model->setData(index, comboBox->currentIndex(), Qt::EditRole); |
| 52 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 53 | |
| 54 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 55 | EndorseComboBoxDelegate::updateEditorGeometry(QWidget* editor, |
| 56 | const QStyleOptionViewItem& option, |
| 57 | const QModelIndex& /* index */) const |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 58 | { |
| 59 | editor->setGeometry(option.rect); |
| 60 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 61 | |
| 62 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 63 | EndorseComboBoxDelegate::paint(QPainter* painter, |
| 64 | const QStyleOptionViewItem& option, |
| 65 | const QModelIndex& index) const |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 66 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 67 | QStyleOptionViewItem myOption = option; |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 68 | 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] | 69 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 70 | myOption.text = text; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 71 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 72 | QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter); |
| 73 | } |
| 74 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 75 | } // namespace chronochat |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 76 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 77 | #if WAF |
| 78 | #include "endorse-combobox-delegate.moc" |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 79 | #endif |