1 #include "CmdMediator.h"
2 #include "CmdSettingsColorFilter.h"
3 #include "ColorFilter.h"
4 #include "ColorFilterHistogram.h"
5 #include "DigitizeStateContext.h"
6 #include "DigitizeStateColorPicker.h"
7 #include "DocumentModelColorFilter.h"
8 #include "EngaugeAssert.h"
10 #include "MainWindow.h"
12 #include <QGraphicsPixmapItem>
13 #include <QGraphicsScene>
15 #include <QMessageBox>
22 DigitizeStateColorPicker::~DigitizeStateColorPicker ()
33 LOG4CPP_INFO_S ((*mainCat)) <<
"DigitizeStateColorPicker::begin";
39 m_previousDigitizeState = previousState;
45 bool DigitizeStateColorPicker::computeFilterFromPixel (
const QPointF &posScreen,
46 const QString &curveName,
49 LOG4CPP_INFO_S ((*mainCat)) <<
"DigitizeStateColorPicker::computeFilterFromPixel";
59 QPointF posScreenPlusHalf = posScreen - QPointF (0.5, 0.5);
62 rtn = findNearestNonBackgroundPixel (image,
70 int r = qRed (pixel.rgb());
71 int g = qGreen (pixel.rgb());
72 int b = qBlue (pixel.rgb());
73 if (r == g && g == b) {
77 COLOR_FILTER_MODE_INTENSITY);
83 COLOR_FILTER_MODE_HUE);
106 int lowerBin = pixelBin, upperBin = pixelBin;
107 while ((lowerBin > 0) &&
108 (histogramBins [lowerBin - 1] <= histogramBins [lowerBin]) &&
109 (histogramBins [lowerBin] > 0)) {
113 (histogramBins [upperBin + 1] <= histogramBins [upperBin]) &&
114 (histogramBins [upperBin] > 0)) {
126 saveLowerValueUpperValue (modelColorFilterAfter,
131 free (histogramBins);
135 QMessageBox::warning (0,
137 "Sorry, but the color picker point must be near a non-background pixel. Please try again.");
147 const int HOT_X_IN_BITMAP = 8;
148 const int HOT_Y_IN_BITMAP = 24;
149 LOG4CPP_DEBUG_S ((*mainCat)) <<
"DigitizeStateColorPicker::cursor";
151 QBitmap bitmap (
":/engauge/img/cursor_eyedropper.xpm");
152 QBitmap bitmapMask (
":/engauge/img/cursor_eyedropper_mask.xpm");
153 return QCursor (bitmap,
161 LOG4CPP_INFO_S ((*mainCat)) <<
"DigitizeStateColorPicker::end";
168 bool DigitizeStateColorPicker::findNearestNonBackgroundPixel (
const QImage &image,
169 const QPointF &posScreenPlusHalf,
170 const QRgb &rgbBackground,
173 QPoint pos = posScreenPlusHalf.toPoint ();
178 for (
int radius = 0; radius < maxRadiusForSearch; radius++) {
180 for (
int xOffset = -radius; xOffset <= radius; xOffset++) {
181 for (
int yOffset = -radius; yOffset <= radius; yOffset++) {
184 pixel = image.pixel (pos.x () + xOffset, pos.y () - radius);
185 if (pixel != rgbBackground) {
190 pixel = image.pixel (pos.x () + xOffset, pos.y () + radius);
191 if (pixel != rgbBackground) {
196 pixel = image.pixel (pos.x () - radius, pos.y () - yOffset);
197 if (pixel != rgbBackground) {
202 pixel = image.pixel (pos.x () + radius, pos.y () + yOffset);
203 if (pixel != rgbBackground) {
215 LOG4CPP_INFO_S ((*mainCat)) <<
"DigitizeStateColorPicker::handleCurveChange";
221 LOG4CPP_INFO_S ((*mainCat)) <<
"DigitizeStateColorPicker::handleKeyPress"
222 <<
" key=" << QKeySequence (key).toString ().toLatin1 ().data ();
232 LOG4CPP_INFO_S ((*mainCat)) <<
"DigitizeStateColorPicker::handleMousePress";
237 LOG4CPP_INFO_S ((*mainCat)) <<
"DigitizeStateColorPicker::handleMouseRelease";
241 if (computeFilterFromPixel (posScreen,
242 context().mainWindow().selectedGraphCurve(),
243 modelColorFilterAfter)) {
250 context ().cmdMediator ().document (),
251 modelColorFilterBefore,
252 modelColorFilterAfter);
258 const QString &curveName,
263 case COLOR_FILTER_MODE_FOREGROUND:
270 case COLOR_FILTER_MODE_HUE:
271 modelColorFilterAfter.
setHueLow(curveName,
277 case COLOR_FILTER_MODE_INTENSITY:
284 case COLOR_FILTER_MODE_SATURATION:
291 case COLOR_FILTER_MODE_VALUE:
299 ENGAUGE_ASSERT (
false);
305 return "DigitizeStateColorPicker";
310 LOG4CPP_INFO_S ((*mainCat)) <<
"DigitizeStateColorPicker::updateModelDigitizeCurve";
315 LOG4CPP_INFO_S ((*mainCat)) <<
"DigitizeStateColorPicker::updateModelSegments";
void requestDelayedStateTransition(DigitizeState digitizeState)
Initiate state transition to be performed later, when DigitizeState is off the stack.
void generate(const ColorFilter &filter, double histogramBins[], ColorFilterMode colorFilterMode, const QImage &image, int &maxBinCount) const
Generate the histogram.
DocumentModelColorFilter modelColorFilter() const
Get method for DocumentModelColorFilter.
virtual void handleMousePress(QPointF posScreen)
Handle a mouse press that was intercepted earlier.
void setColorFilterMode(const QString &curveName, ColorFilterMode colorFilterMode)
Set method for filter mode.
CmdMediator & cmdMediator()
Provide CmdMediator for indirect access to the Document.
void setDragMode(QGraphicsView::DragMode dragMode)
Set QGraphicsView drag mode (in m_view). Called from DigitizeStateAbstractBase subclasses.
DigitizeStateColorPicker(DigitizeStateContext &context)
Single constructor.
virtual QString state() const
State name for debugging.
virtual void begin(DigitizeState previousState)
Method that is called at the exact moment a state is entered.
void updateViewsOfSettings(const QString &activeCurve)
Update curve-specific view of settings. Private version gets active curve name from DigitizeStateCont...
QString selectedGraphCurve() const
Curve name that is currently selected in m_cmbCurve.
int cursorSize() const
Get method for effective cursor size.
QPixmap pixmap() const
Return the image that is being digitized.
virtual void handleKeyPress(Qt::Key key, bool atLeastOneSelectedItem)
Handle a key press that was intercepted earlier.
virtual void updateModelSegments(const DocumentModelSegments &modelSegments)
Update the segments given the new settings.
virtual QString activeCurve() const
Name of the active Curve. This can include AXIS_CURVE_NAME.
Class for filtering image to remove unimportant information.
BackgroundImage selectOriginal(BackgroundImage backgroundImage)
Make original background visible, for DigitizeStateColorPicker.
void setValueLow(const QString &curveName, int valueLow)
Set method for value low.
DigitizeStateContext & context()
Reference to the DigitizeStateContext that contains all the DigitizeStateAbstractBase subclasses...
MainWindow & mainWindow()
Reference to the MainWindow, without const.
virtual QCursor cursor() const
Returns the state-specific cursor shape.
void setForegroundLow(const QString &curveName, int foregroundLow)
Set method for foreground lower bound.
void setHueLow(const QString &curveName, int hueLow)
Set method for hue lower bound.
Model for DlgSettingsDigitizeCurve and CmdSettingsDigitizeCurve.
void setIntensityLow(const QString &curveName, int intensityLow)
Set method for intensity lower bound.
void setForegroundHigh(const QString &curveName, int foregroundHigh)
Set method for foreground higher bound.
Model for DlgSettingsColorFilter and CmdSettingsColorFilter.
void setIntensityHigh(const QString &curveName, int intensityHigh)
Set method for intensity higher bound.
Container for all DigitizeStateAbstractBase subclasses. This functions as the context class in a stan...
void setCursor()
Update the cursor according to the current state.
QRgb marginColor(const QImage *image) const
Identify the margin color of the image, which is defined as the most common color in the four margins...
int binFromPixel(const ColorFilter &filter, ColorFilterMode colorFilterMode, const QColor &pixel, const QRgb &rgbBackground) const
Compute histogram bin number from pixel according to filter.
virtual void handleMouseMove(QPointF posScreen)
Handle a mouse move. This is part of an experiment to see if augmenting the cursor in Point Match mod...
virtual void handleCurveChange()
Handle the selection of a new curve. At a minimum, DigitizeStateSegment will generate a new set of Se...
ColorFilterMode colorFilterMode(const QString &curveName) const
Get method for filter mode.
Command for DlgSettingsColorFilter.
void setSaturationLow(const QString &curveName, int saturationLow)
Set method for saturation low.
int valueFromBin(const ColorFilter &filter, ColorFilterMode colorFilterMode, int bin)
Inverse of binFromPixel.
void setSaturationHigh(const QString &curveName, int saturationHigh)
Set method for saturation high.
void appendNewCmd(QUndoCommand *cmd)
Append just-created QUndoCommand to command stack. This is called from DigitizeStateAbstractBase subc...
virtual void end()
Method that is called at the exact moment a state is exited. Typically called just before begin for t...
void setHueHigh(const QString &curveName, int hueHigh)
Set method for hue higher bound.
Model for DlgSettingsSegments and CmdSettingsSegments.
virtual void updateModelDigitizeCurve(const DocumentModelDigitizeCurve &modelDigitizeCurve)
Update the digitize curve settings.
void setValueHigh(const QString &curveName, int valueHigh)
Set method for value high.
Base class for all digitizing states. This serves as an interface to DigitizeStateContext.
Class that generates a histogram according to the current filter.
static int HISTOGRAM_BINS()
Number of histogram bins.
DocumentModelGeneral modelGeneral() const
Get method for DocumentModelGeneral.
virtual void handleMouseRelease(QPointF posScreen)
Handle a mouse release that was intercepted earlier.