/*****************************************************************************
*
* Copyright (c) 2000 - 2017, Lawrence Livermore National Security, LLC
* Produced at the Lawrence Livermore National Laboratory
* LLNL-CODE-442911
* All rights reserved.
*
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

#include <ColorAttributeList.h>
#include <DataNode.h>
#include <ColorAttribute.h>

// ****************************************************************************
// Method: ColorAttributeList::ColorAttributeList
//
// Purpose: 
//   Init utility for the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

void ColorAttributeList::Init()
{

    ColorAttributeList::SelectAll();
}

// ****************************************************************************
// Method: ColorAttributeList::ColorAttributeList
//
// Purpose: 
//   Copy utility for the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

void ColorAttributeList::Copy(const ColorAttributeList &obj)
{
    AttributeGroupVector::const_iterator pos;

    // *** Copy the colors field ***
    // Delete the AttributeGroup objects and clear the vector.
    for(pos = colors.begin(); pos != colors.end(); ++pos)
        delete *pos;
    colors.clear();
    if(obj.colors.size() > 0)
        colors.reserve(obj.colors.size());
    // Duplicate the colors from obj.
    for(pos = obj.colors.begin(); pos != obj.colors.end(); ++pos)
    {
        ColorAttribute *oldColorAttribute = (ColorAttribute *)(*pos);
        ColorAttribute *newColorAttribute = new ColorAttribute(*oldColorAttribute);
        colors.push_back(newColorAttribute);
    }


    ColorAttributeList::SelectAll();
}

// Type map format string
const char *ColorAttributeList::TypeMapFormatString = COLORATTRIBUTELIST_TMFS;
const AttributeGroup::private_tmfs_t ColorAttributeList::TmfsStruct = {COLORATTRIBUTELIST_TMFS};


// ****************************************************************************
// Method: ColorAttributeList::ColorAttributeList
//
// Purpose: 
//   Default constructor for the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

ColorAttributeList::ColorAttributeList() : 
    AttributeSubject(ColorAttributeList::TypeMapFormatString)
{
    ColorAttributeList::Init();
}

// ****************************************************************************
// Method: ColorAttributeList::ColorAttributeList
//
// Purpose: 
//   Constructor for the derived classes of ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

ColorAttributeList::ColorAttributeList(private_tmfs_t tmfs) : 
    AttributeSubject(tmfs.tmfs)
{
    ColorAttributeList::Init();
}

// ****************************************************************************
// Method: ColorAttributeList::ColorAttributeList
//
// Purpose: 
//   Copy constructor for the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

ColorAttributeList::ColorAttributeList(const ColorAttributeList &obj) : 
    AttributeSubject(ColorAttributeList::TypeMapFormatString)
{
    ColorAttributeList::Copy(obj);
}

// ****************************************************************************
// Method: ColorAttributeList::ColorAttributeList
//
// Purpose: 
//   Copy constructor for derived classes of the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

ColorAttributeList::ColorAttributeList(const ColorAttributeList &obj, private_tmfs_t tmfs) : 
    AttributeSubject(tmfs.tmfs)
{
    ColorAttributeList::Copy(obj);
}

// ****************************************************************************
// Method: ColorAttributeList::~ColorAttributeList
//
// Purpose: 
//   Destructor for the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

ColorAttributeList::~ColorAttributeList()
{
    AttributeGroupVector::iterator pos;

    // Destroy the colors field.
    for(pos = colors.begin(); pos != colors.end(); ++pos)
        delete *pos;
}

// ****************************************************************************
// Method: ColorAttributeList::operator = 
//
// Purpose: 
//   Assignment operator for the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

ColorAttributeList& 
ColorAttributeList::operator = (const ColorAttributeList &obj)
{
    if (this == &obj) return *this;

    ColorAttributeList::Copy(obj);

    return *this;
}

// ****************************************************************************
// Method: ColorAttributeList::operator == 
//
// Purpose: 
//   Comparison operator == for the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

bool
ColorAttributeList::operator == (const ColorAttributeList &obj) const
{
    bool colors_equal = (obj.colors.size() == colors.size());
    for(size_t i = 0; (i < colors.size()) && colors_equal; ++i)
    {
        // Make references to ColorAttribute from AttributeGroup *.
        const ColorAttribute &colors1 = *((const ColorAttribute *)(colors[i]));
        const ColorAttribute &colors2 = *((const ColorAttribute *)(obj.colors[i]));
        colors_equal = (colors1 == colors2);
    }

    // Create the return value
    return (colors_equal);
}

// ****************************************************************************
// Method: ColorAttributeList::operator != 
//
// Purpose: 
//   Comparison operator != for the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

bool
ColorAttributeList::operator != (const ColorAttributeList &obj) const
{
    return !(this->operator == (obj));
}

// ****************************************************************************
// Method: ColorAttributeList::TypeName
//
// Purpose: 
//   Type name method for the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

const std::string
ColorAttributeList::TypeName() const
{
    return "ColorAttributeList";
}

// ****************************************************************************
// Method: ColorAttributeList::CopyAttributes
//
// Purpose: 
//   CopyAttributes method for the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

bool
ColorAttributeList::CopyAttributes(const AttributeGroup *atts)
{
    if(TypeName() != atts->TypeName())
        return false;

    // Call assignment operator.
    const ColorAttributeList *tmp = (const ColorAttributeList *)atts;
    *this = *tmp;

    return true;
}

// ****************************************************************************
// Method: ColorAttributeList::CreateCompatible
//
// Purpose: 
//   CreateCompatible method for the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

AttributeSubject *
ColorAttributeList::CreateCompatible(const std::string &tname) const
{
    AttributeSubject *retval = 0;
    if(TypeName() == tname)
        retval = new ColorAttributeList(*this);
    // Other cases could go here too. 

    return retval;
}

// ****************************************************************************
// Method: ColorAttributeList::NewInstance
//
// Purpose: 
//   NewInstance method for the ColorAttributeList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

AttributeSubject *
ColorAttributeList::NewInstance(bool copy) const
{
    AttributeSubject *retval = 0;
    if(copy)
        retval = new ColorAttributeList(*this);
    else
        retval = new ColorAttributeList;

    return retval;
}

// ****************************************************************************
// Method: ColorAttributeList::SelectAll
//
// Purpose: 
//   Selects all attributes.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

void
ColorAttributeList::SelectAll()
{
    Select(ID_colors, (void *)&colors);
}

// ****************************************************************************
// Method: ColorAttributeList::CreateSubAttributeGroup
//
// Purpose: 
//   This class contains a list of ColorAttributes.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

AttributeGroup *
ColorAttributeList::CreateSubAttributeGroup(int)
{
    return new ColorAttribute;
}

///////////////////////////////////////////////////////////////////////////////
// Persistence methods
///////////////////////////////////////////////////////////////////////////////

// ****************************************************************************
// Method: ColorAttributeList::CreateNode
//
// Purpose: 
//   This method creates a DataNode representation of the object so it can be saved to a config file.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

bool
ColorAttributeList::CreateNode(DataNode *parentNode, bool completeSave, bool forceAdd)
{
    if(parentNode == 0)
        return false;

    ColorAttributeList defaultObject;
    bool addToParent = false;
    // Create a node for ColorAttributeList.
    DataNode *node = new DataNode("ColorAttributeList");

    if(completeSave || !FieldsEqual(ID_colors, &defaultObject))
    {
        addToParent = true;
        for(size_t i = 0; i < colors.size(); ++i)
            colors[i]->CreateNode(node, completeSave, true);
    }


    // Add the node to the parent node.
    if(addToParent || forceAdd)
        parentNode->AddNode(node);
    else
        delete node;

    return (addToParent || forceAdd);
}

// ****************************************************************************
// Method: ColorAttributeList::SetFromNode
//
// Purpose: 
//   This method sets attributes in this object from values in a DataNode representation of the object.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

void
ColorAttributeList::SetFromNode(DataNode *parentNode)
{
    if(parentNode == 0)
        return;

    DataNode *searchNode = parentNode->GetNode("ColorAttributeList");
    if(searchNode == 0)
        return;

    DataNode **children;

    // Clear all the ColorAttributes if we got any.
    bool clearedColors = false;
    // Go through all of the children and construct a new
    // ColorAttribute for each one of them.
    children = searchNode->GetChildren();
    if(children != 0)
    {
        for(int i = 0; i < searchNode->GetNumChildren(); ++i)
        {
            if(children[i]->GetKey() == std::string("ColorAttribute"))
            {
                if (!clearedColors)
                {
                    ClearColors();
                    clearedColors = true;
                }
                ColorAttribute temp;
                temp.SetFromNode(children[i]);
                AddColors(temp);
            }
        }
    }

}

///////////////////////////////////////////////////////////////////////////////
// Set property methods
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// Get property methods
///////////////////////////////////////////////////////////////////////////////

const AttributeGroupVector &
ColorAttributeList::GetColors() const
{
    return colors;
}

AttributeGroupVector &
ColorAttributeList::GetColors()
{
    return colors;
}

///////////////////////////////////////////////////////////////////////////////
// Select property methods
///////////////////////////////////////////////////////////////////////////////

void
ColorAttributeList::SelectColors()
{
    Select(ID_colors, (void *)&colors);
}

///////////////////////////////////////////////////////////////////////////////
// AttributeGroupVector convenience methods.
///////////////////////////////////////////////////////////////////////////////

// ****************************************************************************
// Method: ColorAttributeList::AddColors
//
// Purpose: 
//   This class contains a list of ColorAttributes.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

void
ColorAttributeList::AddColors(const ColorAttribute &obj)
{
    ColorAttribute *newColorAttribute = new ColorAttribute(obj);
    colors.push_back(newColorAttribute);

    // Indicate that things have changed by selecting it.
    Select(ID_colors, (void *)&colors);
}

// ****************************************************************************
// Method: ColorAttributeList::ClearColors
//
// Purpose: 
//   This class contains a list of ColorAttributes.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

void
ColorAttributeList::ClearColors()
{
    AttributeGroupVector::iterator pos;

    for(pos = colors.begin(); pos != colors.end(); ++pos)
        delete *pos;
    colors.clear();

    // Indicate that things have changed by selecting the list.
    Select(ID_colors, (void *)&colors);
}

// ****************************************************************************
// Method: ColorAttributeList::RemoveColors
//
// Purpose: 
//   This class contains a list of ColorAttributes.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

void
ColorAttributeList::RemoveColors(int index)
{
    AttributeGroupVector::iterator pos = colors.begin();

    // Iterate through the vector "index" times. 
    for(int i = 0; i < index; ++i)
        if(pos != colors.end()) ++pos;

    // If pos is still a valid iterator, remove that element.
    if(pos != colors.end())
    {
        delete *pos;
        colors.erase(pos);
    }

    // Indicate that things have changed by selecting the list.
    Select(ID_colors, (void *)&colors);
}

// ****************************************************************************
// Method: ColorAttributeList::GetNumColors
//
// Purpose: 
//   This class contains a list of ColorAttributes.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

int
ColorAttributeList::GetNumColors() const
{
    return (int)colors.size();
}

// ****************************************************************************
// Method: ColorAttributeList::GetColors
//
// Purpose: 
//   This class contains a list of ColorAttributes.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

ColorAttribute &
ColorAttributeList::GetColors(int i)
{
    return *((ColorAttribute *)colors[i]);
}

// ****************************************************************************
// Method: ColorAttributeList::GetColors
//
// Purpose: 
//   This class contains a list of ColorAttributes.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

const ColorAttribute &
ColorAttributeList::GetColors(int i) const
{
    return *((ColorAttribute *)colors[i]);
}

// ****************************************************************************
// Method: ColorAttributeList::operator []
//
// Purpose: 
//   This class contains a list of ColorAttributes.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

ColorAttribute &
ColorAttributeList::operator [] (int i)
{
    return *((ColorAttribute *)colors[i]);
}

// ****************************************************************************
// Method: ColorAttributeList::operator []
//
// Purpose: 
//   This class contains a list of ColorAttributes.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

const ColorAttribute &
ColorAttributeList::operator [] (int i) const
{
    return *((ColorAttribute *)colors[i]);
}

///////////////////////////////////////////////////////////////////////////////
// Keyframing methods
///////////////////////////////////////////////////////////////////////////////

// ****************************************************************************
// Method: ColorAttributeList::GetFieldName
//
// Purpose: 
//   This method returns the name of a field given its index.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

std::string
ColorAttributeList::GetFieldName(int index) const
{
    switch (index)
    {
    case ID_colors: return "colors";
    default:  return "invalid index";
    }
}

// ****************************************************************************
// Method: ColorAttributeList::GetFieldType
//
// Purpose: 
//   This method returns the type of a field given its index.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

AttributeGroup::FieldType
ColorAttributeList::GetFieldType(int index) const
{
    switch (index)
    {
    case ID_colors: return FieldType_attVector;
    default:  return FieldType_unknown;
    }
}

// ****************************************************************************
// Method: ColorAttributeList::GetFieldTypeName
//
// Purpose: 
//   This method returns the name of a field type given its index.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

std::string
ColorAttributeList::GetFieldTypeName(int index) const
{
    switch (index)
    {
    case ID_colors: return "attVector";
    default:  return "invalid index";
    }
}

// ****************************************************************************
// Method: ColorAttributeList::FieldsEqual
//
// Purpose: 
//   This method compares two fields and return true if they are equal.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//   
// ****************************************************************************

bool
ColorAttributeList::FieldsEqual(int index_, const AttributeGroup *rhs) const
{
    const ColorAttributeList &obj = *((const ColorAttributeList*)rhs);
    bool retval = false;
    switch (index_)
    {
    case ID_colors:
        {  // new scope
        bool colors_equal = (obj.colors.size() == colors.size());
        for(size_t i = 0; (i < colors.size()) && colors_equal; ++i)
        {
            // Make references to ColorAttribute from AttributeGroup *.
            const ColorAttribute &colors1 = *((const ColorAttribute *)(colors[i]));
            const ColorAttribute &colors2 = *((const ColorAttribute *)(obj.colors[i]));
            colors_equal = (colors1 == colors2);
        }

        retval = colors_equal;
        }
        break;
    default: retval = false;
    }

    return retval;
}

///////////////////////////////////////////////////////////////////////////////
// User-defined methods.
///////////////////////////////////////////////////////////////////////////////

