Engauge Digitizer  2
 All Classes Functions Variables Typedefs Enumerations Friends Pages
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  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::close";
41 
42  // It is safe to transition to the new state immediately since no BackgroundState classes are on the stack
43  requestStateTransition (BACKGROUND_STATE_UNLOADED);
44  completeRequestedStateTransitionIfExists ();
45 }
46 
47 void BackgroundStateContext::completeRequestedStateTransitionIfExists()
48 {
49  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::completeRequestedStateTransitionIfExists";
50 
51  if (m_currentState != m_requestedState) {
52 
53  // A transition is waiting so perform it
54 
55  if (m_currentState != NUM_BACKGROUND_STATES) {
56 
57  // This is not the first state so close the previous state
58  m_states [m_currentState]->end ();
59  }
60 
61  // Start the new state
62  m_currentState = m_requestedState;
63  m_states [m_requestedState]->begin ();
64  }
65 }
66 
68 {
69  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::fitInView";
70 
71  // After initialization, we should be in unloaded state or some other equally valid state
72  ENGAUGE_ASSERT (m_currentState != NUM_BACKGROUND_STATES);
73 
74  const QGraphicsPixmapItem *imageItem = &m_states [BACKGROUND_STATE_CURVE]->imageItem ();
75 
76  double width = imageItem->boundingRect().width();
77  double height = imageItem->boundingRect().height();
78 
79  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::fitInView"
80  << " state=" << m_states [m_currentState]->state ().toLatin1().data()
81  << " boundingRect=(" << width << "x" << height << ")";
82 
83  // Get the image from a state that is guaranteed to have an image
84  view.fitInView (imageItem);
85 
86 }
87 
89 {
90  return m_states [BACKGROUND_STATE_CURVE]->image();
91 }
92 
93 void BackgroundStateContext::requestStateTransition (BackgroundState backgroundState)
94 {
95  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::requestStateTransition";
96 
97  m_requestedState = backgroundState;
98 }
99 
100 void BackgroundStateContext::setBackgroundImage (BackgroundImage backgroundImage)
101 {
102  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setBackgroundImage"
103  << " background=" << backgroundImageToString (backgroundImage).toLatin1().data();
104 
105  BackgroundState backgroundState;
106  switch (backgroundImage) {
107  case BACKGROUND_IMAGE_FILTERED:
108  backgroundState = BACKGROUND_STATE_CURVE;
109  break;
110 
111  case BACKGROUND_IMAGE_NONE:
112  backgroundState = BACKGROUND_STATE_NONE;
113  break;
114 
115  case BACKGROUND_IMAGE_ORIGINAL:
116  backgroundState = BACKGROUND_STATE_ORIGINAL;
117  break;
118 
119  default:
120  LOG4CPP_ERROR_S ((*mainCat)) << "BackgroundStateContext::selectBackgroundImage";
121  exit (-1);
122  }
123 
124  // It is safe to transition to the new state immediately since no BackgroundState classes are on the stack
125  requestStateTransition (backgroundState);
126  completeRequestedStateTransitionIfExists ();
127 }
128 
130  const Transformation &transformation,
131  const DocumentModelGridRemoval &modelGridRemoval,
132  const DocumentModelColorFilter &modelColorFilter,
133  const QString &curveSelected)
134 {
135  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setCurveSelected"
136  << " curve=" << curveSelected.toLatin1().data();
137 
138  for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
139 
140  m_states [backgroundState]->setCurveSelected (isGnuplot,
141  transformation,
142  modelGridRemoval,
143  modelColorFilter,
144  curveSelected);
145  }
146 }
147 
149  const Transformation &transformation,
150  const DocumentModelGridRemoval &modelGridRemoval,
151  const DocumentModelColorFilter &modelColorFilter,
152  const QPixmap &pixmapOriginal,
153  const QString &curveSelected)
154 {
155  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setPixmap"
156  << " image=" << pixmapOriginal.width() << "x" << pixmapOriginal.height()
157  << " currentState=" << m_states [m_currentState]->state().toLatin1().data();
158 
159  for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
160 
161  m_states [backgroundState]->setPixmap (isGnuplot,
162  transformation,
163  modelGridRemoval,
164  modelColorFilter,
165  pixmapOriginal,
166  curveSelected);
167  }
168 }
169 
171  const Transformation &transformation,
172  const DocumentModelGridRemoval &modelGridRemoval,
173  const DocumentModelColorFilter &modelColorFilter,
174  const QString &curveSelected)
175 {
176  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::updateColorFilter";
177 
178  for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
179 
180  m_states [backgroundState]->updateColorFilter (isGnuplot,
181  transformation,
182  modelGridRemoval,
183  modelColorFilter,
184  curveSelected);
185  }
186 }
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.
QImage imageForCurveState() const
Image for the Curve state, even if the current state is different.
Background image state for showing original (=unfiltered) image.
BackgroundStateContext(MainWindow &mainWindow)
Single constructor.
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