Engauge Digitizer  2
BackgroundStateContext.cpp
1 #include "BackgroundStateContext.h"
2 #include "BackgroundStateCurve.h"
3 #include "BackgroundStateNone.h"
4 #include "BackgroundStateOriginal.h"
5 #include "BackgroundStateUnloaded.h"
6 #include "DocumentModelColorFilter.h"
7 #include "DocumentModelGridRemoval.h"
8 #include "EngaugeAssert.h"
9 #include "GraphicsView.h"
10 #include "Logger.h"
11 #include "MainWindow.h"
12 #include <QGraphicsPixmapItem>
13 #include "Transformation.h"
14 
16  m_mainWindow (mainWindow)
17 {
18  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::BackgroundStateContext";
19 
20  // These states follow the same order as the BackgroundState enumeration
21  m_states.insert (BACKGROUND_STATE_CURVE , new BackgroundStateCurve (*this, mainWindow.scene()));
22  m_states.insert (BACKGROUND_STATE_NONE , new BackgroundStateNone (*this, mainWindow.scene()));
23  m_states.insert (BACKGROUND_STATE_ORIGINAL, new BackgroundStateOriginal (*this, mainWindow.scene()));
24  m_states.insert (BACKGROUND_STATE_UNLOADED, new BackgroundStateUnloaded (*this, mainWindow.scene()));
25  ENGAUGE_ASSERT (m_states.size () == NUM_BACKGROUND_STATES);
26 
27  m_currentState = NUM_BACKGROUND_STATES; // Value that forces a transition right away
28  requestStateTransition (BACKGROUND_STATE_UNLOADED);
29  completeRequestedStateTransitionIfExists();
30 }
31 
32 void BackgroundStateContext::completeRequestedStateTransitionIfExists()
33 {
34  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::completeRequestedStateTransitionIfExists";
35 
36  if (m_currentState != m_requestedState) {
37 
38  // A transition is waiting so perform it
39 
40  if (m_currentState != NUM_BACKGROUND_STATES) {
41 
42  // This is not the first state so close the previous state
43  m_states [m_currentState]->end ();
44  }
45 
46  // Start the new state
47  m_currentState = m_requestedState;
48  m_states [m_requestedState]->begin ();
49  }
50 }
51 
53 {
54  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::fitInView";
55 
56  // After initialization, we should be in unloaded state or some other equally valid state
57  ENGAUGE_ASSERT (m_currentState != NUM_BACKGROUND_STATES);
58 
59  const QGraphicsPixmapItem *imageItem = &m_states [BACKGROUND_STATE_CURVE]->imageItem ();
60 
61  double width = imageItem->boundingRect().width();
62  double height = imageItem->boundingRect().height();
63 
64  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::fitInView"
65  << " state=" << m_states [m_currentState]->state ().toLatin1().data()
66  << " boundingRect=(" << width << "x" << height << ")";
67 
68  // Get the image from a state that is guaranteed to have an image
69  view.fitInView (imageItem);
70 
71 }
72 
74 {
75  return m_states [BACKGROUND_STATE_CURVE]->image();
76 }
77 
78 void BackgroundStateContext::requestStateTransition (BackgroundState backgroundState)
79 {
80  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::requestStateTransition";
81 
82  m_requestedState = backgroundState;
83 }
84 
85 void BackgroundStateContext::setBackgroundImage (BackgroundImage backgroundImage)
86 {
87  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setBackgroundImage"
88  << " background=" << backgroundImageToString (backgroundImage).toLatin1().data();
89 
90  BackgroundState backgroundState;
91  switch (backgroundImage) {
92  case BACKGROUND_IMAGE_FILTERED:
93  backgroundState = BACKGROUND_STATE_CURVE;
94  break;
95 
96  case BACKGROUND_IMAGE_NONE:
97  backgroundState = BACKGROUND_STATE_NONE;
98  break;
99 
100  case BACKGROUND_IMAGE_ORIGINAL:
101  backgroundState = BACKGROUND_STATE_ORIGINAL;
102  break;
103 
104  default:
105  LOG4CPP_ERROR_S ((*mainCat)) << "BackgroundStateContext::selectBackgroundImage";
106  exit (-1);
107  }
108 
109  // It is safe to transition to the new state immediately since no BackgroundState classes are on the stack
110  requestStateTransition (backgroundState);
111  completeRequestedStateTransitionIfExists ();
112 }
113 
115  const DocumentModelGridRemoval &modelGridRemoval,
116  const DocumentModelColorFilter &modelColorFilter,
117  const QString &curveSelected)
118 {
119  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setCurveSelected"
120  << " curve=" << curveSelected.toLatin1().data();
121 
122  ENGAUGE_ASSERT (!curveSelected.isEmpty ());
123 
124  for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
125 
126  m_states [backgroundState]->setCurveSelected (transformation,
127  modelGridRemoval,
128  modelColorFilter,
129  curveSelected);
130  }
131 }
132 
134  const DocumentModelGridRemoval &modelGridRemoval,
135  const DocumentModelColorFilter &modelColorFilter,
136  const QPixmap &pixmapOriginal)
137 {
138  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setPixmap";
139 
140  for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
141 
142  m_states [backgroundState]->setPixmap (transformation,
143  modelGridRemoval,
144  modelColorFilter,
145  pixmapOriginal);
146  }
147 }
148 
150  const DocumentModelGridRemoval &modelGridRemoval,
151  const DocumentModelColorFilter &modelColorFilter)
152 {
153  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::updateColorFilter";
154 
155  for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
156 
157  m_states [backgroundState]->updateColorFilter (transformation,
158  modelGridRemoval,
159  modelColorFilter);
160  }
161 }
void fitInView(GraphicsView &view)
Zoom so background fills the window.
Background image state for interval between startup and loading of the image.
Background image state for showing filter image from current curve.
void requestStateTransition(BackgroundState backgroundState)
Initiate state transition to be performed later, when BackgroundState is off the stack.
Background image state for showing no image.
Affine transformation between screen and graph coordinates, based on digitized axis points...
void setPixmap(const Transformation &transformation, const DocumentModelGridRemoval &modelGridRemoval, const DocumentModelColorFilter &modelColorFilter, const QPixmap &pixmapOriginal)
Update the images of all states, rather than just the current state.
void setBackgroundImage(BackgroundImage backgroundImage)
Transition to the specified state. This method is used by classes outside of the state machine to tri...
Model for DlgSettingsColorFilter and CmdSettingsColorFilter.
GraphicsScene & scene()
Scene container for the QImage and QGraphicsItems.
QGraphicsView class with event handling added. Typically the events are sent to the active digitizing...
Definition: GraphicsView.h:13
QImage imageForCurveState() const
Image for the Curve state, even if the current state is different.
void setCurveSelected(const Transformation &transformation, const DocumentModelGridRemoval &modelGridRemoval, const DocumentModelColorFilter &modelColorFilter, const QString &curveSelected)
Update the selected curve.
Background image state for showing original (=unfiltered) image.
BackgroundStateContext(MainWindow &mainWindow)
Single constructor.
void updateColorFilter(const Transformation &transformation, const DocumentModelGridRemoval &modelGridRemoval, const DocumentModelColorFilter &colorFilter)
Apply color filter settings.
Model for DlgSettingsGridRemoval and CmdSettingsGridRemoval. The settings are unstable until the user...
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:60