blob: 1888baf1e966a846ee4df12834659ab2185c37fa [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
Yingdi Yu0b0a7362014-08-05 16:31:30 -070011#include "endorse-combobox-delegate.hpp"
Yingdi Yub2e747d2013-11-05 23:06:43 -080012
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
Yingdi Yueb692ac2015-02-10 18:46:18 -080020namespace chronochat {
Yingdi Yub2e747d2013-11-05 23:06:43 -080021
Yingdi Yu0b0a7362014-08-05 16:31:30 -070022EndorseComboBoxDelegate::EndorseComboBoxDelegate(QObject* parent)
Yingdi Yub2e747d2013-11-05 23:06:43 -080023 : 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 Yu0b0a7362014-08-05 16:31:30 -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);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070036 for (unsigned int i = 0; i < m_items.size(); ++i)
37 editor->addItem(m_items[i].c_str());
Yingdi Yub2e747d2013-11-05 23:06:43 -080038 return editor;
39}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070040
41void
Yingdi Yu0b0a7362014-08-05 16:31:30 -070042EndorseComboBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
Yingdi Yub2e747d2013-11-05 23:06:43 -080043{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070044 QComboBox* comboBox = static_cast<QComboBox*>(editor);
Yingdi Yub2e747d2013-11-05 23:06:43 -080045 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 Yu0b0a7362014-08-05 16:31:30 -070050EndorseComboBoxDelegate::setModelData(QWidget* editor,
51 QAbstractItemModel* model,
52 const QModelIndex& index) const
Yingdi Yub2e747d2013-11-05 23:06:43 -080053{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070054 QComboBox* comboBox = static_cast<QComboBox*>(editor);
Yingdi Yub2e747d2013-11-05 23:06:43 -080055 model->setData(index, comboBox->currentIndex(), Qt::EditRole);
56}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070057
58void
Yingdi Yu0b0a7362014-08-05 16:31:30 -070059EndorseComboBoxDelegate::updateEditorGeometry(QWidget* editor,
60 const QStyleOptionViewItem& option,
61 const QModelIndex& /* index */) const
Yingdi Yub2e747d2013-11-05 23:06:43 -080062{
63 editor->setGeometry(option.rect);
64}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070065
66void
Yingdi Yu0b0a7362014-08-05 16:31:30 -070067EndorseComboBoxDelegate::paint(QPainter* painter,
68 const QStyleOptionViewItem& option,
69 const QModelIndex& index) const
Yingdi Yub2e747d2013-11-05 23:06:43 -080070{
71 QStyleOptionViewItemV4 myOption = option;
72 QString text = m_items[index.model()->data(index, Qt::EditRole).toUInt()].c_str();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070073
Yingdi Yub2e747d2013-11-05 23:06:43 -080074 myOption.text = text;
Yingdi Yufa0b6a02014-04-30 14:26:42 -070075
Yingdi Yub2e747d2013-11-05 23:06:43 -080076 QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
77}
78
Yingdi Yueb692ac2015-02-10 18:46:18 -080079} // namespace chronochat
Yingdi Yu0b0a7362014-08-05 16:31:30 -070080
Yingdi Yub2e747d2013-11-05 23:06:43 -080081#if WAF
82#include "endorse-combobox-delegate.moc"
Yingdi Yu42125862014-08-07 17:04:28 -070083// #include "endorse-combobox-delegate.cpp.moc"
Yingdi Yub2e747d2013-11-05 23:06:43 -080084#endif