File indexing completed on 2024-05-12 15:28:01

0001 /***************************************************************************
0002     File                 : SpreadsheetDock.cpp
0003     Project              : LabPlot
0004     Description          : widget for spreadsheet properties
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2010-2019 by Alexander Semke (alexander.semke@web.de)
0007     Copyright            : (C) 2012-2013 by Stefan Gerlach (stefan.gerlach@uni-konstanz.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 "SpreadsheetDock.h"
0031 #include "commonfrontend/spreadsheet/SpreadsheetView.h"
0032 #include "backend/datapicker/DatapickerCurve.h"
0033 #include "backend/spreadsheet/Spreadsheet.h"
0034 #include "kdefrontend/TemplateHandler.h"
0035 
0036 #include <QDir>
0037 #include <KLocalizedString>
0038 #include <KConfigGroup>
0039 #include <KConfig>
0040 
0041  /*!
0042   \class SpreadsheetDock
0043   \brief Provides a widget for editing the properties of the spreadsheets currently selected in the project explorer.
0044 
0045   \ingroup kdefrontend
0046 */
0047 
0048 SpreadsheetDock::SpreadsheetDock(QWidget* parent) : BaseDock(parent) {
0049     ui.setupUi(this);
0050     m_leName = ui.leName;
0051     //leComment = ui.teComment; // is not a lineedit
0052 
0053     connect(ui.leName, &QLineEdit::textChanged, this, &SpreadsheetDock::nameChanged);
0054     connect(ui.teComment, &QTextEdit::textChanged, this, &SpreadsheetDock::commentChanged);
0055     connect(ui.sbColumnCount, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &SpreadsheetDock::columnCountChanged);
0056     connect(ui.sbRowCount, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &SpreadsheetDock::rowCountChanged);
0057     connect(ui.cbShowComments, &QCheckBox::stateChanged, this, &SpreadsheetDock::commentsShownChanged);
0058 
0059     auto* templateHandler = new TemplateHandler(this, TemplateHandler::ClassName::Spreadsheet);
0060     ui.gridLayout->addWidget(templateHandler, 11, 0, 1, 4);
0061     templateHandler->show();
0062     connect(templateHandler, &TemplateHandler::loadConfigRequested, this, &SpreadsheetDock::loadConfigFromTemplate);
0063     connect(templateHandler, &TemplateHandler::saveConfigRequested, this, &SpreadsheetDock::saveConfigAsTemplate);
0064     connect(templateHandler, &TemplateHandler::info, this, &SpreadsheetDock::info);
0065 }
0066 
0067 /*!
0068     set the current spreadsheet(s)
0069 */
0070 void SpreadsheetDock::setSpreadsheets(QList<Spreadsheet*> list) {
0071     m_initializing = true;
0072     m_spreadsheetList = list;
0073     m_spreadsheet = list.first();
0074     m_aspect = list.first();
0075 
0076 
0077     //check whether we have non-editable columns:
0078     bool nonEditable = false;
0079     for (auto* s : m_spreadsheetList) {
0080         if (dynamic_cast<DatapickerCurve*>(s->parentAspect())) {
0081             nonEditable = true;
0082             break;
0083         }
0084     }
0085 
0086     if (list.size() == 1) {
0087         ui.leName->setEnabled(true);
0088         ui.teComment->setEnabled(true);
0089 
0090         ui.leName->setText(m_spreadsheet->name());
0091         ui.teComment->setText(m_spreadsheet->comment());
0092     } else {
0093         //disable the fields "Name" and "Comment" if there are more then one spreadsheet
0094         ui.leName->setEnabled(false);
0095         ui.teComment->setEnabled(false);
0096 
0097         ui.leName->setText(QString());
0098         ui.teComment->setText(QString());
0099     }
0100     ui.leName->setStyleSheet("");
0101     ui.leName->setToolTip("");
0102 
0103     //show the properties of the first Spreadsheet in the list
0104     this->load();
0105 
0106     // undo functions
0107     connect(m_spreadsheet, &AbstractAspect::aspectDescriptionChanged, this, &SpreadsheetDock::spreadsheetDescriptionChanged);
0108     connect(m_spreadsheet, &Spreadsheet::rowCountChanged, this, &SpreadsheetDock::spreadsheetRowCountChanged);
0109     connect(m_spreadsheet, &Spreadsheet::columnCountChanged, this, &SpreadsheetDock::spreadsheetColumnCountChanged);
0110     //TODO: show comments
0111 
0112     ui.lDimensions->setVisible(!nonEditable);
0113     ui.lRowCount->setVisible(!nonEditable);
0114     ui.sbRowCount->setVisible(!nonEditable);
0115     ui.lColumnCount->setVisible(!nonEditable);
0116     ui.sbColumnCount->setVisible(!nonEditable);
0117     ui.lFormat->setVisible(!nonEditable);
0118     ui.lShowComments->setVisible(!nonEditable);
0119     ui.cbShowComments->setVisible(!nonEditable);
0120 
0121     m_initializing = false;
0122 }
0123 
0124 //*************************************************************
0125 //****** SLOTs for changes triggered in SpreadsheetDock *******
0126 //*************************************************************
0127 void SpreadsheetDock::commentChanged() {
0128     if (m_initializing)
0129         return;
0130 
0131     m_spreadsheet->setComment(ui.teComment->document()->toPlainText());
0132 }
0133 
0134 void SpreadsheetDock::rowCountChanged(int rows) {
0135     if (m_initializing)
0136         return;
0137 
0138     for (auto* spreadsheet : m_spreadsheetList)
0139         spreadsheet->setRowCount(rows);
0140 }
0141 
0142 void SpreadsheetDock::columnCountChanged(int columns) {
0143     if (m_initializing)
0144         return;
0145 
0146     for (auto* spreadsheet : m_spreadsheetList)
0147         spreadsheet->setColumnCount(columns);
0148 }
0149 
0150 /*!
0151   switches on/off  the comment header in the views of the selected spreadsheets.
0152 */
0153 void SpreadsheetDock::commentsShownChanged(int state) {
0154     if (m_initializing)
0155         return;
0156 
0157     for (auto* spreadsheet : m_spreadsheetList)
0158         static_cast<SpreadsheetView*>(spreadsheet->view())->showComments(state);
0159 }
0160 
0161 //*************************************************************
0162 //******** SLOTs for changes triggered in Spreadsheet *********
0163 //*************************************************************
0164 void SpreadsheetDock::spreadsheetDescriptionChanged(const AbstractAspect* aspect) {
0165     if (m_spreadsheet != aspect)
0166         return;
0167 
0168     m_initializing = true;
0169     if (aspect->name() != ui.leName->text())
0170         ui.leName->setText(aspect->name());
0171     else if (aspect->comment() != ui.teComment->toPlainText())
0172         ui.teComment->document()->setPlainText(aspect->comment());
0173 
0174     m_initializing = false;
0175 }
0176 
0177 void SpreadsheetDock::spreadsheetRowCountChanged(int count) {
0178     m_initializing = true;
0179     ui.sbRowCount->setValue(count);
0180     m_initializing = false;
0181 }
0182 
0183 void SpreadsheetDock::spreadsheetColumnCountChanged(int count) {
0184     m_initializing = true;
0185     ui.sbColumnCount->setValue(count);
0186     m_initializing = false;
0187 }
0188 
0189 void SpreadsheetDock::spreadsheetShowCommentsChanged(int checked) {
0190     m_initializing = true;
0191     ui.cbShowComments->setChecked(checked);
0192     m_initializing = false;
0193 }
0194 
0195 //*************************************************************
0196 //******************** SETTINGS *******************************
0197 //*************************************************************
0198 void SpreadsheetDock::load() {
0199     ui.sbColumnCount->setValue(m_spreadsheet->columnCount());
0200     ui.sbRowCount->setValue(m_spreadsheet->rowCount());
0201 
0202     auto* view = static_cast<SpreadsheetView*>(m_spreadsheet->view());
0203     ui.cbShowComments->setChecked(view->areCommentsShown());
0204 }
0205 
0206 void SpreadsheetDock::loadConfigFromTemplate(KConfig& config) {
0207     //extract the name of the template from the file name
0208     QString name;
0209     const int index = config.name().lastIndexOf(QLatin1String("/"));
0210     if (index != -1)
0211         name = config.name().right(config.name().size() - index - 1);
0212     else
0213         name = config.name();
0214 
0215     const int size = m_spreadsheetList.size();
0216     if (size > 1)
0217         m_spreadsheet->beginMacro(i18n("%1 spreadsheets: template \"%2\" loaded", size, name));
0218     else
0219         m_spreadsheet->beginMacro(i18n("%1: template \"%2\" loaded", m_spreadsheet->name(), name));
0220 
0221     this->loadConfig(config);
0222 
0223     m_spreadsheet->endMacro();
0224 }
0225 
0226 /*!
0227     loads saved spreadsheet properties from \c config.
0228  */
0229 void SpreadsheetDock::loadConfig(KConfig& config) {
0230     KConfigGroup group = config.group( "Spreadsheet" );
0231 
0232     ui.sbColumnCount->setValue(group.readEntry("ColumnCount", m_spreadsheet->columnCount()));
0233     ui.sbRowCount->setValue(group.readEntry("RowCount", m_spreadsheet->rowCount()));
0234 
0235     auto* view = static_cast<SpreadsheetView*>(m_spreadsheet->view());
0236     ui.cbShowComments->setChecked(group.readEntry("ShowComments", view->areCommentsShown()));
0237 }
0238 
0239 /*!
0240     saves spreadsheet properties to \c config.
0241  */
0242 void SpreadsheetDock::saveConfigAsTemplate(KConfig& config) {
0243     KConfigGroup group = config.group( "Spreadsheet" );
0244     group.writeEntry("ColumnCount", ui.sbColumnCount->value());
0245     group.writeEntry("RowCount", ui.sbRowCount->value());
0246     group.writeEntry("ShowComments",ui.cbShowComments->isChecked());
0247     config.sync();
0248 }