File indexing completed on 2024-05-12 03:47:26

0001 /*
0002     File                 : abstractcolumncommands.cpp
0003     Project              : LabPlot
0004     Description          : Commands to be called by AbstractColumn to modify AbstractColumnPrivate
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2007-2009 Tilman Benkert <thzs@gmx.net>
0007     SPDX-FileCopyrightText: 2010 Knut Franke <knut.franke@gmx.de>
0008     SPDX-FileCopyrightText: 2014-2021 Alexander Semke <alexander.semke@web.de>
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 #include "abstractcolumncommands.h"
0013 #include <KLocalizedString>
0014 
0015 /** ***************************************************************************
0016  * \class AbstractColumnClearMasksCmd
0017  * \brief Clear masking information
0018  ** ***************************************************************************/
0019 
0020 /**
0021  * \var AbstractColumnClearMasksCmd::m_col
0022  * \brief The private column data to modify
0023  */
0024 
0025 /**
0026  * \var AbstractColumnClearMasksCmd::m_masking
0027  * \brief The old masks
0028  */
0029 
0030 /**
0031  * \var AbstractColumnClearMasksCmd::m_copied
0032  * \brief A status flag
0033  */
0034 
0035 /**
0036  * \brief Ctor
0037  */
0038 AbstractColumnClearMasksCmd::AbstractColumnClearMasksCmd(AbstractColumnPrivate* col, QUndoCommand* parent)
0039     : QUndoCommand(parent)
0040     , m_col(col) {
0041     setText(i18n("%1: clear masks", col->name()));
0042     m_copied = false;
0043 }
0044 
0045 /**
0046  * \brief Dtor
0047  */
0048 AbstractColumnClearMasksCmd::~AbstractColumnClearMasksCmd() = default;
0049 
0050 /**
0051  * \brief Execute the command
0052  */
0053 void AbstractColumnClearMasksCmd::redo() {
0054     if (!m_copied) {
0055         m_masking = m_col->m_masking;
0056         m_copied = true;
0057     }
0058     m_col->m_masking.clear();
0059     finalize();
0060 }
0061 
0062 /**
0063  * \brief Undo the command
0064  */
0065 void AbstractColumnClearMasksCmd::undo() {
0066     m_col->m_masking = m_masking;
0067     finalize();
0068 }
0069 
0070 void AbstractColumnClearMasksCmd::finalize() const {
0071     // TODO: implement AbstractColumn::setChanged() instead of these two calls,
0072     // move the already available Column::setChanged to the base class.
0073     Q_EMIT m_col->owner()->dataChanged(m_col->owner());
0074     m_col->owner()->invalidateProperties();
0075 }
0076 
0077 /** ***************************************************************************
0078  * \class AbstractColumnSetMaskedCmd
0079  * \brief Mark an interval of rows as masked
0080  ** ***************************************************************************/
0081 
0082 /**
0083  * \var AbstractColumnSetMaskedCmd::m_col
0084  * \brief The private AbstractColumn data to modify
0085  */
0086 
0087 /**
0088  * \var AbstractColumnSetMaskedCmd::m_interval
0089  * \brief The interval
0090  */
0091 
0092 /**
0093  * \var AbstractColumnSetMaskedCmd::m_masked
0094  * \brief Mask/unmask flag
0095  */
0096 
0097 /**
0098  * \var AbstractColumnSetMaskedCmd::m_masking
0099  * \brief Interval attribute backup
0100  */
0101 
0102 /**
0103  * \var AbstractColumnSetMaskedCmd::m_copied
0104  * \brief A status flag
0105  */
0106 
0107 /**
0108  * \brief Ctor
0109  */
0110 AbstractColumnSetMaskedCmd::AbstractColumnSetMaskedCmd(AbstractColumnPrivate* col, const Interval<int>& interval, bool masked, QUndoCommand* parent)
0111     : QUndoCommand(parent)
0112     , m_col(col)
0113     , m_interval(interval)
0114     , m_masked(masked) {
0115     if (masked)
0116         setText(i18n("%1: mask cells", col->name()));
0117     else
0118         setText(i18n("%1: unmask cells", col->name()));
0119     m_copied = false;
0120 }
0121 
0122 /**
0123  * \brief Dtor
0124  */
0125 AbstractColumnSetMaskedCmd::~AbstractColumnSetMaskedCmd() = default;
0126 
0127 /**
0128  * \brief Execute the command
0129  */
0130 void AbstractColumnSetMaskedCmd::redo() {
0131     if (!m_copied) {
0132         m_masking = m_col->m_masking;
0133         m_copied = true;
0134     }
0135     m_col->m_masking.setValue(m_interval, m_masked);
0136     finalize();
0137 }
0138 
0139 /**
0140  * \brief Undo the command
0141  */
0142 void AbstractColumnSetMaskedCmd::undo() {
0143     m_col->m_masking = m_masking;
0144     finalize();
0145 }
0146 
0147 void AbstractColumnSetMaskedCmd::finalize() const {
0148     // TODO: implement AbstractColumn::setChanged() instead of these two calls,
0149     // move the already available Column::setChanged to the base class.
0150     Q_EMIT m_col->owner()->dataChanged(m_col->owner());
0151     m_col->owner()->invalidateProperties();
0152 }
0153 
0154 /** ***************************************************************************
0155  * \class AbstractColumnInsertRowsCmd
0156  * \brief Insert empty rows into a column
0157  ** ***************************************************************************/
0158 
0159 /**
0160  * \var AbstractColumnInsertRowsCmd::m_col
0161  * \brief Private object of AbstractColumn to be modified.
0162  */
0163 
0164 /**
0165  * \var AbstractColumnInsertRowsCmd::m_before
0166  * \brief Row number before which to insert the new rows.
0167  */
0168 
0169 /**
0170  * \var AbstractColumnInsertRowsCmd::m_count
0171  * \brief Number of rows to be inserted.
0172  */
0173 
0174 /**
0175  * \brief Ctor
0176  */
0177 AbstractColumnInsertRowsCmd::AbstractColumnInsertRowsCmd(AbstractColumn* col, int before, int count, QUndoCommand* parent)
0178     : QUndoCommand(parent)
0179     , m_col(col->d)
0180     , m_before(before)
0181     , m_count(count) {
0182 }
0183 
0184 /**
0185  * \brief Dtor
0186  */
0187 AbstractColumnInsertRowsCmd::~AbstractColumnInsertRowsCmd() = default;
0188 
0189 void AbstractColumnInsertRowsCmd::redo() {
0190     m_col->m_masking.insertRows(m_before, m_count);
0191 }
0192 
0193 void AbstractColumnInsertRowsCmd::undo() {
0194     m_col->m_masking.removeRows(m_before, m_count);
0195 }
0196 
0197 /** ***************************************************************************
0198  * \class AbstractColumnRemoveRowsCmd
0199  * \brief Remove rows from a column
0200  *
0201  * See AbstractColumnInsertRowsCmd for a discussion of the design.
0202  ** ***************************************************************************/
0203 
0204 /**
0205  * \var AbstractColumnRemoveRowsCmd::m_col
0206  * \brief Private object of AbstractColumn to be modified.
0207  */
0208 
0209 /**
0210  * \var AbstractColumnRemoveRowsCmd::m_first
0211  * \brief First row number to be removed.
0212  */
0213 
0214 /**
0215  * \var AbstractColumnRemoveRowsCmd::m_count
0216  * \brief Number of rows to be removed.
0217  */
0218 
0219 /**
0220  * \brief Ctor
0221  */
0222 AbstractColumnRemoveRowsCmd::AbstractColumnRemoveRowsCmd(AbstractColumn* col, int first, int count, QUndoCommand* parent)
0223     : QUndoCommand(parent)
0224     , m_col(col->d)
0225     , m_first(first)
0226     , m_count(count) {
0227 }
0228 
0229 /**
0230  * \brief Dtor
0231  */
0232 AbstractColumnRemoveRowsCmd::~AbstractColumnRemoveRowsCmd() = default;
0233 
0234 void AbstractColumnRemoveRowsCmd::redo() {
0235     m_masking = m_col->m_masking;
0236     m_col->m_masking.removeRows(m_first, m_count);
0237 }
0238 
0239 void AbstractColumnRemoveRowsCmd::undo() {
0240     m_col->m_masking = m_masking;
0241 }
0242 
0243 /** ***************************************************************************
0244  * \class AbstractColumnSetHeatmapFormatCmd
0245  * \brief Set the heatmap format
0246  ** ***************************************************************************/
0247 AbstractColumnSetHeatmapFormatCmd::AbstractColumnSetHeatmapFormatCmd(AbstractColumnPrivate* col,
0248                                                                      const AbstractColumn::HeatmapFormat& format,
0249                                                                      QUndoCommand* parent)
0250     : QUndoCommand(parent)
0251     , m_col(col)
0252     , m_format(format) {
0253     setText(i18n("%1: set heatmap format", col->name()));
0254 }
0255 
0256 AbstractColumnSetHeatmapFormatCmd::~AbstractColumnSetHeatmapFormatCmd() = default;
0257 
0258 void AbstractColumnSetHeatmapFormatCmd::redo() {
0259     if (!m_col->m_heatmapFormat)
0260         m_col->m_heatmapFormat = new AbstractColumn::HeatmapFormat();
0261 
0262     AbstractColumn::HeatmapFormat tmp = *(m_col->m_heatmapFormat);
0263     *(m_col->m_heatmapFormat) = m_format;
0264     m_format = tmp;
0265 
0266     Q_EMIT m_col->owner()->formatChanged(m_col->owner());
0267 }
0268 
0269 void AbstractColumnSetHeatmapFormatCmd::undo() {
0270     redo();
0271 }
0272 
0273 /** ***************************************************************************
0274  * \class AbstractColumnRemoveHeatmapFormatCmd
0275  * \brief Set the heatmap format
0276  ** ***************************************************************************/
0277 AbstractColumnRemoveHeatmapFormatCmd::AbstractColumnRemoveHeatmapFormatCmd(AbstractColumnPrivate* col, QUndoCommand* parent)
0278     : QUndoCommand(parent)
0279     , m_col(col) {
0280     setText(i18n("%1: remove heatmap format", col->name()));
0281 }
0282 
0283 AbstractColumnRemoveHeatmapFormatCmd::~AbstractColumnRemoveHeatmapFormatCmd() = default;
0284 
0285 void AbstractColumnRemoveHeatmapFormatCmd::redo() {
0286     if (m_col->m_heatmapFormat) {
0287         m_format = *(m_col->m_heatmapFormat);
0288         delete m_col->m_heatmapFormat;
0289         m_col->m_heatmapFormat = nullptr;
0290     }
0291 
0292     Q_EMIT m_col->owner()->formatChanged(m_col->owner());
0293 }
0294 
0295 void AbstractColumnRemoveHeatmapFormatCmd::undo() {
0296     if (!m_col->m_heatmapFormat)
0297         m_col->m_heatmapFormat = new AbstractColumn::HeatmapFormat();
0298 
0299     *(m_col->m_heatmapFormat) = m_format;
0300 
0301     Q_EMIT m_col->owner()->formatChanged(m_col->owner());
0302 }