File indexing completed on 2024-05-19 03:49:05

0001 /*
0002     File                 : Spreadsheet.h
0003     Project              : LabPlot
0004     Description          : Aspect providing a spreadsheet table with column logic
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2010-2023 Alexander Semke <alexander.semke@web.de>
0007     SPDX-FileCopyrightText: 2006-2008 Tilman Benkert <thzs@gmx.net>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef SPREADSHEET_H
0012 #define SPREADSHEET_H
0013 
0014 #include "backend/core/column/ColumnStringIO.h"
0015 #include "backend/datasources/AbstractDataSource.h"
0016 #include "backend/lib/macros.h"
0017 
0018 class AbstractFileFilter;
0019 class SpreadsheetView;
0020 class SpreadsheetModel;
0021 class SpreadsheetPrivate;
0022 class StatisticsSpreadsheet;
0023 
0024 class Spreadsheet : public AbstractDataSource {
0025     Q_OBJECT
0026 
0027 public:
0028     explicit Spreadsheet(const QString& name, bool loading = false, AspectType type = AspectType::Spreadsheet);
0029     ~Spreadsheet() override;
0030 
0031     QIcon icon() const override;
0032     QMenu* createContextMenu() override;
0033     void fillColumnContextMenu(QMenu*, Column*);
0034     QWidget* view() const override;
0035     StatisticsSpreadsheet* statisticsSpreadsheet() const;
0036 
0037     bool exportView() const override;
0038     bool printView() override;
0039     bool printPreview() const override;
0040 
0041     void setModel(SpreadsheetModel*);
0042     SpreadsheetModel* model() const;
0043 
0044     QVector<AspectType> pasteTypes() const override;
0045     QVector<AspectType> dropableOn() const override;
0046 
0047     void updateHorizontalHeader();
0048     void updateLocale();
0049 
0050     int columnCount() const;
0051     int columnCount(AbstractColumn::PlotDesignation) const;
0052     Column* column(int index) const;
0053     Column* column(const QString&) const;
0054     int rowCount() const; // TODO: should be size_t?
0055 
0056     void removeRows(int first, int count, QUndoCommand* parent = nullptr);
0057     void insertRows(int before, int count, QUndoCommand* parent = nullptr);
0058     void removeColumns(int first, int count, QUndoCommand* parent = nullptr);
0059     void insertColumns(int before, int count, QUndoCommand* parent = nullptr);
0060 
0061     int colX(int col);
0062     int colY(int col);
0063     QString text(int row, int col) const;
0064 
0065     void copy(Spreadsheet* other);
0066 
0067     void save(QXmlStreamWriter*) const override;
0068     bool load(XmlStreamReader*, bool preview) override;
0069 
0070     void setColumnSelectedInView(int index, bool selected);
0071 
0072     // used from model to inform dock
0073     void emitRowCountChanged() {
0074         Q_EMIT rowCountChanged(rowCount());
0075     }
0076     void emitColumnCountChanged() {
0077         Q_EMIT columnCountChanged(columnCount());
0078     }
0079 
0080     // data import
0081     int prepareImport(std::vector<void*>& dataContainer,
0082                       AbstractFileFilter::ImportMode,
0083                       int rows,
0084                       int cols,
0085                       QStringList colNameList,
0086                       QVector<AbstractColumn::ColumnMode>,
0087                       bool initializeContainer) override;
0088     void finalizeImport(size_t columnOffset, size_t startColumn, size_t endColumn, const QString& dateTimeFormat, AbstractFileFilter::ImportMode) override;
0089     int resize(AbstractFileFilter::ImportMode, QStringList colNameList, int cols);
0090 
0091     struct Linking {
0092         bool linking{false};
0093         const Spreadsheet* linkedSpreadsheet{nullptr};
0094         QString linkedSpreadsheetPath;
0095 
0096         QString spreadsheetPath() const {
0097             if (linkedSpreadsheet)
0098                 return linkedSpreadsheet->path();
0099             return linkedSpreadsheetPath;
0100         }
0101     };
0102 
0103     typedef SpreadsheetPrivate Private;
0104 
0105 public Q_SLOTS:
0106     void appendRows(int);
0107     void appendRow();
0108     void removeEmptyRows();
0109     void maskEmptyRows();
0110     void appendColumns(int);
0111     void appendColumn();
0112     void prependColumns(int);
0113 
0114     void setColumnCount(int, QUndoCommand* parent = nullptr);
0115     void setRowCount(int, QUndoCommand* parent = nullptr);
0116 
0117     BASIC_D_ACCESSOR_DECL(bool, linking, Linking)
0118     const Spreadsheet* linkedSpreadsheet() const;
0119     void setLinkedSpreadsheet(const Spreadsheet*, bool skipUndo = false);
0120     QString linkedSpreadsheetPath() const;
0121 
0122     void clear();
0123     void clear(const QVector<Column*>&);
0124     void clearMasks();
0125 
0126     void moveColumn(int from, int to);
0127     void sortColumns(Column* leading, const QVector<Column*>&, bool ascending);
0128 
0129     void toggleStatisticsSpreadsheet(bool);
0130 
0131 private:
0132     void init();
0133     void initConnectionsLinking(const Spreadsheet* sender, const Spreadsheet* receiver);
0134     QVector<int> rowsWithMissingValues() const;
0135     Q_DECLARE_PRIVATE(Spreadsheet)
0136 
0137     SpreadsheetPrivate* const d_ptr;
0138     SpreadsheetModel* m_model{nullptr};
0139 
0140 protected:
0141     mutable SpreadsheetView* m_view{nullptr};
0142 
0143 private Q_SLOTS:
0144     void childSelected(const AbstractAspect*) override;
0145     void childDeselected(const AbstractAspect*) override;
0146     void linkedSpreadsheetDeleted();
0147     void linkedSpreadsheetNewRowCount(int);
0148 
0149 Q_SIGNALS:
0150     void requestProjectContextMenu(QMenu*);
0151     void columnSelected(int);
0152     void columnDeselected(int);
0153 
0154     // for spreadsheet dock
0155     void rowCountChanged(int);
0156     void columnCountChanged(int);
0157     void aboutToResize();
0158     void resizeFinished();
0159 
0160     void aspectsAboutToBeInserted(int first, int last);
0161     void aspectsInserted(int first, int last);
0162     void aspectsAboutToBeRemoved(int first, int last);
0163     void aspectsRemoved();
0164 
0165     void manyAspectsAboutToBeInserted();
0166     void rowsAboutToBeInserted(int before, int last);
0167     void rowsInserted(int newRowCount);
0168     void rowsAboutToBeRemoved(int first, int count);
0169     void rowsRemoved(int newRowCount);
0170 
0171     void linkingChanged(bool);
0172     void linkedSpreadsheetChanged(const Spreadsheet*);
0173 
0174     friend class SpreadsheetSetLinkingCmd;
0175     friend class SpreadsheetSetColumnCountCommand;
0176 };
0177 
0178 #endif