/* -*- Mode: C++ -*-
   $Id: KDGanttViewTaskLinkGroup.cpp 836291 2008-07-22 04:34:42Z pradeepto $
   KDGantt - a multi-platform charting engine
*/
/****************************************************************************
 ** Copyright (C)  2002-2004 Klarälvdalens Datakonsult AB.  All rights reserved.
 **
 ** This file is part of the KDGantt library.
 **
 ** This file may be used under the terms of the GNU General Public
 ** License versions 2.0 or 3.0 as published by the Free Software
 ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
 ** included in the packaging of this file.  Alternatively you may (at
 ** your option) use any later version of the GNU General Public
 ** License if such license has been publicly approved by
 ** Klarälvdalens Datakonsult AB (or its successors, if any).
 ** 
 ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
 ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
 ** A PARTICULAR PURPOSE. Klarälvdalens Datakonsult AB reserves all rights
 ** not expressly granted herein.
 ** 
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ** As a special exception, permission is given to link this program
 ** with any edition of Qt, and distribute the resulting executable,
 ** without including the source code for Qt in the source distribution.
 **
 **********************************************************************/


#include "KDGanttView.h"

#include "KDGanttViewTaskLinkGroup.h"
#include "KDGanttXMLTools.h"

QDict<KDGanttViewTaskLinkGroup> KDGanttViewTaskLinkGroup::sGroupDict;

/*! \class KDGanttViewTaskLinkGroup KDGanttViewTaskLinkGroup.h
  A group of task links.

  This class groups a number of task links together in order to
  manipulate them uniformly.
*/

/*!
  Constructs an empty task link group
*/
KDGanttViewTaskLinkGroup::KDGanttViewTaskLinkGroup()
{

}

/*!
  Destructor
  Removes this task link group from the list of task link groups in the
  KDGanttView class.
*/
KDGanttViewTaskLinkGroup::~KDGanttViewTaskLinkGroup()
{
    if (!myTaskLinkList.isEmpty()) {
        KDGanttViewItem* item = myTaskLinkList.first()->from().first();
        KDGanttView * gv = 0; 
        if ( item )
             gv = item->myGanttView;
        KDGanttViewTaskLink * tl = myTaskLinkList.first();
        while ( tl ) {
            tl->resetGroup();
            tl = myTaskLinkList.next();
        }
        if ( gv )
            gv->removeTaskLinkGroup(this);
    }
}



/*!
  Constructs an empty task link group and records it under the name \a
  name so that it can later be found again with
  KDGanttViewTaskLinkGroup::find().

  \param name the search name of this task link group
*/
KDGanttViewTaskLinkGroup::KDGanttViewTaskLinkGroup( const QString& name )
{
    sGroupDict.insert( name, this );
    _name = name;
}


/*!
  Adds a task link LINK to this group. If the task link is already a member of
  another group, it will be removed from it.
  This function is equivalent to  LINK->setGroup(this), where this is
  a pointer to this TaskLinkGroup.
  \param link a pointer to the task link to add to this task link group
  visible, and false to hide them
  \sa remove()
*/
void KDGanttViewTaskLinkGroup::insert (KDGanttViewTaskLink* link)
{
    link->setGroup(this);
}


/*!
  Removes a task link LINK from this group.
  You may remove a tasklink  LINK from its group with  LINK->setGroup(0).

  \param link a pointer to the task link to remove from this task link group
  \return true if the task link was a member of this group
  \sa insert()
*/
bool KDGanttViewTaskLinkGroup::remove (KDGanttViewTaskLink* link)
{
    KDGanttViewTaskLinkGroup* g = link->group();
    if ((g == this))
        link->setGroup(0);
    return (g == this);
}


/*!
  Specifies whether the task links of this group should be visible or not.

  \param show visible pass true to make the task links of this group
  visible, and false to hide them
  \sa isVisible()
*/
void KDGanttViewTaskLinkGroup::setVisible( bool show )
{
    isvisible = show;
    QPtrListIterator<KDGanttViewTaskLink> it(myTaskLinkList);
    for ( ; it.current(); ++it ) {
        it.current()->setVisible(show);
    }
}


/*!
  Returns whether the task links of this group should be visible or not.

  \return true if the task links of this group are visible
  \sa setVisible()
*/
bool KDGanttViewTaskLinkGroup::visible() const
{
    return isvisible;
}


/*!
  Specifies whether the task links of this group should be shown
  highlighted. The user can also highlight a task link with the mouse.

  \param highlight pass true in order to highlight the task links in
  this group
  \sa highlight()
*/
void KDGanttViewTaskLinkGroup::setHighlight( bool highlight )
{
    ishighlighted=  highlight;
    QPtrListIterator<KDGanttViewTaskLink> it(myTaskLinkList);
    for ( ; it.current(); ++it )
        it.current()->setHighlight(highlight );

}


/*!
  Returns whether all task links in this group are highlighted, either
  programmatically by setHighlight() or by the user with the
  mouse. This method is not particularly useful and is mainly provided
  for API uniformity reasons.

  \return true if all the task links in this group are highlighted
  \sa setHighlight()
*/
bool KDGanttViewTaskLinkGroup::highlight() const
{
    return ishighlighted;
}


