Scroom 0.14-48-ga0fee447
Loading...
Searching...
No Matches
PipetteHandler Class Reference

#include <pipette.hh>

Inheritance diagram for PipetteHandler:
Inheritance graph
Collaboration diagram for PipetteHandler:
Collaboration graph

Public Types

using Ptr = std::shared_ptr< PipetteHandler >
 
- Public Types inherited from ToolStateListener
using Ptr = std::shared_ptr< ToolStateListener >
 
- Public Types inherited from PostRenderer
using Ptr = std::shared_ptr< PostRenderer >
 
- Public Types inherited from SelectionListener
using Ptr = std::shared_ptr< SelectionListener >
 

Public Member Functions

void render (ViewInterface::Ptr const &vi, cairo_t *cr, Scroom::Utils::Rectangle< double > presentationArea, int zoom) override
 
std::string getSelectionType () override
 
void onSelectionStart (Selection p, ViewInterface::Ptr view) override
 
void onSelectionUpdate (Selection s, ViewInterface::Ptr view) override
 
void onSelectionEnd (Selection s, ViewInterface::Ptr view) override
 
void onEnable () override
 
void onDisable () override
 
virtual void computeValues (const ViewInterface::Ptr &view, Scroom::Utils::Rectangle< double > sel_rect)
 
virtual void displayValues (const ViewInterface::Ptr &view, Scroom::Utils::Rectangle< double > rect, const PipetteLayerOperations::PipetteColor &colors)
 
std::optional< SelectiongetSelection () const
 
bool isEnabled () const
 
- Public Member Functions inherited from Scroom::Utils::Base
 Base ()=default
 
 Base (const Base &)=delete
 
 Base (Base &&)=delete
 
Baseoperator= (const Base &)=delete
 
Baseoperator= (Base &&)=delete
 
virtual ~Base ()=default
 
template<typename R >
std::shared_ptr< R > shared_from_this ()
 
template<typename R >
std::shared_ptr< R const > shared_from_this () const
 

Static Public Member Functions

static Ptr create ()
 

Private Attributes

std::optional< Selectionselection
 
bool enabled {false}
 
std::atomic_flag wasDisabled = ATOMIC_FLAG_INIT
 
std::mutex jobMutex
 
ThreadPool::Queue::Ptr currentJob {ThreadPool::Queue::createAsync()}
 
Scroom::Logger logger
 

Member Typedef Documentation

◆ Ptr

using PipetteHandler::Ptr = std::shared_ptr<PipetteHandler>

Member Function Documentation

◆ computeValues()

void PipetteHandler::computeValues ( const ViewInterface::Ptr view,
Scroom::Utils::Rectangle< double >  sel_rect 
)
virtual
63{
64 jobMutex.lock();
65
66 Scroom::GtkHelpers::sync_on_ui_thread([=] { view->setStatusMessage("Computing color values..."); });
67
68 // Get the average color within the rectangle
69 PresentationInterface::Ptr const presentation = view->getCurrentPresentation();
70 auto pipette = std::dynamic_pointer_cast<PipetteViewInterface>(presentation);
71 if(pipette == nullptr || !presentation->isPropertyDefined(PIPETTE_PROPERTY_NAME))
72 {
73 logger->error("Presentation does not implement PipetteViewInterface!");
74 Scroom::GtkHelpers::sync_on_ui_thread([=] { view->setStatusMessage("Pipette is not supported for this presentation."); });
75 jobMutex.unlock();
76 return;
77 }
78 auto image = presentation->getRect();
79 auto rect = sel_rect.intersection(image);
80 auto colors = pipette->getPixelAverages(rect);
81
82 const auto display_rect = roundOutward(rect / getAspectRatio(presentation));
83
84 // If the plugin was switched off ignore the result
85 if(!wasDisabled.test_and_set())
86 {
87 displayValues(view, display_rect, colors);
88 }
89
90 wasDisabled.clear();
91 jobMutex.unlock();
92}
std::mutex jobMutex
Definition pipette.hh:34
virtual void displayValues(const ViewInterface::Ptr &view, Scroom::Utils::Rectangle< double > rect, const PipetteLayerOperations::PipetteColor &colors)
Definition pipette.cc:94
std::atomic_flag wasDisabled
Definition pipette.hh:33
Scroom::Logger logger
Definition pipette.hh:36
std::shared_ptr< PresentationInterface > Ptr
Definition presentationinterface.hh:73
Rectangle intersection(const Rectangle &other) const
Definition rectangle.hh:95
void sync_on_ui_thread(T f)
Definition gtk-helpers.hh:59
Segment< double > roundOutward(Segment< double > s)
Definition linearsegment.hh:240
const std::list< Color > colors
Definition transparentoverlaypresentation.cc:22
const auto view
Definition pipette-tests.cc:227
Pipette::Ptr const pipette
Definition pipette-tests.cc:212
const std::string PIPETTE_PROPERTY_NAME
Definition pipetteviewinterface.hh:14
const auto rect
Definition rectangletests.cc:335
Scroom::Utils::Point< double > getAspectRatio(const PresentationInterface::Ptr &presentation)
Definition transformpresentation.cc:162
Here is the call graph for this function:

