1 /*
2  * %kadu copyright begin%
3  * Copyright 2011 Piotr Galiszewski (piotr.galiszewski@kadu.im)
4  * Copyright 2010 Piotr Dąbrowski (ultr@ultr.pl)
5  * Copyright 2010, 2011, 2013, 2014 Bartosz Brachaczek (b.brachaczek@gmail.com)
6  * Copyright 2009, 2010, 2011, 2012, 2013, 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
7  * %kadu copyright end%
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "buddy-contacts-table-delegate.h"
24 
25 #include "accounts/account.h"
26 #include "core/injected-factory.h"
27 #include "gui/widgets/accounts-combo-box.h"
28 #include "model/roles.h"
29 
30 #include <QtCore/QEvent>
31 #include <QtWidgets/QComboBox>
32 
BuddyContactsTableDelegate(QObject * parent)33 BuddyContactsTableDelegate::BuddyContactsTableDelegate(QObject *parent) :
34 		QStyledItemDelegate(parent)
35 {
36 }
37 
~BuddyContactsTableDelegate()38 BuddyContactsTableDelegate::~BuddyContactsTableDelegate()
39 {
40 }
41 
setInjectedFactory(InjectedFactory * injectedFactory)42 void BuddyContactsTableDelegate::setInjectedFactory(InjectedFactory *injectedFactory)
43 {
44 	m_injectedFactory = injectedFactory;
45 }
46 
createEditor(QWidget * parent,const QStyleOptionViewItem & option,const QModelIndex & index) const47 QWidget * BuddyContactsTableDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
48 {
49 	if (1 != index.column()) // not account
50 		return QStyledItemDelegate::createEditor(parent, option, index);
51 
52 	AccountsComboBox *accountsComboBox = m_injectedFactory->makeInjected<AccountsComboBox>(
53 		index.data(AccountRole).value<Account>().isNull(), AccountsComboBox::NotVisibleWithOneRowSourceModel, parent);
54 	// this connect does not work withour Account
55 	connect(accountsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(dataChanged()));
56 
57 	return accountsComboBox;
58 }
59 
updateEditorGeometry(QWidget * editor,const QStyleOptionViewItem & option,const QModelIndex & index) const60 void BuddyContactsTableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
61 {
62 	Q_UNUSED(index)
63 
64 	editor->setGeometry(option.rect); // use full rect, does not allow to display image next to combo-box
65 }
66 
setEditorData(QWidget * editor,const QModelIndex & index) const67 void BuddyContactsTableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
68 {
69 	AccountsComboBox *accountsComboBox = qobject_cast<AccountsComboBox *>(editor);
70 	if (!accountsComboBox)
71 	{
72 		QStyledItemDelegate::setEditorData(editor, index);
73 		return;
74 	}
75 
76 	accountsComboBox->setCurrentAccount(index.data(AccountRole).value<Account>());
77 }
78 
setModelData(QWidget * editor,QAbstractItemModel * model,const QModelIndex & index) const79 void BuddyContactsTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
80 {
81 	AccountsComboBox *accountsComboBox = qobject_cast<AccountsComboBox *>(editor);
82 	if (!accountsComboBox)
83 	{
84 		QStyledItemDelegate::setModelData(editor, model, index);
85 		return;
86 	}
87 
88 	model->setData(index, QVariant::fromValue<Account>(accountsComboBox->currentAccount()), AccountRole);
89 }
90 
dataChanged()91 void BuddyContactsTableDelegate::dataChanged()
92 {
93 	QWidget *editorWidget = qobject_cast<QWidget *>(sender());
94 	if (editorWidget)
95 		emit commitData(editorWidget);
96 }
97 
eventFilter(QObject * editor,QEvent * event)98 bool BuddyContactsTableDelegate::eventFilter(QObject *editor, QEvent *event)
99 {
100 	bool handled = QStyledItemDelegate::eventFilter(editor, event);
101 
102 	if (!handled && event->type() == QEvent::KeyPress)
103 	{
104 		QWidget *editorWidget = qobject_cast<QWidget *>(editor);
105 		if (editorWidget)
106 			// we need to delay it a bit, otherwise it is executed before the event goes to the widget
107 			// it's exactly how Qt does it
108 			QMetaObject::invokeMethod(this, "commitData", Qt::QueuedConnection, Q_ARG(QWidget *, editorWidget));
109 	}
110 
111 	return handled;
112 }
113 
114 #include "moc_buddy-contacts-table-delegate.cpp"
115