Engauge Digitizer  2
BackgroundStateContext.cpp
Go to the documentation of this file.
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 
8 #include "BackgroundStateCurve.h"
9 #include "BackgroundStateNone.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
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
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 
99 {
100  LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::requestStateTransition";
101 
102  m_requestedState = backgroundState;
103 }
104 
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) {
114  backgroundState = BACKGROUND_STATE_CURVE;
115  break;
116 
118  backgroundState = BACKGROUND_STATE_NONE;
119  break;
120 
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 }
BackgroundStateContext::requestStateTransition
void requestStateTransition(BackgroundState backgroundState)
Initiate state transition to be performed later, when BackgroundState is off the stack.
Definition: BackgroundStateContext.cpp:98
BackgroundStateUnloaded
Background image state for interval between startup and loading of the image.
Definition: BackgroundStateUnloaded.h:12
BACKGROUND_STATE_ORIGINAL
Definition: BackgroundStateAbstractBase.h:19
BACKGROUND_IMAGE_FILTERED
Definition: BackgroundImage.h:18
BackgroundStateContext::BackgroundStateContext
BackgroundStateContext(MainWindow &mainWindow)
Single constructor.
Definition: BackgroundStateContext.cpp:21
DocumentModelGridRemoval.h
NUM_BACKGROUND_STATES
Definition: BackgroundStateAbstractBase.h:21
BackgroundState
BackgroundState
Set of possible states of background image.
Definition: BackgroundStateAbstractBase.h:13
BackgroundStateContext::imageForCurveState
QImage imageForCurveState() const
Image for the Curve state, even if the current state is different.
Definition: BackgroundStateContext.cpp:93
BackgroundStateContext::setBackgroundImage
void setBackgroundImage(BackgroundImage backgroundImage)
Transition to the specified state. This method is used by classes outside of the state machine to tri...
Definition: BackgroundStateContext.cpp:105
BACKGROUND_IMAGE_NONE
Definition: BackgroundImage.h:16
BackgroundStateContext::~BackgroundStateContext
~BackgroundStateContext()
Destructor deallocates memory.
Definition: BackgroundStateContext.cpp:38
Transformation
Affine transformation between screen and graph coordinates, based on digitized axis points.
Definition: Transformation.h:30
EngaugeAssert.h
BackgroundStateCurve.h
BackgroundStateUnloaded.h
DocumentModelColorFilter
Model for DlgSettingsColorFilter and CmdSettingsColorFilter.
Definition: DocumentModelColorFilter.h:20
BackgroundStateOriginal
Background image state for showing original (=unfiltered) image.
Definition: BackgroundStateOriginal.h:12
GraphicsView
QGraphicsView class with event handling added. Typically the events are sent to the active digitizing...
Definition: GraphicsView.h:19
Transformation.h
MainWindow
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:90
Logger.h
BackgroundStateContext::updateColorFilter
void updateColorFilter(bool isGnuplot, const Transformation &transformation, const DocumentModelGridRemoval &modelGridRemoval, const DocumentModelColorFilter &colorFilter, const QString &curveSelected)
Apply color filter settings.
Definition: BackgroundStateContext.cpp:172
BackgroundStateContext.h
BACKGROUND_STATE_CURVE
Definition: BackgroundStateAbstractBase.h:17
LOG4CPP_INFO_S
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
BackgroundStateNone.h
backgroundImageToString
QString backgroundImageToString(BackgroundImage backgroundImage)
Definition: BackgroundImage.cpp:9
BACKGROUND_IMAGE_ORIGINAL
Definition: BackgroundImage.h:17
BACKGROUND_STATE_UNLOADED
Definition: BackgroundStateAbstractBase.h:20
mainCat
log4cpp::Category * mainCat
Definition: Logger.cpp:14
MainWindow.h
BACKGROUND_STATE_NONE
Definition: BackgroundStateAbstractBase.h:18
BackgroundStateContext::fitInView
void fitInView(GraphicsView &view)
Zoom so background fills the window.
Definition: BackgroundStateContext.cpp:72
BackgroundStateNone
Background image state for showing no image.
Definition: BackgroundStateNone.h:12
MainWindow::scene
GraphicsScene & scene()
Scene container for the QImage and QGraphicsItems.
Definition: MainWindow.cpp:1458
BackgroundStateContext::setPixmap
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.
Definition: BackgroundStateContext.cpp:150
BackgroundImage
BackgroundImage
Background selection.
Definition: BackgroundImage.h:12
DocumentModelGridRemoval
Model for DlgSettingsGridRemoval and CmdSettingsGridRemoval. The settings are unstable until the user...
Definition: DocumentModelGridRemoval.h:16
BackgroundStateOriginal.h
DocumentModelColorFilter.h
BackgroundStateCurve
Background image state for showing filter image from current curve.
Definition: BackgroundStateCurve.h:12
BackgroundStateContext::close
void close()
Open Document is being closed so remove the background.
Definition: BackgroundStateContext.cpp:43
ENGAUGE_ASSERT
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS) define ENGAUGE...
Definition: EngaugeAssert.h:19
BackgroundStateContext::setCurveSelected
void setCurveSelected(bool isGnuplot, const Transformation &transformation, const DocumentModelGridRemoval &modelGridRemoval, const DocumentModelColorFilter &modelColorFilter, const QString &curveSelected)
Update the selected curve.
Definition: BackgroundStateContext.cpp:131
GraphicsView.h