File indexing completed on 2024-04-21 05:43:42

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2004 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "outputmethoddlg.h"
0012 #include "docmanager.h"
0013 #include "filemetainfo.h"
0014 #include "microlibrary.h"
0015 #include "microselectwidget.h"
0016 #include "projectmanager.h"
0017 #include "textdocument.h"
0018 #include "filefilters.h"
0019 
0020 #include <KUrlRequester>
0021 #include <kio_version.h>
0022 
0023 #include <QCheckBox>
0024 #include <QDialogButtonBox>
0025 #include <QFile>
0026 #include <QPushButton>
0027 #include <QTemporaryFile>
0028 #include <QVBoxLayout>
0029 
0030 #include <ui_outputmethodwidget.h>
0031 #include <ktechlab_debug.h>
0032 
0033 class OutputMethodWidget : public QWidget, public Ui::OutputMethodWidget
0034 {
0035 public:
0036     OutputMethodWidget(QWidget *parent)
0037         : QWidget(parent)
0038     {
0039         setupUi(this);
0040     }
0041 };
0042 
0043 // BEGIN class OutputMethodInfo
0044 OutputMethodInfo::OutputMethodInfo()
0045 {
0046     m_method = Method::Direct;
0047     m_bAddToProject = false;
0048 }
0049 
0050 void OutputMethodInfo::initialize(OutputMethodDlg *dlg)
0051 {
0052     if (dlg->m_widget->displayDirectCheck->isChecked()) {
0053         m_method = Method::Direct;
0054         // K3TempFile f( QString(), dlg->m_outputExtension );
0055         QTemporaryFile f(QDir::tempPath() + QLatin1String("/ktechlab_XXXXXX") + dlg->m_outputExtension);
0056         f.setAutoRemove(false);
0057         if (!f.open()) {
0058             qCWarning(KTL_LOG) << "failed to open " << f.fileName() << " because " << f.errorString();
0059         }
0060         f.close();
0061         m_outputFile = QUrl::fromLocalFile(f.fileName());
0062         m_bAddToProject = false;
0063     }
0064 
0065     else {
0066         if (dlg->m_widget->loadFileCheck->isChecked())
0067             m_method = Method::SaveAndLoad;
0068 
0069         else
0070             m_method = Method::SaveAndForget;
0071 
0072         m_outputFile = dlg->m_widget->outputFileURL->url();
0073         m_bAddToProject = dlg->m_widget->addToProjectCheck->isChecked();
0074     }
0075 
0076     m_picID = dlg->m_widget->m_pMicroSelect->micro();
0077 }
0078 // END class OutputMethodInfo
0079 
0080 // BEGIN class OutputMethodDlg
0081 
0082 OutputMethodDlg::OutputMethodDlg(const QString &caption, const QUrl &inputURL, bool showPICSelect, QWidget *parent)
0083     : QDialog(parent)
0084 {
0085     setModal(true);
0086     setWindowTitle(caption);
0087 
0088     QVBoxLayout *mainLayout = new QVBoxLayout;
0089     setLayout(mainLayout);
0090 
0091     m_inputURL = inputURL;
0092     m_widget = new OutputMethodWidget(this);
0093 
0094     m_widget->addToProjectCheck->setEnabled(ProjectManager::self()->currentProject());
0095 
0096     if (!showPICSelect) {
0097         m_widget->m_pMicroSelect->hide();
0098         m_widget->adjustSize();
0099     }
0100 
0101     m_widget->outputFileURL->setMode(KFile::File | KFile::LocalOnly);
0102     m_widget->outputFileURL->setAcceptMode(QFileDialog::AcceptSave);
0103 
0104     connect(m_widget->saveFileCheck, &QRadioButton::toggled, m_widget->groupBoxSaveOptions, &QGroupBox::setEnabled);
0105 
0106     fileMetaInfo()->initializeFromMetaInfo(m_inputURL, this);
0107 
0108     mainLayout->addWidget(m_widget);
0109 
0110     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0111     mainLayout->addWidget(buttonBox);
0112 
0113     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0114     okButton->setDefault(true);
0115     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0116     connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0117     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0118 }
0119 
0120 OutputMethodDlg::~OutputMethodDlg()
0121 {
0122 }
0123 
0124 void OutputMethodDlg::setOutputExtension(const QString &extension)
0125 {
0126     m_outputExtension = extension;
0127 }
0128 
0129 void OutputMethodDlg::setFileFilters(const FileFilters &filters)
0130 {
0131 #if KIO_VERSION >= QT_VERSION_CHECK(5, 108, 0)
0132     m_widget->outputFileURL->setNameFilters(filters.toQtStyleStringList());
0133 #else
0134     m_widget->outputFileURL->setFilter(filters.toKDEStyleString());
0135 #endif
0136 }
0137 
0138 void OutputMethodDlg::setMethod(OutputMethodInfo::Method::Type m)
0139 {
0140     switch (m) {
0141     case OutputMethodInfo::Method::Direct:
0142         m_widget->displayDirectCheck->setChecked(true);
0143         break;
0144 
0145     case OutputMethodInfo::Method::SaveAndForget:
0146         m_widget->saveFileCheck->setChecked(true);
0147         m_widget->loadFileCheck->setChecked(false);
0148         break;
0149 
0150     case OutputMethodInfo::Method::SaveAndLoad:
0151         m_widget->saveFileCheck->setChecked(true);
0152         m_widget->loadFileCheck->setChecked(true);
0153         break;
0154     };
0155 }
0156 
0157 void OutputMethodDlg::setPicID(const QString &id)
0158 {
0159     m_widget->m_pMicroSelect->setMicro(id);
0160 }
0161 
0162 void OutputMethodDlg::setOutputFile(const QUrl &out)
0163 {
0164     m_widget->outputFileURL->setUrl(out);
0165 }
0166 
0167 void OutputMethodDlg::accept()
0168 {
0169     m_outputMethodInfo.initialize(this);
0170     fileMetaInfo()->grabMetaInfo(m_inputURL, this);
0171 
0172     QDialog::accept();
0173 }
0174 
0175 MicroSelectWidget *OutputMethodDlg::microSelect() const
0176 {
0177     return m_widget->m_pMicroSelect;
0178 }
0179 // END class OutputMethodDlg
0180 
0181 #include "moc_outputmethoddlg.cpp"