blob: a70a772e7abc3f10be2c916212c91f4daee14d42 [file] [log] [blame]
Yingdi Yu8dacdf22013-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"
18#include "exception.h"
19#endif
20
21INIT_LOGGER("EndorseComboBoxDelegate");
22
23EndorseComboBoxDelegate::EndorseComboBoxDelegate(QObject *parent)
24 : QItemDelegate(parent)
25{
26 m_items.push_back("Not Endorsed");
27 m_items.push_back("Endorsed");
28}
29
30
31QWidget*
32EndorseComboBoxDelegate::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
42void
43EndorseComboBoxDelegate::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 Yu8dacdf22013-11-05 23:06:43 -080047 comboBox->setCurrentIndex(value);
48}
49
50void
51EndorseComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
52{
53 QComboBox *comboBox = static_cast<QComboBox*>(editor);
Yingdi Yu8dacdf22013-11-05 23:06:43 -080054 model->setData(index, comboBox->currentIndex(), Qt::EditRole);
55}
56
57void
58EndorseComboBoxDelegate::updateEditorGeometry(QWidget *editor,
59 const QStyleOptionViewItem &option,
60 const QModelIndex &/* index */) const
61{
62 editor->setGeometry(option.rect);
63}
64
65void
66EndorseComboBoxDelegate::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