EHM DAL 0.2.5
Data abstraction layer for Eastside Hockey Manager
Loading...
Searching...
No Matches
spreadsheet.h
1#pragma once
2
3// Application headers
4#include "include/spreadsheet/private/spreadsheet_file.h"
5namespace ehm_dal::column_data {
6class ColumnIndexList;
7}
8
9namespace ehm_dal::data_types {
10class Pointer;
11}
12namespace ehm_dal::settings {
13class FolderPath;
14}
15namespace ehm_dal::tables {
16class AbstractTable;
17}
18
19// Qt headers
20class QAbstractTableModel;
21#include <QString>
22#include <QVariant>
23#include <span>
24#include <vector>
25
26namespace ehm_dal::spreadsheet {
27
28// --- Spreadsheet --- //
30{
31public:
32 // Constructor
33 Spreadsheet(const QString &file_path = QString());
34 Spreadsheet(const QString &folder_path, const QString &file_name);
37
38 // Add data - cells/rows: basic
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);
44
45 // Add data - cells/rows: AbstractTable data
52 void add(ehm_dal::tables::AbstractTable *model, const Qt::ItemDataRole role = Qt::DisplayRole);
61 const qint32 row,
62 const Qt::ItemDataRole role = Qt::DisplayRole);
71 const std::vector<qint32> &rows,
72 const Qt::ItemDataRole role = Qt::DisplayRole);
73
74 // Add data - cells/rows: Column index list
82 const Qt::ItemDataRole role = Qt::DisplayRole);
91 const qint32 row,
92 const Qt::ItemDataRole role = Qt::DisplayRole);
101 const std::vector<qint32> &rows,
102 const Qt::ItemDataRole role = Qt::DisplayRole);
103
104 // Add data - cells/rows: QAbstractTableModel data
111 void add(QAbstractTableModel *model, const Qt::ItemDataRole role = Qt::DisplayRole);
119 void add(QAbstractTableModel *model,
120 const qint32 row,
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);
132
133 // Add data - cells/rows: Pointer data
139 void add(const ehm_dal::data_types::Pointer &pointer, 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);
165
166 // Add data - prefix cells
170 inline void addPrefix() { addPrefix(QVariant()); }
175 inline void addPrefix(const QVariant &data) { prefix_cell_data_.push_back(data); }
176
177 // Add data - header: basic
181 inline void addHeader() { add(QVariant()); }
186 inline void addHeader(const QString &text) { addHeader(QVariant(text)); }
191 void addHeader(const QStringList &text_list);
196 void addHeader(const QVariant &data);
197
198 // Add data - header: model data
204 void addHeader(QAbstractTableModel *model);
216 void addHeader(QAbstractTableModel *model, const std::vector<qint32> &column_index_list);
217
218 // Add data - prefix header
222 inline void addHeaderPrefix() { addHeaderPrefix(QVariant()); }
227 inline void addHeaderPrefix(const QVariant &data) { prefix_header_data_.push_back(data); }
228
229 // Date format
233 enum class DateFormat {
237 };
238
246 inline static QString dateFormatString() { return date_format_; }
251 static void setDateFormat(const DateFormat date_format);
252
253 // Debugging / diagnostics
258
259 // File details - get data
263 QString file();
268 QString fileExtension() const;
272 inline QString fileName() { return file_name_; }
276 inline QString fileNameAndExtension()
277 {
278 return QStringLiteral("%1.%2").arg(fileName(), fileExtension());
279 }
283 inline QString folderPath() const { return path_; }
287 inline QString identifier() { return headerCell(0,0).toString().replace(" ", "_").toLower(); }
288
289 // File details - set data
294 void addFileNameSuffix(const QString &suffix);
300 bool setFile(const QString &path);
306 bool setFileName(const QString &file_name);
307
308 // File extensions
312 static qint32 defaultFileExtension();
317 static QString fileExtensionListAsString(const bool merged_list = true);
321 static QStringList fileExtensionListAsStringList();
326 static void setDefaultFileExtension(const qint32 i);
327
328 // File I/O
334 bool open(const QString &file_path);
341 bool open(const QString &folder, const QString &file_name);
346 bool read();
353 bool save(const QString &folder, const QString &file_name);
358 bool write();
359
360 // File I/O dialog windows
367 bool showOpenDialog(QString file_path = QString(), const bool read_data = true);
374 bool showOpenDialog(settings::FolderPath &path, const bool read_data = true);
381 bool showSaveDialog(const QString &file_name = "output.csv", QString file_path = QString());
388
389 // Get data - cells
395 QVariant cell(const qint32 row, const qint32 col, const bool return_as_date = false) const;
402 std::span<const QVariant> cells(const qint32 row,
403 const qint32 col,
404 const qint32 col_count) const;
409 bool columnContainsData(const qint32 column) const;
415 qint32 columnCount(const qint32 row) const;
421 std::span<const QVariant> row(const qint32 row) const;
426 inline qint32 rowCount() const { return static_cast<qint32>(cell_data_.size()); }
427
428 // Get data - prefix cells
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()); }
432
433 // Get data - header
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
437 {
438 return std::max(static_cast<qint32>(header_data_.size()), header_count_);
439 }
440
441 // Get data - header
442 QVariant prefixHeader(const qint32 row) const;
443 inline bool hasPrefixHeaderData() const { return prefixHeaderCount() > 0; }
444 inline qint32 prefixHeaderCount() const
445 {
446 return static_cast<qint32>(prefix_header_data_.size());
447 }
448
449 // Get settings
453 static QString exportPath();
457 static QString importPath();
458
459 // Row data: add data
467 void addNewRow();
468
469 // Set header data
474 void setHeaderCount(const qint32 header_row_count);
475
476 // Spreadsheet file
482 std::unique_ptr<SpreadsheetFile> newSpreadsheet(QFile &f);
483
484private:
485 // Data
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_;
490
491 // Add data - cells/rows: Private model data methods
492 /*void addFromDualTableModels(QAbstractTableModel *lhs_model,
493 const std::vector<qint32> &lhs_column_index_list,
494 QAbstractTableModel *rhs_model,
495 const std::vector<qint32> &rhs_column_index_list,
496 const Qt::ItemDataRole role = Qt::DisplayRole);
497 void addFromDualTableModels(QAbstractTableModel *lhs_model,
498 const qint32 lhs_row,
499 const std::vector<qint32> &lhs_column_index_list,
500 QAbstractTableModel *rhs_model,
501 const qint32 rhs_row,
502 const std::vector<qint32> &rhs_column_index_list,
503 const Qt::ItemDataRole role = Qt::DisplayRole);*/
504 void addFromTableModel(const column_data::ColumnIndexList &columns,
505 const Qt::ItemDataRole role = Qt::DisplayRole);
506 void addFromTableModel(QAbstractTableModel *model,
507 const qint32 row,
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);
514
515 // Column data
516 std::vector<qint32> columnList(const qint32 column_count) const;
517
518 // Convert value
519 QDate toDate(const QVariant &value) const;
520
521 // Date format
522 static QString date_format_;
523
524 // File data
525 quint8 file_extension_;
526 QString file_name_;
527 QString path_;
528
529 // File extensions (functions)
530 static QHash<QString, quint8> fileExtensions();
531 static QString filterText(const qint32 type);
532
533 // File extensions (members)
534 enum ENUM_FILE_EXTENSIONS { CSV, XLSX, FILE_EXTENSION_COUNT };
535
536 // Header
537 qint32 header_count_{2};
538
539 // Progress display
540 bool hide_progress_{false};
541
542 // Row data: get data
543 std::vector<QVariant> *currentHeaderRow();
544 std::vector<QVariant> *currentRow();
545};
546} // namespace ehm_dal::spreadsheet
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