Warning, file /office/skrooge/skgbasegui/skgobjectmodelbase.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 #ifndef SKGOBJECTMODELBASE_H
0007 #define SKGOBJECTMODELBASE_H
0008 /** @file
0009  * This file defines classes skgbjectmodelbase.
0010  *
0011  * @author Stephane MANKOWSKI / Guillaume DE BURE
0012  */
0013 
0014 #include <qabstractitemmodel.h>
0015 #include <qstringlist.h>
0016 
0017 #include "skgbasegui_export.h"
0018 #include "skgdocument.h"
0019 #include "skgobjectbase.h"
0020 
0021 class QWidget;
0022 /**
0023  * The Table model managing SKGObjectBase
0024  */
0025 class SKGBASEGUI_EXPORT SKGObjectModelBase : public QAbstractItemModel
0026 {
0027     Q_OBJECT
0028 public:
0029     /**
0030      * Default constructor
0031      * @param iDocument the document where to search
0032      * @param iTable the table where to search
0033      * @param iWhereClause the where clause
0034      * @param iParent parent QT object
0035      * @param iParentAttribute the attribute to find the parent of an object clause to find children
0036      * @param iResetOnCreation to reset data during creation
0037      */
0038     SKGObjectModelBase(SKGDocument* iDocument,
0039                        const QString& iTable,
0040                        QString  iWhereClause,
0041                        QWidget* iParent,
0042                        QString  iParentAttribute = QString(),
0043                        bool iResetOnCreation = true);
0044 
0045     /**
0046      * Destructor
0047      */
0048     ~SKGObjectModelBase() override;
0049 
0050     /**
0051      * Allows to block a refresh
0052      * @param iBlocked blocking status
0053      * @return previous value
0054      */
0055     virtual bool blockRefresh(bool iBlocked);
0056 
0057     /**
0058      * To know if the refresh is blocked
0059      * @return true or false
0060      */
0061     virtual bool isRefreshBlocked();
0062 
0063     /**
0064      * Returns true if parent has at least one child.
0065      * @param iParent the parent
0066      * @return true or false
0067      */
0068     bool hasChildren(const QModelIndex& iParent = QModelIndex()) const override;
0069 
0070     /**
0071      * Returns the number of row for the given parent.
0072      * @param iParent the parent
0073      * @return the number of row
0074      */
0075     int rowCount(const QModelIndex& iParent = QModelIndex()) const override;
0076 
0077     /**
0078      * Returns the index of the item in the model specified by the given row, column and parent index.
0079      * @param row row
0080      * @param column column
0081      * @param iParent parent
0082      * @return the index
0083      */
0084     QModelIndex index(int row, int column, const QModelIndex& iParent = QModelIndex()) const override;
0085 
0086     /**
0087      * Returns the parent of the model item with the given index, or QModelIndex() if it has no parent.
0088      * @param iIndex index
0089      * @return the parent
0090      */
0091     QModelIndex parent(const QModelIndex& iIndex) const override;
0092 
0093     /**
0094      * Returns the number of columns for the given parent.
0095      * @param iParent the parent
0096      * @return the number of column
0097      */
0098     int columnCount(const QModelIndex& iParent = QModelIndex()) const override;
0099 
0100     /**
0101      * Returns the data stored under the given role for the item referred to by the index.
0102      * @param iIndex the index
0103      * @param iRole the role
0104      * @return the returned value
0105      */
0106     QVariant data(const QModelIndex& iIndex, int iRole = Qt::DisplayRole) const override;
0107 
0108     /**
0109      * Returns the data stored under the given role for the item referred to by the index.
0110      * @param iIndex the index
0111      * @param iRole the role
0112      * @return the returned value
0113      */
0114     virtual QVariant computeData(const QModelIndex& iIndex, int iRole = Qt::DisplayRole) const;
0115 
0116     /**
0117      * Returns the SKGObjectBase for the item referred to by the index.
0118      * @param iIndex the index
0119      * @return the returned SKGObjectBase
0120      */
0121     virtual SKGObjectBase getObject(const QModelIndex& iIndex) const;
0122 
0123     /**
0124      * Returns the SKGObjectBase for the item referred to by the index.
0125      * @param iIndex the index
0126      * @return the returned SKGObjectBase. Do not delete it !
0127      */
0128     virtual SKGObjectBase* getObjectPointer(const QModelIndex& iIndex) const;
0129 
0130     /**
0131      * Returns the data for the given role and section in the header with the specified orientation.
0132      * @param iSection the section
0133      * @param iOrientation the orientation
0134      * @param iRole the role
0135      * @return the header data
0136      */
0137     QVariant headerData(int iSection, Qt::Orientation iOrientation, int iRole = Qt::DisplayRole) const override;
0138 
0139     /**
0140      * Returns the item flags for the given index.
0141      * @param iIndex index of the object
0142      * @return flags of the given index
0143      */
0144     Qt::ItemFlags flags(const QModelIndex& iIndex) const override;
0145 
0146     /**
0147      * Sets the role data for the item at index to value. Returns true if successful; otherwise returns false.
0148      * @param iIndex index of the object
0149      * @param iValue value
0150      * @param iRole role
0151      * @return
0152      */
0153     bool setData(const QModelIndex& iIndex, const QVariant& iValue, int iRole = Qt::EditRole) override;
0154 
0155     /**
0156      * Return the index of the attribute, -1 if not found
0157      * @param iAttributeName the attribute name
0158      * @return index of this attribute
0159      */
0160     virtual int getIndexAttribute(const QString& iAttributeName) const;
0161 
0162     /**
0163      * Return the attribute for an index
0164      * @param iIndex the index
0165      * @return attribute
0166      */
0167     virtual QString getAttribute(int iIndex) const;
0168 
0169     /**
0170      * Return the type of attribute for an index
0171      * @param iIndex the index
0172      * @return type of attribute
0173      */
0174     virtual SKGServices::AttributeType getAttributeType(int iIndex) const;
0175 
0176     /**
0177      * Set the table. Do not forget to do a reset after that.
0178      * @param iTable the table name
0179      */
0180     void setTable(const QString& iTable);
0181 
0182     /**
0183      * Get table name
0184      * @return table name
0185      */
0186     virtual QString getTable() const;
0187 
0188     /**
0189      * Set the attribute used for grouping
0190      * @param iAttribute the attribute name
0191      */
0192     virtual void setGroupBy(const QString& iAttribute = QString());
0193 
0194     /**
0195      * Get the attribute used for grouping
0196      * @return attribute name
0197      */
0198     virtual QString getGroupBy() const;
0199 
0200     /**
0201      * Get the attribute used for parent / child relationship
0202      * @return attribute name
0203      */
0204     virtual QString getParentChildAttribute() const;
0205 
0206     /**
0207      * Get real table name
0208      * @return real table name
0209      */
0210     virtual QString getRealTable() const;
0211 
0212     /**
0213      * Get where clause
0214      * @return where clause
0215      */
0216     virtual QString getWhereClause() const;
0217 
0218     /**
0219      * Get document
0220      * @return document
0221      */
0222     virtual SKGDocument* getDocument() const;
0223 
0224     /**
0225      * Set the list of supported attributes. Do not forget to do a reset after that.
0226      * @param iListAttribute the list of attributes. If the list is empty then the default list is set.
0227      * The format of this string is the following one: attribute name[|visibility Y or N[|size]];attribute name[|visibility Y or N[|size]];…
0228      */
0229     virtual void setSupportedAttributes(const QStringList& iListAttribute);
0230 
0231     /**
0232      * Set the filter. Do not forget to do a reset after that.
0233      * @param iWhereClause the where clause
0234      * @return true is the filter is really changed
0235      */
0236     virtual bool setFilter(const QString& iWhereClause);
0237 
0238     /**
0239      * Returns the actions supported by the data in this model.
0240      * @return Qt::DropActions
0241      */
0242     Qt::DropActions supportedDragActions() const override;
0243 
0244     /**
0245      * Returns the actions supported by the data in this model.
0246      * @return Qt::DropActions
0247      */
0248     Qt::DropActions supportedDropActions() const override;
0249 
0250     /**
0251      * Get list of supported schemas
0252      * @return list of schemas @see SKGModelTemplate
0253      */
0254     virtual  SKGDocument::SKGModelTemplateList getSchemas() const;
0255 
0256     /**
0257      * Returns a list of MIME types that can be used to describe a list of model indexes.
0258      * @return list of mime types
0259      */
0260     QStringList mimeTypes() const override;
0261 
0262     /**
0263      * Returns an object that contains serialized items of data corresponding to the list of indexes specified.
0264      *The formats used to describe the encoded data is obtained from the mimeTypes() function.
0265      * If the list of indexes is empty, or there are no supported MIME types, 0 is returned rather than a serialized empty list.
0266      * @param iIndexes the index object
0267      * @return the mime data
0268      */
0269     QMimeData* mimeData(const QModelIndexList& iIndexes) const override;
0270 
0271     /**
0272      * Handles the data supplied by a drag and drop transaction that ended with the given action.
0273      * Returns true if the data and action can be handled by the model; otherwise returns false.
0274      * Although the specified row, column and parent indicate the location of an item in the model where the transaction ended,
0275      *it is the responsibility of the view to provide a suitable location for where the data should be inserted.
0276      * @param iData mime data
0277      * @param iAction action
0278      * @param iRow row
0279      * @param iColumn column
0280      * @param iParent parent
0281      * @return true if the dropping was successful otherwise false.
0282      */
0283     bool dropMimeData(const QMimeData* iData,
0284                       Qt::DropAction iAction,
0285                       int iRow, int iColumn,
0286                       const QModelIndex& iParent) override;
0287 Q_SIGNALS:
0288     /**
0289      * Emitted before reset
0290      */
0291     void beforeReset();
0292 
0293     /**
0294      * Emitted after reset
0295      */
0296     void afterReset();
0297 
0298 public Q_SLOTS:
0299     /**
0300      * Refresh the model.
0301      */
0302     virtual void refresh();
0303 
0304     /**
0305      * data are modified
0306      * @param iTableName table name
0307      * @param iIdTransaction the id of the transaction for direct modifications of the table (update of modify objects is enough)
0308      *or 0 in case of modifications by impact (full table must be refreshed)
0309      */
0310     virtual void dataModified(const QString& iTableName = QString(), int iIdTransaction = 0);
0311 
0312 protected Q_SLOTS:
0313     /**
0314      * This method is called by refresh to build the cache (to improve performance)
0315      */
0316     virtual void buidCache();
0317 
0318 protected:
0319     /**
0320      *list of attributes
0321      */
0322     QStringList m_listAttibutes;
0323 
0324     /**
0325      *list of attributes
0326      */
0327     QList<SKGServices::AttributeType> m_listAttributeTypes;
0328 
0329     /**
0330      *list of supported schemas
0331      */
0332     SKGDocument::SKGModelTemplateList m_listSchema;
0333 
0334     /**
0335      *To disable/enable reset
0336      */
0337     bool m_isResetRealyNeeded;
0338 
0339     /**
0340      *A cache for value computed in data
0341      */
0342     QMap<QString, QVariant>* m_cache;
0343 
0344     /**
0345      * Get the attribute value for grouping
0346      * @param iObject the object
0347      * @param iAttribute the attribute name
0348      * @return the value of the attribute
0349      */
0350     virtual QString getAttributeForGrouping(const SKGObjectBase& iObject, const QString& iAttribute) const;
0351 
0352     /**
0353      *The negative color
0354      */
0355     QVariant m_fontNegativeColor;
0356 
0357     /**
0358      * Get the string of an amount
0359      * @param iValue the value
0360      * @return the string
0361      */
0362     virtual QString formatMoney(double iValue) const;
0363 
0364 private Q_SLOTS:
0365     void pageChanged();
0366 
0367 private:
0368     Q_DISABLE_COPY(SKGObjectModelBase)
0369     void clear();
0370 
0371     SKGDocument* m_document;
0372     QString m_table;
0373     QString m_realTable;
0374     QString m_whereClause;
0375     QString m_parentAttribute;
0376     QString m_groupby;
0377 
0378     SKGObjectBase::SKGListSKGObjectBase m_listObjects;
0379     QHash<int, SKGIntList> m_parentChildRelations;
0380     QHash<int, int> m_childParentRelations;
0381     QHash<int, SKGObjectBase*> m_objectsHashTable;
0382     QHash<int, int> m_objectsHashTableRows;
0383 
0384     QStringList m_listSupported;
0385     QList<bool> m_listVisibility;
0386     QList<int> m_listSize;
0387     bool m_doctransactionTable;
0388     bool m_nodeTable;
0389     bool m_parametersTable;
0390     bool m_refreshBlocked;
0391 };
0392 
0393 #endif