Scope
HistogramView.cpp
1 #include "StdAfx.h"
2 #include "HistogramView.h"
3 #include "helpers/ScopeMultiImage.h"
4 #include "helpers/ScopeMultiImageResonanceSW.h"
5 #include "helpers/ScopeMultiHistogram.h"
6 #include "controllers/ScopeLogger.h"
7 
8 namespace scope {
9  namespace gui {
10 
11 CHistogramView::CHistogramView(const uint32_t& _area, const uint32_t& _channels, uint16_t _range)
12  : area(_area)
13  , channels(_channels)
14  , range(_range)
15  , current_frame(std::make_shared<scope::SCOPE_MULTIIMAGE_T>())
16  , current_histogram(std::make_shared<scope::ScopeMultiHistogram>(area, channels, 512, range))
17  , lower_limits(channels, 0)
18  , upper_limits(channels, range)
19  , renderer(width, channels, current_histogram)
20  , dragginglower(channels, false)
21  , draggingupper(channels, false)
22  , llpos(channels, 0.0)
23  , ulpos(channels, 511.0) {
24 }
25 
26 void CHistogramView::OnFinalMessage(HWND /*hWnd*/) {
27 }
28 
29 void CHistogramView::OnLButtonDown(UINT nFlags, CPoint point) {
30  uint32_t ch = 0;
31  bool uplo = false;
32  if ( renderer.ClickedAt(point, ch, uplo) ) {
33  if ( uplo ) {
34  dragginglower.assign(channels, false); // to be safe
35  draggingupper[ch] = true;
36  }
37  else {
38  draggingupper.assign(channels, false);
39  dragginglower[ch] = true;
40  }
41  }
42 }
43 
44 void CHistogramView::OnLButtonUp(UINT nFlags, CPoint point) {
45  DBOUT(L"OnLButtonUp");
46  draggingupper.assign(channels, false);
47  dragginglower.assign(channels, false);
48 }
49 
50 void CHistogramView::OnMouseMove(UINT nFlags, CPoint point) {
51  if ( nFlags == MK_LBUTTON ) {
52  for ( uint32_t c = 0 ; c < channels ; c++ ) {
53  if ( dragginglower[c] ) {
54  // make sure ul and ll do not swap
55  llpos[c] = std::min(ulpos[c], static_cast<FLOAT>(point.x));
56  // calculate limit value from window coordinate
57  lower_limits[c] = round2ui16(llpos[c] / 512 * (range-0));
59  }
60  if ( draggingupper[c] ) {
61  ulpos[c] = std::max(llpos[c], static_cast<FLOAT>(point.x));
62  upper_limits[c] = round2ui16(ulpos[c] / 512 * (range-0));
64  }
65  // Notify ScopeController of new limits, to adjust display in CChannelFrame etc.
67  }
68  Invalidate();
69  ::SendMessage(GetParent().m_hWnd, WM_UPDATEHISTOLIMITS, NULL, NULL);
70  }
71 }
72 
73 void CHistogramView::OnShowWindow(BOOL bShow, UINT nStatus) {
75  renderer.Create(m_hWnd);
76 }
77 
78 BOOL CHistogramView::PreTranslateMessage(MSG* pMsg) {
79  pMsg;
80  return false;
81 }
82 
83 void CHistogramView::OnPaint(CDCHandle /*dc*/) {
84  PAINTSTRUCT paint;
85  ATLASSERT(BeginPaint(&paint));
86  renderer.Render();
87  EndPaint(&paint);
88  ValidateRect(NULL);
89 }
90 
91 void CHistogramView::OnSize(UINT /*type*/, CSize _size) {
92  assert(_size.cx == 512); // Histogram expects this!
93  // maximum position to draw a line is size-1
94  renderer.Size(_size);
95 }
96 
97 void CHistogramView::OnDisplayChange(UINT /*bpp*/, CSize /*resolution*/) {
98  Invalidate(false); // Sends WM_PAINT
99 }
100 
102  auto firsts = current_histogram->FirstCountPositions();
103  auto lasts = current_histogram->LastCountPositions();
104  for ( uint32_t c = 0 ; c < channels ; c++ ) {
105  lower_limits[c] = firsts[c];
106  upper_limits[c] = lasts[c];
107  DBOUT(L"CHistogramView::Optimize Ch" << c << L" lower limit " << lower_limits[c] << L" upper limit " << upper_limits[c]);
108  }
109  SetLimits();
110  Invalidate(false); // Sends WM_PAINT
111 }
112 
114  range = static_cast<uint16_t>(scope_controller.GuiParameters.areas[area]->histrange);
115  for ( uint32_t c = 0 ; c < channels ; c++ ) {
116  lower_limits[c] = 0;
117  upper_limits[c] = range;
118  }
119  SetLimits();
120  Invalidate(false); // Sends WM_PAINT
121 }
122 
124  for ( uint32_t c = 0 ; c < channels ; c++ ) {
125  // Calculate window coordinates of limit bars positions (be careful with data types here... do not use uint16_t for intermediate results!)
126  llpos[c] = static_cast<float>(lower_limits[c]) / static_cast<FLOAT>((range-0)) * 512.0f;
127  ulpos[c] = static_cast<float>(upper_limits[c]) / static_cast<FLOAT>((range-0)) * 512.0f;
129 
130  // Hand these positions to the renderer
133  // Signals new limits to the ScopeController, to adjust display in CChannelFrame etc.
135  }
136 }
137 
139  // Update the Histogram with new range
140  ScopeMultiHistogramPtr updated_histogram(std::make_shared<scope::ScopeMultiHistogram>(area, channels, 512, range));
141  current_histogram = updated_histogram;
143 }
144 
145 void CHistogramView::SetCurrentFrame(scope::SCOPE_MULTIIMAGECPTR_T const _multi, const bool& _loghist) {
146  current_frame = _multi;
147  current_histogram->Calculate(current_frame, _loghist);
149 }
150 
151 }}
const uint32_t area
for which area
Definition: HistogramView.h:29
const uint32_t channels
how many channels
Definition: HistogramView.h:32
void OnLButtonDown(UINT nFlags, CPoint point)
Ask renderer for hit test and set click state.
std::vector< uint16_t > upper_limits
upper limit values of all channels/the value that corresponds to histogram array index (because of ra...
Definition: HistogramView.h:50
std::vector< bool > dragginglower
Keeps track of mouse dragging which lower limit.
Definition: HistogramView.h:56
uint16_t range
what uin16_t histogram range is used
Definition: HistogramView.h:35
std::array< std::unique_ptr< Area >, SCOPE_NAREAS > areas
holds AreaParameters for all areas.
Definition: Scope.h:231
void OnLButtonUp(UINT nFlags, CPoint point)
Releases click state.
void SetLimits()
Set limits in renderer and scope_controller.
void FullRange()
Set lower and upper limits to full range.
ScopeMultiHistogramPtr current_histogram
currently displayed histogram
Definition: HistogramView.h:44
STL namespace.
SCOPE_MULTIIMAGECPTR_T current_frame
image corresponding to the currently displayed histogram
Definition: HistogramView.h:41
void SetHistogramLimits(const uint32_t &_area, const uint32_t &_channel, const uint16_t &_lower, const uint16_t &_upper)
Sets display limits for a certain area and channel.
void SetHistogram(scope::ScopeMultiHistogramPtr _hist)
Sets the pointer to the current multi histogram.
void Render()
Draws the current multi histogram with the limit lines.
std::vector< bool > draggingupper
Keeps track of mouse dragging which upper limit.
Definition: HistogramView.h:59
void OnMouseMove(UINT nFlags, CPoint point)
If moving with left mouse button down, give position to Renderer and render limit bars...
bool ClickedAt(const CPoint _clickpoint, uint32_t &_channel, bool &_uplo)
Checks if click hit a limit (+-5 pixel) and if yes, on which channel and upper or lower limit line...
This is the include file for standard system include files, or project specific include files that ar...
void OnShowWindow(BOOL bShow, UINT nStatus)
Create the renderer only now, because now the window is shown.
void Create(const HWND &_hwnd)
Creates the render target for a hwnd, the brushes for painting, and sets the limit position to the wi...
std::vector< uint16_t > lower_limits
lower limit values of all channels/the value that corresponds to histogram array index (because of ra...
Definition: HistogramView.h:47
void SetLowerLimitPosition(const uint32_t &_channel, const FLOAT &_pos)
Set position of a lower limit line.
void Optimize()
Set lower limit to the first value with count >0 and the upper limit to the last value with count >0...
#define DBOUT(s)
A debug output to the debug console.
Definition: helpers.h:153
std::vector< FLOAT > ulpos
Keeps track of the upper limit positions in screen coordinates/histogram array index.
Definition: HistogramView.h:65
void OnSize(UINT, CSize _size)
Resize the Renderer accordingly, be careful not to resize to 0.
void UpdateHistogramFrame()
Update Histogram with current parameters.
A multi channel histogram.
ScopeController scope_controller
the ScopeController kept handy here
Definition: HistogramView.h:38
void Size(const CSize &size)
Resizes the renderer and the limit positions.
d2d::D2HistogramRender renderer
this one handles all of the Views rendering
Definition: HistogramView.h:53
CHistogramView(const uint32_t &_area, const uint32_t &_channels, uint16_t _range)
Initialize everything.
void OnPaint(CDCHandle)
Render the histogram and the limit bars (both via Renderer)
std::vector< FLOAT > llpos
Keeps track of the lower limit positions in screen coordinates/histogram array index.
Definition: HistogramView.h:62
virtual void OnFinalMessage(HWND)
Do not do 'delete this;' because view is a member of frame and gets destroyed when frame is destroyed...
static parameters::Scope GuiParameters
The complete pseudo-global parameter set of the microscope.
void SetUpperLimitPosition(const uint32_t &_channel, const FLOAT &_pos)
Set position of an upper limit line.
void SetCurrentFrame(SCOPE_MULTIIMAGECPTR_T const _multi, const bool &_loghisto)
Sets the current frame and calculates its histogram (either with regular count or with logarithmic co...