File indexing completed on 2024-05-19 15:27:31

0001 /*
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KGantt library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #ifndef KGANTTCONSTRAINT_H
0021 #define KGANTTCONSTRAINT_H
0022 
0023 #include <QMap>
0024 #include <QModelIndex>
0025 #include <QObject>
0026 #include <QSharedDataPointer>
0027 #include <QVariant>
0028 
0029 #include "kganttglobal.h"
0030 #ifndef QT_NO_DEBUG_STREAM
0031 #include <QDebug>
0032 #endif
0033 
0034 namespace KGantt {
0035 
0036 
0037     /*!\class KGantt::Constraint
0038      *\ingroup KGantt
0039      * \brief A class used to represent a dependency.
0040      *
0041      * Instances of this class represent a dependency between the
0042      * data items pointed to by a start-QModelIndex and an
0043      * end-QModelIndex.
0044      */
0045     class KGANTT_EXPORT Constraint {
0046         class Private;
0047     public:
0048         /*!\enum KGantt::Constraint::Type
0049          * This enum is unused for now.
0050          */
0051         enum Type
0052         {
0053             TypeSoft = 0,
0054             TypeHard = 1
0055         };
0056         enum RelationType
0057         {
0058             FinishStart = 0,
0059             FinishFinish = 1,
0060             StartStart = 2,
0061             StartFinish = 3
0062         };
0063 
0064         /*!\enum KGantt::Constraint::ConstraintDataRole
0065          * Data roles used when specifying the pen to draw constraints with.
0066          * \sa setData
0067          */
0068         enum ConstraintDataRole
0069         {
0070             ValidConstraintPen = Qt::UserRole,
0071             InvalidConstraintPen
0072         };
0073 
0074         typedef QMap<int, QVariant> DataMap;
0075 
0076         /*! Default constructor, created an invalid constraint. */
0077         Constraint();
0078 
0079         /*! Constructor. Creates a dependency for \a idx2 on \a idx1.
0080          * \param type controls if the constraint is a soft one that
0081          * is allowed to be broken (ie, go backwards in time) or a hard
0082          * constraint that will not allow the user to move an item so
0083          * that the constraint would have to go backwards. The default is
0084          * TypeSoft.
0085          *
0086          * Actually enforcing hard constraints is the responsibility of
0087          * the AbstractGrid subclass used in the view.
0088          *
0089          * \param relationType defines how the tasks depends on each other.
0090          * relationType can be FinishStart (default), FinishFinish, StartStart or StartFinish.
0091          */
0092         Constraint( const QModelIndex& idx1,
0093                     const QModelIndex& idx2,
0094                     Type type=TypeSoft,
0095                     RelationType relType=FinishStart,
0096                     const DataMap& datamap=DataMap() );
0097 
0098         /*! Copy-Constructor. */
0099         Constraint( const Constraint& other);
0100 
0101 
0102         /*! Destructor */
0103         ~Constraint();
0104 
0105         /*! This is unused for now. */
0106         Type type() const;
0107 
0108         /*! This is unused for now. */
0109         RelationType relationType() const;
0110 
0111         /*! \returns The dependency index */
0112         QModelIndex startIndex() const;
0113 
0114         /*! \returns The constrained index */
0115         QModelIndex endIndex() const;
0116 
0117         /*! Set data on this index for the specified role.
0118          * \param role The role to set the data for.
0119          * \param value The data to set on the index.
0120          * \sa ConstraintDataRole
0121          */
0122         void setData( int role, const QVariant& value );
0123 
0124         /*! \returns The data associated with this index for the specified role.
0125          * \param role The role to fetch the data for.
0126          * \sa ConstraintDataRole
0127          */
0128         QVariant data( int role ) const;
0129 
0130         /*! Set data on this constraint to the keys/values in \a datamap.
0131          * Clears any existing data from the constraint.
0132          */
0133         void setDataMap( const QMap< int, QVariant >& datamap );
0134 
0135         /*! \returns all the data set on this constraint. \see setDataMap
0136          */
0137         QMap< int, QVariant > dataMap() const;
0138 
0139         bool compareIndexes(const Constraint& other) const;
0140 
0141         /*! Assignment operator. */
0142         Constraint& operator=( const Constraint& other );
0143 
0144         /*! Compare two Constraint objects. Two Constraints are equal
0145          * if the have the same start and end indexes
0146          */
0147         bool operator==( const Constraint& other ) const;
0148 
0149         inline bool operator!=( const Constraint& other ) const {
0150             return !operator==( other );
0151         }
0152 
0153         /*!\internal*/
0154         uint hash() const;
0155 #ifndef QT_NO_DEBUG_STREAM
0156         QDebug debug( QDebug dbg) const;
0157 #endif
0158 
0159     private:
0160         QSharedDataPointer<Private> d;
0161     };
0162 
0163     inline uint qHash( const Constraint& c ) {return c.hash();}
0164 }
0165 
0166 #ifndef QT_NO_DEBUG_STREAM
0167 QDebug KGANTT_EXPORT operator<<( QDebug dbg, const KGantt::Constraint& c );
0168 #endif /* QT_NO_DEBUG_STREAM */
0169 
0170 #endif /* KGANTTCONSTRAINT_H */
0171