qtmobility/examples/samplephonebook/contacteditor.cpp
changeset 5 453da2cfceef
parent 4 90517678cc4f
child 11 06b8e2af4411
equal deleted inserted replaced
4:90517678cc4f 5:453da2cfceef
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the Qt Mobility Components.
     7 ** This file is part of the Qt Mobility Components.
     8 **
     8 **
     9 ** $QT_BEGIN_LICENSE:LGPL$
     9 ** $QT_BEGIN_LICENSE:BSD$
    10 ** No Commercial Usage
    10 ** You may use this file under the terms of the BSD license as follows:
    11 ** This file contains pre-release code and may not be distributed.
    11 **
    12 ** You may use this file in accordance with the terms and conditions
    12 ** "Redistribution and use in source and binary forms, with or without
    13 ** contained in the Technology Preview License Agreement accompanying
    13 ** modification, are permitted provided that the following conditions are
    14 ** this package.
    14 ** met:
    15 **
    15 **   * Redistributions of source code must retain the above copyright
    16 ** GNU Lesser General Public License Usage
    16 **     notice, this list of conditions and the following disclaimer.
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
    17 **   * Redistributions in binary form must reproduce the above copyright
    18 ** General Public License version 2.1 as published by the Free Software
    18 **     notice, this list of conditions and the following disclaimer in
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
    19 **     the documentation and/or other materials provided with the
    20 ** packaging of this file.  Please review the following information to
    20 **     distribution.
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
    21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    22 **     the names of its contributors may be used to endorse or promote
    23 **
    23 **     products derived from this software without specific prior written
    24 ** In addition, as a special exception, Nokia gives you certain additional
    24 **     permission.
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
    25 **
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    27 **
    27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    28 ** If you have questions regarding the use of this file, please contact
    28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    29 ** Nokia at qt-info@nokia.com.
    29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    30 **
    30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    31 **
    31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    32 **
    32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    33 **
    33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    34 **
    34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    35 **
    35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    36 **
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
    37 **
       
    38 ** $QT_END_LICENSE$
    37 ** $QT_END_LICENSE$
    39 **
    38 **
    40 ****************************************************************************/
    39 ****************************************************************************/
    41 
    40 
    42 #include "contacteditor.h"
    41 #include "contacteditor.h"
    43 
    42 
    44 #include <QtGui>
    43 #include <QtGui>
       
    44 
       
    45 const int MAX_AVATAR_DISPLAY_SIZE = 120;
    45 
    46 
    46 ContactEditor::ContactEditor(QWidget *parent)
    47 ContactEditor::ContactEditor(QWidget *parent)
    47         :QWidget(parent)
    48         :QWidget(parent)
    48 {
    49 {
    49     m_manager = 0;
    50     m_manager = 0;
    51 
    52 
    52     m_nameEdit = new QLineEdit(this);
    53     m_nameEdit = new QLineEdit(this);
    53     m_phoneEdit = new QLineEdit(this);
    54     m_phoneEdit = new QLineEdit(this);
    54     m_emailEdit = new QLineEdit(this);
    55     m_emailEdit = new QLineEdit(this);
    55     m_addrEdit = new QLineEdit(this);
    56     m_addrEdit = new QLineEdit(this);
    56     m_avatarBtn = new QPushButton(tr("Add image"), this);
    57     m_avatarBtn = new QPushButton(tr("Set picture"), this);
    57     m_avatarBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    58     m_clearAvatarBtn = new QPushButton(tr("Clear"), this);
       
    59     m_avatarView = new QLabel(this);
    58     connect(m_avatarBtn, SIGNAL(clicked()), this, SLOT(avatarClicked()));
    60     connect(m_avatarBtn, SIGNAL(clicked()), this, SLOT(avatarClicked()));
    59 
    61     connect(m_clearAvatarBtn, SIGNAL(clicked()), this, SLOT(clearAvatarClicked()));
    60 
    62 
    61     QFormLayout *detailsLayout = new QFormLayout;
    63     QFormLayout *detailsLayout = new QFormLayout;
    62     QLabel *nameLabel = new QLabel(tr("Name"), this);
    64     QLabel *nameLabel = new QLabel(tr("Name"), this);
    63     QLabel *phoneLabel = new QLabel(tr("Phone"), this);
    65     QLabel *phoneLabel = new QLabel(tr("Phone"), this);
    64     QLabel *emailLabel = new QLabel(tr("Email"), this);
    66     QLabel *emailLabel = new QLabel(tr("Email"), this);
    65     QLabel *addressLabel = new QLabel(tr("Address"), this);
    67     QLabel *addressLabel = new QLabel(tr("Address"), this);
    66     QLabel *avatarLabel = new QLabel(tr("Avatar"), this);
    68     QLabel *avatarLabel = new QLabel(tr("Picture"), this);
       
    69     QHBoxLayout *avatarBtnLayout = new QHBoxLayout;
       
    70     avatarBtnLayout->addWidget(m_avatarBtn);
       
    71     avatarBtnLayout->addWidget(m_clearAvatarBtn);
    67     if (QApplication::desktop()->availableGeometry().width() < 360) {
    72     if (QApplication::desktop()->availableGeometry().width() < 360) {
    68         // Narrow screen: put label on separate line to textbox
    73         // Narrow screen: put label on separate line to textbox
    69         detailsLayout->addRow(nameLabel);
    74         detailsLayout->addRow(nameLabel);
    70         detailsLayout->addRow(m_nameEdit);
    75         detailsLayout->addRow(m_nameEdit);
    71         detailsLayout->addRow(phoneLabel);
    76         detailsLayout->addRow(phoneLabel);
    73         detailsLayout->addRow(emailLabel);
    78         detailsLayout->addRow(emailLabel);
    74         detailsLayout->addRow(m_emailEdit);
    79         detailsLayout->addRow(m_emailEdit);
    75         detailsLayout->addRow(addressLabel);
    80         detailsLayout->addRow(addressLabel);
    76         detailsLayout->addRow(m_addrEdit);
    81         detailsLayout->addRow(m_addrEdit);
    77         detailsLayout->addRow(avatarLabel);
    82         detailsLayout->addRow(avatarLabel);
    78         detailsLayout->addRow(m_avatarBtn);
    83         detailsLayout->addRow(avatarBtnLayout);
       
    84         detailsLayout->addRow(m_avatarView);
    79     } else {
    85     } else {
    80         // Wide screen: put label on same line as textbox
    86         // Wide screen: put label on same line as textbox
    81         detailsLayout->addRow(nameLabel, m_nameEdit);
    87         detailsLayout->addRow(nameLabel, m_nameEdit);
    82         detailsLayout->addRow(phoneLabel, m_phoneEdit);
    88         detailsLayout->addRow(phoneLabel, m_phoneEdit);
    83         detailsLayout->addRow(emailLabel, m_emailEdit);
    89         detailsLayout->addRow(emailLabel, m_emailEdit);
    84         detailsLayout->addRow(addressLabel, m_addrEdit);
    90         detailsLayout->addRow(addressLabel, m_addrEdit);
    85         detailsLayout->addRow(avatarLabel, m_avatarBtn);
    91         detailsLayout->addRow(avatarLabel, avatarBtnLayout);
       
    92         detailsLayout->addRow("", m_avatarView);
    86     }
    93     }
    87     detailsLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
    94     detailsLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
    88     detailsLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
    95     detailsLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
    89 
    96 
    90     QScrollArea *detailsScrollArea = new QScrollArea(this);
    97     QScrollArea *detailsScrollArea = new QScrollArea(this);
   134     // Clear UI
   141     // Clear UI
   135     m_nameEdit->clear();
   142     m_nameEdit->clear();
   136     m_phoneEdit->clear();
   143     m_phoneEdit->clear();
   137     m_emailEdit->clear();
   144     m_emailEdit->clear();
   138     m_addrEdit->clear();
   145     m_addrEdit->clear();
   139     m_avatarBtn->setText("Add image");
       
   140     m_avatarBtn->setIcon(QIcon());
       
   141 
   146 
   142     if (manager == 0) {
   147     if (manager == 0) {
   143         m_saveBtn->setEnabled(false);
   148         m_saveBtn->setEnabled(false);
   144         return;
   149         return;
   145     }
   150     }
   156     QMap<QString, QContactDetailDefinition> defs = m_manager->detailDefinitions(QContactType::TypeContact);
   161     QMap<QString, QContactDetailDefinition> defs = m_manager->detailDefinitions(QContactType::TypeContact);
   157 
   162 
   158     // name
   163     // name
   159     //QContactName nm = curr.detail(QContactName::DefinitionName);
   164     //QContactName nm = curr.detail(QContactName::DefinitionName);
   160     if (m_contactId != QContactLocalId(0))
   165     if (m_contactId != QContactLocalId(0))
   161         m_nameEdit->setText(manager->synthesizedDisplayLabel(curr));
   166         m_nameEdit->setText(manager->synthesizedContactDisplayLabel(curr));
   162 
   167 
   163     // phonenumber
   168     // phonenumber
   164     QContactPhoneNumber phn = curr.detail(QContactPhoneNumber::DefinitionName);
   169     QContactPhoneNumber phn = curr.detail(QContactPhoneNumber::DefinitionName);
   165     m_phoneEdit->setText(phn.value(QContactPhoneNumber::FieldNumber));
   170     m_phoneEdit->setText(phn.value(QContactPhoneNumber::FieldNumber));
   166 
   171 
   182     } else {
   187     } else {
   183         m_addrEdit->setText("<not supported>");
   188         m_addrEdit->setText("<not supported>");
   184         m_addrEdit->setReadOnly(true);
   189         m_addrEdit->setReadOnly(true);
   185     }
   190     }
   186 
   191 
   187     // avatar button
   192     // avatar viewer
   188     if (defs.contains(QContactAvatar::DefinitionName)) {
   193     if (defs.contains(QContactAvatar::DefinitionName)
       
   194         && defs.contains(QContactThumbnail::DefinitionName)) {
       
   195         m_avatarBtn->setEnabled(true);
   189         QContactAvatar av = curr.detail(QContactAvatar::DefinitionName);
   196         QContactAvatar av = curr.detail(QContactAvatar::DefinitionName);
   190         QContactThumbnail thumb = curr.detail(QContactThumbnail::DefinitionName);
   197         QContactThumbnail thumb = curr.detail(QContactThumbnail::DefinitionName);
   191         m_avatarBtn->setText(QString());
   198         m_avatarView->clear();
   192         m_avatarBtn->setIcon(QIcon());
   199         m_newAvatarPath = av.imageUrl().toLocalFile();
   193         if (thumb.thumbnail().isNull()) {
   200         m_thumbnail = thumb.thumbnail();
   194             if (av.imageUrl().isEmpty()) {
   201         if (m_thumbnail.isNull()) {
   195                 m_avatarBtn->setText("Add image");
   202             if (m_newAvatarPath.isEmpty()) {
       
   203                 m_avatarView->clear();
       
   204                 m_clearAvatarBtn->setDisabled(true);
   196             } else {
   205             } else {
   197                 m_avatarBtn->setIcon(QIcon(QPixmap(av.imageUrl().toLocalFile())));
   206                 setAvatarPixmap(QPixmap(av.imageUrl().toLocalFile()));
   198                 m_thumbnail = QImage(av.imageUrl().toLocalFile());
   207                 m_thumbnail = QImage(av.imageUrl().toLocalFile());
   199             }
   208             }
   200         } else {
   209         } else {
   201             m_newAvatarPath = av.imageUrl().toLocalFile();
   210             setAvatarPixmap(QPixmap::fromImage(m_thumbnail));
   202             m_thumbnail = thumb.thumbnail();
   211         }
   203             m_avatarBtn->setIcon(QIcon(QPixmap::fromImage(thumb.thumbnail())));
   212     } else {
   204         }
       
   205         m_avatarBtn->setDisabled(false);
       
   206     } else {
       
   207         m_avatarBtn->setIcon(QIcon());
       
   208         m_avatarBtn->setText("<not supported>");
       
   209         m_avatarBtn->setDisabled(true);
   213         m_avatarBtn->setDisabled(true);
       
   214         m_clearAvatarBtn->setDisabled(true);
   210     }
   215     }
   211 }
   216 }
   212 
   217 
   213 QString ContactEditor::nameField()
   218 QString ContactEditor::nameField()
   214 {
   219 {
   225     } else {
   230     } else {
   226         return QString();
   231         return QString();
   227     }
   232     }
   228 }
   233 }
   229 
   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 
   230 void ContactEditor::avatarClicked()
   255 void ContactEditor::avatarClicked()
   231 {
   256 {
   232     // put up a file dialog, and update the new avatar path.
   257     // put up a file dialog, and update the new avatar path.
   233     QString fileName = QFileDialog::getOpenFileName(this,
   258     QString fileName = QFileDialog::getOpenFileName(this,
   234        tr("Select Avatar Image"), ".", tr("Image Files (*.png *.jpg *.bmp)"));
   259        tr("Select Contact Picture"), ".", tr("Image Files (*.png *.jpg *.bmp)"));
   235 
   260 
   236     if (!fileName.isEmpty()) {
   261     if (!fileName.isEmpty()) {
   237         m_newAvatarPath = fileName;
   262         m_newAvatarPath = fileName;
   238         m_thumbnail = QImage(m_newAvatarPath);
   263         m_thumbnail = QImage(m_newAvatarPath);
   239         m_avatarBtn->setText(QString());
   264         setAvatarPixmap(QPixmap::fromImage(m_thumbnail));
   240         m_avatarBtn->setIcon(QIcon(m_newAvatarPath));
       
   241     }
   265     }
   242 }
   266 }
   243 
   267 
   244 void ContactEditor::saveClicked()
   268 void ContactEditor::saveClicked()
   245 {
   269 {
   253         if (m_nameEdit->text().isEmpty()) {
   277         if (m_nameEdit->text().isEmpty()) {
   254             QMessageBox::information(this, "Failed!", "You must give a name for the contact!");
   278             QMessageBox::information(this, "Failed!", "You must give a name for the contact!");
   255             return;
   279             return;
   256         }
   280         }
   257 
   281 
   258         if (m_nameEdit->text() != m_manager->synthesizedDisplayLabel(curr)) {
   282         if (m_nameEdit->text() != m_manager->synthesizedContactDisplayLabel(curr)) {
   259             // if the name has changed (ie, is different to the synthed label) then save it as a custom label.
   283             // if the name has changed (ie, is different to the synthed label) then save it as a custom label.
   260             QString saveNameField = nameField();
   284             QString saveNameField = nameField();
   261             if (!saveNameField.isEmpty()) {
   285             if (!saveNameField.isEmpty()) {
   262                 QContactName nm = curr.detail(QContactName::DefinitionName);
   286                 QContactName nm = curr.detail(QContactName::DefinitionName);
   263                 nm.setValue(saveNameField, m_nameEdit->text());
   287                 nm.setValue(saveNameField, m_nameEdit->text());