DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
ImageViewer.h
Go to the documentation of this file.
1// This file is part of DynExp.
2
8#pragma once
9
10#include "stdafx.h"
11#include "DynExpCore.h"
12#include "../../MetaInstruments/Camera.h"
13#include "../../MetaInstruments/AnalogOut.h"
14#include "../../Instruments/InterModuleCommunicator.h"
15
16#include "CommonModuleEvents.h"
17#include "ImageViewerEvents.h"
18
19#include <QWidget>
20
21namespace Ui
22{
23 class ImageViewer;
24}
25
27{
28 class ImageViewer;
29
39
41
43 {
44 Q_OBJECT
45
46 public:
48 ~ImageViewerWidget() = default;
49
50 bool AllowResize() const noexcept override final { return true; }
51
52 void SetImage(const QImage& NewImage) noexcept;
53 void SetImageViewEnabled(bool Enable);
54 void SetIntensityHistogram(Util::ImageHistogramType&& NewIntensityHistogram) noexcept;
55 void SetRGBHistogram(Util::ImageRGBHistogramType&& NewRGBHistogram) noexcept;
56 auto GetComputeHistogram() const noexcept;
57 void UpdateScene();
58 auto GetSaveImageFilename() const { return SaveImageFilename; }
60
61 std::unique_ptr<Ui::ImageViewer> ui;
62
63 private:
64 bool eventFilter(QObject* obj, QEvent* event) override;
65 virtual void resizeEvent(QResizeEvent* event) override;
66
67 void UpdateHistogram();
68
75
80 QBarSeries* HistogramBarSeries;
82 QValueAxis* HistogramXAxis;
83 QValueAxis* HistogramYAxis;
84
86 QPixmap Pixmap;
87 QGraphicsPixmapItem* GraphicsPixmapItem;
88 QGraphicsScene* GraphicsScene;
89
92
94
95 private slots:
96 void OnHistogramContextMenuRequested(const QPoint& Position);
97 void OnSaveImageClicked();
98 void OnZoomResetClicked();
99 void OnZoomInClicked();
100 void OnZoomOutClicked();
101 void OnZoomFitClicked(bool Checked);
102 void OnImageMouseMove(QMouseEvent* Event);
103 };
104
144
146 {
147 public:
149 virtual ~ImageViewerParams() = default;
150
151 virtual const char* GetParamClassTag() const noexcept override { return "ImageViewerParams"; }
152
154 "Camera", "Camera", "Underlying camera to be used as an image source", DynExpUI::Icons::Instrument };
156 "Focus", "Focusing voltage (AO)", "Analog output (voltage) to adjust the focus", DynExpUI::Icons::Instrument, true };
157 Param<ParamsConfigDialog::NumberType> AutofocusNumSteps = { *this, "AutofocusNumSteps", "Autofocus step count",
158 "Specifies how fine the focus is scanned during the autofocus algorithm. Higher values lead to higher precision and longer execution times.",
159 false, 10, 3, 10000, 10, 0};
161 "Focus change time (ms)", "Time it takes to change the focus after applying a new focus voltage.",
162 false, 500, 0, 10000, 10, 0 };
164 "InterModuleCommunicator", "Inter-module communicator", "Inter-module communicator to control this module with", DynExpUI::Icons::Instrument, true };
165
166 private:
168 };
169
171 {
172 public:
175
177 virtual ~ImageViewerConfigurator() = default;
178
179 private:
180 virtual DynExp::ParamsBasePtrType MakeParams(DynExp::ItemIDType ID, const DynExp::DynExpCore& Core) const override final { return DynExp::MakeParams<ImageViewerConfigurator>(ID, Core); }
181 };
182
184 {
185 public:
189
190 constexpr static auto Name() noexcept { return "Image Viewer"; }
191 constexpr static auto Category() noexcept { return "Image Capturing"; }
192
194 virtual ~ImageViewer();
195
196 virtual std::string GetName() const override { return Name(); }
197 virtual std::string GetCategory() const override { return Category(); }
198
199 // approx. 63 fps
200 std::chrono::milliseconds GetMainLoopDelay() const override final { return std::chrono::milliseconds(16); }
201
202 // Events which the UI thread might enqueue.
203 void OnSaveImage(DynExp::ModuleInstance* Instance, QString Filename) const;
204
205 private:
207
208 void ResetImpl(dispatch_tag<QModuleBase>) override final;
209
210 std::unique_ptr<DynExp::QModuleWidget> MakeUIWidget() override final;
211 void UpdateUIChild(const ModuleBase::ModuleDataGetterType& ModuleDataGetter) override final;
212
213 // Helper functions
214 bool IsReadyState() const;
215 bool IsAutofocusingState() const;
216 void FinishAutofocus(Util::SynchronizedPointer<ModuleDataType>& ModuleData, const FinishedAutofocusEvent& Event) const;
217
218 // Function and types for autofocusing
219 // ->
221 {
225 std::chrono::milliseconds WaitTimeBeforeCapture{};
226
227 constexpr auto GetVoltageDiff() const noexcept { return MaxVoltage - MinVoltage; }
228 constexpr auto GetVoltageIncrement(bool Fine = false) const noexcept { return GetVoltageDiff() / NumSteps / (Fine ? NumSteps : 1); }
229 };
230
232 {
233 bool Success = false;
234 double FocusVoltage = std::numeric_limits<double>::quiet_NaN();
235 };
236
238 {
239 double Voltage{};
240 double Result{};
241 };
242 // <-
243
244 // Events, run in module thread
245 void OnInit(DynExp::ModuleInstance* Instance) const override final;
246 void OnExit(DynExp::ModuleInstance* Instance) const override final;
247 void OnCameraModeChanged(DynExp::ModuleInstance* Instance, int Index) const;
248 void OnExposureTimeChanged(DynExp::ModuleInstance* Instance, int Value) const;
249 void OnCaptureSingle(DynExp::ModuleInstance* Instance, bool) const;
250 void OnCaptureContinuously(DynExp::ModuleInstance* Instance, bool Checked) const;
251 void OnSetFilename(DynExp::ModuleInstance* Instance, const std::string& SaveFilename) const;
252 void OnTrigger(DynExp::ModuleInstance* Instance) const;
253 void OnStop(DynExp::ModuleInstance* Instance) const;
254 void OnPauseImageCapturing(DynExp::ModuleInstance* Instance, bool ResetImageTransformation = false) const;
256 void OnAutofocusClicked(DynExp::ModuleInstance* Instance, bool Checked) const;
257 void OnAutofocus(DynExp::ModuleInstance* Instance, bool ResetImageTransformation = false) const;
258
259 // State functions for state machine
267
268 // States for state machine
272 &ImageViewer::AutofocusInitStateFunc, "Autofocusing...");
280 &ImageViewer::AutofocusStepStateFunc, "Autofocusing...");
282 &ImageViewer::AutofocusFinishedStateFunc, "Autofocusing...");
283
284 // Logical const-ness: allow events to set the state machine's current state.
286
287 // Variables for autofocusing
291 std::vector<AutofocusSampleType> AutofocusSamples;
292 std::vector<AutofocusSampleType>::iterator AutofocusCurrentSample;
293 std::chrono::system_clock::time_point AutofocusWaitingEndTimePoint;
294
295 const std::shared_ptr<std::atomic<bool>> PauseUpdatingUI;
297 };
298}
Provides common events for inter-module communication. Also refer to DynExp::InterModuleEventBase.
Defines DynExp's core module as an interface between the UI and DynExp objects.
Events for inter-module communication handled by the DynExpModule::ImageViewer::ImageViewer module.
CapturingStateType
Type indicating whether the camera is currently capturing images.
Definition Camera.h:106
@ Stopped
The camera is not capturing.
std::chrono::milliseconds TimeType
Time type describing the camera's times like its exposure time.
Definition Camera.h:101
std::vector< std::string > CameraModesType
List type containing strings of modes the camera can operate in.
Definition Camera.h:100
ComputeHistogramType
Type indicating whether histograms should be computed for newly captured images.
Definition Camera.h:115
@ NoHistogram
Histogram computation is disabled.
SampleT SampleType
Alias for SampleT.
virtual DynExp::ParamsBasePtrType MakeParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core) const override final
Override to make derived classes call DynExp::MakeParams with the correct configurator type derived f...
Util::ImageRGBHistogramType RGBHistogram
void ResetImpl(dispatch_tag< QModuleDataBase >) override final
DynExp::LinkedObjectWrapperContainer< DynExpInstr::InterModuleCommunicator > Communicator
double CalcBrennerGradientFromImage() const
Calculates the Brenner gradient of CurrentImage. Refer to J. F. Brenner et al. J. Histochem....
DynExpInstr::CameraData::TimeType TimeType
DynExpInstr::CameraData::CameraModesType CameraModes
DynExpInstr::CameraData::ComputeHistogramType ComputeHistogram
DynExpInstr::CameraData::CapturingStateType CapturingState
DynExp::LinkedObjectWrapperContainer< DynExpInstr::Camera > Camera
Util::ImageHistogramType IntensityHistogram
DynExp::LinkedObjectWrapperContainer< DynExpInstr::AnalogOut > Focus
virtual void ResetImpl(dispatch_tag< ImageViewerData >)
Param< ParamsConfigDialog::NumberType > AutofocusNumSteps
Param< DynExp::ObjectLink< DynExpInstr::Camera > > Camera
Param< DynExp::ObjectLink< DynExpInstr::InterModuleCommunicator > > Communicator
virtual const char * GetParamClassTag() const noexcept override
This function is intended to be overridden once in each derived class returning the name of the respe...
Param< DynExp::ObjectLink< DynExpInstr::AnalogOut > > Focus
void ConfigureParamsImpl(dispatch_tag< QModuleParamsBase >) override final
Param< ParamsConfigDialog::NumberType > AutofocusFocusChangeTime
ImageViewerParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core)
Util::ImageRGBHistogramType RGBHistogram
Definition ImageViewer.h:91
void OnHistogramContextMenuRequested(const QPoint &Position)
void SetImage(const QImage &NewImage) noexcept
virtual void resizeEvent(QResizeEvent *event) override
Util::MarkerGraphicsView * GraphicsView
Definition ImageViewer.h:85
bool eventFilter(QObject *obj, QEvent *event) override
bool AllowResize() const noexcept override final
Indicates the resizing behavior of the user interface window. Override to adjust.
Definition ImageViewer.h:50
void SetIntensityHistogram(Util::ImageHistogramType &&NewIntensityHistogram) noexcept
std::unique_ptr< Ui::ImageViewer > ui
Definition ImageViewer.h:61
void SetRGBHistogram(Util::ImageRGBHistogramType &&NewRGBHistogram) noexcept
Util::ImageHistogramType IntensityHistogram
Definition ImageViewer.h:90
static constexpr auto AutofocusGotoSampleState
StateType AutofocusStepStateFunc(DynExp::ModuleInstance &Instance)
StateType AutofocusInitStateFunc(DynExp::ModuleInstance &Instance)
void FinishAutofocus(Util::SynchronizedPointer< ModuleDataType > &ModuleData, const FinishedAutofocusEvent &Event) const
static constexpr auto AutofocusFinishedState
void OnResumeImageCapturing(DynExp::ModuleInstance *Instance) const
void OnAutofocus(DynExp::ModuleInstance *Instance, bool ResetImageTransformation=false) const
StateType AutofocusFinishedStateFunc(DynExp::ModuleInstance &Instance)
void OnCaptureSingle(DynExp::ModuleInstance *Instance, bool) const
std::unique_ptr< DynExp::QModuleWidget > MakeUIWidget() override final
Used by InitUI() as a factory function for the module's user interface widget. Create the widget here...
void OnInit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread starts. Override it to lock instruments this m...
void OnSetFilename(DynExp::ModuleInstance *Instance, const std::string &SaveFilename) const
void OnStop(DynExp::ModuleInstance *Instance) const
Util::DynExpErrorCodes::DynExpErrorCodes ModuleMainLoop(DynExp::ModuleInstance &Instance) override final
Module main loop. The function is executed periodically by the module thread. Also refer to GetMainLo...
void OnCaptureContinuously(DynExp::ModuleInstance *Instance, bool Checked) const
virtual std::string GetCategory() const override
Returns the category of this Object type.
static constexpr auto AutofocusInitState
void OnTrigger(DynExp::ModuleInstance *Instance) const
virtual std::string GetName() const override
Returns the name of this Object type.
StateType ReadyStateFunc(DynExp::ModuleInstance &Instance)
StateType AutofocusGotoSampleStateFunc(DynExp::ModuleInstance &Instance)
void OnExposureTimeChanged(DynExp::ModuleInstance *Instance, int Value) const
static constexpr auto AutofocusStepState
void OnExit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread terminates (not due to an exception,...
StateType AutofocusWaitForImageStateFunc(DynExp::ModuleInstance &Instance)
std::vector< AutofocusSampleType >::iterator AutofocusCurrentSample
static constexpr auto AutofocusWaitForImageState
std::vector< AutofocusSampleType > AutofocusSamples
StateType AutofocusWaitBeforeCaptureStateFunc(DynExp::ModuleInstance &Instance)
std::chrono::milliseconds GetMainLoopDelay() const override final
Specifies in which time intervals the module's event queue runs to handle pending events.
void OnAutofocusClicked(DynExp::ModuleInstance *Instance, bool Checked) const
void UpdateUIChild(const ModuleBase::ModuleDataGetterType &ModuleDataGetter) override final
const std::shared_ptr< std::atomic< bool > > PauseUpdatingUI
Util::StateMachine< StateMachineStateType > StateMachine
static constexpr auto AutofocusWaitBeforeCaptureState
void ResetImpl(dispatch_tag< QModuleBase >) override final
void OnCameraModeChanged(DynExp::ModuleInstance *Instance, int Index) const
void OnSaveImage(DynExp::ModuleInstance *Instance, QString Filename) const
std::chrono::system_clock::time_point AutofocusWaitingEndTimePoint
static constexpr auto Name() noexcept
static constexpr auto Category() noexcept
void OnPauseImageCapturing(DynExp::ModuleInstance *Instance, bool ResetImageTransformation=false) const
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition DynExpCore.h:127
This class holds a pointer (LinkedObjectWrapperPointer) to a LinkedObjectWrapper. Intances of this cl...
Definition Object.h:3168
Util::CallableMemberWrapper< ModuleBase, ModuleDataTypeSyncPtrType(ModuleBase::*)(const std::chrono::milliseconds)> ModuleDataGetterType
Invoking an instance of this alias is supposed to call ModuleBase::GetModuleData() of the instance th...
Definition Module.h:624
const std::unique_ptr< ModuleDataType > ModuleData
Module data belonging to this ModuleBase instance.
Definition Module.h:788
ModuleBase(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Constructs a ModuleBase instance.
Definition Module.cpp:206
Refer to ParamsBase::dispatch_tag.
Definition Module.h:191
Defines data for a thread belonging to a ModuleBase instance. Refer to RunnableInstance.
Definition Module.h:840
const std::thread::id OwnerThreadID
Thread id of the thread which has constructed (and owns) this Object instance.
Definition Object.h:2302
const ParamsBasePtrType Params
Pointer to the parameter class instance belonging to this Object instance.
Definition Object.h:2303
const auto & GetCore() const noexcept
Returns a reference to DynExp's core.
Definition Object.h:1677
const ItemIDType ID
ID of the Object this parameter class instance belongs to.
Definition Object.h:1779
const DynExpCore & Core
Reference to DynExp's core.
Definition Object.h:1780
Tag for function dispatching mechanism within this class used when derived classes are not intended t...
Definition Object.h:349
Base class for modules with a Qt-based user interface. Derive from this class to implement modules wi...
Definition Module.h:1660
Configurator class for QModuleBase.
Definition Module.h:1646
Data class for QModuleBase.
Definition Module.h:1536
Parameter class for QModuleBase.
Definition Module.h:1622
QModuleParamsBase(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a QModuleBase instance.
Definition Module.h:1628
Window class for Qt-based user interfaces belonging to DynExp modules. User interface Qt window class...
Definition Module.h:1395
QModuleBase & Owner
Module owning this user interface window (reference, because it should never change nor be nullptr).
Definition Module.h:1478
QModuleWidget(QModuleBase &Owner, QWidget *Parent=nullptr)
Constructs a QModuleWidget instance.
Definition Module.cpp:451
double NumberType
Number type used for numeric parameters (DynExp::ParamsBase::Param)
Implements a QGraphicsView the user can interact with to insert graphical markers....
Definition QtUtil.h:379
State machine state as used by class StateMachine. A state mainly wraps a state function of the membe...
Definition Util.h:1367
This class models a state machine. It keeps track of the current state and allows to invoke its assoc...
Definition Util.h:1497
constexpr auto Instrument
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
Definition Object.h:1807
size_t ItemIDType
ID type of objects/items managed by DynExp.
DynExpErrorCodes
DynExp's error codes
Definition Exception.h:22
DynExp's Util namespace contains commonly used functions and templates as well as extensions to Qt an...
std::array< unsigned long long, 256 > ImageHistogramType
Alias which represents a histogram as a std::array with 256 numeric bins. The lowest (highest) index ...
Definition QtUtil.h:244
std::tuple< ImageHistogramType, ImageHistogramType, ImageHistogramType > ImageRGBHistogramType
Alias which represents a RGB histogram as a std::tuple of three ImageHistogramType elements....
Definition QtUtil.h:250
Accumulates include statements to provide a precompiled header.
constexpr auto GetVoltageIncrement(bool Fine=false) const noexcept