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
|
|
|
*
|
2011-10-23 13:04:25 +00:00
|
|
|
* Copyright (C) 2011 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
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//$Id$
|
|
|
|
|
|
|
|
|
|
|
|
#include "layerlistmodel.hpp"
|
|
|
|
#include <QIcon>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <mapnik/layer.hpp>
|
|
|
|
|
|
|
|
using mapnik::Map;
|
|
|
|
|
|
|
|
LayerListModel::LayerListModel(boost::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_)
|
2011-05-04 15:53:36 +00:00
|
|
|
return QVariant();
|
2009-12-21 13:44:56 +00:00
|
|
|
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()).isVisible(scale))
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2011-05-04 15:53:36 +00:00
|
|
|
if (map_->layers().at(index.row()).isActive())
|
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());
|
2007-08-07 15:09:41 +00:00
|
|
|
layers.at(index.row()).setActive(status);
|
|
|
|
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)
|
2007-08-13 09:13:58 +00:00
|
|
|
{
|
|
|
|
if (map_)
|
|
|
|
{
|
2009-12-16 20:02:06 +00:00
|
|
|
std::vector<mapnik::layer> & layers = const_cast<std::vector<mapnik::layer>& >(map_->layers());
|
2009-12-21 13:44:56 +00:00
|
|
|
if (i < int(layers.size()))
|
2011-05-04 15:53:36 +00:00
|
|
|
return boost::optional<mapnik::layer&>(layers[i]);
|
2007-08-13 09:13:58 +00:00
|
|
|
}
|
2009-12-16 20:02:06 +00:00
|
|
|
return boost::optional<mapnik::layer&>();
|
2007-08-13 09:13:58 +00:00
|
|
|
}
|
|
|
|
|
2007-08-07 15:09:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-23 14:09:12 +00:00
|
|
|
|