/* -*- 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>

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();
  
  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);
      }
    }

  }
  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());
  }

  connect(combobox, SIGNAL(currentIndexChanged(int)), this, SLOT(onValueChange(int)));
  connect(combobox, SIGNAL(editTextChanged(const QString&)), this, SLOT(onValueChange(const QString&)));

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

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

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

  return true;
}

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

void AltComboBox::setValue(QString value)
{
  combobox->setValue(value);

  if(combobox->isValid() == false) { // Combobox contain idx == -1 (invalid) if value didn't exist.
    combobox->setValue(altvalue);

    printf("Value %s not in combo.\n", value.toStdString().c_str());

    if(innerwidget) {
      printf("\tSetting value on inner widget (%s) to \"%s\".\n", innerwidget->getName().toStdString().c_str(), value.toStdString().c_str());
      printf("\told value (%s).\n", innerwidget->getValue().toStdString().c_str());
      innerwidget->setValue(value);
    } else {
      printf("Could not set value in AltComboBox, no innerwidget!\n");
    }
    altframe->setEnabled(true);
  } else {
    altframe->setEnabled(false);
  }
}

void AltComboBox::onValueChange(int index)
{
  if(combobox->itemData(index).toString() == altvalue) {
    altframe->setEnabled(true);
  } else {
    altframe->setEnabled(false);
  }
}

void AltComboBox::onValueChange(const QString &text)
{
  int index = combobox->findText(text);
  if(index != -1 && combobox->itemData(index).toString() == altvalue) {
    altframe->setEnabled(true);
  } else {
    altframe->setEnabled(false);
  }
}

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

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