Scroom  0.14
PipetteHandler Class Reference

#include <pipette.hh>

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

Public Types

using Ptr = boost::shared_ptr< PipetteHandler >
 
- Public Types inherited from ToolStateListener
using Ptr = boost::shared_ptr< ToolStateListener >
 
- Public Types inherited from PostRenderer
using Ptr = boost::shared_ptr< PostRenderer >
 
- Public Types inherited from SelectionListener
using Ptr = boost::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 PostRenderer
virtual void render (boost::shared_ptr< ViewInterface > const &vi, cairo_t *cr, Scroom::Utils::Rectangle< double > presentationArea, int zoom)=0
 
- Public Member Functions inherited from SelectionListener
virtual void onSelectionStart (Selection selection, boost::shared_ptr< ViewInterface > view)=0
 
virtual void onSelectionUpdate (Selection selection, boost::shared_ptr< ViewInterface > view)=0
 
virtual void onSelectionEnd (Selection selection, boost::shared_ptr< ViewInterface > view)=0
 
- 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 >
boost::shared_ptr< R > shared_from_this ()
 
template<typename R >
boost::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()}
 

Member Typedef Documentation

◆ Ptr

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

Member Function Documentation

◆ computeValues()

void PipetteHandler::computeValues ( const ViewInterface::Ptr view,
Scroom::Utils::Rectangle< double >  sel_rect 
)
virtual
61 {
62  jobMutex.lock();
63 
64  Scroom::GtkHelpers::sync_on_ui_thread([=] { view->setStatusMessage("Computing color values..."); });
65 
66  // Get the average color within the rectangle
67  PresentationInterface::Ptr const presentation = view->getCurrentPresentation();
68  auto pipette = boost::dynamic_pointer_cast<PipetteViewInterface>(presentation);
69  if(pipette == nullptr || !presentation->isPropertyDefined(PIPETTE_PROPERTY_NAME))
70  {
71  spdlog::error("Presentation does not implement PipetteViewInterface!");
72  Scroom::GtkHelpers::sync_on_ui_thread([=] { view->setStatusMessage("Pipette is not supported for this presentation."); });
73  jobMutex.unlock();
74  return;
75  }
76  auto image = presentation->getRect();
77  auto rect = sel_rect.intersection(image);
78  auto colors = pipette->getPixelAverages(rect);
79 
80  const auto display_rect = roundOutward(rect / presentation->getAspectRatio());
81 
82  // If the plugin was switched off ignore the result
83  if(!wasDisabled.test_and_set())
84  {
85  displayValues(view, display_rect, colors);
86  }
87 
88  wasDisabled.clear();
89  jobMutex.unlock();
90 }
Here is the call graph for this function:

◆ create()

PipetteHandler::Ptr PipetteHandler::create ( )
static
58 { return Ptr(new PipetteHandler()); }

