blob: 906020a0cdafd8d0d934566ad9dcb906bd7ff3b6 [file] [log] [blame]
Yingdi Yub2e747d2013-11-05 23:06:43 -08001/* -*- 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 Yub2e747d2013-11-05 23:06:43 -080018#endif
19
20INIT_LOGGER("EndorseComboBoxDelegate");
21
22EndorseComboBoxDelegate::EndorseComboBoxDelegate(QObject *parent)
23 : QItemDelegate(parent)
24{
25 m_items.push_back("Not Endorsed");
26 m_items.push_back("Endorsed");
27}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070028
29
Yingdi Yub2e747d2013-11-05 23:06:43 -080030QWidget*
Yingdi Yufa0b6a02014-04-30 14:26:42 -070031EndorseComboBoxDelegate::createEditor(QWidget *parent,
32 const QStyleOptionViewItem &/* option */,
33 const QModelIndex &/* index */) const
Yingdi Yub2e747d2013-11-05 23:06:43 -080034{
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}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070040
41void
Yingdi Yub2e747d2013-11-05 23:06:43 -080042EndorseComboBoxDelegate::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 Yub2e747d2013-11-05 23:06:43 -080046 comboBox->setCurrentIndex(value);
47}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070048
49void
Yingdi Yub2e747d2013-11-05 23:06:43 -080050EndorseComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
51{
52 QComboBox *comboBox = static_cast<QComboBox*>(editor);
Yingdi Yub2e747d2013-11-05 23:06:43 -080053 model->setData(index, comboBox->currentIndex(), Qt::EditRole);
54}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070055
56void
57EndorseComboBoxDelegate::updateEditorGeometry(QWidget *editor,
58 const QStyleOptionViewItem &option,
59 const QModelIndex &/* index */) const
Yingdi Yub2e747d2013-11-05 23:06:43 -080060{
61 editor->setGeometry(option.rect);
62}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070063
64void
65EndorseComboBoxDelegate::paint(QPainter *painter,
66 const QStyleOptionViewItem &option,
67 const QModelIndex &index) const
Yingdi Yub2e747d2013-11-05 23:06:43 -080068{
69 QStyleOptionViewItemV4 myOption = option;
70 QString text = m_items[index.model()->data(index, Qt::EditRole).toUInt()].c_str();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070071
Yingdi Yub2e747d2013-11-05 23:06:43 -080072 myOption.text = text;
Yingdi Yufa0b6a02014-04-30 14:26:42 -070073
Yingdi Yub2e747d2013-11-05 23:06:43 -080074 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