PixelPaint: Make GenericConvolutionFilterInputDialog cancellable

Also tweaks the GUI to look a bit less...bad.
This commit is contained in:
AnotherTest 2020-08-01 12:39:06 +04:30 committed by Andreas Kling
parent 2074c9d66d
commit d366ad8232
Notes: sideshowbarker 2024-07-19 04:24:28 +09:00
12 changed files with 47 additions and 56 deletions

View File

@ -43,13 +43,8 @@ BoxBlurFilter<N>::~BoxBlurFilter()
}
template<size_t N>
void BoxBlurFilter<N>::apply(const Filter::Parameters& parameters)
{
GenericConvolutionFilter<N>::apply(parameters);
}
template<size_t N>
typename GenericConvolutionFilter<N>::Parameters BoxBlurFilter<N>::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect)
OwnPtr<typename GenericConvolutionFilter<N>::Parameters>
BoxBlurFilter<N>::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect)
{
Matrix<N, float> kernel;
@ -61,7 +56,7 @@ typename GenericConvolutionFilter<N>::Parameters BoxBlurFilter<N>::get_parameter
normalize(kernel);
return { bitmap, rect, kernel };
return make<typename GenericConvolutionFilter<N>::Parameters>(bitmap, rect, kernel);
}
}

View File

@ -38,9 +38,7 @@ public:
virtual const char* class_name() const override { return "BoxBlurFilter"; }
virtual void apply(const Filter::Parameters&) override;
typename GenericConvolutionFilter<N>::Parameters get_parameters(Gfx::Bitmap&, const Gfx::IntRect&);
OwnPtr<typename GenericConvolutionFilter<N>::Parameters> get_parameters(Gfx::Bitmap&, const Gfx::IntRect&);
};
}

View File

@ -47,6 +47,8 @@ public:
const Gfx::IntRect& rect() const { return m_target_rect; }
virtual bool is_generic_convolution_filter() const { return false; }
virtual ~Parameters() { }
private:
Gfx::Bitmap& m_target_bitmap;
Gfx::IntRect m_target_rect;

View File

@ -106,12 +106,15 @@ void GenericConvolutionFilter<N>::apply(const Filter::Parameters& parameters)
}
template<size_t N>
typename GenericConvolutionFilter<N>::Parameters GenericConvolutionFilter<N>::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect, GUI::Window* parent_window)
OwnPtr<typename GenericConvolutionFilter<N>::Parameters>
GenericConvolutionFilter<N>::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect, GUI::Window* parent_window)
{
auto input = GenericConvolutionFilterInputDialog<N>::construct(parent_window);
input->exec();
if (input->result() == GUI::Dialog::ExecOK)
return make<Parameters>(bitmap, rect, input->matrix(), input->should_wrap());
return { bitmap, rect, input->matrix(), input->should_wrap() };
return {};
}
template<size_t N>
@ -119,11 +122,18 @@ GenericConvolutionFilterInputDialog<N>::GenericConvolutionFilterInputDialog(Wind
: Dialog(parent_window)
{
// FIXME: Help! Make this GUI less ugly.
StringBuilder builder;
builder.appendf("%zux%zu", N, N);
builder.append(" Convolution");
set_title(builder.string_view());
resize(200, 250);
auto& main_widget = set_main_widget<GUI::Frame>();
main_widget.set_frame_shape(Gfx::FrameShape::Container);
main_widget.set_frame_shadow(Gfx::FrameShadow::Raised);
main_widget.set_fill_with_background_color(true);
main_widget.template set_layout<GUI::VerticalBoxLayout>();
auto& layout = main_widget.template set_layout<GUI::VerticalBoxLayout>();
layout.set_margins({ 4, 4, 4, 4 });
size_t index = 0;
size_t columns = N;
@ -158,7 +168,6 @@ GenericConvolutionFilterInputDialog<N>::GenericConvolutionFilterInputDialog(Wind
wrap_checkbox.set_checked(m_should_wrap);
auto& button = main_widget.template add<GUI::Button>("Done");
button.set_button_style(Gfx::ButtonStyle::CoolBar);
button.on_click = [&](auto) {
m_should_wrap = wrap_checkbox.is_checked();
if (norm_checkbox.is_checked())

View File

@ -79,7 +79,7 @@ public:
virtual void apply(const Filter::Parameters&) override;
Parameters get_parameters(Gfx::Bitmap&, const Gfx::IntRect&, GUI::Window* parent_window);
OwnPtr<Parameters> get_parameters(Gfx::Bitmap&, const Gfx::IntRect&, GUI::Window* parent_window);
};
template<size_t N>

View File

@ -36,17 +36,12 @@ LaplacianFilter::~LaplacianFilter()
{
}
void LaplacianFilter::apply(const Filter::Parameters& parameters)
{
GenericConvolutionFilter::apply(parameters);
}
GenericConvolutionFilter<3>::Parameters LaplacianFilter::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect, bool diagonal)
OwnPtr<GenericConvolutionFilter<3>::Parameters> LaplacianFilter::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect, bool diagonal)
{
if (diagonal)
return { bitmap, rect, Matrix<3, float>(-1, -1, -1, -1, 8, -1, -1, -1, -1) };
return make<GenericConvolutionFilter<3>::Parameters>(bitmap, rect, Matrix<3, float>(-1, -1, -1, -1, 8, -1, -1, -1, -1));
return { bitmap, rect, Matrix<3, float>(0, -1, 0, -1, 4, -1, 0, -1, 0) };
return make<GenericConvolutionFilter<3>::Parameters>(bitmap, rect, Matrix<3, float>(0, -1, 0, -1, 4, -1, 0, -1, 0));
}
}