◆ create()

PipetteHandler::Ptr PipetteHandler::create ( )
static
60{ return std::make_shared<PipetteHandler>(); }

Referenced by Pipette::viewAdded().

Here is the caller graph for this function:

◆ displayValues()

void PipetteHandler::displayValues ( const ViewInterface::Ptr view,
Scroom::Utils::Rectangle< double >  rect,
const PipetteLayerOperations::PipetteColor colors 
)
virtual
99{
100 std::stringstream info;
101
102 info << fmt::format(
103 "Top-left: {}, Bottom-right: {}, Height: {}, Width: {}",
104 rect.getTopLeft(),
105 rect.getBottomRight(),
106 rect.getHeight(),
107 rect.getWidth()
108 );
109
110 if(!colors.empty())
111 {
112 info << ", Colors:";
113 for(const auto& [name, value]: colors)
114 {
115 info << fmt::format(" {}: {:.2f}", name, value);
116 }
117 }
118
119 Scroom::GtkHelpers::sync_on_ui_thread([view, status = info.str()] { view->setStatusMessage(status); });
120}
const uint8_t value
Definition blob-tests.cc:114

Referenced by computeValues().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getSelection()

std::optional< Selection > PipetteHandler::getSelection ( ) const
inline
73{ return selection; }
std::optional< Selection > selection
Definition pipette.hh:31

◆ getSelectionType()

std::string PipetteHandler::getSelectionType ( )
overridevirtual

Reimplemented from SelectionListener.

126{ return SelectionType::PIXEL; }
const std::string PIXEL("PixelSelection")
Here is the call graph for this function:

◆ isEnabled()

bool PipetteHandler::isEnabled ( ) const
inline
74{ return enabled; }
bool enabled
Definition pipette.hh:32

◆ onDisable()

void PipetteHandler::onDisable ( )
overridevirtual

Then function is called whenever the tool button is deselected.

Implements ToolStateListener.

187{
188 selection.reset();
189 enabled = false;
190 wasDisabled.test_and_set();
191}

◆ onEnable()

void PipetteHandler::onEnable ( )
overridevirtual

This function is called whenever the tool button is selected.

Implements ToolStateListener.

194{
195 enabled = true;
196 if(jobMutex.try_lock())
197 {
198 wasDisabled.clear();
199 jobMutex.unlock();
200 }
201}

◆ onSelectionEnd()

void PipetteHandler::onSelectionEnd ( Selection  selection,
ViewInterface::Ptr  view 
)
overridevirtual

This function is called whenever the selection ends. That is, whenever the user releases the mouse button that was pressed.

The final selection is passed as an argument.

See also
Selection

Implements SelectionListener.

