mapnik/demo/viewer/styles_model.cpp

416 lines
10 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
*
* 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.
*/
#include "styles_model.hpp"
2010-03-24 19:01:37 +01:00
#include <mapnik/expression_string.hpp>
#include <mapnik/noncopyable.hpp>
#include <mapnik/rule.hpp>
#include <mapnik/feature_type_style.hpp>
2009-01-14 13:41:37 +01:00
// boost
#include <boost/concept_check.hpp>
2007-08-07 17:09:41 +02:00
#include <boost/scoped_ptr.hpp>
2009-01-14 13:41:37 +01:00
// qt
2007-08-07 17:09:41 +02:00
#include <QList>
#include <QIcon>
#include <QPainter>
#include <QPixmap>
2007-08-07 17:09:41 +02:00
class node : private mapnik::noncopyable
2007-08-07 17:09:41 +02:00
{
2010-03-24 19:01:37 +01:00
struct node_base
{
2011-05-04 17:53:36 +02:00
virtual QString name() const=0;
virtual QIcon icon() const=0;
virtual ~node_base() {}
2010-03-24 19:01:37 +01:00
};
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
template <typename T>
struct wrap : public node_base
{
2011-05-04 17:53:36 +02:00
wrap(T const& obj)
: obj_(obj) {}
2011-10-23 16:09:12 +02:00
2011-05-04 17:53:36 +02:00
~wrap() {}
2011-10-23 16:09:12 +02:00
2011-05-04 17:53:36 +02:00
QString name () const
{
return obj_.name();
}
2011-10-23 16:09:12 +02:00
2011-05-04 17:53:36 +02:00
QIcon icon() const
{
return obj_.icon();
}
2011-10-23 16:09:12 +02:00
2011-05-04 17:53:36 +02:00
T obj_;
2010-03-24 19:01:37 +01:00
};
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
public:
2011-10-23 16:09:12 +02:00
template <typename T>
2010-03-24 19:01:37 +01:00
node ( T const& obj, node * parent=0)
2011-05-04 17:53:36 +02:00
: impl_(new wrap<T>(obj)),
parent_(parent)
2010-03-24 19:01:37 +01:00
{}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
QString name() const
{
2011-05-04 17:53:36 +02:00
return impl_->name();
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
QIcon icon() const
{
2011-05-04 17:53:36 +02:00
return impl_->icon();
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
unsigned num_children() const
{
2011-05-04 17:53:36 +02:00
return children_.count();
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
node * child(unsigned row) const
{
2011-05-04 17:53:36 +02:00
return children_.value(row);
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
node * parent() const
{
2011-05-04 17:53:36 +02:00
return parent_;
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
node * add_child(node * child)
{
2011-05-04 17:53:36 +02:00
children_.push_back(child);
return child;
2010-03-24 19:01:37 +01:00
}
int row () const
{
2011-05-04 17:53:36 +02:00
if (parent_)
2007-08-07 17:09:41 +02:00
return parent_->children_.indexOf(const_cast<node*>(this));
2011-05-04 17:53:36 +02:00
else
2007-08-07 17:09:41 +02:00
return 0;
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
~node()
2010-03-24 19:01:37 +01:00
{
2011-05-04 17:53:36 +02:00
qDeleteAll(children_);
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
private:
boost::scoped_ptr<node_base> impl_;
QList<node*> children_;
node * parent_;
2007-08-07 17:09:41 +02:00
};
struct symbolizer_info : public boost::static_visitor<QString>
{
2010-03-24 19:01:37 +01:00
QString operator() (mapnik::point_symbolizer const& sym) const
{
2011-05-04 17:53:36 +02:00
boost::ignore_unused_variable_warning(sym);
return QString("PointSymbolizer");
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
QString operator() (mapnik::line_symbolizer const& sym) const
{
2011-05-04 17:53:36 +02:00
boost::ignore_unused_variable_warning(sym);
return QString("LineSymbolizer");
2010-03-24 19:01:37 +01:00
}
2010-03-24 19:01:37 +01:00
QString operator() (mapnik::line_pattern_symbolizer const& sym) const
{
2011-05-04 17:53:36 +02:00
boost::ignore_unused_variable_warning(sym);
return QString("LinePatternSymbolizer");
2010-03-24 19:01:37 +01:00
}
2010-03-24 19:01:37 +01:00
QString operator() (mapnik::polygon_symbolizer const& sym) const
{
2011-05-04 17:53:36 +02:00
boost::ignore_unused_variable_warning(sym);
return QString("PolygonSymbolizer");
2010-03-24 19:01:37 +01:00
}
2010-03-24 19:01:37 +01:00
QString operator() (mapnik::polygon_pattern_symbolizer const& sym) const
{
2011-05-04 17:53:36 +02:00
boost::ignore_unused_variable_warning(sym);
return QString("PolygonSymbolizer");
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
QString operator() (mapnik::text_symbolizer const& sym) const
{
2011-05-04 17:53:36 +02:00
boost::ignore_unused_variable_warning(sym);
return QString("TextSymbolizer");
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
QString operator() (mapnik::shield_symbolizer const& sym) const
{
2011-05-04 17:53:36 +02:00
boost::ignore_unused_variable_warning(sym);
return QString("ShieldSymbolizer");
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
QString operator() (mapnik::markers_symbolizer const& sym) const
{
boost::ignore_unused_variable_warning(sym);
return QString("MarkersSymbolizer");
}
QString operator() (mapnik::building_symbolizer const& sym) const
{
boost::ignore_unused_variable_warning(sym);
return QString("BuildingSymbolizer");
}
2010-03-24 19:01:37 +01:00
template <typename T>
QString operator() (T const& ) const
{
2011-05-04 17:53:36 +02:00
return QString ("FIXME");
2010-03-24 19:01:37 +01:00
}
};
struct symbolizer_icon : public boost::static_visitor<QIcon>
{
2010-03-24 19:01:37 +01:00
QIcon operator() (mapnik::polygon_symbolizer const& sym) const
{
2011-05-04 17:53:36 +02:00
QPixmap pix(16,16);
QPainter painter(&pix);
mapnik::color const& fill = sym.get_fill();
QBrush brush(QColor(fill.red(),fill.green(),fill.blue(),fill.alpha()));
painter.fillRect(0, 0, 16, 16, brush);
return QIcon(pix);
2010-03-24 19:01:37 +01:00
}
2010-03-24 19:01:37 +01:00
QIcon operator() (mapnik::point_symbolizer const& sym) const
{
2011-05-04 17:53:36 +02:00
// FIXME!
/*
boost::shared_ptr<mapnik::image_data_32> symbol = sym.get_image();
if (symbol)
{
QImage image(symbol->getBytes(),
symbol->width(),symbol->height(),QImage::Format_ARGB32);
QPixmap pix = QPixmap::fromImage(image.rgbSwapped());
return QIcon(pix);
}
*/
return QIcon();
2010-03-24 19:01:37 +01:00
}
QIcon operator() (mapnik::line_symbolizer const& sym) const
{
2011-05-04 17:53:36 +02:00
QPixmap pix(48,16);
pix.fill();
QPainter painter(&pix);
2011-10-23 16:09:12 +02:00
mapnik::stroke const& strk = sym.get_stroke();
2011-05-04 17:53:36 +02:00
mapnik::color const& col = strk.get_color();
QPen pen(QColor(col.red(),col.green(),col.blue(),col.alpha()));
pen.setWidth(strk.get_width());
painter.setPen(pen);
painter.drawLine(0,7,47,7);
//painter.drawLine(7,15,12,0);
2011-10-23 16:09:12 +02:00
//painter.drawLine(12,0,8,15);
2011-05-04 17:53:36 +02:00
return QIcon(pix);
2010-03-24 19:01:37 +01:00
}
2010-03-24 19:01:37 +01:00
template <typename T>
QIcon operator() (T const& ) const
{
2011-05-04 17:53:36 +02:00
return QIcon (":/images/filter.png");
2010-03-24 19:01:37 +01:00
}
};
class symbolizer_node
{
2010-03-24 19:01:37 +01:00
public:
symbolizer_node(mapnik::symbolizer const & sym)
: sym_(sym) {}
2010-03-24 19:01:37 +01:00
~symbolizer_node(){}
2011-10-23 16:09:12 +02:00
QString name() const
2010-03-24 19:01:37 +01:00
{
2011-05-04 17:53:36 +02:00
//return QString("Symbolizer:fixme");
return boost::apply_visitor(symbolizer_info(),sym_);
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
QIcon icon() const
{
2011-05-04 17:53:36 +02:00
return boost::apply_visitor(symbolizer_icon(),sym_);//QIcon(":/images/filter.png");
2010-03-24 19:01:37 +01:00
}
mapnik::symbolizer const& sym_;
};
2007-08-07 17:09:41 +02:00
class rule_node
{
2010-03-24 19:01:37 +01:00
public:
rule_node(QString name,mapnik::rule const & r)
2011-05-04 17:53:36 +02:00
: name_(name),
rule_(r) {}
2010-03-24 19:01:37 +01:00
~rule_node() {}
QString name() const
{
2011-05-04 17:53:36 +02:00
mapnik::expression_ptr filter = rule_.get_filter();
return QString(mapnik::to_expression_string(*filter).c_str());
2011-10-23 16:09:12 +02:00
}
2010-03-24 19:01:37 +01:00
QIcon icon() const
{
2011-05-04 17:53:36 +02:00
return QIcon(":/images/filter.png");
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
private:
QString name_;
mapnik::rule const& rule_;
2007-08-07 17:09:41 +02:00
};
class style_node
{
2010-03-24 19:01:37 +01:00
public:
style_node(QString name, mapnik::feature_type_style const& style)
2011-05-04 17:53:36 +02:00
: name_(name),
style_(style) {}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
~style_node() {}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
QString name() const
{
2011-05-04 17:53:36 +02:00
return name_;
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
QIcon icon() const
{
2011-05-04 17:53:36 +02:00
return QIcon(":/images/style.png");
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
private:
QString name_;
mapnik::feature_type_style const& style_;
2007-08-07 17:09:41 +02:00
};
class map_node
{
2010-03-24 19:01:37 +01:00
public:
explicit map_node(boost::shared_ptr<mapnik::Map> map)
2011-05-04 17:53:36 +02:00
: map_(map) {}
2010-03-24 19:01:37 +01:00
~map_node() {}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
QString name() const
{
2011-05-04 17:53:36 +02:00
return QString("Map");
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
QIcon icon() const
{
2011-05-04 17:53:36 +02:00
return QIcon(":/images/map.png");
2010-03-24 19:01:37 +01:00
}
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
private:
boost::shared_ptr<mapnik::Map> map_;
2007-08-07 17:09:41 +02:00
};
StyleModel::StyleModel(boost::shared_ptr<mapnik::Map> map, QObject * parent)
2010-03-24 19:01:37 +01:00
: QAbstractItemModel(parent),
2011-10-23 16:09:12 +02:00
root_(new node(map_node(map)))
2007-08-07 17:09:41 +02:00
{
2011-10-23 16:09:12 +02:00
typedef std::map<std::string,mapnik::feature_type_style> style_type;
2010-03-24 19:01:37 +01:00
style_type const & styles = map->styles();
style_type::const_iterator itr = styles.begin();
style_type::const_iterator end = styles.end();
for (; itr != end; ++itr)
{
2011-05-04 17:53:36 +02:00
node * style_n = root_->add_child(new node(style_node(QString(itr->first.c_str()),itr->second),root_.get()));
mapnik::rules const& rules = itr->second.get_rules();
mapnik::rules::const_iterator itr2 = rules.begin();
for ( ; itr2 != rules.end();++itr2)
{
node* rule_n = style_n->add_child(new node(rule_node(QString("Rule"),*itr2),style_n));
mapnik::rule::symbolizers::const_iterator itr3 = (*itr2).begin();
2011-05-04 17:53:36 +02:00
for ( ; itr3 !=itr2->end();++itr3)
{
rule_n->add_child(new node(symbolizer_node(*itr3),rule_n));
}
}
2011-10-23 16:09:12 +02:00
}
2007-08-07 17:09:41 +02:00
}
StyleModel::~StyleModel() {}
2011-10-23 16:09:12 +02:00
// interface
2007-08-07 17:09:41 +02:00
QModelIndex StyleModel::index (int row, int col, QModelIndex const& parent) const
{
// qDebug("index() row=%d col=%d parent::internalId() = %lld", row,col,parent.internalId());
2010-03-24 19:01:37 +01:00
node * parent_node;
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
if (!parent.isValid())
2011-05-04 17:53:36 +02:00
parent_node = root_.get();
2010-03-24 19:01:37 +01:00
else
2011-05-04 17:53:36 +02:00
parent_node = static_cast<node*>(parent.internalPointer());
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
node * child_node = parent_node->child(row);
if (child_node)
2011-05-04 17:53:36 +02:00
return createIndex(row,col,child_node);
2010-03-24 19:01:37 +01:00
else
2011-05-04 17:53:36 +02:00
return QModelIndex();
2007-08-07 17:09:41 +02:00
}
QModelIndex StyleModel::parent (QModelIndex const& index) const
{
2010-03-24 19:01:37 +01:00
node * child_node = static_cast<node*>(index.internalPointer());
node * parent_node = child_node->parent();
if (parent_node == root_.get())
2011-05-04 17:53:36 +02:00
return QModelIndex();
2011-10-23 16:09:12 +02:00
2010-03-24 19:01:37 +01:00
return createIndex(parent_node->row(),0,parent_node);
2007-08-07 17:09:41 +02:00
}
int StyleModel::rowCount(QModelIndex const& parent) const
{
2010-03-24 19:01:37 +01:00
//qDebug("rowCount");
node * parent_node;
if (parent.column() > 0) return 0;
if (!parent.isValid())
2011-05-04 17:53:36 +02:00
parent_node = root_.get();
2010-03-24 19:01:37 +01:00
else
2011-05-04 17:53:36 +02:00
parent_node = static_cast<node*>(parent.internalPointer());
2010-03-24 19:01:37 +01:00
return parent_node->num_children();
2007-08-07 17:09:41 +02:00
}
int StyleModel::columnCount( QModelIndex const&) const
{
2010-03-24 19:01:37 +01:00
return 1;
2007-08-07 17:09:41 +02:00
}
QVariant StyleModel::data(const QModelIndex & index, int role) const
{
2010-03-24 19:01:37 +01:00
//qDebug("data index::internalId() = %lld", index.internalId());
if (!index.isValid())
2011-05-04 17:53:36 +02:00
return QVariant();
2010-03-24 19:01:37 +01:00
node * cur_node = static_cast<node*>(index.internalPointer());
if (cur_node)
{
2011-05-04 17:53:36 +02:00
if (role == Qt::DisplayRole)
{
2011-10-23 16:09:12 +02:00
2011-05-04 17:53:36 +02:00
return QVariant(cur_node->name());
}
else if ( role == Qt::DecorationRole)
{
return cur_node->icon();
}
2010-03-24 19:01:37 +01:00
}
return QVariant();
2007-08-07 17:09:41 +02:00
}