blob: d753c87c9e440a74f48ac115a06597c6503b04fd [file] [log] [blame]
Yingdi Yub2e747d2013-11-05 23:06:43 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Yingdi Yub2e747d2013-11-05 23:06:43 -08004 * 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
Yingdi Yueb692ac2015-02-10 18:46:18 -080016namespace chronochat {
Yingdi Yub2e747d2013-11-05 23:06:43 -080017
Yingdi Yu0b0a7362014-08-05 16:31:30 -070018EndorseComboBoxDelegate::EndorseComboBoxDelegate(QObject* parent)
Yingdi Yub2e747d2013-11-05 23:06:43 -080019 : QItemDelegate(parent)
20{
21 m_items.push_back("Not Endorsed");
22 m_items.push_back("Endorsed");
23}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070024
25
Yingdi Yub2e747d2013-11-05 23:06:43 -080026QWidget*
Yingdi Yu0b0a7362014-08-05 16:31:30 -070027EndorseComboBoxDelegate::createEditor(QWidget* parent,
28 const QStyleOptionViewItem& /* option */,
29 const QModelIndex& /* index */) const
Yingdi Yub2e747d2013-11-05 23:06:43 -080030{
31 QComboBox* editor = new QComboBox(parent);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070032 for (unsigned int i = 0; i < m_items.size(); ++i)
33 editor->addItem(m_items[i].c_str());
Yingdi Yub2e747d2013-11-05 23:06:43 -080034 return editor;
35}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070036
37void
Yingdi Yu0b0a7362014-08-05 16:31:30 -070038EndorseComboBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
Yingdi Yub2e747d2013-11-05 23:06:43 -080039{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070040 QComboBox* comboBox = static_cast<QComboBox*>(editor);
Yingdi Yub2e747d2013-11-05 23:06:43 -080041 int value = index.model()->data(index, Qt::EditRole).toUInt();
Yingdi Yub2e747d2013-11-05 23:06:43 -080042 comboBox->setCurrentIndex(value);
43}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070044
45void
Yingdi Yu0b0a7362014-08-05 16:31:30 -070046EndorseComboBoxDelegate::setModelData(QWidget* editor,
47 QAbstractItemModel* model,
48 const QModelIndex& index) const
Yingdi Yub2e747d2013-11-05 23:06:43 -080049{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070050 QComboBox* comboBox = static_cast<QComboBox*>(editor);
Yingdi Yub2e747d2013-11-05 23:06:43 -080051 model->setData(index, comboBox->currentIndex(), Qt::EditRole);
52}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070053
54void
Yingdi Yu0b0a7362014-08-05 16:31:30 -070055EndorseComboBoxDelegate::updateEditorGeometry(QWidget* editor,
56 const QStyleOptionViewItem& option,
57 const QModelIndex& /* index */) const
Yingdi Yub2e747d2013-11-05 23:06:43 -080058{
59 editor->setGeometry(option.rect);
60}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070061
62void
Yingdi Yu0b0a7362014-08-05 16:31:30 -070063EndorseComboBoxDelegate::paint(QPainter* painter,
64 const QStyleOptionViewItem& option,
65 const QModelIndex& index) const
Yingdi Yub2e747d2013-11-05 23:06:43 -080066{
Varun Patil3d850902020-11-23 12:19:14 +053067 QStyleOptionViewItem myOption = option;
Yingdi Yub2e747d2013-11-05 23:06:43 -080068 QString text = m_items[index.model()->data(index, Qt::EditRole).toUInt()].c_str();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070069
Yingdi Yub2e747d2013-11-05 23:06:43 -080070 myOption.text = text;
Yingdi Yufa0b6a02014-04-30 14:26:42 -070071
Yingdi Yub2e747d2013-11-05 23:06:43 -080072 QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
73}
74
Yingdi Yueb692ac2015-02-10 18:46:18 -080075} // namespace chronochat
Yingdi Yu0b0a7362014-08-05 16:31:30 -070076
Yingdi Yub2e747d2013-11-05 23:06:43 -080077#if WAF
78#include "endorse-combobox-delegate.moc"
Yingdi Yub2e747d2013-11-05 23:06:43 -080079#endif