View File

@ -37,9 +37,7 @@ public:
virtual const char* class_name() const override { return "LaplacianFilter"; }
virtual void apply(const Filter::Parameters&) override;
GenericConvolutionFilter::Parameters get_parameters(Gfx::Bitmap&, const Gfx::IntRect&, bool diagonal);
OwnPtr<GenericConvolutionFilter::Parameters> get_parameters(Gfx::Bitmap&, const Gfx::IntRect&, bool diagonal);
};
}

View File

@ -37,14 +37,9 @@ SharpenFilter::~SharpenFilter()
{
}
void SharpenFilter::apply(const Filter::Parameters& parameters)
OwnPtr<GenericConvolutionFilter<3>::Parameters> SharpenFilter::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect)
{
GenericConvolutionFilter::apply(parameters);
}
GenericConvolutionFilter<3>::Parameters SharpenFilter::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect)
{
return { bitmap, rect, Matrix<3, float>(0, -1, 0, -1, 5, -1, 0, -1, 0) };
return make<GenericConvolutionFilter<3>::Parameters>(bitmap, rect, Matrix<3, float>(0, -1, 0, -1, 5, -1, 0, -1, 0));
}
}

View File

@ -37,9 +37,7 @@ public:
virtual const char* class_name() const override { return "SharpenFilter"; }
virtual void apply(const Filter::Parameters&) override;
GenericConvolutionFilter::Parameters get_parameters(Gfx::Bitmap&, const Gfx::IntRect&);
OwnPtr<GenericConvolutionFilter::Parameters> get_parameters(Gfx::Bitmap&, const Gfx::IntRect&);
};
}

View File

@ -38,14 +38,9 @@ SpatialGaussianBlurFilter<N, T>::~SpatialGaussianBlurFilter()
{
}
template<size_t N, typename T>
void SpatialGaussianBlurFilter<N, T>::apply(const Filter::Parameters& parameters)
{
GenericConvolutionFilter<N>::apply(parameters);
}
template<size_t N, typename _T>
typename GenericConvolutionFilter<N>::Parameters SpatialGaussianBlurFilter<N, _T>::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect)
OwnPtr<typename GenericConvolutionFilter<N>::Parameters>
SpatialGaussianBlurFilter<N, _T>::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect)
{
Matrix<N, float> kernel;
auto sigma = 1.0f;
@ -60,7 +55,7 @@ typename GenericConvolutionFilter<N>::Parameters SpatialGaussianBlurFilter<N, _T
normalize(kernel);
return { bitmap, rect, kernel };
return make<typename GenericConvolutionFilter<N>::Parameters>(bitmap, rect, kernel);
}
}

