M src/Application.cpp => src/Application.cpp +62 -12
@@ 20,10 20,12 @@
#include "Application.h"
#include "Simulation.h"
#include "XmlLoader.h"
+#include "XmlWriter.h"
using namespace Gtk;
#include <vector>
+#include <cstring>
#include <fstream>
#include <iostream>
@@ 36,6 38,7 @@ const std::string Application::datadir(DATADIR);
Application::Application(int argc, char **argv):
gtk_main(argc, argv),
+ export_png_dlg(main_win, "", FILE_CHOOSER_ACTION_SAVE),
save_dlg(main_win, "", FILE_CHOOSER_ACTION_SAVE),
open_dlg(main_win, "", FILE_CHOOSER_ACTION_OPEN)
{
@@ 93,27 96,23 @@ void Application::on_export_png_activate()
{
std::string filename = "";
- save_dlg.set_title(_("Export PNG"));
-
- int result = save_dlg.run();
+ int result = export_png_dlg.run();
if(result == RESPONSE_OK)
{
- filename = save_dlg.get_filename();
+ filename = export_png_dlg.get_filename();
#ifdef DEBUG
std::cerr << "Exporting PNG to file `" << filename << "'." << std::endl;
#endif // DEBUG
sim_canvas.save(filename, "png");
}
- save_dlg.hide();
+ export_png_dlg.hide();
}
void Application::on_open_activate()
{
std::string filename = "";
- open_dlg.set_title(_("Open scene"));
-
int result = open_dlg.run();
if(result == RESPONSE_OK)
{
@@ 139,6 138,49 @@ void Application::on_open_activate()
open_dlg.hide();
}
+void Application::on_save_activate()
+{
+ std::string filename = "";
+
+ while(1){
+ int result = save_dlg.run();
+ if(result == RESPONSE_OK)
+ {
+ filename = save_dlg.get_filename();
+ if(save_dlg.get_filter())
+ {
+ if(save_dlg.get_filter()->get_name() == _("Elfelli XML (*.elfelli)"))
+ {
+ if(filename.find_last_of('.') == std::string::npos)
+ {
+ filename += ".elfelli";
+ }
+ }
+ }
+
+ char buf[1024];
+ std::snprintf(buf, 1024, "A file named \"%s\" already exists. Do you want to overwrite it?", Glib::filename_display_basename(filename).c_str());
+
+ MessageDialog overwrite_dlg(main_win, buf, false, MESSAGE_WARNING, BUTTONS_OK_CANCEL, true);
+ if(!Glib::file_test(filename, Glib::FILE_TEST_EXISTS)
+ || (overwrite_dlg.run() == RESPONSE_OK))
+ {
+#ifdef DEBUG
+ std::cerr << "Saving file `" << filename << "'." << std::endl;
+#endif // DEBUG
+
+ XmlWriter::write(filename, &sim_canvas);
+ break;
+ }
+ }
+ else
+ {
+ break;
+ }
+ }
+ save_dlg.hide();
+}
+
void Application::on_quit_activate()
{
quit();
@@ 230,7 272,7 @@ bool Application::setup_ui_actions()
action_group->add( Action::create("MenuScene", _("_Scene")) );
action_group->add( Action::create("New", Stock::NEW) , sigc::mem_fun(*this, &Application::reset_simulation));
action_group->add( Action::create("Open", Stock::OPEN) , sigc::mem_fun(*this, &Application::on_open_activate));
- action_group->add( Action::create("SaveAs", Stock::SAVE_AS) );
+ action_group->add( Action::create("SaveAs", Stock::SAVE_AS) , sigc::mem_fun(*this, &Application::on_save_activate));
action_group->add( Action::create("ExportPNG", _("Export _PNG")) , sigc::mem_fun(*this, &Application::on_export_png_activate));
action_group->add( Action::create("ExportSVG", _("Export S_VG")) );
action_group->add( Action::create("Quit", Stock::QUIT) , sigc::mem_fun(*this, &Application::quit));
@@ 309,19 351,27 @@ bool Application::setup_ui_actions()
void Application::setup_file_chooser_dialogs()
{
- save_dlg.set_do_overwrite_confirmation();
- save_dlg.add_button(Stock::CANCEL, RESPONSE_CANCEL);
- save_dlg.add_button(Stock::SAVE, RESPONSE_OK);
+ export_png_dlg.set_do_overwrite_confirmation();
+ export_png_dlg.add_button(Stock::CANCEL, RESPONSE_CANCEL);
+ export_png_dlg.add_button(Stock::SAVE, RESPONSE_OK);
+ export_png_dlg.set_title(_("Export PNG"));
- FileFilter elfelli_xml, all;
elfelli_xml.set_name(_("Elfelli XML (*.elfelli)"));
elfelli_xml.add_pattern("*.elfelli");
all.set_name(_("All files"));
all.add_pattern("*");
+ save_dlg.set_title(_("Save scene"));
+ save_dlg.add_button(Stock::CANCEL, RESPONSE_CANCEL);
+ save_dlg.add_button(Stock::SAVE, RESPONSE_OK);
+ save_dlg.add_filter(elfelli_xml);
+ save_dlg.add_filter(all);
+
+
open_dlg.add_button(Stock::CANCEL, RESPONSE_CANCEL);
+ open_dlg.set_title(_("Open scene"));
open_dlg.add_button(Stock::OPEN, RESPONSE_OK);
open_dlg.add_filter(elfelli_xml);
open_dlg.add_filter(all);
M src/Application.h => src/Application.h +5 -3
@@ 31,7 31,7 @@ namespace Elfelli
class Application
{
- public:
+public:
Application(int argc, char **argv);
int main();
void quit();
@@ 42,7 42,7 @@ class Application
static const std::string version;
static const std::string datadir;
- private:
+private:
void setup_gettext();
bool build_gui();
@@ 61,12 61,14 @@ class Application
void on_quit_activate();
void on_export_png_activate();
void on_open_activate();
+ void on_save_activate();
Gtk::Main gtk_main;
Gtk::Window main_win;
Gtk::Statusbar sbar;
- Gtk::FileChooserDialog save_dlg, open_dlg;
+ Gtk::FileChooserDialog export_png_dlg, save_dlg, open_dlg;
+ Gtk::FileFilter elfelli_xml, all;
Glib::RefPtr<Gtk::ActionGroup> action_group;
Glib::RefPtr<Gtk::UIManager> ui_manager;
M src/SConscript => src/SConscript +1 -0
@@ 7,6 7,7 @@ elfelli_sources = ['Application.cpp',
'Simulation.cpp',
'SimulationCanvas.cpp',
'XmlLoader.cpp',
+ 'XmlWriter.cpp',
'Main.cpp']
elfelli = env.Program('elfelli', elfelli_sources)
A src/XmlWriter.cpp => src/XmlWriter.cpp +68 -0
@@ 0,0 1,68 @@
+/*
+ * XmlWriter.cpp
+ * Copyright (C) 2006 Johann Rudloff
+ *
+ * This program 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
+ * (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <vector>
+#include <fstream>
+
+#include "XmlWriter.h"
+
+namespace Elfelli
+{
+
+namespace XmlWriter
+{
+
+const char *version_string = "elfelli-xml-1";
+
+bool write(const std::string& filename, const Simulation *sim)
+{
+ std::ofstream out(filename.c_str(), std::ios::out);
+
+ const std::vector<Body>& bodies = sim->get_bodies();
+ const std::vector<PlateBody>& plates = sim->get_plates();
+
+ out << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
+ out << "<scene version=\"" << version_string << "\">\n";
+
+ std::vector<Body>::const_iterator b_iter;
+ for(b_iter = bodies.begin(); b_iter != bodies.end(); b_iter++)
+ {
+ out << " <point x=\"" << (*b_iter).pos.get_x() << "\" "
+ << "y=\"" << (*b_iter).pos.get_y() << "\" "
+ << "charge=\"" << (*b_iter).charge << "\" />\n";
+ }
+
+ std::vector<PlateBody>::const_iterator p_iter;
+ for(p_iter = plates.begin(); p_iter != plates.end(); p_iter++)
+ {
+ out << " <plate x1=\"" << (*p_iter).pos_a.get_x() << "\" "
+ << "y1=\"" << (*p_iter).pos_a.get_y() << "\" "
+ << "x2=\"" << (*p_iter).pos_b.get_x() << "\" "
+ << "y2=\"" << (*p_iter).pos_b.get_y() << "\" "
+ << "charge=\"" << (*p_iter).charge << "\" />\n";
+ }
+
+ out << "</scene>\n";
+ out.close();
+ return true;
+}
+
+}
+
+}
A src/XmlWriter.h => src/XmlWriter.h +38 -0
@@ 0,0 1,38 @@
+// -*- C++ -*-
+/*
+ * XmlWriter.h
+ * Copyright (C) 2006 Johann Rudloff
+ *
+ * This program 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
+ * (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef _XML_WRITER_H_
+#define _XML_WRITER_H_
+
+#include <string>
+
+#include "Simulation.h"
+
+namespace Elfelli
+{
+
+namespace XmlWriter
+{
+ bool write(const std::string& filename, const Simulation *sim);
+}
+
+}
+
+#endif // _XML_WRITER_H_