emailuis/nmhswidget/src/nmhswidgetlistviewitem.cpp
changeset 68 83cc6bae1de8
child 74 6c59112cfd31
equal deleted inserted replaced
67:459da34cdb45 68:83cc6bae1de8
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <hbtextitem.h>
       
    19 #include <hbiconitem.h>
       
    20 #include <hbframeitem.h>
       
    21 #include <hbframebackground.h>
       
    22 #include <hbextendedlocale.h>
       
    23 #include <hbcolorscheme.h>
       
    24 #include <nmmessageenvelope.h>
       
    25 #include <nmicons.h>
       
    26 #include "nmhswidgetlistviewitem.h"
       
    27 
       
    28 /*!
       
    29     \class NmHsWidgetListViewItem
       
    30     \brief Mail widget list view item inherited from HbListViewItem
       
    31 */
       
    32 
       
    33 /*!
       
    34     Constructor
       
    35 */
       
    36 NmHsWidgetListViewItem::NmHsWidgetListViewItem(QGraphicsItem *parent)
       
    37 : HbListViewItem(parent),
       
    38         mSeparator(NULL),
       
    39         mSender(NULL),
       
    40         mSubject(NULL),
       
    41         mTime(NULL),
       
    42         mNewMsgIcon(NULL),
       
    43         mIcon1(NULL),
       
    44         mIcon2(NULL)
       
    45 {
       
    46     NM_FUNCTION; 
       
    47 }
       
    48 
       
    49 /*!
       
    50     Destructor
       
    51 */
       
    52 NmHsWidgetListViewItem::~NmHsWidgetListViewItem()
       
    53 {
       
    54     NM_FUNCTION;
       
    55 }
       
    56 
       
    57 /*!
       
    58     Item creation
       
    59 */
       
    60 HbAbstractViewItem* NmHsWidgetListViewItem::createItem()
       
    61 {
       
    62     NM_FUNCTION;
       
    63     
       
    64     return new NmHsWidgetListViewItem(*this);
       
    65 }
       
    66 
       
    67 /*!
       
    68     update child items
       
    69 */
       
    70 void NmHsWidgetListViewItem::updateChildItems()
       
    71 {
       
    72     NM_FUNCTION;
       
    73 
       
    74     NmMessageEnvelope *envelope =
       
    75            modelIndex().data(Qt::DisplayRole).value<NmMessageEnvelope*>();
       
    76   
       
    77     createMessageItemLayout();
       
    78     setContentsToMessageItem(*envelope);
       
    79     
       
    80     HbListViewItem::updateChildItems();   
       
    81 }
       
    82 
       
    83 /*!
       
    84     Set item layout.
       
    85     Function does not take ownership of model or model item.
       
    86  */
       
    87 void NmHsWidgetListViewItem::createMessageItemLayout()
       
    88 {
       
    89     NM_FUNCTION;
       
    90     getFontSizes();
       
    91     // Create separator icon and set name from widgetml.
       
    92     if (!mSeparator) {
       
    93         mSeparator = new HbIconItem(this);
       
    94         HbStyle::setItemName(mSeparator, "separator");
       
    95         mSeparator->setObjectName("ListViewItemMessageSeparator");
       
    96         mSeparator->setIcon(HbIcon("qtg_graf_divider_h_thin"));   
       
    97     }
       
    98     // Create sender label and set name from widgetml.
       
    99     if (!mSender) {
       
   100         mSender = new HbTextItem(this);
       
   101         HbStyle::setItemName(mSender, "sender");
       
   102         mSender->setObjectName("ListViewItemMessageSender");
       
   103     }
       
   104     // Create time label and set name from widgetml.
       
   105     if (!mTime) {
       
   106         mTime = new HbTextItem(this);
       
   107         HbStyle::setItemName(mTime, "time");
       
   108         mTime->setObjectName("ListViewItemMessageTime");
       
   109     }
       
   110     // Create subject label and set name from widgetml.
       
   111     if (!mSubject) {
       
   112         mSubject = new HbTextItem(this);
       
   113         HbStyle::setItemName(mSubject, "subject");
       
   114         mSubject->setObjectName("ListViewItemMessageSubject");
       
   115     }
       
   116     // Set new message icon.
       
   117     if (!mNewMsgIcon) {
       
   118         mNewMsgIcon = new HbFrameItem(this);
       
   119         HbStyle::setItemName(mNewMsgIcon, "msgicon");
       
   120     }
       
   121     
       
   122     // Create priority icon and set name from widgetml.
       
   123     if(!mIcon1){
       
   124         mIcon1 = new HbIconItem(this);
       
   125         HbStyle::setItemName(mIcon1, "icon1");
       
   126         mStatusIcons.append(mIcon1);
       
   127     }
       
   128 
       
   129     // Create attachment icon and set name from widgetml.
       
   130     if(!mIcon2){
       
   131         mIcon2 = new HbIconItem(this);
       
   132         HbStyle::setItemName(mIcon2, "icon2");
       
   133         mStatusIcons.append(mIcon2);
       
   134     }
       
   135         
       
   136     //set empty frame as list item only shows background when selected
       
   137     setDefaultFrame(HbFrameBackground("", HbFrameDrawer::OnePiece));
       
   138 }
       
   139 
       
   140 /*!
       
   141     Set item text label contents, icons, etc.
       
   142     Function does not take ownership of model or model item.
       
   143 */
       
   144 void NmHsWidgetListViewItem::setContentsToMessageItem(const NmMessageEnvelope &envelope)
       
   145 {
       
   146     NM_FUNCTION;
       
   147     // Member variables are created in previous function.
       
   148     // Sender.
       
   149     QString senderDisplayName = envelope.sender().displayName();
       
   150     if (!senderDisplayName.isNull() && !senderDisplayName.isEmpty()) {
       
   151         mSender->setText(senderDisplayName);
       
   152     }
       
   153     else {
       
   154         mSender->setText(envelope.sender().address());
       
   155     }
       
   156 
       
   157     // Time.
       
   158     HbExtendedLocale locale = HbExtendedLocale::system();
       
   159     QDateTime localTime = envelope.sentTime().addSecs(locale.universalTimeOffset());
       
   160     QDate sentLocalDate = localTime.date();
       
   161     QDate currentdate = QDate::currentDate();
       
   162     if (sentLocalDate == currentdate) {
       
   163         QString shortTimeSpec = r_qtn_time_usual;
       
   164         QTime time = localTime.time();
       
   165         mTime->setText(locale.format(time, shortTimeSpec));
       
   166     } else {
       
   167         QString shortDateSpec = r_qtn_date_without_year;
       
   168         mTime->setText(locale.format(sentLocalDate, shortDateSpec));
       
   169     }
       
   170     // Subject.
       
   171     QString subjectText = envelope.subject();
       
   172     if (subjectText.length()) {
       
   173         mSubject->setText(subjectText);
       
   174     } else {
       
   175         mSubject->setText(hbTrId("txt_mailwdgt_dblist_val_no_subject"));
       
   176     }
       
   177     
       
   178     // Here we have list for priority and attachment icons. Later it is easy to add
       
   179     // followup icon or something else if needed.
       
   180     QList<HbIcon> iconList;
       
   181     
       
   182     //make sure icons are not shown yet
       
   183     for (int i = 0; i < mStatusIcons.count(); i++) {
       
   184         mStatusIcons.at(i)->hide();
       
   185     }
       
   186     
       
   187     // Priority icon is added to list first thus it is always shown most right.
       
   188     switch (envelope.priority()) {
       
   189         case NmMessagePriorityLow:
       
   190             iconList.append(NmIcons::getIcon(NmIcons::NmIconPriorityLow));
       
   191             break;
       
   192         case NmMessagePriorityHigh:
       
   193             iconList.append(NmIcons::getIcon(NmIcons::NmIconPriorityHigh));
       
   194             break;
       
   195         default:
       
   196             // Normal priority has no icon
       
   197             break;
       
   198     }
       
   199 
       
   200     // Attachment icon is always shown on the left side of priority icon if it
       
   201     // exists. Otherwise in the most right.
       
   202     if (envelope.hasAttachments()) {
       
   203         iconList.append(NmIcons::getIcon(NmIcons::NmIconAttachment));
       
   204     }
       
   205 
       
   206     // Here we show icons added to the iconList in the order they have been added.
       
   207     for (int count = 0; count < iconList.count(); count++) {
       
   208         mStatusIcons.at(count)->setIcon(iconList.at(count));
       
   209         mStatusIcons.at(count)->show();
       
   210     }
       
   211     
       
   212     // Message read status.
       
   213     bool msgReadStatus(envelope.isRead());
       
   214     HbFrameDrawer *drawer(NULL);
       
   215     HbStyle::setItemName(mNewMsgIcon, "msgicon");
       
   216     if (!msgReadStatus) {
       
   217         setFontsUnread();
       
   218         mNewMsgIcon->setObjectName("ListViewItemMessageIconUnread");
       
   219         drawer = new HbFrameDrawer("qtg_fr_list_new_item",
       
   220                 HbFrameDrawer::ThreePiecesVertical);
       
   221         drawer->setFillWholeRect(false);
       
   222         // Set drawer. Takes ownership of the drawer and deletes previous drawer.
       
   223         mNewMsgIcon->setFrameDrawer(drawer);
       
   224         mNewMsgIcon->show();
       
   225    } else {
       
   226         setFontsRead();
       
   227         mNewMsgIcon->setObjectName("ListViewItemMessageIconRead");
       
   228         drawer = new HbFrameDrawer(); // Create empty drawer.
       
   229         drawer->setFillWholeRect(false);
       
   230         // Set drawer. Takes ownership of the drawer and deletes previous drawer.
       
   231         mNewMsgIcon->setFrameDrawer(drawer);
       
   232         mNewMsgIcon->hide();        
       
   233     }
       
   234    repolish();
       
   235 }
       
   236 
       
   237 /*!
       
   238     Boolean value indicating model index availability. Always true since
       
   239     this item prototype is used for both divider and message.
       
   240  */
       
   241 bool NmHsWidgetListViewItem::canSetModelIndex(const QModelIndex &index) const
       
   242 {
       
   243     NM_FUNCTION;
       
   244     Q_UNUSED(index);
       
   245     // This item class can handle all items in message list.
       
   246     return true;
       
   247 }
       
   248 
       
   249 /*!
       
   250     Set fonts unread.
       
   251  */
       
   252 void  NmHsWidgetListViewItem::setFontsUnread()
       
   253 {
       
   254     NM_FUNCTION;
       
   255     static QColor colorRole = HbColorScheme::color("qtc_list_item_title_normal");
       
   256     HbFontSpec fontSpec(HbFontSpec::Primary);
       
   257     setFonts(colorRole, fontSpec);
       
   258 }
       
   259 
       
   260 /*!
       
   261     Set fonts read.
       
   262  */
       
   263 void  NmHsWidgetListViewItem::setFontsRead()
       
   264 {
       
   265     NM_FUNCTION;
       
   266     static QColor colorRole = HbColorScheme::color("qtc_list_item_content_normal");
       
   267     HbFontSpec fontSpec(HbFontSpec::Secondary);
       
   268     setFonts(colorRole, fontSpec);
       
   269 }
       
   270 
       
   271 /*!
       
   272     Get font sizes.
       
   273  */
       
   274 void  NmHsWidgetListViewItem::getFontSizes()
       
   275 {
       
   276     NM_FUNCTION;
       
   277     // Get font sizes from style.
       
   278     qreal currentSize;
       
   279     bool found = style()->parameter(QString("hb-param-text-height-secondary"), currentSize );
       
   280     if (found) {
       
   281         mPrimarySize = currentSize;
       
   282     }      
       
   283     found = style()->parameter(QString("hb-param-text-height-tiny"), currentSize );
       
   284     if (found) {
       
   285         mSecondarySize = currentSize;
       
   286     }        
       
   287 }
       
   288 
       
   289 /*!
       
   290     Set fonts.
       
   291  */
       
   292 void  NmHsWidgetListViewItem::setFonts(const QColor &colorRole, 
       
   293         HbFontSpec &fontSpec)
       
   294 {
       
   295     NM_FUNCTION;
       
   296     // Change sizes explicitly since css is overwritten in polish now.    
       
   297     if (mSender && mSubject && mTime) {
       
   298 
       
   299         fontSpec.setTextHeight(mPrimarySize);
       
   300         
       
   301         mSender->setFontSpec(fontSpec);
       
   302         mSender->setTextColor(colorRole);
       
   303 
       
   304         mSubject->setFontSpec(fontSpec);
       
   305         mSubject->setTextColor(colorRole);
       
   306 
       
   307         fontSpec.setTextHeight(mSecondarySize);
       
   308 
       
   309         mTime->setFontSpec(fontSpec);        
       
   310         mTime->setTextColor(colorRole);
       
   311     }
       
   312 }