File indexing completed on 2024-05-12 15:27:55

0001 /***************************************************************************
0002     File                 : CantorWorksheetDock.cpp
0003     Project              : LabPlot
0004     Description          : widget for CantorWorksheet properties
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2015 Garvit Khatri (garvitdelhi@gmail.com)
0007     Copyright            : (C) 2015-2018 Alexander Semke (alexander.semke@web.de)
0008 
0009  ***************************************************************************/
0010 
0011 /***************************************************************************
0012  *                                                                         *
0013  *  This program is free software; you can redistribute it and/or modify   *
0014  *  it under the terms of the GNU General Public License as published by   *
0015  *  the Free Software Foundation; either version 2 of the License, or      *
0016  *  (at your option) any later version.                                    *
0017  *                                                                         *
0018  *  This program is distributed in the hope that it will be useful,        *
0019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0021  *  GNU General Public License for more details.                           *
0022  *                                                                         *
0023  *   You should have received a copy of the GNU General Public License     *
0024  *   along with this program; if not, write to the Free Software           *
0025  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0026  *   Boston, MA  02110-1301  USA                                           *
0027  *                                                                         *
0028  ***************************************************************************/
0029 
0030 #include "CantorWorksheetDock.h"
0031 #include "backend/cantorWorksheet/CantorWorksheet.h"
0032 #include <KParts/ReadWritePart>
0033 #include <QAction>
0034 
0035 CantorWorksheetDock::CantorWorksheetDock(QWidget* parent) : BaseDock(parent) {
0036     ui.setupUi(this);
0037     ui.tabWidget->setMovable(true);
0038     m_leName = ui.leName;
0039     m_leComment = ui.leComment;
0040 
0041     //SLOTs
0042     //General
0043     connect(ui.leName, &QLineEdit::textChanged, this, &CantorWorksheetDock::nameChanged);
0044     connect(ui.leComment, &QLineEdit::textChanged, this, &CantorWorksheetDock::commentChanged);
0045     connect(ui.bEvaluate, &QPushButton::pressed, this, &CantorWorksheetDock::evaluateWorksheet);
0046     connect(ui.bRestart, &QPushButton::pressed, this, &CantorWorksheetDock::restartBackend);
0047 }
0048 
0049 void CantorWorksheetDock::setCantorWorksheets(QList<CantorWorksheet*> list) {
0050     m_initializing = true;
0051     m_cantorworksheetlist = list;
0052     m_worksheet = list.first();
0053     m_aspect = list.first();
0054 
0055     //show name/comment
0056     ui.leName->setText(m_worksheet->name());
0057     ui.leName->setStyleSheet("");
0058     ui.leName->setToolTip("");
0059     ui.leComment->setText(m_worksheet->comment());
0060 
0061     //show all available plugins
0062     int k = 0;
0063     int prev_index = ui.tabWidget->currentIndex();
0064     for (int i : index) {
0065         ui.tabWidget->removeTab(i-k);
0066         ++k;
0067     }
0068 
0069     if (m_cantorworksheetlist.size() == 1) {
0070         QList<Cantor::PanelPlugin*> plugins = m_cantorworksheetlist.first()->getPlugins();
0071         index.clear();
0072         for (auto* plugin : plugins) {
0073             if (plugin->name() == QLatin1String("File Browser"))
0074                 continue;
0075             connect(plugin, &Cantor::PanelPlugin::visibilityRequested, this, &CantorWorksheetDock::visibilityRequested);
0076             plugin->setParentWidget(this);
0077             int i = ui.tabWidget->addTab(plugin->widget(), plugin->name());
0078             index.append(i);
0079         }
0080     }
0081     ui.tabWidget->setCurrentIndex(prev_index);
0082 
0083     if (m_worksheet->part()) {
0084         ui.bEvaluate->show();
0085         ui.bRestart->show();
0086     } else {
0087         ui.bEvaluate->hide();
0088         ui.bRestart->hide();
0089     }
0090 
0091     //SIGNALs/SLOTs
0092     connect(m_worksheet, &AbstractAspect::aspectDescriptionChanged, this, &CantorWorksheetDock::worksheetDescriptionChanged);
0093     m_initializing = false;
0094 }
0095 
0096 //*************************************************************
0097 //**** SLOTs for changes triggered in CantorWorksheetDock *****
0098 //*************************************************************
0099 // "General"-tab
0100 void CantorWorksheetDock::evaluateWorksheet() {
0101     m_worksheet->part()->action("evaluate_worksheet")->trigger();
0102 }
0103 
0104 void CantorWorksheetDock::restartBackend() {
0105     m_worksheet->part()->action("restart_backend")->trigger();
0106 }
0107 
0108 /*!
0109  * this slot is called when the visibility for one of the panels in Cantor is requested.
0110  * At the moment this can only happen for the integrated help in Maxima, R, etc.
0111  * Here we hard-code the selection of the second tab being for the help.
0112  * TODO: improve this logic without hard-coding for a fixed index.
0113  */
0114 void CantorWorksheetDock::visibilityRequested() {
0115     ui.tabWidget->setCurrentIndex(1);
0116 }
0117 
0118 //*************************************************************
0119 //******** SLOTs for changes triggered in CantorWorksheet ***********
0120 //*************************************************************
0121 void CantorWorksheetDock::worksheetDescriptionChanged(const AbstractAspect* aspect) {
0122     if (m_worksheet != aspect)
0123         return;
0124 
0125     m_initializing = true;
0126     if (aspect->name() != ui.leName->text())
0127         ui.leName->setText(aspect->name());
0128     else if (aspect->comment() != ui.leComment->text())
0129         ui.leComment->setText(aspect->comment());
0130     m_initializing = false;
0131 }