/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            metawidget.cc
 *
 *  Wed Nov 26 08:51:52 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 "metawidget.h"

#include <QHBoxLayout>
#include <QVBoxLayout>

#include "messagebox.h"

#include "widgetbuilder.h"
#include "formatparser.h"

#include "common.h"

MetaWidget::MetaWidget(QDomNode &node, MacroWindow *macrowindow)
  : QFrame(), Widget(node, macrowindow)
{
  setCommonAttributes(this, node);
  setCommonLayout(this, node);
  
  QDomElement elem = node.toElement();
  storechildren = elem.attribute("storechildren", "false") == "true";

  // Create children
  QDomNodeList children = node.childNodes();
  for (int i=0; i<children.count();i++) {
    QDomNode child = children.at(i);
    widgets += widgetBuilder(child, this, macrowindow, false);
  }
  if(storechildren) macrowindow->addWidgets(widgets);
  else macrowindow->addAuxWidgets(widgets);

  // Setup format string
  if(elem.hasAttribute("formatlanguage")) {
    formatlanguage = elem.attribute("formatlanguage");
  } else {
    formatlanguage = "pracro";
  }

  if(elem.hasAttribute("format")) {
    format = elem.attribute("format");
  } else {
    QVector< Widget* >::iterator i = widgets.begin();
    while (i != widgets.end()) {
      Widget* w = *i;
      if(format != "") format += ", ";
      format += "${" + w->getName() + "}";
      i++;
    }
  }

  // Connect all children wasChanged signal, to catch changes.
  QVector< Widget* >::iterator i = widgets.begin();
  while (i != widgets.end()) {
    Widget* w = *i;
    w->connectFrom(SIGNAL(wasChanged()), this, SLOT(changed()));
    i++;
  }
}

void MetaWidget::changed()
{
  emit wasChanged();
}

QString MetaWidget::getValue()
{
  return format_parser(format, widgets, formatlanguage);
}

void MetaWidget::setValue(QString, QString)
{
  // Nothing reasonable we can do here.
}

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

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

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

bool MetaWidget::isValid()
{
  // If children are stored they will validate themselves.
  if(!storechildren) {
    QVector< Widget* >::iterator i = widgets.begin();
    while (i != widgets.end()) {
      Widget* w = *i;
      if(w->isValid() == false) {
        MessageBox::critical(NULL, "Fejl",
                              "Et af inputfelterne (" + w->getName()
                              + ") er ikke udfyldt korrekt, pr�v igen.\n"
                              , MessageBox::Ok);
        return false;
      }
      i++;
    }
  }

  return regexpValidator() && luaValidator();
}

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

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

bool MetaWidget::setKeyboardFocus()
{
  QVector< Widget* >::iterator i = widgets.begin();
  while (i != widgets.end()) {
    Widget* w = *i;
    if(w->setKeyboardFocus()) return true;
    i++;
  }
  return false;
}

void MetaWidget::reset()
{
  // If children are stored they will be reset individually.
  if(!storechildren) {
    QVector< Widget* >::iterator i = widgets.begin();
    while (i != widgets.end()) {
      Widget* w = *i;
      w->reset();
      i++;
    }
  }
}

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