140{
141 if(enabled && jobMutex.try_lock())
142 {
143 selection = s;
144
145 // Get the selection rectangle
146 const auto sel_rect = Scroom::Utils::make_rect_from_start_end(selection->start, selection->end);
147 Sequentially()->schedule(
148 [me = shared_from_this<PipetteHandler>(), view, sel_rect] { me->computeValues(view, sel_rect); }, currentJob
149 );
150 jobMutex.unlock();
151 }
152}
ThreadPool::Queue::Ptr currentJob
Definition pipette.hh:35
Rectangle< T > make_rect_from_start_end(Point< T > start, Point< T > end)
Definition rectangle.hh:296
ThreadPool::Ptr Sequentially()
Definition threadpoolimpl.cc:462
BitmapSurface::Ptr const s
Definition transformpresentation_test.cc:70
Here is the call graph for this function:

◆ onSelectionStart()

void PipetteHandler::onSelectionStart ( Selection  selection,
ViewInterface::Ptr  view 
)
overridevirtual

This function is called whenever the user clicks a view. The point that is clicked is passed as an argument.

The passed point is a point in the presentation coordinate space.

Implements SelectionListener.

128{}

◆ onSelectionUpdate()

void PipetteHandler::onSelectionUpdate ( Selection  selection,
ViewInterface::Ptr  view 
)
overridevirtual

This function is called whenever the selection updates. That is, whenever the user moves the mouse while keeping the mouse button pressed.

The updated selection is passed as an argument.

See also
Selection

Implements SelectionListener.

131{
132 if(enabled && jobMutex.try_lock())
133 {
134 selection = s;
135 jobMutex.unlock();
136 }
137}

◆ render()

void PipetteHandler::render ( ViewInterface::Ptr const &  vi,
cairo_t *  cr,
Scroom::Utils::Rectangle< double >  presentationArea,
int  zoom 
)
overridevirtual

This function is called after the presentation finishes redrawing.

Parameters
viThe ViewInterface on whose behalf the request is made
crThe context to with, the origin of the context is translated to be the same as the origin of the presentation.
presentationAreathe area that is to be drawn. The given x and y coordinates should map on 0,0 of the given context cr.
zoomThe requested zoom level. One pixel should have size 2**zoom when drawn. zoom may be negative.

Implements PostRenderer.

164{
165 if(selection)
166 {
167 const auto pixelSize = pixelSizeFromZoom(zoom);
168 const auto start = (selection->start - presentationArea.getTopLeft()) * pixelSize;
169 const auto end = (selection->end - presentationArea.getTopLeft()) * pixelSize;
170
171 cairo_set_line_width(cr, 1);
172 cairo_set_source_rgb(cr, 0, 0, 1); // Blue
173 cairo_move_to(cr, end.x, start.y);
174 cairo_line_to(cr, start.x, start.y);
175 cairo_line_to(cr, start.x, end.y);
176 cairo_line_to(cr, end.x, end.y);
177 cairo_line_to(cr, end.x, start.y);
178 cairo_stroke(cr);
179 }
180}
double pixelSizeFromZoom(int zoom)
Definition cairo-helpers.cc:123
xy_type getTopLeft() const
Definition rectangle.hh:133
const SampleIterator< const uint8_t > start(nullptr, initial_offset, bps)
cairo_t * cr
Definition transformpresentation_test.cc:72
Here is the call graph for this function:

Member Data Documentation

◆ currentJob

ThreadPool::Queue::Ptr PipetteHandler::currentJob {ThreadPool::Queue::createAsync()}
private
static Ptr createAsync()
Definition threadpoolimpl.cc:382

Referenced by onSelectionEnd().

◆ enabled

bool PipetteHandler::enabled {false}
private

◆ jobMutex

std::mutex PipetteHandler::jobMutex
private

◆ logger

Scroom::Logger PipetteHandler::logger
private

Referenced by computeValues().

◆ selection

std::optional<Selection> PipetteHandler::selection
private

◆ wasDisabled

std::atomic_flag PipetteHandler::wasDisabled = ATOMIC_FLAG_INIT
private

Referenced by computeValues(), onDisable(), and onEnable().


The documentation for this class was generated from the following files: