1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 06:28:13 +00:00

PixelPaint: Make images keep track of their path & title

The title is either "Untitled" (default), or the basename of the
image after we've opened or saved it.
This commit is contained in:
Andreas Kling 2021-06-16 12:08:05 +02:00
parent 35456f035c
commit abc40af809
5 changed files with 45 additions and 1 deletions

View file

@ -10,6 +10,7 @@
#include <AK/JsonObject.h>
#include <AK/JsonObjectSerializer.h>
#include <AK/JsonValue.h>
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/StringBuilder.h>
#include <LibCore/File.h>
@ -51,7 +52,8 @@ RefPtr<Image> Image::try_create_with_size(Gfx::IntSize const& size)
}
Image::Image(Gfx::IntSize const& size)
: m_size(size)
: m_title("Untitled")
, m_size(size)
{
}
@ -131,6 +133,7 @@ Result<NonnullRefPtr<Image>, String> Image::try_create_from_pixel_paint_file(Str
image->add_layer(*layer);
}
image->set_path(file_path);
return image.release_nonnull();
}
@ -152,6 +155,7 @@ Result<NonnullRefPtr<Image>, String> Image::try_create_from_file(String const& f
auto image = Image::try_create_from_bitmap(bitmap.release_nonnull());
if (!image)
return String { "Unable to allocate Image"sv };
image->set_path(file_path);
return image.release_nonnull();
}
@ -418,4 +422,17 @@ void ImageUndoCommand::redo()
undo();
}
void Image::set_title(String title)
{
m_title = move(title);
for (auto* client : m_clients)
client->image_did_change_title(m_title);
}
void Image::set_path(String path)
{
m_path = move(path);
set_title(LexicalPath(m_path).basename());
}
}

View file

@ -30,6 +30,7 @@ public:
virtual void image_did_modify_layer_stack() { }
virtual void image_did_change() { }
virtual void image_select_layer(Layer*) { }
virtual void image_did_change_title(String const&) { }
protected:
virtual ~ImageClient() = default;
@ -76,6 +77,12 @@ public:
size_t index_of(Layer const&) const;
String const& path() const { return m_path; }
void set_path(String);
String const& title() const { return m_title; }
void set_title(String);
private:
explicit Image(Gfx::IntSize const&);
@ -84,6 +91,9 @@ private:
void did_change();
void did_modify_layer_stack();
String m_path;
String m_title;
Gfx::IntSize m_size;
NonnullRefPtrVector<Layer> m_layers;

View file

@ -395,6 +395,12 @@ void ImageEditor::image_did_change()
update();
}
void ImageEditor::image_did_change_title(String const& path)
{
if (on_image_title_change)
on_image_title_change(path);
}
void ImageEditor::image_select_layer(Layer* layer)
{
set_active_layer(layer);

View file

@ -63,6 +63,8 @@ public:
Function<void(Layer*)> on_active_layer_change;
Function<void(String const&)> on_image_title_change;
Gfx::FloatRect layer_rect_to_editor_rect(Layer const&, Gfx::IntRect const&) const;
Gfx::FloatRect image_rect_to_editor_rect(Gfx::IntRect const&) const;
Gfx::FloatRect editor_rect_to_image_rect(Gfx::IntRect const&) const;
@ -86,6 +88,7 @@ private:
virtual void image_did_change() override;
virtual void image_select_layer(Layer*) override;
virtual void image_did_change_title(String const&) override;
GUI::MouseEvent event_adjusted_for_layer(GUI::MouseEvent const&, Layer const&) const;
GUI::MouseEvent event_with_pan_and_scale_applied(GUI::MouseEvent const&) const;

View file

@ -135,6 +135,7 @@ int main(int argc, char** argv)
GUI::MessageBox::show_error(window, String::formatted("Could not save {}: {}", save_path.value(), result.error()));
return;
}
editor->image().set_path(save_path.value());
});
auto menubar = GUI::Menubar::construct();
@ -506,6 +507,13 @@ int main(int argc, char** argv)
layer_properties_widget.set_layer(layer);
};
image_editor.on_image_title_change = [&](auto const& title) {
tab_widget.set_tab_title(image_editor, title);
};
// NOTE: We invoke the above hook directly here to make sure the tab title is set up.
image_editor.on_image_title_change(image->title());
if (image->layer_count())
image_editor.set_active_layer(&image->layer(0));