File indexing completed on 2025-10-12 03:31:32

0001 /*
0002     File                 : FITSHeaderEditNewKeywordDialog.cpp
0003     Project              : LabPlot
0004     Description          : Widget for adding new keyword in the FITS edit widget
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2016-2017 Fabian Kristof <fkristofszabolcs@gmail.com>
0007     SPDX-FileCopyrightText: 2016-2019 Alexander Semke <alexander.semke@web.de>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 #include "FITSHeaderEditNewKeywordDialog.h"
0011 #include "backend/core/Settings.h"
0012 
0013 #include <KMessageBox>
0014 
0015 #include <KWindowConfig>
0016 #include <kcoreaddons_version.h>
0017 
0018 #include <QCompleter>
0019 #include <QDialog>
0020 #include <QDialogButtonBox>
0021 #include <QPushButton>
0022 #include <QWindow>
0023 
0024 #define FLEN_KEYWORD 75 /* max length of a keyword (HIERARCH convention) */
0025 #define FLEN_VALUE 71 /* max length of a keyword value string */
0026 #define FLEN_COMMENT 73 /* max length of a keyword comment string */
0027 
0028 /*! \class FITSHeaderEditNewKeywordDialog
0029  * \brief Dialog class for adding new keywords to the FITSHeaderEditDialog's table.
0030  * \since 2.4.0
0031  * \ingroup widgets
0032  */
0033 FITSHeaderEditNewKeywordDialog::FITSHeaderEditNewKeywordDialog(QWidget* parent)
0034     : QDialog(parent) {
0035     ui.setupUi(this);
0036 
0037     auto* btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0038 
0039     ui.gridLayout->addWidget(btnBox, 3, 1, 1, 2);
0040     m_okButton = btnBox->button(QDialogButtonBox::Ok);
0041     m_cancelButton = btnBox->button(QDialogButtonBox::Cancel);
0042 
0043     m_okButton->setText(i18n("&Add Keyword"));
0044 
0045     connect(btnBox, &QDialogButtonBox::clicked, this, &FITSHeaderEditNewKeywordDialog::slotButtonClicked);
0046 
0047     setWindowTitle(i18nc("@title:window", "Specify the New Keyword"));
0048     setWindowIcon(QIcon::fromTheme(QStringLiteral("document-new")));
0049 
0050     auto* keyCompleter = new QCompleter(FITSFilter::standardKeywords(), this);
0051     keyCompleter->setCaseSensitivity(Qt::CaseInsensitive);
0052     ui.leKey->setCompleter(keyCompleter);
0053 
0054     ui.leKey->setPlaceholderText(i18n("Specify the name"));
0055     ui.leValue->setPlaceholderText(i18n("Specify the value"));
0056     ui.leComment->setPlaceholderText(i18n("Specify the comment"));
0057 
0058     ui.leKey->setMaxLength(FLEN_KEYWORD);
0059     ui.leValue->setMaxLength(FLEN_VALUE);
0060     ui.leComment->setMaxLength(FLEN_COMMENT);
0061 
0062     // restore saved settings if available
0063     create(); // ensure there's a window created
0064     KConfigGroup conf = Settings::group(QStringLiteral("FITSHeaderEditNewKeywordDialog"));
0065     if (conf.exists()) {
0066         KWindowConfig::restoreWindowSize(windowHandle(), conf);
0067         resize(windowHandle()->size()); // workaround for QTBUG-40584
0068     } else
0069         resize(QSize(300, 0).expandedTo(minimumSize()));
0070 }
0071 
0072 FITSHeaderEditNewKeywordDialog::~FITSHeaderEditNewKeywordDialog() {
0073     KConfigGroup conf = Settings::group(QStringLiteral("FITSHeaderEditNewKeywordDialog"));
0074     KWindowConfig::saveWindowSize(windowHandle(), conf);
0075 }
0076 
0077 /*!
0078  * \brief Decides whether the keyword can be used, messagebox pops up if the keywords key is empty.
0079  * \return Whether the keyword was "Ok" or not.
0080  */
0081 int FITSHeaderEditNewKeywordDialog::okClicked() {
0082     if (!ui.leKey->text().isEmpty()) {
0083         m_newKeyword = FITSFilter::Keyword(ui.leKey->text(), ui.leValue->text(), ui.leComment->text());
0084         return QMessageBox::Ok;
0085     } else {
0086 #if KCOREADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
0087         auto status = KMessageBox::warningTwoActions(this,
0088                                                      i18n("Cannot add new keyword without key, would you like to try again?"),
0089                                                      i18n("Cannot add empty key"),
0090                                                      KStandardGuiItem::ok(),
0091                                                      KStandardGuiItem::cancel());
0092         if (status == KMessageBox::SecondaryAction)
0093             return QMessageBox::Cancel;
0094 #else
0095         auto status = KMessageBox::warningYesNo(this, i18n("Cannot add new keyword without key, would you like to try again?"), i18n("Cannot add empty key"));
0096         if (status == KMessageBox::No)
0097             return QMessageBox::Cancel;
0098 #endif
0099         return status;
0100     }
0101 }
0102 
0103 /*!
0104  * \brief Returns the new keyword.
0105  * \return The newly constructed keyword from the line edits.
0106  */
0107 FITSFilter::Keyword FITSHeaderEditNewKeywordDialog::newKeyword() const {
0108     return m_newKeyword;
0109 }
0110 
0111 /*!
0112  * \brief Decides whether the dialog should move in an accepted state or canceled.
0113  * \param button the button clicked
0114  */
0115 void FITSHeaderEditNewKeywordDialog::slotButtonClicked(QAbstractButton* button) {
0116     if (button == m_okButton) {
0117         int okClickedBtn = okClicked();
0118         if (okClickedBtn == QMessageBox::Ok)
0119             accept();
0120         else if (okClickedBtn == QMessageBox::Cancel)
0121             reject();
0122     } else if (button == m_cancelButton)
0123         reject();
0124 }