File: layerlistmodel.cpp

package info (click to toggle)
mapnik 4.2.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,656 kB
  • sloc: cpp: 163,870; python: 1,332; sh: 690; xml: 161; makefile: 123; perl: 28; lisp: 13
file content (123 lines) | stat: -rw-r--r-- 3,612 bytes parent folder | download
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
/* This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2025 Artem Pavlenko
 *
 * Mapnik is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "layerlistmodel.hpp"
#include <QIcon>
#include <QBrush>
#include <mapnik/layer.hpp>

using mapnik::Map;

LayerListModel::LayerListModel(std::shared_ptr<Map> map, QObject* parent)
    : QAbstractListModel(parent),
      map_(map)
{}

int LayerListModel::rowCount(QModelIndex const&) const
{
    if (map_)
        return map_->layers().size();
    return 0;
}

QVariant LayerListModel::data(QModelIndex const& index, int role) const
{
    if (!index.isValid() || !map_)
        return QVariant();
    if (index.row() < 0 || index.row() >= int(map_->layers().size()))
        return QVariant();
    if (role == Qt::DisplayRole)
        return QString(map_->layers().at(index.row()).name().c_str());
    else if (role == Qt::DecorationRole)
    {
        double scale = map_->scale();
        if (map_->layers().at(index.row()).visible(scale))
        {
            return QIcon(":/images/globe.png");
        }
        else
        {
            return QIcon(":/images/globe_bw.png");
        }
    }
    else if (role == Qt::CheckStateRole)
    {
        if (map_->layers().at(index.row()).active())
            return QVariant(Qt::Checked);
        else
            return QVariant(Qt::Unchecked);
    }
    else if (role == Qt::ForegroundRole)
    {
        if (map_->layers().at(index.row()).active())
            return QBrush(QColor("black"));
        else
            return QBrush(QColor("lightgrey"));
    }
    else
    {
        return QVariant();
    }
}

QVariant LayerListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal)
        return QString("TODO Column %1").arg(section);
    else
        return QString("TODO Row %1").arg(section);
}

bool LayerListModel::setData(QModelIndex const& index, QVariant const& value, int role)
{
    if (!map_)
        return false;

    if (index.isValid() && role == Qt::CheckStateRole)
    {
        int status = value.toInt();
        std::vector<mapnik::layer>& layers = const_cast<std::vector<mapnik::layer>&>(map_->layers());
        layers.at(index.row()).set_active(status);
        emit dataChanged(index, index);
        return true;
    }
    return false;
}

Qt::ItemFlags LayerListModel::flags(QModelIndex const& index) const
{
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    if (index.isValid())
        flags |= Qt::ItemIsUserCheckable;
    return flags;
}

boost::optional<mapnik::layer&> LayerListModel::map_layer(int i)
{
    if (map_)
    {
        std::vector<mapnik::layer>& layers = const_cast<std::vector<mapnik::layer>&>(map_->layers());
        if (i < int(layers.size()))
            return boost::optional<mapnik::layer&>(layers[i]);
    }
    return boost::optional<mapnik::layer&>();
}