Home · All Classes · Main Classes · Deprecated

mpopuplistview.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (directui@nokia.com)
00006 **
00007 ** This file is part of libmeegotouch.
00008 **
00009 ** If you have questions regarding the use of this file, please contact
00010 ** Nokia at directui@nokia.com.
00011 **
00012 ** This library is free software; you can redistribute it and/or
00013 ** modify it under the terms of the GNU Lesser General Public
00014 ** License version 2.1 as published by the Free Software Foundation
00015 ** and appearing in the file LICENSE.LGPL included in the packaging
00016 ** of this file.
00017 **
00018 ****************************************************************************/
00019 
00020 #include "mpopuplistview.h"
00021 #include "mpopuplistview_p.h"
00022 
00023 #include "mpopuplist.h"
00024 #include "mlist.h"
00025 #include "mpannableviewport.h"
00026 #include "mlabel.h"
00027 #include "mimagewidget.h"
00028 #include "mlayout.h"
00029 #include "mlinearlayoutpolicy.h"
00030 
00031 #include <QGraphicsLinearLayout>
00032 
00033 MPopupListItem::MPopupListItem(QGraphicsItem* parent)
00034     : MListItem(parent),
00035       icon(0),
00036       title(0),
00037       itemStyle(SingleTitle)
00038 {
00039     memset(policy, 0, sizeof(policy));
00040     setObjectName("PopupListItem");
00041     setLayout(new MLayout(this));
00042     layout()->setContentsMargins(0, 0, 0, 0);
00043 }
00044 
00045 void MPopupListItem::updateLayout()
00046 {
00047     MLayout* mlayout = static_cast<MLayout*>(layout());
00048     if (!policy[itemStyle]) {
00049         policy[itemStyle] = new MLinearLayoutPolicy(mlayout, Qt::Horizontal);
00050         if (itemStyle == IconTitle) {
00051             policy[itemStyle]->addItem(icon);
00052             policy[itemStyle]->addItem(title);
00053         } else {
00054             policy[itemStyle]->addItem(title);
00055         }
00056     }
00057     mlayout->setPolicy(policy[itemStyle]);
00058 }
00059 
00060 void MPopupListItem::setTitle(const QString &text)
00061 {
00062     if (!title) {
00063         title = new MLabel(this);
00064         title->setTextElide(true);
00065         title->setStyleName("CommonSingleTitle");
00066     }
00067     title->setText(text);
00068     updateLayout();
00069 }
00070 
00071 void MPopupListItem::setIconID(const QString& id)
00072 {
00073     if (id.isEmpty()) {
00074         itemStyle = SingleTitle;
00075     } else {
00076         itemStyle = IconTitle;
00077         if (!icon) {
00078             icon = new MImageWidget(this);
00079             icon->setStyleName("CommonMainIcon");
00080         }
00081         icon->setImage(id);
00082     }
00083     updateLayout();
00084 }
00085 
00086 
00087 MPopupListViewPrivate::MPopupListViewPrivate()
00088     : controller(0), list(0), cellCreator(0)
00089 {
00090 }
00091 
00092 MPopupListViewPrivate::~MPopupListViewPrivate()
00093 {
00094 }
00095 
00096 void MPopupListViewPrivate::init()
00097 {
00098     Q_Q(MPopupListView);
00099 
00100     list = new MList();
00101     cellCreator = new MPopupListCellCreator(list);
00102     list->setCellCreator(cellCreator);
00103     list->setSelectionMode(MList::SingleSelection);
00104     q->contentsLayout()->insertItem(0, list);
00105 
00106     QObject::connect(list, SIGNAL(itemClicked(QModelIndex)), controller, SLOT(click(QModelIndex)));
00107     QObject::connect(list, SIGNAL(displayEntered()), q, SLOT(_q_scrollOnFirstAppearance()));
00108 }
00109 
00110 MPopupListCellCreator::MPopupListCellCreator(MList *list)
00111     : list(list)
00112 {}
00113 
00114 void MPopupListCellCreator::updateCell(const QModelIndex& index, MWidget * cell) const
00115 {
00116     MPopupListItem* item = static_cast<MPopupListItem*>(cell);
00117 
00118     QString title;
00119     QString iconID;
00120     QVariant value;
00121 
00122     value = list->itemModel()->data(index, Qt::DisplayRole);
00123     if (value.isValid())
00124         title = value.toString();
00125 
00126     value = list->itemModel()->data(index, Qt::DecorationRole);
00127     if (value.isValid())
00128         iconID = value.toString();
00129 
00130     item->setTitle(title);
00131     item->setIconID(iconID);
00132 
00133     if (list->selectionModel()->isSelected(index))
00134         item->setSelected(true);
00135 
00136     if (index.row() == 0)
00137         item->setLayoutPosition(M::VerticalTopPosition);
00138     else if (index.row() == index.model()->rowCount() - 1)
00139         item->setLayoutPosition(M::VerticalBottomPosition);
00140     else
00141         item->setLayoutPosition(M::VerticalCenterPosition);
00142 }
00143 
00144 void MPopupListViewPrivate::_q_scrollOnFirstAppearance()
00145 {
00146     Q_Q(MPopupListView);
00147     list->scrollTo(q->model()->scrollToIndex());
00148     QObject::disconnect(list, SIGNAL(displayEntered()), q, SLOT(_q_scrollOnFirstAppearance()));
00149 }
00150 
00151 MPopupListView::MPopupListView(MPopupList *controller)
00152     : MDialogView(* new MPopupListViewPrivate, controller)
00153 {
00154     Q_D(MPopupListView);
00155     d->controller = controller;
00156 
00157     d->init();
00158 }
00159 
00160 MPopupListView::MPopupListView(MPopupListViewPrivate &dd, MPopupList *controller) :
00161     MDialogView(dd, controller)
00162 {
00163     Q_D(MPopupListView);
00164     d->controller = controller;
00165 
00166     d->init();
00167 }
00168 
00169 MPopupListView::~MPopupListView()
00170 {
00171 }
00172 
00173 void MPopupListView::updateData(const QList<const char *>& modifications)
00174 {
00175     MDialogView::updateData(modifications);
00176 
00177     Q_D(MPopupListView);
00178     foreach(const char* member, modifications) {
00179         if (member == MPopupListModel::ItemModel)
00180             d->list->setItemModel(model()->itemModel());
00181         else if (member == MPopupListModel::SelectionModel)
00182             d->list->setSelectionModel(model()->selectionModel());
00183         else if (member == MPopupListModel::ScrollToIndex)
00184             d->list->scrollTo(model()->scrollToIndex());
00185     }
00186 }
00187 
00188 void MPopupListView::setupModel()
00189 {
00190     Q_D(MPopupListView);
00191 
00192     MDialogView::setupModel();
00193 
00194     d->list->setItemModel(model()->itemModel());
00195     d->list->setSelectionModel(model()->selectionModel());
00196 }
00197 
00198 void MPopupListView::applyStyle()
00199 {
00200     Q_D(MPopupListView);
00201 
00202     MDialogView::applyStyle();
00203 
00204     d->cellCreator->setCellObjectName(style()->itemStyleName());
00205     contentsViewport()->setStyleName(style()->contentsViewportStyleName());
00206 }
00207 
00208 void MPopupListView::scrollTo(const QModelIndex &index) const
00209 {
00210     const_cast<MPopupListModel*>(model())->setScrollToIndex(index);
00211 }
00212 
00213 M_REGISTER_VIEW_NEW(MPopupListView, MPopupList)
00214 
00215 #include "moc_mpopuplistview.cpp"
00216 

Copyright © 2010 Nokia Corporation Generated on Thu Nov 4 2010 18:14:21 (PDT)
Doxygen 1.7.1
MeeGo Touch