diff --git a/src/backend/matrix/Matrix.cpp b/src/backend/matrix/Matrix.cpp index 3676644f0..0364d9447 100644 --- a/src/backend/matrix/Matrix.cpp +++ b/src/backend/matrix/Matrix.cpp @@ -1,1293 +1,1293 @@ /*************************************************************************** File : Matrix.cpp Project : Matrix Description : Spreadsheet with a MxN matrix data model -------------------------------------------------------------------- Copyright : (C) 2008-2009 Tilman Benkert (thzs@gmx.net) Copyright : (C) 2015-2017 Alexander Semke (alexander.semke@web.de) Copyright : (C) 2017-2018 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "Matrix.h" #include "MatrixPrivate.h" #include "matrixcommands.h" #include "backend/matrix/MatrixModel.h" #include "backend/core/Folder.h" #include "backend/lib/commandtemplates.h" #include "backend/lib/XmlStreamReader.h" #include "commonfrontend/matrix/MatrixView.h" #include "kdefrontend/spreadsheet/ExportSpreadsheetDialog.h" #include #include #include #include #include #include #include /*! This class manages matrix based data (i.e., mathematically a MxN matrix with M rows, N columns). This data is typically used to for 3D plots. The values of the matrix are stored as generic values. Each column of the matrix is stored in a QVector objects. \ingroup backend */ Matrix::Matrix(int rows, int cols, const QString& name, const AbstractColumn::ColumnMode mode) : AbstractDataSource(name), d(new MatrixPrivate(this, mode)), m_model(nullptr), m_view(nullptr) { //set initial number of rows and columns appendColumns(cols); appendRows(rows); init(); } Matrix::Matrix(const QString& name, bool loading, const AbstractColumn::ColumnMode mode) : AbstractDataSource(name), d(new MatrixPrivate(this, mode)), m_model(nullptr), m_view(nullptr) { if (!loading) init(); } Matrix::~Matrix() { delete d; } void Matrix::init() { KConfig config; KConfigGroup group = config.group("Matrix"); //matrix dimension int rows = group.readEntry("RowCount", 10); int cols = group.readEntry("ColumnCount", 10); appendRows(rows); appendColumns(cols); //mapping to logical x- and y-coordinates d->xStart = group.readEntry("XStart", 0.0); d->xEnd = group.readEntry("XEnd", 1.0); d->yStart = group.readEntry("YStart", 0.0); d->yEnd = group.readEntry("YEnd", 1.0); //format QByteArray formatba = group.readEntry("NumericFormat", "f").toLatin1(); d->numericFormat = *formatba.data(); d->precision = group.readEntry("Precision", 3); d->headerFormat = (Matrix::HeaderFormat)group.readEntry("HeaderFormat", (int)Matrix::HeaderRowsColumns); } /*! Returns an icon to be used for decorating my views. */ QIcon Matrix::icon() const { return QIcon::fromTheme("labplot-matrix"); } /*! Returns a new context menu. The caller takes ownership of the menu. */ QMenu* Matrix::createContextMenu() { QMenu* menu = AbstractPart::createContextMenu(); emit requestProjectContextMenu(menu); return menu; } QWidget* Matrix::view() const { if (!m_partView) { m_view= new MatrixView(const_cast(this)); m_partView = m_view; m_model = m_view->model(); } return m_partView; } bool Matrix::exportView() const { auto* dlg = new ExportSpreadsheetDialog(m_view); dlg->setFileName(name()); dlg->setMatrixMode(true); //TODO FITS filter to decide if it can be exported to both dlg->setExportTo(QStringList() << i18n("FITS image") << i18n("FITS table")); if (m_view->selectedColumnCount() == 0) { dlg->setExportSelection(false); } bool ret; if ( (ret = (dlg->exec() == QDialog::Accepted)) ) { const QString path = dlg->path(); WAIT_CURSOR; if (dlg->format() == ExportSpreadsheetDialog::LaTeX) { const bool verticalHeader = dlg->matrixVerticalHeader(); const bool horizontalHeader = dlg->matrixHorizontalHeader(); const bool latexHeader = dlg->exportHeader(); const bool gridLines = dlg->gridLines(); const bool entire = dlg->entireSpreadheet(); const bool captions = dlg->captions(); m_view->exportToLaTeX(path, verticalHeader, horizontalHeader, latexHeader, gridLines, entire, captions); } else if (dlg->format() == ExportSpreadsheetDialog::FITS) { const int exportTo = dlg->exportToFits(); m_view->exportToFits(path, exportTo ); } else { const QString separator = dlg->separator(); const QLocale::Language format = dlg->numberFormat(); m_view->exportToFile(path, separator, format); } RESET_CURSOR; } delete dlg; return ret; } bool Matrix::printView() { QPrinter printer; auto* dlg = new QPrintDialog(&printer, m_view); bool ret; dlg->setWindowTitle(i18nc("@title:window", "Print Matrix")); if ( (ret = (dlg->exec() == QDialog::Accepted)) ) m_view->print(&printer); delete dlg; return ret; } bool Matrix::printPreview() const { QPrintPreviewDialog* dlg = new QPrintPreviewDialog(m_view); connect(dlg, &QPrintPreviewDialog::paintRequested, m_view, &MatrixView::print); return dlg->exec(); } //############################################################################## //########################## getter methods ################################## //############################################################################## void* Matrix::data() const { return d->data; } BASIC_D_READER_IMPL(Matrix, AbstractColumn::ColumnMode, mode, mode) BASIC_D_READER_IMPL(Matrix, int, rowCount, rowCount) BASIC_D_READER_IMPL(Matrix, int, columnCount, columnCount) BASIC_D_READER_IMPL(Matrix, double, xStart, xStart) BASIC_D_READER_IMPL(Matrix, double, xEnd, xEnd) BASIC_D_READER_IMPL(Matrix, double, yStart, yStart) BASIC_D_READER_IMPL(Matrix, double, yEnd, yEnd) BASIC_D_READER_IMPL(Matrix, char, numericFormat, numericFormat) BASIC_D_READER_IMPL(Matrix, int, precision, precision) BASIC_D_READER_IMPL(Matrix, Matrix::HeaderFormat, headerFormat, headerFormat) CLASS_D_READER_IMPL(Matrix, QString, formula, formula) void Matrix::setSuppressDataChangedSignal(bool b) { if (m_model) m_model->setSuppressDataChangedSignal(b); } void Matrix::setChanged() { if (m_model) m_model->setChanged(); } //############################################################################## //################# setter methods and undo commands ########################## //############################################################################## void Matrix::setRowCount(int count) { if (count == d->rowCount) return; const int diff = count - d->rowCount; if (diff > 0) appendRows(diff); else if (diff < 0) removeRows(rowCount() + diff, -diff); } void Matrix::setColumnCount(int count) { if (count == d->columnCount) return; const int diff = count - columnCount(); if (diff > 0) appendColumns(diff); else if (diff < 0) removeColumns(columnCount() + diff, -diff); } STD_SETTER_CMD_IMPL_F_S(Matrix, SetXStart, double, xStart, updateViewHeader) void Matrix::setXStart(double xStart) { if (xStart != d->xStart) exec(new MatrixSetXStartCmd(d, xStart, ki18n("%1: x-start changed"))); } STD_SETTER_CMD_IMPL_F_S(Matrix, SetXEnd, double, xEnd, updateViewHeader) void Matrix::setXEnd(double xEnd) { if (xEnd != d->xEnd) exec(new MatrixSetXEndCmd(d, xEnd, ki18n("%1: x-end changed"))); } STD_SETTER_CMD_IMPL_F_S(Matrix, SetYStart, double, yStart, updateViewHeader) void Matrix::setYStart(double yStart) { if (yStart != d->yStart) exec(new MatrixSetYStartCmd(d, yStart, ki18n("%1: y-start changed"))); } STD_SETTER_CMD_IMPL_F_S(Matrix, SetYEnd, double, yEnd, updateViewHeader) void Matrix::setYEnd(double yEnd) { if (yEnd != d->yEnd) exec(new MatrixSetYEndCmd(d, yEnd, ki18n("%1: y-end changed"))); } STD_SETTER_CMD_IMPL_S(Matrix, SetNumericFormat, char, numericFormat) void Matrix::setNumericFormat(char format) { if (format != d->numericFormat) exec(new MatrixSetNumericFormatCmd(d, format, ki18n("%1: numeric format changed"))); } STD_SETTER_CMD_IMPL_S(Matrix, SetPrecision, int, precision) void Matrix::setPrecision(int precision) { if (precision != d->precision) exec(new MatrixSetPrecisionCmd(d, precision, ki18n("%1: precision changed"))); } //TODO: make this undoable? void Matrix::setHeaderFormat(Matrix::HeaderFormat format) { d->headerFormat = format; m_model->updateHeader(); if (m_view) m_view->resizeHeaders(); emit headerFormatChanged(format); } //columns void Matrix::insertColumns(int before, int count) { if (count < 1 || before < 0 || before > columnCount()) return; WAIT_CURSOR; exec(new MatrixInsertColumnsCmd(d, before, count)); RESET_CURSOR; } void Matrix::appendColumns(int count) { insertColumns(columnCount(), count); } void Matrix::removeColumns(int first, int count) { if (count < 1 || first < 0 || first+count > columnCount()) return; WAIT_CURSOR; switch (d->mode) { case AbstractColumn::Numeric: exec(new MatrixRemoveColumnsCmd(d, first, count)); break; case AbstractColumn::Text: exec(new MatrixRemoveColumnsCmd(d, first, count)); break; case AbstractColumn::Integer: exec(new MatrixRemoveColumnsCmd(d, first, count)); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: exec(new MatrixRemoveColumnsCmd(d, first, count)); break; } RESET_CURSOR; } void Matrix::clearColumn(int c) { WAIT_CURSOR; switch (d->mode) { case AbstractColumn::Numeric: exec(new MatrixClearColumnCmd(d, c)); break; case AbstractColumn::Text: exec(new MatrixClearColumnCmd(d, c)); break; case AbstractColumn::Integer: exec(new MatrixClearColumnCmd(d, c)); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: exec(new MatrixClearColumnCmd(d, c)); break; } RESET_CURSOR; } //rows void Matrix::insertRows(int before, int count) { if (count < 1 || before < 0 || before > rowCount()) return; WAIT_CURSOR; exec(new MatrixInsertRowsCmd(d, before, count)); RESET_CURSOR; } void Matrix::appendRows(int count) { insertRows(rowCount(), count); } void Matrix::removeRows(int first, int count) { if (count < 1 || first < 0 || first+count > rowCount()) return; WAIT_CURSOR; switch (d->mode) { case AbstractColumn::Numeric: exec(new MatrixRemoveRowsCmd(d, first, count)); break; case AbstractColumn::Text: exec(new MatrixRemoveRowsCmd(d, first, count)); break; case AbstractColumn::Integer: exec(new MatrixRemoveRowsCmd(d, first, count)); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: exec(new MatrixRemoveRowsCmd(d, first, count)); break; } RESET_CURSOR; } void Matrix::clearRow(int r) { switch (d->mode) { case AbstractColumn::Numeric: for (int c = 0; c < columnCount(); ++c) exec(new MatrixSetCellValueCmd(d, r, c, 0.0)); break; case AbstractColumn::Text: for (int c = 0; c < columnCount(); ++c) exec(new MatrixSetCellValueCmd(d, r, c, QString())); break; case AbstractColumn::Integer: for (int c = 0; c < columnCount(); ++c) exec(new MatrixSetCellValueCmd(d, r, c, 0)); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: for (int c = 0; c < columnCount(); ++c) exec(new MatrixSetCellValueCmd(d, r, c, QDateTime())); break; } } //! Return the value in the given cell (needs explicit instantiation) template T Matrix::cell(int row, int col) const { return d->cell(row, col); } template double Matrix::cell(int row, int col) const; template int Matrix::cell(int row, int col) const; template QDateTime Matrix::cell(int row, int col) const; template QString Matrix::cell(int row, int col) const; //! Return the text displayed in the given cell (needs explicit instantiation) template QString Matrix::text(int row, int col) { return QLocale().toString(cell(row,col)); } // special cases template <> QString Matrix::text(int row, int col) { return QLocale().toString(cell(row,col), d->numericFormat, d->precision); } template <> QString Matrix::text(int row, int col) { return cell(row,col); } template QString Matrix::text(int row, int col); template QString Matrix::text(int row, int col); //! Set the value of the cell (needs explicit instantiation) template void Matrix::setCell(int row, int col, T value) { - if(row < 0 || row >= rowCount()) return; - if(col < 0 || col >= columnCount()) return; + if (row < 0 || row >= rowCount()) return; + if (col < 0 || col >= columnCount()) return; exec(new MatrixSetCellValueCmd(d, row, col, value)); } template void Matrix::setCell(int row, int col, double value); template void Matrix::setCell(int row, int col, int value); template void Matrix::setCell(int row, int col, QDateTime value); template void Matrix::setCell(int row, int col, QString value); void Matrix::clearCell(int row, int col) { switch (d->mode) { case AbstractColumn::Numeric: exec(new MatrixSetCellValueCmd(d, row, col, 0.0)); break; case AbstractColumn::Text: exec(new MatrixSetCellValueCmd(d, row, col, QString())); break; case AbstractColumn::Integer: exec(new MatrixSetCellValueCmd(d, row, col, 0)); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: exec(new MatrixSetCellValueCmd(d, row, col, QDateTime())); break; } } void Matrix::setDimensions(int rows, int cols) { if( (rows < 0) || (cols < 0 ) || (rows == rowCount() && cols == columnCount()) ) return; WAIT_CURSOR; beginMacro(i18n("%1: set matrix size to %2x%3", name(), rows, cols)); int col_diff = cols - columnCount(); if (col_diff > 0) insertColumns(columnCount(), col_diff); else if (col_diff < 0) removeColumns(columnCount() + col_diff, -col_diff); int row_diff = rows - rowCount(); if(row_diff > 0) appendRows(row_diff); else if (row_diff < 0) removeRows(rowCount() + row_diff, -row_diff); endMacro(); RESET_CURSOR; } void Matrix::copy(Matrix* other) { WAIT_CURSOR; beginMacro(i18n("%1: copy %2", name(), other->name())); int rows = other->rowCount(); int columns = other->columnCount(); setDimensions(rows, columns); for (int i=0; irowHeight(i)); for (int i=0; icolumnWidth(i)); d->suppressDataChange = true; switch (d->mode) { case AbstractColumn::Numeric: for (int i = 0; i < columns; i++) setColumnCells(i, 0, rows-1, other->columnCells(i, 0, rows-1)); break; case AbstractColumn::Text: for (int i = 0; i < columns; i++) setColumnCells(i, 0, rows-1, other->columnCells(i, 0, rows-1)); break; case AbstractColumn::Integer: for (int i = 0; i < columns; i++) setColumnCells(i, 0, rows-1, other->columnCells(i, 0, rows-1)); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: for (int i = 0; i < columns; i++) setColumnCells(i, 0, rows-1, other->columnCells(i, 0, rows-1)); break; } setCoordinates(other->xStart(), other->xEnd(), other->yStart(), other->yEnd()); setNumericFormat(other->numericFormat()); setPrecision(other->precision()); d->formula = other->formula(); d->suppressDataChange = false; emit dataChanged(0, 0, rows-1, columns-1); if (m_view) m_view->adjustHeaders(); endMacro(); RESET_CURSOR; } //! Duplicate the matrix inside its folder void Matrix::duplicate() { Matrix* matrix = new Matrix(rowCount(), columnCount(), name()); matrix->copy(this); if (folder()) folder()->addChild(matrix); } void Matrix::addRows() { if (!m_view) return; WAIT_CURSOR; int count = m_view->selectedRowCount(false); beginMacro(i18np("%1: add %2 rows", "%1: add %2 rows", name(), count)); exec(new MatrixInsertRowsCmd(d, rowCount(), count)); endMacro(); RESET_CURSOR; } void Matrix::addColumns() { if (!m_view) return; WAIT_CURSOR; int count = m_view->selectedRowCount(false); beginMacro(i18np("%1: add %2 column", "%1: add %2 columns", name(), count)); exec(new MatrixInsertColumnsCmd(d, columnCount(), count)); endMacro(); RESET_CURSOR; } void Matrix::setCoordinates(double x1, double x2, double y1, double y2) { exec(new MatrixSetCoordinatesCmd(d, x1, x2, y1, y2)); } void Matrix::setFormula(const QString& formula) { exec(new MatrixSetFormulaCmd(d, formula)); } //! This method should only be called by the view. /** This method does not change the view, it only changes the * values that are saved when the matrix is saved. The view * has to take care of reading and applying these values */ void Matrix::setRowHeight(int row, int height) { d->setRowHeight(row, height); } //! This method should only be called by the view. /** This method does not change the view, it only changes the * values that are saved when the matrix is saved. The view * has to take care of reading and applying these values */ void Matrix::setColumnWidth(int col, int width) { d->setColumnWidth(col, width); } int Matrix::rowHeight(int row) const { return d->rowHeight(row); } int Matrix::columnWidth(int col) const { return d->columnWidth(col); } //! Return the values in the given cells as vector template QVector Matrix::columnCells(int col, int first_row, int last_row) { return d->columnCells(col, first_row, last_row); } //! Set the values in the given cells from a type T vector template void Matrix::setColumnCells(int col, int first_row, int last_row, const QVector& values) { WAIT_CURSOR; exec(new MatrixSetColumnCellsCmd(d, col, first_row, last_row, values)); RESET_CURSOR; } //! Return the values in the given cells as vector (needs explicit instantiation) template QVector Matrix::rowCells(int row, int first_column, int last_column) { return d->rowCells(row, first_column, last_column); } template QVector Matrix::rowCells(int row, int first_column, int last_column); template QVector Matrix::rowCells(int row, int first_column, int last_column); template QVector Matrix::rowCells(int row, int first_column, int last_column); template QVector Matrix::rowCells(int row, int first_column, int last_column); //! Set the values in the given cells from a type T vector template void Matrix::setRowCells(int row, int first_column, int last_column, const QVector& values) { WAIT_CURSOR; exec(new MatrixSetRowCellsCmd(d, row, first_column, last_column, values)); RESET_CURSOR; } void Matrix::setData(void* data) { bool isEmpty = false; switch (d->mode) { case AbstractColumn::Numeric: if (static_cast>*>(data)->isEmpty()) isEmpty = true; break; case AbstractColumn::Text: if (static_cast>*>(data)->isEmpty()) isEmpty = true; break; case AbstractColumn::Integer: if (static_cast>*>(data)->isEmpty()) isEmpty = true; break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: if (static_cast>*>(data)->isEmpty()) isEmpty = true; break; } if (!isEmpty) exec(new MatrixReplaceValuesCmd(d, data)); } //############################################################################## //######################### Public slots ##################################### //############################################################################## //! Clear the whole matrix (i.e. reset all cells) void Matrix::clear() { WAIT_CURSOR; beginMacro(i18n("%1: clear", name())); switch (d->mode) { case AbstractColumn::Numeric: exec(new MatrixClearCmd(d)); break; case AbstractColumn::Text: exec(new MatrixClearCmd(d)); break; case AbstractColumn::Integer: exec(new MatrixClearCmd(d)); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: exec(new MatrixClearCmd(d)); break; } endMacro(); RESET_CURSOR; } void Matrix::transpose() { WAIT_CURSOR; switch (d->mode) { case AbstractColumn::Numeric: exec(new MatrixTransposeCmd(d)); break; case AbstractColumn::Text: exec(new MatrixTransposeCmd(d)); break; case AbstractColumn::Integer: exec(new MatrixTransposeCmd(d)); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: exec(new MatrixTransposeCmd(d)); break; } RESET_CURSOR; } void Matrix::mirrorHorizontally() { WAIT_CURSOR; switch (d->mode) { case AbstractColumn::Numeric: exec(new MatrixMirrorHorizontallyCmd(d)); break; case AbstractColumn::Text: exec(new MatrixMirrorHorizontallyCmd(d)); break; case AbstractColumn::Integer: exec(new MatrixMirrorHorizontallyCmd(d)); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: exec(new MatrixMirrorHorizontallyCmd(d)); break; } RESET_CURSOR; } void Matrix::mirrorVertically() { WAIT_CURSOR; switch (d->mode) { case AbstractColumn::Numeric: exec(new MatrixMirrorVerticallyCmd(d)); break; case AbstractColumn::Text: exec(new MatrixMirrorVerticallyCmd(d)); break; case AbstractColumn::Integer: exec(new MatrixMirrorVerticallyCmd(d)); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: exec(new MatrixMirrorVerticallyCmd(d)); break; } RESET_CURSOR; } //############################################################################## //###################### Private implementation ############################### //############################################################################## MatrixPrivate::MatrixPrivate(Matrix* owner, const AbstractColumn::ColumnMode m) : q(owner), data(nullptr), mode(m), rowCount(0), columnCount(0), suppressDataChange(false) { switch (mode) { case AbstractColumn::Numeric: data = new QVector>(); break; case AbstractColumn::Text: data = new QVector>(); break; case AbstractColumn::Month: case AbstractColumn::Day: case AbstractColumn::DateTime: data = new QVector>(); break; case AbstractColumn::Integer: data = new QVector>(); break; } } MatrixPrivate::~MatrixPrivate() { if (data) { switch (mode) { case AbstractColumn::Numeric: delete static_cast>*>(data); break; case AbstractColumn::Text: delete static_cast>*>(data); break; case AbstractColumn::Integer: delete static_cast>*>(data); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: delete static_cast>*>(data); break; } } } void MatrixPrivate::updateViewHeader() { q->m_view->model()->updateHeader(); } /*! Insert \count columns before column number \c before */ void MatrixPrivate::insertColumns(int before, int count) { Q_ASSERT(before >= 0); Q_ASSERT(before <= columnCount); emit q->columnsAboutToBeInserted(before, count); switch (mode) { case AbstractColumn::Numeric: for (int i = 0; i < count; i++) { static_cast>*>(data)->insert(before+i, QVector(rowCount)); columnWidths.insert(before+i, 0); } break; case AbstractColumn::Text: for (int i = 0; i < count; i++) { static_cast>*>(data)->insert(before+i, QVector(rowCount)); columnWidths.insert(before+i, 0); } break; case AbstractColumn::Integer: for (int i = 0; i < count; i++) { static_cast>*>(data)->insert(before+i, QVector(rowCount)); columnWidths.insert(before+i, 0); } break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: for (int i = 0; i < count; i++) { static_cast>*>(data)->insert(before+i, QVector(rowCount)); columnWidths.insert(before+i, 0); } break; } columnCount += count; emit q->columnsInserted(before, count); } /*! Remove \c count columns starting with column index \c first */ void MatrixPrivate::removeColumns(int first, int count) { emit q->columnsAboutToBeRemoved(first, count); Q_ASSERT(first >= 0); Q_ASSERT(first + count <= columnCount); switch (mode) { case AbstractColumn::Numeric: (static_cast>*>(data))->remove(first, count); break; case AbstractColumn::Text: (static_cast>*>(data))->remove(first, count); break; case AbstractColumn::Integer: (static_cast>*>(data))->remove(first, count); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: (static_cast>*>(data))->remove(first, count); break; } for (int i = 0; i < count; i++) columnWidths.remove(first); columnCount -= count; emit q->columnsRemoved(first, count); } /*! Insert \c count rows before row with the index \c before */ void MatrixPrivate::insertRows(int before, int count) { emit q->rowsAboutToBeInserted(before, count); Q_ASSERT(before >= 0); Q_ASSERT(before <= rowCount); switch (mode) { case AbstractColumn::Numeric: for (int col = 0; col < columnCount; col++) for (int i = 0; i < count; i++) (static_cast>*>(data))->operator[](col).insert(before+i, 0.0); break; case AbstractColumn::Text: for (int col = 0; col < columnCount; col++) for (int i = 0; i < count; i++) (static_cast>*>(data))->operator[](col).insert(before+i, QString()); break; case AbstractColumn::Integer: for (int col = 0; col < columnCount; col++) for (int i = 0; i < count; i++) (static_cast>*>(data))->operator[](col).insert(before+i, 0); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: for (int col = 0; col < columnCount; col++) for (int i = 0; i < count; i++) (static_cast>*>(data))->operator[](col).insert(before+i, QDateTime()); } for(int i=0; irowsInserted(before, count); } /*! Remove \c count columns starting from the column with index \c first */ void MatrixPrivate::removeRows(int first, int count) { emit q->rowsAboutToBeRemoved(first, count); Q_ASSERT(first >= 0); Q_ASSERT(first+count <= rowCount); switch (mode) { case AbstractColumn::Numeric: for (int col = 0; col < columnCount; col++) (static_cast>*>(data))->operator[](col).remove(first, count); break; case AbstractColumn::Text: for (int col = 0; col < columnCount; col++) (static_cast>*>(data))->operator[](col).remove(first, count); break; case AbstractColumn::Integer: for (int col = 0; col < columnCount; col++) (static_cast>*>(data))->operator[](col).remove(first, count); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: for (int col = 0; col < columnCount; col++) (static_cast>*>(data))->operator[](col).remove(first, count); break; } for (int i = 0; i < count; i++) rowHeights.remove(first); rowCount -= count; emit q->rowsRemoved(first, count); } //! Fill column with zeroes void MatrixPrivate::clearColumn(int col) { switch (mode) { case AbstractColumn::Numeric: static_cast>*>(data)->operator[](col).fill(0.0); break; case AbstractColumn::Text: static_cast>*>(data)->operator[](col).fill(QString()); break; case AbstractColumn::Integer: static_cast>*>(data)->operator[](col).fill(0); break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: static_cast>*>(data)->operator[](col).fill(QDateTime()); break; } if (!suppressDataChange) emit q->dataChanged(0, col, rowCount-1, col); } //############################################################################## //################## Serialization/Deserialization ########################### //############################################################################## void Matrix::save(QXmlStreamWriter* writer) const { DEBUG("Matrix::save()"); writer->writeStartElement("matrix"); writeBasicAttributes(writer); writeCommentElement(writer); //formula writer->writeStartElement("formula"); writer->writeCharacters(d->formula); writer->writeEndElement(); //format writer->writeStartElement("format"); writer->writeAttribute("mode", QString::number(d->mode)); writer->writeAttribute("headerFormat", QString::number(d->headerFormat)); writer->writeAttribute("numericFormat", QString(QChar(d->numericFormat))); writer->writeAttribute("precision", QString::number(d->precision)); writer->writeEndElement(); //dimensions writer->writeStartElement("dimension"); writer->writeAttribute("columns", QString::number(d->columnCount)); writer->writeAttribute("rows", QString::number(d->rowCount)); writer->writeAttribute("x_start", QString::number(d->xStart)); writer->writeAttribute("x_end", QString::number(d->xEnd)); writer->writeAttribute("y_start", QString::number(d->yStart)); writer->writeAttribute("y_end", QString::number(d->yEnd)); writer->writeEndElement(); //vector with row heights writer->writeStartElement("row_heights"); const char* data = reinterpret_cast(d->rowHeights.constData()); int size = d->rowHeights.size() * sizeof(int); writer->writeCharacters(QByteArray::fromRawData(data,size).toBase64()); writer->writeEndElement(); //vector with column widths writer->writeStartElement("column_widths"); data = reinterpret_cast(d->columnWidths.constData()); size = d->columnWidths.size()*sizeof(int); writer->writeCharacters(QByteArray::fromRawData(data, size).toBase64()); writer->writeEndElement(); //columns DEBUG(" mode = " << d->mode); switch (d->mode) { case AbstractColumn::Numeric: size = d->rowCount*sizeof(double); for (int i = 0; i < d->columnCount; ++i) { data = reinterpret_cast(static_cast>*>(d->data)->at(i).constData()); writer->writeStartElement("column"); writer->writeCharacters(QByteArray::fromRawData(data, size).toBase64()); writer->writeEndElement(); } break; case AbstractColumn::Text: size = d->rowCount*sizeof(QString); for (int i = 0; i < d->columnCount; ++i) { QDEBUG(" string: " << static_cast>*>(d->data)->at(i)); data = reinterpret_cast(static_cast>*>(d->data)->at(i).constData()); writer->writeStartElement("column"); writer->writeCharacters(QByteArray::fromRawData(data, size).toBase64()); writer->writeEndElement(); } break; case AbstractColumn::Integer: size = d->rowCount*sizeof(int); for (int i = 0; i < d->columnCount; ++i) { data = reinterpret_cast(static_cast>*>(d->data)->at(i).constData()); writer->writeStartElement("column"); writer->writeCharacters(QByteArray::fromRawData(data, size).toBase64()); writer->writeEndElement(); } break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: size = d->rowCount*sizeof(QDateTime); for (int i = 0; i < d->columnCount; ++i) { data = reinterpret_cast(static_cast>*>(d->data)->at(i).constData()); writer->writeStartElement("column"); writer->writeCharacters(QByteArray::fromRawData(data, size).toBase64()); writer->writeEndElement(); } break; } writer->writeEndElement(); // "matrix" } bool Matrix::load(XmlStreamReader* reader, bool preview) { DEBUG("Matrix::load()"); if (!readBasicAttributes(reader)) return false; KLocalizedString attributeWarning = ki18n("Attribute '%1' missing or empty, default value is used"); QXmlStreamAttributes attribs; QString str; // read child elements while (!reader->atEnd()) { reader->readNext(); if (reader->isEndElement() && reader->name() == "matrix") break; if (!reader->isStartElement()) continue; if (reader->name() == "comment") { if (!readCommentElement(reader)) return false; } else if(!preview && reader->name() == "formula") { d->formula = reader->text().toString().trimmed(); } else if (!preview && reader->name() == "format") { attribs = reader->attributes(); str = attribs.value("mode").toString(); if(str.isEmpty()) reader->raiseWarning(attributeWarning.subs("mode").toString()); else d->mode = AbstractColumn::ColumnMode(str.toInt()); str = attribs.value("headerFormat").toString(); if(str.isEmpty()) reader->raiseWarning(attributeWarning.subs("headerFormat").toString()); else d->headerFormat = Matrix::HeaderFormat(str.toInt()); str = attribs.value("numericFormat").toString(); if(str.isEmpty()) reader->raiseWarning(attributeWarning.subs("numericFormat").toString()); else { QByteArray formatba = str.toLatin1(); d->numericFormat = *formatba.data(); } str = attribs.value("precision").toString(); if(str.isEmpty()) reader->raiseWarning(attributeWarning.subs("precision").toString()); else d->precision = str.toInt(); } else if (!preview && reader->name() == "dimension") { attribs = reader->attributes(); str = attribs.value("columns").toString(); if(str.isEmpty()) reader->raiseWarning(attributeWarning.subs("columns").toString()); else d->columnCount = str.toInt(); str = attribs.value("rows").toString(); if(str.isEmpty()) reader->raiseWarning(attributeWarning.subs("rows").toString()); else d->rowCount = str.toInt(); str = attribs.value("x_start").toString(); if(str.isEmpty()) reader->raiseWarning(attributeWarning.subs("x_start").toString()); else d->xStart = str.toDouble(); str = attribs.value("x_end").toString(); if(str.isEmpty()) reader->raiseWarning(attributeWarning.subs("x_end").toString()); else d->xEnd = str.toDouble(); str = attribs.value("y_start").toString(); if(str.isEmpty()) reader->raiseWarning(attributeWarning.subs("y_start").toString()); else d->yStart = str.toDouble(); str = attribs.value("y_end").toString(); if(str.isEmpty()) reader->raiseWarning(attributeWarning.subs("y_end").toString()); else d->yEnd = str.toDouble(); } else if (!preview && reader->name() == "row_heights") { reader->readNext(); QString content = reader->text().toString().trimmed(); QByteArray bytes = QByteArray::fromBase64(content.toLatin1()); int count = bytes.size()/sizeof(int); d->rowHeights.resize(count); memcpy(d->rowHeights.data(), bytes.data(), count*sizeof(int)); } else if (!preview && reader->name() == "column_widths") { reader->readNext(); QString content = reader->text().toString().trimmed(); QByteArray bytes = QByteArray::fromBase64(content.toLatin1()); int count = bytes.size()/sizeof(int); d->columnWidths.resize(count); memcpy(d->columnWidths.data(), bytes.data(), count*sizeof(int)); } else if (!preview && reader->name() == "column") { //TODO: parallelize reading of columns? reader->readNext(); QString content = reader->text().toString().trimmed(); QByteArray bytes = QByteArray::fromBase64(content.toLatin1()); switch (d->mode) { case AbstractColumn::Numeric: { int count = bytes.size()/sizeof(double); QVector column; column.resize(count); memcpy(column.data(), bytes.data(), count*sizeof(double)); static_cast>*>(d->data)->append(column); break; } case AbstractColumn::Text: { int count = bytes.size()/sizeof(char); QVector column; column.resize(count); //TODO: warning (GCC8): writing to an object of type 'class QString' with no trivial copy-assignment; use copy-assignment or copy-initialization instead //memcpy(column.data(), bytes.data(), count*sizeof(QString)); //QDEBUG(" string: " << column.data()); static_cast>*>(d->data)->append(column); break; } case AbstractColumn::Integer: { int count = bytes.size()/sizeof(int); QVector column; column.resize(count); memcpy(column.data(), bytes.data(), count*sizeof(int)); static_cast>*>(d->data)->append(column); break; } case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: { int count = bytes.size()/sizeof(QDateTime); QVector column; column.resize(count); //TODO: warning (GCC8): writing to an object of type 'class QDateTime' with no trivial copy-assignment; use copy-assignment or copy-initialization instead //memcpy(column.data(), bytes.data(), count*sizeof(QDateTime)); static_cast>*>(d->data)->append(column); break; } } } else { // unknown element reader->raiseWarning(i18n("unknown element '%1'", reader->name().toString())); if (!reader->skipToEndElement()) return false; } } return true; } //############################################################################## //######################## Data Import ####################################### //############################################################################## int Matrix::prepareImport(QVector& dataContainer, AbstractFileFilter::ImportMode mode, int actualRows, int actualCols, QStringList colNameList, QVector columnMode) { QDEBUG("prepareImport() rows =" << actualRows << " cols =" << actualCols); QDEBUG(" column modes = " << columnMode); Q_UNUSED(colNameList); int columnOffset = 0; setUndoAware(false); setSuppressDataChangedSignal(true); // resize the matrix if (mode == AbstractFileFilter::Replace) { clear(); setDimensions(actualRows, actualCols); } else { if (rowCount() < actualRows) setDimensions(actualRows, actualCols); else setDimensions(rowCount(), actualCols); } // data() returns a void* which is a pointer to a matrix of any data type (see ColumnPrivate.cpp) dataContainer.resize(actualCols); switch (columnMode[0]) { // only columnMode[0] is used case AbstractColumn::Numeric: for (int n = 0; n < actualCols; n++) { QVector* vector = &(static_cast>*>(data())->operator[](n)); vector->resize(actualRows); dataContainer[n] = static_cast(vector); } d->mode = AbstractColumn::Numeric; break; case AbstractColumn::Integer: for (int n = 0; n < actualCols; n++) { QVector* vector = &(static_cast>*>(data())->operator[](n)); vector->resize(actualRows); dataContainer[n] = static_cast(vector); } d->mode = AbstractColumn::Integer; break; case AbstractColumn::Text: for (int n = 0; n < actualCols; n++) { QVector* vector = &(static_cast>*>(data())->operator[](n)); vector->resize(actualRows); dataContainer[n] = static_cast(vector); } d->mode = AbstractColumn::Text; break; case AbstractColumn::Day: case AbstractColumn::Month: case AbstractColumn::DateTime: for (int n = 0; n < actualCols; n++) { QVector* vector = &(static_cast>*>(data())->operator[](n)); vector->resize(actualRows); dataContainer[n] = static_cast(vector); } d->mode = AbstractColumn::DateTime; break; } return columnOffset; } void Matrix::finalizeImport(int columnOffset, int startColumn, int endColumn, int numRows, const QString& dateTimeFormat, AbstractFileFilter::ImportMode importMode) { DEBUG("Matrix::finalizeImport()"); Q_UNUSED(columnOffset); Q_UNUSED(startColumn); Q_UNUSED(endColumn); Q_UNUSED(numRows); Q_UNUSED(dateTimeFormat); Q_UNUSED(importMode); setSuppressDataChangedSignal(false); setChanged(); setUndoAware(true); DEBUG("Matrix::finalizeImport() DONE"); } diff --git a/src/backend/matrix/MatrixModel.cpp b/src/backend/matrix/MatrixModel.cpp index 87c745641..0f051c029 100644 --- a/src/backend/matrix/MatrixModel.cpp +++ b/src/backend/matrix/MatrixModel.cpp @@ -1,281 +1,304 @@ /*************************************************************************** File : MatrixModel.cpp Project : LabPlot Description : Matrix data model -------------------------------------------------------------------- Copyright : (C) 2015-2016 Alexander Semke (alexander.semke@web.de) Copyright : (C) 2008-2009 Tilman Benkert (thzs@gmx.net) Copyright : (C) 2018 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "backend/matrix/MatrixModel.h" #include "backend/matrix/Matrix.h" #include #include /*! \class MatrixModel \brief Model for the access to data of a Matrix-object. This is a model in the sense of Qt4 model/view framework which is used to access a Matrix object from any of Qt4s view classes, typically a QMatrixView. Its main purposes are translating Matrix signals into QAbstractItemModel signals and translating calls to the QAbstractItemModel read/write API into calls in the public API of Matrix. \ingroup backend */ MatrixModel::MatrixModel(Matrix* matrix) : QAbstractItemModel(nullptr), m_matrix(matrix), m_suppressDataChangedSignal(false) { connect(m_matrix, &Matrix::columnsAboutToBeInserted, this, &MatrixModel::handleColumnsAboutToBeInserted); connect(m_matrix, &Matrix::columnsInserted, this, &MatrixModel::handleColumnsInserted); connect(m_matrix, &Matrix::columnsAboutToBeRemoved, this, &MatrixModel::handleColumnsAboutToBeRemoved); connect(m_matrix, &Matrix::columnsRemoved, this, &MatrixModel::handleColumnsRemoved); connect(m_matrix, &Matrix::rowsAboutToBeInserted, this, &MatrixModel::handleRowsAboutToBeInserted); connect(m_matrix, &Matrix::rowsInserted, this, &MatrixModel::handleRowsInserted); connect(m_matrix, &Matrix::rowsAboutToBeRemoved, this, &MatrixModel::handleRowsAboutToBeRemoved); connect(m_matrix, &Matrix::rowsRemoved, this, &MatrixModel::handleRowsRemoved); connect(m_matrix, &Matrix::dataChanged, this, &MatrixModel::handleDataChanged); connect(m_matrix, &Matrix::coordinatesChanged, this, &MatrixModel::handleCoordinatesChanged); connect(m_matrix, &Matrix::numericFormatChanged, this, &MatrixModel::handleFormatChanged); connect(m_matrix, &Matrix::precisionChanged, this, &MatrixModel::handleFormatChanged); } void MatrixModel::setSuppressDataChangedSignal(bool b) { m_suppressDataChangedSignal = b; } void MatrixModel::setChanged() { emit changed(); } Qt::ItemFlags MatrixModel::flags(const QModelIndex& index) const { if (index.isValid()) return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable; else return Qt::ItemIsEnabled; } QVariant MatrixModel::data(const QModelIndex& index, int role) const { if( !index.isValid() ) return QVariant(); int row = index.row(); int col = index.column(); switch(role) { case Qt::ToolTipRole: case Qt::EditRole: case Qt::DisplayRole: { AbstractColumn::ColumnMode mode = m_matrix->mode(); //DEBUG("MatrixModel::data() DisplayRole, mode = " << mode); switch (mode) { case AbstractColumn::Numeric: return QVariant(m_matrix->text(row, col)); case AbstractColumn::Integer: return QVariant(m_matrix->text(row, col)); case AbstractColumn::DateTime: case AbstractColumn::Month: case AbstractColumn::Day: return QVariant(m_matrix->text(row, col)); case AbstractColumn::Text: // should not happen return QVariant(m_matrix->text(row, col)); default: DEBUG(" unknown column mode " << mode << " found"); break; } break; } case Qt::BackgroundRole: //use bluish background color to distinguish Matrix from Spreadsheet return QVariant(QBrush(QColor(192,255,255))); case Qt::ForegroundRole: //ignore current theme settings and always use black foreground color so Matrix is usable with dark themes, too. return QVariant(QBrush(QColor(Qt::black))); } return QVariant(); } QVariant MatrixModel::headerData(int section, Qt::Orientation orientation, int role) const { QString result; Matrix::HeaderFormat headerFormat = m_matrix->headerFormat(); switch(orientation) { case Qt::Horizontal: switch(role) { case Qt::DisplayRole: case Qt::ToolTipRole: if (headerFormat==Matrix::HeaderRowsColumns) { result = QString::number(section+1); } else if (headerFormat == Matrix::HeaderValues) { double diff = m_matrix->xEnd() - m_matrix->xStart(); double step = 0.0; if (m_matrix->columnCount() > 1) step = diff/double(m_matrix->columnCount()-1); result = QLocale().toString(m_matrix->xStart()+double(section)*step, m_matrix->numericFormat(), m_matrix->precision()); } else { result = QString::number(section+1) + QLatin1String(" ("); double diff = m_matrix->xEnd() - m_matrix->xStart(); double step = 0.0; if (m_matrix->columnCount() > 1) step = diff/double(m_matrix->columnCount()-1); result += QLocale().toString(m_matrix->xStart()+double(section)*step, m_matrix->numericFormat(), m_matrix->precision()); result += ')'; } return QVariant(result); } break; case Qt::Vertical: switch(role) { case Qt::DisplayRole: case Qt::ToolTipRole: if (headerFormat==Matrix::HeaderRowsColumns) { result = QString::number(section+1); } else if (headerFormat==Matrix::HeaderValues) { double diff = m_matrix->yEnd() - m_matrix->yStart(); double step = 0.0; if (m_matrix->rowCount() > 1) step = diff/double(m_matrix->rowCount()-1); // TODO: implement decent double == 0 check // if (diff < 1e-10) // result += QLocale().toString(m_matrix->yStart(), // m_matrix->numericFormat(), m_matrix->displayedDigits()); result += QLocale().toString(m_matrix->yStart()+double(section)*step, m_matrix->numericFormat(), m_matrix->precision()); } else { result = QString::number(section+1) + QString(" ("); double diff = m_matrix->yEnd() - m_matrix->yStart(); double step = 0.0; if (m_matrix->rowCount() > 1) step = diff/double(m_matrix->rowCount()-1); result += QLocale().toString(m_matrix->yStart()+double(section)*step, m_matrix->numericFormat(), m_matrix->precision()); result += ')'; } return QVariant(result); } } return QVariant(); } int MatrixModel::rowCount(const QModelIndex& parent) const { Q_UNUSED(parent) return m_matrix->rowCount(); } int MatrixModel::columnCount(const QModelIndex& parent) const { Q_UNUSED(parent) return m_matrix->columnCount(); } bool MatrixModel::setData(const QModelIndex& index, const QVariant& value, int role) { + //QDEBUG("MatrixModel::setData() value =" << value); if (!index.isValid()) return false; int row = index.row(); int column = index.column(); if(role == Qt::EditRole) { - m_matrix->setCell(row, column, value.toDouble()); + AbstractColumn::ColumnMode mode = m_matrix->mode(); + //DEBUG(" mode = " << mode); + switch (mode) { + case AbstractColumn::Numeric: + m_matrix->setCell(row, column, value.toDouble()); + break; + case AbstractColumn::Integer: + m_matrix->setCell(row, column, value.toInt()); + break; + case AbstractColumn::DateTime: + case AbstractColumn::Month: + case AbstractColumn::Day: + //TODO: m_matrix->setCell(row, column, value.toDateTime()); + break; + case AbstractColumn::Text: + DEBUG(" WARNING: Text format not supported"); // should not happen + m_matrix->setCell(row, column, value.toString()); + break; + default: + DEBUG(" unknown column mode " << mode << " found"); + break; + } + if (!m_suppressDataChangedSignal) emit changed(); return true; } return false; } QModelIndex MatrixModel::index(int row, int column, const QModelIndex& parent) const { Q_UNUSED(parent) return createIndex(row, column); } QModelIndex MatrixModel::parent(const QModelIndex& child) const { Q_UNUSED(child) return QModelIndex{}; } void MatrixModel::updateHeader() { emit headerDataChanged(Qt::Horizontal, 0, m_matrix->columnCount()); emit headerDataChanged(Qt::Vertical, 0, m_matrix->rowCount()); } void MatrixModel::handleColumnsAboutToBeInserted(int before, int count) { beginInsertColumns(QModelIndex(), before, before+count-1); } void MatrixModel::handleColumnsInserted(int first, int count) { Q_UNUSED(first) Q_UNUSED(count) endInsertColumns(); if (!m_suppressDataChangedSignal) emit changed(); } void MatrixModel::handleColumnsAboutToBeRemoved(int first, int count) { beginRemoveColumns(QModelIndex(), first, first+count-1); } void MatrixModel::handleColumnsRemoved(int first, int count) { Q_UNUSED(first) Q_UNUSED(count) endRemoveColumns(); if (!m_suppressDataChangedSignal) emit changed(); } void MatrixModel::handleRowsAboutToBeInserted(int before, int count) { beginInsertRows(QModelIndex(), before, before+count-1); } void MatrixModel::handleRowsInserted(int first, int count) { Q_UNUSED(first) Q_UNUSED(count) endInsertRows(); if (!m_suppressDataChangedSignal) emit changed(); } void MatrixModel::handleRowsAboutToBeRemoved(int first, int count) { beginRemoveRows(QModelIndex(), first, first+count-1); } void MatrixModel::handleRowsRemoved(int first, int count) { Q_UNUSED(first) Q_UNUSED(count) endRemoveRows(); if (!m_suppressDataChangedSignal) emit changed(); } void MatrixModel::handleDataChanged(int top, int left, int bottom, int right) { emit dataChanged(index(top, left), index(bottom, right)); if (!m_suppressDataChangedSignal) emit changed(); } void MatrixModel::handleCoordinatesChanged() { emit headerDataChanged(Qt::Horizontal, 0, columnCount()-1); emit headerDataChanged(Qt::Vertical, 0, rowCount()-1); } void MatrixModel::handleFormatChanged() { handleCoordinatesChanged(); handleDataChanged(0, 0, rowCount()-1, columnCount()-1); } diff --git a/src/backend/matrix/matrixcommands.h b/src/backend/matrix/matrixcommands.h index 35af5d1bc..a1662b69f 100644 --- a/src/backend/matrix/matrixcommands.h +++ b/src/backend/matrix/matrixcommands.h @@ -1,410 +1,411 @@ /*************************************************************************** File : matrixcommands.h Project : LabPlot Description : Commands used in Matrix (part of the undo/redo framework) -------------------------------------------------------------------- Copyright : (C) 2008 Tilman Benkert (thzs@gmx.net) Copyright : (C) 2015 Alexander Semke (alexander.semke@web.de) Copyright : (C) 2017 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #ifndef MATRIX_COMMANDS_H #define MATRIX_COMMANDS_H #include #include #include #include "Matrix.h" #include "MatrixPrivate.h" //! Insert columns class MatrixInsertColumnsCmd : public QUndoCommand { public: MatrixInsertColumnsCmd(MatrixPrivate*, int before, int count, QUndoCommand* = nullptr); void redo() override; void undo() override; private: MatrixPrivate* m_private_obj; int m_before; //! Column to insert before int m_count; //! The number of new columns }; //! Insert rows class MatrixInsertRowsCmd : public QUndoCommand { public: MatrixInsertRowsCmd(MatrixPrivate*, int before, int count, QUndoCommand* = nullptr); void redo() override; void undo() override; private: MatrixPrivate* m_private_obj; int m_before; //! Row to insert before int m_count; //! The number of new rows }; //! Remove columns template class MatrixRemoveColumnsCmd : public QUndoCommand { public: MatrixRemoveColumnsCmd(MatrixPrivate* private_obj, int first, int count, QUndoCommand* parent = nullptr) : QUndoCommand(parent), m_private_obj(private_obj), m_first(first), m_count(count) { setText(i18np("%1: remove %2 column", "%1: remove %2 columns", m_private_obj->name(), m_count)); } void redo() override { if(m_backups.isEmpty()) { int last_row = m_private_obj->rowCount-1; for (int i = 0; i < m_count; i++) m_backups.append(m_private_obj->columnCells(m_first+i, 0, last_row)); } m_private_obj->removeColumns(m_first, m_count); emit m_private_obj->q->columnCountChanged(m_private_obj->columnCount); } void undo() override { m_private_obj->insertColumns(m_first, m_count); int last_row = m_private_obj->rowCount-1; //TODO: use memcopy to copy from the backup vector for (int i = 0; i < m_count; i++) m_private_obj->setColumnCells(m_first+i, 0, last_row, m_backups.at(i)); emit m_private_obj->q->columnCountChanged(m_private_obj->columnCount); } private: MatrixPrivate* m_private_obj; int m_first; //! First column to remove int m_count; //! The number of columns to remove QVector> m_backups; //! Backups of the removed columns }; //! Remove rows template class MatrixRemoveRowsCmd : public QUndoCommand { public: MatrixRemoveRowsCmd(MatrixPrivate* private_obj, int first, int count, QUndoCommand* parent = nullptr) : QUndoCommand(parent), m_private_obj(private_obj), m_first(first), m_count(count) { setText(i18np("%1: remove %2 row", "%1: remove %2 rows", m_private_obj->name(), m_count)); } void redo() override { if(m_backups.isEmpty()) { int last_row = m_first+m_count-1; for (int col = 0; col < m_private_obj->columnCount; col++) m_backups.append(m_private_obj->columnCells(col, m_first, last_row)); } m_private_obj->removeRows(m_first, m_count); emit m_private_obj->q->rowCountChanged(m_private_obj->rowCount); } void undo() override { m_private_obj->insertRows(m_first, m_count); int last_row = m_first+m_count-1; for (int col = 0; col < m_private_obj->columnCount; col++) m_private_obj->setColumnCells(col, m_first, last_row, m_backups.at(col)); emit m_private_obj->q->rowCountChanged(m_private_obj->rowCount); } private: MatrixPrivate* m_private_obj; int m_first; //! First row to remove int m_count; //! The number of rows to remove QVector< QVector > m_backups; //! Backups of the removed rows }; //! Clear matrix template class MatrixClearCmd : public QUndoCommand { public: explicit MatrixClearCmd(MatrixPrivate* private_obj, QUndoCommand* parent = nullptr) : QUndoCommand(parent), m_private_obj(private_obj) { setText(i18n("%1: clear", m_private_obj->name())); } void redo() override { if(m_backups.isEmpty()) { int last_row = m_private_obj->rowCount-1; for (int i = 0; i < m_private_obj->columnCount; i++) m_backups.append(m_private_obj->columnCells(i, 0, last_row)); } for (int i = 0; i < m_private_obj->columnCount; i++) m_private_obj->clearColumn(i); } void undo() override { int last_row = m_private_obj->rowCount-1; for (int i = 0; i < m_private_obj->columnCount; i++) m_private_obj->setColumnCells(i, 0, last_row, m_backups.at(i)); } private: MatrixPrivate* m_private_obj; QVector> m_backups; //! Backups of the cleared cells }; //! Clear matrix column template class MatrixClearColumnCmd : public QUndoCommand { public: MatrixClearColumnCmd(MatrixPrivate* private_obj, int col, QUndoCommand* parent = nullptr) : QUndoCommand(parent), m_private_obj(private_obj), m_col(col) { setText(i18n("%1: clear column %2", m_private_obj->name(), m_col+1)); } void redo() override { if(m_backup.isEmpty()) m_backup = m_private_obj->columnCells(m_col, 0, m_private_obj->rowCount-1); m_private_obj->clearColumn(m_col); } void undo() override { m_private_obj->setColumnCells(m_col, 0, m_private_obj->rowCount-1, m_backup); } private: MatrixPrivate* m_private_obj; int m_col; //! The index of the column QVector m_backup; //! Backup of the cleared column }; // Set cell value template class MatrixSetCellValueCmd : public QUndoCommand { public: MatrixSetCellValueCmd(MatrixPrivate* private_obj, int row, int col, T value, QUndoCommand* parent = nullptr) : QUndoCommand(parent), m_private_obj(private_obj), m_row(row), m_col(col), m_value(value) { + QDEBUG("MatrixSetCellValueCmd() value =" << value << ", m_value =" << m_value); // remark: don't use many QString::arg() calls in ctors of commands that might be called often, // they use a lot of execution time setText(i18n("%1: set cell value", m_private_obj->name())); } void redo() override { m_old_value = m_private_obj->cell(m_row, m_col); m_private_obj->setCell(m_row, m_col, m_value); } void undo() override { m_private_obj->setCell(m_row, m_col, m_old_value); } private: MatrixPrivate* m_private_obj; int m_row; //! The index of the row int m_col; //! The index of the column T m_value; //! New cell value T m_old_value; //! Backup of the changed value }; // Set matrix coordinates class MatrixSetCoordinatesCmd : public QUndoCommand { public: MatrixSetCoordinatesCmd(MatrixPrivate*, double x1, double x2, double y1, double y2, QUndoCommand* = nullptr); void redo() override; void undo() override; private: MatrixPrivate* m_private_obj; double m_new_x1; double m_new_x2; double m_new_y1; double m_new_y2; double m_old_x1; double m_old_x2; double m_old_y1; double m_old_y2; }; //! Set matrix formula class MatrixSetFormulaCmd : public QUndoCommand { public: MatrixSetFormulaCmd(MatrixPrivate*, QString formula); void redo() override; void undo() override; private: MatrixPrivate* m_private_obj; QString m_other_formula; }; // Set cell values for (a part of) a column at once template class MatrixSetColumnCellsCmd : public QUndoCommand { public: MatrixSetColumnCellsCmd(MatrixPrivate* private_obj, int col, int first_row, int last_row, const QVector& values, QUndoCommand* parent = nullptr) : QUndoCommand(parent), m_private_obj(private_obj), m_col(col), m_first_row(first_row), m_last_row(last_row), m_values(values) { setText(i18n("%1: set cell values", m_private_obj->name())); } void redo() override { if (m_old_values.isEmpty()) m_old_values = m_private_obj->columnCells(m_col, m_first_row, m_last_row); m_private_obj->setColumnCells(m_col, m_first_row, m_last_row, m_values); } void undo() override { m_private_obj->setColumnCells(m_col, m_first_row, m_last_row, m_old_values); } private: MatrixPrivate* m_private_obj; int m_col; //! The index of the column int m_first_row; //! The index of the first row int m_last_row; //! The index of the last row QVector m_values; //! New cell values QVector m_old_values; //! Backup of the changed values }; //! Set cell values for (a part of) a row at once template class MatrixSetRowCellsCmd : public QUndoCommand { public: MatrixSetRowCellsCmd(MatrixPrivate* private_obj, int row, int first_column, int last_column, const QVector& values, QUndoCommand* parent = nullptr) : QUndoCommand(parent), m_private_obj(private_obj), m_row(row), m_first_column(first_column), m_last_column(last_column), m_values(values) { setText(i18n("%1: set cell values", m_private_obj->name())); } void redo() override { if (m_old_values.isEmpty()) m_old_values = m_private_obj->rowCells(m_row, m_first_column, m_last_column); m_private_obj->setRowCells(m_row, m_first_column, m_last_column, m_values); } void undo() override { m_private_obj->setRowCells(m_row, m_first_column, m_last_column, m_old_values); } private: MatrixPrivate* m_private_obj; int m_row; //! The index of the row int m_first_column; //! The index of the first column int m_last_column; //! The index of the last column QVector m_values; //! New cell values QVector m_old_values; //! Backup of the changed values }; //! Transpose the matrix template class MatrixTransposeCmd : public QUndoCommand { public: explicit MatrixTransposeCmd(MatrixPrivate* private_obj, QUndoCommand* parent = nullptr) : QUndoCommand(parent), m_private_obj(private_obj) { setText(i18n("%1: transpose", m_private_obj->name())); } void redo() override { int rows = m_private_obj->rowCount; int cols = m_private_obj->columnCount; int temp_size = qMax(rows, cols); m_private_obj->suppressDataChange = true; if (cols < rows) m_private_obj->insertColumns(cols, temp_size - cols); else if (cols > rows) m_private_obj->insertRows(rows, temp_size - rows); for (int i = 1; i < temp_size; i++) { QVector row = m_private_obj->rowCells(i, 0, i-1); QVector col = m_private_obj->columnCells(i, 0, i-1); m_private_obj->setRowCells(i, 0, i-1, col); m_private_obj->setColumnCells(i, 0, i-1, row); } if (cols < rows) m_private_obj->removeRows(cols, temp_size - cols); else if (cols > rows) m_private_obj->removeColumns(rows, temp_size - rows); m_private_obj->suppressDataChange = false; m_private_obj->emitDataChanged(0, 0, m_private_obj->rowCount-1, m_private_obj->columnCount-1); } void undo() override { redo(); } private: MatrixPrivate* m_private_obj; }; //! Mirror the matrix horizontally template class MatrixMirrorHorizontallyCmd : public QUndoCommand { public: explicit MatrixMirrorHorizontallyCmd(MatrixPrivate* private_obj, QUndoCommand* parent = nullptr) : QUndoCommand(parent), m_private_obj(private_obj) { setText(i18n("%1: mirror horizontally", m_private_obj->name())); } void redo() override { int rows = m_private_obj->rowCount; int cols = m_private_obj->columnCount; int middle = cols/2; m_private_obj->suppressDataChange = true; for (int i = 0; i temp = m_private_obj->columnCells(i, 0, rows-1); m_private_obj->setColumnCells(i, 0, rows-1, m_private_obj->columnCells(cols-i-1, 0, rows-1)); m_private_obj->setColumnCells(cols-i-1, 0, rows-1, temp); } m_private_obj->suppressDataChange = false; m_private_obj->emitDataChanged(0, 0, rows-1, cols-1); } void undo() override { redo(); } private: MatrixPrivate* m_private_obj; }; // Mirror the matrix vertically template class MatrixMirrorVerticallyCmd : public QUndoCommand { public: explicit MatrixMirrorVerticallyCmd(MatrixPrivate* private_obj, QUndoCommand* parent = nullptr) : QUndoCommand(parent), m_private_obj(private_obj) { setText(i18n("%1: mirror vertically", m_private_obj->name())); } void redo() override { int rows = m_private_obj->rowCount; int cols = m_private_obj->columnCount; int middle = rows/2; m_private_obj->suppressDataChange = true; for (int i = 0; i < middle; i++) { QVector temp = m_private_obj->rowCells(i, 0, cols-1); m_private_obj->setRowCells(i, 0, cols-1, m_private_obj->rowCells(rows-i-1, 0, cols-1)); m_private_obj->setRowCells(rows-i-1, 0, cols-1, temp); } m_private_obj->suppressDataChange = false; m_private_obj->emitDataChanged(0, 0, rows-1, cols-1); } void undo() override { redo(); } private: MatrixPrivate* m_private_obj; }; // Replace matrix values class MatrixReplaceValuesCmd : public QUndoCommand { public: explicit MatrixReplaceValuesCmd(MatrixPrivate*, void* new_values, QUndoCommand* = nullptr); void redo() override; void undo() override; private: MatrixPrivate* m_private_obj; void* m_old_values; void* m_new_values; }; #endif // MATRIX_COMMANDS_H