examples/samplephonebook/contacteditor.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** "Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are
       
    14 ** met:
       
    15 **   * Redistributions of source code must retain the above copyright
       
    16 **     notice, this list of conditions and the following disclaimer.
       
    17 **   * Redistributions in binary form must reproduce the above copyright
       
    18 **     notice, this list of conditions and the following disclaimer in
       
    19 **     the documentation and/or other materials provided with the
       
    20 **     distribution.
       
    21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
       
    22 **     the names of its contributors may be used to endorse or promote
       
    23 **     products derived from this software without specific prior written
       
    24 **     permission.
       
    25 **
       
    26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
       
    37 ** $QT_END_LICENSE$
       
    38 **
       
    39 ****************************************************************************/
       
    40 
       
    41 #include "contacteditor.h"
       
    42 
       
    43 #include <QtGui>
       
    44 
       
    45 const int MAX_AVATAR_DISPLAY_SIZE = 120;
       
    46 
       
    47 ContactEditor::ContactEditor(QWidget *parent)
       
    48         :QWidget(parent)
       
    49 {
       
    50     m_manager = 0;
       
    51     m_contactId = QContactLocalId(0);
       
    52 
       
    53     m_nameEdit = new QLineEdit(this);
       
    54     m_phoneEdit = new QLineEdit(this);
       
    55     m_emailEdit = new QLineEdit(this);
       
    56     m_addrEdit = new QLineEdit(this);
       
    57     m_avatarBtn = new QPushButton(tr("Set picture"), this);
       
    58     m_clearAvatarBtn = new QPushButton(tr("Clear"), this);
       
    59     m_avatarView = new QLabel(this);
       
    60     connect(m_avatarBtn, SIGNAL(clicked()), this, SLOT(avatarClicked()));
       
    61     connect(m_clearAvatarBtn, SIGNAL(clicked()), this, SLOT(clearAvatarClicked()));
       
    62 
       
    63     QFormLayout *detailsLayout = new QFormLayout;
       
    64     QLabel *nameLabel = new QLabel(tr("Name"), this);
       
    65     QLabel *phoneLabel = new QLabel(tr("Phone"), this);
       
    66     QLabel *emailLabel = new QLabel(tr("Email"), this);
       
    67     QLabel *addressLabel = new QLabel(tr("Address"), this);
       
    68     QLabel *avatarLabel = new QLabel(tr("Picture"), this);
       
    69     QHBoxLayout *avatarBtnLayout = new QHBoxLayout;
       
    70     avatarBtnLayout->addWidget(m_avatarBtn);
       
    71     avatarBtnLayout->addWidget(m_clearAvatarBtn);
       
    72     if (QApplication::desktop()->availableGeometry().width() < 360) {
       
    73         // Narrow screen: put label on separate line to textbox
       
    74         detailsLayout->addRow(nameLabel);
       
    75         detailsLayout->addRow(m_nameEdit);
       
    76         detailsLayout->addRow(phoneLabel);
       
    77         detailsLayout->addRow(m_phoneEdit);
       
    78         detailsLayout->addRow(emailLabel);
       
    79         detailsLayout->addRow(m_emailEdit);
       
    80         detailsLayout->addRow(addressLabel);
       
    81         detailsLayout->addRow(m_addrEdit);
       
    82         detailsLayout->addRow(avatarLabel);
       
    83         detailsLayout->addRow(avatarBtnLayout);
       
    84         detailsLayout->addRow(m_avatarView);
       
    85     } else {
       
    86         // Wide screen: put label on same line as textbox
       
    87         detailsLayout->addRow(nameLabel, m_nameEdit);
       
    88         detailsLayout->addRow(phoneLabel, m_phoneEdit);
       
    89         detailsLayout->addRow(emailLabel, m_emailEdit);
       
    90         detailsLayout->addRow(addressLabel, m_addrEdit);
       
    91         detailsLayout->addRow(avatarLabel, avatarBtnLayout);
       
    92         detailsLayout->addRow("", m_avatarView);
       
    93     }
       
    94     detailsLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
       
    95     detailsLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
       
    96 
       
    97     QScrollArea *detailsScrollArea = new QScrollArea(this);
       
    98     detailsScrollArea->setWidgetResizable(true);
       
    99     QWidget *detailsContainer = new QWidget(detailsScrollArea);
       
   100     detailsContainer->setLayout(detailsLayout);
       
   101     detailsScrollArea->setWidget(detailsContainer);
       
   102 
       
   103     QVBoxLayout *editLayout = new QVBoxLayout;
       
   104     editLayout->addWidget(detailsScrollArea);
       
   105 
       
   106 #ifdef Q_OS_SYMBIAN
       
   107     // In symbian "save" and "cancel" buttons are softkeys.
       
   108     m_saveBtn = new QAction(tr("Save"), this);
       
   109     m_saveBtn->setSoftKeyRole(QAction::PositiveSoftKey);
       
   110     addAction(m_saveBtn);
       
   111     connect(m_saveBtn, SIGNAL(triggered(bool)), this, SLOT(saveClicked()));
       
   112     m_cancelBtn = new QAction(tr("Cancel"), this);
       
   113     m_cancelBtn->setSoftKeyRole(QAction::NegativeSoftKey);
       
   114     addAction(m_cancelBtn);
       
   115     connect(m_cancelBtn, SIGNAL(triggered(bool)), this, SLOT(cancelClicked()));
       
   116 #else
       
   117     m_saveBtn = new QPushButton(tr("&Save"), this);
       
   118     m_saveBtn->setDefault(true);
       
   119     connect(m_saveBtn, SIGNAL(clicked()), this, SLOT(saveClicked()));
       
   120     m_cancelBtn = new QPushButton(tr("&Cancel"), this);
       
   121     connect(m_cancelBtn, SIGNAL(clicked()), this, SLOT(cancelClicked()));
       
   122     QHBoxLayout *btnLayout = new QHBoxLayout;
       
   123     btnLayout->addWidget(m_saveBtn);
       
   124     btnLayout->addWidget(m_cancelBtn);
       
   125     editLayout->addLayout(btnLayout);
       
   126 #endif
       
   127 
       
   128     setLayout(editLayout);
       
   129 }
       
   130 
       
   131 ContactEditor::~ContactEditor()
       
   132 {
       
   133 }
       
   134 
       
   135 void ContactEditor::setCurrentContact(QContactManager* manager, QContactLocalId currentId)
       
   136 {
       
   137     m_manager = manager;
       
   138     m_contactId = currentId;
       
   139     m_newAvatarPath = QString();
       
   140 
       
   141     // Clear UI
       
   142     m_nameEdit->clear();
       
   143     m_phoneEdit->clear();
       
   144     m_emailEdit->clear();
       
   145     m_addrEdit->clear();
       
   146 
       
   147     if (manager == 0) {
       
   148         m_saveBtn->setEnabled(false);
       
   149         return;
       
   150     }
       
   151 
       
   152     // enable the UI.
       
   153     m_saveBtn->setEnabled(true);
       
   154 
       
   155     // otherwise, build from the contact details.
       
   156     QContact curr;
       
   157     if (m_contactId != QContactLocalId(0))
       
   158         curr = manager->contact(m_contactId);
       
   159 
       
   160     // Disable fields & buttons according to what the backend supports
       
   161     QMap<QString, QContactDetailDefinition> defs = m_manager->detailDefinitions(QContactType::TypeContact);
       
   162 
       
   163     // name
       
   164     //QContactName nm = curr.detail(QContactName::DefinitionName);
       
   165     if (m_contactId != QContactLocalId(0))
       
   166         m_nameEdit->setText(manager->synthesizedContactDisplayLabel(curr));
       
   167 
       
   168     // phonenumber
       
   169     QContactPhoneNumber phn = curr.detail(QContactPhoneNumber::DefinitionName);
       
   170     m_phoneEdit->setText(phn.value(QContactPhoneNumber::FieldNumber));
       
   171 
       
   172     // email
       
   173     if (defs.contains(QContactEmailAddress::DefinitionName)) {
       
   174         QContactEmailAddress em = curr.detail(QContactEmailAddress::DefinitionName);
       
   175         m_emailEdit->setText(em.value(QContactEmailAddress::FieldEmailAddress));
       
   176         m_emailEdit->setReadOnly(false);
       
   177     } else {
       
   178         m_emailEdit->setText("<not supported>");
       
   179         m_emailEdit->setReadOnly(true);
       
   180     }
       
   181 
       
   182     // address
       
   183     if (defs.contains(QContactAddress::DefinitionName)) {
       
   184         QContactAddress adr = curr.detail(QContactAddress::DefinitionName);
       
   185         m_addrEdit->setText(adr.value(QContactAddress::FieldStreet)); // ugly hack.
       
   186         m_addrEdit->setReadOnly(false);
       
   187     } else {
       
   188         m_addrEdit->setText("<not supported>");
       
   189         m_addrEdit->setReadOnly(true);
       
   190     }
       
   191 
       
   192     // avatar viewer
       
   193     if (defs.contains(QContactAvatar::DefinitionName)
       
   194         || defs.contains(QContactThumbnail::DefinitionName)) {
       
   195         m_avatarBtn->setEnabled(true);
       
   196         QContactAvatar av = curr.detail(QContactAvatar::DefinitionName);
       
   197         QContactThumbnail thumb = curr.detail(QContactThumbnail::DefinitionName);
       
   198         m_avatarView->clear();
       
   199         m_newAvatarPath = av.imageUrl().toLocalFile();
       
   200         m_thumbnail = thumb.thumbnail();
       
   201         if (m_thumbnail.isNull()) {
       
   202             if (m_newAvatarPath.isEmpty()) {
       
   203                 m_avatarView->clear();
       
   204                 m_clearAvatarBtn->setDisabled(true);
       
   205             } else {
       
   206                 setAvatarPixmap(QPixmap(av.imageUrl().toLocalFile()));
       
   207                 m_thumbnail = QImage(av.imageUrl().toLocalFile());
       
   208             }
       
   209         } else {
       
   210             setAvatarPixmap(QPixmap::fromImage(m_thumbnail));
       
   211         }
       
   212     } else {
       
   213         m_avatarBtn->setDisabled(true);
       
   214         m_clearAvatarBtn->setDisabled(true);
       
   215     }
       
   216 }
       
   217 
       
   218 QString ContactEditor::nameField()
       
   219 {
       
   220     // return the field which the name data should be saved in.
       
   221     if (!m_manager)
       
   222         return QString();
       
   223 
       
   224     QMap<QString, QContactDetailDefinition> defs = m_manager->detailDefinitions(QContactType::TypeContact);
       
   225     QContactDetailDefinition nameDef = defs.value(QContactName::DefinitionName);
       
   226     if (nameDef.fields().keys().contains(QContactName::FieldCustomLabel)) {
       
   227         return QString(QLatin1String(QContactName::FieldCustomLabel));
       
   228     } else if (nameDef.fields().keys().contains(QContactName::FieldFirstName)) {
       
   229         return QString(QLatin1String(QContactName::FieldFirstName));
       
   230     } else {
       
   231         return QString();
       
   232     }
       
   233 }
       
   234 
       
   235 void ContactEditor::setAvatarPixmap(const QPixmap &pixmap)
       
   236 {
       
   237     if (pixmap.isNull())
       
   238         return;
       
   239     QPixmap scaled = pixmap.scaled(QSize(MAX_AVATAR_DISPLAY_SIZE, MAX_AVATAR_DISPLAY_SIZE),
       
   240                                    Qt::KeepAspectRatio,
       
   241                                    Qt::SmoothTransformation);
       
   242     m_avatarView->setPixmap(scaled);
       
   243     m_avatarView->setMaximumSize(scaled.size());
       
   244     m_clearAvatarBtn->setEnabled(true);
       
   245 }
       
   246 
       
   247 void ContactEditor::clearAvatarClicked()
       
   248 {
       
   249     m_avatarView->clear();
       
   250     m_thumbnail = QImage();
       
   251     m_newAvatarPath.clear();
       
   252     m_clearAvatarBtn->setDisabled(true);
       
   253 }
       
   254 
       
   255 void ContactEditor::avatarClicked()
       
   256 {
       
   257     // put up a file dialog, and update the new avatar path.
       
   258     QString fileName = QFileDialog::getOpenFileName(this,
       
   259        tr("Select Contact Picture"), ".", tr("Image Files (*.png *.jpg *.bmp)"));
       
   260 
       
   261     if (!fileName.isEmpty()) {
       
   262         m_newAvatarPath = fileName;
       
   263         m_thumbnail = QImage(m_newAvatarPath);
       
   264         setAvatarPixmap(QPixmap::fromImage(m_thumbnail));
       
   265     }
       
   266 }
       
   267 
       
   268 void ContactEditor::saveClicked()
       
   269 {
       
   270     if (!m_manager) {
       
   271         qWarning() << "No manager selected; cannot save.";
       
   272     } else {
       
   273         QContact curr;
       
   274         if (m_contactId != QContactLocalId(0))
       
   275             curr = m_manager->contact(m_contactId);
       
   276 
       
   277         if (m_nameEdit->text().isEmpty()) {
       
   278             QMessageBox::information(this, "Failed!", "You must give a name for the contact!");
       
   279             return;
       
   280         }
       
   281 
       
   282         if (m_nameEdit->text() != m_manager->synthesizedContactDisplayLabel(curr)) {
       
   283             // if the name has changed (ie, is different to the synthed label) then save it as a custom label.
       
   284             QString saveNameField = nameField();
       
   285             if (!saveNameField.isEmpty()) {
       
   286                 QContactName nm = curr.detail(QContactName::DefinitionName);
       
   287                 nm.setValue(saveNameField, m_nameEdit->text());
       
   288                 curr.saveDetail(&nm);
       
   289             }
       
   290         }
       
   291 
       
   292         QContactPhoneNumber phn = curr.detail(QContactPhoneNumber::DefinitionName);
       
   293         phn.setNumber(m_phoneEdit->text());
       
   294         curr.saveDetail(&phn);
       
   295 
       
   296         if (!m_emailEdit->isReadOnly()) {
       
   297             QContactEmailAddress em = curr.detail(QContactEmailAddress::DefinitionName);
       
   298             em.setEmailAddress(m_emailEdit->text());
       
   299             curr.saveDetail(&em);
       
   300         }
       
   301 
       
   302         if (!m_addrEdit->isReadOnly()) {
       
   303             QContactAddress adr = curr.detail(QContactAddress::DefinitionName);
       
   304             adr.setStreet(m_addrEdit->text());
       
   305             curr.saveDetail(&adr);
       
   306         }
       
   307 
       
   308         if (m_avatarBtn->isEnabled()) {
       
   309             QContactAvatar av = curr.detail(QContactAvatar::DefinitionName);
       
   310             av.setImageUrl(QUrl(m_newAvatarPath));
       
   311             curr.saveDetail(&av);
       
   312 
       
   313             QContactThumbnail thumb = curr.detail(QContactThumbnail::DefinitionName);
       
   314             QImage img(m_thumbnail);
       
   315             thumb.setThumbnail(img);
       
   316             curr.saveDetail(&thumb);
       
   317         }
       
   318 
       
   319         curr = m_manager->compatibleContact(curr);
       
   320         bool success = m_manager->saveContact(&curr);
       
   321         if (!success)
       
   322             QMessageBox::information(this, "Failed!", QString("Failed to save contact!\n(error code %1)").arg(m_manager->error()));
       
   323     }
       
   324 
       
   325     emit showListPage();
       
   326 }
       
   327 
       
   328 void ContactEditor::cancelClicked()
       
   329 {
       
   330     emit showListPage();
       
   331 }