mapnik/demo/viewer/layerlistmodel.cpp

125 lines
3.4 KiB
C++
Raw Normal View History

2007-08-07 17:09:41 +02:00
/* This file is part of Mapnik (c++ mapping toolkit)
* Copyright (C) 2011 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
* 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 17:53:36 +02:00
return QVariant();
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();
if (map_->layers().at(index.row()).isVisible(scale))
{
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)
{
2011-05-04 17:53:36 +02:00
if (map_->layers().at(index.row()).isActive())
2007-08-07 17:09:41 +02:00
return QVariant(Qt::Checked);
2011-05-04 17:53:36 +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();
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;
if (index.isValid() && role == Qt::CheckStateRole)
{
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());
2007-08-07 17:09:41 +02:00
layers.at(index.row()).setActive(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;
}
2009-12-16 21:02:06 +01:00
boost::optional<mapnik::layer&> LayerListModel::map_layer(int i)
{
if (map_)
{
2009-12-16 21:02:06 +01:00
std::vector<mapnik::layer> & layers = const_cast<std::vector<mapnik::layer>& >(map_->layers());
if (i < int(layers.size()))
2011-05-04 17:53:36 +02:00
return boost::optional<mapnik::layer&>(layers[i]);
}
2009-12-16 21:02:06 +01:00
return boost::optional<mapnik::layer&>();
}
2007-08-07 17:09:41 +02:00