/**
 * Reimplemented from UMLWidget::saveToXMI to save
 * classifierwidget data either to 'interfacewidget' or 'classwidget'
 * XMI element.
 */
void ClassifierWidget::saveToXMI(QDomDocument & qDoc, QDomElement & qElement)
{
    QDomElement conceptElement;
    UMLClassifier *umlc = classifier();

    QString tagName = umlc->isInterface() ?
        "interfacewidget" : "classwidget";
    conceptElement = qDoc.createElement(tagName);
    UMLWidget::saveToXMI( qDoc, conceptElement );

    conceptElement.setAttribute("showoperations", visualProperty(ShowOperations));
    conceptElement.setAttribute("showpubliconly", visualProperty(ShowPublicOnly));
    conceptElement.setAttribute("showopsigs",     m_operationSignature);
    conceptElement.setAttribute("showpackage",    visualProperty(ShowPackage));
    conceptElement.setAttribute("showscope",      visualProperty(ShowVisibility));

    if (! umlc->isInterface()) {
        conceptElement.setAttribute("showattributes", visualProperty(ShowAttributes));
        conceptElement.setAttribute("showattsigs",    m_attributeSignature);
    }

    if (umlc->isInterface() || umlc->isAbstract()) {
        conceptElement.setAttribute("drawascircle", visualProperty(DrawAsCircle));
    }
    qElement.appendChild(conceptElement);
}
/**
 * Reimplemented from UMLWidget::updateTextItemGroups to
 * calculate the Text strings, their properties and also hide/show
 * them based on the current state.
 */
void ClassifierWidget::updateTextItemGroups()
{
    // Invalidate stuff and recalculate them.
    invalidateDummies();

    TextItemGroup *headerGroup = textItemGroupAt(HeaderGroupIndex);
    TextItemGroup *attribOpGroup = textItemGroupAt(AttribOpGroupIndex);
    TextItemGroup *templateGroup = textItemGroupAt(TemplateGroupIndex);

    attribOpGroup->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    templateGroup->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);

    UMLClassifier *umlC = classifier();
    UMLClassifierListItemList attribList = umlC->getFilteredList(UMLObject::ot_Attribute);
    UMLClassifierListItemList opList = umlC->getFilteredList(UMLObject::ot_Operation);

    // Set up template group and template text items.
    UMLTemplateList tlist = umlC->getTemplateList();
    templateGroup->setTextItemCount(tlist.size());
    bool templateHide = shouldDrawAsCircle(); // Hide if draw as circle.
    for(int i = 0; i < tlist.size(); ++i) {
        UMLTemplate *t = tlist[i];
        templateGroup->textItemAt(i)->setText(t->toString());
        templateGroup->textItemAt(i)->setExplicitVisibility(!templateHide);
    }

    // Stereo type and name.
    const int headerItemCount = 2;
    headerGroup->setTextItemCount(headerItemCount);

    const int cnt = attribList.count() + opList.count();
    attribOpGroup->setTextItemCount(cnt);

    // Setup Stereo text item.
    TextItem *stereoItem = headerGroup->textItemAt(StereotypeItemIndex);
    stereoItem->setBold(true);
    stereoItem->setText(umlC->stereotype(true));

    bool v = !shouldDrawAsCircle()
        && visualProperty(ShowStereotype)
        && !(umlC->stereotype(false).isEmpty());
    stereoItem->setExplicitVisibility(v);

    // name item is always visible.
    TextItem *nameItem = headerGroup->textItemAt(NameItemIndex);
    nameItem->setBold(true);
    nameItem->setItalic(umlC->isAbstract());
    nameItem->setUnderline(shouldDrawAsCircle());
    QString nameText = name();
    if (visualProperty(ShowPackage) == true) {
        nameText = umlC->fullyQualifiedName();
    }

    bool showNameOnly = (!visualProperty(ShowAttributes) && !visualProperty(ShowOperations)
                         && !visualProperty(ShowStereotype) && !shouldDrawAsCircle());
    nameItem->setText(nameText);

    int attribStartIndex = 0;
    int opStartIndex = attribStartIndex + attribList.size();

    // Now setup attribute texts.
    int visibleAttributes = 0;
    for (int i=0; i < attribList.size(); ++i) {
        UMLClassifierListItem *obj = attribList[i];

        TextItem *item = attribOpGroup->textItemAt(attribStartIndex + i);
        item->setItalic(obj->isAbstract());
        item->setUnderline(obj->isStatic());
        item->setText(obj->toString(m_attributeSignature));

        bool v = !shouldDrawAsCircle()
            && ( !visualProperty(ShowPublicOnly)
                 || obj->visibility() == Uml::Visibility::Public)
            && visualProperty(ShowAttributes) == true;

        item->setExplicitVisibility(v);
        if (v) {
            ++visibleAttributes;
        }
    }

    // Update expander box to reflect current state and also visibility
    m_attributeExpanderBox->setExpanded(visualProperty(ShowAttributes));

    const QString dummyText;
    // Setup line and dummies.
    if (!showNameOnly) {
        // Stuff in a dummy item as spacer if there are no attributes,
        if (!shouldDrawAsCircle() && (visibleAttributes == 0 || !visualProperty(ShowAttributes))) {
            m_dummyAttributeItem = new TextItem(dummyText);
            int index = attribStartIndex;
            if (visibleAttributes == 0 && !attribList.isEmpty()) {
                index = opStartIndex;
            }
            attribOpGroup->insertTextItemAt(index, m_dummyAttributeItem);
            m_lineItem2Index = index;
            ++opStartIndex;
        }
        else {
            // Now set the second index.
            m_lineItem2Index = opStartIndex - 1;
        }
    }

    int visibleOperations = 0;
    for (int i=0; i < opList.size(); ++i) {
        UMLClassifierListItem *obj = opList[i];

        TextItem *item = attribOpGroup->textItemAt(opStartIndex + i);
        item->setItalic(obj->isAbstract());
        item->setUnderline(obj->isStatic());
        item->setText(obj->toString(m_operationSignature));

        bool v = !shouldDrawAsCircle()
            && ( !visualProperty(ShowPublicOnly)
                 || obj->visibility() == Uml::Visibility::Public)
            && visualProperty(ShowOperations);

        item->setExplicitVisibility(v);
        if (v) {
            ++visibleOperations;
        }
    }
    m_operationExpanderBox->setExpanded(visualProperty(ShowOperations));

    if (!showNameOnly) {
        if (!shouldDrawAsCircle() && (visibleOperations == 0 || !visualProperty(ShowOperations))) {
            m_dummyOperationItem = new TextItem(dummyText);
            attribOpGroup->insertTextItemAt(opStartIndex+opList.size(), m_dummyOperationItem);
        }
    }

    UMLWidget::updateTextItemGroups();
}
/**
 * update the start and end text for this hierarchicalcodeblock.
 */
