File indexing completed on 2024-05-12 04:20:41

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KGantt library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 /** \file kganttglobal.h
0009 \brief Contains KGantt macros.
0010 
0011 Contains KGantt macros.  */
0012 
0013 #ifndef KGANTTGLOBAL_H
0014 #define KGANTTGLOBAL_H
0015 
0016 #include <Qt>
0017 #include <QDateTime>
0018 #include <QDebug>
0019 #include <QMetaType>
0020 
0021 #include "kgantt_export.h"
0022 
0023 #if (defined(__MINGW32__) || defined(__MINGW64__)) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 4) && (__GNUC_PATCHLEVEL__ == 0)
0024 // workaround a mingw bug, http://sourceforge.net/tracker/index.php?func=detail&aid=2373234&group_id=2435&atid=102435
0025 int swprintf (wchar_t *, size_t, const wchar_t *, ...);
0026 int vswprintf(wchar_t *, const wchar_t *, va_list);
0027 #endif
0028 
0029 #ifndef KDAB_SET_OBJECT_NAME
0030 template <typename T>
0031 inline T & __kdab__dereference_for_methodcall( T & o ) {
0032     return o;
0033 }
0034 
0035 template <typename T>
0036 inline T & __kdab__dereference_for_methodcall( T * o ) {
0037     return *o;
0038 }
0039 
0040 #define KDAB_SET_OBJECT_NAME( x ) __kdab__dereference_for_methodcall( x ).setObjectName( QLatin1String( #x ) )
0041 #endif
0042 
0043 #if defined(_MSC_VER) && _MSC_VER <= 1300
0044 #define KGANTT_DECLARE_PRIVATE_DERIVED( X )      \
0045 public:                                           \
0046     class Private;                                \
0047     friend class Private;                     \
0048 protected:                                        \
0049     inline Private * d_func();                    \
0050     inline const Private * d_func() const;        \
0051     explicit inline X( Private * );               \
0052 private:                                          \
0053     void init();
0054 #else
0055 #define KGANTT_DECLARE_PRIVATE_DERIVED( X )      \
0056 protected:                                        \
0057     class Private;                                \
0058     friend class Private;                     \
0059     inline Private * d_func();                    \
0060     inline const Private * d_func() const;        \
0061     explicit inline X( Private * );               \
0062 private:                                          \
0063     void init();
0064 #endif
0065 
0066 #if defined(_MSC_VER) && _MSC_VER <= 1300
0067 #define KGANTT_DECLARE_PRIVATE_DERIVED_PARENT( X, ParentType )      \
0068 public:                                           \
0069     class Private;                                \
0070     friend class Private;                     \
0071 protected:                                        \
0072     inline Private * d_func();                    \
0073     inline const Private * d_func() const;        \
0074     explicit inline X( Private *, ParentType );   \
0075 private:                                          \
0076     void init();
0077 #else
0078 #define KGANTT_DECLARE_PRIVATE_DERIVED_PARENT( X, ParentType )      \
0079 protected:                                        \
0080     class Private;                                \
0081     friend class Private;                     \
0082     inline Private * d_func();                    \
0083     inline const Private * d_func() const;        \
0084     explicit inline X( Private *, ParentType );   \
0085 private:                                          \
0086     void init();
0087 #endif
0088 
0089 #define KGANTT_DECLARE_PRIVATE_DERIVED_QWIDGET( X )         \
0090     KGANTT_DECLARE_PRIVATE_DERIVED_PARENT( X, QWidget* )
0091 
0092 #define KGANTT_DECLARE_PRIVATE_BASE_VALUE( X )              \
0093 public:                                                      \
0094     inline void swap( X & other ) { qSwap( _d, other._d ); } \
0095 protected:                                                   \
0096     class Private;                                           \
0097     friend class Private;                                \
0098     Private * d_func() { return _d; }                        \
0099     const Private * d_func() const { return _d; }            \
0100 private:                                                     \
0101     void init();                                             \
0102     Private * _d;
0103 
0104 #if defined(_MSC_VER) && _MSC_VER <= 1300
0105 #define KGANTT_DECLARE_PRIVATE_BASE_POLYMORPHIC( X ) \
0106 public:                                               \
0107     class Private;                                    \
0108     friend class Private;                         \
0109 protected:                                            \
0110     Private * d_func() { return _d; }                 \
0111     const Private * d_func() const { return _d; }     \
0112     explicit inline X( Private * );                   \
0113 private:                                              \
0114     void init();                                      \
0115     Private * _d;
0116 #else
0117 #define KGANTT_DECLARE_PRIVATE_BASE_POLYMORPHIC( X ) \
0118 protected:                                            \
0119     class Private;                                    \
0120     friend class Private;                         \
0121     Private * d_func() { return _d; }                 \
0122     const Private * d_func() const { return _d; }     \
0123     explicit inline X( Private * );                   \
0124 private:                                              \
0125     void init();                                      \
0126     Private * _d;
0127 #endif
0128 
0129 #if defined(_MSC_VER) && _MSC_VER <= 1300
0130 #define KGANTT_DECLARE_PRIVATE_BASE_POLYMORPHIC_QWIDGET( X ) \
0131 public:                                               \
0132     class Private;                                    \
0133     friend class Private;                         \
0134 protected:                                            \
0135     Private * d_func() { return _d; }                 \
0136     const Private * d_func() const { return _d; }     \
0137     explicit inline X( Private *, QWidget* );         \
0138 private:                                              \
0139     void init();                                      \
0140     Private * _d;
0141 #else
0142 #define KGANTT_DECLARE_PRIVATE_BASE_POLYMORPHIC_QWIDGET( X ) \
0143 protected:                                            \
0144     class Private;                                    \
0145     friend class Private;                         \
0146     Private * d_func() { return _d; }                 \
0147     const Private * d_func() const { return _d; }     \
0148     explicit inline X( Private *, QWidget* );         \
0149 private:                                              \
0150     void init();                                      \
0151     Private * _d;
0152 #endif
0153 
0154 
0155 
0156 // enable this for testing if KD Gantt works without STL support in Qt:
0157 //
0158 // #define QT_NO_STL
0159 
0160 
0161 #include <QtAlgorithms> // qSwap
0162 #ifndef QT_NO_STL
0163 #include <algorithm>
0164 #define KGANTT_DECLARE_SWAP_SPECIALISATION( X )            \
0165 QT_BEGIN_NAMESPACE                                          \
0166     template <> inline void qSwap<X>( X & lhs, X & rhs )    \
0167     { lhs.swap( rhs ); }                                    \
0168 QT_END_NAMESPACE                                            \
0169     namespace std {                                         \
0170         template <> inline void swap<X>( X & lhs, X & rhs ) \
0171         { lhs.swap( rhs ); }                                \
0172     }
0173 #else
0174 #define KGANTT_DECLARE_SWAP_SPECIALISATION( X )            \
0175 QT_BEGIN_NAMESPACE                                          \
0176     template <> inline void qSwap<X>( X & lhs, X & rhs )    \
0177     { lhs.swap( rhs ); }                                    \
0178 QT_END_NAMESPACE
0179 #endif
0180 
0181 #define KGANTT_DECLARE_SWAP_SPECIALISATION_DERIVED( X )    \
0182     KGANTT_DECLARE_SWAP_SPECIALISATION( X )
0183 
0184 #define KGANTT_DECLARE_SWAP_BASE( X ) \
0185 protected: \
0186     void doSwap( X& other ) \
0187     { qSwap( _d, other._d); }
0188 
0189 #define KGANTT_DECLARE_SWAP_DERIVED( X ) \
0190     void swap( X& other ) { doSwap( other ); }
0191 
0192 #if defined(Q_OS_WIN) && defined(QT_DLL)
0193 #if defined(_MSC_VER) && _MSC_VER >= 1300
0194 // workaround http://support.microsoft.com/default.aspx?scid=kb;en-us;309801
0195 #include <QPointF>
0196 #include <QVector>
0197 template class Q_DECL_IMPORT QVector<QPointF>;
0198 #endif
0199 #endif
0200 
0201 /*!\namespace KGantt
0202  *\ingroup KGantt
0203  *\brief Global namespace
0204  */
0205 namespace KGantt {
0206 
0207     /*!\enum KGantt::ItemDataRole
0208      *\ingroup KGantt
0209      * The values of this enum are used for the special data roles
0210      * for gantt items
0211      */
0212     enum ItemDataRole {
0213         KGanttRoleBase     = Qt::UserRole + 1174, ///< The base value used for the KGantt role enum values. The actual roles have values base+1, base+2, ...
0214         StartTimeRole       = KGanttRoleBase + 1, ///< Start time (or other start value) for a gantt item.
0215         EndTimeRole         = KGanttRoleBase + 2, ///< End time (or other end value) for a gantt item.
0216         TaskCompletionRole  = KGanttRoleBase + 3, ///< Task completion percentage used by Task items. Should be an integer og a qreal between 0 and 100.
0217         ItemTypeRole        = KGanttRoleBase + 4, ///< The item type. \see KGantt::ItemType.
0218         LegendRole          = KGanttRoleBase + 5, ///< The Legend text
0219         TextPositionRole    = KGanttRoleBase + 6 ///< The position of the text label on the item. The type of this value is KGantt::StyleOptionGanttItem::Position and the default values is Right.
0220     };
0221 
0222     /*!\enum KGantt::ItemType
0223      *\ingroup KGantt
0224      * The values of this enum are used to represent the different
0225      * types of gantt items that KGantt understands. The itemtype
0226      * is served through the KGantt::ItemTypeRole role
0227      */
0228     enum ItemType {
0229         TypeNone    = 0,
0230         TypeEvent   = 1,
0231         TypeTask    = 2,
0232         TypeSummary = 3,
0233         TypeMulti   = 4,
0234         TypeUser    = 1000
0235     };
0236 
0237     /*!\class KGantt::Span kganttglobal.h KGanttGlobal
0238      *\ingroup KGantt
0239      * \brief A class representing a start point and a length */
0240     class Span {
0241         qreal m_start;
0242         qreal m_length;
0243     public:
0244         inline Span() : m_start( -1 ), m_length( 0 ) {}
0245         inline Span( qreal start, qreal length ) : m_start( start ), m_length( length ) {}
0246         inline Span( const Span& other ) : m_start(other.m_start), m_length(other.m_length) {}
0247 
0248         inline Span& operator=( const Span& other ) { m_start=other.m_start; m_length=other.m_length; return *this; }
0249 
0250         inline void setStart( qreal start ) { m_start=start; }
0251         inline qreal start() const { return m_start; }
0252         inline void setEnd( qreal end ) { m_length = m_start-end; }
0253         inline qreal end() const { return m_start+m_length; }
0254 
0255         inline void setLength( qreal length ) { m_length=length; }
0256         inline qreal length() const { return m_length; }
0257 
0258         inline bool isValid() const { return m_start >= 0.;}
0259 
0260         inline bool equals( const Span& other ) const {
0261             return m_start == other.m_start && m_length == other.m_length;
0262         }
0263         inline Span expandedTo( const Span& other) const {
0264             const qreal new_start = qMin(start(),other.start());
0265             return Span( new_start, (end()<other.end())?other.end()-new_start:end()-new_start);
0266         }
0267     };
0268 
0269     inline bool operator==( const Span& s1, const Span& s2) { return s1.equals( s2 ); }
0270     inline bool operator!=( const Span& s1, const Span& s2) { return !s1.equals( s2 ); }
0271 
0272 
0273     class DateTimeSpan {
0274         QDateTime m_start;
0275         QDateTime m_end;
0276     public:
0277         DateTimeSpan();
0278         DateTimeSpan( const QDateTime& start, const QDateTime& end );
0279         DateTimeSpan( const DateTimeSpan& other );
0280         ~DateTimeSpan();
0281 
0282         DateTimeSpan& operator=( const DateTimeSpan& other );
0283 
0284         inline void setStart( const QDateTime& start ) { m_start=start; }
0285         inline QDateTime start() const { return m_start; }
0286 
0287         inline void setEnd( const QDateTime& end ) { m_end=end; }
0288         inline QDateTime end() const { return m_end; }
0289 
0290         bool isValid() const;
0291 
0292         bool equals( const DateTimeSpan& other ) const;
0293     };
0294 
0295     inline bool operator==( const DateTimeSpan& s1, const DateTimeSpan& s2) { return s1.equals( s2 ); }
0296     inline bool operator!=( const DateTimeSpan& s1, const DateTimeSpan& s2) { return !s1.equals( s2 ); }
0297 }
0298 
0299 Q_DECLARE_METATYPE(KGantt::ItemType)
0300 
0301 #ifndef QT_NO_DEBUG_STREAM
0302 QDebug KGANTT_EXPORT operator<<( QDebug dbg, KGantt::ItemDataRole r);
0303 QDebug KGANTT_EXPORT operator<<( QDebug dbg, KGantt::ItemType t);
0304 QDebug KGANTT_EXPORT operator<<( QDebug dbg, const KGantt::Span& s );
0305 QDebug KGANTT_EXPORT operator<<( QDebug dbg, const KGantt::DateTimeSpan& s );
0306 #endif /* QT_NO_DEBUG_STREAM */
0307 
0308 QT_BEGIN_NAMESPACE
0309 Q_DECLARE_TYPEINFO(KGantt::Span, Q_MOVABLE_TYPE);
0310 Q_DECLARE_TYPEINFO(KGantt::DateTimeSpan, Q_MOVABLE_TYPE);
0311 QT_END_NAMESPACE
0312 
0313 #endif /* KGANTTGLOBAL_H */