File indexing completed on 2024-04-21 14:47:02

0001 /*
0002     SPDX-FileCopyrightText: 2001-2002 Pablo de Vicente <vicente@oan.es>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "dmsbox.h"
0008 
0009 #include <QApplication>
0010 #include <QFocusEvent>
0011 #include <QRegExp>
0012 
0013 #include <KLocalizedString>
0014 
0015 #include <QDebug>
0016 
0017 #include <cstdlib>
0018 
0019 dmsBox::dmsBox(QWidget *parent, Unit unit) : QLineEdit(parent), m_unit(unit)
0020 {
0021     setMaxLength(14);
0022     setMaximumWidth(160);
0023     setUnits(unit);
0024 }
0025 
0026 void dmsBox::setPlaceholderText()
0027 {
0028     if (m_unit == DEGREES)
0029         QLineEdit::setPlaceholderText("dd mm ss.s");
0030     else
0031         QLineEdit::setPlaceholderText("hh mm ss.s");
0032 }
0033 
0034 void dmsBox::setUnits(Unit unit)
0035 {
0036     m_unit = unit;
0037     const bool t = (m_unit == Unit::DEGREES);
0038 
0039     QString sTip = (t ? i18n("Angle value in degrees.") : i18n("Angle value in hours."));
0040     QString sWhatsThis;
0041 
0042     if (isReadOnly())
0043     {
0044         if (t)
0045         {
0046             sWhatsThis = i18n("This box displays an angle in degrees. "
0047                               "The three numbers displayed are the angle's "
0048                               "degrees, arcminutes, and arcseconds.");
0049         }
0050         else
0051         {
0052             sWhatsThis = i18n("This box displays an angle in hours. "
0053                               "The three numbers displayed are the angle's "
0054                               "hours, minutes, and seconds.");
0055         }
0056     }
0057     else
0058     {
0059         if (t)
0060         {
0061             sTip += i18n("  You may enter a simple integer, or a floating-point value, "
0062                          "or space- or colon-delimited values specifying "
0063                          "degrees, arcminutes and arcseconds");
0064 
0065             sWhatsThis = i18n("Enter an angle value in degrees.  The angle can be expressed "
0066                               "as a simple integer (\"12\"), a floating-point value "
0067                               "(\"12.33\"), or as space- or colon-delimited "
0068                               "values specifying degrees, arcminutes and arcseconds (\"12:20\", \"12:20:00\", "
0069                               "\"12 20\", \"12 20 00.0\", etc.).");
0070         }
0071         else
0072         {
0073             sTip += i18n("  You may enter a simple integer, or a floating-point value, "
0074                          "or space- or colon-delimited values specifying "
0075                          "hours, minutes and seconds");
0076 
0077             sWhatsThis = i18n("Enter an angle value in hours.  The angle can be expressed "
0078                               "as a simple integer (\"12\"), a floating-point value "
0079                               "(\"12.33\"), or as space- or colon-delimited "
0080                               "values specifying hours, minutes and seconds (\"12:20\", \"12:20:00\", "
0081                               "\"12 20\", \"12 20 00.0\", etc.).");
0082         }
0083     }
0084 
0085     setToolTip(sTip);
0086     setWhatsThis(sWhatsThis);
0087     setPlaceholderText();
0088 
0089     clear();
0090 }
0091 
0092 void dmsBox::show(const dms &d)
0093 {
0094     if (m_unit == Unit::DEGREES)
0095     {
0096         double seconds = d.arcsec() + d.marcsec() / 1000.;
0097         setText(QString::asprintf("%02d %02d %05.2f", d.degree(), d.arcmin(), seconds));
0098     }
0099     else if (m_unit == Unit::HOURS)
0100     {
0101         double seconds = d.second() + d.msecond() / 1000.;
0102         setText(QString::asprintf("%02d %02d %05.2f", d.hour(), d.minute(), seconds));
0103     }
0104     else
0105     {
0106         Q_ASSERT(false); // Unhandled unit type
0107     }
0108 }
0109 
0110 dms dmsBox::createDms(bool *ok)
0111 {
0112     dms dmsAngle;
0113     bool check;
0114     check = dmsAngle.setFromString(text(), (m_unit == Unit::DEGREES));
0115     if (ok)
0116         *ok = check; //ok might be a null pointer!
0117 
0118     return dmsAngle;
0119 }