Home · All Classes · Main Classes · Deprecated

mcontainerview.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 <QGraphicsLinearLayout>
00021 #include <QGraphicsSceneMouseEvent>
00022 #include <MLabel>
00023 #include <MImageWidget>
00024 #include <MSeparator>
00025 #include <MProgressIndicator>
00026 
00027 #include <QCoreApplication>
00028 #include <QSizePolicy>
00029 
00030 #include "mcontainerview.h"
00031 #include "mcontainerview_p.h"
00032 #include "mcontainerheader_p.h"
00033 #include "mcontainer.h"
00034 
00035 #include "mscalableimage.h"
00036 #include "mviewcreator.h"
00037 #include "mtheme.h"
00038 
00039 /*
00040  * layout:
00041  *
00042  *   + controller +
00043  *                |
00044  *                +- mainLayout +- header -+- headerLayout  +- icon
00045  *                              |                           |
00046  *                              |                           +- title
00047  *                              +- centralWidget            |
00048  *                                                          +- text
00049  *                                                          |
00050  *                                                          +- spinner
00051  */
00052 
00053 MContainerViewPrivate::MContainerViewPrivate()
00054     : controller(0)
00055     , headerLayout(0)
00056     , header(0)
00057     , icon(0) // created only if needed
00058     , title(0)
00059     , text(0)
00060     , background(0)
00061     , headerPressed(false)
00062     , progressIndicator(0)
00063     , q_ptr(0)
00064 {
00065 }
00066 
00067 MContainerViewPrivate::~MContainerViewPrivate()
00068 {
00069     delete icon;
00070 }
00071 
00072 void MContainerViewPrivate::init()
00073 {
00074     controller->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
00075 
00076     QGraphicsLinearLayout* mainLayout = new QGraphicsLinearLayout(Qt::Vertical);
00077 
00078     // pack
00079     mainLayout->setContentsMargins(0, 0, 0, 0);
00080     mainLayout->setSpacing(0);
00081     mainLayout->addItem(controller->centralWidget());
00082     controller->setLayout(mainLayout);
00083 }
00084 
00085 /*
00086  * creates the header by adding a header widget, a layout and
00087  * labels
00088  */
00089 
00090 void MContainerViewPrivate::createHeader()
00091 {
00092     if (!header) {
00093         header = new MContainerHeader();
00094 
00095         headerLayout = new QGraphicsLinearLayout(Qt::Horizontal);
00096         headerLayout->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
00097         header->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
00098 
00099         headerLayout->setContentsMargins(0, 0, 0, 0);
00100         header->setLayout(headerLayout);
00101 
00102         title = new MLabel();
00103         text = new MLabel();
00104 
00105         title->setObjectName("MContainerTitle");
00106         text->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
00107 
00108         headerLayout->addItem(title);
00109         headerLayout->setAlignment(title,  Qt::AlignVCenter | Qt::AlignLeft);
00110         title->show();
00111 
00112         headerLayout->addItem(text);
00113         headerLayout->setAlignment(text,  Qt::AlignVCenter | Qt::AlignRight);
00114         text->show();
00115 
00116         // insert icon if available
00117         if (icon) {
00118             headerLayout->insertItem(0, icon);
00119             headerLayout->setAlignment(icon,  Qt::AlignVCenter | Qt::AlignLeft);
00120             icon->show();
00121         }
00122 
00123         QGraphicsLinearLayout* mainLayout = dynamic_cast<QGraphicsLinearLayout*>(controller->layout());
00124         
00125         if (mainLayout)
00126             mainLayout->insertItem(0, header);
00127     }
00128 }
00129 
00130 /*
00131  * delete header, mainLayout will now reuse the space previously
00132  * demanded by the header
00133  */
00134 
00135 void MContainerViewPrivate::removeHeader()
00136 {
00137     if (header) {
00138         /*
00139            delete header and its children, the header must be delted with
00140            deleteLater() as this gets called from a slot.
00141         */
00142         header->deleteLater();
00143         header = 0;
00144         title = 0;
00145         text = 0;
00146         icon = 0;
00147         progressIndicator = 0;
00148         headerLayout = 0;
00149     }
00150 }
00151 
00152 /*
00153  * create icon if it doesn't exist
00154  */
00155 
00156 void MContainerViewPrivate::setupIcon(const QSize &size)
00157 {
00158     if (!icon)
00159         icon = new MImageWidget;
00160 
00161     icon->setImage(controller->model()->icon(), size);
00162     icon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00163 
00164     // insert it if header is available
00165     if (header) {
00166         headerLayout->insertItem(0, icon);
00167         headerLayout->setAlignment(icon,  Qt::AlignVCenter | Qt::AlignLeft);
00168         icon->show();
00169     }
00170 }
00171 
00172 void MContainerViewPrivate::createProgressIndicator()
00173 {
00174     if (!progressIndicator)
00175         progressIndicator = new MProgressIndicator;
00176     progressIndicator->setUnknownDuration(true);
00177 }
00178 
00179 
00180 void MContainerViewPrivate::layoutProgressIndicator()
00181 {
00182     if (header) {
00183         headerLayout->addItem(progressIndicator);
00184         headerLayout->setAlignment(progressIndicator,  Qt::AlignVCenter | Qt::AlignRight);
00185         progressIndicator->show();
00186     }
00187 }
00188 
00189 MContainerView::MContainerView(MContainer *controller) :
00190     MWidgetView(*new MContainerViewPrivate, controller)
00191 {
00192     Q_D(MContainerView);
00193     d->controller = controller;
00194 
00195     // setup main infrastructure
00196     d->init();
00197 
00198     connect(this, SIGNAL(headerClicked()), controller, SIGNAL(headerClicked()));
00199 }
00200 
00201 MContainerView::MContainerView(MContainerViewPrivate &dd, MContainer *controller) :
00202     MWidgetView(dd, controller)
00203 {
00204     Q_D(MContainerView);
00205     d->controller = controller;
00206 
00207     d->init();
00208 
00209     connect(this, SIGNAL(headerClicked()), controller, SIGNAL(headerClicked()));
00210 }
00211 
00212 MContainerView::~MContainerView()
00213 {
00214 }
00215 
00216 void MContainerView::drawBackground(QPainter *painter, const QStyleOptionGraphicsItem *option) const
00217 {
00218     Q_UNUSED(option);
00219     Q_D(const MContainerView);
00220 
00221     // draw the background
00222     qreal oldOpacity = painter->opacity();
00223     painter->setOpacity(style()->backgroundOpacity() * effectiveOpacity());
00224     if (style()->backgroundImage()) {
00225         const QSize _size = d->controller->centralWidget()->geometry().size().toSize();
00226         const qreal headerHeight = d->header ? d->header->size().height() : 0;
00227         style()->backgroundImage()->draw(0, headerHeight, _size.width(), _size.height(), painter);
00228     }
00229     painter->setOpacity(oldOpacity);
00230 }
00231 
00232 void MContainerView::applyStyle()
00233 {
00234     Q_D(MContainerView);
00235 
00236     MWidgetView::applyStyle();
00237 
00238     //  get background from style
00239     d->background = style()->backgroundImage();
00240 }
00241 
00242 void MContainerView::updateData(const QList<const char *>& modifications)
00243 {
00244     Q_D(MContainerView);
00245     MWidgetView::updateData(modifications);
00246 
00247     const MContainerModel *model = MContainerView::model();
00248 
00249     // make sure the header exists before icons or centralWidget are set
00250     // or modified
00251     if (model->headerVisible()) {
00252         d->createHeader();
00253 
00254         QObject::connect(d->header, SIGNAL(pressed()), this, SLOT(headerPressed()));
00255         QObject::connect(d->header, SIGNAL(released()), this, SLOT(headerReleased()));
00256         QObject::connect(d->header, SIGNAL(moved()), this, SLOT(headerMoved()));
00257         QObject::connect(d->header, SIGNAL(canceled()), this, SLOT(headerCanceled()));
00258 
00259         // update labels
00260         d->title->setText(model->title());
00261         d->text->setText(model->text());
00262 
00263         // make sure the icon is present if headerVisible() changed at runtime
00264         if (!model->icon().isEmpty())
00265             d->setupIcon(style()->iconSize());
00266 
00267         // same for the progress indicator
00268         if (model->progressIndicatorVisible()) {
00269             d->createProgressIndicator();
00270             d->layoutProgressIndicator();
00271         }
00272 
00273     } else {
00274         d->removeHeader();
00275     }
00276 
00277     const char *member;
00278     foreach(member, modifications) {
00279         if (member == MContainerModel::CentralWidget) {
00280             QGraphicsLinearLayout* mainLayout = dynamic_cast<QGraphicsLinearLayout*>(d->controller->layout());
00281             if (mainLayout) {
00282                 // Remove the old central widget but don't destroy since the controller does it
00283                 mainLayout->removeAt(mainLayout->count() - 1);
00284                 // Add the new central widget
00285                 mainLayout->addItem(d->controller->centralWidget());
00286             }
00287             // icon value changed
00288         } else if (member == MContainerModel::Icon) {
00289             // set new icon
00290             if (!model->icon().isEmpty()) {
00291                 d->setupIcon(style()->iconSize());
00292             } else {
00293                 d->headerLayout->removeItem(d->icon);
00294                 d->icon->hide();
00295             }
00296         } else if (member == MContainerModel::ProgressIndicatorVisible) {
00297             if (model->progressIndicatorVisible()) {
00298                 d->createProgressIndicator();
00299                 d->layoutProgressIndicator();
00300             } else {
00301                 delete d->progressIndicator;
00302                 d->progressIndicator = 0;
00303             }
00304         }
00305     }
00306 
00307     // update visuals
00308     update();
00309 }
00310 
00311 void MContainerView::setupModel()
00312 {
00313     Q_D(MContainerView);
00314     MWidgetView::setupModel();
00315 
00316     // initially creating the header if defined in the model
00317     if (model()->headerVisible()) {
00318         d->createHeader();
00319 
00320         QObject::connect(d->header, SIGNAL(pressed()), this, SLOT(headerPressed()));
00321         QObject::connect(d->header, SIGNAL(released()), this, SLOT(headerReleased()));
00322 
00323         d->title->setText(model()->title());
00324         d->text->setText(model()->text());
00325 
00326         // create icon if it is set to be shown by the model
00327         if (!model()->icon().isEmpty())
00328             d->setupIcon(style()->iconSize());
00329 
00330     }
00331 
00332     int spacing = style()->internalItemSpacing();
00333     int margin = style()->internalMargins();
00334 
00335     // In case of strange or missing values while reading css
00336     if (spacing < 0 || spacing > 100)
00337         spacing = 0;
00338     if (margin < 0 || margin > 100)
00339         margin = 0;
00340 
00341     QGraphicsLinearLayout* mainLayout = dynamic_cast<QGraphicsLinearLayout*>(d->controller->layout());
00342     if (mainLayout) {
00343         mainLayout->setContentsMargins(margin, margin, margin, margin);
00344         mainLayout->setSpacing(spacing);
00345         mainLayout->addItem(d->controller->centralWidget());
00346     }
00347 
00348     if (model()->progressIndicatorVisible()) {
00349         d->createProgressIndicator();
00350         d->layoutProgressIndicator();
00351     }
00352 }
00353 
00354 void MContainerView::headerMoved()
00355 {
00356     Q_D(MContainerView);
00357 
00358     if (d->headerPressed) {
00359         d->headerPressed = false;
00360         update();
00361     }
00362 }
00363 
00364 void MContainerView::headerCanceled()
00365 {
00366     Q_D(MContainerView);
00367 
00368     if (d->headerPressed) {
00369         d->headerPressed = false;
00370         update();
00371     }
00372 }
00373 
00374 void MContainerView::headerPressed()
00375 {
00376     Q_D(MContainerView);
00377     d->headerPressed = true;
00378 }
00379 
00380 void MContainerView::headerReleased()
00381 {
00382     Q_D(MContainerView);
00383 
00384     if (d->headerPressed) {
00385         emit headerClicked();
00386         d->headerPressed = false;
00387         update();
00388     }
00389 }
00390 
00391 // bind view and controller together
00392 M_REGISTER_VIEW_NEW(MContainerView, MContainer)

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