aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/widgets/mpopuplist.cpp
blob: 98218c6a529bd6a3156a9e85c6786f6ed4705aef (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
/***************************************************************************
**
** 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 "mpopuplist.h"
#include "mpopuplist_p.h"

#include "mwidgetcreator.h"
M_REGISTER_WIDGET(MPopupList)

MPopupListPrivate::MPopupListPrivate()
{
}

MPopupList::MPopupList()
    : MDialog(new MPopupListPrivate(), new MPopupListModel(), MSceneWindow::PopupList)
{
    setCentralWidget(0);
}

MPopupList::MPopupList(MPopupListPrivate *dd, MPopupListModel *model, MSceneWindow::WindowType windowType)
    : MDialog(dd, model, windowType)
{
    setCentralWidget(0);
}

MPopupList::~MPopupList()
{
}

void MPopupList::setItemModel(QAbstractItemModel *itemModel)
{
    model()->setItemModel(itemModel);

    if (itemModel)
        model()->setSelectionModel(new QItemSelectionModel(itemModel, this));
    else
        model()->setSelectionModel(0);
}

QAbstractItemModel *MPopupList::itemModel() const
{
    return model()->itemModel();
}

void MPopupList::setSelectionModel(QItemSelectionModel *selectionModel)
{
    if (selectionModel && selectionModel->model() != itemModel()) {
        qWarning("MPopupList::setSelectionModel() failed: "
                 "Trying to set a selection model, which works on "
                 "a different model than the view.");
        return;
    }

    model()->setSelectionModel(selectionModel);
}

QItemSelectionModel *MPopupList::selectionModel() const
{
    return model()->selectionModel();
}

QModelIndex MPopupList::currentIndex() const
{
    if (!selectionModel() || !selectionModel()->hasSelection())
        return QModelIndex();

    return selectionModel()->currentIndex();
}

void MPopupList::scrollTo(const QModelIndex &index)
{
    if (index.model() != itemModel()) return;
    emit scrollToIndex(index);
    model()->setScrollToIndex(index);
}

void MPopupList::click(const QModelIndex &index)
{
    if (index.model() != itemModel()) return;

    if (index == currentIndex()) {
        emit clicked(index);
    } else {
        selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);

        emit clicked(index);
        emit currentIndexChanged(index);
    }

    accept();
}

void MPopupList::setCurrentIndex(const QModelIndex &index)
{
    if (itemModel() == NULL)
        return;

    if (index.isValid() && selectionModel()->model() != index.model()) {
        qWarning("MPopupList::selectItem() failed: "
                 "Trying to select an item that is for"
                 " a different model than the view ");
        return;
    }

    // Since we didn't change the current index, no new signals should not send
    if (index == currentIndex()) return;

    if (index.isValid())
        selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
    else
        selectionModel()->clear();

    emit currentIndexChanged(index);
}