Engauge Digitizer  2
DlgEditScale.cpp
Go to the documentation of this file.
1 /******************************************************************************************************
2  * (C) 2017 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 "DlgEditScale.h"
8 #include "DlgValidatorAbstract.h"
9 #include "DlgValidatorFactory.h"
11 #include "DocumentModelCoords.h"
12 #include "DocumentModelGeneral.h"
13 #include "EngaugeAssert.h"
14 #include "FormatCoordsUnits.h"
15 #include "FormatDateTime.h"
18 #include "Logger.h"
19 #include "MainWindow.h"
20 #include "MainWindowModel.h"
21 #include <QGridLayout>
22 #include <QGroupBox>
23 #include <QHBoxLayout>
24 #include <QLabel>
25 #include <QRect>
26 #include "QtToString.h"
27 #include <QVBoxLayout>
28 #include "Transformation.h"
29 
30 const Qt::Alignment ALIGNMENT = Qt::AlignCenter;
31 
33 
35  const DocumentModelCoords &modelCoords,
36  const DocumentModelGeneral &modelGeneral,
37  const MainWindowModel &modelMainWindow,
38  const double *scaleLength) :
39  QDialog (&mainWindow),
40  m_modelCoords (modelCoords),
41  m_modelGeneral (modelGeneral),
42  m_modelMainWindow (modelMainWindow)
43 {
44  LOG4CPP_INFO_S ((*mainCat)) << "DlgEditScale::DlgEditScale";
45 
46  QVBoxLayout *layout = new QVBoxLayout;
47  setLayout (layout);
48 
49  setCursor (QCursor (Qt::ArrowCursor));
50  setModal(true);
51  setWindowTitle (tr ("Edit Axis Point"));
52 
53  createScaleLength (layout);
54  createHint (layout);
55  createOkCancel (layout);
56 
57  initializeScaleLength (scaleLength);
58 
59  updateControls ();
60 }
61 
63 {
64  LOG4CPP_INFO_S ((*mainCat)) << "DlgEditScale::~DlgEditScale";
65 }
66 
67 void DlgEditScale::createHint (QVBoxLayout *layoutOuter)
68 {
69  // Insert a hint explaining why decimal points may not be accepted. Very confusing for user to figure out the problem at first, and
70  // then figure out which setting should change to fix it. The hint is centered so it is slightly less intrusive
71 
72  QWidget *widget = new QWidget;
73  layoutOuter->addWidget (widget, 0, Qt::AlignCenter);
74 
75  QHBoxLayout *layout = new QHBoxLayout;
76  widget->setLayout (layout);
77 
78  QString locale = QLocaleToString (m_modelMainWindow.locale ());
79  QString hint = QString ("%1: %2")
80  .arg (tr ("Number format"))
81  .arg (locale);
82  QLabel *label = new QLabel (hint);
83  layout->addWidget (label);
84 }
85 
86 void DlgEditScale::createOkCancel (QVBoxLayout *layoutOuter)
87 {
88  QWidget *panel = new QWidget (this);
89  layoutOuter->addWidget (panel, 0, Qt::AlignCenter);
90 
91  QHBoxLayout *layout = new QHBoxLayout (panel);
92  panel->setLayout (layout);
93 
94  m_btnOk = new QPushButton (tr ("Ok"), this);
95  layout->addWidget(m_btnOk);
96  connect (m_btnOk, SIGNAL (released ()), this, SLOT (accept ()));
97 
98  m_btnCancel = new QPushButton (tr ("Cancel"), this);
99  layout->addWidget(m_btnCancel);
100  connect (m_btnCancel, SIGNAL (released ()), this, SLOT (reject ()));
101 }
102 
103 void DlgEditScale::createScaleLength (QVBoxLayout *layoutOuter)
104 {
105  // Simple validation strategy
106  DlgValidatorFactory dlgValidatorFactory;
107  m_validatorScaleLength = dlgValidatorFactory.createAboveZero (m_modelMainWindow.locale());
108 
109  // Label, with guidance in terms of legal ranges and units
110  QGroupBox *panel = new QGroupBox (tr ("Scale Length"), this);
111  layoutOuter->addWidget (panel);
112 
113  QHBoxLayout *layout = new QHBoxLayout (panel);
114  panel->setLayout (layout);
115 
116  // Row
117  m_editScaleLength = new QLineEdit;
118  m_editScaleLength->setMinimumWidth(MIN_WIDTH_TO_FIT_STRANGE_UNITS);
119  m_editScaleLength->setAlignment (ALIGNMENT);
120  m_editScaleLength->setValidator (m_validatorScaleLength);
121  // setStatusTip does not work for modal dialogs
122  m_editScaleLength->setWhatsThis (tr ("Enter the scale bar length"));
123  layout->addWidget(m_editScaleLength, 0);
124  connect (m_editScaleLength, SIGNAL (textChanged (const QString &)), this, SLOT (slotTextChanged (const QString &)));
125 }
126 
127 void DlgEditScale::initializeScaleLength (const double *scaleLength)
128 {
129  if (scaleLength != nullptr) {
130  m_editScaleLength->setText (QString::number (*scaleLength));
131  }
132 }
133 
135 {
136  double xTheta, yRadius;
137  const QString DUMMY_Y ("0");
138 
139  FormatCoordsUnits format;
140 
141  // Format conversion is done using x coordinate. Y coordinate is given a dummy value and the result is ignored
142  format.formattedToUnformatted (m_editScaleLength->text(),
143  DUMMY_Y,
144  m_modelCoords,
145  m_modelMainWindow,
146  xTheta,
147  yRadius);
148 
149  return xTheta;
150 }
151 
152 void DlgEditScale::slotTextChanged (const QString &)
153 {
154  updateControls ();
155 }
156 
157 void DlgEditScale::updateControls ()
158 {
159  QString textScaleLength = m_editScaleLength->text();
160 
161  int posScaleLength;
162 
163  // Check for not empty (which allows single minus sign) and for valid number (which prevents single minus sign)
164  m_btnOk->setEnabled (!textScaleLength.isEmpty () &&
165  (m_validatorScaleLength->validate(textScaleLength, posScaleLength) == QValidator::Acceptable));
166 }
Model for DlgSettingsGeneral and CmdSettingsGeneral.
void formattedToUnformatted(const QString &xThetaFormatted, const QString &yRadiusFormatted, const DocumentModelCoords &modelCoords, const MainWindowModel &mainWindowModel, double &xThetaUnformatted, double &yRadiusUnformatted) const
Convert formatted string to unformatted numeric value.
DlgValidatorAbstract * createAboveZero(const QLocale &locale) const
Factory method for generating validators for scale length which must be a number greater than zero.
virtual QValidator::State validate(QString &input, int &pos) const =0
Validate according to the numeric format specific to the leaf class.
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
Model for DlgSettingsMainWindow.
Model for DlgSettingsCoords and CmdSettingsCoords.
Highest-level wrapper around other Formats classes.
QLocale locale() const
Get method for locale.
log4cpp::Category * mainCat
Definition: Logger.cpp:14
const Qt::Alignment ALIGNMENT
const int MIN_WIDTH_TO_FIT_STRANGE_UNITS
Validator factory.
double scaleLength() const
Return the scale bar length specified by the user. Only applies if dialog was accepted.
DlgEditScale(MainWindow &mainWindow, const DocumentModelCoords &modelCoords, const DocumentModelGeneral &modelGeneral, const MainWindowModel &modelMainWindow, const double *scaleLength=0)
Single constructor.
QString QLocaleToString(const QLocale &locale)
Definition: QtToString.cpp:59
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:91