mapnik/demo/viewer/layerlistmodel.cpp

120 lines
3.4 KiB
C++
Raw Normal View History

2007-08-07 15:09:41 +00:00
/* This file is part of Mapnik (c++ mapping toolkit)
2011-10-23 14:09:12 +00:00
*
2017-05-05 11:02:01 +00:00
* Copyright (C) 2017 Artem Pavlenko
2007-08-07 15:09:41 +00:00
*
* 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
2016-01-26 09:54:42 +00:00
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2007-08-07 15:09:41 +00:00
*/
#include "layerlistmodel.hpp"
#include <QIcon>
#include <iostream>
#include <mapnik/layer.hpp>
using mapnik::Map;
LayerListModel::LayerListModel(std::shared_ptr<Map> map,QObject *parent)
2007-08-07 15:09:41 +00:00
: 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_)
2011-05-04 15:53:36 +00:00
return QVariant();
if (index.row() < 0 || index.row() >= int(map_->layers().size()))
2011-05-04 15:53:36 +00:00
return QVariant();
2007-08-07 15:09:41 +00:00
if (role == Qt::DisplayRole)
2011-05-04 15:53:36 +00:00
return QString(map_->layers().at(index.row()).name().c_str());
2007-08-07 15:09:41 +00:00
else if (role == Qt::DecorationRole)
{
2011-05-04 15:53:36 +00:00
double scale = map_->scale();
if (map_->layers().at(index.row()).visible(scale))
2011-05-04 15:53:36 +00:00
{
return QIcon(":/images/globe.png");
}
else
{
return QIcon(":/images/globe_bw.png");
}
2007-08-07 15:09:41 +00:00
}
else if (role == Qt::CheckStateRole)
{
if (map_->layers().at(index.row()).active())
2007-08-07 15:09:41 +00:00
return QVariant(Qt::Checked);
2011-10-23 14:09:12 +00:00
else
2007-08-07 15:09:41 +00:00
return QVariant(Qt::Unchecked);
}
else
{
2011-05-04 15:53:36 +00:00
return QVariant();
2007-08-07 15:09:41 +00:00
}
}
QVariant LayerListModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (role != Qt::DisplayRole)
2011-05-04 15:53:36 +00:00
return QVariant();
2011-10-23 14:09:12 +00:00
2007-08-07 15:09:41 +00:00
if (orientation == Qt::Horizontal)
2011-05-04 15:53:36 +00:00
return QString("TODO Column %1").arg(section);
2007-08-07 15:09:41 +00:00
else
2011-05-04 15:53:36 +00:00
return QString("TODO Row %1").arg(section);
2007-08-07 15:09:41 +00:00
}
bool LayerListModel::setData(const QModelIndex &index,
2011-05-04 15:53:36 +00:00
const QVariant &value, int role)
2007-08-07 15:09:41 +00:00
{
if (!map_) return false;
2011-10-23 14:09:12 +00:00
2007-08-07 15:09:41 +00:00
if (index.isValid() && role == Qt::CheckStateRole)
{
2011-10-23 14:09:12 +00:00
int status = value.toInt();
2009-12-16 20:02:06 +00:00
std::vector<mapnik::layer> & layers = const_cast<std::vector<mapnik::layer>& >(map_->layers());
layers.at(index.row()).set_active(status);
2007-08-07 15:09:41 +00:00
emit dataChanged(index, index);
return true;
2011-10-23 14:09:12 +00:00
}
2007-08-07 15:09:41 +00:00
return false;
}
Qt::ItemFlags LayerListModel::flags(QModelIndex const& index) const
{
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
2011-10-23 14:09:12 +00:00
2007-08-07 15:09:41 +00:00
if (index.isValid())
flags |= Qt::ItemIsUserCheckable;
return flags;
}
2009-12-16 20:02:06 +00:00
boost::optional<mapnik::layer&> LayerListModel::map_layer(int i)
{
if (map_)
{
2009-12-16 20:02:06 +00:00
std::vector<mapnik::layer> & layers = const_cast<std::vector<mapnik::layer>& >(map_->layers());
if (i < int(layers.size()))
2011-05-04 15:53:36 +00:00
return boost::optional<mapnik::layer&>(layers[i]);
}
2009-12-16 20:02:06 +00:00
return boost::optional<mapnik::layer&>();
}