qtmobility/examples/samplephonebook/contacteditor.cpp
changeset 5 453da2cfceef
parent 4 90517678cc4f
child 11 06b8e2af4411
--- a/qtmobility/examples/samplephonebook/contacteditor.cpp	Mon May 03 13:18:40 2010 +0300
+++ b/qtmobility/examples/samplephonebook/contacteditor.cpp	Fri May 14 16:41:33 2010 +0300
@@ -6,35 +6,34 @@
 **
 ** This file is part of the Qt Mobility Components.
 **
-** $QT_BEGIN_LICENSE:LGPL$
-** No Commercial Usage
-** This file contains pre-release code and may not be distributed.
-** You may use this file in accordance with the terms and conditions
-** contained in the Technology Preview License Agreement accompanying
-** this package.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 2.1 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL included in the
-** packaging of this file.  Please review the following information to
-** ensure the GNU Lesser General Public License version 2.1 requirements
-** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
 **
-** In addition, as a special exception, Nokia gives you certain additional
-** rights.  These rights are described in the Nokia Qt LGPL Exception
-** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
-**
-** If you have questions regarding the use of this file, please contact
-** Nokia at qt-info@nokia.com.
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+**   * Redistributions of source code must retain the above copyright
+**     notice, this list of conditions and the following disclaimer.
+**   * Redistributions in binary form must reproduce the above copyright
+**     notice, this list of conditions and the following disclaimer in
+**     the documentation and/or other materials provided with the
+**     distribution.
+**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+**     the names of its contributors may be used to endorse or promote
+**     products derived from this software without specific prior written
+**     permission.
 **
-**
-**
-**
-**
-**
-**
-**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/
@@ -43,6 +42,8 @@
 
 #include <QtGui>
 
