2 * \brief Implementation of TocUserInfoWidget class
4 * Tieto Open Communicator - Client for the Telepathy communications framework.
5 * Copyright (c) 2010, Tieto Corporation
8 * Redistribution and use in source and binary forms, with or without modification,
9 * are permitted provided that the following conditions are met:
11 * Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * Neither the name of the Tieto Corporation nor the names of its contributors
17 * may be used to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <QFontMetrics>
34 #include <TocContactListWidget>
35 #include <QMessageBox>
37 #include <QListWidgetItem>
39 #include "TocUserInfoWidget"
40 #include "IconProvider"
41 #include "ui_TocUserInfoWidget.h"
45 Q_DECLARE_METATYPE(TocContact*);
48 TocUserInfoWidget::TocUserInfoWidget(QListWidgetItem * contactItem, TocContactListWidget * parent, Qt::WindowFlags flags)
49 : QWidget(parent, flags), _pContactList(parent)
52 ui = new Ui_TocUserInfoWidgetClass();
54 setContentsMargins(-10, -10, -10, -10 );
56 ui->resolveButton->hide(); //TODO: implement resolving contact's name functionality
58 //Adjust labels size to their content
59 QFontMetrics fm(ui->uidLabel->font());
63 list << fm.boundingRect(ui->uidLabel->text()).width();
64 list << fm.boundingRect(ui->nameLabel->text()).width();
65 list << fm.boundingRect(ui->genderLabel->text()).width();
66 qSort(list.begin(), list.end());
69 ui->uidLabel->setMinimumWidth(list.last());
70 ui->nameLabel->setMinimumWidth(list.last());
71 ui->genderLabel->setMinimumWidth(list.last());
73 _newCustomPicturePath = "";
75 _pContactItem = contactItem;
77 ui->genderComboBox->addItem(tr("Unknown") );
78 ui->genderComboBox->setItemData(0, NotSpecified);
80 ui->genderComboBox->addItem(tr("Male") );
81 ui->genderComboBox->setItemData(1, Male);
83 ui->genderComboBox->addItem(tr("Female"));
84 ui->genderComboBox->setItemData(2, Female);
86 CONNECT(ui->genderComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(onCurrentIndexChanged(int)));
87 CONNECT(ui->okButton, SIGNAL(clicked()),this,SLOT(okClicked()));
88 CONNECT(ui->cancelButton,SIGNAL(clicked()),this,SIGNAL(finished()));
90 _pIconProvider = IconProvider::getInstance();
93 ui->pictureButton->setIcon(_pIconProvider->matchIcon(NotSpecified));
97 ui->nameLineEdit->setText(contactItem->data(ContactRole).value<TocContact*>()->name);
98 ui->uidLineEdit->setText(contactItem->data(ContactRole).value<TocContact*>()->uid);
101 Gender gender = contactItem->data(ContactRole).value<TocContact*>()->gender;
103 int idx = ui->genderComboBox->findData(gender);
105 if (idx >= 0 && idx <= 2) {
106 ui->genderComboBox->setCurrentIndex(idx);
110 _customPicturePath = _newCustomPicturePath = contactItem->data(ContactRole).value<TocContact*>()->customIconPath;
114 if(_customPicturePath.isEmpty())
115 icon = _pIconProvider->matchIcon(gender);
117 icon = QIcon(_customPicturePath);
119 ui->pictureButton->setIcon(icon);
122 TocUserInfoWidget::~TocUserInfoWidget() {
126 void TocUserInfoWidget::onCurrentIndexChanged (int index) {
128 Gender gender = static_cast<Gender>(ui->genderComboBox->itemData(index).value<int>());
132 if(_newCustomPicturePath.isEmpty())
133 icon = _pIconProvider->matchIcon(gender);
135 icon = QIcon(_customPicturePath);
137 ui->pictureButton->setIcon(icon);
140 void TocUserInfoWidget::okClicked() {
142 if(_pContactItem) { // Edit an existing item
145 QString uid = ui->uidLineEdit->text();
148 QMessageBox::warning(this, tr("TEC"), tr("User ID is empty.\nCannot commit changes."));
152 // Check if UID is not already on contact list
153 if(_pContactList->findItem(uid, _pContactItem)) {
154 QMessageBox::warning(this, tr("TEC"), tr("The given user ID already exists on Your contact list.\nCannot commit changes."));
158 QString name = ui->nameLineEdit->text();
159 int idx = ui->genderComboBox->currentIndex();
161 // Check if something has changed
162 bool changed = false;
163 if(_pContactItem->data(ContactRole).value<TocContact*>()->uid != uid)
165 else if(_pContactItem->text() != name)
167 else if(_currentIndex != idx)
169 else if(_customPicturePath != _newCustomPicturePath)
172 // If yes: save changes and emit signal to notify TocContactListWidget (commitEdit)
174 gender = static_cast<Gender>(ui->genderComboBox->itemData(idx).value<int>());
176 //Get contact from QListWidgetItem
177 TocContact *pContact = _pContactItem->data(ContactRole).value<TocContact*>();
180 _pContactItem->setText(uid);
182 _pContactItem->setText(name);
185 pContact->name = name;
186 pContact->gender = gender;
188 _pContactItem->setData(Qt::DecorationRole, *(_pIconProvider->matchIcon(pContact)));
189 emit contactChanged(_pContactItem);
191 } else { // Create new item
193 // Collect data for the new contact
195 QString uid = ui->uidLineEdit->text();
198 QMessageBox::warning(this, tr("TEC"), tr("User ID is empty.\nCannot commit changes."));
202 // Check if UID is not already on contact list
203 if(_pContactList->findItem(uid)) {
204 QMessageBox::warning(this, tr("TEC"), tr("The given user ID already exists on Your contact list.\nCannot commit changes."));
209 QString name = ui->nameLineEdit->text();
212 int idx = ui->genderComboBox->currentIndex();
213 Gender gender = static_cast<Gender>(ui->genderComboBox->itemData(idx).value<int>());
215 // Create QListWidgetItem and emit it to notify TocContactListWidget (commitAdd)
216 QString displayedText;
220 displayedText = name;
222 QListWidgetItem* pNewItem = new QListWidgetItem(displayedText, 0);
224 // Create new contact structure...
225 TocContact* pNewContact = new TocContact;
227 pNewContact->uid = uid;
228 pNewContact->name = name;
229 pNewContact->gender = gender;
230 pNewContact->customIconId = 0;
231 pNewContact->customIconPath = "";
232 pNewContact->presence = Offline;
234 // ... and associate it with QListWidgetItem
235 pNewItem->setData(ContactRole, QVariant::fromValue(pNewContact));
237 pNewItem->setData(Qt::DecorationRole, *(_pIconProvider->matchIcon(pNewContact)));
238 pNewItem->setFlags(Qt::ItemIsEnabled);
240 emit contactChanged(pNewItem);
243 // Emit this to finish the job
247 void TocUserInfoWidget::showEvent(QShowEvent* event) {
248 ui->uidLineEdit->setFocus();