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

0001 /***************************************************************************
0002 File                 : FITSHeaderEditAddUnitDialog.cpp
0003 Project              : LabPlot
0004 Description          : Widget for adding or modifying FITS header keyword units
0005 --------------------------------------------------------------------
0006 Copyright            : (C) 2016-2017 by Fabian Kristof (fkristofszabolcs@gmail.com)
0007 ***************************************************************************/
0008 
0009 /***************************************************************************
0010 *                                                                         *
0011 *  This program is free software; you can redistribute it and/or modify   *
0012 *  it under the terms of the GNU General Public License as published by   *
0013 *  the Free Software Foundation; either version 2 of the License, or      *
0014 *  (at your option) any later version.                                    *
0015 *                                                                         *
0016 *  This program is distributed in the hope that it will be useful,        *
0017 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0018 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0019 *  GNU General Public License for more details.                           *
0020 *                                                                         *
0021 *   You should have received a copy of the GNU General Public License     *
0022 *   along with this program; if not, write to the Free Software           *
0023 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0024 *   Boston, MA  02110-1301  USA                                           *
0025 *                                                                         *
0026 ***************************************************************************/
0027 #include "FITSHeaderEditAddUnitDialog.h"
0028 #include "backend/datasources/filters/FITSFilter.h"
0029 
0030 #include <QDialogButtonBox>
0031 #include <QCompleter>
0032 #include <QPushButton>
0033 #include <QWindow>
0034 #include <QVBoxLayout>
0035 
0036 #include <KSharedConfig>
0037 #include <KWindowConfig>
0038 
0039 FITSHeaderEditAddUnitDialog::FITSHeaderEditAddUnitDialog(const QString& unit, QWidget* parent) : QDialog(parent) {
0040     ui.setupUi(this);
0041     auto* btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0042 
0043     ui.gridLayout->addWidget(btnBox, 1, 0, 1, 2);
0044     m_okButton = btnBox->button(QDialogButtonBox::Ok);
0045     m_okButton->setText(i18n("&Add"));
0046 
0047     setWindowTitle(i18nc("@title:window", "Add New Unit"));
0048     setWindowIcon(QIcon::fromTheme("document-new"));
0049     m_okButton->setEnabled(false);
0050 
0051     QCompleter* keyCompleter = new QCompleter(FITSFilter::units(), this);
0052     ui.leUnit->setCompleter(keyCompleter);
0053     ui.leUnit->setPlaceholderText(i18n("Enter unit name here"));
0054 
0055     connect(ui.leUnit, &QLineEdit::textChanged, this, &FITSHeaderEditAddUnitDialog::unitChanged);
0056     connect(btnBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &FITSHeaderEditAddUnitDialog::close);
0057 
0058     connect(btnBox, &QDialogButtonBox::accepted, this, &FITSHeaderEditAddUnitDialog::accept);
0059     connect(btnBox, &QDialogButtonBox::rejected, this, &FITSHeaderEditAddUnitDialog::reject);
0060 
0061     ui.leUnit->setText(unit);
0062 
0063     //restore saved settings if available
0064     create(); // ensure there's a window created
0065     KConfigGroup conf(KSharedConfig::openConfig(), "FITSHeaderEditAddUnitDialog");
0066     if (conf.exists()) {
0067         KWindowConfig::restoreWindowSize(windowHandle(), conf);
0068         resize(windowHandle()->size()); // workaround for QTBUG-40584
0069     } else
0070         resize(QSize(300, 0).expandedTo(minimumSize()));
0071 }
0072 
0073 FITSHeaderEditAddUnitDialog::~FITSHeaderEditAddUnitDialog() {
0074     KConfigGroup conf(KSharedConfig::openConfig(), "FITSHeaderEditAddUnitDialog");
0075     KWindowConfig::saveWindowSize(windowHandle(), conf);
0076 }
0077 
0078 QString FITSHeaderEditAddUnitDialog::unit() const {
0079     QString unit = ui.leUnit->text();
0080     if (unit.contains(QLatin1Char('(')))
0081         unit = unit.left(unit.indexOf(QLatin1Char('('))-1);
0082 
0083     return unit;
0084 }
0085 
0086 void FITSHeaderEditAddUnitDialog::unitChanged() {
0087     m_okButton->setEnabled(!ui.leUnit->text().isEmpty());
0088 }