Here's an updated version of the code that uses GTK4 and its corresponding C++ wrapper, gtkmm-4.0.
#include <gtkmm.h>
#include <nlohmann/json.hpp>
#include <fstream>
using json = nlohmann::json;
class PathEditor : public Gtk::ApplicationWindow {
Gtk::DrawingArea drawing_area;
std::vector<std::vector<Gdk::Point>> paths;
std::vector<Gdk::Point> current_path;
bool drawing = false;
public:
PathEditor() {
set_default_size(800, 600);
set_title("Simple Path Editor");
drawing_area.set_draw_func([this](Gtk::DrawingArea& da, const Cairo::RefPtr<Cairo::Context>& cr) {
on_draw(da, cr);
});
drawing_area.signal_button_press_event().connect(sigc::mem_fun(*this, &PathEditor::on_button_press));
drawing_area.signal_motion_notify_event().connect(sigc::mem_fun(*this, &PathEditor::on_motion));
drawing_area.signal_button_release_event().connect(sigc::mem_fun(*this, &PathEditor::on_button_release));
Gtk::Box box(Gtk::Orientation::VERTICAL, 10);
box.append(drawing_area);
Gtk::Button import_button("Import JSON");
import_button.signal_clicked().connect(sigc::mem_fun(*this, &PathEditor::import_json));
box.append(import_button);
Gtk::Button export_button("Export JSON");
export_button.signal_clicked().connect(sigc::mem_fun(*this, &PathEditor::export_json));
box.append(export_button);
set_child(box);
}
void on_draw(Gtk::DrawingArea& da, const Cairo::RefPtr<Cairo::Context>& cr) {
cr->set_source_rgb(1, 1, 1);
cr->paint();
cr->set_source_rgb(0, 0, 0);
for (const auto& path : paths) {
for (size_t i = 0; i < path.size() - 1; ++i) {
cr->move_to(path[i].x, path[i].y);
cr->line_to(path[i + 1].x, path[i + 1].y);
}
cr->stroke();
}
if (!current_path.empty()) {
for (size_t i = 0; i < current_path.size() - 1; ++i) {
cr->move_to(current_path[i].x, current_path[i].y);
cr->line_to(current_path[i + 1].x, current_path[i + 1].y);
}
cr->stroke();
}
}
bool on_button_press(Gdk::EventButton* event) {
if (event->button == 1) {
drawing = true;
current_path.clear();
current_path.push_back(Gdk::Point(event->x, event->y));
}
return true;
}
bool on_motion(Gdk::EventMotion* event) {
if (drawing) {
current_path.push_back(Gdk::Point(event->x, event->y));
drawing_area.queue_draw();
}
return true;
}
bool on_button_release(Gdk::EventButton* event) {
if (drawing) {
drawing = false;
paths.push_back(current_path);
current_path.clear();
drawing_area.queue_draw();
}
return true;
}
void import_json() {
Gtk::FileChooserDialog dialog("Import JSON", *this, Gtk::FileChooser::Action::OPEN);
dialog.add_button("_Cancel", Gtk::ResponseType::CANCEL);
dialog.add_button("_Open", Gtk::ResponseType::ACCEPT);
auto filter_json = Gtk::FileFilter::create();
filter_json->set_name("JSON files");
filter_json->add_mime_type("application/json");
dialog.add_filter(filter_json);
if (dialog.run() == Gtk::ResponseType::ACCEPT) {
std::ifstream file(dialog.get_filename());
json data = json::parse(file);
paths = json_to_paths(data);
drawing_area.queue_draw();
}
}
void export_json() {
Gtk::FileChooserDialog dialog("Export JSON", *this, Gtk::FileChooser::Action::SAVE);
dialog.add_button("_Cancel", Gtk::ResponseType::CANCEL);
dialog.add_button("_Save", Gtk::ResponseType::ACCEPT);
auto filter_json = Gtk::FileFilter::create();
filter_json->set_name("JSON files");
filter_json->add_mime_type("application/json");
dialog.add_filter(filter_json);
if (dialog.run() == Gtk::ResponseType::ACCEPT) {
std::ofstream file(dialog.get_filename());
file << paths_to_json(paths).dump(4);
}
}
private:
json paths_to_json(const std::vector<std::vector<Gdk::Point>>& paths) {
json data = json::array();
for (const auto& path : paths) {
json path_data = json::array();
for (const auto& point : path) {
path_data.push_back({{"x", point.x}, {"y", point.y}});
}
data.push_back(path_data);
}
return data;
}
std::vector<std::vector<Gdk::Point>> json_to_paths(const json& data) {
std::vector<std::vector<Gdk::Point>> paths;
for (const auto& path_data : data) {
std::vector<Gdk::Point> path;
for (const auto& point_data : path_data) {
path.emplace_back(point_data["x"], point_data["y"]);
}
paths.push_back(path);
}
return paths;
}
};
int main(int argc, char* argv[]) {
auto app = Gtk::Application::create("org.example.patheditor");
PathEditor window;
return app->run(window);
}
Explanation:
The changes made to the code include:
- Replaced
Gtk::DrawingArea::signal_draw() with Gtk::DrawingArea::set_draw_func().
- Replaced
Gtk::FileChooserDialog constructors with Gtk::FileChooserDialog constructor that takes a parent window.
- Replaced
Gtk::RESPONSE_CANCEL and Gtk::RESPONSE_OK with Gtk::ResponseType::CANCEL and Gtk::ResponseType::ACCEPT.
- Replaced
add() with append() for Gtk::Box.
- Replaced
show_all_children() with nothing, as it is not needed in GTK4.
Compiling the Code:
Save the code to a file (e.g., path_editor.cpp) and compile it using:
g++ path_editor.cpp -o path_editor `pkg-config --cflags --libs gtk4 gtkmm-4.0` -lnlohmann_json
Run the executable:
./path_editor
This will open a window where you can draw paths, import, and export them as JSON.