/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   copyright (C) 2003-2006                                               *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

// own header
#include "assocrolepage.h"

// qt includes
#include <tqlayout.h>

// kde includes
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kdebug.h>

// local includes
#include "../dialog_utils.h"

AssocRolePage::AssocRolePage (UMLDoc *d, TQWidget *parent, AssociationWidget *assoc)
        : TQWidget(parent)
{

    m_pAssociationWidget = assoc;
    m_pWidget = 0;
    m_pUmldoc = d;

    m_pRoleALE = 0;
    m_pRoleBLE = 0;
    m_pMultiALE = 0;
    m_pMultiBLE = 0;

    constructWidget();

}

AssocRolePage::~AssocRolePage() {}

void AssocRolePage::constructWidget() {

    // underlying roles and objects
    TQString nameA = m_pAssociationWidget->getRoleName(Uml::A);
    TQString nameB = m_pAssociationWidget->getRoleName(Uml::B);
    TQString titleA = i18n("Role A Properties");
    TQString titleB = i18n("Role B Properties");
    TQString widgetNameA = m_pAssociationWidget->getWidget(Uml::A)->getName();
    TQString widgetNameB = m_pAssociationWidget->getWidget(Uml::B)->getName();
    if(!widgetNameA.isEmpty())
        titleA.append(" (" + widgetNameA + ')');
    if(!widgetNameB.isEmpty())
        titleB.append(" (" + widgetNameB + ')');

    // general configuration of the GUI
    int margin = fontMetrics().height();
    
    TQGridLayout * mainLayout = new TQGridLayout(this, 4, 2);
    mainLayout -> setSpacing(6);

    // group boxes for role, documentation properties
    TQGroupBox *propsAGB = new TQGroupBox(this);
    TQGroupBox *propsBGB = new TQGroupBox(this);
    TQButtonGroup * scopeABG = new TQButtonGroup(i18n("Role A Visibility"), this );
    TQButtonGroup * scopeBBG = new TQButtonGroup(i18n("Role B Visibility"), this );
    TQButtonGroup * changeABG = new TQButtonGroup(i18n("Role A Changeability"), this );
    TQButtonGroup * changeBBG = new TQButtonGroup(i18n("Role B Changeability"), this );
    TQGroupBox *docAGB = new TQGroupBox(this);
    TQGroupBox *docBGB = new TQGroupBox(this);
    propsAGB -> setTitle(titleA);
    propsBGB -> setTitle(titleB);
    docAGB -> setTitle(i18n("Documentation"));
    docBGB -> setTitle(i18n("Documentation"));
    
    TQGridLayout * propsALayout = new TQGridLayout(propsAGB, 2, 2);
    propsALayout -> setSpacing(6);
    propsALayout -> setMargin(margin);

    TQGridLayout * propsBLayout = new TQGridLayout(propsBGB, 3, 2);
    propsBLayout -> setSpacing(6);
    propsBLayout -> setMargin(margin);

    // Properties
    //

    // Rolename A
    TQLabel *pRoleAL = NULL;
    Dialog_Utils::makeLabeledEditField( propsAGB, propsALayout, 0,
                                    pRoleAL, i18n("Rolename:"),
                                    m_pRoleALE, nameA );

    // Multi A
    TQLabel *pMultiAL = NULL;
    Dialog_Utils::makeLabeledEditField( propsAGB, propsALayout, 1,
                                    pMultiAL, i18n("Multiplicity:"),
                                    m_pMultiALE, m_pAssociationWidget->getMulti(Uml::A) );

    // Visibility A
    TQHBoxLayout * scopeALayout = new TQHBoxLayout(scopeABG);
    scopeALayout -> setMargin(margin);

    m_PublicARB = new TQRadioButton(i18n("Public"), scopeABG);
    scopeALayout -> addWidget(m_PublicARB);

    m_PrivateARB = new TQRadioButton(i18n("Private"), scopeABG);
    scopeALayout -> addWidget(m_PrivateARB);

    m_ProtectedARB = new TQRadioButton(i18n("Protected"), scopeABG);
    scopeALayout -> addWidget(m_ProtectedARB);

    m_ImplementationARB = new TQRadioButton(i18n("Implementation"), scopeABG);
    scopeALayout -> addWidget(m_ImplementationARB);

    Uml::Visibility scope = m_pAssociationWidget->getVisibility(Uml::A);
    if( scope == Uml::Visibility::Public )
        m_PublicARB -> setChecked( true );
    else if( scope == Uml::Visibility::Private )
      m_PrivateARB -> setChecked( true );
    else if( scope == Uml::Visibility::Implementation )
      m_PrivateARB -> setChecked( true );
    else
      m_ProtectedARB -> setChecked( true );

    // Changeability A
    TQHBoxLayout * changeALayout = new TQHBoxLayout(changeABG);
    changeALayout -> setMargin(margin);

    m_ChangeableARB = new TQRadioButton(i18n("Changeable"), changeABG);
    changeALayout -> addWidget(m_ChangeableARB);

    m_FrozenARB = new TQRadioButton(i18n("Frozen"), changeABG);
    changeALayout -> addWidget(m_FrozenARB);

    m_AddOnlyARB = new TQRadioButton(i18n("Add only"), changeABG);
    changeALayout -> addWidget(m_AddOnlyARB);

    Uml::Changeability_Type changeability = m_pAssociationWidget->getChangeability(Uml::A);
    if( changeability == Uml::chg_Changeable )
        m_ChangeableARB -> setChecked( true );
    else if( changeability == Uml::chg_Frozen )
        m_FrozenARB -> setChecked( true );
    else
        m_AddOnlyARB -> setChecked( true );

    // Rolename B
    TQLabel * pRoleBL = NULL;
    Dialog_Utils::makeLabeledEditField( propsBGB, propsBLayout, 0,
                                    pRoleBL, i18n("Rolename:"),
                                    m_pRoleBLE, nameB );

    // Multi B
    TQLabel * pMultiBL = NULL;
    Dialog_Utils::makeLabeledEditField( propsBGB, propsBLayout, 1,
                                    pMultiBL, i18n("Multiplicity:"),
                                    m_pMultiBLE, m_pAssociationWidget->getMulti(Uml::B) );

    // Visibility B

    TQHBoxLayout * scopeBLayout = new TQHBoxLayout(scopeBBG);
    scopeBLayout -> setMargin(margin);

    m_PublicBRB = new TQRadioButton(i18n("Public"), scopeBBG);
    scopeBLayout -> addWidget(m_PublicBRB);

    m_PrivateBRB = new TQRadioButton(i18n("Private"), scopeBBG);
    scopeBLayout -> addWidget(m_PrivateBRB);

    m_ProtectedBRB = new TQRadioButton(i18n("Protected"), scopeBBG);
    scopeBLayout -> addWidget(m_ProtectedBRB);

    m_ImplementationBRB = new TQRadioButton(i18n("Implementation"), scopeBBG);
    scopeBLayout -> addWidget(m_ImplementationBRB);

    scope = m_pAssociationWidget->getVisibility(Uml::B);
    if( scope == Uml::Visibility::Public )
        m_PublicBRB -> setChecked( true );
    else if( scope == Uml::Visibility::Private )
        m_PrivateBRB -> setChecked( true );
    else if( scope == Uml::Visibility::Protected )
          m_ProtectedBRB -> setChecked( true );
    else
        m_ImplementationBRB -> setChecked( true );

    // Changeability B
    TQHBoxLayout * changeBLayout = new TQHBoxLayout(changeBBG);
    changeBLayout -> setMargin(margin);

    m_ChangeableBRB = new TQRadioButton(i18n("Changeable"), changeBBG);
    changeBLayout -> addWidget(m_ChangeableBRB);

    m_FrozenBRB = new TQRadioButton(i18n("Frozen"), changeBBG);
    changeBLayout -> addWidget(m_FrozenBRB);

    m_AddOnlyBRB = new TQRadioButton(i18n("Add only"), changeBBG);
    changeBLayout -> addWidget(m_AddOnlyBRB);

    changeability = m_pAssociationWidget->getChangeability(Uml::B);
    if( changeability == Uml::chg_Changeable )
        m_ChangeableBRB -> setChecked( true );
    else if( changeability == Uml::chg_Frozen )
        m_FrozenBRB -> setChecked( true );
    else
        m_AddOnlyBRB -> setChecked( true );

    // Documentation
    //

    // Document A
    TQHBoxLayout * docALayout = new TQHBoxLayout(docAGB);
    docALayout -> setMargin(margin);
    m_pDocA = new TQMultiLineEdit(docAGB);
    docALayout -> addWidget(m_pDocA);
    m_pDocA-> setText(m_pAssociationWidget-> getRoleDoc(Uml::A));
    // m_pDocA-> setText("<<not implemented yet>>");
    // m_pDocA-> setEnabled(false);
    m_pDocA->setWordWrap(TQMultiLineEdit::WidgetWidth);

    // Document B
    TQHBoxLayout * docBLayout = new TQHBoxLayout(docBGB);
    docBLayout -> setMargin(margin);
    m_pDocB = new TQMultiLineEdit(docBGB);
    docBLayout -> addWidget(m_pDocB);
    m_pDocB-> setText(m_pAssociationWidget-> getRoleDoc(Uml::B));
    // m_pDocB-> setEnabled(false);
    m_pDocB->setWordWrap(TQMultiLineEdit::WidgetWidth);

    // add group boxes to main layout 
    mainLayout -> addWidget( propsAGB, 0, 0);
    mainLayout -> addWidget( scopeABG, 1, 0);
    mainLayout -> addWidget(changeABG, 2, 0);
    mainLayout -> addWidget(   docAGB, 3, 0);
    mainLayout -> addWidget( propsBGB, 0, 1);
    mainLayout -> addWidget( scopeBBG, 1, 1);
    mainLayout -> addWidget(changeBBG, 2, 1);
    mainLayout -> addWidget(   docBGB, 3, 1);

}

