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

0001 /***************************************************************************
0002 File                 : FITSHeaderEditDialog.h
0003 Project              : LabPlot
0004 Description          : Dialog for listing/editing FITS header keywords
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 
0029 #include "FITSHeaderEditDialog.h"
0030 
0031 
0032 #include <QDialogButtonBox>
0033 #include <QPushButton>
0034 #include <QWindow>
0035 #include <QVBoxLayout>
0036 
0037 #include <KSharedConfig>
0038 #include <KWindowConfig>
0039 
0040 /*! \class FITSHeaderEditDialog
0041  * \brief Dialog class for editing FITS header units.
0042  * \since 2.4.0
0043  * \ingroup widgets
0044  */
0045 FITSHeaderEditDialog::FITSHeaderEditDialog(QWidget* parent) : QDialog(parent),
0046     m_headerEditWidget(new FITSHeaderEditWidget(this)) {
0047 
0048     auto* btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0049     auto* layout = new QVBoxLayout;
0050 
0051     layout->addWidget(m_headerEditWidget);
0052     layout->addWidget(btnBox);
0053 
0054     setLayout(layout);
0055 
0056     m_okButton = btnBox->button(QDialogButtonBox::Ok);
0057     m_okButton->setText(i18n("&Save"));
0058     m_okButton->setEnabled(false);
0059 
0060     connect(btnBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &FITSHeaderEditDialog::reject);
0061     connect(btnBox, &QDialogButtonBox::accepted, this, &FITSHeaderEditDialog::accept);
0062     connect(btnBox, &QDialogButtonBox::rejected, this, &FITSHeaderEditDialog::reject);
0063 
0064     setWindowTitle(i18nc("@title:window", "FITS Metadata Editor"));
0065     setWindowIcon(QIcon::fromTheme("document-edit"));
0066 
0067     connect(m_okButton, &QPushButton::clicked, this, &FITSHeaderEditDialog::save);
0068     connect(m_headerEditWidget, &FITSHeaderEditWidget::changed, this, &FITSHeaderEditDialog::headersChanged);
0069 
0070     setAttribute(Qt::WA_DeleteOnClose);
0071 
0072     //restore saved settings if available
0073     create(); // ensure there's a window created
0074     KConfigGroup conf(KSharedConfig::openConfig(), "FITSHeaderEditDialog");
0075     if (conf.exists()) {
0076         KWindowConfig::restoreWindowSize(windowHandle(), conf);
0077         resize(windowHandle()->size()); // workaround for QTBUG-40584
0078     } else
0079         resize(QSize(300, 0).expandedTo(minimumSize()));
0080 }
0081 
0082 /*!
0083  * \brief FITSHeaderEditDialog::~FITSHeaderEditDialog
0084  */
0085 FITSHeaderEditDialog::~FITSHeaderEditDialog() {
0086     KConfigGroup conf(KSharedConfig::openConfig(), "FITSHeaderEditDialog");
0087     KWindowConfig::saveWindowSize(windowHandle(), conf);
0088     delete m_headerEditWidget;
0089 }
0090 
0091 void FITSHeaderEditDialog::headersChanged(bool changed) {
0092     if (changed) {
0093         setWindowTitle(i18nc("@title:window", "FITS Metadata Editor  [Changed]"));
0094         m_okButton->setEnabled(true);
0095     } else {
0096         setWindowTitle(i18nc("@title:window", "FITS Metadata Editor"));
0097         m_okButton->setEnabled(false);
0098     }
0099 }
0100 
0101 /*!
0102  * \brief This slot is triggered when the Save button was clicked in the ui.
0103  */
0104 void FITSHeaderEditDialog::save() {
0105     m_saved = m_headerEditWidget->save();
0106 }
0107 
0108 /*!
0109  * \brief Returns whether there were changes saved.
0110  * \return
0111  */
0112 bool FITSHeaderEditDialog::saved() const {
0113     return m_saved;
0114 }