diff options
author | Tobias Christiansen <tobyase@serenityos.org> | 2021-08-31 18:10:27 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-01 13:46:44 +0200 |
commit | 7e2028a3cd336a942a5ddacce2f0bd5e636ad0bf (patch) | |
tree | 5207e05975afc515a64be0c9c8c11679e2627cb8 | |
parent | abcb9824856ab9bde7f96163c3565763391cadf2 (diff) | |
download | serenity-7e2028a3cd336a942a5ddacce2f0bd5e636ad0bf.zip |
PixelPaint: Move the conversion to pixels into the EditGuideDialog
This seems like the most appropriate location to put this.
-rw-r--r-- | Userland/Applications/PixelPaint/EditGuideDialog.cpp | 22 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/EditGuideDialog.h | 2 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/main.cpp | 29 |
3 files changed, 31 insertions, 22 deletions
diff --git a/Userland/Applications/PixelPaint/EditGuideDialog.cpp b/Userland/Applications/PixelPaint/EditGuideDialog.cpp index 9b9c32785b..5e8b993c9c 100644 --- a/Userland/Applications/PixelPaint/EditGuideDialog.cpp +++ b/Userland/Applications/PixelPaint/EditGuideDialog.cpp @@ -62,4 +62,26 @@ EditGuideDialog::EditGuideDialog(GUI::Window* parent_window) }; } +Optional<float> EditGuideDialog::offset_as_pixel(const ImageEditor& editor) +{ + float offset = 0; + if (m_offset.ends_with('%')) { + auto percentage = m_offset.substring_view(0, m_offset.length() - 1).to_int(); + if (!percentage.has_value()) + return {}; + + if (orientation() == PixelPaint::Guide::Orientation::Horizontal) + offset = editor.image().size().height() * ((double)percentage.value() / 100.0); + else if (orientation() == PixelPaint::Guide::Orientation::Vertical) + offset = editor.image().size().width() * ((double)percentage.value() / 100.0); + } else { + auto parsed_int = m_offset.to_int(); + if (!parsed_int.has_value()) + return {}; + offset = parsed_int.value(); + } + + return offset; +} + } diff --git a/Userland/Applications/PixelPaint/EditGuideDialog.h b/Userland/Applications/PixelPaint/EditGuideDialog.h index a221d96f30..3f8d44fcc4 100644 --- a/Userland/Applications/PixelPaint/EditGuideDialog.h +++ b/Userland/Applications/PixelPaint/EditGuideDialog.h @@ -19,6 +19,8 @@ public: String const offset() const { return m_offset; } Guide::Orientation orientation() const { return m_orientation; } + Optional<float> offset_as_pixel(ImageEditor const&); + private: EditGuideDialog(GUI::Window* parent_window); diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp index 27fa7ae9d6..aba514a0ff 100644 --- a/Userland/Applications/PixelPaint/main.cpp +++ b/Userland/Applications/PixelPaint/main.cpp @@ -372,28 +372,13 @@ int main(int argc, char** argv) auto add_guide_action = GUI::Action::create( "Add Guide", [&](auto&) { auto dialog = PixelPaint::EditGuideDialog::construct(window); - if (dialog->exec() == GUI::Dialog::ExecOK) { - if (auto* editor = current_image_editor()) { - auto specified_offset = dialog->offset(); - float offset = 0; - if (specified_offset.ends_with('%')) { - auto percentage = specified_offset.substring_view(0, specified_offset.length() - 1).to_int(); - if (!percentage.has_value()) - return; - if (dialog->orientation() == PixelPaint::Guide::Orientation::Horizontal) - offset = editor->image().size().height() * ((double)percentage.value() / 100.0); - else if (dialog->orientation() == PixelPaint::Guide::Orientation::Vertical) - offset = editor->image().size().width() * ((double)percentage.value() / 100.0); - else - VERIFY_NOT_REACHED(); - } else { - auto parsed_int = dialog->offset().to_int(); - if (!parsed_int.has_value()) - return; - offset = parsed_int.value(); - } - editor->add_guide(PixelPaint::Guide::construct(dialog->orientation(), offset)); - } + if (dialog->exec() != GUI::Dialog::ExecOK) + return; + if (auto* editor = current_image_editor()) { + auto offset = dialog->offset_as_pixel(*editor); + if (!offset.has_value()) + return; + editor->add_guide(PixelPaint::Guide::construct(dialog->orientation(), offset.value())); } }, window); |