mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 18:47:44 +00:00
PixelPaint: Extract common scope code into a generic ScopeWidget
When we add more scopes, this will facilitate code sharing.
This commit is contained in:
parent
8b60305698
commit
fe88fd22fa
5 changed files with 86 additions and 45 deletions
|
@ -52,6 +52,7 @@ set(SOURCES
|
||||||
ProjectLoader.cpp
|
ProjectLoader.cpp
|
||||||
ResizeImageDialog.cpp
|
ResizeImageDialog.cpp
|
||||||
ResizeImageDialogGML.h
|
ResizeImageDialogGML.h
|
||||||
|
ScopeWidget.cpp
|
||||||
Selection.cpp
|
Selection.cpp
|
||||||
ToolPropertiesWidget.cpp
|
ToolPropertiesWidget.cpp
|
||||||
ToolboxWidget.cpp
|
ToolboxWidget.cpp
|
||||||
|
|
|
@ -16,25 +16,6 @@ REGISTER_WIDGET(PixelPaint, HistogramWidget);
|
||||||
|
|
||||||
namespace PixelPaint {
|
namespace PixelPaint {
|
||||||
|
|
||||||
HistogramWidget::~HistogramWidget()
|
|
||||||
{
|
|
||||||
if (m_image)
|
|
||||||
m_image->remove_client(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void HistogramWidget::set_image(Image* image)
|
|
||||||
{
|
|
||||||
if (m_image == image)
|
|
||||||
return;
|
|
||||||
if (m_image)
|
|
||||||
m_image->remove_client(*this);
|
|
||||||
m_image = image;
|
|
||||||
if (m_image)
|
|
||||||
m_image->add_client(*this);
|
|
||||||
|
|
||||||
(void)rebuild_histogram_data();
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<void> HistogramWidget::rebuild_histogram_data()
|
ErrorOr<void> HistogramWidget::rebuild_histogram_data()
|
||||||
{
|
{
|
||||||
if (!m_image)
|
if (!m_image)
|
||||||
|
@ -81,16 +62,15 @@ ErrorOr<void> HistogramWidget::rebuild_histogram_data()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scale the frequency values to fit the widgets height.
|
// Scale the frequency values to fit the widgets height.
|
||||||
m_widget_height = height();
|
auto widget_height = height();
|
||||||
|
|
||||||
for (int i = 0; i < 256; i++) {
|
for (int i = 0; i < 256; i++) {
|
||||||
m_data.red[i] = m_data.red[i] != 0 ? (static_cast<float>(m_data.red[i]) / max_color_frequency) * m_widget_height : 0;
|
m_data.red[i] = m_data.red[i] != 0 ? (static_cast<float>(m_data.red[i]) / max_color_frequency) * widget_height : 0;
|
||||||
m_data.green[i] = m_data.green[i] != 0 ? (static_cast<float>(m_data.green[i]) / max_color_frequency) * m_widget_height : 0;
|
m_data.green[i] = m_data.green[i] != 0 ? (static_cast<float>(m_data.green[i]) / max_color_frequency) * widget_height : 0;
|
||||||
m_data.blue[i] = m_data.blue[i] != 0 ? (static_cast<float>(m_data.blue[i]) / max_color_frequency) * m_widget_height : 0;
|
m_data.blue[i] = m_data.blue[i] != 0 ? (static_cast<float>(m_data.blue[i]) / max_color_frequency) * widget_height : 0;
|
||||||
m_data.brightness[i] = m_data.brightness[i] != 0 ? (static_cast<float>(m_data.brightness[i]) / max_brightness_frequency) * m_widget_height : 0;
|
m_data.brightness[i] = m_data.brightness[i] != 0 ? (static_cast<float>(m_data.brightness[i]) / max_brightness_frequency) * widget_height : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
update();
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +82,7 @@ void HistogramWidget::paint_event(GUI::PaintEvent& event)
|
||||||
if (!m_image)
|
if (!m_image)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int bottom_line = m_widget_height - 1;
|
int bottom_line = height() - 1;
|
||||||
float step_width = static_cast<float>(width()) / 256;
|
float step_width = static_cast<float>(width()) / 256;
|
||||||
|
|
||||||
Gfx::Path brightness_path;
|
Gfx::Path brightness_path;
|
||||||
|
@ -153,14 +133,6 @@ void HistogramWidget::paint_event(GUI::PaintEvent& event)
|
||||||
void HistogramWidget::image_changed()
|
void HistogramWidget::image_changed()
|
||||||
{
|
{
|
||||||
(void)rebuild_histogram_data();
|
(void)rebuild_histogram_data();
|
||||||
}
|
|
||||||
|
|
||||||
void HistogramWidget::set_color_at_mouseposition(Color color)
|
|
||||||
{
|
|
||||||
if (m_color_at_mouseposition == color)
|
|
||||||
return;
|
|
||||||
|
|
||||||
m_color_at_mouseposition = color;
|
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,21 +7,17 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Image.h"
|
#include "Image.h"
|
||||||
#include <LibGUI/AbstractScrollableWidget.h>
|
#include "ScopeWidget.h"
|
||||||
|
|
||||||
namespace PixelPaint {
|
namespace PixelPaint {
|
||||||
|
|
||||||
class HistogramWidget final
|
class HistogramWidget final
|
||||||
: public GUI::Frame
|
: public ScopeWidget {
|
||||||
, ImageClient {
|
|
||||||
C_OBJECT(HistogramWidget);
|
C_OBJECT(HistogramWidget);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual ~HistogramWidget() override;
|
virtual ~HistogramWidget() = default;
|
||||||
|
virtual void image_changed() override;
|
||||||
void set_image(Image*);
|
|
||||||
void image_changed();
|
|
||||||
void set_color_at_mouseposition(Color);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HistogramWidget() = default;
|
HistogramWidget() = default;
|
||||||
|
@ -29,9 +25,6 @@ private:
|
||||||
virtual void paint_event(GUI::PaintEvent&) override;
|
virtual void paint_event(GUI::PaintEvent&) override;
|
||||||
|
|
||||||
ErrorOr<void> rebuild_histogram_data();
|
ErrorOr<void> rebuild_histogram_data();
|
||||||
int m_widget_height = 0;
|
|
||||||
Color m_color_at_mouseposition = Color::Transparent;
|
|
||||||
RefPtr<Image> m_image;
|
|
||||||
|
|
||||||
struct HistogramData {
|
struct HistogramData {
|
||||||
Vector<int> red;
|
Vector<int> red;
|
||||||
|
|
41
Userland/Applications/PixelPaint/ScopeWidget.cpp
Normal file
41
Userland/Applications/PixelPaint/ScopeWidget.cpp
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "ScopeWidget.h"
|
||||||
|
#include "Layer.h"
|
||||||
|
|
||||||
|
namespace PixelPaint {
|
||||||
|
|
||||||
|
ScopeWidget::~ScopeWidget()
|
||||||
|
{
|
||||||
|
if (m_image)
|
||||||
|
m_image->remove_client(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScopeWidget::set_image(Image* image)
|
||||||
|
{
|
||||||
|
if (m_image == image)
|
||||||
|
return;
|
||||||
|
if (m_image)
|
||||||
|
m_image->remove_client(*this);
|
||||||
|
m_image = image;
|
||||||
|
if (m_image)
|
||||||
|
m_image->add_client(*this);
|
||||||
|
|
||||||
|
image_changed();
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScopeWidget::set_color_at_mouseposition(Color color)
|
||||||
|
{
|
||||||
|
if (m_color_at_mouseposition == color)
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_color_at_mouseposition = color;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
34
Userland/Applications/PixelPaint/ScopeWidget.h
Normal file
34
Userland/Applications/PixelPaint/ScopeWidget.h
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Image.h"
|
||||||
|
#include <LibCore/Object.h>
|
||||||
|
#include <LibGUI/Frame.h>
|
||||||
|
|
||||||
|
namespace PixelPaint {
|
||||||
|
|
||||||
|
class ScopeWidget
|
||||||
|
: public GUI::Frame
|
||||||
|
, public ImageClient {
|
||||||
|
C_OBJECT_ABSTRACT(ScopeWidget);
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual ~ScopeWidget() override;
|
||||||
|
|
||||||
|
void set_image(Image*);
|
||||||
|
virtual void image_changed() = 0;
|
||||||
|
void set_color_at_mouseposition(Color);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void paint_event(GUI::PaintEvent&) override = 0;
|
||||||
|
|
||||||
|
Color m_color_at_mouseposition = Color::Transparent;
|
||||||
|
RefPtr<Image> m_image;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue