File indexing completed on 2024-05-12 07:41:28

0001 /*
0002     File                 : SettingsWorksheetPage.cpp
0003     Project              : LabPlot
0004     Description          : settings page for Worksheet
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2008-2017 Alexander Semke <alexander.semke@web.de>
0007     SPDX-FileCopyrightText: 2021 Stefan Gerlach <stefan.gerlach@uni.kn>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "SettingsWorksheetPage.h"
0012 #include "backend/core/Settings.h"
0013 #include "kdefrontend/widgets/ThemesComboBox.h"
0014 #include "tools/TeXRenderer.h"
0015 
0016 #include <KConfigGroup>
0017 #include <KLocalizedString>
0018 
0019 /**
0020  * \brief Page for the 'General' settings of the Labplot settings dialog.
0021  */
0022 SettingsWorksheetPage::SettingsWorksheetPage(QWidget* parent)
0023     : SettingsPage(parent) {
0024     ui.setupUi(this);
0025 
0026     m_cbThemes = new ThemesComboBox();
0027     ui.gridLayout->addWidget(m_cbThemes, 1, 4, 1, 1);
0028     QString info = i18n("Default theme for newly created worksheets and worksheet objects");
0029     ui.lTheme->setToolTip(info);
0030     m_cbThemes->setToolTip(info);
0031 
0032     const int size = ui.cbTexEngine->height();
0033     ui.lLatexWarning->setPixmap(QIcon::fromTheme(QLatin1String("state-warning")).pixmap(size, size));
0034 
0035     // add available TeX typesetting engines
0036     if (TeXRenderer::executableExists(QLatin1String("lualatex")))
0037         ui.cbTexEngine->addItem(QLatin1String("LuaLaTeX"), QLatin1String("lualatex"));
0038 
0039     if (TeXRenderer::executableExists(QLatin1String("xelatex")))
0040         ui.cbTexEngine->addItem(QLatin1String("XeLaTex"), QLatin1String("xelatex"));
0041 
0042     if (TeXRenderer::executableExists(QLatin1String("pdflatex")))
0043         ui.cbTexEngine->addItem(QLatin1String("pdfLaTeX"), QLatin1String("pdflatex"));
0044 
0045     if (TeXRenderer::executableExists(QLatin1String("latex")))
0046         ui.cbTexEngine->addItem(QLatin1String("LaTeX"), QLatin1String("latex"));
0047 
0048     connect(m_cbThemes, &ThemesComboBox::currentThemeChanged, this, &SettingsWorksheetPage::changed);
0049     connect(ui.chkPresenterModeInteractive, &QCheckBox::toggled, this, &SettingsWorksheetPage::changed);
0050     connect(ui.chkDoubleBuffering, &QCheckBox::toggled, this, &SettingsWorksheetPage::changed);
0051     connect(ui.cbTexEngine, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsWorksheetPage::changed);
0052     connect(ui.cbTexEngine, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsWorksheetPage::checkTeX);
0053 
0054     loadSettings();
0055 }
0056 
0057 void SettingsWorksheetPage::applySettings() {
0058     if (!m_changed)
0059         return;
0060 
0061     KConfigGroup group = Settings::group(QStringLiteral("Settings_Worksheet"));
0062     if (m_cbThemes->currentText() == i18n("Default"))
0063         group.writeEntry(QLatin1String("Theme"), QString());
0064     else
0065         group.writeEntry(QLatin1String("Theme"), m_cbThemes->currentText());
0066     group.writeEntry(QLatin1String("PresenterModeInteractive"), ui.chkPresenterModeInteractive->isChecked());
0067     group.writeEntry(QLatin1String("DoubleBuffering"), ui.chkDoubleBuffering->isChecked());
0068     group.writeEntry(QLatin1String("LaTeXEngine"), ui.cbTexEngine->itemData(ui.cbTexEngine->currentIndex()));
0069 }
0070 
0071 void SettingsWorksheetPage::restoreDefaults() {
0072     m_cbThemes->setItemText(0, i18n("Default")); // default theme
0073     ui.chkPresenterModeInteractive->setChecked(false);
0074     ui.chkDoubleBuffering->setChecked(true);
0075 
0076     int index = ui.cbTexEngine->findData(QLatin1String("xelatex"));
0077     if (index == -1) {
0078         index = ui.cbTexEngine->findData(QLatin1String("lualatex"));
0079         if (index == -1) {
0080             index = ui.cbTexEngine->findData(QLatin1String("pdflatex"));
0081             if (index == -1)
0082                 index = ui.cbTexEngine->findData(QLatin1String("latex"));
0083         }
0084     }
0085     ui.cbTexEngine->setCurrentIndex(index);
0086 }
0087 
0088 void SettingsWorksheetPage::loadSettings() {
0089     const KConfigGroup group = Settings::group(QStringLiteral("Settings_Worksheet"));
0090     m_cbThemes->setItemText(0, group.readEntry(QLatin1String("Theme"), ""));
0091     ui.chkPresenterModeInteractive->setChecked(group.readEntry(QLatin1String("PresenterModeInteractive"), false));
0092     ui.chkDoubleBuffering->setChecked(group.readEntry(QLatin1String("DoubleBuffering"), true));
0093 
0094     QString engine = group.readEntry(QLatin1String("LaTeXEngine"), "");
0095     int index = -1;
0096     if (engine.isEmpty()) {
0097         // empty string was found in the settings (either the settings never saved or no tex engine was available during the last save)
0098         //->check whether the latex environment was installed in the meantime
0099         index = ui.cbTexEngine->findData(QLatin1String("xelatex"));
0100         if (index == -1) {
0101             index = ui.cbTexEngine->findData(QLatin1String("lualatex"));
0102             if (index == -1) {
0103                 index = ui.cbTexEngine->findData(QLatin1String("pdflatex"));
0104                 if (index == -1)
0105                     index = ui.cbTexEngine->findData(QLatin1String("latex"));
0106             }
0107         }
0108 
0109         if (index != -1) {
0110             // one of the tex engines was found -> automatically save it in the settings without any user action
0111             KConfigGroup group = Settings::group(QStringLiteral("Settings_Worksheet"));
0112             group.writeEntry(QLatin1String("LaTeXEngine"), ui.cbTexEngine->itemData(index));
0113         }
0114     } else
0115         index = ui.cbTexEngine->findData(engine);
0116 
0117     ui.cbTexEngine->setCurrentIndex(index);
0118     checkTeX(index);
0119 }
0120 
0121 void SettingsWorksheetPage::changed() {
0122     m_changed = true;
0123     Q_EMIT settingsChanged();
0124 }
0125 
0126 /*!
0127  checks whether all tools required for latex typesetting are available. shows a warning if not.
0128  \sa TeXRenderer::active()
0129  */
0130 void SettingsWorksheetPage::checkTeX(int engineIndex) {
0131     if (engineIndex == -1) {
0132         ui.lLatexWarning->show();
0133         ui.lLatexWarning->setToolTip(i18n("No LaTeX installation found or selected. LaTeX typesetting not possible."));
0134         return;
0135     }
0136 
0137     QString engine = ui.cbTexEngine->itemData(engineIndex).toString();
0138 
0139     if (engine == QLatin1String("latex")) {
0140         // need convert to convert to PNG
0141         if (!TeXRenderer::executableExists(QLatin1String("convert"))) {
0142             ui.lLatexWarning->show();
0143             ui.lLatexWarning->setToolTip(i18n("No 'convert' found. LaTeX typesetting not possible."));
0144             return;
0145         }
0146         // to convert the generated PS files to DVI we need 'dvips'
0147         if (!TeXRenderer::executableExists(QLatin1String("dvips"))) {
0148             ui.lLatexWarning->show();
0149             ui.lLatexWarning->setToolTip(i18n("No 'dvips' found. LaTeX typesetting not possible."));
0150             return;
0151         }
0152 
0153 #if defined(_WIN64)
0154         if (!TeXRenderer::executableExists(QLatin1String("gswin64c"))) {
0155             ui.lLatexWarning->show();
0156             ui.lLatexWarning->setToolTip(i18n("No Ghostscript found. LaTeX typesetting not possible."));
0157             return;
0158         }
0159 #elif defined(HAVE_WINDOWS)
0160         if (!TeXRenderer::executableExists(QLatin1String("gswin32c"))) {
0161             ui.lLatexWarning->show();
0162             ui.lLatexWarning->setToolTip(i18n("No Ghostscript found. LaTeX typesetting not possible."));
0163             return;
0164         }
0165 #endif
0166     }
0167 
0168     ui.lLatexWarning->hide();
0169 }