/***************************************************************************
                          structtreetag.cpp  -  description
                             -------------------
    begin                : Sat Apr 29 2000
    copyright            : (C) 2000 by Yacovlev Alexander & Dmitry Poplavsky <pdima@mail.univ.kiev.ua>
                           (C) 2002, 2003 Andras Mantia <amantia@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
//qt include
#include <tqregexp.h>

// KDE includes
#include <kiconloader.h>
#include <klocale.h>

// app includes
#include "structtreeview.h"
#include "structtreetag.h"
#include "messageoutput.h"
#include "tag.h"
#include "node.h"
#include "quantacommon.h"
#include "document.h"
#include "resource.h"

StructTreeTag::StructTreeTag(TQListView *tqparent, TQString a_title)
  : KListViewItem(tqparent, a_title)
{
  node = 0L;
  hasOpenFileMenu = false;
  groupTag = 0L;
  tqparentTree = static_cast<StructTreeView*>(tqparent);
}

StructTreeTag::StructTreeTag(StructTreeTag *tqparent, Node *a_node, const TQString a_title,
                             TQListViewItem *after )
: KListViewItem(tqparent, after, a_title)
{
  tqparentTree = tqparent->tqparentTree;
  hasOpenFileMenu = false;
  groupTag = 0L;
  static const TQString space = " ";
  static const TQRegExp nbspRx("&nbsp;|\\n");
  node = a_node;
  if (node)
  {
    Tag *tag = node->tag;
    TQString title = tag->name.lower();
    if (a_title.isEmpty())
    {
      switch (tag->type)
      {
        case Tag::XmlTag:
            {
              if (title == "font")
              {
                setPixmap( 0, UserIcon("tag_font_small") );
                if ( tag->attrCount() )
                  title = space + tag->attribute(0) + "=" + tag->attributeValue(0);
                else
                  title = "";
              } else
              if (title == "img")
              {
                setPixmap( 0, SmallIcon("image") );
                title = space + tag->attributeValue("src");

              } else
              if (title == "a")
              {
                setPixmap( 0, SmallIcon("www") );
                if ( tag->hasAttribute("href") )
                  title = space + "href "+ tag->attributeValue("href");
                if ( tag->hasAttribute("name") )
                  title = space + "name "+ tag->attributeValue("name");
              } else
              if ( title == "br")
              {
                  setPixmap( 0, UserIcon("tag_br_small") );
                  title = "";
              } else
              if ( title == "hr")
              {
                setPixmap( 0, UserIcon("tag_hr_small") );
                title = "";
              } else
              if ( title == "li")
              {
                setPixmap( 0, UserIcon("ball") );
                title = "";
              } else
              if ( title == "p")
              {
                setPixmap( 0, UserIcon("tag_p") );
                title = "";
              }

              if (!typingInProgress)
              {
                TQTag *tqparentTQTag = 0L;
                if (node->tqparent)
                  tqparentTQTag = QuantaCommon::tagFromDTD(node->tqparent);
                TQString qTagName = node->tag->dtd()->caseSensitive ? node->tag->name : node->tag->name.upper();
                int line, col;
                node->tag->beginPos(line, col);
                if (tqparentTQTag && !tqparentTQTag->childTags.tqcontains(qTagName) &&
                !tqparentTQTag->childTags.isEmpty())
                {
                  node->tag->write()->setErrorMark(line);
                  TQString tqparentTagName = node->tag->dtd()->caseSensitive ? node->tqparent->tag->name : node->tqparent->tag->name.upper();
                  tqparentTree->showMessage(i18n("Line %1: %2 is not a possible child of %3.\n").tqarg(line + 1).tqarg(qTagName).tqarg(tqparentTagName));
                }
                TQString nextTagName;
                if (node->next)
                {
                  nextTagName = node->tag->dtd()->caseSensitive ? node->next->tag->name : node->next->tag->name.upper();
                }
                tqparentTQTag = QuantaCommon::tagFromDTD(node);
                if (tqparentTQTag && !tqparentTQTag->isSingle() &&
                    !tqparentTQTag->isOptional() &&
                    (!node->next || ( !node->getClosingNode()))  )
                {
                  node->tag->write()->setErrorMark(line);
                  tqparentTree->showMessage(i18n("Line %1, column %2: Closing tag for %3 is missing.").tqarg(line + 1).tqarg(col + 1).tqarg(qTagName));
                } else
                if (!tqparentTQTag && node->tag->name.upper() != "!DOCTYPE")
                {
                  node->tag->write()->setErrorMark(line);
                  tqparentTree->showMessage(i18n("Line %1, column %2: %3 is not part of %4.").tqarg(line + 1).tqarg(col + 1).tqarg(qTagName).tqarg(node->tag->dtd()->nickName));
                }
              }
              break;
            }
        case Tag::Text:
            {
              title = tag->tagStr();
              title = title.left(70).stripWhiteSpace();
              title.tqreplace( nbspRx," ");
              break;
            }
        case Tag::Comment:
            {
              setPixmap( 0, UserIcon("tag_comm") );
              title = tag->tagStr();
              title = title.left(70).stripWhiteSpace();
              title.tqreplace( nbspRx," ");
              break;
            }
         case Tag::ScriptTag:
            {
              title = tag->name;
              break;
            }
         case Tag::Empty:
            {
              title = i18n("Empty tag");
              break;
            }
         case Tag::ScriptStructureBegin:
             {
              title = tag->cleanStr.stripWhiteSpace();
              break;
            }
         default:
            {
              if (!typingInProgress && node->tag->type == Tag::XmlTagEnd && !node->tag->name.isEmpty())
              {
                int line, col;
                node->tag->beginPos(line, col);
                TQString qTagName = node->tag->dtd()->caseSensitive ? node->tag->name : node->tag->name.upper();
                TQString qPrevTagName;
                if (node->prev)
                 qPrevTagName = node->tag->dtd()->caseSensitive ? node->prev->tag->name : node->prev->tag->name.upper();
                if (!node->prev || qTagName != "/" + qPrevTagName)
                {
                  node->tag->write()->setErrorMark(line);
                  tqparentTree->showMessage(i18n("Line %1, column %2: Opening tag for %3 is missing.").tqarg(line + 1).tqarg(col + 1).tqarg(qTagName));
                }
              }
              title = tag->tagStr().left(70).stripWhiteSpace();
            }


      }
    } else
    {
      title = a_title;
    }

    title.tqreplace(newLineRx," ");
    setText(0, title);
    node->listItems.append(this);
  }
}

StructTreeTag::StructTreeTag(StructTreeTag *tqparent, TQString a_title )
: KListViewItem(tqparent, a_title)
{
  node = 0L;
  hasOpenFileMenu = false;
  groupTag = 0L;
  tqparentTree = tqparent->tqparentTree;
}


StructTreeTag::~StructTreeTag(){
  if (node)
    node->listItems.remove(node->listItems.tqfind(this));
}