Engauge Digitizer  2
DlgSettingsSegments.cpp
1 #include "CmdMediator.h"
2 #include "CmdSettingsSegments.h"
3 #include "DlgSettingsSegments.h"
4 #include "EngaugeAssert.h"
5 #include "Logger.h"
6 #include "MainWindow.h"
7 #include "PointStyle.h"
8 #include <QCheckBox>
9 #include <QComboBox>
10 #include <QGridLayout>
11 #include <QGraphicsScene>
12 #include <QLabel>
13 #include <qmath.h>
14 #include <QSpinBox>
15 #include "Segment.h"
16 #include "SegmentFactory.h"
17 #include "ViewPreview.h"
18 
19 const int MIN_LENGTH_MIN = 1;
20 const int MIN_LENGTH_MAX = 10000;
21 const int POINT_SEPARATION_MIN = 5;
22 const int POINT_SEPARATION_MAX = 10000;
23 
24 const int IMAGE_WIDTH = 400;
25 const int IMAGE_HEIGHT = 300;
26 
27 const double TWOPI = 2.0 * 3.1415926535;
28 
29 const double BRUSH_WIDTH = 2.0;
30 
32  DlgSettingsAbstractBase ("Segment Fill",
33  "DlgSettingsSegments",
34  mainWindow),
35  m_scenePreview (0),
36  m_viewPreview (0),
37  m_modelSegmentsBefore (0),
38  m_modelSegmentsAfter (0),
39  m_loading (false)
40 {
41  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::DlgSettingsSegments";
42 
43  QWidget *subPanel = createSubPanel ();
44  finishPanel (subPanel);
45 }
46 
47 DlgSettingsSegments::~DlgSettingsSegments()
48 {
49  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::~DlgSettingsSegments";
50 }
51 
52 void DlgSettingsSegments::clearPoints ()
53 {
54  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::clearPoints";
55 
56  QList<GraphicsPoint*>::iterator itrP;
57  for (itrP = m_points.begin(); itrP != m_points.end(); itrP++) {
58  GraphicsPoint *point = *itrP;
59  delete point;
60  }
61 
62  m_points.clear();
63 }
64 
65 void DlgSettingsSegments::createControls (QGridLayout *layout,
66  int &row)
67 {
68  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::createControls";
69 
70  QLabel *labelMinLength = new QLabel("Minimum length (points):");
71  layout->addWidget(labelMinLength, row, 1);
72 
73  m_spinMinLength = new QSpinBox;
74  m_spinMinLength->setRange (MIN_LENGTH_MIN, MIN_LENGTH_MAX);
75  m_spinMinLength->setWhatsThis (tr ("Select a minimum number of points in a segment.\n\n"
76  "Only segments with more points will be created.\n\n"
77  "This value should be as large as possible to reduce memory usage. This value has "
78  "a lower limit"));
79  connect (m_spinMinLength, SIGNAL (valueChanged (const QString &)), this, SLOT (slotMinLength (const QString &)));
80  layout->addWidget(m_spinMinLength, row++, 2);
81 
82  QLabel *labelPointSeparation = new QLabel("Point separation (pixels):");
83  layout->addWidget (labelPointSeparation, row, 1);
84 
85  m_spinPointSeparation = new QSpinBox;
86  m_spinPointSeparation->setRange (POINT_SEPARATION_MIN, POINT_SEPARATION_MAX);
87  m_spinPointSeparation->setWhatsThis (tr ("Select a point separation in pixels.\n\n"
88  "Successive points added to a segment will be separated by this number of pixels. "
89  "If Fill Corners is enabled, then additional points will be inserted at corners so some points "
90  "will be closer.\n\n"
91  "This value has a lower limit"));
92  connect (m_spinPointSeparation, SIGNAL (valueChanged (const QString &)), this, SLOT (slotPointSeparation (const QString &)));
93  layout->addWidget (m_spinPointSeparation, row++, 2);
94 
95  QLabel *labelFillCorners = new QLabel ("Fill corners:");
96  layout->addWidget (labelFillCorners, row, 1);
97 
98  m_chkFillCorners = new QCheckBox;
99  m_chkFillCorners->setWhatsThis (tr ("Fill corners.\n\n"
100  "In addition to the points placed at regular intervals, this option causes a point to be "
101  "placed at each corner. This option can capture important information in piecewise linear graphs, "
102  "but gradually curving graphs may not benefit from the additional points"));
103  connect (m_chkFillCorners, SIGNAL (stateChanged (int)), this, SLOT (slotFillCorners (int)));
104  layout->addWidget (m_chkFillCorners, row++, 2);
105 
106  QLabel *labelLineWidth = new QLabel("Line width:");
107  layout->addWidget (labelLineWidth, row, 1);
108 
109  m_spinLineWidth = new QSpinBox;
110  m_spinLineWidth->setWhatsThis (tr ("Select a size for the lines drawn along a segment"));
111  m_spinLineWidth->setMinimum(1);
112  connect (m_spinLineWidth, SIGNAL (valueChanged (int)), this, SLOT (slotLineWidth (int)));
113  layout->addWidget (m_spinLineWidth, row++, 2);
114 
115  QLabel *labelLineColor = new QLabel("Line color:");
116  layout->addWidget (labelLineColor, row, 1);
117 
118  m_cmbLineColor = new QComboBox;
119  m_cmbLineColor->setWhatsThis (tr ("Select a color for the lines drawn along a segment"));
120  populateColorComboWithTransparent (*m_cmbLineColor);
121  connect (m_cmbLineColor, SIGNAL (activated (const QString &)), this, SLOT (slotLineColor (const QString &))); // activated() ignores code changes
122  layout->addWidget (m_cmbLineColor, row++, 2);
123 }
124 
125 void DlgSettingsSegments::createPreview (QGridLayout *layout,
126  int &row)
127 {
128  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::createPreview";
129 
130  QLabel *labelPreview = new QLabel ("Preview");
131  layout->addWidget (labelPreview, row++, 0, 1, 4);
132 
133  m_scenePreview = new QGraphicsScene (this);
134  m_viewPreview = new ViewPreview (m_scenePreview, this);
135  m_viewPreview->setWhatsThis (tr ("Preview window shows the shortest line that can be segment filled, "
136  "and the effects of current settings on segments and points generated by segment fill"));
137  m_viewPreview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
138  m_viewPreview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
139  m_viewPreview->setMinimumHeight (MINIMUM_PREVIEW_HEIGHT);
140 
141  layout->addWidget (m_viewPreview, row++, 0, 1, 4);
142 }
143 
144 QImage DlgSettingsSegments::createPreviewImage () const
145 {
146  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::createPreviewImage";
147 
148  QImage image (IMAGE_WIDTH,
149  IMAGE_HEIGHT,
150  QImage::Format_RGB32);
151  image.fill (Qt::white);
152  QPainter painter (&image);
153  painter.setRenderHint(QPainter::Antialiasing);
154  painter.setPen (QPen (QBrush (Qt::black), BRUSH_WIDTH));
155 
156  int margin = IMAGE_WIDTH / 15;
157  int yCenter = IMAGE_HEIGHT / 2;
158  int yHeight = IMAGE_HEIGHT / 4;
159  int x, y, xLast, yLast;
160  bool isFirst;
161 
162  // Draw sinusoid
163  isFirst = true;
164  int xStart = margin, xEnd = IMAGE_WIDTH / 2 - margin;
165  for (x = xStart; x < xEnd; x++) {
166  double s = (double) (x - xStart) / (double) (xEnd - xStart);
167  int y = yCenter - yHeight * qSin (TWOPI * s);
168 
169  if (!isFirst) {
170  painter.drawLine (xLast, yLast, x, y);
171  }
172  isFirst = false;
173  xLast = x;
174  yLast = y;
175  }
176 
177  // Draw triangular waveform that looks like sinusoid straightened up into line segments
178  isFirst = true;
179  xStart = IMAGE_WIDTH / 2 + margin, xEnd = IMAGE_WIDTH - margin;
180  for (x = xStart; x < xEnd; x++) {
181  double s = (double) (x - xStart) / (double) (xEnd - xStart);
182  if (s <= 0.25) {
183  y = yCenter - yHeight * (4.0 * s);
184  } else if (s < 0.75) {
185  y = yCenter - yHeight * (1.0 - 4.0 * (s - 0.25));
186  } else {
187  y = yCenter + yHeight * (1.0 - 4 * (s - 0.75));
188  }
189 
190  if (!isFirst) {
191  painter.drawLine (xLast, yLast, x, y);
192  }
193  isFirst = false;
194  xLast = x;
195  yLast = y;
196  }
197 
198  return image;
199 }
200 
202 {
203  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::createSubPanel";
204 
205  QWidget *subPanel = new QWidget ();
206  QGridLayout *layout = new QGridLayout (subPanel);
207  subPanel->setLayout (layout);
208 
209  layout->setColumnStretch (0, 1); // Empty first column
210  layout->setColumnStretch (1, 0); // Labels
211  layout->setColumnStretch (2, 0); // User controls
212  layout->setColumnStretch (3, 1); // Empty last column
213 
214  int row = 0;
215  createControls(layout, row);
216  createPreview (layout, row);
217  QPixmap pixmap = QPixmap::fromImage (createPreviewImage());
218  m_scenePreview->addPixmap (pixmap);
219 
220  return subPanel;
221 }
222 
224 {
225  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::handleOk";
226 
228  cmdMediator ().document(),
229  *m_modelSegmentsBefore,
230  *m_modelSegmentsAfter);
231  cmdMediator ().push (cmd);
232 
233  hide ();
234 }
235 
237 {
238  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::load";
239 
240  // Loading starts here
241  m_loading = true;
242 
243  setCmdMediator (cmdMediator);
244 
245  // Flush old data
246  if (m_modelSegmentsBefore != 0) {
247  delete m_modelSegmentsBefore;
248  }
249  if (m_modelSegmentsAfter != 0) {
250  delete m_modelSegmentsAfter;
251  }
252 
253  // Save new data
254  m_modelSegmentsBefore = new DocumentModelSegments (cmdMediator.document());
255  m_modelSegmentsAfter = new DocumentModelSegments (cmdMediator.document());
256 
257  // Sanity checks. Incoming defaults must be acceptable to the local limits
258  ENGAUGE_ASSERT (MIN_LENGTH_MIN <= m_modelSegmentsAfter->minLength ());
259  ENGAUGE_ASSERT (MIN_LENGTH_MAX >= m_modelSegmentsAfter->minLength ());
260  ENGAUGE_ASSERT (POINT_SEPARATION_MIN <= m_modelSegmentsAfter->pointSeparation());
261  ENGAUGE_ASSERT (POINT_SEPARATION_MAX >= m_modelSegmentsAfter->pointSeparation());
262 
263  // Populate controls
264  m_spinPointSeparation->setValue (m_modelSegmentsAfter->pointSeparation());
265  m_spinMinLength->setValue (m_modelSegmentsAfter->minLength());
266  m_chkFillCorners->setChecked (m_modelSegmentsAfter->fillCorners ());
267  m_spinLineWidth->setValue (m_modelSegmentsAfter->lineWidth());
268 
269  int indexLineColor = m_cmbLineColor->findData(QVariant (m_modelSegmentsAfter->lineColor()));
270  ENGAUGE_ASSERT (indexLineColor >= 0);
271  m_cmbLineColor->setCurrentIndex(indexLineColor);
272 
273  // Loading finishes here
274  m_loading = false;
275 
276  updateControls();
277  enableOk (false); // Disable Ok button since there not yet any changes
278  updatePreview();
279 }
280 
281 void DlgSettingsSegments::slotFillCorners (int state)
282 {
283  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::slotFillCorner";
284 
285  m_modelSegmentsAfter->setFillCorners(state == Qt::Checked);
286  updateControls();
287  updatePreview();
288 }
289 
290 void DlgSettingsSegments::slotLineColor (const QString &)
291 {
292  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::slotLineColor";
293 
294  m_modelSegmentsAfter->setLineColor((ColorPalette) m_cmbLineColor->currentData().toInt());
295  updateControls();
296  updatePreview();
297 }
298 
299 void DlgSettingsSegments::slotLineWidth (int lineWidth)
300 {
301  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::slotLineWidth";
302 
303  m_modelSegmentsAfter->setLineWidth(lineWidth);
304  updateControls();
305  updatePreview();
306 }
307 
308 void DlgSettingsSegments::slotMinLength (const QString &minLength)
309 {
310  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::slotMinLength";
311 
312  m_modelSegmentsAfter->setMinLength(minLength.toDouble());
313  updateControls();
314  updatePreview();
315 }
316 
317 void DlgSettingsSegments::slotPointSeparation (const QString &pointSeparation)
318 {
319  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::slotPointSeparation";
320 
321  m_modelSegmentsAfter->setPointSeparation(pointSeparation.toDouble());
322  updateControls();
323  updatePreview();
324 }
325 
326 void DlgSettingsSegments::updateControls()
327 {
328  enableOk (true);
329 }
330 
331 void DlgSettingsSegments::updatePreview()
332 {
333  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsSegments::updatePreview"
334  << " loading=" << (m_loading ? "true" : "false");
335 
336  const QString ARBITRARY_IDENTIFIER ("");
337  const QColor COLOR (Qt::blue);
338  const int RADIUS = 5;
339 
340  if (!m_loading) {
341 
342  SegmentFactory segmentFactory (*m_scenePreview,
343  mainWindow().isGnuplot());
344 
345  clearPoints();
346  segmentFactory.clearSegments (m_segments);
347 
348  // Create new segments
349  segmentFactory.makeSegments (createPreviewImage(),
350  *m_modelSegmentsAfter,
351  m_segments);
352 
353  // Make the segment visible
354  QList<Segment*>::iterator itrS;
355  for (itrS = m_segments.begin(); itrS != m_segments.end(); itrS++) {
356  Segment *segment = *itrS;
357  segment->slotHover (true);
358  }
359 
360  // Create some points
361  PointStyle pointStyle (POINT_SHAPE_CROSS,
362  RADIUS,
363  BRUSH_WIDTH,
364  COLOR_PALETTE_BLUE);
365  QPolygonF polygon = pointStyle.polygon();
366  QList<QPoint> points = segmentFactory.fillPoints (*m_modelSegmentsAfter,
367  m_segments);
368  QList<QPoint>::iterator itrP;
369  for (itrP = points.begin(); itrP != points.end(); itrP++) {
370  QPoint pos = *itrP;
371  GraphicsPoint *graphicsPoint = new GraphicsPoint (*m_scenePreview,
372  ARBITRARY_IDENTIFIER,
373  pos,
374  COLOR,
375  polygon,
376  BRUSH_WIDTH);
377  m_points.push_back (graphicsPoint);
378  }
379  }
380 }
ColorPalette lineColor() const
Get method for line color.
void setLineColor(ColorPalette lineColor)
Set method for line color.
void setMinLength(double minLength)
Set method for min length.
void setCmdMediator(CmdMediator &cmdMediator)
Store CmdMediator for easy access by the leaf class.
Document & document()
Provide the Document to commands, primarily for undo/redo processing.
Definition: CmdMediator.cpp:61
double pointSeparation() const
Get method for point separation.
virtual void load(CmdMediator &cmdMediator)
Load settings from Document.
void setLineWidth(double lineWidth)
Set method for line width.
Factory class for Segment objects.
void slotHover(bool hover)
Slot for hover enter/leave events in the associated SegmentLines.
Definition: Segment.cpp:519
Class that modifies QGraphicsView to automatically expand/shrink the view to fit the window...
Definition: ViewPreview.h:8
void setFillCorners(bool fillCorners)
Set method for fill corners.
Details for a specific Point.
Definition: PointStyle.h:14
Selectable piecewise-defined line that follows a filtered line in the image.
Definition: Segment.h:15
double lineWidth() const
Get method for line width.
Graphics item for drawing a circular or polygonal Point.
Definition: GraphicsPoint.h:33
void finishPanel(QWidget *subPanel)
Add Ok and Cancel buttons to subpanel to get the whole dialog.
static int MINIMUM_PREVIEW_HEIGHT
Dialog layout constant that guarantees preview has sufficent room.
Command for DlgSettingsSegments.
void enableOk(bool enable)
Let leaf subclass control the Ok button.
Command queue stack.
Definition: CmdMediator.h:16
void populateColorComboWithTransparent(QComboBox &combo)
Add colors in color palette to combobox, with transparent entry at end.
DlgSettingsSegments(MainWindow &mainWindow)
Single constructor.
double minLength() const
Get method for min length.
Model for DlgSettingsSegments and CmdSettingsSegments.
Abstract base class for all Settings dialogs.
virtual void handleOk()
Process slotOk.
virtual QWidget * createSubPanel()
Create dialog-specific panel to which base class will add Ok and Cancel buttons.
void setPointSeparation(double pointSeparation)
Set method for point separation.
MainWindow & mainWindow()
Get method for MainWindow.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:60
CmdMediator & cmdMediator()
Provide access to Document information wrapped inside CmdMediator.
bool fillCorners() const
Get method for fill corners.