File indexing completed on 2024-05-12 05:39:38

0001 /***************************************************************************
0002  * Copyright (C) 2014 by Renaud Guezennec                                   *
0003  * http://www.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 FIELDMODEL_H
0023 #define FIELDMODEL_H
0024 
0025 #include <QAbstractItemModel>
0026 #include <QJsonObject>
0027 #include <QObject>
0028 #include <QTextStream>
0029 
0030 #include "charactersheet/charactersheetitem.h"
0031 #include "charactersheet/controllers/fieldcontroller.h"
0032 #include "charactersheet/controllers/section.h"
0033 
0034 class EditorController;
0035 
0036 namespace Formula
0037 {
0038 class FormulaManager;
0039 }
0040 /**
0041 s * @brief The Column class
0042  */
0043 class Column
0044 {
0045 
0046 public:
0047     /**
0048      * @brief Column
0049      */
0050     Column(QString, TreeSheetItem::ColumnId);
0051     /**
0052      * @brief getName
0053      * @return
0054      */
0055     QString getName() const;
0056     /**
0057      * @brief setName
0058      * @param name
0059      */
0060     void setName(const QString& name);
0061 
0062     /**
0063      * @brief getPos
0064      * @return
0065      */
0066     TreeSheetItem::ColumnId getPos() const;
0067     /**
0068      * @brief setPos
0069      * @param pos
0070      */
0071     void setPos(const TreeSheetItem::ColumnId& pos);
0072 
0073 private:
0074     QString m_name;
0075     TreeSheetItem::ColumnId m_pos;
0076 };
0077 
0078 /**
0079  * @brief The FieldModel class
0080  */
0081 class FieldModel : public QAbstractItemModel
0082 {
0083     Q_OBJECT
0084 public:
0085     explicit FieldModel(QObject* parent= nullptr);
0086     virtual ~FieldModel();
0087 
0088     // Model API
0089     QVariant data(const QModelIndex& index, int role) const override;
0090     QModelIndex index(int row, int column, const QModelIndex& parent) const override;
0091     QModelIndex parent(const QModelIndex& child) const override;
0092     int rowCount(const QModelIndex& parent) const override;
0093     int columnCount(const QModelIndex& parent) const override;
0094     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0095     bool setData(const QModelIndex& index, const QVariant& value, int role) override;
0096     Qt::ItemFlags flags(const QModelIndex& index) const override;
0097 
0098     void appendField(CSItem* f);
0099     void save(QJsonObject& json, bool exp= false);
0100     void load(const QJsonObject& json);
0101     void generateQML(QTextStream& out, int indentation, bool isTable= false);
0102     Q_INVOKABLE QString getValue(const QString& key);
0103     QList<TreeSheetItem*> children();
0104     QList<CSItem*> allChildren() const;
0105     void removePageId(int);
0106     Section* getRootSection() const;
0107     void setRootSection(Section* rootSection);
0108     void removeItem(QModelIndex& index);
0109     void removeField(FieldController* field);
0110     void setValueForAll(QModelIndex& index);
0111     void insertField(CSItem* field, TreeSheetItem* parent, int pos);
0112     void getFieldFromPage(int pagePos, QList<CSItem*>& list);
0113     FieldController* getFieldFromIndex(const QModelIndex& index);
0114     QRectF childrenRect() const;
0115 signals:
0116     void valuesChanged(QString valueKey, QString value);
0117     void modelChanged();
0118     void fieldAdded(CSItem* item);
0119 
0120 public slots:
0121     void updateItem(CSItem*);
0122     void clearModel();
0123     void resetAllId();
0124 
0125 private:
0126     QHash<QString, QString> buildDicto();
0127 
0128 private:
0129     std::unique_ptr<Section> m_rootSection;
0130     std::unique_ptr<Formula::FormulaManager> m_formulaManager;
0131     QList<Column*> m_colunm;
0132     QStringList m_alignList;
0133 };
0134 
0135 #endif // FIELDMODEL_H