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 
33 {
34  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::close";
35 
36  // It is safe to transition to the new state immediately since no BackgroundState classes are on the stack
37  requestStateTransition (BACKGROUND_STATE_UNLOADED);
38  completeRequestedStateTransitionIfExists ();
39 }
40 
41 void BackgroundStateContext::completeRequestedStateTransitionIfExists()
42 {
43  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::completeRequestedStateTransitionIfExists";
44 
45  if (m_currentState != m_requestedState) {
46 
47  // A transition is waiting so perform it
48 
49  if (m_currentState != NUM_BACKGROUND_STATES) {
50 
51  // This is not the first state so close the previous state
52  m_states [m_currentState]->end ();
53  }
54 
55  // Start the new state
56  m_currentState = m_requestedState;
57  m_states [m_requestedState]->begin ();
58  }
59 }
60 
62 {
63  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::fitInView";
64 
65  // After initialization, we should be in unloaded state or some other equally valid state
66  ENGAUGE_ASSERT (m_currentState != NUM_BACKGROUND_STATES);
67 
68  const QGraphicsPixmapItem *imageItem = &m_states [BACKGROUND_STATE_CURVE]->imageItem ();
69 
70  double width = imageItem->boundingRect().width();
71  double height = imageItem->boundingRect().height();
72 
73  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::fitInView"
74  << " state=" << m_states [m_currentState]->state ().toLatin1().data()
75  << " boundingRect=(" << width << "x" << height << ")";
76 
77  // Get the image from a state that is guaranteed to have an image
78  view.fitInView (imageItem);
79 
80 }
81 
83 {
84  return m_states [BACKGROUND_STATE_CURVE]->image();
85 }
86 
87 void BackgroundStateContext::requestStateTransition (BackgroundState backgroundState)
88 {
89  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::requestStateTransition";
90 
91  m_requestedState = backgroundState;
92 }
93 
94 void BackgroundStateContext::setBackgroundImage (BackgroundImage backgroundImage)
95 {
96  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setBackgroundImage"
97  << " background=" << backgroundImageToString (backgroundImage).toLatin1().data();
98 
99  BackgroundState backgroundState;
100  switch (backgroundImage) {
101  case BACKGROUND_IMAGE_FILTERED:
102  backgroundState = BACKGROUND_STATE_CURVE;
103  break;
104 
105  case BACKGROUND_IMAGE_NONE:
106  backgroundState = BACKGROUND_STATE_NONE;
107  break;
108 
109  case BACKGROUND_IMAGE_ORIGINAL:
110  backgroundState = BACKGROUND_STATE_ORIGINAL;
111  break;
112 
113  default:
114  LOG4CPP_ERROR_S ((*mainCat)) << "BackgroundStateContext::selectBackgroundImage";
115  exit (-1);
116  }
117 
118  // It is safe to transition to the new state immediately since no BackgroundState classes are on the stack
119  requestStateTransition (backgroundState);
120  completeRequestedStateTransitionIfExists ();
121 }
122 
124  const DocumentModelGridRemoval &modelGridRemoval,
125  const DocumentModelColorFilter &modelColorFilter,
126  const QString &curveSelected)
127 {
128  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setCurveSelected"
129  << " curve=" << curveSelected.toLatin1().data();
130 
131  ENGAUGE_ASSERT (!curveSelected.isEmpty ());
132 
133  for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
134 
135  m_states [backgroundState]->setCurveSelected (transformation,
136  modelGridRemoval,
137  modelColorFilter,
138  curveSelected);
139  }
140 }
141 
143  const DocumentModelGridRemoval &modelGridRemoval,
144  const DocumentModelColorFilter &modelColorFilter,
145  const QPixmap &pixmapOriginal)
146 {
147  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setPixmap";
148 
149  for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
150 
151  m_states [backgroundState]->setPixmap (transformation,
152  modelGridRemoval,
153  modelColorFilter,
154  pixmapOriginal);
155  }
156 }
157 
159  const DocumentModelGridRemoval &modelGridRemoval,
160  const DocumentModelColorFilter &modelColorFilter)
161 {
162  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::updateColorFilter";
163 
164  for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
165 
166  m_states [backgroundState]->updateColorFilter (transformation,
167  modelGridRemoval,
168  modelColorFilter);
169  }
170 }
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:14
void close()
Open Document is being closed so remove the background.
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:66