diff --git a/src/commonfrontend/spreadsheet/SpreadsheetView.h b/src/commonfrontend/spreadsheet/SpreadsheetView.h index 11100e149..ef6336851 100644 --- a/src/commonfrontend/spreadsheet/SpreadsheetView.h +++ b/src/commonfrontend/spreadsheet/SpreadsheetView.h @@ -1,297 +1,297 @@ /*************************************************************************** File : SpreadsheetView.h Project : LabPlot Description : View class for Spreadsheet -------------------------------------------------------------------- Copyright : (C) 2010-2019 by Alexander Semke (alexander.semke@web.de) ***************************************************************************/ /*************************************************************************** * * * 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 SPREADSHEETVIEW_H #define SPREADSHEETVIEW_H #include #include "backend/core/AbstractColumn.h" #include "backend/lib/IntervalAttribute.h" class Column; class Spreadsheet; class SpreadsheetModel; class SpreadsheetItemDelegate; class SpreadsheetHeaderView; class AbstractAspect; class QTableView; class QPrinter; class QMenu; class QToolBar; class QModelIndex; class QItemSelection; #ifdef Q_OS_MAC class KDMacTouchBar; #endif class SpreadsheetView : public QWidget { Q_OBJECT public: explicit SpreadsheetView(Spreadsheet* spreadsheet, bool readOnly = false); ~SpreadsheetView() override; void resizeHeader(); void showComments(bool on = true); bool areCommentsShown() const; int selectedColumnCount(bool full = false) const; int selectedColumnCount(AbstractColumn::PlotDesignation) const; bool isColumnSelected(int col, bool full = false) const; QVector selectedColumns(bool full = false) const; int firstSelectedColumn(bool full = false) const; int lastSelectedColumn(bool full = false) const; bool isRowSelected(int row, bool full = false) const; int firstSelectedRow(bool full = false) const; int lastSelectedRow(bool full = false) const; IntervalAttribute selectedRows(bool full = false) const; bool isCellSelected(int row, int col) const; void setCellSelected(int row, int col, bool select = true); void setCellsSelected(int first_row, int first_col, int last_row, int last_col, bool select = true); void getCurrentCell(int* row, int* col) const; bool exportView(); bool printView(); bool printPreview(); void registerShortcuts(); void unregisterShortcuts(); private: void init(); void initActions(); void initMenus(); void connectActions(); bool formulaModeActive() const; void exportToFile(const QString&, const bool, const QString&, QLocale::Language) const; void exportToLaTeX(const QString&, const bool exportHeaders, const bool gridLines, const bool captions, const bool latexHeaders, const bool skipEmptyRows,const bool exportEntire) const; void exportToFits(const QString& path, const int exportTo, const bool commentsAsUnits) const; void exportToSQLite(const QString& path) const; void insertColumnsLeft(int); void insertColumnsRight(int); void insertRowsAbove(int); void insertRowsBelow(int); QTableView* m_tableView; Spreadsheet* m_spreadsheet; SpreadsheetItemDelegate* m_delegate; SpreadsheetModel* m_model; SpreadsheetHeaderView* m_horizontalHeader; bool m_suppressSelectionChangedEvent{false}; bool m_readOnly; bool eventFilter(QObject*, QEvent*) override; void checkSpreadsheetMenu(); //selection related actions QAction* action_cut_selection; QAction* action_copy_selection; QAction* action_paste_into_selection; QAction* action_mask_selection; QAction* action_unmask_selection; QAction* action_clear_selection; // QAction* action_set_formula; // QAction* action_recalculate; QAction* action_fill_row_numbers; QAction* action_fill_sel_row_numbers; QAction* action_fill_random; QAction* action_fill_equidistant; QAction* action_fill_random_nonuniform; QAction* action_fill_const; QAction* action_fill_function; //spreadsheet related actions QAction* action_toggle_comments; QAction* action_select_all; QAction* action_clear_spreadsheet; QAction* action_clear_masks; QAction* action_sort_spreadsheet; QAction* action_go_to_cell; QAction* action_statistics_all_columns; //column related actions QAction* action_insert_column_left; QAction* action_insert_column_right; QAction* action_insert_columns_left; QAction* action_insert_columns_right; QAction* action_remove_columns; QAction* action_clear_columns; QAction* action_add_columns; QAction* action_set_as_none; QAction* action_set_as_x; QAction* action_set_as_y; QAction* action_set_as_z; QAction* action_set_as_xerr; QAction* action_set_as_xerr_plus; QAction* action_set_as_xerr_minus; QAction* action_set_as_yerr; QAction* action_set_as_yerr_plus; QAction* action_set_as_yerr_minus; QAction* action_reverse_columns; QAction* action_add_value; QAction* action_subtract_value; QAction* action_multiply_value; QAction* action_divide_value; QAction* action_drop_values; QAction* action_mask_values; QAction* action_join_columns; QAction* action_normalize_columns; QAction* action_normalize_selection; QAction* action_sort_columns; QAction* action_sort_asc_column; QAction* action_sort_desc_column; QAction* action_statistics_columns; //row related actions QAction* action_insert_row_above; QAction* action_insert_row_below; QAction* action_insert_rows_above; QAction* action_insert_rows_below; QAction* action_remove_rows; QAction* action_clear_rows; QAction* action_statistics_rows; //analysis and plot data menu actions QAction* action_plot_data_xycurve; QAction* action_plot_data_histogram; QAction* addDataOperationAction; QAction* addDataReductionAction; QAction* addDifferentiationAction; QAction* addIntegrationAction; QAction* addInterpolationAction; QAction* addSmoothAction; QVector addFitAction; QAction* addFourierFilterAction; //Menus QMenu* m_selectionMenu; QMenu* m_columnMenu; QMenu* m_columnSetAsMenu{nullptr}; QMenu* m_columnGenerateDataMenu{nullptr}; QMenu* m_columnManipulateDataMenu; QMenu* m_columnSortMenu{nullptr}; QMenu* m_rowMenu; QMenu* m_spreadsheetMenu; QMenu* m_plotDataMenu; QMenu* m_analyzePlotMenu; public slots: void createContextMenu(QMenu*); void fillToolBar(QToolBar*); #ifdef Q_OS_MAC void fillTouchBar(KDMacTouchBar*); #endif void print(QPrinter*) const; + void pasteIntoSelection(); private slots: void createColumnContextMenu(QMenu*); void goToCell(int row, int col); void toggleComments(); void goToNextColumn(); void goToPreviousColumn(); void goToCell(); void sortSpreadsheet(); void sortDialog(QVector); void cutSelection(); void copySelection(); - void pasteIntoSelection(); void clearSelectedCells(); void maskSelection(); void unmaskSelection(); // void recalculateSelectedCells(); void plotData(); void fillSelectedCellsWithRowNumbers(); void fillWithRowNumbers(); void fillSelectedCellsWithRandomNumbers(); void fillWithRandomValues(); void fillWithEquidistantValues(); void fillWithFunctionValues(); void fillSelectedCellsWithConstValues(); void insertRowAbove(); void insertRowBelow(); void insertRowsAbove(); void insertRowsBelow(); void removeSelectedRows(); void clearSelectedRows(); void insertColumnLeft(); void insertColumnRight(); void insertColumnsLeft(); void insertColumnsRight(); void removeSelectedColumns(); void clearSelectedColumns(); void modifyValues(); void reverseColumns(); void dropColumnValues(); void maskColumnValues(); void joinColumns(); void normalizeSelectedColumns(); void normalizeSelection(); void sortSelectedColumns(); void sortColumnAscending(); void sortColumnDescending(); void setSelectionAs(); void activateFormulaMode(bool on); void showColumnStatistics(bool forAll = false); void showAllColumnsStatistics(); void showRowStatistics(); void handleHorizontalSectionResized(int logicalIndex, int oldSize, int newSize); void handleHorizontalSectionMoved(int index, int from, int to); void handleHorizontalHeaderDoubleClicked(int index); void handleHeaderDataChanged(Qt::Orientation orientation, int first, int last); void currentColumnChanged(const QModelIndex& current, const QModelIndex & previous); void handleAspectAdded(const AbstractAspect*); void handleAspectAboutToBeRemoved(const AbstractAspect*); void updateHeaderGeometry(Qt::Orientation o, int first, int last); void selectColumn(int); void deselectColumn(int); void columnClicked(int); void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected); }; #endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8bc6aeb4d..1b7ae4bd7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,7 @@ set(SRC_DIR ${labplot2_SOURCE_DIR}/src) INCLUDE_DIRECTORIES(${SRC_DIR}) add_subdirectory(analysis) add_subdirectory(import_export) add_subdirectory(nsl) +add_subdirectory(spreadsheet) diff --git a/tests/spreadsheet/CMakeLists.txt b/tests/spreadsheet/CMakeLists.txt new file mode 100644 index 000000000..ce50289b9 --- /dev/null +++ b/tests/spreadsheet/CMakeLists.txt @@ -0,0 +1,9 @@ +INCLUDE_DIRECTORIES(${GSL_INCLUDE_DIR}) + +add_executable (spreadsheettest SpreadsheetTest.cpp) + +target_link_libraries(spreadsheettest Qt5::Test) +target_link_libraries(spreadsheettest ${GSL_LIBRARIES} ${GSL_CBLAS_LIBRARIES}) +target_link_libraries(spreadsheettest labplot2lib) + +add_test(NAME spreadsheettest COMMAND spreadsheettest) diff --git a/tests/spreadsheet/SpreadsheetTest.cpp b/tests/spreadsheet/SpreadsheetTest.cpp new file mode 100644 index 000000000..0eae7233c --- /dev/null +++ b/tests/spreadsheet/SpreadsheetTest.cpp @@ -0,0 +1,151 @@ +/*************************************************************************** + File : SpreadsheetTest.cpp + Project : LabPlot + Description : Tests for the Spreadsheet + -------------------------------------------------------------------- + Copyright : (C) 2020 Alexander Semke (alexander.semke@web.de) + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 +#include + +#include "SpreadsheetTest.h" +#include "backend/spreadsheet/Spreadsheet.h" +#include "../src/commonfrontend/spreadsheet/SpreadsheetView.h" + +void SpreadsheetTest::initTestCase() { + qRegisterMetaType("const AbstractAspect*"); + qRegisterMetaType("const AbstractColumn*"); +} + +/*! + insert two columns with float values into an empty spreadsheet +*/ +void SpreadsheetTest::testCopyPaste00() { + Spreadsheet* sheet = new Spreadsheet("test", false); + + const QString str = "10.0 100.0\n20.0 200.0"; + + QApplication::clipboard()->setText(str); + + SpreadsheetView* view = new SpreadsheetView(sheet, false); + view->pasteIntoSelection(); + + //column modes + QCOMPARE(sheet->column(0)->columnMode(), AbstractColumn::Numeric); + QCOMPARE(sheet->column(1)->columnMode(), AbstractColumn::Numeric); + + //values + QCOMPARE(sheet->column(0)->valueAt(0), 10.0); + QCOMPARE(sheet->column(1)->valueAt(0), 100.0); + QCOMPARE(sheet->column(0)->valueAt(1), 20.0); + QCOMPARE(sheet->column(1)->valueAt(1), 200.0); +} + +/*! + insert one column with integer values and one column with float numbers into an empty spreadsheet. + the first column has to be converted to integer column. +*/ +void SpreadsheetTest::testCopyPaste01() { + Spreadsheet* sheet = new Spreadsheet("test", false); + + const QString str = "10 100.0\n20 200.0"; + + QApplication::clipboard()->setText(str); + + SpreadsheetView* view = new SpreadsheetView(sheet, false); + view->pasteIntoSelection(); + + //column modes + QCOMPARE(sheet->column(0)->columnMode(), AbstractColumn::Integer); + QCOMPARE(sheet->column(1)->columnMode(), AbstractColumn::Numeric); + + //values + QCOMPARE(sheet->column(0)->integerAt(0), 10); + QCOMPARE(sheet->column(1)->valueAt(0), 100.0); + QCOMPARE(sheet->column(0)->integerAt(1), 20); + QCOMPARE(sheet->column(1)->valueAt(1), 200.0); +} + +/*! + insert irregular data, new columns should be added appropriately. +*/ +void SpreadsheetTest::testCopyPaste02() { + Spreadsheet* sheet = new Spreadsheet("test", false); + + const QString str = "0\n" + "10 20\n" + "11 21 31\n" + "12 22 32 42\n" + "13 23\n" + "14"; + + QApplication::clipboard()->setText(str); + + int rows = sheet->rowCount(); + + SpreadsheetView* view = new SpreadsheetView(sheet, false); + view->pasteIntoSelection(); + + //spreadsheet size + QCOMPARE(sheet->columnCount(), 4); + QCOMPARE(sheet->rowCount(), rows); + + //column modes + QCOMPARE(sheet->column(0)->columnMode(), AbstractColumn::Integer); + QCOMPARE(sheet->column(1)->columnMode(), AbstractColumn::Integer); + QCOMPARE(sheet->column(2)->columnMode(), AbstractColumn::Integer); + QCOMPARE(sheet->column(3)->columnMode(), AbstractColumn::Integer); + + //values + QCOMPARE(sheet->column(0)->integerAt(0), 0); + QCOMPARE(sheet->column(1)->integerAt(0), 0); + QCOMPARE(sheet->column(2)->integerAt(0), 0); + QCOMPARE(sheet->column(3)->integerAt(0), 0); + + QCOMPARE(sheet->column(0)->integerAt(1), 10); + QCOMPARE(sheet->column(1)->integerAt(1), 20); + QCOMPARE(sheet->column(2)->integerAt(1), 0); + QCOMPARE(sheet->column(3)->integerAt(1), 0); + + QCOMPARE(sheet->column(0)->integerAt(2), 11); + QCOMPARE(sheet->column(1)->integerAt(2), 21); + QCOMPARE(sheet->column(2)->integerAt(2), 31); + QCOMPARE(sheet->column(3)->integerAt(2), 0); + + QCOMPARE(sheet->column(0)->integerAt(3), 12); + QCOMPARE(sheet->column(1)->integerAt(3), 22); + QCOMPARE(sheet->column(2)->integerAt(3), 32); + QCOMPARE(sheet->column(3)->integerAt(3), 42); + + QCOMPARE(sheet->column(0)->integerAt(4), 13); + QCOMPARE(sheet->column(1)->integerAt(4), 23); + QCOMPARE(sheet->column(2)->integerAt(4), 0); + QCOMPARE(sheet->column(3)->integerAt(4), 0); + + QCOMPARE(sheet->column(0)->integerAt(5), 14); + QCOMPARE(sheet->column(1)->integerAt(5), 0); + QCOMPARE(sheet->column(2)->integerAt(5), 0); + QCOMPARE(sheet->column(3)->integerAt(5), 0); +} + +QTEST_MAIN(SpreadsheetTest) diff --git a/tests/spreadsheet/SpreadsheetTest.h b/tests/spreadsheet/SpreadsheetTest.h new file mode 100644 index 000000000..c4b43d36e --- /dev/null +++ b/tests/spreadsheet/SpreadsheetTest.h @@ -0,0 +1,45 @@ +/*************************************************************************** + File : SpreadsheetTest.h + Project : LabPlot + Description : Tests for the Spreadsheet + -------------------------------------------------------------------- + Copyright : (C) 2020 Alexander Semke (alexander.semke@web.de) + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 SPREADSHEETTEST_H +#define SPREADSHEETTEST_H + +#include + +class SpreadsheetTest : public QObject { + Q_OBJECT + +private slots: + void initTestCase(); + + //copy and paste + void testCopyPaste00(); + void testCopyPaste01(); + void testCopyPaste02(); +}; + +#endif