EHM DAL 0.2.3
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::data_types {
6class Pointer;
7}
8namespace ehm_dal::settings {
9class FolderPath;
10}
11namespace ehm_dal::tables {
12class AbstractTable;
13}
14
15// Qt headers
16class QAbstractTableModel;
17#include <QString>
18#include <QVariant>
19#include <span>
20#include <vector>
21
22namespace ehm_dal::spreadsheet {
23
24// --- Spreadsheet --- //
26{
27public:
28 // Constructor
29 Spreadsheet(const QString &file_path = QString());
32
33 // Add data - cells/rows: basic
34 inline void add() { add(QVariant()); }
35 inline void add(const QString &text) { add(QVariant(text)); }
36 void add(const QVariant &data);
37 void add(const QVariant &data, const qint32 row);
38 void add(const QVariant &data, const qint32 start_row, const qint32 row_count);
39
40 // Add data - cells/rows: AbstractTable data
47 void add(ehm_dal::tables::AbstractTable *model, const Qt::ItemDataRole role = Qt::DisplayRole);
56 const std::vector<qint32> &column_index_list,
57 const Qt::ItemDataRole role = Qt::DisplayRole);
66 const qint32 row,
67 const Qt::ItemDataRole role = Qt::DisplayRole);
77 const qint32 row,
78 const std::vector<qint32> &column_index_list,
79 const Qt::ItemDataRole role = Qt::DisplayRole);
80
81 // Add data - cells/rows: QAbstractTableModel data
88 void add(QAbstractTableModel *model, const Qt::ItemDataRole role = Qt::DisplayRole);
96 void add(QAbstractTableModel *model,
97 const std::vector<qint32> &column_index_list,
98 const Qt::ItemDataRole role = Qt::DisplayRole);
106 void add(QAbstractTableModel *model,
107 const qint32 row,
108 const Qt::ItemDataRole role = Qt::DisplayRole);
117 void add(QAbstractTableModel *model,
118 const qint32 row,
119 const std::vector<qint32> &column_index_list,
120 const Qt::ItemDataRole role = Qt::DisplayRole);
121
122 // Add data - cells/rows: Pointer data
128 void add(const ehm_dal::data_types::Pointer &pointer, const Qt::ItemDataRole role = Qt::DisplayRole);
136 const std::vector<qint32> &column_index_list,
137 const Qt::ItemDataRole role = Qt::DisplayRole);
143 void add(const std::vector<ehm_dal::data_types::Pointer> &pointer_list,
144 const Qt::ItemDataRole role = Qt::DisplayRole);
151 void add(const std::vector<ehm_dal::data_types::Pointer> &pointer_list,
152 const std::vector<qint32> &column_index_list,
153 const Qt::ItemDataRole role = Qt::DisplayRole);
154
155 // Add data - prefix cells
159 inline void addPrefix() { addPrefix(QVariant()); }
164 inline void addPrefix(const QVariant &data) { prefix_cell_data_.push_back(data); }
165
166 // Add data - header: basic
170 inline void addHeader() { add(QVariant()); }
175 inline void addHeader(const QString &text) { addHeader(QVariant(text)); }
180 void addHeader(const QStringList &text_list);
185 void addHeader(const QVariant &data);
186
187 // Add data - header: model data
193 void addHeader(QAbstractTableModel *model);
200 void addHeader(QAbstractTableModel *model, const std::vector<qint32> &column_index_list);
201
202 // Add data - prefix header
206 inline void addHeaderPrefix() { addHeaderPrefix(QVariant()); }
211 inline void addHeaderPrefix(const QVariant &data) { prefix_header_data_.push_back(data); }
212
213 // Date format
217 enum class DateFormat {
221 };
222
230 inline static QString dateFormatString() { return date_format_; }
235 static void setDateFormat(const DateFormat date_format);
236
237 // Debugging / diagnostics
242
243 // File details - get data
247 QString file();
251 inline QString fileName() { return file_name_; }
256 QString fileType() const;
260 inline QString folderPath() const { return path_; }
264 inline QString identifier() { return headerCell(0,0).toString().replace(" ", "_").toLower(); }
265
266 // File details - set data
271 void addFileNameSuffix(const QString &suffix);
277 bool setFile(const QString &path);
278
279 // File extensions
283 static qint32 defaultFileExtension();
288 static QString fileExtensionListAsString(const bool merged_list = true);
292 static QStringList fileExtensionListAsStringList();
297 static void setDefaultFileExtension(const qint32 i);
298
299 // File I/O
305 bool open(const QString &file_path);
312 bool open(const QString &folder, const QString &file_name);
317 bool read();
324 bool save(const QString &folder, const QString &file_name);
329 bool write();
330
331 // File I/O dialog windows
338 bool showOpenDialog(QString file_path = QString(), const bool read_data = true);
345 bool showOpenDialog(settings::FolderPath &path, const bool read_data = true);
352 bool showSaveDialog(const QString &file_name = "output.csv", QString file_path = QString());
359
360 // Get data - cells
366 QVariant cell(const qint32 row, const qint32 col, const bool return_as_date = false) const;
373 std::span<const QVariant> cells(const qint32 row,
374 const qint32 col,
375 const qint32 col_count) const;
380 bool columnContainsData(const qint32 column) const;
386 qint32 columnCount(const qint32 row) const;
392 std::span<const QVariant> row(const qint32 row) const;
397 inline qint32 rowCount() const { return static_cast<qint32>(cell_data_.size()); }
398
399 // Get data - prefix cells
400 QVariant prefix(const qint32 row) const;
401 inline bool hasPrefixData() const { return prefixCount() > 0; }
402 inline qint32 prefixCount() const { return static_cast<qint32>(prefix_cell_data_.size()); }
403
404 // Get data - header
405 std::span<const QVariant> header(const qint32 row) const;
406 QVariant headerCell(const qint32 row, const quint16 col) const;
407 inline qint32 headerCount() const
408 {
409 return std::max(static_cast<qint32>(header_data_.size()), header_count_);
410 }
411
412 // Get data - header
413 QVariant prefixHeader(const qint32 row) const;
414 inline bool hasPrefixHeaderData() const { return prefixHeaderCount() > 0; }
415 inline qint32 prefixHeaderCount() const
416 {
417 return static_cast<qint32>(prefix_header_data_.size());
418 }
419
420 // Get settings
424 static QString exportPath();
428 static QString importPath();
429
430 // Row data: add data
438 void addNewRow();
439
440 // Set header data
445 void setHeaderCount(const qint32 header_row_count);
446
447 // Spreadsheet file
453 std::unique_ptr<SpreadsheetFile> newSpreadsheet(QFile &f);
454
455private:
456 // Data
457 std::vector<std::vector<QVariant>> cell_data_;
458 std::vector<std::vector<QVariant>> header_data_;
459 std::vector<QVariant> prefix_cell_data_;
460 std::vector<QVariant> prefix_header_data_;
461
462 // Add data - cells/rows: Private model data methods
463 void addFromTableModel(QAbstractTableModel *model,
464 const std::vector<qint32> &column_index_list,
465 const Qt::ItemDataRole role = Qt::DisplayRole);
466 void addFromTableModel(QAbstractTableModel *model,
467 const qint32 row,
468 const std::vector<qint32> &column_index_list,
469 const Qt::ItemDataRole role = Qt::DisplayRole);
470 void addFromTreeModel(QAbstractTableModel *model,
471 const std::vector<qint32> &column_index_list,
472 const Qt::ItemDataRole role = Qt::DisplayRole);
473 void addFromTreeModel(QAbstractTableModel *model,
474 const qint32 parent_row,
475 const std::vector<qint32> &column_index_list,
476 const Qt::ItemDataRole role = Qt::DisplayRole);
477
478 // Column data
479 std::vector<qint32> columnList(const qint32 column_count) const;
480
481 // Convert value
482 QDate toDate(const QVariant &value) const;
483
484 // Date format
485 static QString date_format_;
486
487 // File data
488 quint8 file_extension_;
489 QString file_name_;
490 QString path_;
491
492 // File extensions (functions)
493 static QHash<QString, quint8> fileExtensions();
494 static QString filterText(const qint32 type);
495
496 // File extensions (members)
497 enum ENUM_FILE_EXTENSIONS { CSV, XLSX, FILE_EXTENSION_COUNT };
498
499 // Header
500 qint32 header_count_{2};
501
502 // Progress display
503 bool hide_progress_{false};
504
505 // Row data: get data
506 std::vector<QVariant> *currentHeaderRow();
507 std::vector<QVariant> *currentRow();
508};
509} // namespace ehm_dal::spreadsheet
The Pointer class represents a pointer to a table item.
Definition: pointer.h:21
Definition: folder_path.h:11
Definition: spreadsheet.h:26
qint32 rowCount() const
Returns the data row count.
Definition: spreadsheet.h:397
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:264
QString file()
Returns the file path, file base name and file extension.
QString fileName()
Returns the file path and name of the output spreadsheet.
Definition: spreadsheet.h:251
void add(ehm_dal::tables::AbstractTable *model, const qint32 row, const std::vector< qint32 > &column_index_list, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds the selected row and selected columns from model to the spreadsheet. This is generally used to e...
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...
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:211
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:230
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:164
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:159
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.
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:170
DateFormat
The DateFormat enum represents the date format used when returning cell data.
Definition: spreadsheet.h:217
bool setFile(const QString &path)
Set the file path, name and extension from path.
QString folderPath() const
Returns the file path.
Definition: spreadsheet.h:260
void add(QAbstractTableModel *model, const qint32 row, const std::vector< qint32 > &column_index_list, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds the selected row and selected columns from model to the spreadsheet. This is generally used to e...
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::...
void add(QAbstractTableModel *model, const std::vector< qint32 > &column_index_list, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds all rows and the selected columns from model to the spreadsheet. This is generally used to expor...
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:175
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...
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 columnd 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:206
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.
void add(ehm_dal::tables::AbstractTable *model, const std::vector< qint32 > &column_index_list, const Qt::ItemDataRole role=Qt::DisplayRole)
Adds all rows and the selected columns from model to the spreadsheet. This is generally used to expor...
bool showOpenDialog(settings::FolderPath &path, const bool read_data=true)
Displays a dialog window allowing the user to select a spreadsheet to open.
QString fileType() const
Returns the file type of the output spreadsheet.
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