Fixing: 273759 - <MemLeak>memory leak observed in accounts-ui when trying to configur...
[accounts-sso:libaccounts-ui.git] / lib / AccountsUI / basic-item-widget.cpp
1 /*
2  * This file is part of accounts-ui
3  *
4  * Copyright (C) 2009-2011 Nokia Corporation.
5  *
6  * Contact: Tomi Suviola <tomi.suviola@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22
23 #include "basic-item-widget.h"
24
25 #include <MImageWidget>
26 #include <MLabel>
27 #include <MLayout>
28 #include <MLinearLayoutPolicy>
29 #include <MLocale>
30
31 //Qt
32 #include <QDebug>
33
34 namespace AccountsUI {
35
36 class BasicItemWidgetPrivate: public QObject
37 {
38 public:
39     BasicItemWidgetPrivate()
40         :image(0),
41         title(0),
42         subtitle(0)
43     {}
44
45     ~BasicItemWidgetPrivate() {}
46
47 public:
48     MImageWidget *image;
49     MLabel *title;
50     MLabel *subtitle;
51     BasicItemWidget::BasicItemWidgetType type;
52 };
53
54 BasicItemWidget::BasicItemWidget(BasicItemWidgetType type, QGraphicsItem *parent)
55         :MWidgetController(parent),
56         d_ptr(new BasicItemWidgetPrivate())
57 {
58     Q_D(BasicItemWidget);
59     d->type = type;
60 }
61
62 BasicItemWidget::~BasicItemWidget()
63 {
64     delete d_ptr;
65 }
66
67 void BasicItemWidget::createLayout()
68 {
69     Q_D(BasicItemWidget);
70
71     MLayout *mainLayout = new MLayout();
72     mainLayout->setContentsMargins(0, 0, 0, 0);
73     mainLayout->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
74     MLinearLayoutPolicy *mainLayoutPolicy = new MLinearLayoutPolicy(mainLayout, Qt::Horizontal);
75
76     MLayout *titleLayout = new MLayout();
77     MLinearLayoutPolicy *titleLayoutPolicy = new MLinearLayoutPolicy(titleLayout, Qt::Vertical);
78
79     switch(d->type) {
80     case TitleAndSubtitle:
81         d->title = new MLabel();
82         d->title->setStyleName("CommonTitleInverted");
83         d->title->setTextElide(true);
84         d->subtitle = new MLabel();
85         d->subtitle->setStyleName("CommonSubTitleInverted");
86         d->subtitle->setWordWrap(true);
87         d->subtitle->setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
88         d->subtitle->setTextElide(false);
89         d->subtitle->setPreferredWidth(QFontMetrics(d->subtitle->font()).height() * 1000);
90         titleLayoutPolicy->addItem(d->title, Qt::AlignLeft | Qt::AlignTop);
91         titleLayoutPolicy->addItem(d->subtitle, Qt::AlignLeft | Qt::AlignTop);
92         break;
93
94     case IconWithTitle:
95         d->image = new MImageWidget();
96         d->image->setStyleName("CommonMainIcon");
97         d->title = new MLabel();
98         d->title->setStyleName("CommonTitleInverted");
99         d->title->setTextElide(true);
100         titleLayoutPolicy->addItem(d->title, Qt::AlignLeft | Qt::AlignTop);
101         mainLayoutPolicy->addItem(d->image, Qt::AlignCenter);
102         break;
103
104     case IconWithTitleAndSubTitle:
105         d->image = new MImageWidget();
106         d->image->setStyleName("CommonMainIcon");
107         d->title = new MLabel();
108         d->title->setStyleName("CommonTitleInverted");
109         d->title->setTextElide(true);
110         d->subtitle = new MLabel();
111         d->subtitle->setStyleName("CommonSubTitleInverted");
112         d->subtitle->setWordWrap(true);
113         d->subtitle->setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
114         d->subtitle->setTextElide(false);
115         titleLayoutPolicy->addItem(d->title, Qt::AlignLeft | Qt::AlignTop);
116         titleLayoutPolicy->addItem(d->subtitle, Qt::AlignLeft | Qt::AlignTop);
117         mainLayoutPolicy->addItem(d->image, Qt::AlignCenter);
118         break;
119     }
120
121     mainLayoutPolicy->addItem(titleLayout, Qt::AlignLeft | Qt::AlignCenter);
122     mainLayoutPolicy->addItem(new QGraphicsWidget());
123
124     setLayout(mainLayout);
125     updateGeometry();
126 }
127
128 void BasicItemWidget::setTitle(const QString &titleText)
129 {
130     Q_D(BasicItemWidget);
131     if (d->title)
132         d->title->setText(titleText);
133 }
134
135 void BasicItemWidget::setSubtitle(const QString &subtitleText)
136 {
137     Q_D(BasicItemWidget);
138     if (d->subtitle) {
139         d->subtitle->setText(subtitleText);
140         if(MLocale::directionForText(subtitleText) == Qt::RightToLeft)
141             d->subtitle->setAlignment(Qt::AlignRight | Qt::AlignAbsolute);
142         else
143             d->subtitle->setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);
144     }
145 }
146
147 void BasicItemWidget::setImage(const QString &imageId)
148 {
149     Q_D(BasicItemWidget);
150     if (d->image)
151         d->image->setImage(imageId);
152 }
153
154 } // namespace