📄 application.cpp
字号:
/* GHelm - Nautical Navigation Software * Copyright (C) 2005 Jon Michaelchuck * * This application is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * This software 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 software; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */#include <iostream>#include "application.h"#include "util.h"#include "progressdialog.h"/** * Constructor */Application::Application(){ set_title(APPNAME); set_reallocate_redraws(true); set_default_size(640, 480); Gtk::VBox *main_vbox = manage(new Gtk::VBox()); add(*main_vbox); // the top of our ui CreateTopUI(); main_vbox->pack_start(*menubar, Gtk::PACK_SHRINK); main_vbox->pack_start(*toolbar, Gtk::PACK_SHRINK); Gtk::VPaned *paned = manage(new Gtk::VPaned()); main_vbox->add(*paned); paned->pack1(renderer_book); // the bottom of our ui CreateBottomUI(); paned->pack2(bottom_hbox, false, false); show_all();}/** * Destructor */Application::~Application(){ std::vector<Chart *>::const_iterator ch_it; std::vector<Chart *>::const_iterator ch_end(charts.end()); for (ch_it = charts.begin(); ch_it != ch_end; ++ch_it) delete *ch_it; Gtk::Main::quit();}/** * Initialize the application app. * @param confdir Path of directory holding configuration files. * @return -1 on failure, 0 on success */int Application::Init(boost::filesystem::path &confdir){ if (LoadConfiguration(confdir) < 0) { if (FirstTime(confdir) < 0) { std::cerr << "Application::Init(): Fatal error creating default " << "configuration." << std::endl; return -1; } } ProgressDialog loading_dialog("Loading Catalog", 0.01, *this); enc_browser.signal_progress.connect( sigc::mem_fun(loading_dialog, &ProgressDialog::on_progress)); loading_dialog.show_all(); if (enc_browser.Init(configuration) < 0) { std::cerr << "Application::Init(): Error initializing enc browser" << std::endl; loading_dialog.hide(); return -1; } loading_dialog.hide(); if (renderer_book.Init(&configuration) < 0) { std::cerr << "Application::Init(): Error initializing renderer book" << std::endl; return -1; } renderer_book.signal_switched.connect( sigc::mem_fun(waypoint_list, &WaypointList::on_switched)); renderer_book.signal_pointer_longlat_updated.connect( sigc::mem_fun(*this, &Application::on_pointer_longlat_updated)); renderer_book.signal_add_waypoint.connect( sigc::mem_fun(*this, &Application::on_add_waypoint)); renderer_book.signal_switched.connect(sigc::mem_fun(preview_renderer, &GLChartPreviewRenderer::on_switched)); renderer_book.signal_load_chart.connect(sigc::mem_fun(preview_renderer, &GLChartPreviewRenderer::on_load_chart)); renderer_book.signal_unload_chart.connect(sigc::mem_fun(preview_renderer, &GLChartPreviewRenderer::on_unload_chart)); renderer_book.signal_set_view_rect.connect(sigc::mem_fun(preview_renderer, &GLChartPreviewRenderer::on_set_view_rect)); if (preview_renderer.Init(&configuration, renderer_book.glconfig, renderer_book.shared_context) < 0) { std::cerr << "Application::Init(): Error initializing preview frame" << std::endl; return -1; } longlat_preview_vbox.pack_start(preview_renderer, Gtk::PACK_EXPAND_WIDGET); if (drawing_configuration.Init(configuration) < 0) { std::cerr << "Application::Init(): Error initializing drawconf" << std::endl; return -1; } drawing_configuration.signal_changed.connect( sigc::mem_fun(renderer_book, &RendererBook::on_changed)); renderer_book.signal_switched.connect( sigc::mem_fun(drawing_configuration, &DrawingConfiguration::on_switched)); show_all(); return 0;}//// initialization helpers///** * Load a configuration * @param dirname Directory where configuration files are * @return -1 on failure, 0 on success */int Application::LoadConfiguration(boost::filesystem::path &confdir){ if (configuration.LoadConfiguration(confdir) < 0) { std::cerr << "Application::LoadConfiguration: Error loading " << "configuration" << std::endl; return -1; } return 0;}/** * First time.. create configuration * @param confdir path to configuration directory */int Application::FirstTime(boost::filesystem::path &confdir){ Gtk::FileChooserDialog file_chooser("Please choose a catalog file", Gtk::FILE_CHOOSER_ACTION_OPEN); file_chooser.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); file_chooser.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK); bool active = true; std::string text = "This appears to be the first time you have run "; text = text + APPNAME + ". Please select a chart catalog file to use" + "(generally located in ENC_ROOT)."; Gtk::MessageDialog first(*this, text); first.run(); while (active) { switch (file_chooser.run()) { case (Gtk::RESPONSE_OK): { boost::filesystem::path filepath(file_chooser.get_filename()); if (!is_catalog(filepath.string().c_str())) { Gtk::MessageDialog invalid_dialog(file_chooser, "Not a catalog file"); invalid_dialog.run(); } else { if (configuration.GenerateConfiguration(filepath, confdir) < 0) { Gtk::MessageDialog invalid_dialog(file_chooser, "Error generating configuration"); invalid_dialog.run(); } else { if (LoadConfiguration(confdir) < 0) { Gtk::MessageDialog invalid_dialog(file_chooser, "Error loading generated configuration"); invalid_dialog.run(); } else active = false; } } break; } default: Gtk::MessageDialog invalid_dialog(file_chooser, "GHelm requires a catalog file to be selected..."); invalid_dialog.add_button(Gtk::Stock::QUIT, Gtk::RESPONSE_DELETE_EVENT); int result = invalid_dialog.run(); if (result == Gtk::RESPONSE_DELETE_EVENT) return -1; break; } } Gtk::MessageDialog valid_dialog(file_chooser, "Charts loaded. Now click the File menu and then Browse Catalog."); valid_dialog.run(); return 0;}//// callbacks///** * Run the ENC_ROOT browser */void Application::BrowseCatalog(){ enc_browser.show_all(); Chart *chart = NULL; bool active = true; while (active) { int result = enc_browser.run(); boost::filesystem::path filepath = enc_browser.GetSelectedFilepath(); Glib::ustring description = enc_browser.GetSelectedDescription(); float slat = enc_browser.GetSelectedDimension(SLAT); float wlon = enc_browser.GetSelectedDimension(WLON); float nlat = enc_browser.GetSelectedDimension(NLAT); float elon = enc_browser.GetSelectedDimension(ELON); switch (result) { case (Gtk::RESPONSE_OK): { chart = LoadChartHelper(filepath, description); if (chart) { chart->description = description; chart->slat = slat; chart->wlon = wlon; chart->nlat = nlat; chart->elon = elon; Gtk::Notebook_Helpers::PageList::iterator it = renderer_book.get_current(); if (renderer_book.LoadChart(chart, it) < 0) { Gtk::MessageDialog error_dialog(enc_browser, "Error loading chart into renderer"); error_dialog.run(); } else active = false; } break; } case (Gtk::RESPONSE_CANCEL): { active = false; break; } default: break; } } enc_browser.hide();}/** * Helper function for loading charts * @param filepath Filepath of chart * @param description Chart description (from enc browser) * @return Pointer to the loaded chart, or NULL on failure. */Chart *Application::LoadChartHelper(boost::filesystem::path &filepath, Glib::ustring description){ // first we check if this chart has already been loaded std::vector<Chart *>::iterator chit; std::vector<Chart *>::iterator end(charts.end()); for (chit = charts.begin(); chit != end; ++chit) { if ((*chit)->filepath.string() == filepath.string()) { return *chit; } } ProgressDialog loading_dialog("Loading Chart", 0.01, enc_browser); loading_dialog.show_all(); Chart *chart = new Chart; chart->signal_progress.connect( sigc::mem_fun(loading_dialog, &ProgressDialog::on_progress)); if (chart->Load(filepath, configuration) < 0) { Gtk::MessageDialog dialog(enc_browser, "LoadChart(): Loading chart failed"); dialog.run(); delete chart; loading_dialog.hide(); return NULL; } else { chart->description = description; charts.push_back(chart); loading_dialog.hide(); } // modify menu action_group->add( Gtk::Action::create(chart->description, chart->description), sigc::bind<boost::filesystem::path>(sigc::mem_fun(*this, &Application::SelectChart), chart->filepath)); Glib::ustring ui_info = "<ui>" "<menubar name='MenuBar'>"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -