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