View File

@ -39,9 +39,7 @@ public:
virtual const char* class_name() const override { return "SpatialGaussianBlurFilter"; }
virtual void apply(const Filter::Parameters&) override;
typename GenericConvolutionFilter<N>::Parameters get_parameters(Gfx::Bitmap&, const Gfx::IntRect&);
OwnPtr<typename GenericConvolutionFilter<N>::Parameters> get_parameters(Gfx::Bitmap&, const Gfx::IntRect&);
};
}

View File

@ -207,44 +207,51 @@ int main(int argc, char** argv)
edge_detect_submenu.add_action(GUI::Action::create("Laplacian (cardinal)", [&](auto&) {
if (auto* layer = image_editor.active_layer()) {
PixelPaint::LaplacianFilter filter;
filter.apply(filter.get_parameters(layer->bitmap(), layer->rect(), false));
if (auto parameters = filter.get_parameters(layer->bitmap(), layer->rect(), false))
filter.apply(*parameters);
}
}));
edge_detect_submenu.add_action(GUI::Action::create("Laplacian (diagonal)", [&](auto&) {
if (auto* layer = image_editor.active_layer()) {
PixelPaint::LaplacianFilter filter;
filter.apply(filter.get_parameters(layer->bitmap(), layer->rect(), true));
if (auto parameters = filter.get_parameters(layer->bitmap(), layer->rect(), true))
filter.apply(*parameters);
}
}));
auto& blur_submenu = spatial_filters_menu.add_submenu("Blur and Sharpen");
blur_submenu.add_action(GUI::Action::create("Gaussian Blur (3x3)", [&](auto&) {
if (auto* layer = image_editor.active_layer()) {
PixelPaint::SpatialGaussianBlurFilter<3> filter;
filter.apply(filter.get_parameters(layer->bitmap(), layer->rect()));
if (auto parameters = filter.get_parameters(layer->bitmap(), layer->rect()))
filter.apply(*parameters);
}
}));
blur_submenu.add_action(GUI::Action::create("Gaussian Blur (5x5)", [&](auto&) {
if (auto* layer = image_editor.active_layer()) {
PixelPaint::SpatialGaussianBlurFilter<5> filter;
filter.apply(filter.get_parameters(layer->bitmap(), layer->rect()));
if (auto parameters = filter.get_parameters(layer->bitmap(), layer->rect()))
filter.apply(*parameters);
}
}));
blur_submenu.add_action(GUI::Action::create("Box Blur (3x3)", [&](auto&) {
if (auto* layer = image_editor.active_layer()) {
PixelPaint::BoxBlurFilter<3> filter;
filter.apply(filter.get_parameters(layer->bitmap(), layer->rect()));
if (auto parameters = filter.get_parameters(layer->bitmap(), layer->rect()))
filter.apply(*parameters);
}
}));
blur_submenu.add_action(GUI::Action::create("Box Blur (5x5)", [&](auto&) {
if (auto* layer = image_editor.active_layer()) {
PixelPaint::BoxBlurFilter<5> filter;
filter.apply(filter.get_parameters(layer->bitmap(), layer->rect()));
if (auto parameters = filter.get_parameters(layer->bitmap(), layer->rect()))
filter.apply(*parameters);
}
}));
blur_submenu.add_action(GUI::Action::create("Sharpen", [&](auto&) {
if (auto* layer = image_editor.active_layer()) {
PixelPaint::SharpenFilter filter;
filter.apply(filter.get_parameters(layer->bitmap(), layer->rect()));
if (auto parameters = filter.get_parameters(layer->bitmap(), layer->rect()))
filter.apply(*parameters);
}
}));
@ -252,7 +259,8 @@ int main(int argc, char** argv)
spatial_filters_menu.add_action(GUI::Action::create("Generic 5x5 Convolution", [&](auto&) {
if (auto* layer = image_editor.active_layer()) {
PixelPaint::GenericConvolutionFilter<5> filter;
filter.apply(filter.get_parameters(layer->bitmap(), layer->rect(), window));
if (auto parameters = filter.get_parameters(layer->bitmap(), layer->rect(), window))
filter.apply(*parameters);
}
}));