/***************************************************************************
 *   Copyright (C) 2005 Novell, Inc.                                       *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA           *
 ***************************************************************************/

#include "hitwidget.h"
#include "kerrylabel.h"

#include <kpushbutton.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <QToolButton>
#include <kfileitem.h>
//Added by qt3to4:
#include <QEvent>
#include <klocale.h>
#include <kurllabel.h>
#include <kglobalsettings.h>
#include <QLayout>
//#include <konq_filetip.h>
#include <q3scrollview.h>

HitWidget::HitWidget(QString uri, QString mimetype, KWidgetListbox *parent)
  : QWidget(parent), Ui_HitWidgetLayout(), m_uri(uri), m_parent_uri(""), m_mimetype(mimetype), m_collapsed(false),
    m_is_collapsible(false), m_icon(QString::null), m_result(0)
{
  setAutoFillBackground(true);
  setupUi(this);

//  HitWidgetLayoutLayout->setMargin(4);
  toolButton1->setEnabled(false);

  score->setText("");
  setDescriptionText("");
  setPropertiesText("");
  hiticon->installEventFilter(this);
//  pFileTip = new KonqFileTip(parent);
//  pFileTip->setItem(0L);
  qsv=parent;

  connect(toolButton1, SIGNAL(clicked()), SLOT(toggleCollapsed()));
}

HitWidget::~HitWidget()
{
//  delete pFileTip;
}

void HitWidget::setIcon(const QString name)
{
  m_icon = name;
  if (m_collapsed)
    hiticon->setPixmap(KIconLoader::global()->loadIcon(m_icon, KIconLoader::NoGroup, KIconLoader::SizeSmall));
  else
    hiticon->setPixmap(KIconLoader::global()->loadIcon(m_icon, KIconLoader::NoGroup, KIconLoader::SizeLarge));
}

void HitWidget::toggleCollapsed()
{
  setCollapsed(!m_collapsed);
}

void HitWidget::setCollapsible(BeagleSearch::beagle_result_struct *result)
{
  m_result = result;
  m_is_collapsible = (m_result!=0);

  bool show_expanded;
  if (result)
     show_expanded = m_result->show_expanded;

  m_collapsed=!m_collapsed;
  toggleCollapsed();

  if (result)
    m_result->show_expanded = show_expanded;

  if (m_is_collapsible)
    toolButton1->setEnabled(true);
}

void HitWidget::setCollapsed(bool collapsed)
{
  if (!m_is_collapsible || m_collapsed==collapsed)
    return;

  if (collapsed) {
    hiticon->setPixmap(KIconLoader::global()->loadIcon(m_icon, KIconLoader::NoGroup, KIconLoader::SizeSmall));
    toolButton1->setIconSet(SmallIconSet("kerry_info"));
    toolButton1->setTextLabel(i18n("Expand"));
    description->setHidden(true);
    properties->setHidden(true);
    score->setHidden(true);
    hiticon->setMinimumSize( QSize( 64, 16 ) );
    hiticon->setMaximumSize( QSize( 64, 16 ) );
    m_collapsed = collapsed;
  }
  else {
    hiticon->setPixmap(KIconLoader::global()->loadIcon(m_icon, KIconLoader::NoGroup, KIconLoader::SizeLarge));
    toolButton1->setIconSet(SmallIconSet("kerry_arrow"));
    toolButton1->setTextLabel(i18n("Collapse"));
    description->setHidden(false);
    properties->setHidden(false);
    score->setHidden(false);
    hiticon->setMinimumSize( QSize( 64, 64 ) );
    hiticon->setMaximumSize( QSize( 64, 64 ) );
    m_collapsed = collapsed;
    emit uncollapsed(this);
  }

  if (m_result) 
     m_result->show_expanded = !collapsed;

  if (qsv)
     qsv->adjustSize(this);
}

bool HitWidget::isCollapsed() const
{
  return m_collapsed;
}

bool HitWidget::isCollapsible() const
{
  return m_is_collapsible;
}

void HitWidget::setDescriptionText(QString text)
{
  description->setText(text);
}

void HitWidget::setPropertiesText(QString text)
{
  properties->setText(text);
}

void HitWidget::insertHeaderWidget( int index, QWidget * widget)
{
  layoutHeader->insertWidget(index,widget);
}

void HitWidget::insertHitWidget( int index, QWidget * widget)
{
  layoutButton->insertWidget(index,widget);
}

void HitWidget::insertTextWidget( int index, QWidget * widget)
{
  layoutText->insertWidget(index,widget);
}

void HitWidget::insertHitSpacing( int index, int size)
{
  layoutButton->insertSpacing(index,size);
}

QString HitWidget::uri() const
{
  return m_uri;
}

QString HitWidget::parentUri() const
{
  return m_parent_uri;
}

void HitWidget::setUri(const QString uri)
{
//  pFileTip->setItem(0L);
  m_uri = uri;
}

void HitWidget::setParentUri(const QString uri)
{
  m_parent_uri = uri;
}

QString HitWidget::mimetype() const
{
  return m_mimetype;
}

void HitWidget::adjustSize()
{
  int dwidth, pwidth;

  QWidget::adjustSize();
  if (m_is_collapsible) {
     dwidth = width()-160;
     pwidth = width()-160;
  }
  else {
     dwidth = description->size().width();
     pwidth = properties->size().width();
  }

  description->setFixedSize(dwidth,description->heightForWidth(dwidth));
  properties->setFixedSize(pwidth,properties->heightForWidth(pwidth));
  QWidget::adjustSize();
}

bool HitWidget::eventFilter( QObject *obj, QEvent *ev )
{
  if ( obj == hiticon && !m_uri.isEmpty() ) {
    if ( ev->type() == QEvent::Enter && parent() ) {
//      pFileTip->setOptions(true, true, 6);
      KFileItem *fileitem=new KFileItem(m_uri,m_mimetype,KFileItem::Unknown);
      QPoint viewport = qsv->viewport()->mapFromGlobal(mapToGlobal(hiticon->pos()));
      QRect qr(qsv->viewportToContents(viewport),QSize(hiticon->width()*2,hiticon->height()));
//      pFileTip->setItem(fileitem,qr,hiticon->pixmap());
    }
    else if ( ev->type() == QEvent::Leave ) {
//      pFileTip->setItem(0L);
    }
    return QWidget::eventFilter( obj, ev );
  }
  return false;
}

int HitWidget::collapsedHeight() const
{
  return description->fontMetrics().height()+14;
}

#include "hitwidget.moc"
