1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "gui/config_dialog/combobox_delegate.h"
31
32 #include <QtGui/QtGui>
33 #include <QtWidgets/QComboBox>
34
35 namespace mozc {
36 namespace gui {
37
ComboBoxDelegate(QObject * parent)38 ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
39 : QItemDelegate(parent) {}
40
~ComboBoxDelegate()41 ComboBoxDelegate::~ComboBoxDelegate() {}
42
createEditor(QWidget * parent,const QStyleOptionViewItem & option,const QModelIndex & index) const43 QWidget *ComboBoxDelegate::createEditor(
44 QWidget *parent,
45 const QStyleOptionViewItem &option,
46 const QModelIndex &index) const {
47 QComboBox *editor = new QComboBox(parent);
48 connect(editor, SIGNAL(currentIndexChanged(const QString &)),
49 this, SLOT(CommitAndCloseEditor(const QString &)));
50 for (int i = 0; i < item_list_.size(); ++i) {
51 editor->addItem(item_list_.at(i));
52 }
53 return editor;
54 }
55
SetItemList(const QStringList & item_list)56 void ComboBoxDelegate::SetItemList(const QStringList &item_list) {
57 item_list_ = item_list;
58 }
59
setEditorData(QWidget * editor,const QModelIndex & index) const60 void ComboBoxDelegate::setEditorData(
61 QWidget *editor,
62 const QModelIndex &index) const {
63 QString str = index.model()->data(index, Qt::EditRole).toString();
64 QComboBox *comboBox = static_cast<QComboBox*>(editor);
65 if (comboBox == NULL) {
66 return;
67 }
68 comboBox->setCurrentIndex(comboBox->findText(str));
69 }
70
setModelData(QWidget * editor,QAbstractItemModel * model,const QModelIndex & index) const71 void ComboBoxDelegate::setModelData(
72 QWidget *editor, QAbstractItemModel *model,
73 const QModelIndex &index) const {
74 QComboBox *comboBox = static_cast<QComboBox*>(editor);
75 if (comboBox == NULL || model == NULL) {
76 return;
77 }
78 model->setData(index, comboBox->currentText(), Qt::EditRole);
79 }
80
updateEditorGeometry(QWidget * editor,const QStyleOptionViewItem & option,const QModelIndex & index) const81 void ComboBoxDelegate::updateEditorGeometry(
82 QWidget *editor,
83 const QStyleOptionViewItem &option,
84 const QModelIndex &index) const {
85 if (editor == NULL) {
86 return;
87 }
88 editor->setGeometry(option.rect);
89 }
90
CommitAndCloseEditor(const QString &)91 void ComboBoxDelegate::CommitAndCloseEditor(const QString &) {
92 QComboBox *editor = qobject_cast<QComboBox *>(sender());
93 emit commitData(editor);
94 emit closeEditor(editor);
95 }
96
97 } // namespace gui
98 } // namespace mozc
99