mapnik/demo/viewer/layerlistmodel.cpp

124 lines
3.5 KiB
C++
Raw Normal View History

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
*
2024-07-22 11:20:47 +02:00
* Copyright (C) 2024 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 <QBrush>
2007-08-07 17:09:41 +02:00
#include <mapnik/layer.hpp>
using mapnik::Map;
2022-01-26 23:34:08 +01:00
LayerListModel::LayerListModel(std::shared_ptr<Map> map, QObject* parent)
: QAbstractListModel(parent)
, map_(map)
{}
2007-08-07 17:09:41 +02:00
int LayerListModel::rowCount(QModelIndex const&) const
{
2022-01-26 23:34:08 +01:00
if (map_)
return map_->layers().size();
return 0;
2007-08-07 17:09:41 +02:00
}
QVariant LayerListModel::data(QModelIndex const& index, int role) const
2007-08-07 17:09:41 +02:00
{
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()).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)
{
if (map_->layers().at(index.row()).active())
2022-01-26 23:34:08 +01:00
return QVariant(Qt::Checked);
2011-10-23 16:09:12 +02:00
else
2022-01-26 23:34:08 +01:00
return QVariant(Qt::Unchecked);
2007-08-07 17:09:41 +02:00
}
else if (role == Qt::ForegroundRole)
{
if (map_->layers().at(index.row()).active())
return QBrush(QColor("black"));
else
return QBrush(QColor("lightgrey"));
}
2007-08-07 17:09:41 +02:00
else
{
2011-05-04 17:53:36 +02:00
return QVariant();
2007-08-07 17:09:41 +02:00
}
}
2022-01-26 23:34:08 +01:00
QVariant LayerListModel::headerData(int section, Qt::Orientation orientation, int role) const
2007-08-07 17:09:41 +02:00
{
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
}
2022-01-26 23:34:08 +01:00
bool LayerListModel::setData(const QModelIndex& index, const QVariant& value, int role)
2007-08-07 17:09:41 +02:00
{
2022-01-26 23:34:08 +01:00
if (!map_)
return false;
2011-10-23 16:09:12 +02:00
2022-01-26 23:34:08 +01:00
if (index.isValid() && role == Qt::CheckStateRole)
{
int status = value.toInt();
std::vector<mapnik::layer>& layers = const_cast<std::vector<mapnik::layer>&>(map_->layers());
layers.at(index.row()).set_active(status);
emit dataChanged(index, index);
return true;
}
return false;
2007-08-07 17:09:41 +02:00
}
Qt::ItemFlags LayerListModel::flags(QModelIndex const& index) const
{
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
if (index.isValid())
2022-01-26 23:34:08 +01:00
flags |= Qt::ItemIsUserCheckable;
2007-08-07 17:09:41 +02:00
return flags;
}
2009-12-16 21:02:06 +01:00
boost::optional<mapnik::layer&> LayerListModel::map_layer(int i)
{
2022-01-26 23:34:08 +01:00
if (map_)
{
std::vector<mapnik::layer>& layers = const_cast<std::vector<mapnik::layer>&>(map_->layers());
if (i < int(layers.size()))
return boost::optional<mapnik::layer&>(layers[i]);
}
return boost::optional<mapnik::layer&>();
}