blob: e909e3d0123004431cd1f5836ed6522560183740 [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"
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();
47 _LOG_DEBUG("value: " << value);
48 comboBox->setCurrentIndex(value);
49}
50
51void
52EndorseComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
53{
54 QComboBox *comboBox = static_cast<QComboBox*>(editor);
55 _LOG_DEBUG("currentIndex: " << comboBox->currentIndex());
56 model->setData(index, comboBox->currentIndex(), Qt::EditRole);
57}
58
59void
60EndorseComboBoxDelegate::updateEditorGeometry(QWidget *editor,
61 const QStyleOptionViewItem &option,
62 const QModelIndex &/* index */) const
63{
64 editor->setGeometry(option.rect);
65}
66
67void
68EndorseComboBoxDelegate::paint(QPainter *painter,
69 const QStyleOptionViewItem &option,
70 const QModelIndex &index) const
71{
72 QStyleOptionViewItemV4 myOption = option;
73 QString text = m_items[index.model()->data(index, Qt::EditRole).toUInt()].c_str();
74
75 myOption.text = text;
76
77 QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
78}
79
80#if WAF
81#include "endorse-combobox-delegate.moc"
82#include "endorse-combobox-delegate.cpp.moc"
83#endif