+ don't call zoom_all if bounding box is supplied

This commit is contained in:
Artem Pavlenko 2010-06-15 12:27:34 +00:00
parent 68ff49d429
commit bb6fafad42

View file

@ -45,335 +45,335 @@
#include "about_dialog.hpp" #include "about_dialog.hpp"
MainWindow::MainWindow() MainWindow::MainWindow()
: filename_(), : filename_(),
default_extent_(-20037508.3428,-20037508.3428,20037508.3428,20037508.3428) default_extent_(-20037508.3428,-20037508.3428,20037508.3428,20037508.3428)
{ {
mapWidget_ = new MapWidget(this); mapWidget_ = new MapWidget(this);
QSplitter *splitter = new QSplitter(this); QSplitter *splitter = new QSplitter(this);
QTabWidget *tabWidget=new QTabWidget; QTabWidget *tabWidget=new QTabWidget;
layerTab_ = new LayerTab; layerTab_ = new LayerTab;
layerTab_->setFocusPolicy(Qt::NoFocus); layerTab_->setFocusPolicy(Qt::NoFocus);
layerTab_->setIconSize(QSize(16,16)); layerTab_->setIconSize(QSize(16,16));
//LayerDelegate *delegate = new LayerDelegate(this); //LayerDelegate *delegate = new LayerDelegate(this);
//layerTab_->setItemDelegate(delegate); //layerTab_->setItemDelegate(delegate);
//layerTab_->setItemDelegate(new QItemDelegate(this)); //layerTab_->setItemDelegate(new QItemDelegate(this));
//layerTab_->setViewMode(QListView::IconMode); //layerTab_->setViewMode(QListView::IconMode);
layerTab_->setFlow(QListView::TopToBottom); layerTab_->setFlow(QListView::TopToBottom);
tabWidget->addTab(layerTab_,tr("Layers")); tabWidget->addTab(layerTab_,tr("Layers"));
// Styles tab // Styles tab
styleTab_ = new StyleTab; styleTab_ = new StyleTab;
tabWidget->addTab(styleTab_,tr("Styles")); tabWidget->addTab(styleTab_,tr("Styles"));
splitter->addWidget(tabWidget); splitter->addWidget(tabWidget);
splitter->addWidget(mapWidget_); splitter->addWidget(mapWidget_);
QList<int> list; QList<int> list;
list.push_back(200); list.push_back(200);
list.push_back(600); list.push_back(600);
splitter->setSizes(list); splitter->setSizes(list);
mapWidget_->setFocusPolicy(Qt::StrongFocus); mapWidget_->setFocusPolicy(Qt::StrongFocus);
mapWidget_->setFocus(); mapWidget_->setFocus();
//setCentralWidget(mapWidget_); //setCentralWidget(mapWidget_);
setCentralWidget(splitter); setCentralWidget(splitter);
createActions(); createActions();
createMenus(); createMenus();
createToolBars(); createToolBars();
createContextMenu(); createContextMenu();
setWindowTitle(tr("Mapnik Viewer")); setWindowTitle(tr("Mapnik Viewer"));
status=new QStatusBar(this); status=new QStatusBar(this);
status->showMessage(tr("")); status->showMessage(tr(""));
setStatusBar(status); setStatusBar(status);
resize(800,600); resize(800,600);
//connect mapview to layerlist //connect mapview to layerlist
connect(mapWidget_, SIGNAL(mapViewChanged()),layerTab_, SLOT(update())); connect(mapWidget_, SIGNAL(mapViewChanged()),layerTab_, SLOT(update()));
// slider // slider
connect(slider_,SIGNAL(valueChanged(int)),mapWidget_,SLOT(zoomToLevel(int))); connect(slider_,SIGNAL(valueChanged(int)),mapWidget_,SLOT(zoomToLevel(int)));
// //
connect(layerTab_,SIGNAL(update_mapwidget()),mapWidget_,SLOT(updateMap())); connect(layerTab_,SIGNAL(update_mapwidget()),mapWidget_,SLOT(updateMap()));
connect(layerTab_,SIGNAL(layerSelected(int)), connect(layerTab_,SIGNAL(layerSelected(int)),
mapWidget_,SLOT(layerSelected(int))); mapWidget_,SLOT(layerSelected(int)));
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
delete mapWidget_; delete mapWidget_;
} }
void MainWindow::closeEvent(QCloseEvent* event) void MainWindow::closeEvent(QCloseEvent* event)
{ {
event->accept(); event->accept();
} }
void MainWindow::createContextMenu() void MainWindow::createContextMenu()
{ {
layerTab_->setContextMenuPolicy(Qt::ActionsContextMenu); layerTab_->setContextMenuPolicy(Qt::ActionsContextMenu);
layerTab_->addAction(openAct); layerTab_->addAction(openAct);
layerTab_->addAction(layerInfo); layerTab_->addAction(layerInfo);
} }
void MainWindow::open(QString const& path) void MainWindow::open(QString const& path)
{ {
if (path.isNull()) if (path.isNull())
{ {
filename_ = QFileDialog::getOpenFileName(this,tr("Open Mapnik file"), filename_ = QFileDialog::getOpenFileName(this,tr("Open Mapnik file"),
currentPath,"*.xml"); currentPath,"*.xml");
} }
else else
{ {
filename_ = path; filename_ = path;
} }
if (!filename_.isEmpty()) if (!filename_.isEmpty())
{ {
load_map_file(filename_); load_map_file(filename_);
//zoom_all(); //zoom_all();
setWindowTitle(tr("%1 - Mapnik Viewer").arg(filename_)); setWindowTitle(tr("%1 - Mapnik Viewer").arg(filename_));
} }
} }
void MainWindow::reload() void MainWindow::reload()
{ {
if (!filename_.isEmpty()) if (!filename_.isEmpty())
{ {
mapnik::box2d<double> bbox = mapWidget_->getMap()->getCurrentExtent(); mapnik::box2d<double> bbox = mapWidget_->getMap()->getCurrentExtent();
load_map_file(filename_); load_map_file(filename_);
mapWidget_->zoomToBox(bbox); mapWidget_->zoomToBox(bbox);
setWindowTitle(tr("%1 - *Reloaded*").arg(filename_)); setWindowTitle(tr("%1 - *Reloaded*").arg(filename_));
} }
} }
void MainWindow::save() void MainWindow::save()
{ {
QString initialPath = QDir::currentPath() + "/untitled.xml"; QString initialPath = QDir::currentPath() + "/untitled.xml";
QString filename = QFileDialog::getSaveFileName(this, tr("Save"), QString filename = QFileDialog::getSaveFileName(this, tr("Save"),
initialPath, initialPath,
tr("%1 Files (*.xml)") tr("%1 Files (*.xml)")
.arg(QString("Mapnik definition"))); .arg(QString("Mapnik definition")));
if (!filename.isEmpty()) if (!filename.isEmpty())
{ {
std::cout<<"saving "<< filename.toStdString() << std::endl; std::cout<<"saving "<< filename.toStdString() << std::endl;
mapnik::save_map(*mapWidget_->getMap(),filename.toStdString()); mapnik::save_map(*mapWidget_->getMap(),filename.toStdString());
} }
} }
void MainWindow::load_map_file(QString const& filename) void MainWindow::load_map_file(QString const& filename)
{ {
std::cout<<"loading "<< filename.toStdString() << std::endl; std::cout<<"loading "<< filename.toStdString() << std::endl;
unsigned width = mapWidget_->width(); unsigned width = mapWidget_->width();
unsigned height = mapWidget_->height(); unsigned height = mapWidget_->height();
boost::shared_ptr<mapnik::Map> map(new mapnik::Map(width,height)); boost::shared_ptr<mapnik::Map> map(new mapnik::Map(width,height));
mapWidget_->setMap(map); mapWidget_->setMap(map);
try try
{ {
mapnik::load_map(*map,filename.toStdString()); mapnik::load_map(*map,filename.toStdString());
} }
catch (mapnik::config_error & ex) catch (mapnik::config_error & ex)
{ {
std::cout << ex.what() << "\n"; std::cout << ex.what() << "\n";
} }
map->zoom_all(); //map->zoom_all();
mapnik::box2d<double> const& ext = map->getCurrentExtent(); //mapnik::box2d<double> const& ext = map->getCurrentExtent();
mapWidget_->zoomToBox(ext); //mapWidget_->zoomToBox(ext);
layerTab_->setModel(new LayerListModel(map,this)); layerTab_->setModel(new LayerListModel(map,this));
styleTab_->setModel(new StyleModel(map,this)); styleTab_->setModel(new StyleModel(map,this));
} }
void MainWindow::zoom_all() void MainWindow::zoom_all()
{ {
mapWidget_->defaultView(); mapWidget_->defaultView();
} }
void MainWindow::zoom_to_box() void MainWindow::zoom_to_box()
{ {
mapWidget_->setTool(MapWidget::ZoomToBox); mapWidget_->setTool(MapWidget::ZoomToBox);
} }
void MainWindow::pan() void MainWindow::pan()
{ {
mapWidget_->setTool(MapWidget::Pan); mapWidget_->setTool(MapWidget::Pan);
} }
void MainWindow::info() void MainWindow::info()
{ {
mapWidget_->setTool(MapWidget::Info); mapWidget_->setTool(MapWidget::Info);
} }
void MainWindow::pan_left() void MainWindow::pan_left()
{ {
mapWidget_->panLeft(); mapWidget_->panLeft();
} }
void MainWindow::pan_right() void MainWindow::pan_right()
{ {
mapWidget_->panRight(); mapWidget_->panRight();
} }
void MainWindow::pan_up() void MainWindow::pan_up()
{ {
mapWidget_->panUp(); mapWidget_->panUp();
} }
void MainWindow::pan_down() void MainWindow::pan_down()
{ {
mapWidget_->panDown(); mapWidget_->panDown();
} }
void MainWindow::about() void MainWindow::about()
{ {
about_dialog dlg; about_dialog dlg;
dlg.exec(); dlg.exec();
} }
void MainWindow::export_as() void MainWindow::export_as()
{ {
QAction *action = qobject_cast<QAction *>(sender()); QAction *action = qobject_cast<QAction *>(sender());
QByteArray fileFormat = action->data().toByteArray(); QByteArray fileFormat = action->data().toByteArray();
QString initialPath = QDir::currentPath() + "/map." + fileFormat; QString initialPath = QDir::currentPath() + "/map." + fileFormat;
QString fileName = QFileDialog::getSaveFileName(this, tr("Export As"), QString fileName = QFileDialog::getSaveFileName(this, tr("Export As"),
initialPath, initialPath,
tr("%1 Files (*.%2);;All Files (*)") tr("%1 Files (*.%2);;All Files (*)")
.arg(QString(fileFormat.toUpper())) .arg(QString(fileFormat.toUpper()))
.arg(QString(fileFormat))); .arg(QString(fileFormat)));
if (!fileName.isEmpty()) if (!fileName.isEmpty())
{ {
QPixmap const& pix = mapWidget_->pixmap(); QPixmap const& pix = mapWidget_->pixmap();
pix.save(fileName); pix.save(fileName);
} }
} }
void MainWindow::print() void MainWindow::print()
{ {
//Q_ASSERT(mapWidget_->pixmap()); //Q_ASSERT(mapWidget_->pixmap());
//QPrintDialog dialog(&printer, this); //QPrintDialog dialog(&printer, this);
//if (dialog.exec()) { //if (dialog.exec()) {
// QPainter painter(&printer); // QPainter painter(&printer);
// QRect rect = painter.viewport(); // QRect rect = painter.viewport();
// QSize size = mapWidget_->pixmap()->size(); // QSize size = mapWidget_->pixmap()->size();
// size.scale(rect.size(), Qt::KeepAspectRatio); // size.scale(rect.size(), Qt::KeepAspectRatio);
// painter.setViewport(rect.x(), rect.y(), size.width(), size.height()); // painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
// painter.setWindow(mapWidget_->pixmap()->rect()); // painter.setWindow(mapWidget_->pixmap()->rect());
// painter.drawPixmap(0, 0, *mapWidget_->pixmap()); // painter.drawPixmap(0, 0, *mapWidget_->pixmap());
//} //}
} }
void MainWindow::createActions() void MainWindow::createActions()
{ {
//exportAct = new QAction(tr("&Export as ..."),this); //exportAct = new QAction(tr("&Export as ..."),this);
//exportAct->setShortcut(tr("Ctrl+E")); //exportAct->setShortcut(tr("Ctrl+E"));
//connect(exportAct, SIGNAL(triggered()), this, SLOT(export_as())); //connect(exportAct, SIGNAL(triggered()), this, SLOT(export_as()));
zoomAllAct = new QAction(QIcon(":/images/home.png"),tr("Zoom All"),this); zoomAllAct = new QAction(QIcon(":/images/home.png"),tr("Zoom All"),this);
connect(zoomAllAct, SIGNAL(triggered()), this, SLOT(zoom_all())); connect(zoomAllAct, SIGNAL(triggered()), this, SLOT(zoom_all()));
zoomBoxAct = new QAction(QIcon(":/images/zoombox.png"),tr("Zoom To Box"),this); zoomBoxAct = new QAction(QIcon(":/images/zoombox.png"),tr("Zoom To Box"),this);
zoomBoxAct->setCheckable(true); zoomBoxAct->setCheckable(true);
connect(zoomBoxAct, SIGNAL(triggered()), this, SLOT(zoom_to_box())); connect(zoomBoxAct, SIGNAL(triggered()), this, SLOT(zoom_to_box()));
panAct = new QAction(QIcon(":/images/pan.png"),tr("Pan"),this); panAct = new QAction(QIcon(":/images/pan.png"),tr("Pan"),this);
panAct->setCheckable(true); panAct->setCheckable(true);
connect(panAct, SIGNAL(triggered()), this, SLOT(pan())); connect(panAct, SIGNAL(triggered()), this, SLOT(pan()));
infoAct = new QAction(QIcon(":/images/info.png"),tr("Info"),this); infoAct = new QAction(QIcon(":/images/info.png"),tr("Info"),this);
infoAct->setCheckable(true); infoAct->setCheckable(true);
connect(infoAct, SIGNAL(triggered()), this, SLOT(info())); connect(infoAct, SIGNAL(triggered()), this, SLOT(info()));
toolsGroup=new QActionGroup(this); toolsGroup=new QActionGroup(this);
toolsGroup->addAction(zoomBoxAct); toolsGroup->addAction(zoomBoxAct);
toolsGroup->addAction(panAct); toolsGroup->addAction(panAct);
toolsGroup->addAction(infoAct); toolsGroup->addAction(infoAct);
zoomBoxAct->setChecked(true); zoomBoxAct->setChecked(true);
openAct=new QAction(tr("Open Map definition"),this); openAct=new QAction(tr("Open Map definition"),this);
connect(openAct,SIGNAL(triggered()),this,SLOT(open())); connect(openAct,SIGNAL(triggered()),this,SLOT(open()));
saveAct=new QAction(tr("Save Map definition"),this); saveAct=new QAction(tr("Save Map definition"),this);
connect(saveAct,SIGNAL(triggered()),this,SLOT(save())); connect(saveAct,SIGNAL(triggered()),this,SLOT(save()));
panLeftAct = new QAction(QIcon(":/images/left.png"),tr("&Pan Left"),this); panLeftAct = new QAction(QIcon(":/images/left.png"),tr("&Pan Left"),this);
connect(panLeftAct, SIGNAL(triggered()), this, SLOT(pan_left())); connect(panLeftAct, SIGNAL(triggered()), this, SLOT(pan_left()));
panRightAct = new QAction(QIcon(":/images/right.png"),tr("&Pan Right"),this); panRightAct = new QAction(QIcon(":/images/right.png"),tr("&Pan Right"),this);
connect(panRightAct, SIGNAL(triggered()), this, SLOT(pan_right())); connect(panRightAct, SIGNAL(triggered()), this, SLOT(pan_right()));
panUpAct = new QAction(QIcon(":/images/up.png"),tr("&Pan Up"),this); panUpAct = new QAction(QIcon(":/images/up.png"),tr("&Pan Up"),this);
connect(panUpAct, SIGNAL(triggered()), this, SLOT(pan_up())); connect(panUpAct, SIGNAL(triggered()), this, SLOT(pan_up()));
panDownAct = new QAction(QIcon(":/images/down.png"),tr("&Pan Down"),this); panDownAct = new QAction(QIcon(":/images/down.png"),tr("&Pan Down"),this);
connect(panDownAct, SIGNAL(triggered()), this, SLOT(pan_down())); connect(panDownAct, SIGNAL(triggered()), this, SLOT(pan_down()));
reloadAct = new QAction(QIcon(":/images/reload.png"),tr("Reload"),this); reloadAct = new QAction(QIcon(":/images/reload.png"),tr("Reload"),this);
connect(reloadAct, SIGNAL(triggered()), this, SLOT(reload())); connect(reloadAct, SIGNAL(triggered()), this, SLOT(reload()));
layerInfo = new QAction(QIcon(":/images/info.png"),tr("&Layer info"),layerTab_); layerInfo = new QAction(QIcon(":/images/info.png"),tr("&Layer info"),layerTab_);
connect(layerInfo, SIGNAL(triggered()), layerTab_,SLOT(layerInfo())); connect(layerInfo, SIGNAL(triggered()), layerTab_,SLOT(layerInfo()));
connect(layerTab_, SIGNAL(doubleClicked(QModelIndex const&)), layerTab_,SLOT(layerInfo2(QModelIndex const&))); connect(layerTab_, SIGNAL(doubleClicked(QModelIndex const&)), layerTab_,SLOT(layerInfo2(QModelIndex const&)));
foreach (QByteArray format, QImageWriter::supportedImageFormats()) foreach (QByteArray format, QImageWriter::supportedImageFormats())
{ {
QString text = tr("%1...").arg(QString(format).toUpper()); QString text = tr("%1...").arg(QString(format).toUpper());
QAction *action = new QAction(text, this); QAction *action = new QAction(text, this);
action->setData(format); action->setData(format);
connect(action, SIGNAL(triggered()), this, SLOT(export_as())); connect(action, SIGNAL(triggered()), this, SLOT(export_as()));
exportAsActs.append(action); exportAsActs.append(action);
} }
printAct = new QAction(QIcon(":/images/print.png"),tr("&Print ..."),this); printAct = new QAction(QIcon(":/images/print.png"),tr("&Print ..."),this);
printAct->setShortcut(tr("Ctrl+E")); printAct->setShortcut(tr("Ctrl+E"));
connect(printAct, SIGNAL(triggered()), this, SLOT(print())); connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
exitAct = new QAction(tr("E&xit"), this); exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q")); exitAct->setShortcut(tr("Ctrl+Q"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
aboutAct = new QAction(QIcon(":/images/about.png"),tr("&About"), this); aboutAct = new QAction(QIcon(":/images/about.png"),tr("&About"), this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
} }
void MainWindow::createMenus() void MainWindow::createMenus()
{ {
exportMenu = new QMenu(tr("&Export As"), this); exportMenu = new QMenu(tr("&Export As"), this);
foreach (QAction *action, exportAsActs) foreach (QAction *action, exportAsActs)
exportMenu->addAction(action); exportMenu->addAction(action);
fileMenu = new QMenu(tr("&File"),this); fileMenu = new QMenu(tr("&File"),this);
fileMenu->addAction(openAct); fileMenu->addAction(openAct);
fileMenu->addAction(saveAct); fileMenu->addAction(saveAct);
fileMenu->addMenu(exportMenu); fileMenu->addMenu(exportMenu);
fileMenu->addAction(printAct); fileMenu->addAction(printAct);
fileMenu->addSeparator(); fileMenu->addSeparator();
fileMenu->addAction(exitAct); fileMenu->addAction(exitAct);
menuBar()->addMenu(fileMenu); menuBar()->addMenu(fileMenu);
helpMenu = new QMenu(tr("&Help"), this); helpMenu = new QMenu(tr("&Help"), this);
helpMenu->addAction(aboutAct); helpMenu->addAction(aboutAct);
menuBar()->addMenu(helpMenu); menuBar()->addMenu(helpMenu);
} }
void MainWindow::createToolBars() void MainWindow::createToolBars()
{ {
fileToolBar = addToolBar(tr("Actions")); fileToolBar = addToolBar(tr("Actions"));
fileToolBar->addAction(zoomAllAct); fileToolBar->addAction(zoomAllAct);
fileToolBar->addAction(zoomBoxAct); fileToolBar->addAction(zoomBoxAct);
fileToolBar->addAction(panAct); fileToolBar->addAction(panAct);
fileToolBar->addAction(panLeftAct); fileToolBar->addAction(panLeftAct);
fileToolBar->addAction(panRightAct); fileToolBar->addAction(panRightAct);
fileToolBar->addAction(panUpAct); fileToolBar->addAction(panUpAct);
fileToolBar->addAction(panDownAct); fileToolBar->addAction(panDownAct);
fileToolBar->addAction(infoAct); fileToolBar->addAction(infoAct);
fileToolBar->addAction(reloadAct); fileToolBar->addAction(reloadAct);
fileToolBar->addAction(printAct); fileToolBar->addAction(printAct);
slider_ = new QSlider(Qt::Horizontal,fileToolBar); slider_ = new QSlider(Qt::Horizontal,fileToolBar);
slider_->setRange(1,18); slider_->setRange(1,18);
slider_->setTickPosition(QSlider::TicksBelow); slider_->setTickPosition(QSlider::TicksBelow);
slider_->setTickInterval(1); slider_->setTickInterval(1);
slider_->setTracking(false); slider_->setTracking(false);
fileToolBar->addWidget(slider_); fileToolBar->addWidget(slider_);
fileToolBar->addAction(aboutAct); fileToolBar->addAction(aboutAct);
} }
@ -382,16 +382,16 @@ void MainWindow::set_default_extent(double x0,double y0, double x1, double y1)
{ {
try try
{ {
boost::shared_ptr<mapnik::Map> map_ptr = mapWidget_->getMap(); boost::shared_ptr<mapnik::Map> map_ptr = mapWidget_->getMap();
if (map_ptr) if (map_ptr)
{ {
mapnik::projection prj(map_ptr->srs()); mapnik::projection prj(map_ptr->srs());
prj.forward(x0,y0); prj.forward(x0,y0);
prj.forward(x1,y1); prj.forward(x1,y1);
default_extent_=mapnik::box2d<double>(x0,y0,x1,y1); default_extent_=mapnik::box2d<double>(x0,y0,x1,y1);
mapWidget_->zoomToBox(default_extent_); mapWidget_->zoomToBox(default_extent_);
std::cout << "SET DEFAULT EXT\n"; std::cout << "SET DEFAULT EXT\n";
} }
} }
catch (...) {} catch (...) {}