contacteditor.cpp Example File
samplephonebook/contacteditor.cpp
#include "contacteditor.h"
#include <QtGui>
#include <QDesktopServices>
const int MAX_THUMBNAIL_DISPLAY_SIZE = 120;
ContactEditor::ContactEditor(QWidget *parent)
:QWidget(parent)
{
m_manager = 0;
m_contactId = QContactLocalId(0);
m_nameEdit = new QLineEdit(this);
m_phoneEdit = new QLineEdit(this);
m_emailEdit = new QLineEdit(this);
m_addrEdit = new QLineEdit(this);
m_thumbnailBtn = new QPushButton(tr("Set picture"), this);
m_clearThumbnailBtn = new QPushButton(tr("Clear"), this);
m_thumbnailView = new QLabel(this);
connect(m_thumbnailBtn, SIGNAL(clicked()), this, SLOT(thumbnailClicked()));
connect(m_clearThumbnailBtn, SIGNAL(clicked()), this, SLOT(clearThumbnailClicked()));
QFormLayout *detailsLayout = new QFormLayout;
QLabel *nameLabel = new QLabel(tr("Name"), this);
QLabel *phoneLabel = new QLabel(tr("Phone"), this);
QLabel *emailLabel = new QLabel(tr("Email"), this);
QLabel *addressLabel = new QLabel(tr("Address"), this);
QLabel *thumbnailLabel = new QLabel(tr("Picture"), this);
QHBoxLayout *thumbnailBtnLayout = new QHBoxLayout;
thumbnailBtnLayout->addWidget(m_thumbnailBtn);
thumbnailBtnLayout->addWidget(m_clearThumbnailBtn);
if (QApplication::desktop()->availableGeometry().width() < 360) {
detailsLayout->addRow(nameLabel);
detailsLayout->addRow(m_nameEdit);
detailsLayout->addRow(phoneLabel);
detailsLayout->addRow(m_phoneEdit);
detailsLayout->addRow(emailLabel);
detailsLayout->addRow(m_emailEdit);
detailsLayout->addRow(addressLabel);
detailsLayout->addRow(m_addrEdit);
detailsLayout->addRow(thumbnailLabel);
detailsLayout->addRow(thumbnailBtnLayout);
detailsLayout->addRow(m_thumbnailView);
} else {
detailsLayout->addRow(nameLabel, m_nameEdit);
detailsLayout->addRow(phoneLabel, m_phoneEdit);
detailsLayout->addRow(emailLabel, m_emailEdit);
detailsLayout->addRow(addressLabel, m_addrEdit);
detailsLayout->addRow(thumbnailLabel, thumbnailBtnLayout);
detailsLayout->addRow("", m_thumbnailView);
}
detailsLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
detailsLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
QScrollArea *detailsScrollArea = new QScrollArea(this);
detailsScrollArea->setWidgetResizable(true);
QWidget *detailsContainer = new QWidget(detailsScrollArea);
detailsContainer->setLayout(detailsLayout);
detailsScrollArea->setWidget(detailsContainer);
QVBoxLayout *editLayout = new QVBoxLayout;
editLayout->addWidget(detailsScrollArea);
#ifdef Q_OS_SYMBIAN
m_saveBtn = new QAction(tr("Save"), this);
m_saveBtn->setSoftKeyRole(QAction::PositiveSoftKey);
addAction(m_saveBtn);
connect(m_saveBtn, SIGNAL(triggered(bool)), this, SLOT(saveClicked()));
m_cancelBtn = new QAction(tr("Cancel"), this);
m_cancelBtn->setSoftKeyRole(QAction::NegativeSoftKey);
addAction(m_cancelBtn);
connect(m_cancelBtn, SIGNAL(triggered(bool)), this, SLOT(cancelClicked()));
#else
m_saveBtn = new QPushButton(tr("&Save"), this);
m_saveBtn->setDefault(true);
connect(m_saveBtn, SIGNAL(clicked()), this, SLOT(saveClicked()));
m_cancelBtn = new QPushButton(tr("&Cancel"), this);
connect(m_cancelBtn, SIGNAL(clicked()), this, SLOT(cancelClicked()));
QHBoxLayout *btnLayout = new QHBoxLayout;
btnLayout->addWidget(m_saveBtn);
btnLayout->addWidget(m_cancelBtn);
editLayout->addLayout(btnLayout);
#endif
setLayout(editLayout);
}
ContactEditor::~ContactEditor()
{
}
void ContactEditor::setCurrentContact(QContactManager* manager, QContactLocalId currentId)
{
m_manager = manager;
m_contactId = currentId;
m_newThumbnailPath = QString();
m_nameEdit->clear();
m_phoneEdit->clear();
m_emailEdit->clear();
m_addrEdit->clear();
if (manager == 0) {
m_saveBtn->setEnabled(false);
return;
}
m_saveBtn->setEnabled(true);
QContact curr;
if (m_contactId != QContactLocalId(0))
curr = manager->contact(m_contactId);
QMap<QString, QContactDetailDefinition> defs = m_manager->detailDefinitions(QContactType::TypeContact);
if (m_contactId != QContactLocalId(0))
m_nameEdit->setText(manager->synthesizedContactDisplayLabel(curr));
QContactPhoneNumber phn = curr.detail(QContactPhoneNumber::DefinitionName);
m_phoneEdit->setText(phn.value(QContactPhoneNumber::FieldNumber));
if (defs.contains(QContactEmailAddress::DefinitionName)) {
QContactEmailAddress em = curr.detail(QContactEmailAddress::DefinitionName);
m_emailEdit->setText(em.value(QContactEmailAddress::FieldEmailAddress));
m_emailEdit->setReadOnly(false);
} else {
m_emailEdit->setText("<not supported>");
m_emailEdit->setReadOnly(true);
}
if (defs.contains(QContactAddress::DefinitionName)) {
QContactAddress adr = curr.detail(QContactAddress::DefinitionName);
m_addrEdit->setText(adr.value(QContactAddress::FieldStreet));
m_addrEdit->setReadOnly(false);
} else {
m_addrEdit->setText("<not supported>");
m_addrEdit->setReadOnly(true);
}
if (defs.contains(QContactThumbnail::DefinitionName)) {
m_thumbnailBtn->setEnabled(true);
QContactThumbnail thumb = curr.detail(QContactThumbnail::DefinitionName);
m_thumbnailView->clear();
m_thumbnail = thumb.thumbnail();
if (m_thumbnail.isNull()) {
m_thumbnailView->clear();
m_clearThumbnailBtn->setDisabled(true);
} else {
setThumbnailPixmap(QPixmap::fromImage(m_thumbnail));
}
} else {
m_thumbnailBtn->setDisabled(true);
m_clearThumbnailBtn->setDisabled(true);
}
}
QString ContactEditor::nameField()
{
if (!m_manager)
return QString();
QMap<QString, QContactDetailDefinition> defs = m_manager->detailDefinitions(QContactType::TypeContact);
QContactDetailDefinition nameDef = defs.value(QContactName::DefinitionName);
if (nameDef.fields().keys().contains(QContactName::FieldCustomLabel)) {
return QString(QLatin1String(QContactName::FieldCustomLabel));
} else if (nameDef.fields().keys().contains(QContactName::FieldFirstName)) {
return QString(QLatin1String(QContactName::FieldFirstName));
} else {
return QString();
}
}
void ContactEditor::setThumbnailPixmap(const QPixmap &pixmap)
{
if (pixmap.isNull())
return;
QPixmap scaled = pixmap.scaled(QSize(MAX_THUMBNAIL_DISPLAY_SIZE, MAX_THUMBNAIL_DISPLAY_SIZE),
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
m_thumbnailView->setPixmap(scaled);
m_thumbnailView->setMaximumSize(scaled.size());
m_clearThumbnailBtn->setEnabled(true);
}
void ContactEditor::clearThumbnailClicked()
{
m_thumbnailView->clear();
m_thumbnail = QImage();
m_newThumbnailPath.clear();
m_clearThumbnailBtn->setDisabled(true);
}
void ContactEditor::thumbnailClicked()
{
QString docPath = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
if (docPath.isEmpty())
docPath = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
if (docPath.isEmpty())
docPath = ".";
QString fileName = QFileDialog::getOpenFileName(this,
tr("Select Contact Picture"), docPath, tr("Image Files (*.png *.jpg *.bmp)"));
if (!fileName.isEmpty()) {
m_newThumbnailPath = fileName;
m_thumbnail = QImage(m_newThumbnailPath);
setThumbnailPixmap(QPixmap::fromImage(m_thumbnail));
}
}
void ContactEditor::saveClicked()
{
if (!m_manager) {
qWarning() << "No manager selected; cannot save.";
} else {
QContact curr;
if (m_contactId != QContactLocalId(0))
curr = m_manager->contact(m_contactId);
if (m_nameEdit->text().isEmpty()) {
QMessageBox::information(this, "Failed!", "You must give a name for the contact!");
return;
}
if (m_nameEdit->text() != m_manager->synthesizedContactDisplayLabel(curr)) {
QString saveNameField = nameField();
if (!saveNameField.isEmpty()) {
QContactName nm = curr.detail(QContactName::DefinitionName);
nm.setValue(saveNameField, m_nameEdit->text());
curr.saveDetail(&nm);
}
}
QContactPhoneNumber phn = curr.detail(QContactPhoneNumber::DefinitionName);
phn.setNumber(m_phoneEdit->text());
curr.saveDetail(&phn);
if (!m_emailEdit->isReadOnly()) {
QContactEmailAddress em = curr.detail(QContactEmailAddress::DefinitionName);
em.setEmailAddress(m_emailEdit->text());
curr.saveDetail(&em);
}
if (!m_addrEdit->isReadOnly()) {
QContactAddress adr = curr.detail(QContactAddress::DefinitionName);
adr.setStreet(m_addrEdit->text());
curr.saveDetail(&adr);
}
if (m_thumbnailBtn->isEnabled()) {
QContactThumbnail thumb = curr.detail(QContactThumbnail::DefinitionName);
QImage img(m_thumbnail);
thumb.setThumbnail(img);
curr.saveDetail(&thumb);
}
curr = m_manager->compatibleContact(curr);
bool success = m_manager->saveContact(&curr);
if (!success)
QMessageBox::information(this, "Failed!", QString("Failed to save contact!\n(error code %1)").arg(m_manager->error()));
}
emit showListPage();
}
void ContactEditor::cancelClicked()
{
emit showListPage();
}
|