/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            altcombobox.cc
 *
 *  Tue Nov 25 08:18:10 CET 2008
 *  Copyright 2008 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Pracro.
 *
 *  Pracro 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.
 *
 *  Pracro is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Pracro; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "altcombobox.h"

#include <QHBoxLayout>
#include <QVBoxLayout>

#include "common.h"
#include "widgetbuilder.h"

#include <QObject>
#include "multilist.h"

AltComboBox::AltComboBox(QDomNode &node, MacroWindow *macrowindow)
  : QFrame(), Widget(node, macrowindow)
{
  innerwidget = NULL;

  setCommonAttributes(this, node);
  setCommonLayout(this, node);
  
  combobox = new ComboBox(node, macrowindow);
  layout()->addWidget(combobox);
  combobox->show();

  altframerepl = new QFrame();
  QHBoxLayout *l = new QHBoxLayout();
  altframerepl->setLayout(l);
  l->addStretch();
  altframe = new QFrame();
  layout()->addWidget(altframe);

  QVector< Widget* > widgets;

  QString iwname;

  QDomNodeList items = node.childNodes();
  for(int i = 0; i < items.count(); i++) {
    QDomElement item = items.at(i).toElement();

    if(item.tagName() == "altitem") {
      
      if(item.hasAttribute("value")) {
        altvalue = item.attribute("value");
      } else {
        printf("ERROR: altitem tag is missing the value attribute, in altcombobox!\n");
      }

      if(item.hasAttribute("innerwidget")) {
        iwname = item.attribute("innerwidget");
      } else {
        printf("ERROR: altitem tag is missing the innerwidget attribute, in altcombobox!\n");
      }

      if(item.hasAttribute("layout")) {
        if(item.attribute("layout") == "hbox") {
          QHBoxLayout *layout = new QHBoxLayout();
          altframe->setLayout(layout);
        } else if(item.attribute("layout") == "vbox") {
          QVBoxLayout *layout = new QVBoxLayout();
          altframe->setLayout(layout);      
        }
      } else {
        QHBoxLayout *layout = new QHBoxLayout();
        altframe->setLayout(layout);
      }

      QDomNodeList children = item.childNodes();
      for(int i = 0; i < children.count(); i++) {
        QDomNode child = children.at(i);
        widgets += widgetBuilder(child, altframe, macrowindow, false);
      }
    }

  }
  macrowindow->addAuxWidgets(widgets);
  /*
  QVector< Widget* >::iterator ws = widgets.begin();
  while(ws != widgets.end()) {
    if((*ws)->getName() == iwname) innerwidget = *ws;
    ws++;
  }
  */

  innerwidget = macrowindow->getWidget(iwname);

  if(innerwidget == NULL) {
    printf("ERROR: Inner Widget %s not found (in multilist)!\n",
           iwname.toStdString().c_str());
  }

  // To detect if the altvalue has been selected:
  connect(combobox, SIGNAL(currentIndexChanged(int)), this, SLOT(onValueChange(int)));
  connect(combobox, SIGNAL(editTextChanged(const QString&)), this, SLOT(onValueChange(const QString&)));

  // To react to changes in any of the children:
  connect(combobox, SIGNAL(wasChanged()), this, SLOT(onChildChange()));
  innerwidget->connectFrom(SIGNAL(wasChanged()), this, SLOT(onChildChange()));

  layout()->setContentsMargins(0,0,0,0);
  altframe->layout()->setContentsMargins(0,0,0,0);

  show(); // Force altframe to get resized to its real size.
  altframerepl->setFixedHeight(altframe->height());
}


bool AltComboBox::isValid()
{
  if(!combobox->isValid()) return false;

  if(innerwidget && combobox->getValue() == altvalue) {
    if(!innerwidget->isValid()) return false;
  }

  return regexpValidator() && luaValidator();
}

QString AltComboBox::getValue()
{
  if(combobox->getValue() == altvalue) {
    if(innerwidget) return innerwidget->getValue();
    else return "";
  } else {
    return combobox->getValue();
  }
}

void AltComboBox::setValue(QString value, QString source)
{
  //  if(isUserSource(source)) emit wasChanged(); // No need for this, it will be enitted by the children.

  if(combobox->findData(value) != -1) {

    combobox->setValue(value, source);

  } else {
    combobox->setValue(altvalue);

    if(innerwidget) {
      innerwidget->setValue(value, source);
    }
  }

  setInitialValue(value);
}

void AltComboBox::onValueChange(int index)
{
  if(combobox->itemData(index).toString() == altvalue) {
    //    altframe->setEnabled(true);
    altframerepl->setVisible(false);
    layout()->removeWidget(altframerepl);

    layout()->addWidget(altframe);
    altframe->setVisible(true);
  } else {
    //    altframe->setEnabled(false);
    altframe->setVisible(false);
    layout()->removeWidget(altframe);

    layout()->addWidget(altframerepl);
    altframerepl->setVisible(true);
  }
}

void AltComboBox::onValueChange(const QString &text)
{
  onValueChange(combobox->findText(text));
}

void AltComboBox::enable()
{
  setEnabled(true);
}

void AltComboBox::disable()
{
  setEnabled(false);
}

bool AltComboBox::isDisabled()
{
  return isEnabled() == false;
}

void AltComboBox::onChildChange()
{
  emit wasChanged();
}

void AltComboBox::connectFrom(const char *signal,
                           const QObject *receiver, const char *method)
{
  connect(this, signal, receiver, method);
}

void AltComboBox::connectTo(const QObject *sender, const char *signal,
                         const char *method)
{
  connect(sender, signal, this, method);
}

bool AltComboBox::setKeyboardFocus()
{
  if(combobox->getValue() == altvalue) {
    if(innerwidget) return innerwidget->setKeyboardFocus();
  }

  combobox->setKeyboardFocus();
  return true;
}

void AltComboBox::setVisibility(bool visible)
{
  setVisible(visible);
}