~cypheon/elfelli

8564e24c7e7086b198b35034e86a57d591c16a69 — Johann Rudloff 14 years ago 78cf533
Preparations for easy changing of the charge.
5 files changed, 136 insertions(+), 41 deletions(-)

M data/ui.xml
M src/Application.cpp
M src/Application.h
M src/SimulationCanvas.cpp
M src/SimulationCanvas.h
M data/ui.xml => data/ui.xml +2 -0
@@ 17,6 17,8 @@
      <menuitem action="AddPositive"/>
      <menuitem action="AddNegativePlate"/>
      <menuitem action="AddPositivePlate"/>
      <separator/>
      <menuitem action="Remove"/>
    </menu>
    <menu action="MenuHelp">
      <menuitem action="About"/>

M src/Application.cpp => src/Application.cpp +80 -22
@@ 284,6 284,11 @@ void Application::reset_simulation()
  sim_canvas.refresh();
}

void Application::on_remove_selected_activate()
{
  sim_canvas.delete_selected();
}

void Application::setup_stock_items()
{
  Glib::RefPtr<IconFactory> fact = IconFactory::create();


@@ 336,30 341,39 @@ void Application::setup_stock_items()

bool Application::setup_ui_actions()
{
  action_group = ActionGroup::create();

  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) , sigc::mem_fun(*this, &Application::on_save_activate));
  action_group->add( Action::create("ExportPNG", Stock::EXPORT_PNG) , sigc::mem_fun(*this, &Application::on_export_png_activate));
  action_group->add( Action::create("ExportSVG", Stock::EXPORT_SVG) );
  action_group->add( Action::create("Quit", Stock::QUIT) , sigc::mem_fun(*this, &Application::quit));

  action_group->add( Action::create("MenuEdit", _("E_dit")) );
  action_group->add( Action::create("Clear", Stock::CLEAR, "", _("Remove all objects")) , sigc::mem_fun(*this, &Application::reset_simulation));
  action_group->add( Action::create("AddNegative", Stock::ADD_NEGATIVE, "", _("Add new negative body")) , sigc::mem_fun(*this, &Application::on_add_negative_body_clicked));
  action_group->add( Action::create("AddPositive", Stock::ADD_POSITIVE, "", _("Add new positive body")) , sigc::mem_fun(*this, &Application::on_add_positive_body_clicked));
  action_group->add( Action::create("AddNegativePlate", Stock::ADD_NEGATIVE_PLATE, "", _("Add new negative plate")) , sigc::mem_fun(*this, &Application::on_add_negative_plate_clicked));
  action_group->add( Action::create("AddPositivePlate", Stock::ADD_POSITIVE_PLATE, "", _("Add new positive plate")) , sigc::mem_fun(*this, &Application::on_add_positive_plate_clicked));

  action_group->add( Action::create("MenuHelp", _("_Help")) );
  action_group->add( Action::create("About", Stock::ABOUT) , sigc::mem_fun(*this, &Application::on_about_activate));

  ui_manager = UIManager::create();
  ui_manager->insert_action_group(action_group);
  main_win.add_accel_group(ui_manager->get_accel_group());

  general_actions = ActionGroup::create();

  general_actions->add( Action::create("MenuScene", _("_Scene")) );
  general_actions->add( Action::create("New", Stock::NEW) , sigc::mem_fun(*this, &Application::reset_simulation));
  general_actions->add( Action::create("Open", Stock::OPEN) , sigc::mem_fun(*this, &Application::on_open_activate));
  general_actions->add( Action::create("SaveAs", Stock::SAVE_AS) , sigc::mem_fun(*this, &Application::on_save_activate));
  general_actions->add( Action::create("ExportPNG", Stock::EXPORT_PNG) , sigc::mem_fun(*this, &Application::on_export_png_activate));
  general_actions->add( Action::create("ExportSVG", Stock::EXPORT_SVG) );
  general_actions->add( Action::create("Quit", Stock::QUIT) , sigc::mem_fun(*this, &Application::quit));

  general_actions->add( Action::create("MenuEdit", _("E_dit")) );
  general_actions->add( Action::create("Clear", Stock::CLEAR, "", _("Remove all objects")) , sigc::mem_fun(*this, &Application::reset_simulation));
  general_actions->add( Action::create("AddNegative", Stock::ADD_NEGATIVE, "", _("Add new negative body")) , sigc::mem_fun(*this, &Application::on_add_negative_body_clicked));
  general_actions->add( Action::create("AddPositive", Stock::ADD_POSITIVE, "", _("Add new positive body")) , sigc::mem_fun(*this, &Application::on_add_positive_body_clicked));
  general_actions->add( Action::create("AddNegativePlate", Stock::ADD_NEGATIVE_PLATE, "", _("Add new negative plate")) , sigc::mem_fun(*this, &Application::on_add_negative_plate_clicked));
  general_actions->add( Action::create("AddPositivePlate", Stock::ADD_POSITIVE_PLATE, "", _("Add new positive plate")) , sigc::mem_fun(*this, &Application::on_add_positive_plate_clicked));

  general_actions->add( Action::create("MenuHelp", _("_Help")) );
  general_actions->add( Action::create("About", Stock::ABOUT) , sigc::mem_fun(*this, &Application::on_about_activate));

  ui_manager->insert_action_group(general_actions);


  object_actions = ActionGroup::create();
  object_actions->add( Action::create("Remove", Stock::REMOVE, "", _("Remove this object")) , sigc::mem_fun(*this, &Application::on_remove_selected_activate));
  object_actions->set_sensitive(false);

  ui_manager->insert_action_group(object_actions);


  main_win.add_accel_group(ui_manager->get_accel_group());
  ui_manager->add_ui_from_file(find_datafile("ui.xml"));

  return true;


