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