Scope
HistogramFrame.cpp
1 #include "StdAfx.h"
2 #include "HistogramFrame.h"
3 #include "helpers/ScopeMultiImage.h"
4 #include "helpers/ScopeMultiImageResonanceSW.h"
5 #include "controllers/ScopeController.h"
6 #include "resource.h"
7 #include "parameters\IO.h"
8 
9 namespace scope {
10  namespace gui {
11 
12 CHistogramFrame::CHistogramFrame(const uint32_t& _area, const uint32_t& _channels)
13  : area(_area)
14  , channels(_channels)
15  , attached(false)
16  , loghist(false)
17  , statusstr(L"")
18  , limitsstr(L"")
19  , view(area, channels, scope_controller.GuiParameters.areas[area]->histrange)
20  , framecount(0) {
21 }
22 
24  // If we land here through an exception, OnDestroy is eventually not called thus we need to detach here to avoid
25  // an invalid pointer in the DisplayController
26  if ( attached ) {
27  try {
28  scope_controller.DetachFrame(this);
29  } catch (...) { ScopeExceptionHandler(__FUNCTION__); }
30  attached = false;
31  }
32  // We have to stop the Active's worker thread before member destruction starts, since stuff executed in that thread could want to access CHistogramFrame member
33  // and these get destroyed before the Active and its thread gets destroyed!
34  Quit();
35 }
36 
37 void CHistogramFrame::OnFinalMessage(HWND /*hWnd*/) {
38  delete this;
39 }
40 
42  UISetText(0, statusstr.c_str());
43  std::wostringstream stream;
44  for ( uint32_t c = 0 ; c < channels ; c++ )
45  stream << L"(" << view.LowerLimit(c) << L", " << view.UpperLimit(c) << L") ";
46  limitsstr = stream.str();
47  UISetText(1, limitsstr.c_str());
48  UIUpdateStatusBar();
49  return true;
50 }
51 
52 int CHistogramFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {
53  // with this client width will be 512 pixel
54  RECT client_rect;
55  client_rect.top = 0;
56  client_rect.left = 0;
57  client_rect.bottom = 356;
58  client_rect.right = 516;
59  m_hWndClient = view.Create(m_hWnd, &client_rect, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE);
60 
61  SetMenu(HMENU(NULL));
62 
63  toolbar = CreateSimpleToolBarCtrl(m_hWnd, IDR_HISTOGRAMFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE_EX);
64 
65  AddToolbarButtonText(toolbar, IDC_OPTIMIZE, L"Optimize");
66  AddToolbarButtonText(toolbar, IDC_FULLRANGE, L"Full range");
67 
68  CreateSimpleReBar(ATL_SIMPLE_REBAR_STYLE);
69  AddSimpleReBarBand(toolbar);
70  UIAddToolBar(toolbar);
71 
72  // Standard style but without SBARS_SIZEGRIP (no resize gripper)
73  m_hWndStatusBar = m_wndStatusBar.Create(*this, ATL_IDS_IDLEMESSAGE, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
74  UIAddStatusBar (m_hWndStatusBar);
75  // Create the status bar panes.
76  int32_t anPanes[] = { IDPANE_STATUS , IDPANE_HISTOGRAMLIMITS };
77 
78  m_wndStatusBar.SetPanes ( anPanes, 2, false );
79  UISetText(0, L"Stopped");
80  UISetText(1, L"Limits ");
81  UIUpdateStatusBar();
82 
83  UIEnable(IDC_CH1BUTTON, (channels>0));
84  UIEnable(IDC_CH2BUTTON, (channels>1));
85  UIEnable(IDC_CH3BUTTON, (channels>2));
86  UIEnable(IDC_CH4BUTTON, (channels>3));
87 
88  UIUpdateToolBar();
89  UpdateLayout();
90 
91  std::wostringstream stream;
92  stream << L"Histogram (Area " << area+1 << L")";
93  SetWindowText(stream.str().c_str());
94 
95  scope_controller.AttachFrame(this);
96  attached = true;
97 
98  return 1;
99 }
100 
102  try {
103  scope_controller.DetachFrame(this);
104  } catch (...) { ScopeExceptionHandler(__FUNCTION__); }
105  attached = false;
106 }
107 
109  UIUpdateToolBar();
110  //UIUpdateStatusBar();
111  return FALSE;
112 }
113 
114 void CHistogramFrame::OnOptimize(UINT uNotifyCode, int nID, CWindow wndCtl) {
115  view.Optimize();
116  Send(std::bind(&CHistogramFrame::RunUpdateStatusbar, this, std::placeholders::_1));
117 }
118 
119 void CHistogramFrame::OnFullRange(UINT uNotifyCode, int nID, CWindow wndCtl) {
120  view.FullRange();
121  Send(std::bind(&CHistogramFrame::RunUpdateStatusbar, this, std::placeholders::_1));
122 }
123 
124 void CHistogramFrame::OnLogHistogram(UINT uNotifyCode, int nID, CWindow wndCtl) {
125  loghist = !loghist;
126  toolbar.CheckButton(IDC_LOGHISTOGRAM, loghist);
127 }
128 
129 LRESULT CHistogramFrame::OnUpdateHistogramLimits(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
130  Send(std::bind(&CHistogramFrame::RunUpdateStatusbar, this, std::placeholders::_1));
131  return 0;
132 }
133 
135  statusstr = _rs;
136  Send(std::bind(&CHistogramFrame::RunUpdateStatusbar, this, std::placeholders::_1));
137 }
138 
139 void CHistogramFrame::HistoGramAndRender(scope::SCOPE_MULTIIMAGECPTR_T const _multi) {
140  framecount = _multi->GetImageNumber();
141  // More than 6 elements in queue -> just throw away what is coming now...
142  if ( ptq.Size() < 7 ) {
143  Send([=](StopCondition* const sc) {
144  // This calls the actual histogram calculation
145  view.SetCurrentFrame(_multi, loghist);
146 
147  // This causes a WM_PAINT message, and CHistogramView's OnPaint calls the D2 renderer
148  view.Invalidate(false);
149 
150  return true;
151  });
152  Send(std::bind(&CHistogramFrame::RunUpdateStatusbar, this, std::placeholders::_1));
153  }
154  else {
155  DBOUT(L"CHistogramFrame::HistoGramAndRender dropping frames");
156  }
157 }
158 
159 }}
const uint32_t channels
number of channels for this area
Thread-safe lock-free bool to signal a requested stop to the worker function currently executed in th...
Definition: helpers.h:87
void OnFullRange(UINT uNotifyCode, int nID, CWindow wndCtl)
Calls CHistogramView::FullRange on view.
void Quit()
Send lambda with done=true to the worker, join the worker thread, and clear the packaged task queue...
Definition: Active.h:80
CHistogramFrame(const uint32_t &_area, const uint32_t &_channels)
Simple constructor.
const uint32_t area
which area is the histogram for
~CHistogramFrame()
Detaches if not yet happened and quits.
void FullRange()
Set lower and upper limits to full range.
size_t Size() const
Definition: SyncQueues.h:31
void UpdateStatus(const RunState &_rs)
Updates the statusstr and send RunUpdateStatusbar to the Active object.
void HistoGramAndRender(scope::SCOPE_MULTIIMAGECPTR_T const _multi)
Sends the worker function 'RunHistoGramAndRender' to the ActiveObject.
Base class for all Scope datatypes here, provides a uniform interface (and saves typing...).
std::wstring limitsstr
holds the current limits as string (we need a member since the lifetime of a local variable is too sh...
LRESULT OnUpdateHistogramLimits(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Limit bar movements are handled in the client window (CHistogramView) but we want also an update of C...
uint32_t framecount
number of the currently displayed histogram
This is the include file for standard system include files, or project specific include files that ar...
std::future< bool > Send(const Command &_cmd)
Sends a worker function/packaged task to the queue to be executed in the Active's thread...
Definition: Active.h:59
bool attached
are we attached to ScopeController?
void Optimize()
Set lower limit to the first value with count >0 and the upper limit to the last value with count >0...
bool RunUpdateStatusbar(StopCondition *const ac)
Worker function for all statusbar updates which will run in the Active's thread.
#define DBOUT(s)
A debug output to the debug console.
Definition: helpers.h:153
void ScopeExceptionHandler(const std::string &_origin, const bool &_log, const bool &_showmessagebox, const bool &_trace, const bool &_rethrow)
Handles all exceptions and does nice logging.
CHistogramView view
The view inside the client window.
bool loghist
do we want a logarithmic count histogram
CToolBarCtrl toolbar
the toolbar
SynchronizedQueue< const std::function< void(StopCondition *const sc)> > ptq
synchronized queue of worker functions/packaged tasks that are executed in the Active' thread (see la...
Definition: Active.h:26
void OnLogHistogram(UINT uNotifyCode, int nID, CWindow wndCtl)
Switches between normal and logarithmic histogram.
scope::ScopeController scope_controller
our ScopeController here
CMultiPaneStatusBarCtrl m_wndStatusBar
the statusbar (multipane)
void OnOptimize(UINT uNotifyCode, int nID, CWindow wndCtl)
Calls CHistogramView::Optimize on view.
std::wstring statusstr
holds the current run status as string (we need a member since the lifetime of a local variable is to...
void OnDestroy()
Detaches frame from ScopeController, because after OnDestroy the HWND is not valid anymore...
int OnCreate(LPCREATESTRUCT lpCreateStruct)
Creates view, toolbar, statusbar etc., resizes window correctly.
uint16_t LowerLimit(const uint32_t &_ch) const
Render histogram and limit bars.
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...