1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KGantt library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "kganttglobal.h"
21 
22 using namespace KGantt;
23 
24 
DateTimeSpan()25 DateTimeSpan::DateTimeSpan()
26 {
27 }
28 
DateTimeSpan(const QDateTime & start,const QDateTime & end)29 DateTimeSpan::DateTimeSpan( const QDateTime& start, const QDateTime& end )
30     : m_start( start ), m_end( end )
31 {
32 }
33 
DateTimeSpan(const DateTimeSpan & other)34 DateTimeSpan::DateTimeSpan( const DateTimeSpan& other )
35 {
36     *this = other;
37 }
38 
~DateTimeSpan()39 DateTimeSpan::~DateTimeSpan()
40 {
41 }
42 
operator =(const DateTimeSpan & other)43 DateTimeSpan& DateTimeSpan::operator=( const DateTimeSpan& other )
44 {
45     if ( this != &other ) {
46         m_start = other.m_start;
47         m_end = other.m_end;
48     }
49     return *this;
50 }
51 
isValid() const52 bool DateTimeSpan::isValid() const
53 {
54     return m_start.isValid() && m_end.isValid();
55 }
56 
equals(const DateTimeSpan & other) const57 bool DateTimeSpan::equals( const DateTimeSpan& other ) const
58 {
59     return m_start==other.m_start && m_end==other.m_end;
60 }
61 
62 #ifndef QT_NO_DEBUG_STREAM
63 
operator <<(QDebug dbg,KGantt::ItemDataRole r)64 QDebug operator<<( QDebug dbg, KGantt::ItemDataRole r)
65 {
66   switch (r) {
67   case KGantt::StartTimeRole:      dbg << "KGantt::StartTimeRole"; break;
68   case KGantt::EndTimeRole:        dbg << "KGantt::EndTimeRole"; break;
69   case KGantt::TaskCompletionRole: dbg << "KGantt::TaskCompletionRole"; break;
70   case KGantt::ItemTypeRole:       dbg << "KGantt::ItemTypeRole"; break;
71   case KGantt::LegendRole:         dbg << "KGantt::LegendRole"; break;
72   default: dbg << static_cast<Qt::ItemDataRole>(r);
73   }
74   return dbg;
75 }
76 
operator <<(QDebug dbg,KGantt::ItemType t)77 QDebug operator<<( QDebug dbg, KGantt::ItemType t)
78 {
79     switch ( t ) {
80     case KGantt::TypeNone:        dbg << "KGantt::TypeNone"; break;
81     case KGantt::TypeEvent:       dbg << "KGantt::TypeEvent"; break;
82     case KGantt::TypeTask:        dbg << "KGantt::TypeTask"; break;
83     case KGantt::TypeSummary:     dbg << "KGantt::TypeSummary"; break;
84     case KGantt::TypeMulti:       dbg << "KGantt::TypeMulti"; break;
85     case KGantt::TypeUser:        dbg << "KGantt::TypeUser"; break;
86     default: dbg << static_cast<int>(t);
87     }
88     return dbg;
89 }
90 
operator <<(QDebug dbg,const KGantt::Span & s)91 QDebug operator<<( QDebug dbg, const KGantt::Span& s )
92 {
93     dbg << "KGantt::Span[ start="<<s.start()<<" length="<<s.length()<<"]";
94     return dbg;
95 }
operator <<(QDebug dbg,const KGantt::DateTimeSpan & s)96 QDebug operator<<( QDebug dbg, const KGantt::DateTimeSpan& s )
97 {
98     dbg << "KGantt::DateTimeSpan[ start="<<s.start()<<" end="<<s.end()<<"]";
99     return dbg;
100 }
101 
102 #endif /* QT_NO_DEBUG_STREAM */
103 
104 #ifndef KDAB_NO_UNIT_TESTS
105 
106 #include <ostream>
107 
operator <<(std::ostream & os,const Span & span)108 static std::ostream& operator<<( std::ostream& os, const Span& span )
109 {
110     os << "Span[ start="<<span.start()<<", length="<<span.length()<<"]";
111     return os;
112 }
113 
operator <<(std::ostream & os,const DateTimeSpan & span)114 static std::ostream& operator<<( std::ostream& os, const DateTimeSpan& span )
115 {
116 #ifdef QT_NO_STL
117     os << "DateTimeSpan[ start="<<span.start().toString().toLatin1().constData()
118        << ", end="<<span.end().toString().toLatin1().constData() << "]";
119 #else
120     os << "DateTimeSpan[ start="<<span.start().toString().toStdString()
121        << ", end="<<span.end().toString().toStdString() << "]";
122 #endif
123     return os;
124 }
125 
126 #include "unittest/test.h"
127 
128 KDAB_SCOPED_UNITTEST_SIMPLE( KGantt, Span, "test" ) {
129     Span s1;
130     assertFalse( s1.isValid() );
131     s1.setStart( 10. );
132     s1.setLength( 2. );
133 
134     Span s2( s1.start(), s1.length() );
135     assertEqual( s1, s2 );
136 }
137 
138 KDAB_SCOPED_UNITTEST_SIMPLE( KGantt, DateTimeSpan, "test" ) {
139     DateTimeSpan s1;
140     assertFalse( s1.isValid() );
141     QDateTime dt = QDateTime::currentDateTime();
142     s1.setStart( dt );
143     assertTrue( dt.isValid() );
144     s1.setEnd( dt.addDays( 1 ) );
145 
146     DateTimeSpan s2( dt, dt.addDays( 1 ) );
147 
148     assertEqual( s1, s2 );
149 
150     DateTimeSpan s3;
151 
152     assertNotEqual( s1, s3 );
153 }
154 #endif /* KDAB_NO_UNIT_TESTS */
155