File indexing completed on 2025-03-16 10:52:55

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2002, 2003 Robert JACOLIN <rjacolin@ifrance.com>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005    This file use code from koTemplateOpenDia for the method chooseSlot.
0006 */
0007 
0008 #include "latexexportdialog.h"
0009 
0010 #include <QDir>
0011 #include <QApplication>
0012 #include <QDBusConnection>
0013 
0014 #include <KLocalizedString>
0015 #include <kconfig.h>
0016 #include <KoFilterManager.h>
0017 
0018 #include "LatexDebug.h"
0019 #include "latexexportAdaptor.h"
0020 
0021 #include "document.h"
0022 
0023 /*#ifdef __FreeBSD__
0024 #include <unistd.h>
0025 #endif*/
0026 
0027 /*
0028  *  Constructs a LatexExportDialog which is a child of 'parent', with the
0029  *  name 'name' and widget flags set to 'f'.
0030  *
0031  *  The dialog will by default be modeless, unless you set 'modal' to
0032  *  true to construct a modal dialog.
0033  */
0034 LatexExportDialog::LatexExportDialog(KoStore* inputStore, QWidget* parent)
0035   : KoDialog(parent)
0036   , m_inputStore(inputStore)
0037 {
0038     QWidget *mainWidget = new QWidget();
0039     m_ui.setupUi(mainWidget);
0040 
0041     int i = 0;
0042 
0043     QApplication::restoreOverrideCursor();
0044 
0045     /* Recent files */
0046     m_config = new KConfig("kspreadlatexexportdialog");
0047     //m_config->setGroup( "KSpread latex export filter" );
0048     //QString value;
0049     while (i < 10) {
0050         /*value = m_config->readPathEntry( QString("Recent%1").arg(i), QString() );
0051         debugLatex <<"recent :" << value;
0052         if(!value.isEmpty())
0053         {
0054          _recentList.append( value );
0055          recentBox->addItem(value);
0056         }
0057         else
0058          i = 10;*/
0059         i = i + 1;
0060     }
0061 
0062     new LatexExportAdaptor(this);
0063     QDBusConnection::sessionBus().registerObject("/filter/latex", this);
0064 
0065 
0066     /* All these items inserted must not be translated so they are inserted here
0067      * without i18n() method. */
0068     /*qualityComboBox->addItem("final");
0069      qualityComboBox->addItem("draft");*/
0070 
0071     KComboBox* classComboBox = m_ui.classComboBox;
0072     classComboBox->addItem("article");
0073     classComboBox->addItem("book");
0074     classComboBox->addItem("letter");
0075     classComboBox->addItem("report");
0076     classComboBox->addItem("slides");
0077 
0078     KComboBox* encodingComboBox = m_ui.encodingComboBox;
0079     encodingComboBox->addItem("unicode");
0080     encodingComboBox->addItem("ansinew");
0081     encodingComboBox->addItem("applemac");
0082     encodingComboBox->addItem("ascii");
0083     encodingComboBox->addItem("latin1");
0084     encodingComboBox->addItem("latin2");
0085     encodingComboBox->addItem("latin3");
0086     encodingComboBox->addItem("latin5");
0087     encodingComboBox->addItem("cp437");
0088     encodingComboBox->addItem("cp437de");
0089     encodingComboBox->addItem("cp850");
0090     encodingComboBox->addItem("cp852");
0091     encodingComboBox->addItem("cp865");
0092     encodingComboBox->addItem("cp1250");
0093     encodingComboBox->addItem("cp1252");
0094     encodingComboBox->addItem("decmulti");
0095     encodingComboBox->addItem("next");
0096 
0097     QListWidget* languagesList = m_ui.languagesList;
0098     languagesList->addItem("american");
0099     languagesList->addItem("austrian");
0100     languagesList->addItem("bahasa");
0101     languagesList->addItem("brazil");
0102     languagesList->addItem("breton");
0103     languagesList->addItem("catalan");
0104     languagesList->addItem("croatian");
0105     languagesList->addItem("czech");
0106     languagesList->addItem("danish");
0107     languagesList->addItem("dutch");
0108     languagesList->addItem("english");
0109     languagesList->addItem("esperanto");
0110     languagesList->addItem("finnish");
0111     languagesList->addItem("francais");
0112     languagesList->addItem("french");
0113     languagesList->addItem("galician");
0114     languagesList->addItem("german");
0115     languagesList->addItem("germanb");
0116     languagesList->addItem("hungarian");
0117     languagesList->addItem("magyar");
0118     languagesList->addItem("italian");
0119     languagesList->addItem("norsk");
0120     languagesList->addItem("nynorsk");
0121     languagesList->addItem("polish");
0122     languagesList->addItem("portuges");
0123     languagesList->addItem("romanian");
0124     languagesList->addItem("russian");
0125     languagesList->addItem("spanish");
0126     languagesList->addItem("slovak");
0127     languagesList->addItem("slovene");
0128     languagesList->addItem("swedish");
0129     languagesList->addItem("turkish");
0130 
0131     connect(m_ui.addLanguageBtn, &QAbstractButton::clicked, this, &LatexExportDialog::addLanguage);
0132     connect(m_ui.rmLanguageBtn, &QAbstractButton::clicked, this, &LatexExportDialog::removeLanguage);
0133 
0134     setMainWidget(mainWidget);
0135 
0136     setButtons(Ok | Cancel);
0137     setDefaultButton(Ok);
0138     setModal(true);
0139     setCaption(i18n("Latex Export Filter Configuration"));
0140 }
0141 
0142 /*
0143  *  Destroys the object and frees any allocated resources
0144  */
0145 LatexExportDialog::~LatexExportDialog()
0146 {
0147     delete m_config;
0148 }
0149 
0150 /**
0151  * Called when the cancel button is clicked.
0152  * Close the dialog box.
0153  */
0154 void LatexExportDialog::reject()
0155 {
0156     debugLatex << "Export cancelled";
0157     QDialog::reject();
0158 }
0159 
0160 /**
0161  * Called when the user clicks on the ok button. The xslt sheet is put on the recent list which is
0162  * saved, then export the document.
0163  */
0164 void LatexExportDialog::accept()
0165 {
0166     hide();
0167     debugLatex << "KSPREAD LATEX EXPORT FILTER --> BEGIN";
0168     Config* config = Config::instance();
0169 
0170     /* Document tab */
0171     config->setEmbeded(m_ui.embededButton->isChecked());
0172     if (m_ui.wordsStyleButton->isChecked())
0173         config->useWordsStyle();
0174     else
0175         config-> useLatexStyle();
0176     /* class names are not translated */
0177     config->setClass(m_ui.classComboBox->currentText());
0178 
0179     if (m_ui.qualityComboBox->currentIndex() == 0)
0180         config->setQuality("final");
0181     else
0182         config->setQuality("draft");
0183     config->setDefaultFontSize(m_ui.defaultFontSize->value());
0184 
0185     /* Pictures tab */
0186     if (m_ui.pictureCheckBox->isChecked())
0187         config->convertPictures();
0188     config->setPicturesDir(m_ui.pathPictures->url().path());
0189 
0190     /* Language tab */
0191     config->setEncoding(m_ui.encodingComboBox->currentText());
0192     QListWidget* langUsedList = m_ui.langUsedList;
0193     for (int index = 0; index < langUsedList->count(); ++index) {
0194         debugLatex << "lang. :" << langUsedList->item(index)->text();
0195         config->addLanguage(langUsedList->item(index)->text());
0196     }
0197 
0198     /* The default language is the first language in the list */
0199     if (langUsedList->item(0) != nullptr)
0200         config->setDefaultLanguage(langUsedList->item(0)->text());
0201     if (langUsedList->currentItem() != nullptr) {
0202         const QString currentLanguage = langUsedList->currentItem()->text();
0203         debugLatex << "default lang. :" << currentLanguage;
0204         config->setDefaultLanguage(currentLanguage);
0205     }
0206 
0207     Document doc(m_inputStore, m_fileOut);
0208 //     debugLatex << "---------- analyze file -------------";
0209     doc.analyze();
0210 //     debugLatex << "---------- generate file -------------";
0211     doc.generate();
0212 //     debugLatex << "KSPREAD LATEX EXPORT FILTER --> END";
0213 }
0214 
0215 void LatexExportDialog::addLanguage()
0216 {
0217     QListWidgetItem* currentItem = m_ui.languagesList->takeItem(m_ui.languagesList->currentRow());
0218     if (! currentItem) {
0219         return;
0220     }
0221     const QString text = currentItem->text();
0222     debugLatex << "add a new supported language" << text;
0223     m_ui.langUsedList->addItem(text);
0224     delete currentItem;
0225 }
0226 
0227 void LatexExportDialog::removeLanguage()
0228 {
0229     QListWidgetItem* currentItem = m_ui.langUsedList->takeItem(m_ui.langUsedList->currentRow());
0230     if (! currentItem) {
0231         return;
0232     }
0233     const QString text = currentItem->text();
0234     debugLatex << "remove a language" << text;
0235     m_ui.languagesList->addItem(text);
0236     delete currentItem;
0237 }