aboutsummaryrefslogtreecommitdiff
path: root/demos/widgetsgallery/phonebookcell.cpp
blob: a6dafb26b2335af73fc3814e8d133508863b81c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it 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.
**
****************************************************************************/


#include "phonebookcell.h"

#include <MLabel>
#include <MLayout>

#include <MGridLayoutPolicy>
#include <MLinearLayoutPolicy>
#include <MImageWidget>
#include <MProgressIndicator>

PhoneBookCell::PhoneBookCell()
    : MListItem(),
    layout(NULL),
    landscapePolicy(NULL),
    portraitPolicy(NULL),
    landscapeTitleLabel(NULL),
    portraitTitleLabel(NULL),
    subtitleLabel(NULL),
    icon(NULL)
{
}

PhoneBookCell::~PhoneBookCell()
{
    if (icon)
        delete icon;
}

MLayout *PhoneBookCell::createLayout()
{
    setObjectName("BasicListItemIconWithTitleAndSubtitle");
    layout = new MLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    landscapePolicy = new MGridLayoutPolicy(layout);
    landscapePolicy->setContentsMargins(0, 0, 0, 0);
    landscapePolicy->setSpacing(0);

    // add to layout
    landscapePolicy->addItem(imageWidget(), 0, 0, 3, 1, Qt::AlignLeft | Qt::AlignVCenter);
    landscapePolicy->addItem(landscapeTitleLabelWidget(), 0, 1, Qt::AlignLeft | Qt::AlignTop);
    landscapePolicy->addItem(subtitleLabelWidget(), 1, 1, Qt::AlignLeft | Qt::AlignBottom);
    landscapePolicy->addItem(new QGraphicsWidget(this), 2, 1);

    portraitPolicy = new MLinearLayoutPolicy(layout, Qt::Horizontal);
    portraitPolicy->setContentsMargins(0, 0, 0, 0);
    portraitPolicy->setSpacing(0);

    portraitPolicy->addItem(imageWidget(), Qt::AlignLeft | Qt::AlignVCenter);
    portraitPolicy->addItem(portraitTitleLabelWidget(), Qt::AlignLeft | Qt::AlignVCenter);

    layout->setPortraitPolicy(portraitPolicy);
    layout->setLandscapePolicy(landscapePolicy);

    return layout;
}

MLabel *PhoneBookCell::landscapeTitleLabelWidget()
{
    if (!landscapeTitleLabel) {
        landscapeTitleLabel = new MLabel(this);
        landscapeTitleLabel->setTextElide(true);
        landscapeTitleLabel->setObjectName("CommonTitle");
    }
    return landscapeTitleLabel;
}

MLabel *PhoneBookCell::portraitTitleLabelWidget()
{
    if (!portraitTitleLabel) {
        // In portrait we have only one title, also object name should be different
        portraitTitleLabel = new MLabel(this);
        portraitTitleLabel->setTextElide(true);
        portraitTitleLabel->setObjectName("CommonSingleTitle");
    }
    return portraitTitleLabel;
}

MLabel *PhoneBookCell::subtitleLabelWidget()
{
    if (!subtitleLabel) {
        subtitleLabel = new MLabel(this);
        subtitleLabel->setTextElide(true);
        subtitleLabel->setObjectName("CommonSubTitle");
    }
    return subtitleLabel;
}

MImageWidget *PhoneBookCell::imageWidget()
{
    if (!icon) {
        // icon
        icon = new MImageWidget();
        icon->setObjectName("CommonMainIcon");
        icon->setImage("icon-m-content-avatar-placeholder");
    }
    return icon;
}


void PhoneBookCell::resizeEvent(QGraphicsSceneResizeEvent *event)
{
    MListItem::resizeEvent(event);

    if (!layout)
        setLayout(createLayout());
}

QString PhoneBookCell::title()
{
    return landscapeTitleLabelWidget()->text();
}

void PhoneBookCell::setTitle(const QString &title)
{
    landscapeTitleLabelWidget()->setText(title);
    portraitTitleLabelWidget()->setText(title);
}

QString PhoneBookCell::subtitle()
{
    return subtitleLabelWidget()->text();
}

void PhoneBookCell::setSubtitle(const QString &subtitle)
{
    subtitleLabelWidget()->setText(subtitle);
}

QImage PhoneBookCell::image()
{
    return imageWidget()->pixmap()->toImage();
}

void PhoneBookCell::setImage(const QImage &iconImage)
{
    if (!iconImage.isNull())
        imageWidget()->setImage(iconImage);
    else
        imageWidget()->setImage("icon-m-content-avatar-placeholder");
}