+const int MAX_AVATAR_DISPLAY_SIZE = 120;
+
 ContactEditor::ContactEditor(QWidget *parent)
         :QWidget(parent)
 {
@@ -53,17 +54,21 @@
     m_phoneEdit = new QLineEdit(this);
     m_emailEdit = new QLineEdit(this);
     m_addrEdit = new QLineEdit(this);
-    m_avatarBtn = new QPushButton(tr("Add image"), this);
-    m_avatarBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+    m_avatarBtn = new QPushButton(tr("Set picture"), this);
+    m_clearAvatarBtn = new QPushButton(tr("Clear"), this);
+    m_avatarView = new QLabel(this);
     connect(m_avatarBtn, SIGNAL(clicked()), this, SLOT(avatarClicked()));
-
+    connect(m_clearAvatarBtn, SIGNAL(clicked()), this, SLOT(clearAvatarClicked()));
 
     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 *avatarLabel = new QLabel(tr("Avatar"), this);
+    QLabel *avatarLabel = new QLabel(tr("Picture"), this);
+    QHBoxLayout *avatarBtnLayout = new QHBoxLayout;
+    avatarBtnLayout->addWidget(m_avatarBtn);
+    avatarBtnLayout->addWidget(m_clearAvatarBtn);
     if (QApplication::desktop()->availableGeometry().width() < 360) {
         // Narrow screen: put label on separate line to textbox
         detailsLayout->addRow(nameLabel);
@@ -75,14 +80,16 @@
         detailsLayout->addRow(addressLabel);
         detailsLayout->addRow(m_addrEdit);
         detailsLayout->addRow(avatarLabel);
-        detailsLayout->addRow(m_avatarBtn);
+        detailsLayout->addRow(avatarBtnLayout);
+        detailsLayout->addRow(m_avatarView);
     } else {
         // Wide screen: put label on same line as textbox
         detailsLayout->addRow(nameLabel, m_nameEdit);
         detailsLayout->addRow(phoneLabel, m_phoneEdit);
         detailsLayout->addRow(emailLabel, m_emailEdit);
         detailsLayout->addRow(addressLabel, m_addrEdit);
-        detailsLayout->addRow(avatarLabel, m_avatarBtn);
+        detailsLayout->addRow(avatarLabel, avatarBtnLayout);
+        detailsLayout->addRow("", m_avatarView);
     }
     detailsLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
     detailsLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
@@ -136,8 +143,6 @@
     m_phoneEdit->clear();
     m_emailEdit->clear();
     m_addrEdit->clear();
-    m_avatarBtn->setText("Add image");
-    m_avatarBtn->setIcon(QIcon());
 
     if (manager == 0) {
         m_saveBtn->setEnabled(false);
@@ -158,7 +163,7 @@
     // name
     //QContactName nm = curr.detail(QContactName::DefinitionName);
     if (m_contactId != QContactLocalId(0))
-        m_nameEdit->setText(manager->synthesizedDisplayLabel(curr));
+        m_nameEdit->setText(manager->synthesizedContactDisplayLabel(curr));
 
     // phonenumber
     QContactPhoneNumber phn = curr.detail(QContactPhoneNumber::DefinitionName);
@@ -184,29 +189,29 @@
         m_addrEdit->setReadOnly(true);
     }
 
-    // avatar button
-    if (defs.contains(QContactAvatar::DefinitionName)) {
+    // avatar viewer
+    if (defs.contains(QContactAvatar::DefinitionName)
+        && defs.contains(QContactThumbnail::DefinitionName)) {
+        m_avatarBtn->setEnabled(true);
         QContactAvatar av = curr.detail(QContactAvatar::DefinitionName);
         QContactThumbnail thumb = curr.detail(QContactThumbnail::DefinitionName);
-        m_avatarBtn->setText(QString());
-        m_avatarBtn->setIcon(QIcon());
-        if (thumb.thumbnail().isNull()) {
-            if (av.imageUrl().isEmpty()) {
-                m_avatarBtn->setText("Add image");
+        m_avatarView->clear();
+        m_newAvatarPath = av.imageUrl().toLocalFile();
+        m_thumbnail = thumb.thumbnail();
+        if (m_thumbnail.isNull()) {
+            if (m_newAvatarPath.isEmpty()) {
+                m_avatarView->clear();
+                m_clearAvatarBtn->setDisabled(true);
             } else {
-                m_avatarBtn->setIcon(QIcon(QPixmap(av.imageUrl().toLocalFile())));
+                setAvatarPixmap(QPixmap(av.imageUrl().toLocalFile()));
                 m_thumbnail = QImage(av.imageUrl().toLocalFile());
             }
         } else {
-            m_newAvatarPath = av.imageUrl().toLocalFile();
-            m_thumbnail = thumb.thumbnail();
-            m_avatarBtn->setIcon(QIcon(QPixmap::fromImage(thumb.thumbnail())));
+            setAvatarPixmap(QPixmap::fromImage(m_thumbnail));
         }
-        m_avatarBtn->setDisabled(false);
     } else {
-        m_avatarBtn->setIcon(QIcon());
-        m_avatarBtn->setText("<not supported>");
         m_avatarBtn->setDisabled(true);
+        m_clearAvatarBtn->setDisabled(true);
     }
 }
 
@@ -227,17 +232,36 @@
     }
 }
 
+void ContactEditor::setAvatarPixmap(const QPixmap &pixmap)
+{
+    if (pixmap.isNull())
+        return;
+    QPixmap scaled = pixmap.scaled(QSize(MAX_AVATAR_DISPLAY_SIZE, MAX_AVATAR_DISPLAY_SIZE),
+                                   Qt::KeepAspectRatio,
+                                   Qt::SmoothTransformation);
+    m_avatarView->setPixmap(scaled);
+    m_avatarView->setMaximumSize(scaled.size());
+    m_clearAvatarBtn->setEnabled(true);
+}
+
+void ContactEditor::clearAvatarClicked()
+{
+    m_avatarView->clear();
+    m_thumbnail = QImage();
+    m_newAvatarPath.clear();
+    m_clearAvatarBtn->setDisabled(true);
+}
+
 void ContactEditor::avatarClicked()
 {
     // put up a file dialog, and update the new avatar path.
     QString fileName = QFileDialog::getOpenFileName(this,
-       tr("Select Avatar Image"), ".", tr("Image Files (*.png *.jpg *.bmp)"));
+       tr("Select Contact Picture"), ".", tr("Image Files (*.png *.jpg *.bmp)"));
 
     if (!fileName.isEmpty()) {
         m_newAvatarPath = fileName;
         m_thumbnail = QImage(m_newAvatarPath);
-        m_avatarBtn->setText(QString());
-        m_avatarBtn->setIcon(QIcon(m_newAvatarPath));
+        setAvatarPixmap(QPixmap::fromImage(m_thumbnail));
     }
 }
 
@@ -255,7 +279,7 @@
             return;
         }
 
-        if (m_nameEdit->text() != m_manager->synthesizedDisplayLabel(curr)) {
+        if (m_nameEdit->text() != m_manager->synthesizedContactDisplayLabel(curr)) {
             // if the name has changed (ie, is different to the synthed label) then save it as a custom label.
             QString saveNameField = nameField();
             if (!saveNameField.isEmpty()) {