root/trunk/demo/viewer/layerlistmodel.cpp

Revision 1501, 3.2 kB (checked in by artem, 7 months ago)

+ fix compiler warnings (signed/unsigned comp)

Line 
1/* This file is part of Mapnik (c++ mapping toolkit)
2 * Copyright (C) 2007 Artem Pavlenko
3 *
4 * Mapnik is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18
19//$Id$
20
21
22#include "layerlistmodel.hpp"
23#include <QIcon>
24
25#include <iostream>
26#include <mapnik/layer.hpp>
27
28using mapnik::Map;
29
30LayerListModel::LayerListModel(boost::shared_ptr<Map> map,QObject *parent)
31    : QAbstractListModel(parent),
32      map_(map) {}
33
34int LayerListModel::rowCount(QModelIndex const&) const
35{
36   if (map_) return map_->layers().size();
37   return 0;
38}
39
40QVariant LayerListModel::data(QModelIndex const& index,int role) const
41{
42    if (!index.isValid() || !map_)
43        return QVariant();
44    if (index.row() < 0 || index.row() >= int(map_->layers().size()))
45        return QVariant();
46    if (role == Qt::DisplayRole)
47        return QString(map_->layers().at(index.row()).name().c_str());
48    else if (role == Qt::DecorationRole)
49    {
50        double scale = map_->scale();
51        if (map_->layers().at(index.row()).isVisible(scale))
52        {
53            return QIcon(":/images/globe.png");
54        }
55        else
56        {
57            return QIcon(":/images/globe_bw.png");
58        }
59    }
60    else if (role == Qt::CheckStateRole)
61    {
62        if (map_->layers().at(index.row()).isActive())
63           return QVariant(Qt::Checked);
64        else 
65           return QVariant(Qt::Unchecked);
66    }
67    else
68    {
69        return QVariant();
70    }
71}
72
73QVariant LayerListModel::headerData(int section, Qt::Orientation orientation,
74                                         int role) const
75{
76    if (role != Qt::DisplayRole)
77        return QVariant();
78   
79    if (orientation == Qt::Horizontal)
80        return QString("TODO Column %1").arg(section);
81    else
82        return QString("TODO Row %1").arg(section);
83}
84
85bool LayerListModel::setData(const QModelIndex &index,
86                             const QVariant &value, int role)
87{
88   if (!map_) return false;
89   
90   if (index.isValid() && role == Qt::CheckStateRole)
91   {
92      int status = value.toInt(); 
93      std::vector<mapnik::layer> & layers = const_cast<std::vector<mapnik::layer>& >(map_->layers());
94      layers.at(index.row()).setActive(status);
95      emit dataChanged(index, index);
96      return true;
97   } 
98   return false;
99}
100
101Qt::ItemFlags LayerListModel::flags(QModelIndex const& index) const
102{
103    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
104   
105    if (index.isValid())
106       flags |= Qt::ItemIsUserCheckable;
107    return flags;
108}
109
110boost::optional<mapnik::layer&> LayerListModel::map_layer(int i)
111{
112   if (map_)
113   {
114      std::vector<mapnik::layer> & layers = const_cast<std::vector<mapnik::layer>& >(map_->layers());
115      if (i < int(layers.size()))
116          return boost::optional<mapnik::layer&>(layers[i]);
117   }
118   return boost::optional<mapnik::layer&>();
119}
120
121
122
123
124 
Note: See TracBrowser for help on using the browser.