File indexing completed on 2024-05-12 15:28:18

0001 /***************************************************************************
0002 File                 : FITSHeaderEditNewKeywordDialog.cpp
0003 Project              : LabPlot
0004 Description          : Widget for adding new keyword in the FITS edit widget
0005 --------------------------------------------------------------------
0006 Copyright            : (C) 2016-2017 by Fabian Kristof (fkristofszabolcs@gmail.com)
0007 Copyright            : (C) 2016-2019 by Alexander Semke (alexander.semke@web.de)
0008 ***************************************************************************/
0009 
0010 /***************************************************************************
0011 *                                                                         *
0012 *  This program is free software; you can redistribute it and/or modify   *
0013 *  it under the terms of the GNU General Public License as published by   *
0014 *  the Free Software Foundation; either version 2 of the License, or      *
0015 *  (at your option) any later version.                                    *
0016 *                                                                         *
0017 *  This program is distributed in the hope that it will be useful,        *
0018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020 *  GNU General Public License for more details.                           *
0021 *                                                                         *
0022 *   You should have received a copy of the GNU General Public License     *
0023 *   along with this program; if not, write to the Free Software           *
0024 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025 *   Boston, MA  02110-1301  USA                                           *
0026 *                                                                         *
0027 ***************************************************************************/
0028 #include "FITSHeaderEditNewKeywordDialog.h"
0029 
0030 #include <QCompleter>
0031 #include <QDialog>
0032 #include <QDialogButtonBox>
0033 #include <QPushButton>
0034 #include <QWindow>
0035 
0036 #include <KMessageBox>
0037 #include <KSharedConfig>
0038 #include <KWindowConfig>
0039 
0040 #define FLEN_KEYWORD   75  /* max length of a keyword (HIERARCH convention) */
0041 #define FLEN_VALUE     71  /* max length of a keyword value string */
0042 #define FLEN_COMMENT   73  /* max length of a keyword comment string */
0043 
0044 /*! \class FITSHeaderEditNewKeywordDialog
0045  * \brief Dialog class for adding new keywords to the FITSHeaderEditDialog's table.
0046  * \since 2.4.0
0047  * \ingroup widgets
0048  */
0049 FITSHeaderEditNewKeywordDialog::FITSHeaderEditNewKeywordDialog(QWidget *parent) : QDialog(parent) {
0050     ui.setupUi(this);
0051 
0052     auto* btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0053 
0054     ui.gridLayout->addWidget(btnBox, 3, 1, 1, 2);
0055     m_okButton = btnBox->button(QDialogButtonBox::Ok);
0056     m_cancelButton = btnBox->button(QDialogButtonBox::Cancel);
0057 
0058     m_okButton->setText(i18n("&Add Keyword"));
0059 
0060     connect(btnBox, &QDialogButtonBox::clicked, this, &FITSHeaderEditNewKeywordDialog::slotButtonClicked);
0061 
0062     setWindowTitle(i18nc("@title:window", "Specify the New Keyword"));
0063     setWindowIcon(QIcon::fromTheme("document-new"));
0064 
0065     QCompleter* keyCompleter = new QCompleter(FITSFilter::standardKeywords(), this);
0066     keyCompleter->setCaseSensitivity(Qt::CaseInsensitive);
0067     ui.leKey->setCompleter(keyCompleter);
0068 
0069     ui.leKey->setPlaceholderText(i18n("Specify the name"));
0070     ui.leValue->setPlaceholderText(i18n("Specify the value"));
0071     ui.leComment->setPlaceholderText(i18n("Specify the comment"));
0072 
0073     ui.leKey->setMaxLength(FLEN_KEYWORD);
0074     ui.leValue->setMaxLength(FLEN_VALUE);
0075     ui.leComment->setMaxLength(FLEN_COMMENT);
0076 
0077 
0078     //restore saved settings if available
0079     create(); // ensure there's a window created
0080     KConfigGroup conf(KSharedConfig::openConfig(), "FITSHeaderEditNewKeywordDialog");
0081     if (conf.exists()) {
0082         KWindowConfig::restoreWindowSize(windowHandle(), conf);
0083         resize(windowHandle()->size()); // workaround for QTBUG-40584
0084     } else
0085         resize(QSize(300, 0).expandedTo(minimumSize()));
0086 }
0087 
0088 FITSHeaderEditNewKeywordDialog::~FITSHeaderEditNewKeywordDialog() {
0089     KConfigGroup conf(KSharedConfig::openConfig(), "FITSHeaderEditNewKeywordDialog");
0090     KWindowConfig::saveWindowSize(windowHandle(), conf);
0091 }
0092 
0093 /*!
0094  * \brief Decides whether the keyword can be used, messagebox pops up if the keywords key is empty.
0095  * \return Whether the keyword was "Ok" or not.
0096  */
0097 int FITSHeaderEditNewKeywordDialog::okClicked() {
0098     if (!ui.leKey->text().isEmpty()) {
0099         m_newKeyword = FITSFilter::Keyword(ui.leKey->text(), ui.leValue->text(), ui.leComment->text());
0100         return QMessageBox::Ok;
0101     } else {
0102         const int yesNo = KMessageBox::warningYesNo(this, i18n("Cannot add new keyword without key, would you like to try again?"),
0103                           i18n("Cannot add empty key"));
0104         if (yesNo == KMessageBox::No)
0105             return QMessageBox::Cancel;
0106         return yesNo;
0107     }
0108 }
0109 
0110 /*!
0111  * \brief Returns the new keyword.
0112  * \return The newly constructed keyword from the line edits.
0113  */
0114 FITSFilter::Keyword FITSHeaderEditNewKeywordDialog::newKeyword() const {
0115     return m_newKeyword;
0116 }
0117 
0118 /*!
0119  * \brief Decides whether the dialog should move in an accepted state or canceled.
0120  * \param button the button clicked
0121  */
0122 void FITSHeaderEditNewKeywordDialog::slotButtonClicked(QAbstractButton* button) {
0123     if (button == m_okButton) {
0124         int okClickedBtn = okClicked();
0125         if (okClickedBtn == QMessageBox::Ok)
0126             accept();
0127         else if (okClickedBtn == QMessageBox::Cancel)
0128             reject();
0129     } else if (button == m_cancelButton)
0130         reject();
0131 }