@@ 393,6 407,45 @@ void Application::setup_file_chooser_dialogs()
  open_dlg.add_filter(all);
}

Widget *Application::build_object_toolbar()
{
  Alignment *al = new Alignment(0.5, 0.5, 1.0, 0.0);
  HBox *tb = new HBox;
  al->add(*manage(tb));
  al->set_padding(2, 2, 0, 0);
  al->set_sensitive(false);


  Tooltips *tips = new Tooltips;
  Button *btn;
  Image *img;

  btn = manage(new Button);
  img = new Image(Stock::REMOVE, Gtk::ICON_SIZE_SMALL_TOOLBAR);
  btn->unset_flags(CAN_FOCUS);
  btn->set_image(*manage(img));
  btn->set_relief(RELIEF_NONE);
  tips->set_tip(*btn, _("Remove this object"));
  tb->pack_start(*btn, false, false);

  int width = img->size_request().width + 4 + 2*btn->get_style()->get_xthickness();
  int height = img->size_request().height + 4 + 2*btn->get_style()->get_ythickness();
  btn->set_size_request(width, height);


  HBox *charge_box = manage(new HBox);
  charge_box->pack_start(*manage(new Label(_("Charge:"))));
  SpinButton *charge_spin = manage(new SpinButton);
  charge_box->pack_start(*charge_spin);
  Alignment *charge_al = manage(new Alignment);
  charge_al->add(*charge_box);
  charge_al->set_padding(0, 0, 15, 0);
  tb->pack_start(*charge_al, false, false);


  return al;
}