Referenced by BOOST_AUTO_TEST_CASE(), and 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
95 {
96  std::stringstream info;
97 
98  info << fmt::format("Top-left: {}, Bottom-right: {}, Height: {}, Width: {}",
99  rect.getTopLeft(),
100  rect.getBottomRight(),
101  rect.getHeight(),
102  rect.getWidth());
103 
104  if(!colors.empty())
105  {
106  info << ", Colors:";
107  for(const auto& [name, value]: colors)
108  {
109  info << fmt::format(" {}: {:.2f}", name, value);
110  }
111  }
112 
113  Scroom::GtkHelpers::sync_on_ui_thread([view, status = info.str()] { view->setStatusMessage(status); });
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
69 { return selection; }

◆ getSelectionType()

std::string PipetteHandler::getSelectionType ( )
overridevirtual

Reimplemented from SelectionListener.

120 { return SelectionType::PIXEL; }
Here is the call graph for this function:

◆ isEnabled()

bool PipetteHandler::isEnabled ( ) const
inline
70 { return enabled; }

◆ onDisable()

void PipetteHandler::onDisable ( )
overridevirtual

Then function is called whenever the tool button is deselected.

Implements ToolStateListener.

179 {
180  selection.reset();
181  enabled = false;
182  wasDisabled.test_and_set();
183 }

◆ onEnable()

void PipetteHandler::onEnable ( )
overridevirtual

This function is called whenever the tool button is selected.

Implements ToolStateListener.

186 {
187  enabled = true;
188  if(jobMutex.try_lock())
189  {
190  wasDisabled.clear();
191  jobMutex.unlock();
192  }
193 }

◆ onSelectionEnd()

void PipetteHandler::onSelectionEnd ( Selection  s,
ViewInterface::Ptr  view 
)
override
135 {
136  if(enabled && jobMutex.try_lock())
137  {
138  selection = s;
139 
140  // Get the selection rectangle
141  const auto sel_rect = Scroom::Utils::make_rect_from_start_end(selection->start, selection->end);
142  Sequentially()->schedule([me = shared_from_this<PipetteHandler>(), view, sel_rect] { me->computeValues(view, sel_rect); },
143  currentJob);
144  jobMutex.unlock();
145  }
146 }
Here is the call graph for this function:

◆ onSelectionStart()

void PipetteHandler::onSelectionStart ( Selection  p,
ViewInterface::Ptr  view 
)
override
122 {}

◆ onSelectionUpdate()

void PipetteHandler::onSelectionUpdate ( Selection  s,
ViewInterface::Ptr  view 
)
override
125 {
126  UNUSED(view);
127  if(enabled && jobMutex.try_lock())
128  {
129  selection = s;
130  jobMutex.unlock();
131  }
132 }

◆ render()

void PipetteHandler::render ( ViewInterface::Ptr const &  vi,
cairo_t *  cr,
Scroom::Utils::Rectangle< double >  presentationArea,
int  zoom 
)
override
156 {
157  if(selection)
158  {
159  const auto pixelSize = pixelSizeFromZoom(zoom);
160  const auto start = (selection->start - presentationArea.getTopLeft()) * pixelSize;
161  const auto end = (selection->end - presentationArea.getTopLeft()) * pixelSize;
162 
163  cairo_set_line_width(cr, 1);
164  cairo_set_source_rgb(cr, 0, 0, 1); // Blue
165  cairo_move_to(cr, end.x, start.y);
166  cairo_line_to(cr, start.x, start.y);
167  cairo_line_to(cr, start.x, end.y);
168  cairo_line_to(cr, end.x, end.y);
169  cairo_line_to(cr, end.x, start.y);
170  cairo_stroke(cr);
171  }
172 }
Here is the call graph for this function:

Member Data Documentation

◆ currentJob

ThreadPool::Queue::Ptr PipetteHandler::currentJob {ThreadPool::Queue::createAsync()}
private

Referenced by onSelectionEnd().

◆ enabled

bool PipetteHandler::enabled {false}
private

◆ jobMutex

std::mutex PipetteHandler::jobMutex
private

◆ 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:
PipetteHandler::Ptr
boost::shared_ptr< PipetteHandler > Ptr
Definition: pipette.hh:27
UNUSED
#define UNUSED(x)
Definition: unused.hh:10
Scroom::Utils::Rectangle::getTopLeft
xy_type getTopLeft() const
Definition: rectangle.hh:133
PipetteHandler::displayValues
virtual void displayValues(const ViewInterface::Ptr &view, Scroom::Utils::Rectangle< double > rect, const PipetteLayerOperations::PipetteColor &colors)
Definition: pipette.cc:92
PipetteHandler::selection
std::optional< Selection > selection
Definition: pipette.hh:30
anonymous_namespace{transparentoverlaypresentation.cc}::colors
const std::list< Color > colors
Definition: transparentoverlaypresentation.cc:21
Scroom::Utils::Rectangle::intersection
Rectangle intersection(const Rectangle &other) const
Definition: rectangle.hh:95
Sequentially
ThreadPool::Ptr Sequentially()
Definition: threadpoolimpl.cc:459
SelectionType::PIXEL
const std::string PIXEL("PixelSelection")
Scroom::Utils::Rectangle::getHeight
value_type getHeight() const
Definition: rectangle.hh:123
PIPETTE_PROPERTY_NAME
const std::string PIPETTE_PROPERTY_NAME
Definition: pipetteviewinterface.hh:14
Scroom::GtkHelpers::sync_on_ui_thread
void sync_on_ui_thread(T f)
Definition: gtk-helpers.hh:59
PipetteHandler::wasDisabled
std::atomic_flag wasDisabled
Definition: pipette.hh:32
PipetteHandler::jobMutex
std::mutex jobMutex
Definition: pipette.hh:33
Scroom::Utils::Rectangle::getBottomRight
xy_type getBottomRight() const
Definition: rectangle.hh:139
PresentationInterface::Ptr
boost::shared_ptr< PresentationInterface > Ptr
Definition: presentationinterface.hh:74
Scroom::Utils::Rectangle::getWidth
value_type getWidth() const
Definition: rectangle.hh:121
PipetteHandler::currentJob
ThreadPool::Queue::Ptr currentJob
Definition: pipette.hh:34
Scroom::Utils::make_rect_from_start_end
Rectangle< T > make_rect_from_start_end(Point< T > start, Point< T > end)
Definition: rectangle.hh:296
pixelSizeFromZoom
double pixelSizeFromZoom(int zoom)
Definition: cairo-helpers.cc:112
PipetteHandler
Definition: pipette.hh:20
Scroom::Utils::roundOutward
Segment< double > roundOutward(Segment< double > s)
Definition: linearsegment.hh:240
PipetteHandler::enabled
bool enabled
Definition: pipette.hh:31