void CPPHeaderClassDeclarationBlock::updateContent ()
{
    CPPHeaderCodeDocument *parentDoc = dynamic_cast<CPPHeaderCodeDocument*>(getParentDocument());
    UMLClassifier *c = parentDoc->getParentClassifier();
    QString endLine = UMLApp::app()->commonPolicy()->getNewLineEndingChars();
    bool isInterface = parentDoc->parentIsInterface(); // a little shortcut
    QString CPPHeaderClassName = CodeGenerator::cleanName(c->name());
    bool forceDoc = UMLApp::app()->commonPolicy()->getCodeVerboseDocumentComments();

    // COMMENT

    //check if class is abstract.. it should have abstract methods
    if(!isInterface && c->isAbstract() && !c->hasAbstractOps())
    {
        getComment()->setText(QLatin1String("******************************* Abstract Class ****************************") + endLine
                              + CPPHeaderClassName + QLatin1String(" does not have any pure virtual methods, but its author") + endLine
                              + QLatin1String("  defined it as an abstract class, so you should not use it directly.") + endLine
                              + QLatin1String("  Inherit from it instead and create only objects from the derived classes") + endLine
                              + QLatin1String("*****************************************************************************"));
    } else {
        if(isInterface)
            getComment()->setText(QLatin1String("Interface ") + CPPHeaderClassName + endLine + c->doc());
        else
            getComment()->setText(QLatin1String("Class ") + CPPHeaderClassName + endLine + c->doc());
    }

    if(forceDoc || !c->doc().isEmpty())
        getComment()->setWriteOutText(true);
    else
        getComment()->setWriteOutText(false);


    // Now set START/ENDING Text
    QString startText;

    /*
    */

    /*
        if(parentDoc->parentIsInterface())
                startText.append(QLatin1String("interface "));
        else
    */
    startText.append(QLatin1String("class "));

    startText.append(CPPHeaderClassName);

    // write inheritances out
    UMLClassifierList superclasses = c->findSuperClassConcepts();
    int nrof_superclasses = superclasses.count();

    // write out inheritance
    int i = 0;
    if(nrof_superclasses >0)
        startText.append(QLatin1String(" : "));
    foreach (UMLClassifier* concept, superclasses) {
        startText.append(Uml::Visibility::toString(concept->visibility()) + QLatin1Char(' ') +
            CodeGenerator::cleanName(concept->name()));
        if(i != (nrof_superclasses-1))
            startText.append(QLatin1String(", "));
        i++;
    }