File indexing completed on 2025-10-26 03:35:45

0001 /*
0002     File                 : WorksheetDock.cpp
0003     Project              : LabPlot
0004     Description          : widget for worksheet properties
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2010-2022 Alexander Semke <alexander.semke@web.de>
0007     SPDX-FileCopyrightText: 2012-2013 Stefan Gerlach <stefan.gerlach@uni-konstanz.de>
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 #include "WorksheetDock.h"
0013 #include "backend/core/Settings.h"
0014 #include "kdefrontend/GuiTools.h"
0015 #include "kdefrontend/TemplateHandler.h"
0016 #include "kdefrontend/ThemeHandler.h"
0017 #include "kdefrontend/widgets/BackgroundWidget.h"
0018 
0019 #include <KConfig>
0020 #include <KLocalizedString>
0021 
0022 #include <QPageSize>
0023 
0024 #include <gsl/gsl_const_cgs.h>
0025 
0026 /*!
0027   \class WorksheetDock
0028   \brief  Provides a widget for editing the properties of the worksheets currently selected in the project explorer.
0029 
0030   \ingroup kdefrontend
0031 */
0032 
0033 WorksheetDock::WorksheetDock(QWidget* parent)
0034     : BaseDock(parent) {
0035     ui.setupUi(this);
0036     setBaseWidgets(ui.leName, ui.teComment);
0037 
0038     // Background-tab
0039     auto* layout = static_cast<QHBoxLayout*>(ui.tabBackground->layout());
0040     backgroundWidget = new BackgroundWidget(ui.tabBackground);
0041     layout->insertWidget(0, backgroundWidget);
0042 
0043     // Layout-tab
0044     ui.chScaleContent->setToolTip(i18n("If checked, rescale the content of the worksheet on size changes. Otherwise resize the canvas only."));
0045 
0046     ui.cbLayout->addItem(QIcon::fromTheme(QStringLiteral("labplot-editbreaklayout")), i18n("No Layout"));
0047     ui.cbLayout->addItem(QIcon::fromTheme(QStringLiteral("labplot-edithlayout")), i18n("Vertical Layout"));
0048     ui.cbLayout->addItem(QIcon::fromTheme(QStringLiteral("labplot-editvlayout")), i18n("Horizontal Layout"));
0049     ui.cbLayout->addItem(QIcon::fromTheme(QStringLiteral("labplot-editgrid")), i18n("Grid Layout"));
0050 
0051     // adjust layouts in the tabs
0052     for (int i = 0; i < ui.tabWidget->count(); ++i) {
0053         auto* layout = dynamic_cast<QGridLayout*>(ui.tabWidget->widget(i)->layout());
0054         if (!layout)
0055             continue;
0056 
0057         layout->setContentsMargins(2, 2, 2, 2);
0058         layout->setHorizontalSpacing(2);
0059         layout->setVerticalSpacing(2);
0060     }
0061 
0062     WorksheetDock::updateLocale();
0063 
0064     // SLOTs
0065     // General
0066     connect(ui.cbSizeType, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &WorksheetDock::sizeTypeChanged);
0067     connect(ui.cbPage, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &WorksheetDock::pageChanged);
0068     connect(ui.sbWidth, QOverload<double>::of(&NumberSpinBox::valueChanged), this, &WorksheetDock::sizeChanged);
0069     connect(ui.sbHeight, QOverload<double>::of(&NumberSpinBox::valueChanged), this, &WorksheetDock::sizeChanged);
0070     connect(ui.cbOrientation, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &WorksheetDock::orientationChanged);
0071 
0072     // Layout
0073     connect(ui.cbLayout, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &WorksheetDock::layoutChanged);
0074     connect(ui.chScaleContent, &QCheckBox::clicked, this, &WorksheetDock::scaleContentChanged);
0075     connect(ui.sbLayoutTopMargin, QOverload<double>::of(&NumberSpinBox::valueChanged), this, &WorksheetDock::layoutTopMarginChanged);
0076     connect(ui.sbLayoutBottomMargin, QOverload<double>::of(&NumberSpinBox::valueChanged), this, &WorksheetDock::layoutBottomMarginChanged);
0077     connect(ui.sbLayoutLeftMargin, QOverload<double>::of(&NumberSpinBox::valueChanged), this, &WorksheetDock::layoutLeftMarginChanged);
0078     connect(ui.sbLayoutRightMargin, QOverload<double>::of(&NumberSpinBox::valueChanged), this, &WorksheetDock::layoutRightMarginChanged);
0079     connect(ui.sbLayoutHorizontalSpacing, QOverload<double>::of(&NumberSpinBox::valueChanged), this, &WorksheetDock::layoutHorizontalSpacingChanged);
0080     connect(ui.sbLayoutVerticalSpacing, QOverload<double>::of(&NumberSpinBox::valueChanged), this, &WorksheetDock::layoutVerticalSpacingChanged);
0081     connect(ui.sbLayoutRowCount, QOverload<int>::of(&QSpinBox::valueChanged), this, &WorksheetDock::layoutRowCountChanged);
0082     connect(ui.sbLayoutColumnCount, QOverload<int>::of(&QSpinBox::valueChanged), this, &WorksheetDock::layoutColumnCountChanged);
0083 
0084     // theme and template handlers
0085     auto* frame = new QFrame(this);
0086     layout = new QHBoxLayout(frame);
0087     layout->setContentsMargins(0, 11, 0, 11);
0088 
0089     m_themeHandler = new ThemeHandler(this);
0090     layout->addWidget(m_themeHandler);
0091     connect(m_themeHandler, &ThemeHandler::loadThemeRequested, this, &WorksheetDock::loadTheme);
0092     connect(m_themeHandler, &ThemeHandler::info, this, &WorksheetDock::info);
0093 
0094     auto* templateHandler = new TemplateHandler(this, QLatin1String("Worksheet"));
0095     layout->addWidget(templateHandler);
0096     connect(templateHandler, &TemplateHandler::loadConfigRequested, this, &WorksheetDock::loadConfigFromTemplate);
0097     connect(templateHandler, &TemplateHandler::saveConfigRequested, this, &WorksheetDock::saveConfigAsTemplate);
0098     connect(templateHandler, &TemplateHandler::info, this, &WorksheetDock::info);
0099 
0100     ui.verticalLayout->addWidget(frame);
0101 
0102     this->retranslateUi();
0103 }
0104 
0105 void WorksheetDock::setWorksheets(QList<Worksheet*> list) {
0106     CONDITIONAL_LOCK_RETURN;
0107     m_worksheetList = list;
0108     m_worksheet = list.first();
0109     setAspects(list);
0110 
0111     // set the initial standard page and the orientation to A4/portrait.
0112     // this should be used as default initial setting when the user switches
0113     // from Custom to Standard Page
0114     ui.cbPage->setCurrentIndex(ui.cbPage->findData(QPageSize::A4));
0115     ui.cbOrientation->setCurrentIndex(0);
0116 
0117     // show the properties of the first worksheet
0118     this->load();
0119     this->worksheetLayoutChanged(m_worksheet->layout());
0120 
0121     m_themeHandler->setCurrentTheme(m_worksheet->theme());
0122 
0123     connect(m_worksheet, &Worksheet::aspectDescriptionChanged, this, &WorksheetDock::worksheetDescriptionChanged);
0124     connect(m_worksheet, &Worksheet::pageRectChanged, this, &WorksheetDock::worksheetPageRectChanged);
0125     connect(m_worksheet, &Worksheet::scaleContentChanged, this, &WorksheetDock::worksheetScaleContentChanged);
0126     connect(m_worksheet, &Worksheet::useViewSizeChanged, this, &WorksheetDock::worksheetUseViewSizeChanged);
0127 
0128     connect(m_worksheet, &Worksheet::layoutChanged, this, &WorksheetDock::worksheetLayoutChanged);
0129     connect(m_worksheet, &Worksheet::layoutTopMarginChanged, this, &WorksheetDock::worksheetLayoutTopMarginChanged);
0130     connect(m_worksheet, &Worksheet::layoutBottomMarginChanged, this, &WorksheetDock::worksheetLayoutBottomMarginChanged);
0131     connect(m_worksheet, &Worksheet::layoutLeftMarginChanged, this, &WorksheetDock::worksheetLayoutLeftMarginChanged);
0132     connect(m_worksheet, &Worksheet::layoutRightMarginChanged, this, &WorksheetDock::worksheetLayoutRightMarginChanged);
0133     connect(m_worksheet, &Worksheet::layoutVerticalSpacingChanged, this, &WorksheetDock::worksheetLayoutVerticalSpacingChanged);
0134     connect(m_worksheet, &Worksheet::layoutHorizontalSpacingChanged, this, &WorksheetDock::worksheetLayoutHorizontalSpacingChanged);
0135     connect(m_worksheet, &Worksheet::layoutRowCountChanged, this, &WorksheetDock::worksheetLayoutRowCountChanged);
0136     connect(m_worksheet, &Worksheet::layoutColumnCountChanged, this, &WorksheetDock::worksheetLayoutColumnCountChanged);
0137 
0138     connect(m_worksheet, &Worksheet::themeChanged, m_themeHandler, &ThemeHandler::setCurrentTheme);
0139 }
0140 
0141 void WorksheetDock::updateLocale() {
0142     const auto numberLocale = QLocale();
0143     ui.sbWidth->setLocale(numberLocale);
0144     ui.sbHeight->setLocale(numberLocale);
0145     ui.sbLayoutTopMargin->setLocale(numberLocale);
0146     ui.sbLayoutBottomMargin->setLocale(numberLocale);
0147     ui.sbLayoutLeftMargin->setLocale(numberLocale);
0148     ui.sbLayoutRightMargin->setLocale(numberLocale);
0149     ui.sbLayoutHorizontalSpacing->setLocale(numberLocale);
0150     ui.sbLayoutVerticalSpacing->setLocale(numberLocale);
0151 }
0152 
0153 void WorksheetDock::updateUnits() {
0154     const KConfigGroup group = Settings::group(QStringLiteral("Settings_General"));
0155     BaseDock::Units units = (BaseDock::Units)group.readEntry(QStringLiteral("Units"), static_cast<int>(Units::Metric));
0156     if (units == m_units)
0157         return;
0158 
0159     m_units = units;
0160     CONDITIONAL_LOCK_RETURN;
0161     QString suffix;
0162     if (m_units == Units::Metric) {
0163         // convert from imperial to metric
0164         m_worksheetUnit = Worksheet::Unit::Centimeter;
0165         suffix = QLatin1String(" cm");
0166         ui.sbWidth->setValue(ui.sbWidth->value() * GSL_CONST_CGS_INCH);
0167         ui.sbHeight->setValue(ui.sbHeight->value() * GSL_CONST_CGS_INCH);
0168         ui.sbLayoutTopMargin->setValue(ui.sbLayoutTopMargin->value() * GSL_CONST_CGS_INCH);
0169         ui.sbLayoutBottomMargin->setValue(ui.sbLayoutBottomMargin->value() * GSL_CONST_CGS_INCH);
0170         ui.sbLayoutLeftMargin->setValue(ui.sbLayoutLeftMargin->value() * GSL_CONST_CGS_INCH);
0171         ui.sbLayoutRightMargin->setValue(ui.sbLayoutRightMargin->value() * GSL_CONST_CGS_INCH);
0172         ui.sbLayoutHorizontalSpacing->setValue(ui.sbLayoutHorizontalSpacing->value() * GSL_CONST_CGS_INCH);
0173         ui.sbLayoutVerticalSpacing->setValue(ui.sbLayoutVerticalSpacing->value() * GSL_CONST_CGS_INCH);
0174     } else {
0175         // convert from metric to imperial
0176         m_worksheetUnit = Worksheet::Unit::Inch;
0177         suffix = QLatin1String(" in");
0178         ui.sbWidth->setValue(ui.sbWidth->value() / GSL_CONST_CGS_INCH);
0179         ui.sbHeight->setValue(ui.sbHeight->value() / GSL_CONST_CGS_INCH);
0180         ui.sbLayoutTopMargin->setValue(ui.sbLayoutTopMargin->value() / GSL_CONST_CGS_INCH);
0181         ui.sbLayoutBottomMargin->setValue(ui.sbLayoutBottomMargin->value() / GSL_CONST_CGS_INCH);
0182         ui.sbLayoutLeftMargin->setValue(ui.sbLayoutLeftMargin->value() / GSL_CONST_CGS_INCH);
0183         ui.sbLayoutRightMargin->setValue(ui.sbLayoutRightMargin->value() / GSL_CONST_CGS_INCH);
0184         ui.sbLayoutHorizontalSpacing->setValue(ui.sbLayoutHorizontalSpacing->value() / GSL_CONST_CGS_INCH);
0185         ui.sbLayoutVerticalSpacing->setValue(ui.sbLayoutVerticalSpacing->value() / GSL_CONST_CGS_INCH);
0186     }
0187 
0188     ui.sbWidth->setSuffix(suffix);
0189     ui.sbHeight->setSuffix(suffix);
0190     ui.sbLayoutTopMargin->setSuffix(suffix);
0191     ui.sbLayoutBottomMargin->setSuffix(suffix);
0192     ui.sbLayoutLeftMargin->setSuffix(suffix);
0193     ui.sbLayoutRightMargin->setSuffix(suffix);
0194     ui.sbLayoutHorizontalSpacing->setSuffix(suffix);
0195     ui.sbLayoutVerticalSpacing->setSuffix(suffix);
0196 }
0197 
0198 /*!
0199     Checks whether the size is one of the QPageSize::PageSizeId and
0200     updates Size and Orientation checkbox when width/height changes.
0201 */
0202 void WorksheetDock::updatePaperSize() {
0203     if (m_worksheet->useViewSize()) {
0204         ui.cbSizeType->setCurrentIndex(ui.cbSizeType->findData((int)SizeType::ViewSize));
0205         return;
0206     }
0207 
0208     double w = ui.sbWidth->value();
0209     double h = ui.sbHeight->value();
0210     if (m_units == Units::Metric) {
0211         // In UI we use cm, so we need to convert to mm first before we check with QPageSize
0212         w *= 10;
0213         h *= 10;
0214     }
0215 
0216     const QSizeF s = QSizeF(w, h);
0217     const QSizeF st = s.transposed();
0218 
0219     // determine the position of the QPageSize::PageSizeId in the combobox
0220     bool found = false;
0221     for (int i = 0; i < ui.cbPage->count(); ++i) {
0222         const QVariant v = ui.cbPage->itemData(i);
0223         if (!v.isValid())
0224             continue;
0225 
0226         const auto id = v.value<QPageSize::PageSizeId>();
0227         QPageSize::Unit pageUnit = (m_units == Units::Metric) ? QPageSize::Millimeter : QPageSize::Inch;
0228         const QSizeF ps = QPageSize::size(id, pageUnit);
0229         if (s == ps) { // check the portrait-orientation first
0230             ui.cbPage->setCurrentIndex(i);
0231             ui.cbOrientation->setCurrentIndex(0); // a QPageSize::PaperSize in portrait-orientation was found
0232             found = true;
0233             break;
0234         } else if (st == ps) { // check for the landscape-orientation
0235             ui.cbPage->setCurrentIndex(i);
0236             ui.cbOrientation->setCurrentIndex(1); // a QPageSize::PaperSize in landscape-orientation was found
0237             found = true;
0238             break;
0239         }
0240     }
0241 
0242     if (!found)
0243         ui.cbSizeType->setCurrentIndex(ui.cbSizeType->findData((int)SizeType::Custom));
0244     else
0245         ui.cbSizeType->setCurrentIndex(ui.cbSizeType->findData((int)SizeType::StandardPage));
0246 }
0247 
0248 //*************************************************************
0249 //****** SLOTs for changes triggered in WorksheetDock *********
0250 //*************************************************************
0251 void WorksheetDock::retranslateUi() {
0252     CONDITIONAL_LOCK_RETURN;
0253 
0254     // Geometry
0255     ui.cbOrientation->clear();
0256     ui.cbOrientation->addItem(i18n("Portrait"));
0257     ui.cbOrientation->addItem(i18n("Landscape"));
0258 
0259     QString suffix;
0260     if (m_units == Units::Metric)
0261         suffix = QLatin1String(" cm");
0262     else
0263         suffix = QLatin1String(" in");
0264 
0265     ui.sbWidth->setSuffix(suffix);
0266     ui.sbHeight->setSuffix(suffix);
0267     ui.sbLayoutTopMargin->setSuffix(suffix);
0268     ui.sbLayoutBottomMargin->setSuffix(suffix);
0269     ui.sbLayoutLeftMargin->setSuffix(suffix);
0270     ui.sbLayoutRightMargin->setSuffix(suffix);
0271     ui.sbLayoutHorizontalSpacing->setSuffix(suffix);
0272     ui.sbLayoutVerticalSpacing->setSuffix(suffix);
0273 
0274     ui.cbSizeType->clear();
0275     ui.cbSizeType->addItem(i18n("View Size"), (int)SizeType::ViewSize);
0276     ui.cbSizeType->addItem(i18n("Standard Page"), (int)SizeType::StandardPage);
0277     ui.cbSizeType->addItem(i18n("Custom"), (int)SizeType::Custom);
0278 
0279     const QVector<QPageSize::PageSizeId> pageSizeIds = {
0280         QPageSize::A0,    QPageSize::A1,     QPageSize::A2,    QPageSize::A3,     QPageSize::A4,      QPageSize::A5,     QPageSize::A6,  QPageSize::A7,
0281         QPageSize::A8,    QPageSize::A9,     QPageSize::B0,    QPageSize::B1,     QPageSize::B2,      QPageSize::B3,     QPageSize::B4,  QPageSize::B5,
0282         QPageSize::B6,    QPageSize::B7,     QPageSize::B8,    QPageSize::B9,     QPageSize::B10,     QPageSize::C5E,    QPageSize::DLE, QPageSize::Executive,
0283         QPageSize::Folio, QPageSize::Ledger, QPageSize::Legal, QPageSize::Letter, QPageSize::Tabloid, QPageSize::Comm10E};
0284     ui.cbPage->clear();
0285     for (auto id : pageSizeIds)
0286         ui.cbPage->addItem(QPageSize::name(id), id);
0287 }
0288 
0289 // "General"-tab
0290 void WorksheetDock::scaleContentChanged(bool scaled) {
0291     CONDITIONAL_LOCK_RETURN;
0292 
0293     for (auto* worksheet : m_worksheetList)
0294         worksheet->setScaleContent(scaled);
0295 }
0296 
0297 void WorksheetDock::sizeTypeChanged(int index) {
0298     const auto sizeType = static_cast<SizeType>(ui.cbSizeType->itemData(index).toInt());
0299 
0300     switch (sizeType) {
0301     case SizeType::ViewSize:
0302         ui.lPage->hide();
0303         ui.cbPage->hide();
0304         ui.lOrientation->hide();
0305         ui.cbOrientation->hide();
0306         ui.sbWidth->setEnabled(false);
0307         ui.sbHeight->setEnabled(false);
0308         break;
0309     case SizeType::StandardPage:
0310         ui.lPage->show();
0311         ui.cbPage->show();
0312         ui.lOrientation->show();
0313         ui.cbOrientation->show();
0314         ui.sbWidth->setEnabled(false);
0315         ui.sbHeight->setEnabled(false);
0316         break;
0317     case SizeType::Custom:
0318         ui.lPage->hide();
0319         ui.cbPage->hide();
0320         ui.lOrientation->hide();
0321         ui.cbOrientation->hide();
0322         ui.sbWidth->setEnabled(true);
0323         ui.sbHeight->setEnabled(true);
0324         break;
0325     }
0326 
0327     if (m_initializing) // don't lock here since we potentially need to call setters in pageChanged() below
0328         return;
0329 
0330     switch (sizeType) {
0331     case SizeType::ViewSize:
0332         for (auto* worksheet : m_worksheetList)
0333             worksheet->setUseViewSize(true);
0334         break;
0335     case SizeType::StandardPage:
0336         pageChanged(ui.cbPage->currentIndex());
0337         break;
0338     case SizeType::Custom:
0339         if (m_worksheet->useViewSize()) {
0340             for (auto* worksheet : m_worksheetList)
0341                 worksheet->setUseViewSize(false);
0342         }
0343         sizeChanged();
0344         break;
0345     }
0346 }
0347 
0348 /*!
0349  * \brief called when one of the standard page sizes was changed
0350  * \param i - index of the page size in the combobox
0351  */
0352 void WorksheetDock::pageChanged(int i) {
0353     CONDITIONAL_LOCK_RETURN;
0354 
0355     // determine the width and the height of the to be used predefined layout
0356     const auto index = ui.cbPage->itemData(i).value<QPageSize::PageSizeId>();
0357     QSizeF s = QPageSize::size(index, QPageSize::Millimeter);
0358     if (ui.cbOrientation->currentIndex() == 1)
0359         s.transpose();
0360 
0361     // s is in mm, in UI we show everything in cm/in
0362     if (m_units == Units::Metric) {
0363         ui.sbWidth->setValue(s.width() / 10);
0364         ui.sbHeight->setValue(s.height() / 10);
0365     } else {
0366         ui.sbWidth->setValue(s.width() / 25.4);
0367         ui.sbHeight->setValue(s.height() / 25.4);
0368     }
0369 
0370     const double w = Worksheet::convertToSceneUnits(s.width(), Worksheet::Unit::Millimeter);
0371     const double h = Worksheet::convertToSceneUnits(s.height(), Worksheet::Unit::Millimeter);
0372     for (auto* worksheet : m_worksheetList) {
0373         worksheet->beginMacro(i18n("%1: set page size", worksheet->name()));
0374         worksheet->setUseViewSize(false);
0375         worksheet->setPageRect(QRectF(0, 0, w, h));
0376         worksheet->endMacro();
0377     }
0378 }
0379 
0380 /*!
0381  * \brief called when the width or the height of the page was changed manually
0382  */
0383 void WorksheetDock::sizeChanged() {
0384     CONDITIONAL_RETURN_NO_LOCK;
0385 
0386     const double w = Worksheet::convertToSceneUnits(ui.sbWidth->value(), m_worksheetUnit);
0387     const double h = Worksheet::convertToSceneUnits(ui.sbHeight->value(), m_worksheetUnit);
0388     for (auto* worksheet : m_worksheetList)
0389         worksheet->setPageRect(QRectF(0, 0, w, h));
0390 }
0391 
0392 void WorksheetDock::orientationChanged(int /*index*/) {
0393     this->pageChanged(ui.cbPage->currentIndex());
0394 }
0395 
0396 //"Layout"-tab
0397 void WorksheetDock::layoutChanged(int index) {
0398     auto layout = static_cast<Worksheet::Layout>(index);
0399 
0400     bool b = (layout != Worksheet::Layout::NoLayout);
0401     ui.sbLayoutTopMargin->setEnabled(b);
0402     ui.sbLayoutBottomMargin->setEnabled(b);
0403     ui.sbLayoutLeftMargin->setEnabled(b);
0404     ui.sbLayoutRightMargin->setEnabled(b);
0405     ui.sbLayoutHorizontalSpacing->setEnabled(b);
0406     ui.sbLayoutVerticalSpacing->setEnabled(b);
0407     ui.sbLayoutRowCount->setEnabled(b);
0408     ui.sbLayoutColumnCount->setEnabled(b);
0409 
0410     // show the "scale content" option if no layout active
0411     ui.lScaleContent->setVisible(!b);
0412     ui.chScaleContent->setVisible(!b);
0413 
0414     if (b) {
0415         // show grid specific settings if grid layout selected
0416         bool grid = (layout == Worksheet::Layout::GridLayout);
0417         ui.lGrid->setVisible(grid);
0418         ui.lRowCount->setVisible(grid);
0419         ui.sbLayoutRowCount->setVisible(grid);
0420         ui.lColumnCount->setVisible(grid);
0421         ui.sbLayoutColumnCount->setVisible(grid);
0422     } else {
0423         // no layout selected, hide grid specific settings that were potentially shown before
0424         ui.lGrid->setVisible(false);
0425         ui.lRowCount->setVisible(false);
0426         ui.sbLayoutRowCount->setVisible(false);
0427         ui.lColumnCount->setVisible(false);
0428         ui.sbLayoutColumnCount->setVisible(false);
0429     }
0430 
0431     CONDITIONAL_LOCK_RETURN;
0432 
0433     for (auto* worksheet : m_worksheetList)
0434         worksheet->setLayout(layout);
0435 }
0436 
0437 void WorksheetDock::layoutTopMarginChanged(double margin) {
0438     CONDITIONAL_RETURN_NO_LOCK;
0439 
0440     for (auto* worksheet : m_worksheetList)
0441         worksheet->setLayoutTopMargin(Worksheet::convertToSceneUnits(margin, m_worksheetUnit));
0442 }
0443 
0444 void WorksheetDock::layoutBottomMarginChanged(double margin) {
0445     CONDITIONAL_RETURN_NO_LOCK;
0446 
0447     for (auto* worksheet : m_worksheetList)
0448         worksheet->setLayoutBottomMargin(Worksheet::convertToSceneUnits(margin, m_worksheetUnit));
0449 }
0450 
0451 void WorksheetDock::layoutLeftMarginChanged(double margin) {
0452     CONDITIONAL_RETURN_NO_LOCK;
0453 
0454     for (auto* worksheet : m_worksheetList)
0455         worksheet->setLayoutLeftMargin(Worksheet::convertToSceneUnits(margin, m_worksheetUnit));
0456 }
0457 
0458 void WorksheetDock::layoutRightMarginChanged(double margin) {
0459     CONDITIONAL_RETURN_NO_LOCK;
0460 
0461     for (auto* worksheet : m_worksheetList)
0462         worksheet->setLayoutRightMargin(Worksheet::convertToSceneUnits(margin, m_worksheetUnit));
0463 }
0464 
0465 void WorksheetDock::layoutHorizontalSpacingChanged(double spacing) {
0466     CONDITIONAL_RETURN_NO_LOCK;
0467 
0468     for (auto* worksheet : m_worksheetList)
0469         worksheet->setLayoutHorizontalSpacing(Worksheet::convertToSceneUnits(spacing, m_worksheetUnit));
0470 }
0471 
0472 void WorksheetDock::layoutVerticalSpacingChanged(double spacing) {
0473     CONDITIONAL_RETURN_NO_LOCK;
0474 
0475     for (auto* worksheet : m_worksheetList)
0476         worksheet->setLayoutVerticalSpacing(Worksheet::convertToSceneUnits(spacing, m_worksheetUnit));
0477 }
0478 
0479 void WorksheetDock::layoutRowCountChanged(int count) {
0480     CONDITIONAL_LOCK_RETURN;
0481 
0482     for (auto* worksheet : m_worksheetList)
0483         worksheet->setLayoutRowCount(count);
0484 }
0485 
0486 void WorksheetDock::layoutColumnCountChanged(int count) {
0487     CONDITIONAL_LOCK_RETURN;
0488 
0489     for (auto* worksheet : m_worksheetList)
0490         worksheet->setLayoutColumnCount(count);
0491 }
0492 
0493 //*************************************************************
0494 //******** SLOTs for changes triggered in Worksheet ***********
0495 //*************************************************************
0496 void WorksheetDock::worksheetDescriptionChanged(const AbstractAspect* aspect) {
0497     if (m_worksheet != aspect)
0498         return;
0499 
0500     CONDITIONAL_LOCK_RETURN;
0501     if (aspect->name() != ui.leName->text())
0502         ui.leName->setText(aspect->name());
0503     else if (aspect->comment() != ui.teComment->text())
0504         ui.teComment->setText(aspect->comment());
0505 }
0506 
0507 void WorksheetDock::worksheetScaleContentChanged(bool scaled) {
0508     CONDITIONAL_LOCK_RETURN;
0509     ui.chScaleContent->setChecked(scaled);
0510 }
0511 void WorksheetDock::worksheetUseViewSizeChanged(bool useViewSize) {
0512     CONDITIONAL_LOCK_RETURN;
0513     if (useViewSize)
0514         ui.cbSizeType->setCurrentIndex(0);
0515     else
0516         updatePaperSize();
0517 }
0518 void WorksheetDock::worksheetPageRectChanged(const QRectF& rect) {
0519     CONDITIONAL_LOCK_RETURN;
0520     ui.sbWidth->setValue(Worksheet::convertFromSceneUnits(rect.width(), m_worksheetUnit));
0521     ui.sbHeight->setValue(Worksheet::convertFromSceneUnits(rect.height(), m_worksheetUnit));
0522     updatePaperSize();
0523 }
0524 
0525 void WorksheetDock::worksheetLayoutChanged(Worksheet::Layout layout) {
0526     CONDITIONAL_LOCK_RETURN;
0527     ui.cbLayout->setCurrentIndex(static_cast<int>(layout));
0528 }
0529 
0530 void WorksheetDock::worksheetLayoutTopMarginChanged(double value) {
0531     CONDITIONAL_LOCK_RETURN;
0532     ui.sbLayoutTopMargin->setValue(Worksheet::convertFromSceneUnits(value, m_worksheetUnit));
0533 }
0534 
0535 void WorksheetDock::worksheetLayoutBottomMarginChanged(double value) {
0536     CONDITIONAL_LOCK_RETURN;
0537     ui.sbLayoutBottomMargin->setValue(Worksheet::convertFromSceneUnits(value, m_worksheetUnit));
0538 }
0539 
0540 void WorksheetDock::worksheetLayoutLeftMarginChanged(double value) {
0541     CONDITIONAL_LOCK_RETURN;
0542     ui.sbLayoutLeftMargin->setValue(Worksheet::convertFromSceneUnits(value, m_worksheetUnit));
0543 }
0544 
0545 void WorksheetDock::worksheetLayoutRightMarginChanged(double value) {
0546     CONDITIONAL_LOCK_RETURN;
0547     ui.sbLayoutRightMargin->setValue(Worksheet::convertFromSceneUnits(value, m_worksheetUnit));
0548 }
0549 
0550 void WorksheetDock::worksheetLayoutVerticalSpacingChanged(double value) {
0551     CONDITIONAL_LOCK_RETURN;
0552     ui.sbLayoutVerticalSpacing->setValue(Worksheet::convertFromSceneUnits(value, m_worksheetUnit));
0553 }
0554 
0555 void WorksheetDock::worksheetLayoutHorizontalSpacingChanged(double value) {
0556     CONDITIONAL_LOCK_RETURN;
0557     ui.sbLayoutHorizontalSpacing->setValue(Worksheet::convertFromSceneUnits(value, m_worksheetUnit));
0558 }
0559 
0560 void WorksheetDock::worksheetLayoutRowCountChanged(int value) {
0561     CONDITIONAL_LOCK_RETURN;
0562     ui.sbLayoutRowCount->setValue(value);
0563 }
0564 
0565 void WorksheetDock::worksheetLayoutColumnCountChanged(int value) {
0566     CONDITIONAL_LOCK_RETURN;
0567     ui.sbLayoutColumnCount->setValue(value);
0568 }
0569 
0570 //*************************************************************
0571 //******************** SETTINGS *******************************
0572 //*************************************************************
0573 void WorksheetDock::load() {
0574     // Geometry
0575     ui.chScaleContent->setChecked(m_worksheet->scaleContent());
0576     ui.sbWidth->setValue(Worksheet::convertFromSceneUnits(m_worksheet->pageRect().width(), m_worksheetUnit));
0577     ui.sbHeight->setValue(Worksheet::convertFromSceneUnits(m_worksheet->pageRect().height(), m_worksheetUnit));
0578     updatePaperSize();
0579 
0580     // Background
0581     QList<Background*> backgrounds;
0582     for (auto* worksheet : m_worksheetList)
0583         backgrounds << worksheet->background();
0584 
0585     backgroundWidget->setBackgrounds(backgrounds);
0586 
0587     // Layout
0588     ui.cbLayout->setCurrentIndex((int)m_worksheet->layout());
0589     ui.sbLayoutTopMargin->setValue(Worksheet::convertFromSceneUnits(m_worksheet->layoutTopMargin(), m_worksheetUnit));
0590     ui.sbLayoutBottomMargin->setValue(Worksheet::convertFromSceneUnits(m_worksheet->layoutBottomMargin(), m_worksheetUnit));
0591     ui.sbLayoutLeftMargin->setValue(Worksheet::convertFromSceneUnits(m_worksheet->layoutLeftMargin(), m_worksheetUnit));
0592     ui.sbLayoutRightMargin->setValue(Worksheet::convertFromSceneUnits(m_worksheet->layoutRightMargin(), m_worksheetUnit));
0593     ui.sbLayoutHorizontalSpacing->setValue(Worksheet::convertFromSceneUnits(m_worksheet->layoutHorizontalSpacing(), m_worksheetUnit));
0594     ui.sbLayoutVerticalSpacing->setValue(Worksheet::convertFromSceneUnits(m_worksheet->layoutVerticalSpacing(), m_worksheetUnit));
0595 
0596     ui.sbLayoutRowCount->setValue(m_worksheet->layoutRowCount());
0597     ui.sbLayoutColumnCount->setValue(m_worksheet->layoutColumnCount());
0598 }
0599 
0600 void WorksheetDock::loadConfigFromTemplate(KConfig& config) {
0601     auto name = TemplateHandler::templateName(config);
0602     int size = m_worksheetList.size();
0603     if (size > 1)
0604         m_worksheet->beginMacro(i18n("%1 worksheets: template \"%2\" loaded", size, name));
0605     else
0606         m_worksheet->beginMacro(i18n("%1: template \"%2\" loaded", m_worksheet->name(), name));
0607 
0608     this->loadConfig(config);
0609     m_worksheet->endMacro();
0610 }
0611 
0612 void WorksheetDock::loadConfig(KConfig& config) {
0613     KConfigGroup group = config.group(QStringLiteral("Worksheet"));
0614 
0615     // Geometry
0616     ui.chScaleContent->setChecked(group.readEntry(QStringLiteral("ScaleContent"), false));
0617     ui.sbWidth->setValue(Worksheet::convertFromSceneUnits(group.readEntry(QStringLiteral("Width"), m_worksheet->pageRect().width()), m_worksheetUnit));
0618     ui.sbHeight->setValue(Worksheet::convertFromSceneUnits(group.readEntry(QStringLiteral("Height"), m_worksheet->pageRect().height()), m_worksheetUnit));
0619     if (group.readEntry(QStringLiteral("UseViewSize"), false))
0620         ui.cbSizeType->setCurrentIndex(0);
0621     else
0622         updatePaperSize();
0623 
0624     // Background
0625     backgroundWidget->loadConfig(group);
0626 
0627     // Layout
0628     ui.cbLayout->setCurrentIndex(group.readEntry(QStringLiteral("Layout"), (int)m_worksheet->layout()));
0629     ui.sbLayoutTopMargin->setValue(
0630         Worksheet::convertFromSceneUnits(group.readEntry(QStringLiteral("LayoutTopMargin"), m_worksheet->layoutTopMargin()), m_worksheetUnit));
0631     ui.sbLayoutBottomMargin->setValue(
0632         Worksheet::convertFromSceneUnits(group.readEntry(QStringLiteral("LayoutBottomMargin"), m_worksheet->layoutBottomMargin()), m_worksheetUnit));
0633     ui.sbLayoutLeftMargin->setValue(
0634         Worksheet::convertFromSceneUnits(group.readEntry(QStringLiteral("LayoutLeftMargin"), m_worksheet->layoutLeftMargin()), m_worksheetUnit));
0635     ui.sbLayoutRightMargin->setValue(
0636         Worksheet::convertFromSceneUnits(group.readEntry(QStringLiteral("LayoutRightMargin"), m_worksheet->layoutRightMargin()), m_worksheetUnit));
0637     ui.sbLayoutHorizontalSpacing->setValue(
0638         Worksheet::convertFromSceneUnits(group.readEntry(QStringLiteral("LayoutHorizontalSpacing"), m_worksheet->layoutHorizontalSpacing()), m_worksheetUnit));
0639     ui.sbLayoutVerticalSpacing->setValue(
0640         Worksheet::convertFromSceneUnits(group.readEntry(QStringLiteral("LayoutVerticalSpacing"), m_worksheet->layoutVerticalSpacing()), m_worksheetUnit));
0641 
0642     ui.sbLayoutRowCount->setValue(group.readEntry(QStringLiteral("LayoutRowCount"), m_worksheet->layoutRowCount()));
0643     ui.sbLayoutColumnCount->setValue(group.readEntry(QStringLiteral("LayoutColumnCount"), m_worksheet->layoutColumnCount()));
0644 }
0645 
0646 void WorksheetDock::saveConfigAsTemplate(KConfig& config) {
0647     KConfigGroup group = config.group(QStringLiteral("Worksheet"));
0648 
0649     // General
0650     group.writeEntry(QStringLiteral("ScaleContent"), ui.chScaleContent->isChecked());
0651     group.writeEntry(QStringLiteral("UseViewSize"), ui.cbSizeType->currentIndex() == 0);
0652     group.writeEntry(QStringLiteral("Width"), Worksheet::convertToSceneUnits(ui.sbWidth->value(), m_worksheetUnit));
0653     group.writeEntry(QStringLiteral("Height"), Worksheet::convertToSceneUnits(ui.sbHeight->value(), m_worksheetUnit));
0654 
0655     // Background
0656     backgroundWidget->saveConfig(group);
0657 
0658     // Layout
0659     group.writeEntry(QStringLiteral("Layout"), ui.cbLayout->currentIndex());
0660     group.writeEntry(QStringLiteral("LayoutTopMargin"), Worksheet::convertToSceneUnits(ui.sbLayoutTopMargin->value(), m_worksheetUnit));
0661     group.writeEntry(QStringLiteral("LayoutBottomMargin"), Worksheet::convertToSceneUnits(ui.sbLayoutBottomMargin->value(), m_worksheetUnit));
0662     group.writeEntry(QStringLiteral("LayoutLeftMargin"), Worksheet::convertToSceneUnits(ui.sbLayoutLeftMargin->value(), m_worksheetUnit));
0663     group.writeEntry(QStringLiteral("LayoutRightMargin"), Worksheet::convertToSceneUnits(ui.sbLayoutRightMargin->value(), m_worksheetUnit));
0664     group.writeEntry(QStringLiteral("LayoutVerticalSpacing"), Worksheet::convertToSceneUnits(ui.sbLayoutVerticalSpacing->value(), m_worksheetUnit));
0665     group.writeEntry(QStringLiteral("LayoutHorizontalSpacing"), Worksheet::convertToSceneUnits(ui.sbLayoutHorizontalSpacing->value(), m_worksheetUnit));
0666     group.writeEntry(QStringLiteral("LayoutRowCount"), ui.sbLayoutRowCount->value());
0667     group.writeEntry(QStringLiteral("LayoutColumnCount"), ui.sbLayoutColumnCount->value());
0668 
0669     config.sync();
0670 }
0671 
0672 void WorksheetDock::loadTheme(const QString& theme) {
0673     for (auto* worksheet : m_worksheetList)
0674         worksheet->setTheme(theme);
0675 }