File indexing completed on 2024-04-28 15:14:03

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