00001
00002
00003
00004
00005
00006
00007 #include "BackgroundStateContext.h"
00008 #include "BackgroundStateCurve.h"
00009 #include "BackgroundStateNone.h"
00010 #include "BackgroundStateOriginal.h"
00011 #include "BackgroundStateUnloaded.h"
00012 #include "DocumentModelColorFilter.h"
00013 #include "DocumentModelGridRemoval.h"
00014 #include "EngaugeAssert.h"
00015 #include "GraphicsView.h"
00016 #include "Logger.h"
00017 #include "MainWindow.h"
00018 #include <QGraphicsPixmapItem>
00019 #include "Transformation.h"
00020
00021 BackgroundStateContext::BackgroundStateContext(MainWindow &mainWindow) :
00022 m_mainWindow (mainWindow)
00023 {
00024 LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::BackgroundStateContext";
00025
00026
00027 m_states.insert (BACKGROUND_STATE_CURVE , new BackgroundStateCurve (*this, mainWindow.scene()));
00028 m_states.insert (BACKGROUND_STATE_NONE , new BackgroundStateNone (*this, mainWindow.scene()));
00029 m_states.insert (BACKGROUND_STATE_ORIGINAL, new BackgroundStateOriginal (*this, mainWindow.scene()));
00030 m_states.insert (BACKGROUND_STATE_UNLOADED, new BackgroundStateUnloaded (*this, mainWindow.scene()));
00031 ENGAUGE_ASSERT (m_states.size () == NUM_BACKGROUND_STATES);
00032
00033 m_currentState = NUM_BACKGROUND_STATES;
00034 requestStateTransition (BACKGROUND_STATE_UNLOADED);
00035 completeRequestedStateTransitionIfExists();
00036 }
00037
00038 void BackgroundStateContext::close()
00039 {
00040 LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::close";
00041
00042
00043 requestStateTransition (BACKGROUND_STATE_UNLOADED);
00044 completeRequestedStateTransitionIfExists ();
00045 }
00046
00047 void BackgroundStateContext::completeRequestedStateTransitionIfExists()
00048 {
00049 LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::completeRequestedStateTransitionIfExists";
00050
00051 if (m_currentState != m_requestedState) {
00052
00053
00054
00055 if (m_currentState != NUM_BACKGROUND_STATES) {
00056
00057
00058 m_states [m_currentState]->end ();
00059 }
00060
00061
00062 m_currentState = m_requestedState;
00063 m_states [m_requestedState]->begin ();
00064 }
00065 }
00066
00067 void BackgroundStateContext::fitInView (GraphicsView &view)
00068 {
00069 LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::fitInView";
00070
00071
00072 ENGAUGE_ASSERT (m_currentState != NUM_BACKGROUND_STATES);
00073
00074 const QGraphicsPixmapItem *imageItem = &m_states [BACKGROUND_STATE_CURVE]->imageItem ();
00075
00076 double width = imageItem->boundingRect().width();
00077 double height = imageItem->boundingRect().height();
00078
00079 LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::fitInView"
00080 << " state=" << m_states [m_currentState]->state ().toLatin1().data()
00081 << " boundingRect=(" << width << "x" << height << ")";
00082
00083
00084 view.fitInView (imageItem);
00085
00086 }
00087
00088 QImage BackgroundStateContext::imageForCurveState () const
00089 {
00090 return m_states [BACKGROUND_STATE_CURVE]->image();
00091 }
00092
00093 void BackgroundStateContext::requestStateTransition (BackgroundState backgroundState)
00094 {
00095 LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::requestStateTransition";
00096
00097 m_requestedState = backgroundState;
00098 }
00099
00100 void BackgroundStateContext::setBackgroundImage (BackgroundImage backgroundImage)
00101 {
00102 LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setBackgroundImage"
00103 << " background=" << backgroundImageToString (backgroundImage).toLatin1().data();
00104
00105 BackgroundState backgroundState;
00106 switch (backgroundImage) {
00107 case BACKGROUND_IMAGE_FILTERED:
00108 backgroundState = BACKGROUND_STATE_CURVE;
00109 break;
00110
00111 case BACKGROUND_IMAGE_NONE:
00112 backgroundState = BACKGROUND_STATE_NONE;
00113 break;
00114
00115 case BACKGROUND_IMAGE_ORIGINAL:
00116 backgroundState = BACKGROUND_STATE_ORIGINAL;
00117 break;
00118
00119 default:
00120 LOG4CPP_ERROR_S ((*mainCat)) << "BackgroundStateContext::selectBackgroundImage";
00121 exit (-1);
00122 }
00123
00124
00125 requestStateTransition (backgroundState);
00126 completeRequestedStateTransitionIfExists ();
00127 }
00128
00129 void BackgroundStateContext::setCurveSelected (const Transformation &transformation,
00130 const DocumentModelGridRemoval &modelGridRemoval,
00131 const DocumentModelColorFilter &modelColorFilter,
00132 const QString &curveSelected)
00133 {
00134 LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setCurveSelected"
00135 << " curve=" << curveSelected.toLatin1().data();
00136
00137 for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
00138
00139 m_states [backgroundState]->setCurveSelected (transformation,
00140 modelGridRemoval,
00141 modelColorFilter,
00142 curveSelected);
00143 }
00144 }
00145
00146 void BackgroundStateContext::setPixmap (const Transformation &transformation,
00147 const DocumentModelGridRemoval &modelGridRemoval,
00148 const DocumentModelColorFilter &modelColorFilter,
00149 const QPixmap &pixmapOriginal,
00150 const QString &curveSelected)
00151 {
00152 LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::setPixmap"
00153 << " image=" << pixmapOriginal.width() << "x" << pixmapOriginal.height()
00154 << " currentState=" << m_states [m_currentState]->state().toLatin1().data();
00155
00156 for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
00157
00158 m_states [backgroundState]->setPixmap (transformation,
00159 modelGridRemoval,
00160 modelColorFilter,
00161 pixmapOriginal,
00162 curveSelected);
00163 }
00164 }
00165
00166 void BackgroundStateContext::updateColorFilter (const Transformation &transformation,
00167 const DocumentModelGridRemoval &modelGridRemoval,
00168 const DocumentModelColorFilter &modelColorFilter,
00169 const QString &curveSelected)
00170 {
00171 LOG4CPP_INFO_S ((*mainCat)) << "BackgroundStateContext::updateColorFilter";
00172
00173 for (int backgroundState = 0; backgroundState < NUM_BACKGROUND_STATES; backgroundState++) {
00174
00175 m_states [backgroundState]->updateColorFilter (transformation,
00176 modelGridRemoval,
00177 modelColorFilter,
00178 curveSelected);
00179 }
00180 }