File indexing completed on 2024-05-12 15:54:24

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