bool Application::build_gui()
{
  VBox *vbox1 = manage(new VBox);


@@ 411,6 464,11 @@ bool Application::build_gui()
  Toolbox *main_toolbox = manage(new Toolbox(main_toolbar));
  vbox1->pack_start(*main_toolbox, false, false);

  Gtk::Widget *object_toolbar = manage(build_object_toolbar());
  HandleBox *object_toolbox = manage(new HandleBox);
  object_toolbox->add(*object_toolbar);
  vbox1->pack_start(*object_toolbox, false, false);

  vbox1->pack_start(sim_canvas);
  sim_canvas.set_size_request(640, 480);


M src/Application.h => src/Application.h +3 -1
@@ 60,6 60,7 @@ public:
private:
  void setup_gettext();

  Gtk::Widget *build_object_toolbar();
  bool build_gui();
  bool setup_ui_actions();
  void setup_file_chooser_dialogs();


@@ 68,6 69,7 @@ private:
  void reset_simulation();

  bool on_expose(GdkEventExpose *event);
  void on_remove_selected_activate();
  void on_add_positive_body_clicked();
  void on_add_negative_body_clicked();
  void on_add_positive_plate_clicked();


@@ 86,7 88,7 @@ private:
  Gtk::FileChooserDialog export_png_dlg, save_dlg, open_dlg;
  Gtk::FileFilter elfelli_xml, all;

  Glib::RefPtr<Gtk::ActionGroup> action_group;
  Glib::RefPtr<Gtk::ActionGroup> general_actions, object_actions;
  Glib::RefPtr<Gtk::UIManager> ui_manager;

  SimulationCanvas sim_canvas;

M src/SimulationCanvas.cpp => src/SimulationCanvas.cpp +48 -18
@@ 146,10 146,38 @@ bool SimulationCanvas::delete_selected()
  return r;
}

bool SimulationCanvas::change_selected_charge(float delta)
float SimulationCanvas::get_selected_charge()
{
  int n;

  if(active < 0)
    return 0;

  if(active < 1024)
  {
    n = active;
    if(n < bodies.size())
    {
      return bodies[n].charge;
    }
  }
  else
  {
    n = active-1024;
    if(n < plates.size())
    {
      return plates[n].charge;
    }
  }

  refresh();

  return 0;
}

bool SimulationCanvas::set_selected_charge(float value)
{
  int n;
  float new_charge;

  if(active < 0)
    return false;


@@ 159,14 187,7 @@ bool SimulationCanvas::change_selected_charge(float delta)
    n = active;
    if(n < bodies.size())
    {
      float& charge = bodies[n].charge;
      new_charge = charge + sign(charge)*delta;
      if((fabs(new_charge) > 0.001)
         && (fabs(new_charge) <= MAX_CHARGE)
         && (sign(new_charge) == sign(charge)))
      {
        charge = new_charge;
      }
      bodies[n].charge = value;
    }
  }
  else


@@ 174,14 195,7 @@ bool SimulationCanvas::change_selected_charge(float delta)
    n = active-1024;
    if(n < plates.size())
    {
      float& charge = plates[n].charge;
      new_charge = charge + sign(charge)*delta;
      if((fabs(new_charge) > 0.001)
         && (fabs(new_charge) <= MAX_CHARGE)
         && (sign(new_charge) == sign(charge)))
      {
        charge = new_charge;
      }
      plates[n].charge = value;
    }
  }



@@ 190,6 204,22 @@ bool SimulationCanvas::change_selected_charge(float delta)
  return true;
}

bool SimulationCanvas::change_selected_charge(float delta)
{
  float new_charge;

  float charge = get_selected_charge();
  new_charge = charge + sign(charge)*delta;
  if((fabs(new_charge) > 0.01)
     && (fabs(new_charge) <= MAX_CHARGE)
     && (sign(new_charge) == sign(charge)))
  {
    set_selected_charge(new_charge);
  }

  return true;
}

void SimulationCanvas::run()
{
  profile_func_start(__PRETTY_FUNCTION__);

M src/SimulationCanvas.h => src/SimulationCanvas.h +3 -0
@@ 60,6 60,9 @@ public:
  bool delete_plate(int n);
  bool delete_selected();

  bool set_selected_charge(float value);
  float get_selected_charge();

  bool change_selected_charge(float delta);
  bool increase_selected_charge(){return change_selected_charge(1);};
  bool decrease_selected_charge(){return change_selected_charge(-1);};