aboutsummaryrefslogtreecommitdiff
path: root/demos/widgetsgallery/maincategorypage.cpp
blob: dc3f23d1b95ce8d71754ad37dbd91edb65f17dc0 (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
#include "maincategorypage.h"
#include "mainpage.h"

#include <MAbstractCellCreator>
#include <MBasicListItem>
#include <MLayout>
#include <MLinearLayoutPolicy>
#include <MList>

class WidgetsGalleryCategoryDataModel : public QAbstractListModel {
public:
    WidgetsGalleryCategoryDataModel(QAbstractItemModel *parentModel, const QModelIndex &parentIndex)
        : QAbstractListModel(), widgetsGalleryModel(parentModel), categoryIndex(parentIndex) {

    }

    QModelIndex parent(const QModelIndex &child) const {
        Q_UNUSED(child);

        return categoryIndex;
    }

    int rowCount(const QModelIndex &index) const {
        Q_UNUSED(index);

        return widgetsGalleryModel->rowCount(categoryIndex);
    }

    QVariant data(const QModelIndex &index, int role) const {
        return widgetsGalleryModel->data(index, role);
    }
private:
    QAbstractItemModel *widgetsGalleryModel;
    QModelIndex categoryIndex;
};

class WidgetGalleryCellCreator : public MAbstractCellCreator<MBasicListItem>
{
public:
    WidgetGalleryCellCreator() : MAbstractCellCreator<MBasicListItem>() {
    }

    MWidget *createCell(const QModelIndex &index, MWidgetRecycler &recycler) const {
        Q_UNUSED(index);

        MBasicListItem *cell = dynamic_cast<MBasicListItem *>(recycler.take(MBasicListItem::staticMetaObject.className()));
        if (cell == NULL) {
            cell = new MBasicListItem(MBasicListItem::SingleTitle);
            cell->setLayoutPosition(M::CenterPosition);
        }
        updateCell(index, cell);

        return cell;
    }

    void updateCell(const QModelIndex &index, MWidget *cell) const {
        MBasicListItem *item = qobject_cast<MBasicListItem*>(cell);
        if(!item)
            return;

        item->setTitle(index.data().toString());
    }
};

MainCategoryPage::MainCategoryPage(QAbstractItemModel *demosDataModel, const QModelIndex &parentIndex) :
        dataModel(new WidgetsGalleryCategoryDataModel(demosDataModel, parentIndex))
{
}

QString MainCategoryPage::timedemoTitle()
{
    return "MainCategoryPage";
}

void MainCategoryPage::createContent()
{
    TimedemoPage::createContent();

    QGraphicsWidget *panel = centralWidget();

    MLayout *layout = new MLayout(panel);
    layout->setContentsMargins(0, 0, 0, 0);
    panel->setLayout(layout);
    policy = new MLinearLayoutPolicy(layout, Qt::Vertical);
    policy->setSpacing(0);

    populateLayout();
}

void MainCategoryPage::populateLayout()
{
    list = new MList(centralWidget());
    list->setCellCreator(new WidgetGalleryCellCreator());
    list->setItemModel(dataModel);

    policy->addItem(list, Qt::AlignCenter);

    connect(list, SIGNAL(itemClicked(QModelIndex)), this, SLOT(galleryPageItemClicked(QModelIndex)));
}

void MainCategoryPage::galleryPageItemClicked(const QModelIndex &index)
{
    TemplatePage *page = static_cast<TemplatePage *>(index.data(MainPage::Page).value<void *>());
    page->setParent(this);
    page->appear(scene());
}