4#include "include/spreadsheet/private/spreadsheet_file.h"
5namespace ehm_dal::column_data {
12namespace ehm_dal::settings {
20class QAbstractTableModel;
26namespace ehm_dal::spreadsheet {
34 Spreadsheet(
const QString &folder_path,
const QString &file_name);
39 inline void add() { add(QVariant()); }
40 inline void add(
const QString &text) { add(QVariant(text)); }
41 void add(
const QVariant &data);
42 void add(
const QVariant &data,
const qint32
row);
43 void add(
const QVariant &data,
const qint32 start_row,
const qint32 row_count);
62 const Qt::ItemDataRole role = Qt::DisplayRole);
71 const std::vector<qint32> &rows,
72 const Qt::ItemDataRole role = Qt::DisplayRole);
82 const Qt::ItemDataRole role = Qt::DisplayRole);
92 const Qt::ItemDataRole role = Qt::DisplayRole);
101 const std::vector<qint32> &rows,
102 const Qt::ItemDataRole role = Qt::DisplayRole);
111 void add(QAbstractTableModel *model,
const Qt::ItemDataRole role = Qt::DisplayRole);
119 void add(QAbstractTableModel *model,
121 const Qt::ItemDataRole role = Qt::DisplayRole);
129 void add(QAbstractTableModel *model,
130 const std::vector<qint32> &rows,
131 const Qt::ItemDataRole role = Qt::DisplayRole);
147 const std::vector<qint32> &column_index_list,
148 const Qt::ItemDataRole role = Qt::DisplayRole);
154 void add(
const std::vector<ehm_dal::data_types::Pointer> &pointer_list,
155 const Qt::ItemDataRole role = Qt::DisplayRole);
162 void add(
const std::vector<ehm_dal::data_types::Pointer> &pointer_list,
163 const std::vector<qint32> &column_index_list,
164 const Qt::ItemDataRole role = Qt::DisplayRole);
175 inline void addPrefix(
const QVariant &data) { prefix_cell_data_.push_back(data); }
216 void addHeader(QAbstractTableModel *model,
const std::vector<qint32> &column_index_list);
227 inline void addHeaderPrefix(
const QVariant &data) { prefix_header_data_.push_back(data); }
287 inline QString
identifier() {
return headerCell(0,0).toString().replace(
" ",
"_").toLower(); }
334 bool open(
const QString &file_path);
341 bool open(
const QString &folder,
const QString &file_name);
353 bool save(
const QString &folder,
const QString &file_name);
381 bool showSaveDialog(
const QString &file_name =
"output.csv", QString file_path = QString());
395 QVariant
cell(
const qint32
row,
const qint32 col,
const bool return_as_date =
false)
const;
404 const qint32 col_count)
const;
421 std::span<const QVariant>
row(
const qint32
row)
const;
426 inline qint32
rowCount()
const {
return static_cast<qint32
>(cell_data_.size()); }
429 QVariant prefix(
const qint32
row)
const;
430 inline bool hasPrefixData()
const {
return prefixCount() > 0; }
431 inline qint32 prefixCount()
const {
return static_cast<qint32
>(prefix_cell_data_.size()); }
434 std::span<const QVariant> header(
const qint32
row)
const;
435 QVariant headerCell(
const qint32
row,
const quint16 col)
const;
436 inline qint32 headerCount()
const
438 return std::max(
static_cast<qint32
>(header_data_.size()), header_count_);
442 QVariant prefixHeader(
const qint32
row)
const;
443 inline bool hasPrefixHeaderData()
const {
return prefixHeaderCount() > 0; }
444 inline qint32 prefixHeaderCount()
const
446 return static_cast<qint32
>(prefix_header_data_.size());
486 std::vector<std::vector<QVariant>> cell_data_;
487 std::vector<std::vector<QVariant>> header_data_;
488 std::vector<QVariant> prefix_cell_data_;
489 std::vector<QVariant> prefix_header_data_;
505 const Qt::ItemDataRole role = Qt::DisplayRole);
506 void addFromTableModel(QAbstractTableModel *model,
508 const qint32 column_count,
509 const Qt::ItemDataRole role = Qt::DisplayRole);
510 void addFromTreeModel(QAbstractTableModel *model,
511 const qint32 parent_row,
512 const qint32 column_count,
513 const Qt::ItemDataRole role = Qt::DisplayRole);
516 std::vector<qint32> columnList(
const qint32 column_count)
const;
519 QDate toDate(
const QVariant &value)
const;
522 static QString date_format_;
525 quint8 file_extension_;
530 static QHash<QString, quint8> fileExtensions();
531 static QString filterText(
const qint32 type);
534 enum ENUM_FILE_EXTENSIONS { CSV, XLSX, FILE_EXTENSION_COUNT };
537 qint32 header_count_{2};
540 bool hide_progress_{
false};
543 std::vector<QVariant> *currentHeaderRow();
544 std::vector<QVariant> *currentRow();
The ColumnIndexList class is a container/list of ehm_dal::column_data::Column indexes from ehm_dal:...
Definition: column_index_list.h:24
The Pointer class represents a pointer to a table item based on ID.
Definition: pointer.h:21
Definition: folder_path.h:11
Definition: spreadsheet.h:30
qint32 rowCount() const
Returns the data row count.
Definition: spreadsheet.h:426
QString fileNameAndExtension()
Returns the file and extension of the output spreadsheet.
Definition: spreadsheet.h:276
std::span< const QVariant > cells(const qint32 row, const qint32 col, const qint32 col_count) const
Returns the cell data as a QDate located at row and col.
void add(const ehm_dal::data_types::Pointer &pointer, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds all columns of the selected Pointer to the spreadsheet.
void addNewHeaderRow()
Adds a new header row.
void addHeader(const QStringList &text_list)
Adds the text_list to the current header row.
QString identifier()
Returns the identifier text string located at the top left cell (i.e. Cell A1).
Definition: spreadsheet.h:287
void add(const ehm_dal::column_data::ColumnIndexList &columns, const std::vector< qint32 > &rows, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds selected rows and columns to the spreadsheet. This is generally used to export a ehm_dal::tables...
void add(const ehm_dal::column_data::ColumnIndexList &columns, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds all rows and selected columns to the spreadsheet. This is generally used to export a ehm_dal::ta...
QString file()
Returns the file path, file base name and file extension.
void add(const ehm_dal::column_data::ColumnIndexList &columns, const qint32 row, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds selected row and columns to the spreadsheet. This is generally used to export a ehm_dal::tables:...
QString fileName()
Returns the file name of the output spreadsheet without the file extension.
Definition: spreadsheet.h:272
bool showSaveDialog(const QString &file_name="output.csv", QString file_path=QString())
Displays a dialog window allowing the user to save the spreadsheet.
void add(QAbstractTableModel *model, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds all rows and columns from model to the spreadsheet. This is generally used to export a QAbstract...
void add(QAbstractTableModel *model, const std::vector< qint32 > &rows, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds the selected rows and all columns from model to the spreadsheet. This is generally used to expor...
static void setDefaultFileExtension(const qint32 i)
Set the default file extension to i.
static void setDateFormat(const DateFormat date_format)
Sets the data format to date_format when returning cell data. This can be modified before or after re...
void addHeaderPrefix(const QVariant &data)
Adds data to the current prefix header row.
Definition: spreadsheet.h:227
qint32 columnCount(const qint32 row) const
Returns the column count for the selected row.
static QString dateFormatString()
Returns the current date format used when returning cell data.
Definition: spreadsheet.h:246
void add(const std::vector< ehm_dal::data_types::Pointer > &pointer_list, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds all columns of the selected Pointers to the spreadsheet.
static QStringList fileExtensionListAsStringList()
Returns a list of supported file extensions.
void addPrefix(const QVariant &data)
Adds data to the current prefix row.
Definition: spreadsheet.h:175
bool showSaveDialog(settings::FolderPath &path)
Displays a dialog window allowing the user to save the spreadsheet.
void addPrefix()
Adds a blank cell to the current prefix row.
Definition: spreadsheet.h:170
QString fileExtension() const
Returns the file extension of the output spreadsheet.
bool save(const QString &folder, const QString &file_name)
Saves the spreadsheet to the selected folder path and file name.
bool read()
Reads the current file.
bool columnContainsData(const qint32 column) const
Returns whether a column contains any data.
void add(ehm_dal::tables::AbstractTable *model, const std::vector< qint32 > &rows, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds the selected rows and all columns from model to the spreadsheet. This is generally used to expor...
static QString fileExtensionListAsString(const bool merged_list=true)
Returns a list of supported file extensions.
bool showOpenDialog(QString file_path=QString(), const bool read_data=true)
Displays a dialog window allowing the user to select a spreadsheet to open.
static QString importPath()
Returns the default file path used for importing data.
void addHeader()
Adds a blank cell to the current header row.
Definition: spreadsheet.h:181
DateFormat
The DateFormat enum represents the date format used when returning cell data.
Definition: spreadsheet.h:233
bool setFile(const QString &path)
Set the file path, name and extension from path.
QString folderPath() const
Returns the file path.
Definition: spreadsheet.h:283
bool write()
Writes/saves the spreadsheet to the output file.
void setHeaderCount(const qint32 header_row_count)
Sets the number of header rows to header_row_count.
void add(ehm_dal::tables::AbstractTable *model, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds all rows and columns from model to the spreadsheet. This is generally used to export a ehm_dal::...
QVariant cell(const qint32 row, const qint32 col, const bool return_as_date=false) const
Returns the data located at row and col.
void addHeader(const QString &text)
Adds text to the current header row.
Definition: spreadsheet.h:186
static qint32 defaultFileExtension()
Returns the default file extension.
void add(const std::vector< ehm_dal::data_types::Pointer > &pointer_list, const std::vector< qint32 > &column_index_list, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds the selected columns of the selected Pointers to the spreadsheet.
std::span< const QVariant > row(const qint32 row) const
Returns the data of the entirety of the selected row.
void add(QAbstractTableModel *model, const qint32 row, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds the selected row and all columns from model to the spreadsheet. This is generally used to export...
bool setFileName(const QString &file_name)
Set the file name from file_name.
void addHeader(const ehm_dal::column_data::ColumnIndexList &columns)
Adds a header row of column names corresponding to the columns.
void addHeader(QAbstractTableModel *model)
Adds a header row of column names of all columns from the 'model'. This is typically used to add the ...
void addHeader(QAbstractTableModel *model, const std::vector< qint32 > &column_index_list)
Adds a header row of column names of the selected columns from the 'model'. This is typically used to...
std::unique_ptr< SpreadsheetFile > newSpreadsheet(QFile &f)
Creates a new spreadsheet from f and returns a pointer to the created ehm_dal::spreadsheet::Spreadshe...
void addNewRow()
Adds a new data row.
void addHeaderPrefix()
Adds a blank cell to the current prefix header row.
Definition: spreadsheet.h:222
void printDiagnosticData() const
Prints various diagnostic data to the console.
bool open(const QString &folder, const QString &file_name)
Opens a file without showing a dialog window.
static DateFormat dateFormat()
Returns the current date format used when returning cell data.
static QString exportPath()
Returns the default file path used for exporting data.
void addFileNameSuffix(const QString &suffix)
Adds suffix to the end of the file name.
void add(ehm_dal::tables::AbstractTable *model, const qint32 row, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds the selected row and all columns from model to the spreadsheet. This is generally used to export...
void add(const ehm_dal::data_types::Pointer &pointer, const std::vector< qint32 > &column_index_list, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds the selected columns of the selected Pointer to the spreadsheet.
bool showOpenDialog(settings::FolderPath &path, const bool read_data=true)
Displays a dialog window allowing the user to select a spreadsheet to open.
bool open(const QString &file_path)
Opens a file without showing a dialog window.
void addHeader(const QVariant &data)
Adds data to the current header row.
The Table class represents a database table and provides access to rows of data.
Definition: abstract_table.h:29
namespace ehm_dal::data_types
Definition: attribute.h:6
namespace ehm_dal::tables
Definition: column.h:4