Commit 0f319481 authored by Grumbel's avatar Grumbel

Keep better track of spawned dialog windows to avoid double spawn

parent b3efa465
......@@ -30,24 +30,25 @@
#include "joystick_calibration_widget.hpp"
#include "joystick_test_widget.hpp"
JoystickTestWidget::JoystickTestWidget(Joystick& joystick_, bool simple_ui)
: Gtk::Dialog(joystick_.get_name()),
joystick(joystick_),
m_simple_ui(simple_ui),
label("<b>" + joystick.get_name() + "</b>\nDevice: " + joystick.get_filename() ,
Gtk::ALIGN_START, Gtk::ALIGN_START),
axis_frame("Axes"),
button_frame("Buttons"),
mapping_button("Mapping"),
calibration_button("Calibration"),
buttonbox(Gtk::BUTTONBOX_SPREAD),
stick1_widget(128, 128),
stick2_widget(128, 128),
stick3_widget(128, 128),
rudder_widget(128, 32),
throttle_widget(32, 128),
left_trigger_widget(32, 128, true),
right_trigger_widget(32, 128, true)
JoystickTestWidget::JoystickTestWidget(JoystickGui& gui, Joystick& joystick_, bool simple_ui) :
Gtk::Dialog(joystick_.get_name()),
m_gui(gui),
joystick(joystick_),
m_simple_ui(simple_ui),
label("<b>" + joystick.get_name() + "</b>\nDevice: " + joystick.get_filename() ,
Gtk::ALIGN_START, Gtk::ALIGN_START),
axis_frame("Axes"),
button_frame("Buttons"),
mapping_button("Mapping"),
calibration_button("Calibration"),
buttonbox(Gtk::BUTTONBOX_SPREAD),
stick1_widget(128, 128),
stick2_widget(128, 128),
stick3_widget(128, 128),
rudder_widget(128, 32),
throttle_widget(32, 128),
left_trigger_widget(32, 128, true),
right_trigger_widget(32, 128, true)
{
label.set_use_markup(true);
label.set_selectable();
......@@ -254,13 +255,13 @@ JoystickTestWidget::button_move(int number, bool value)
void
JoystickTestWidget::on_calibrate()
{
Main::current()->show_calibration_dialog(joystick);
m_gui.show_calibration_dialog();
}
void
JoystickTestWidget::on_mapping()
{
Main::current()->show_mapping_dialog(joystick);
m_gui.show_mapping_dialog();
}
void
......
......@@ -37,11 +37,13 @@
#include "axis_widget.hpp"
class Joystick;
class JoystickGui;
class ButtonWidget;
class JoystickTestWidget : public Gtk::Dialog
{
private:
JoystickGui& m_gui;
Joystick& joystick;
bool m_simple_ui;
......@@ -79,7 +81,7 @@ private:
std::vector<sigc::signal<void, double> > axis_callbacks;
public:
JoystickTestWidget(Joystick& joystick, bool simple_ui);
JoystickTestWidget(JoystickGui& gui, Joystick& joystick, bool simple_ui);
void axis_move(int number, int value);
void button_move(int number, bool value);
......
......@@ -33,6 +33,53 @@
Main* Main::current_ = 0;
JoystickGui::JoystickGui(std::unique_ptr<Joystick> joystick, bool simple_ui, Gtk::Window* parent) :
m_joystick(std::move(joystick)),
m_test_widget(),
m_mapping_widget(),
m_calibration_widget()
{
m_test_widget = std::unique_ptr<JoystickTestWidget>(new JoystickTestWidget(*this, *m_joystick, simple_ui));
if (parent) {
m_test_widget->set_transient_for(*parent);
}
m_test_widget->show_all();
}
void
JoystickGui::show_calibration_dialog()
{
if (m_calibration_widget)
{
m_calibration_widget->present();
}
else
{
m_calibration_widget.reset(new JoystickCalibrationWidget(*m_joystick));
m_calibration_widget->signal_hide().connect([this] { m_calibration_widget.reset(); });
m_calibration_widget->set_transient_for(*m_test_widget);
m_calibration_widget->show_all();
}
}
void
JoystickGui::show_mapping_dialog()
{
if (m_mapping_widget)
{
m_mapping_widget->present();
}
else
{
m_mapping_widget.reset(new JoystickMapWidget(*m_joystick));
m_mapping_widget->signal_hide().connect([this] { m_calibration_widget.reset(); });
m_mapping_widget->set_transient_for(*m_test_widget);
m_mapping_widget->show_all();
}
}
Main::Main() :
Gtk::Application("com.gmail.grumbel.jstest-gtk", Gio::APPLICATION_HANDLES_OPEN),
datadir("data/"),
......@@ -48,44 +95,26 @@ Main::~Main()
JoystickTestWidget*
Main::show_device_property_dialog(const std::string& filename, Gtk::Window* parent)
{
Joystick* joystick = new Joystick(filename);
JoystickTestWidget* dialog = new JoystickTestWidget(*joystick, m_simple_ui);
if (parent) {
dialog->set_transient_for(*parent);
auto it = m_joystick_guis.find(filename);
if (it != m_joystick_guis.end())
{
JoystickTestWidget* widget = it->second->get_test_widget();
widget->present();
return widget;
}
else
{
std::unique_ptr<Joystick> joystick(new Joystick(filename));
std::unique_ptr<JoystickGui> gui(new JoystickGui(std::move(joystick), m_simple_ui, parent));
dialog->signal_hide().connect(sigc::bind(sigc::mem_fun(this, &Main::on_dialog_hide), dialog));
dialog->show_all();
joysticks.push_back(joystick);
dialogs.push_back(dialog);
return dialog;
}
void
Main::show_calibration_dialog(Joystick& joystick)
{
JoystickCalibrationWidget* dialog = new JoystickCalibrationWidget(joystick);
dialog->signal_hide().connect(sigc::bind(sigc::mem_fun(this, &Main::on_dialog_hide), dialog));
dialog->show_all();
dialogs.push_back(dialog);
}
void
Main::show_mapping_dialog(Joystick& joystick)
{
JoystickMapWidget* dialog = new JoystickMapWidget(joystick);
dialog->signal_hide().connect(sigc::bind(sigc::mem_fun(this, &Main::on_dialog_hide), dialog));
dialog->show_all();
dialogs.push_back(dialog);
}
JoystickTestWidget* widget = gui->get_test_widget();
m_joystick_guis[filename] = std::move(gui);
widget->signal_hide().connect([this, filename]{
m_joystick_guis.erase(filename);
});
void
Main::on_dialog_hide(Gtk::Dialog* dialog)
{
dialogs.erase(std::remove(dialogs.begin(), dialogs.end(), dialog), dialogs.end());
delete dialog;
return widget;
}
}
int
......
......@@ -21,10 +21,33 @@
#include <vector>
#include <gtkmm.h>
#include <map>
class Joystick;
class JoystickListWidget;
class JoystickTestWidget;
class JoystickMapWidget;
class JoystickCalibrationWidget;
class JoystickGui
{
private:
std::unique_ptr<Joystick> m_joystick;
std::unique_ptr<JoystickTestWidget> m_test_widget;
std::unique_ptr<JoystickMapWidget> m_mapping_widget;
std::unique_ptr<JoystickCalibrationWidget> m_calibration_widget;
public:
JoystickGui(std::unique_ptr<Joystick> joystick,
bool simple_ui,
Gtk::Window* parent = nullptr);
JoystickTestWidget* get_test_widget() const { return m_test_widget.get(); }
void show_calibration_dialog();
void show_mapping_dialog();
};
class Main : public Gtk::Application
{
......@@ -37,18 +60,13 @@ private:
std::string datadir;
bool m_simple_ui;
std::vector<Joystick*> joysticks;
std::vector<Gtk::Dialog*> dialogs;
void on_dialog_hide(Gtk::Dialog* dialog);
std::map<std::string, std::unique_ptr<JoystickGui> > m_joystick_guis;
public:
Main();
~Main();
JoystickTestWidget* show_device_property_dialog(const std::string& filename, Gtk::Window* parent = nullptr);
void show_calibration_dialog(Joystick& joystick);
void show_mapping_dialog(Joystick& joystick);
static Glib::RefPtr<Main> create();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment