File indexing completed on 2024-05-26 05:41:00

0001 /***************************************************************************
0002  * Copyright (C) 2014 by Renaud Guezennec                                   *
0003  * https://rolisteam.org/                                                *
0004  *                                                                          *
0005  *  This file is part of rcse                                               *
0006  *                                                                          *
0007  * rcse is free software; you can redistribute it and/or modify             *
0008  * it under the terms of the GNU General Public License as published by     *
0009  * the Free Software Foundation; either version 2 of the License, or        *
0010  * (at your option) any later version.                                      *
0011  *                                                                          *
0012  * rcse is distributed in the hope that it will be useful,                  *
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of           *
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
0015  * GNU General Public License for more details.                             *
0016  *                                                                          *
0017  * You should have received a copy of the GNU General Public License        *
0018  * along with this program; if not, write to the                            *
0019  * Free Software Foundation, Inc.,                                          *
0020  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.                 *
0021  ***************************************************************************/
0022 #ifndef TABLEFIELD_H
0023 #define TABLEFIELD_H
0024 
0025 #include "charactersheet/charactersheetitem.h"
0026 #include "fieldcontroller.h"
0027 #include <QGraphicsItem>
0028 #include <QLabel>
0029 #include <QObject>
0030 #include <QStandardItemModel>
0031 #include <QWidget>
0032 
0033 #include <charactersheet/charactersheet_global.h>
0034 
0035 class TableField;
0036 /**
0037  * @brief The LineFieldItem class
0038  */
0039 class CHARACTERSHEET_EXPORT LineFieldItem : public QObject
0040 {
0041     Q_OBJECT
0042 public:
0043     explicit LineFieldItem(QObject* parent= nullptr);
0044     ~LineFieldItem();
0045     void insertField(FieldController* field);
0046 
0047     Q_INVOKABLE FieldController* getField(int k) const;
0048 
0049     QList<FieldController*> getFields() const;
0050     void setFields(const QList<FieldController*>& fields);
0051 
0052     int getFieldCount() const;
0053 
0054     FieldController* getFieldById(const QString& id);
0055     FieldController* getFieldByLabel(const QString& label);
0056 
0057     void save(QJsonArray& json);
0058     void load(QJsonArray& json, TreeSheetItem* parent);
0059     void saveDataItem(QJsonArray& json);
0060     void loadDataItem(QJsonArray& json, TreeSheetItem* parent);
0061 
0062 private:
0063     QList<FieldController*> m_fields;
0064 };
0065 
0066 /**
0067  * @brief The LineModel class
0068  */
0069 class CHARACTERSHEET_EXPORT LineModel : public QAbstractListModel
0070 {
0071     Q_OBJECT
0072 public:
0073     enum customRole
0074     {
0075         LineRole= Qt::UserRole + 1
0076     };
0077     LineModel();
0078     int rowCount(const QModelIndex& parent) const;
0079     QVariant data(const QModelIndex& index, int role) const;
0080     bool setData(const QModelIndex& index, const QVariant& data, int role);
0081     QHash<int, QByteArray> roleNames() const;
0082     void insertLine(LineFieldItem* line);
0083     void appendLine(TableField* field);
0084     void clear();
0085     int childrenCount() const;
0086     int getColumnCount() const;
0087     FieldController* getField(int line, int col);
0088     FieldController* getFieldById(const QString& id);
0089     void removeLine(int index);
0090     void save(QJsonArray& json);
0091     void load(const QJsonArray& json, TreeSheetItem* parent);
0092     void saveDataItem(QJsonArray& json);
0093     void loadDataItem(const QJsonArray& json, TreeSheetItem* parent);
0094     void setChildFieldData(const QJsonObject& json);
0095     int sumColumn(const QString& name) const;
0096     void setFieldInDictionnary(QHash<QString, QString>& dict, const QString& id, const QString& label) const;
0097 
0098 private:
0099     QList<LineFieldItem*> m_lines;
0100 };
0101 
0102 /**
0103  * @brief The Field class managed text field in qml and datamodel.
0104  */
0105 class CHARACTERSHEET_EXPORT TableField : public FieldController
0106 {
0107     Q_OBJECT
0108     Q_PROPERTY(QAbstractItemModel* model READ getModel CONSTANT)
0109 
0110 public:
0111     enum ControlPosition
0112     {
0113         CtrlLeftTop,
0114         CtrlLeftBottom,
0115         CtrlTopLeft,
0116         CtrlTopRight,
0117         CtrlBottomLeft,
0118         CtrlBottomRight,
0119         CtrlRightTop,
0120         CtrlRightBottom
0121     };
0122     explicit TableField(bool addCount= true, QObject* parent= nullptr);
0123     explicit TableField(QPointF topleft, bool addCount= true, QObject* parent= nullptr);
0124     void fillModel();
0125     virtual ~TableField();
0126 
0127     LineModel* getModel() const;
0128 
0129     virtual bool mayHaveChildren() const override;
0130 
0131     // virtual void setCanvasField(CanvasField* canvasField) override;
0132 
0133     virtual QVariant valueFrom(TreeSheetItem::ColumnId, int role) const override;
0134 
0135     /// Overriden from charactersheetitem
0136     virtual bool hasChildren() override;
0137     virtual int childrenCount() const override;
0138     virtual TreeSheetItem* childFromId(const QString& id) const override;
0139     virtual TreeSheetItem* childAt(int) const override;
0140     virtual void save(QJsonObject& json, bool exp= false) override;
0141     virtual void load(const QJsonObject& json) override;
0142     virtual void copyField(TreeSheetItem* oldItem, bool copyData, bool sameId= true);
0143 
0144     ControlPosition getPosition() const;
0145     void setPosition(const ControlPosition& position);
0146 
0147     void saveDataItem(QJsonObject& json) override;
0148     void loadDataItem(const QJsonObject& json) override;
0149     void setChildFieldData(const QJsonObject& json);
0150 
0151     int getMaxVisibleRowCount() const;
0152 
0153     TreeSheetItem* getRoot();
0154     void appendChild(TreeSheetItem*) override;
0155     int lineNumber() const;
0156     int itemPerLine() const;
0157 
0158     Q_INVOKABLE int sumColumn(const QString& name) const;
0159     void setFieldInDictionnary(QHash<QString, QString>& dict) const override;
0160 
0161 public slots:
0162     void addLine();
0163     void removeLine(int line);
0164     void removeLastLine();
0165 
0166 signals:
0167     void lineMustBeAdded(TableField* table);
0168 
0169 protected:
0170     void init();
0171 
0172 protected:
0173     ControlPosition m_position;
0174     // TableCanvasField* m_tableCanvasField= nullptr;
0175     LineModel* m_model= nullptr;
0176 };
0177 
0178 #endif // TABLEFIELD_H