Engauge Digitizer  2
BackgroundStateContext.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "BackgroundStateContext.h"
8 #include "BackgroundStateCurve.h"
9 #include "BackgroundStateNone.h"
10 #include "BackgroundStateOriginal.h"
11 #include "BackgroundStateUnloaded.h"
12 #include "DocumentModelColorFilter.h"
13 #include "DocumentModelGridRemoval.h"
14 #include "EngaugeAssert.h"
15 #include "GraphicsView.h"
16 #include "Logger.h"
17 #include "MainWindow.h"
18 #include <QGraphicsPixmapItem>
19 #include "Transformation.h"
20 
22  m_mainWindow (mainWindow)
23 {
24  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::BackgroundStateContext";
25 
26  // These states follow the same order as the BackgroundState enumeration
27  m_states.insert (BACKGROUND_STATE_CURVE , new BackgroundStateCurve (*this, mainWindow.scene()));
28  m_states.insert (BACKGROUND_STATE_NONE , new BackgroundStateNone (*this, mainWindow.scene()));
29  m_states.insert (BACKGROUND_STATE_ORIGINAL, new BackgroundStateOriginal (*this, mainWindow.scene()));
30  m_states.insert (BACKGROUND_STATE_UNLOADED, new BackgroundStateUnloaded (*this, mainWindow.scene()));
31  ENGAUGE_ASSERT (m_states.size () == NUM_BACKGROUND_STATES);
32 
33  m_currentState = NUM_BACKGROUND_STATES; // Value that forces a transition right away
34  requestStateTransition (BACKGROUND_STATE_UNLOADED);
35  completeRequestedStateTransitionIfExists();
36 }
37 
39 {
40  qDeleteAll (m_states);
41 }
42 
44 {
45  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::close";
46 
47  // It is safe to transition to the new state immediately since no BackgroundState classes are on the stack
48  requestStateTransition (BACKGROUND_STATE_UNLOADED);
49  completeRequestedStateTransitionIfExists ();
50 }
51 
52 void BackgroundStateContext::completeRequestedStateTransitionIfExists()
53 {
54  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::completeRequestedStateTransitionIfExists";
55 
56  if (m_currentState != m_requestedState) {
57 
58  // A transition is waiting so perform it
59 
60  if (m_currentState != NUM_BACKGROUND_STATES) {
61 
62  // This is not the first state so close the previous state
63  m_states [m_currentState]->end ();
64  }
65 
66  // Start the new state
67  m_currentState = m_requestedState;
68  m_states [m_requestedState]->begin ();
69  }
70 }
71 
73 {
74  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::fitInView";
75 
76  // After initialization, we should be in unloaded state or some other equally valid state
77  ENGAUGE_ASSERT (m_currentState != NUM_BACKGROUND_STATES);
78 
79  const QGraphicsPixmapItem *imageItem = &m_states [BACKGROUND_STATE_CURVE]->imageItem ();
80 
81  double width = imageItem->boundingRect().width();
82  double height = imageItem->boundingRect().height();
83 
84  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::fitInView"
85  << " state=" << m_states [m_currentState]->state ().toLatin1().data()
86  << " boundingRect=(" << width << "x" << height << ")";
87 
88  // Get the image from a state that is guaranteed to have an image
89  view.fitInView (imageItem);
90 
91 }
92 
94 {
95  return m_states [BACKGROUND_STATE_CURVE]->image();
96 }
97 
98 void BackgroundStateContext::requestStateTransition (BackgroundState backgroundState)
99 {
100  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::requestStateTransition";
101 
102  m_requestedState = backgroundState;
103 }
104 
105 void BackgroundStateContext::setBackgroundImage (BackgroundImage backgroundImage)
106 {
107  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setBackgroundImage"
108  << " background=" << backgroundImageToString (backgroundImage).toLatin1().data();
109 
110  BackgroundState backgroundState= BACKGROUND_STATE_NONE;
111 
112  switch (backgroundImage) {
113  case BACKGROUND_IMAGE_FILTERED:
114  backgroundState = BACKGROUND_STATE_CURVE;
115  break;
116 
117  case BACKGROUND_IMAGE_NONE:
118  backgroundState = BACKGROUND_STATE_NONE;
119  break;
120 
121  case BACKGROUND_IMAGE_ORIGINAL:
122  backgroundState = BACKGROUND_STATE_ORIGINAL;
123  break;
124  }
125 
126  // It is safe to transition to the new state immediately since no BackgroundState classes are on the stack
127  requestStateTransition (backgroundState);
128  completeRequestedStateTransitionIfExists ();
129 }
130 
132  const Transformation &transformation,
133  const DocumentModelGridRemoval &modelGridRemoval,
134  const DocumentModelColorFilter &modelColorFilter,
135  const QString &curveSelected)
136 {
137  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setCurveSelected"
138  << " curve=" << curveSelected.toLatin1().data();
139 
140  for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
141 
142  m_states [backgroundState]->setCurveSelected (isGnuplot,
143  transformation,
144  modelGridRemoval,
145  modelColorFilter,
146  curveSelected);
147  }
148 }
149 
151  const Transformation &transformation,
152  const DocumentModelGridRemoval &modelGridRemoval,
153  const DocumentModelColorFilter &modelColorFilter,
154  const QPixmap &pixmapOriginal,
155  const QString &curveSelected)
156 {
157  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setPixmap"
158  << " image=" << pixmapOriginal.width() << "x" << pixmapOriginal.height()
159  << " currentState=" << m_states [m_currentState]->state().toLatin1().data();
160 
161  for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
162 
163  m_states [backgroundState]->setPixmap (isGnuplot,
164  transformation,
165  modelGridRemoval,
166  modelColorFilter,
167  pixmapOriginal,
168  curveSelected);
169  }
170 }
171 
173  const Transformation &transformation,
174  const DocumentModelGridRemoval &modelGridRemoval,
175  const DocumentModelColorFilter &modelColorFilter,
176  const QString &curveSelected)
177 {
178  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::updateColorFilter";
179 
180  for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
181 
182  m_states [backgroundState]->updateColorFilter (isGnuplot,
183  transformation,
184  modelGridRemoval,
185  modelColorFilter,
186  curveSelected);
187  }
188 }
void fitInView(GraphicsView &view)
Zoom so background fills the window.
void updateColorFilter(bool isGnuplot, const Transformation &transformation, const DocumentModelGridRemoval &modelGridRemoval, const DocumentModelColorFilter &colorFilter, const QString &curveSelected)
Apply color filter settings.
Background image state for interval between startup and loading of the image.
void setPixmap(bool isGnuplot, const Transformation &transformation, const DocumentModelGridRemoval &modelGridRemoval, const DocumentModelColorFilter &modelColorFilter, const QPixmap &pixmapOriginal, const QString &curveSelected)
Update the images of all states, rather than just the current state.
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 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:20
void setCurveSelected(bool isGnuplot, const Transformation &transformation, const DocumentModelGridRemoval &modelGridRemoval, const DocumentModelColorFilter &modelColorFilter, const QString &curveSelected)
Update the selected curve.
void close()
Open Document is being closed so remove the background.
Background image state for showing original (=unfiltered) image.
BackgroundStateContext(MainWindow &mainWindow)
Single constructor.
QImage imageForCurveState() const
Image for the Curve state, even if the current state is different.
~BackgroundStateContext()
Destructor deallocates memory.
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:91