aboutsummaryrefslogtreecommitdiff
path: root/src/views/mpopuplistview.cpp
blob: 516f470ae633637e6f976f48934e432055031383 (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
/***************************************************************************
**
** 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 "mpopuplistview.h"
#include "mpopuplistview_p.h"

#include "mpopuplist.h"
#include "mlist.h"
#include "mgriditem.h"
#include "mpannableviewport.h"

#include <QGraphicsLinearLayout>

MPopupListViewPrivate::MPopupListViewPrivate()
    : controller(0), list(0)
{
}

MPopupListViewPrivate::~MPopupListViewPrivate()
{
}

void MPopupListViewPrivate::init()
{
    Q_Q(MPopupListView);

    list = new MList();
    list->setCellCreator(this);
    list->setSelectionMode(MList::SingleSelection);
    q->contentsLayout()->insertItem(0, list);

    contentsViewport->setObjectName("MPopupListContentsViewport");

    QObject::connect(list, SIGNAL(itemClicked(QModelIndex)), controller, SLOT(click(QModelIndex)));
    QObject::connect(list, SIGNAL(displayEntered()), q, SLOT(_q_scrollOnFirstAppearance()));
}

void MPopupListViewPrivate::updateCell(const QModelIndex& index, MWidget * cell) const
{
    MPopupListItem* item = static_cast<MPopupListItem*>(cell);

    QString title;
    QPixmap* pixmap = NULL;
    QVariant value;

    value = list->itemModel()->data(index, Qt::DisplayRole);
    if (value != QVariant())
        title = value.toString();

    value = list->itemModel()->data(index, Qt::DecorationRole);
    if (value != QVariant()) {
        // TODO: Use MTheme::pixmap() when MContentItem starts to support
        //       pixmaps that are loaded asynchronously
        pixmap = MTheme::pixmapCopy(value.toString());
    }

    item->setTitle(title);
    item->setPixmap(pixmap ? *pixmap : QPixmap());
    delete pixmap;

    if (list->selectionModel()->isSelected(index))
        item->setSelected(true);

    if (index.row() == 0) {
        item->setItemMode(MContentItem::SingleColumnTop);
    } else if (index.row() == index.model()->rowCount() - 1) {
        item->setItemMode(MContentItem::SingleColumnBottom);
    } else {
        item->setItemMode(MContentItem::SingleColumnCenter);
    }
}

void MPopupListViewPrivate::_q_scrollOnFirstAppearance()
{
    Q_Q(MPopupListView);
    list->scrollTo(q->model()->scrollToIndex());
    QObject::disconnect(list, SIGNAL(displayEntered()), q, SLOT(_q_scrollOnFirstAppearance()));
}

MPopupListView::MPopupListView(MPopupList *controller)
    : MDialogView(* new MPopupListViewPrivate, controller)
{
    Q_D(MPopupListView);
    d->controller = controller;

    d->init();
}

MPopupListView::MPopupListView(MPopupListViewPrivate &dd, MPopupList *controller) :
    MDialogView(dd, controller)
{
    Q_D(MPopupListView);
    d->controller = controller;

    d->init();
}

MPopupListView::~MPopupListView()
{
}

void MPopupListView::updateData(const QList<const char *>& modifications)
{
    MDialogView::updateData(modifications);

    Q_D(MPopupListView);
    foreach(const char* member, modifications) {
        if (member == MPopupListModel::ItemModel)
            d->list->setItemModel(model()->itemModel());
        else if (member == MPopupListModel::SelectionModel)
            d->list->setSelectionModel(model()->selectionModel());
        else if (member == MPopupListModel::ScrollToIndex)
            d->list->scrollTo(model()->scrollToIndex());
    }
}

void MPopupListView::setupModel()
{
    MDialogView::setupModel();

    Q_D(MPopupListView);
    d->list->setItemModel(model()->itemModel());
    d->list->setSelectionModel(model()->selectionModel());
}

void MPopupListView::scrollTo(const QModelIndex &index) const
{
    const_cast<MPopupListModel*>(model())->setScrollToIndex(index);
}

M_REGISTER_VIEW_NEW(MPopupListView, MPopupList)

#include "moc_mpopuplistview.cpp"