Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <gtkmm/main.h>
00009 #include <iostream>
00010 #include <gtkmm/builder.h>
00011 #include <gtkmm/window.h>
00012 #include "MapDrawingArea.h"
00013 #include "../osmdb/osmdb.h"
00014 #include <memory>
00015 #include <gtkmm/drawingarea.h>
00016 #include <gtkmm/spinbutton.h>
00017 #include <gtkmm/textview.h>
00018 #include <glibmm/optioncontext.h>
00019 #include <glibmm/optiongroup.h>
00020 #include <glibmm/optionentry.h>
00021 #include <osmdisp_config.h>
00022 #include "../util.h"
00023
00024 class ZoomerDrawAreaConnector
00025 {
00026 public:
00027 ZoomerDrawAreaConnector(Gtk::SpinButton* b, display::MapDrawingArea* ar):
00028 b(b),
00029 ar(ar)
00030 {
00031 }
00032 Gtk::SpinButton* b;
00033 display::MapDrawingArea* ar;
00034 void update()
00035 {
00036 ar->set_zoom(b->get_adjustment()->get_value());
00037 }
00038 };
00039
00040 void write_ptree(boost::property_tree::ptree const& ptree, std::ostream& ost, int depth)
00041 {
00042 for (auto it = ptree.begin(); it != ptree.end(); ++it)
00043 {
00044 if (it->second.data() == "" && it->second.size() == 0)
00045 continue;
00046 for (int i = 0; i < depth; ++i)
00047 ost << "\t";
00048 ost << it->first << " ";
00049 ost << it->second.data();
00050 ost << std::endl;
00051 write_ptree(it->second, ost, depth + 1);
00052 }
00053 }
00054
00055 std::string get_els_text(std::vector<std::unique_ptr<osm::Element> > const& els)
00056 {
00057 std::ostringstream str;
00058 std::vector<osm::Element const*> used;
00059 str << "clicked" << std::endl;
00060 for (unsigned int i = 0; i < els.size(); ++i)
00061 {
00062 if (util::find<decltype(osm::deref_eq_by_id(*used.begin(), els[i]))>(used.begin(), used.end(), els[i]) == used.end())
00063 {
00064 used.push_back(els[i].get());
00065 write_ptree(els[i]->get_description(), str, 0);
00066 }
00067 }
00068 return str.str();
00069 }
00070
00071 Glib::OptionEntry make_entry(std::string lname, char sname, std::string desc = "")
00072 {
00073 Glib::OptionEntry e;
00074 e.set_long_name(lname);
00075 e.set_short_name(sname);
00076 e.set_description(desc);
00077 return e;
00078 }
00079
00080 int main(int argc, char** argv)
00081 {
00082 Glib::ustring dbname;
00083 Glib::ustring schema;
00084 double lat = -500;
00085 double lon = -500;
00086 int zoom = -1;
00087 Glib::OptionGroup gr("displayer", "displayer options");
00088 auto e1 = make_entry("database", 'd', "database to connect to");
00089 auto e2 = make_entry("schema", 's', "additional schema to look into");
00090 auto e3 = make_entry("latitude", 'y', "starting center latitude");
00091 auto e4 = make_entry("longitude", 'x', "starting center longitude");
00092 auto e5 = make_entry("zoom", 'z', "starting zoom level");
00093 gr.add_entry(e1, dbname);
00094 gr.add_entry(e2, schema);
00095 gr.add_entry(e3, lat);
00096 gr.add_entry(e4, lon);
00097 gr.add_entry(e5, zoom);
00098 Glib::OptionContext cxt;
00099 cxt.add_group(gr);
00100 try
00101 {
00102 Gtk::Main kit(argc, argv, cxt);
00103
00104 Glib::RefPtr<Gtk::Builder> bldr = Gtk::Builder::create_from_file(GLADE_PATH);
00105 Gtk::Window* wnd = 0;
00106 bldr->get_widget("window1", wnd);
00107 display::MapDrawingArea* area = 0;
00108 bldr->get_widget_derived("drawingarea1", area);
00109
00110 Gtk::SpinButton* zoomer;
00111 bldr->get_widget("zoomspinbutton", zoomer);
00112 ZoomerDrawAreaConnector conn(zoomer, area);
00113 zoomer->get_adjustment()->signal_value_changed().connect(sigc::mem_fun(&conn, &ZoomerDrawAreaConnector::update));
00114
00115 Gtk::TextView* view;
00116 bldr->get_widget("textview1", view);
00117
00118 std::string conninfo;
00119 if (dbname == "")
00120 conninfo = "";
00121 else
00122 conninfo = "dbname=" + dbname;
00123 psql::Database pdb(conninfo);
00124 if (schema != "")
00125 psql::execute_sql(pdb, "SET search_path TO " + schema + ", public");
00126 osmdb::OsmDatabase odb(pdb);
00127 zoomer->get_adjustment()->set_value(area->get_zoom());
00128 area->add_dp(1, std::shared_ptr<display::DisplayProvider>(new osmdb::DisplayDB(odb, TO_SHOW_EDGES, 1, 15)));
00129 area->zoom_changed.connect([zoomer](int val)
00130 {
00131 zoomer->get_adjustment()->set_value(val);
00132 });
00133 area->element_clicked.connect([view](std::vector<std::unique_ptr<osm::Element> > const & els)
00134 {
00135 view->get_buffer()->set_text(get_els_text(els));
00136 });
00137 if (zoom >= 1 && zoom <= 15)
00138 area->set_zoom(zoom);
00139 if (lat >= -90 && lat <= 90)
00140 area->set_latitude(lat);
00141 if (lon >= -180 && lon <= 180)
00142 area->set_longitude(lon);
00143
00144 area->show();
00145
00146 kit.run(*wnd);
00147 }
00148 catch (std::exception& err)
00149 {
00150 std::cout << "Error running displayer: " << std::endl << err.what() << std::endl;
00151 return 1;
00152 }
00153 }
00154
00155