Engauge Digitizer  2
DlgSettingsAxesChecker.cpp
1 #include "Checker.h"
2 #include "CmdMediator.h"
3 #include "CmdSettingsAxesChecker.h"
4 #include "CoordScale.h"
5 #include "DlgSettingsAxesChecker.h"
6 #include "EngaugeAssert.h"
7 #include "Logger.h"
8 #include "MainWindow.h"
9 #include <QButtonGroup>
10 #include <QComboBox>
11 #include <QGraphicsRectItem>
12 #include <QGraphicsScene>
13 #include <QGridLayout>
14 #include <QGroupBox>
15 #include <QLabel>
16 #include <QLineEdit>
17 #include <qmath.h>
18 #include <QRadioButton>
19 #include "ViewPreview.h"
20 
21 const int AXIS_WIDTH = 4;
22 const int RECT_WIDTH = 640;
23 const int RECT_HEIGHT = 480;
24 const int X_LEFT = RECT_WIDTH / 8;
25 const int X_RIGHT = RECT_WIDTH * 7 / 8;
26 const int Y_TOP = RECT_HEIGHT / 8;
27 const int Y_BOTTOM = RECT_HEIGHT * 7 / 8;
28 const int TICKS_PER_AXIS = 6;
29 const int TICK_MARK_LENGTH = 8;
30 
32  DlgSettingsAbstractBase ("Axes Checker",
33  "DlgSettingsAxesChecker",
34  mainWindow),
35  m_checker (0),
36  m_modelAxesCheckerBefore (0),
37  m_modelAxesCheckerAfter (0),
38  m_modelCoords (0)
39 {
40  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAxesChecker::DlgSettingsAxesChecker";
41 
42  QWidget *subPanel = createSubPanel ();
43  finishPanel (subPanel);
44 }
45 
46 DlgSettingsAxesChecker::~DlgSettingsAxesChecker()
47 {
48  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAxesChecker::~DlgSettingsAxesChecker";
49 }
50 
51 void DlgSettingsAxesChecker::createControls (QGridLayout *layout,
52  int &row)
53 {
54  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAxesChecker::createControls";
55 
56  QGroupBox *groupBox = new QGroupBox (tr ("Axes Checker Lifetime"));
57  layout->addWidget (groupBox, row++, 1, 1, 2);
58 
59  QGridLayout *layoutLifetime = new QGridLayout;
60  groupBox->setLayout (layoutLifetime);
61 
62  int rowLifetime = 0;
63  m_btnNever = new QRadioButton ("Do not show", groupBox);
64  m_btnNever->setWhatsThis (tr ("Never show axes checker."));
65  layoutLifetime->addWidget (m_btnNever, rowLifetime++, 0, 1, 2);
66 
67  m_btnNSeconds = new QRadioButton ("Show for a number of seconds", groupBox);
68  m_btnNSeconds->setWhatsThis (tr ("Show axes checker for a number of seconds after changing axes points."));
69  layoutLifetime->addWidget (m_btnNSeconds, rowLifetime, 0, 1, 1);
70 
71  m_cmbSeconds = new QComboBox;
72  for (int seconds = 1; seconds <= 10; seconds++) {
73  m_cmbSeconds->addItem (QString::number (seconds), QVariant (seconds));
74  }
75  layoutLifetime->addWidget (m_cmbSeconds, rowLifetime++, 1);
76  connect (m_cmbSeconds, SIGNAL (activated (const QString &)), this, SLOT (slotSeconds (const QString &))); // activated() ignores code changes
77 
78  m_btnForever = new QRadioButton ("Show always", groupBox);
79  m_btnForever->setWhatsThis (tr ("Always show axes checker."));
80  layoutLifetime->addWidget (m_btnForever, rowLifetime++, 0, 1, 2);
81 
82  m_groupMode = new QButtonGroup;
83  m_groupMode->addButton (m_btnNever);
84  m_groupMode->addButton (m_btnNSeconds);
85  m_groupMode->addButton (m_btnForever);
86  connect (m_groupMode, SIGNAL (buttonReleased (QAbstractButton*)), this, SLOT (slotGroupMode (QAbstractButton*)));
87 
88  QLabel *labelLineColor = new QLabel ("Line color:");
89  layout->addWidget (labelLineColor, row, 1);
90 
91  m_cmbLineColor = new QComboBox;
92  m_cmbLineColor->setWhatsThis (tr ("Select a color for the highlight lines drawn at each axis point"));
93  populateColorComboWithoutTransparent (*m_cmbLineColor);
94  connect (m_cmbLineColor, SIGNAL (activated (const QString &)), this, SLOT (slotLineColor (const QString &))); // activated() ignores code changes
95  layout->addWidget (m_cmbLineColor, row++, 2);
96 }
97 
98 void DlgSettingsAxesChecker::createPoints ()
99 {
100  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAxesChecker::createPoints";
101 
102  QBrush AXES_BRUSH (Qt::gray);
103 
104  m_checker = new Checker (*m_scenePreview);
105 
106  // Create an invisible rectangular item that will guarantee a margin all around the outside, since otherwise QGraphicsView
107  // will zoom in on the points
108  QGraphicsRectItem *itemRect = new QGraphicsRectItem (0,
109  0,
110  RECT_WIDTH,
111  RECT_HEIGHT);
112  itemRect->setPen (Qt::NoPen);
113  m_scenePreview->addItem (itemRect);
114 
115  // For a realistic background, draw a rectangle underneath (lower z value), and some tick marks
116  QGraphicsRectItem *frameBox = new QGraphicsRectItem (X_LEFT,
117  Y_BOTTOM,
118  X_RIGHT - X_LEFT,
119  Y_TOP - Y_BOTTOM);
120  frameBox->setPen (QPen (AXES_BRUSH, AXIS_WIDTH));
121  frameBox->setZValue (-1);
122  m_scenePreview->addItem (frameBox);
123  for (int x = X_LEFT; x < X_RIGHT; x += (X_RIGHT - X_LEFT) / TICKS_PER_AXIS) {
124  QGraphicsLineItem *tick = new QGraphicsLineItem (x, Y_BOTTOM, x, Y_BOTTOM + TICK_MARK_LENGTH);
125  tick->setPen (QPen (AXES_BRUSH, AXIS_WIDTH));
126  tick->setZValue (-1);
127  m_scenePreview->addItem (tick);
128  }
129  for (int y = Y_TOP; y < Y_BOTTOM; y += (Y_BOTTOM - Y_TOP) / TICKS_PER_AXIS) {
130  QGraphicsLineItem *tick = new QGraphicsLineItem (X_LEFT, y, X_LEFT + TICK_MARK_LENGTH, y);
131  tick->setPen (QPen (AXES_BRUSH, AXIS_WIDTH));
132  tick->setZValue (-1);
133  m_scenePreview->addItem (tick);
134  }
135 }
136 
137 void DlgSettingsAxesChecker::createPreview (QGridLayout *layout,
138  int &row)
139 {
140  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAxesChecker::createPreview";
141 
142  QLabel *labelPreview = new QLabel ("Preview");
143  layout->addWidget (labelPreview, row++, 0, 1, 4);
144 
145  m_scenePreview = new QGraphicsScene (this);
146  m_viewPreview = new ViewPreview (m_scenePreview, this);
147  m_viewPreview->setWhatsThis (tr ("Preview window that shows how current settings affect the displayed axes checker"));
148  m_viewPreview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
149  m_viewPreview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
150  m_viewPreview->setMinimumHeight (MINIMUM_PREVIEW_HEIGHT);
151 
152  layout->addWidget (m_viewPreview, row++, 0, 1, 4);
153 }
154 
156 {
157  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAxesChecker::createSubPanel";
158 
159  QWidget *subPanel = new QWidget ();
160  QGridLayout *layout = new QGridLayout (subPanel);
161  subPanel->setLayout (layout);
162 
163  layout->setColumnStretch(0, 1); // Empty first column
164  layout->setColumnStretch(1, 0); // X
165  layout->setColumnStretch(2, 0); // Y
166  layout->setColumnStretch(3, 1); // Empty first column
167 
168  int row = 0;
169  createControls (layout, row);
170  createPreview (layout, row);
171 
172  createPoints ();
173 
174  return subPanel;
175 }
176 
178 {
179  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAxesChecker::handleOk";
180 
182  cmdMediator ().document(),
183  *m_modelAxesCheckerBefore,
184  *m_modelAxesCheckerAfter);
185  cmdMediator ().push (cmd);
186 
187  hide ();
188 }
189 
191 {
192  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAxesChecker::load";
193 
194  setCmdMediator (cmdMediator);
195 
196  // Flush old data
197  if (m_modelAxesCheckerBefore != 0) {
198  delete m_modelAxesCheckerBefore;
199  }
200  if (m_modelAxesCheckerAfter != 0) {
201  delete m_modelAxesCheckerAfter;
202  }
203  if (m_modelCoords != 0) {
204  delete m_modelCoords;
205  }
206 
207  // Save new data
208  m_modelAxesCheckerBefore = new DocumentModelAxesChecker (cmdMediator.document());
209  m_modelAxesCheckerAfter = new DocumentModelAxesChecker (cmdMediator.document());
210  m_modelCoords = new DocumentModelCoords (cmdMediator.document());
211 
212  // Populate controls
213  CheckerMode checkerMode = m_modelAxesCheckerAfter->checkerMode();
214  m_btnNever->setChecked (checkerMode == CHECKER_MODE_NEVER);
215  m_btnNSeconds->setChecked (checkerMode == CHECKER_MODE_N_SECONDS);
216  m_btnForever->setChecked (checkerMode == CHECKER_MODE_FOREVER);
217  int indexSeconds = m_cmbSeconds->findData (QVariant (m_modelAxesCheckerAfter->checkerSeconds()));
218  ENGAUGE_ASSERT (indexSeconds >= 0);
219  m_cmbSeconds->setCurrentIndex(indexSeconds);
220 
221  int indexLineColor = m_cmbLineColor->findData (QVariant (m_modelAxesCheckerAfter->lineColor()));
222  ENGAUGE_ASSERT (indexLineColor >= 0);
223  m_cmbLineColor->setCurrentIndex (indexLineColor);
224 
225  updateControls ();
226  enableOk (false); // Disable Ok button since there not yet any changes
227  updatePreview();
228 }
229 
230 void DlgSettingsAxesChecker::slotGroupMode (QAbstractButton*)
231 {
232  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAxesChecker::slotGroupMode";
233 
234  if (m_btnNever->isChecked ()) {
235  m_modelAxesCheckerAfter->setCheckerMode(CHECKER_MODE_NEVER);
236  } else if (m_btnNSeconds->isChecked ()) {
237  m_modelAxesCheckerAfter->setCheckerMode(CHECKER_MODE_N_SECONDS);
238  } else {
239  m_modelAxesCheckerAfter->setCheckerMode(CHECKER_MODE_FOREVER);
240  }
241 
242  updateControls ();
243  updatePreview();
244 }
245 
246 void DlgSettingsAxesChecker::slotLineColor(const QString &)
247 {
248  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAxesChecker::slotLineColor";
249 
250  m_modelAxesCheckerAfter->setLineColor ((ColorPalette) m_cmbLineColor->currentData().toInt());
251  updateControls();
252  updatePreview();
253 }
254 
255 void DlgSettingsAxesChecker::slotSeconds (const QString &)
256 {
257  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAxesChecker::slotLineColor";
258 
259  m_modelAxesCheckerAfter->setCheckerSeconds(m_cmbSeconds->currentData().toInt());
260  updateControls();
261 }
262 
263 void DlgSettingsAxesChecker::updateControls ()
264 {
265  enableOk (true);
266 
267  m_cmbSeconds->setEnabled (m_btnNSeconds->isChecked ());
268 }
269 
270 void DlgSettingsAxesChecker::updatePreview()
271 {
272  const int ZERO_RADIUS_SINCE_NO_POINTS = 0;
273 
274  QVector<QPointF> points;
275  points.push_back (QPointF (X_LEFT, Y_TOP));
276  points.push_back (QPointF (X_LEFT, Y_BOTTOM));
277  points.push_back (QPointF (X_RIGHT, Y_BOTTOM));
278 
279  QPolygonF polygon (points);
280 
281  ENGAUGE_ASSERT (m_checker != 0);
282  m_checker->prepareForDisplay (polygon,
283  ZERO_RADIUS_SINCE_NO_POINTS,
284  *m_modelAxesCheckerAfter,
285  *m_modelCoords);
286 }
int checkerSeconds() const
Get method for checker lifetime in seconds.
void setCheckerMode(CheckerMode checkerMode)
Set method for checker mode.
void setCheckerSeconds(int seconds)
Set method for checker lifetime in seconds.
void setCmdMediator(CmdMediator &cmdMediator)
Store CmdMediator for easy access by the leaf class.
ColorPalette lineColor() const
Get method for line color.
Document & document()
Provide the Document to commands, primarily for undo/redo processing.
Definition: CmdMediator.cpp:61
Box shape that is drawn through the three axis points, to temporarily (usually) or permanently (rarel...
Definition: Checker.h:28
void setLineColor(ColorPalette lineColor)
Set method for line color.
virtual void load(CmdMediator &cmdMediator)
Load settings from Document.
CheckerMode checkerMode() const
Get method for checker lifetime mode.
Class that modifies QGraphicsView to automatically expand/shrink the view to fit the window...
Definition: ViewPreview.h:8
Command for DlgSettingsAxesChecker.
void populateColorComboWithoutTransparent(QComboBox &combo)
Add colors in color palette to combobox, without transparent entry at end.
Model for DlgSettingsCoords and CmdSettingsCoords.
virtual QWidget * createSubPanel()
Create dialog-specific panel to which base class will add Ok and Cancel buttons.
void finishPanel(QWidget *subPanel)
Add Ok and Cancel buttons to subpanel to get the whole dialog.
virtual void handleOk()
Process slotOk.
Model for DlgSettingsAxesChecker and CmdSettingsAxesChecker.
static int MINIMUM_PREVIEW_HEIGHT
Dialog layout constant that guarantees preview has sufficent room.
void enableOk(bool enable)
Let leaf subclass control the Ok button.
Command queue stack.
Definition: CmdMediator.h:16
Abstract base class for all Settings dialogs.
DlgSettingsAxesChecker(MainWindow &mainWindow)
Single constructor.
void prepareForDisplay(const QPolygonF &polygon, int pointRadius, const DocumentModelAxesChecker &modelAxesChecker, const DocumentModelCoords &modelCoords)
Create the polygon from current information, including pixel coordinates, just prior to display...
Definition: Checker.cpp:432
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.