/*!
  Specifies the color to draw the task links in this group in.

  \param color the color to draw the task links in this group in
  \sa color()
*/
void KDGanttViewTaskLinkGroup::setColor( const QColor& color )
{
    myColor = color;
    QPtrListIterator<KDGanttViewTaskLink> it(myTaskLinkList);
    for ( ; it.current(); ++it )
        it.current()->setColor(color);
}


/*!
  Returns the color in which the task links in this group are
  drawn. If task links have been assigned individual colors, the
  return value of this method is undefined. This method is not
  particularly useful and is mainly provided for API uniformity
  reasons.

  \return the color in which the task links in this group are drawn
  \sa setColor()
*/
QColor KDGanttViewTaskLinkGroup::color() const
{
    return myColor;
}


/*!
  Specifies the highlight color to draw the task links in this group in.

  \param color the highlight color to draw the task links in this group in
  \sa color()
*/
void KDGanttViewTaskLinkGroup::setHighlightColor( const QColor& color )
{

    myColorHL = color;
    QPtrListIterator<KDGanttViewTaskLink> it(myTaskLinkList);
    for ( ; it.current(); ++it )
        it.current()->setHighlightColor(color);
}


/*!
  Returns the highlight color in which the task links in this group are
  drawn. If task links have been assigned individual highlight colors,
  the return value of this method is undefined. This method is not
  particularly useful and is mainly provided for API uniformity reasons.

  \return the highlight color in which the task links in this group
  are drawn
  \sa setColor()
*/
QColor KDGanttViewTaskLinkGroup::highlightColor() const
{
    return myColorHL;
}


/*!
  Adds a task link LINK to this group. If the task link is already a member of
  another group, it will  not be removed from it.
  \param a pointer to the task link to add to this task link group
  visible, and false to hide them
  \sa removeItem()
*/
void KDGanttViewTaskLinkGroup::insertItem (KDGanttViewTaskLink* link)
{
    myTaskLinkList.append (link);
}


/*!
  Removes a task link LINK from this group.

  \param a pointer to the task link to remove from this task link group
  \sa insertItem()
*/
void KDGanttViewTaskLinkGroup::removeItem (KDGanttViewTaskLink* link)
{
    myTaskLinkList.remove(link);
}


/*!
  Returns the task link group with the specified name.

  \param name the name to search for
  \return the task link group with the specified name; 0 if no group
  with that name exists
*/
KDGanttViewTaskLinkGroup* KDGanttViewTaskLinkGroup::find( const QString& name )
{
    return sGroupDict.find( name );
}


/*!
  Creates a DOM node that describes this task link group.

  \param doc the DOM document to which the node belongs
  \param parentElement the element into which to insert this node
*/
void KDGanttViewTaskLinkGroup::createNode( QDomDocument& doc,
                                           QDomElement& parentElement )
{
    QDomElement taskLinkGroupElement = doc.createElement( "TaskLink" );
    parentElement.appendChild( taskLinkGroupElement );

    KDGanttXML::createBoolNode( doc, taskLinkGroupElement, "Highlight",
                           highlight() );
    KDGanttXML::createColorNode( doc, taskLinkGroupElement, "Color", color() );
    KDGanttXML::createColorNode( doc, taskLinkGroupElement, "HighlightColor",
                            highlightColor() );
    KDGanttXML::createBoolNode( doc, taskLinkGroupElement, "Visible",
                           visible() );
    KDGanttXML::createStringNode( doc, taskLinkGroupElement, "Name", _name );
}


/*!
  Creates a KDGanttViewTaskLinkGroup according to the specification in a DOM
  element.

  \param element the DOM element from which to read the specification
  \return the newly created task link group
*/
KDGanttViewTaskLinkGroup* KDGanttViewTaskLinkGroup::createFromDomElement( QDomElement& element )
{
    QDomNode node = element.firstChild();
    bool highlight = false, visible = false;
    QColor color, highlightColor;
    QString name;
    while( !node.isNull() ) {
        QDomElement element = node.toElement();
        if( !element.isNull() ) { // was really an element
            QString tagName = element.tagName();
            if( tagName == "Highlight" ) {
                bool value;
                if( KDGanttXML::readBoolNode( element, value ) )
                    highlight = value;
            } else if( tagName == "Visible" ) {
                bool value;
                if( KDGanttXML::readBoolNode( element, value ) )
                    visible = value;
            } else if( tagName == "Color" ) {
                QColor value;
                if( KDGanttXML::readColorNode( element, value ) )
                    color = value;
            } else if( tagName == "HighlightColor" ) {
                QColor value;
                if( KDGanttXML::readColorNode( element, value ) )
                    highlightColor = value;
            } else if( tagName == "Name" ) {
                QString value;
                if( KDGanttXML::readStringNode( element, value ) )
                    name = value;
            } else {
                qDebug( "Unrecognized tag name: %s", qPrintable(tagName) );
                Q_ASSERT( false );
            }
        }
        node = node.nextSibling();
    }

    KDGanttViewTaskLinkGroup* tlg;
    if( !name.isEmpty() )
        tlg = new KDGanttViewTaskLinkGroup( name );
    else
        tlg = new KDGanttViewTaskLinkGroup();

    tlg->setHighlight( highlight );
    tlg->setVisible( visible );
    tlg->setHighlightColor( highlightColor );
    tlg->setColor( color );

    return tlg;
}
