File indexing completed on 2024-04-28 16:26:32

0001 /********************************************************************************
0002  * Copyright (C) 2005-2006 by Holger Danielsson (holger.danielsson@t-online.de) *
0003  *           (C) 2011 by Michel Ludwig (michel.ludwig@kdemail.net)              *
0004  ********************************************************************************/
0005 
0006 
0007 /***************************************************************************
0008  *                                                                         *
0009  *   This program is free software; you can redistribute it and/or modify  *
0010  *   it under the terms of the GNU General Public License as published by  *
0011  *   the Free Software Foundation; either version 2 of the License, or     *
0012  *   (at your option) any later version.                                   *
0013  *                                                                         *
0014  ***************************************************************************/
0015 
0016 #include "previewconfigwidget.h"
0017 
0018 #include <QCheckBox>
0019 #include <QGroupBox>
0020 #include <QGridLayout>
0021 #include <QLabel>
0022 #include <QLayout>
0023 #include <QMap>
0024 #include <QStringList>
0025 #include <QValidator>
0026 #include <QVBoxLayout>
0027 
0028 #include <KColorButton>
0029 #include <KComboBox>
0030 #include <QDialog>
0031 #include <QLineEdit>
0032 #include <KLocalizedString>
0033 #include <KConfigGroup>
0034 
0035 #include "kileconfig.h"
0036 #include "kiledebug.h"
0037 
0038 KileWidgetPreviewConfig::KileWidgetPreviewConfig(KConfig *config, KileTool::QuickPreview *preview, QWidget *parent, const char *name)
0039     : QWidget(parent),
0040       m_config(config),
0041       m_preview(preview)
0042 {
0043     setObjectName(name);
0044     // Layout
0045     QVBoxLayout *vbox = new QVBoxLayout();
0046     vbox->setContentsMargins(0, 0, 0, 0);
0047 //TODO PORT QT5     vbox->setSpacing(QDialog::spacingHint());
0048     setLayout(vbox);
0049 
0050     QGroupBox *groupbox = new QGroupBox(i18n("Quick Preview in a Separate Window"), this);
0051     groupbox->setFlat(false);
0052     QGridLayout *groupboxLayout = new QGridLayout();
0053 //TODO PORT QT5     groupboxLayout->setMargin(QDialog::marginHint());
0054 //TODO PORT QT5     groupboxLayout->setSpacing(QDialog::spacingHint());
0055     groupboxLayout->setAlignment(Qt::AlignTop);
0056     groupbox->setLayout(groupboxLayout);
0057 
0058     QLabel *label = new QLabel(i18n("Select a configuration:"), groupbox);
0059     label->setObjectName("label");
0060     m_combobox = new KComboBox(false, groupbox);
0061     m_combobox->setObjectName("combobox");
0062 
0063     groupboxLayout->addWidget(label, 0, 0);
0064     groupboxLayout->addWidget(m_combobox, 0, 2);
0065     groupboxLayout->setColumnMinimumWidth(1, 8);
0066     groupboxLayout->setColumnStretch(3, 1);
0067 
0068     QGroupBox *gbResolution = new QGroupBox(i18n("Quick Preview in Bottom Bar"), this);
0069     gbResolution->setFlat(false);
0070     QGridLayout *resLayout = new QGridLayout();
0071 //TODO PORT QT5     resLayout->setMargin(QDialog::marginHint());
0072 //TODO PORT QT5     resLayout->setSpacing(QDialog::spacingHint());
0073     resLayout->setAlignment(Qt::AlignTop);
0074     gbResolution->setLayout(resLayout);
0075 
0076     QLabel *resLabel = new QLabel(i18n("&Resolution:"), gbResolution);
0077     m_leDvipngResolution = new QLineEdit(gbResolution);
0078     QLabel *resDpi = new QLabel(i18n("dpi"), gbResolution);
0079     QLabel *resAllowed = new QLabel(i18n("(allowed values: 30-1000 dpi)"), gbResolution);
0080 
0081     QLabel *backgroundColorLabel = new QLabel(i18n("&Background Color:"), gbResolution);
0082     m_backgroundColorButton = new KColorButton(gbResolution);
0083     m_backgroundColorButton->setDefaultColor(QColor(Qt::white));
0084     backgroundColorLabel->setBuddy(m_backgroundColorButton);
0085     // set validator
0086     QValidator* validator = new QIntValidator(30, 1000, this);
0087     m_leDvipngResolution->setValidator(validator);
0088     resLabel->setBuddy(m_leDvipngResolution);
0089 
0090     QString sep = "&nbsp;&nbsp;&nbsp;&nbsp;";
0091     QString title = i18n("Kile supports three kinds of conversion to png images");
0092     QString tool1 = i18n("dvi --> png") + sep + i18n("(uses dvipng)");
0093     QString tool2 = i18n("dvi --> ps --> png") + sep + i18n("(uses dvips/convert)");
0094     QString tool3 = i18n("pdf --> png") + sep + i18n("(uses convert)");
0095     QString description = QString("%1:<ul><li>%2<li>%3<li>%4</ul>").arg(title, tool1, tool2, tool3);
0096 
0097     QLabel *labelDescription = new QLabel(description, gbResolution);
0098     QLabel *labelDvipng = new QLabel(i18n("dvipng:"), gbResolution);
0099     QLabel *labelConvert = new QLabel(i18n("convert:"), gbResolution);
0100     m_lbDvipng = new QLabel(gbResolution);
0101     m_lbConvert = new QLabel(gbResolution);
0102 
0103     resLayout->addWidget(resLabel, 0, 0);
0104     resLayout->addWidget(m_leDvipngResolution, 0, 2);
0105     resLayout->addWidget(resDpi, 0, 3);
0106     resLayout->addWidget(resAllowed, 0, 5, Qt::AlignLeft);
0107     resLayout->addWidget(backgroundColorLabel, 1, 0);
0108     resLayout->addWidget(m_backgroundColorButton, 1, 2);
0109     resLayout->addWidget(labelDescription, 2, 0, 1, 6);
0110     resLayout->addWidget(labelDvipng, 3, 0);
0111     resLayout->addWidget(m_lbDvipng, 3, 2);
0112     resLayout->addWidget(labelConvert, 4, 0);
0113     resLayout->addWidget(m_lbConvert, 4, 2);
0114     resLayout->setColumnMinimumWidth(1, 8);
0115     resLayout->setColumnMinimumWidth(4, 24);
0116     resLayout->setColumnStretch(5, 1);
0117 
0118     m_gbPreview = new QGroupBox(i18n("Properties"), this);
0119     m_gbPreview->setFlat(false);
0120     m_gbPreview->setObjectName("gbpreview");
0121     QGridLayout *previewLayout = new QGridLayout();
0122 //TODO PORT QT5     previewLayout->setMargin(QDialog::marginHint());
0123 //TODO PORT QT5     previewLayout->setSpacing(QDialog::spacingHint());
0124     previewLayout->setAlignment(Qt::AlignTop);
0125     m_gbPreview->setLayout(previewLayout);
0126 
0127     QLabel *labelPreviewWidget = new QLabel(i18n("Show preview in bottom bar:"), m_gbPreview);
0128     QLabel *labelPreviewType = new QLabel(i18n("Conversion to image:"), m_gbPreview);
0129     QLabel *labelSelection = new QLabel(i18n("Selection:"), m_gbPreview);
0130     QLabel *labelEnvironment = new QLabel(i18n("Environment:"), m_gbPreview);
0131     QLabel *labelMathgroup = new QLabel(i18n("Mathgroup:"), m_gbPreview);
0132     QLabel *labelSubdocument1 = new QLabel(i18n("Subdocument:"), m_gbPreview);
0133     QLabel *labelSubdocument2 = new QLabel(i18n("Not available, opens always in a separate window."), m_gbPreview);
0134     m_cbSelection = new QCheckBox(m_gbPreview);
0135     m_cbEnvironment = new QCheckBox(m_gbPreview);
0136     m_cbMathgroup = new QCheckBox(m_gbPreview);
0137     m_coSelection = new KComboBox(false, m_gbPreview);
0138     m_coEnvironment = new KComboBox(false, m_gbPreview);
0139     m_coMathgroup = new KComboBox(false, m_gbPreview);
0140 
0141     previewLayout->addWidget(labelPreviewWidget, 0, 0, 1, 3);
0142     previewLayout->addWidget(labelPreviewType, 0, 4);
0143     previewLayout->addWidget(labelSelection, 1, 0);
0144     previewLayout->addWidget(m_cbSelection, 1, 2);
0145     previewLayout->addWidget(m_coSelection, 1, 4);
0146     previewLayout->addWidget(labelEnvironment, 2, 0);
0147     previewLayout->addWidget(m_cbEnvironment, 2, 2);
0148     previewLayout->addWidget(m_coEnvironment, 2, 4);
0149     previewLayout->addWidget(labelMathgroup, 3, 0);
0150     previewLayout->addWidget(m_cbMathgroup, 3, 2);
0151     previewLayout->addWidget(m_coMathgroup, 3, 4);
0152     previewLayout->addWidget(labelSubdocument1, 4, 0);
0153     previewLayout->addWidget(labelSubdocument2, 4, 2, 1, 4, Qt::AlignLeft);
0154     previewLayout->setRowMinimumHeight(0, 3 * labelPreviewWidget->sizeHint().height() / 2);
0155     previewLayout->setRowMinimumHeight(3, m_coEnvironment->sizeHint().height());
0156     previewLayout->setColumnMinimumWidth(1, 12);
0157     previewLayout->setColumnMinimumWidth(3, 40);
0158     previewLayout->setColumnStretch(5, 1);
0159 
0160     vbox->addWidget(groupbox);
0161     vbox->addWidget(gbResolution);
0162     vbox->addWidget(m_gbPreview);
0163     vbox->addStretch();
0164 
0165     connect(m_cbEnvironment, SIGNAL(clicked()), this, SLOT(updateConversionTools()));
0166     connect(m_cbSelection, SIGNAL(clicked()), this, SLOT(updateConversionTools()));
0167     connect(m_cbMathgroup, SIGNAL(clicked()), this, SLOT(updateConversionTools()));
0168 }
0169 
0170 //////////////////// read/write configuration ////////////////////
0171 
0172 void KileWidgetPreviewConfig::readConfig()
0173 {
0174     setupSeparateWindow();
0175     setupBottomBar();
0176     setupProperties();
0177 
0178     updateConversionTools();
0179 }
0180 
0181 void KileWidgetPreviewConfig::writeConfig()
0182 {
0183     KileConfig::setPreviewTask(m_combobox->currentText());
0184 
0185     bool ok;
0186     QString resolution = m_leDvipngResolution->text();
0187     int dpi = resolution.toInt(&ok);
0188     if(ok) {
0189         if(dpi < 30) {
0190             resolution = "30";
0191         }
0192         else {
0193             if(dpi > 1000) {
0194                 resolution = "1000";
0195             }
0196         }
0197         KileConfig::setDvipngResolution(resolution);
0198     }
0199     KileConfig::setPreviewPaneBackgroundColor(m_backgroundColorButton->color());
0200     if(m_gbPreview->isEnabled()) {
0201         KileConfig::setSelPreviewInWidget(m_cbSelection->isChecked());
0202         KileConfig::setEnvPreviewInWidget(m_cbEnvironment->isChecked());
0203         KileConfig::setMathgroupPreviewInWidget(m_cbMathgroup->isChecked());
0204     }
0205     else {
0206         KileConfig::setEnvPreviewInWidget(false);
0207         KileConfig::setSelPreviewInWidget(false);
0208         KileConfig::setMathgroupPreviewInWidget(false);
0209     }
0210 
0211     KileConfig::setSelPreviewTool(index2tool(m_coSelection->currentIndex()));
0212     KileConfig::setEnvPreviewTool(index2tool(m_coEnvironment->currentIndex()));
0213     KileConfig::setMathgroupPreviewTool(index2tool(m_coMathgroup->currentIndex()));
0214 }
0215 
0216 void KileWidgetPreviewConfig::setupSeparateWindow()
0217 {
0218     // get all possible tasks for QuickPreview in a separate window
0219     QStringList tasklist;
0220     m_preview->getTaskList(tasklist);
0221 
0222     // split them into group and combobox entry
0223     m_combobox->clear();
0224     for(int i = 0; i < tasklist.count(); ++i) {
0225         QStringList list = tasklist[i].split('=');
0226         if (m_config->hasGroup(list[0])) {
0227             m_combobox->addItem(list[1]);
0228         }
0229     }
0230 
0231     // set current task
0232     m_combobox->setCurrentIndex(m_combobox->findText(KileConfig::previewTask()));
0233 }
0234 
0235 void KileWidgetPreviewConfig::setupBottomBar()
0236 {
0237     // setup resolution for QuickPreview in bottom bar
0238     m_leDvipngResolution->setText(KileConfig::dvipngResolution());
0239 
0240     // setup tools
0241     m_dvipngInstalled = KileConfig::dvipng();
0242     m_convertInstalled = KileConfig::convert();
0243 
0244     m_backgroundColorButton->setColor(KileConfig::previewPaneBackgroundColor());
0245 
0246     m_lbDvipng->setText((m_dvipngInstalled) ? i18n("installed") : i18n("not installed"));
0247     m_lbConvert->setText((m_convertInstalled) ? i18n("installed") : i18n("not installed"));
0248 }
0249 
0250 void KileWidgetPreviewConfig::setupProperties()
0251 {
0252     // setup properties for QuickPreview
0253     m_cbSelection->setChecked(KileConfig::selPreviewInWidget());
0254     m_cbEnvironment->setChecked(KileConfig::envPreviewInWidget());
0255     m_cbMathgroup->setChecked(KileConfig::mathgroupPreviewInWidget());
0256 
0257     // setup conversion tools
0258     QStringList toollist;
0259     if(m_dvipngInstalled) {
0260         toollist << i18n("dvi --> png");
0261     }
0262     if(m_convertInstalled) {
0263         toollist << i18n("dvi --> ps --> png");
0264         toollist << i18n("pdf --> png");
0265     }
0266 
0267     // setup comboboxes
0268     if(installedTools() == 0) {
0269         m_gbPreview->setEnabled(false);
0270     }
0271     else {
0272         m_coSelection->addItems(toollist);
0273         m_coEnvironment->addItems(toollist);
0274         m_coMathgroup->addItems(toollist);
0275 
0276         m_coSelection->setCurrentIndex(tool2index(KileConfig::selPreviewTool()));
0277         m_coEnvironment->setCurrentIndex(tool2index(KileConfig::envPreviewTool()));
0278         m_coMathgroup->setCurrentIndex(tool2index(KileConfig::mathgroupPreviewTool()));
0279     }
0280 }
0281 
0282 //////////////////// manage tools ////////////////////
0283 
0284 // Tool is 0 (dvi->png), 1 (dvi->ps->png) or 2 (pdf->png).
0285 // But this may not be valid, when tools are not installed anymore.
0286 // So we have to calc a new index for the combobox
0287 //  available = 0:   doesn't matter, everything will be disabled
0288 //  available = 1:   only dvipng, so we use index 0
0289 //  available = 2:   only convert: 0->0, 1->0, 2->1
0290 //  available = 3:   dvipng/convert: index is valid
0291 
0292 int KileWidgetPreviewConfig::tool2index(int tool)
0293 {
0294     int result = 0;
0295 
0296     int available = installedTools();
0297     if(available == 3) {
0298         result = tool;
0299     }
0300     else {
0301         if(available == 2 && tool > 0) {
0302             result = tool - 1;
0303         }
0304     }
0305 
0306     return result;
0307 }
0308 
0309 int KileWidgetPreviewConfig::index2tool(int index)
0310 {
0311     int result = 0;
0312 
0313     int available = installedTools();
0314     if(available == 3) {
0315         result = index;
0316     }
0317     else {
0318         if(available == 2) {
0319             result = index + 1;
0320         }
0321     }
0322 
0323     return result;
0324 }
0325 
0326 // calc installed tools:
0327 //  - 0 : no tools installed
0328 //  - 1 : dvipng installed
0329 //  - 2 : convert installed
0330 //  - 3 : dvipng/convert installed
0331 
0332 int KileWidgetPreviewConfig::installedTools()
0333 {
0334     int tools = 0;
0335     if(m_dvipngInstalled) {
0336         tools += 1;
0337     }
0338     if(m_convertInstalled) {
0339         tools += 2;
0340     }
0341 
0342     return tools;
0343 }
0344 
0345 void KileWidgetPreviewConfig::updateConversionTools()
0346 {
0347     m_coSelection->setEnabled(m_cbSelection->isChecked());
0348     m_coEnvironment->setEnabled(m_cbEnvironment->isChecked());
0349     m_coMathgroup->setEnabled(m_cbMathgroup->isChecked());
0350 }
0351