00001
00002
00003
00004
00005
00006
00007 #include "DlgEditPointGraph.h"
00008 #include "DlgEditPointGraphLineEdit.h"
00009 #include "DlgValidatorAbstract.h"
00010 #include "DlgValidatorFactory.h"
00011 #include "DocumentModelCoords.h"
00012 #include "DocumentModelGeneral.h"
00013 #include "FormatCoordsUnits.h"
00014 #include "Logger.h"
00015 #include "MainWindow.h"
00016 #include "MainWindowModel.h"
00017 #include <QGroupBox>
00018 #include <QLabel>
00019 #include <QPushButton>
00020 #include "QtToString.h"
00021 #include <QVBoxLayout>
00022 #include "Transformation.h"
00023
00024 const Qt::Alignment ALIGNMENT = Qt::AlignCenter;
00025
00026 const int MIN_WIDTH_TO_FIT_STRANGE_UNITS = 200;
00027
00028 DlgEditPointGraph::DlgEditPointGraph (MainWindow &mainWindow,
00029 const DocumentModelCoords &modelCoords,
00030 const DocumentModelGeneral &modelGeneral,
00031 const MainWindowModel &modelMainWindow,
00032 const Transformation &transformation,
00033 const double *xInitialValue,
00034 const double *yInitialValue) :
00035 QDialog (&mainWindow),
00036 m_changed (false),
00037 m_modelCoords (modelCoords),
00038 m_modelGeneral (modelGeneral),
00039 m_modelMainWindow (modelMainWindow)
00040 {
00041 LOG4CPP_INFO_S ((*mainCat)) << "DlgEditPointGraph::DlgEditPointGraph";
00042
00043 QVBoxLayout *layout = new QVBoxLayout;
00044 setLayout (layout);
00045
00046 setCursor (QCursor (Qt::ArrowCursor));
00047 setModal(true);
00048 setWindowTitle (tr ("Edit Curve Point(s)"));
00049
00050 createCoords (layout);
00051 createHint (layout);
00052 createOkCancel (layout);
00053
00054 initializeGraphCoordinates (xInitialValue,
00055 yInitialValue,
00056 transformation);
00057
00058 m_changed = false;
00059 updateControls ();
00060 }
00061
00062 DlgEditPointGraph::~DlgEditPointGraph()
00063 {
00064 LOG4CPP_INFO_S ((*mainCat)) << "DlgEditPointGraph::~DlgEditPointGraph";
00065 }
00066
00067 void DlgEditPointGraph::createCoords (QVBoxLayout *layoutOuter)
00068 {
00069
00070 bool isConstraintX = (m_modelCoords.coordScaleXTheta() == COORD_SCALE_LOG);
00071 bool isConstraintY = (m_modelCoords.coordScaleYRadius() == COORD_SCALE_LOG);
00072 DlgValidatorFactory dlgValidatorFactory;
00073 m_validatorGraphX = dlgValidatorFactory.createCartesianOrPolarWithPolarPolar (m_modelCoords.coordScaleXTheta(),
00074 isCartesian (),
00075 m_modelCoords.coordUnitsX(),
00076 m_modelCoords.coordUnitsTheta(),
00077 m_modelCoords.coordUnitsDate(),
00078 m_modelCoords.coordUnitsTime(),
00079 m_modelMainWindow.locale());
00080 m_validatorGraphY = dlgValidatorFactory.createCartesianOrPolarWithNonPolarPolar (m_modelCoords.coordScaleYRadius(),
00081 isCartesian (),
00082 m_modelCoords.coordUnitsY(),
00083 m_modelCoords.coordUnitsRadius(),
00084 m_modelCoords.coordUnitsDate(),
00085 m_modelCoords.coordUnitsTime(),
00086 m_modelMainWindow.locale());
00087
00088
00089 QString description = QString ("%1 (%2, %3)%4%5%6%7%8%9 %10:")
00090 .arg (tr ("Graph Coordinates"))
00091 .arg (nameXTheta ())
00092 .arg (nameYRadius ())
00093 .arg (isConstraintX || isConstraintY ? " with " : "")
00094 .arg (isConstraintX ? QString (nameXTheta ()) : "")
00095 .arg (isConstraintX ? " > 0" : "")
00096 .arg (isConstraintX && isConstraintY ? " and " : "")
00097 .arg ( isConstraintY ? QString (nameYRadius ()) : "")
00098 .arg ( isConstraintY ? " > 0" : "")
00099 .arg (tr ("as"));
00100 QGroupBox *panel = new QGroupBox (description, this);
00101 layoutOuter->addWidget (panel);
00102
00103 QHBoxLayout *layout = new QHBoxLayout (panel);
00104 panel->setLayout (layout);
00105
00106
00107 QLabel *labelGraphParLeft = new QLabel (tr ("("), this);
00108 layout->addWidget(labelGraphParLeft, 0);
00109
00110 m_editGraphX = new DlgEditPointGraphLineEdit;
00111 m_editGraphX->setMinimumWidth(MIN_WIDTH_TO_FIT_STRANGE_UNITS);
00112 m_editGraphX->setAlignment (ALIGNMENT);
00113 m_editGraphX->setValidator (m_validatorGraphX);
00114
00115 m_editGraphX->setWhatsThis (tr ("Enter the first graph coordinate value to be applied to the graph points.\n\n"
00116 "Leave this field empty if no value is to be applied to the graph points.\n\n"
00117 "For cartesian plots this is the X coordinate. For polar plots this is the radius R.\n\n"
00118 "The expected format of the coordinate value is determined by the locale setting. If "
00119 "typed values are not recognized as expected, check the locale setting in Settings / Main Window..."));
00120 layout->addWidget(m_editGraphX, 0);
00121 connect (m_editGraphX, SIGNAL (textChanged (const QString &)), this, SLOT (slotTextChanged (const QString &)));
00122
00123 QLabel *labelGraphComma = new QLabel (tr (", "), this);
00124 layout->addWidget(labelGraphComma, 0);
00125
00126 m_editGraphY = new DlgEditPointGraphLineEdit;
00127 m_editGraphY->setMinimumWidth(MIN_WIDTH_TO_FIT_STRANGE_UNITS);
00128 m_editGraphY->setAlignment (ALIGNMENT);
00129 m_editGraphY->setValidator (m_validatorGraphY);
00130
00131 m_editGraphY->setWhatsThis (tr ("Enter the second graph coordinate value to be applied to the graph points.\n\n"
00132 "Leave this field empty if no value is to be applied to the graph points.\n\n"
00133 "For cartesian plots this is the Y coordinate. For plot plots this is the angle Theta.\n\n"
00134 "The expected format of the coordinate value is determined by the locale setting. If "
00135 "typed values are not recognized as expected, check the locale setting in Settings / Main Window..."));
00136 layout->addWidget(m_editGraphY, 0);
00137 connect (m_editGraphY, SIGNAL (textChanged (const QString &)), this, SLOT (slotTextChanged (const QString &)));
00138
00139 QLabel *labelGraphParRight = new QLabel (tr (")"), this);
00140 layout->addWidget(labelGraphParRight, 0);
00141 }
00142
00143 void DlgEditPointGraph::createHint (QVBoxLayout *layoutOuter)
00144 {
00145
00146
00147
00148 QWidget *widget = new QWidget;
00149 layoutOuter->addWidget (widget, 0, Qt::AlignCenter);
00150
00151 QHBoxLayout *layout = new QHBoxLayout;
00152 widget->setLayout (layout);
00153
00154 QString locale = QLocaleToString (m_modelMainWindow.locale ());
00155 QString hint = QString ("%1: %2")
00156 .arg (tr ("Number format"))
00157 .arg (locale);
00158 QLabel *label = new QLabel (hint);
00159 layout->addWidget (label);
00160 }
00161
00162 void DlgEditPointGraph::createOkCancel (QVBoxLayout *layoutOuter)
00163 {
00164 QWidget *panel = new QWidget (this);
00165 layoutOuter->addWidget (panel, 0, Qt::AlignCenter);
00166
00167 QHBoxLayout *layout = new QHBoxLayout (panel);
00168 panel->setLayout (layout);
00169
00170 m_btnOk = new QPushButton (tr ("Ok"), this);
00171 layout->addWidget(m_btnOk);
00172 connect (m_btnOk, SIGNAL (released ()), this, SLOT (accept ()));
00173
00174 m_btnCancel = new QPushButton (tr ("Cancel"), this);
00175 layout->addWidget(m_btnCancel);
00176 connect (m_btnCancel, SIGNAL (released ()), this, SLOT (reject ()));
00177 }
00178
00179 void DlgEditPointGraph::initializeGraphCoordinates (const double *xInitialValue,
00180 const double *yInitialValue,
00181 const Transformation &transformation)
00182 {
00183 LOG4CPP_INFO_S ((*mainCat)) << "DlgEditPointGraph::initializeGraphCoordinates";
00184
00185 QString xTheta, yRadius;
00186 if ((xInitialValue != 0) &&
00187 (yInitialValue != 0)) {
00188
00189 FormatCoordsUnits format;
00190 format.unformattedToFormatted (*xInitialValue,
00191 *yInitialValue,
00192 m_modelCoords,
00193 m_modelGeneral,
00194 m_modelMainWindow,
00195 xTheta,
00196 yRadius,
00197 transformation);
00198 }
00199
00200 m_editGraphX->setText (xTheta);
00201 m_editGraphY->setText (yRadius);
00202 }
00203
00204 bool DlgEditPointGraph::isCartesian () const
00205 {
00206 return (m_modelCoords.coordsType() == COORDS_TYPE_CARTESIAN);
00207 }
00208
00209 QChar DlgEditPointGraph::nameXTheta () const
00210 {
00211 return (isCartesian () ? QChar ('X') : THETA);
00212 }
00213
00214 QChar DlgEditPointGraph::nameYRadius () const
00215 {
00216 return (isCartesian () ? QChar ('Y') : QChar ('R'));
00217 }
00218
00219 void DlgEditPointGraph::posGraph (bool &isX,
00220 double &x,
00221 bool &isY,
00222 double &y) const
00223 {
00224 FormatCoordsUnits format;
00225
00226
00227 QString xTextNotEmpty = QString ("%1").arg (m_editGraphX->text().isEmpty () ? "0" : m_editGraphX->text());
00228 QString yTextNotEmpty = QString ("%1").arg (m_editGraphY->text().isEmpty () ? "0" : m_editGraphY->text());
00229
00230 format.formattedToUnformatted (xTextNotEmpty,
00231 yTextNotEmpty,
00232 m_modelCoords,
00233 m_modelMainWindow,
00234 x,
00235 y);
00236
00237 isX = !m_editGraphX->text().isEmpty();
00238 isY = !m_editGraphY->text().isEmpty();
00239 }
00240
00241 void DlgEditPointGraph::slotTextChanged (const QString &)
00242 {
00243 m_changed = true;
00244 updateControls ();
00245 }
00246
00247 QString DlgEditPointGraph::unitsType (bool isXTheta) const
00248 {
00249 if (isCartesian ()) {
00250 if (isXTheta) {
00251 return coordUnitsNonPolarThetaToBriefType (m_modelCoords.coordUnitsX());
00252 } else {
00253 return coordUnitsNonPolarThetaToBriefType (m_modelCoords.coordUnitsY());
00254 }
00255 } else {
00256 if (isXTheta) {
00257 return coordUnitsPolarThetaToBriefType (m_modelCoords.coordUnitsTheta());
00258 } else {
00259 return coordUnitsNonPolarThetaToBriefType (m_modelCoords.coordUnitsRadius());
00260 }
00261 }
00262 }
00263
00264 void DlgEditPointGraph::updateControls ()
00265 {
00266 QString textX = m_editGraphX->text();
00267 QString textY = m_editGraphY->text();
00268
00269
00270 m_editGraphX->updateBackground ();
00271 m_editGraphY->updateBackground ();
00272
00273
00274
00275
00276
00277
00278
00279 bool test2 = (!textX.isEmpty() || !textY.isEmpty());
00280
00281 int posX, posY;
00282 bool test3 = true;
00283 if (!textX.isEmpty()) {
00284 test3 &= (m_validatorGraphX->validate(textX, posX) == QValidator::Acceptable);
00285 }
00286 if (!textY.isEmpty()) {
00287 test3 &= (m_validatorGraphY->validate(textY, posY) == QValidator::Acceptable);
00288 }
00289
00290 m_btnOk->setEnabled (m_changed && test2 && test3);
00291 }