void AssocRolePage::updateObject() {

    if(m_pAssociationWidget) {

        // set props
        m_pAssociationWidget->setRoleName(m_pRoleALE->text(), Uml::A);
        m_pAssociationWidget->setRoleName(m_pRoleBLE->text(), Uml::B);
        m_pAssociationWidget->setMulti(m_pMultiALE->text(), Uml::A);
        m_pAssociationWidget->setMulti(m_pMultiBLE->text(), Uml::B);

        if(m_PrivateARB->isChecked())
              m_pAssociationWidget->setVisibility(Uml::Visibility::Private, Uml::A);
        else if(m_ProtectedARB->isChecked())
              m_pAssociationWidget->setVisibility(Uml::Visibility::Protected, Uml::A);
        else if(m_PublicARB->isChecked())
            m_pAssociationWidget->setVisibility(Uml::Visibility::Public, Uml::A);
        else if(m_ImplementationARB->isChecked())
              m_pAssociationWidget->setVisibility(Uml::Visibility::Implementation, Uml::A);

        if(m_PrivateBRB->isChecked())
              m_pAssociationWidget->setVisibility(Uml::Visibility::Private, Uml::B);
        else if(m_ProtectedBRB->isChecked())
              m_pAssociationWidget->setVisibility(Uml::Visibility::Protected, Uml::B);
        else if(m_PublicBRB->isChecked())
              m_pAssociationWidget->setVisibility(Uml::Visibility::Public, Uml::B);
        else if(m_ImplementationBRB->isChecked())
              m_pAssociationWidget->setVisibility(Uml::Visibility::Implementation, Uml::B);

        if(m_FrozenARB->isChecked())
            m_pAssociationWidget->setChangeability(Uml::chg_Frozen, Uml::A);
        else if(m_AddOnlyARB->isChecked())
            m_pAssociationWidget->setChangeability(Uml::chg_AddOnly, Uml::A);
        else
            m_pAssociationWidget->setChangeability(Uml::chg_Changeable, Uml::A);

        if(m_FrozenBRB->isChecked())
            m_pAssociationWidget->setChangeability(Uml::chg_Frozen, Uml::B);
        else if(m_AddOnlyBRB->isChecked())
            m_pAssociationWidget->setChangeability(Uml::chg_AddOnly, Uml::B);
        else
            m_pAssociationWidget->setChangeability(Uml::chg_Changeable, Uml::B);

        m_pAssociationWidget->setRoleDoc(m_pDocA->text(), Uml::A);
        m_pAssociationWidget->setRoleDoc(m_pDocB->text(), Uml::B);

    } //end if m_pAssociationWidget

}


#include "assocrolepage.moc"
