/* 
    KWinTV view class
    Copyright (C) 2002 George Staikos (staikos@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.

    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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <qtimer.h>

#include <kcursor.h>
#include <kdebug.h>

#include "view.h"

QtVisionView::QtVisionView(QWidget *parent, const char *name) 
    : QWidget(parent,name ? name : "qtvision_view")
{
  setWFlags(WResizeNoErase | WRepaintNoErase);
  setFocusPolicy(StrongFocus);
  KCursor::setAutoHideCursor( this, true );
  KCursor::setHideCursorDelay( 500 );
  setFocus();
  topLevelWidget()->installEventFilter(this);
}

QtVisionView::~QtVisionView()
{
}

void QtVisionView::resizeEvent(QResizeEvent *e) {
  kdDebug() << "QtVisionView::resizeEvent()" << endl;
  QWidget::resizeEvent(e);

  // If the aspect ratio is set, use it. Otherwise, do nothing.
  if ( aspectRatio != ASPECT_RATIO_NONE ) {
    resizeWithFixedAR();
  }
  emit resized(width(), height());
}

bool QtVisionView::eventFilter(QObject *, QEvent *e) {
    if (e->type() == QEvent::Move) {
        emit moved(x(), y());
    }
    return false;
}

void QtVisionView::setAspectRatio( double aspect, int mode ) {
  aspectRatio = aspect;
  aspectMode = mode;
}

void QtVisionView::resizeWithFixedAR() {
  switch( aspectMode ) {
    case ASPECT_H_TO_W: /* fix height to width */
      resize( width(), (int) (width() / aspectRatio) );
      break;
    case ASPECT_W_TO_H: /* fix width to height */
      resize( (int) (height() * aspectRatio), height() );
      break;
    default:
      kdDebug() << "QtVision::resizeWithFixedAR(). AR mode unknown."
                << "We should never reach this point!" << endl;
      break;
  }
}  

void QtVisionView::setFixedAspectRatio( bool fixed, int mode ) {
  if ( !fixed ) {
      setAspectRatio( ASPECT_RATIO_NONE, mode );
      resize( width(), height() );
  } else {
      setAspectRatio( ASPECT_RATIO_NORMAL, mode );
      resizeWithFixedAR();
  }
  emit resized( width(), height() );
}

void QtVisionView::mouseDoubleClickEvent(QMouseEvent *) {
  kdDebug() << "QtVisionView::mouseDoubleClickEvent()" << endl;
  emit doubleClicked();
}

void QtVisionView::mouseMoveEvent(QMouseEvent *e) {
  KCursor::autoHideEventFilter(this, e);
}

void QtVisionView::focusOutEvent( QFocusEvent *) {
  setFocus();
}

void QtVisionView::wheelEvent( QWheelEvent *e )
{
  e->accept();
  if (e->delta() > 0)
      emit channelUp();
  if (e->delta() < 0)
      emit channelDown();
}


void QtVisionView::keyPressEvent(QKeyEvent *kpe)
{
  kdDebug() << "QtVisionView::keyPressEvent()" << endl;
  switch (kpe->key()) {
    case Key_0:
    case Key_1:
    case Key_2:
    case Key_3:
    case Key_4:
    case Key_5:
    case Key_6:
    case Key_7:
    case Key_8:
    case Key_9:
    {
      kpe->accept ();
      emit numberKeyPressed(kpe->key() - 48);
      break;
    }
    case Key_Enter:
    case Key_Return:
    {
      kpe->accept ();
      emit numberKeyPressed (-1);
      break;
    }
    default:
      kpe->ignore ();
      break;
  } // switch
  
  QWidget::keyPressEvent(kpe);
} // keyPressEvent

void QtVisionView::hideEvent( QHideEvent* ) {
  emit visibilityChanged( false );
}

void QtVisionView::showEvent( QShowEvent* ) {
  emit visibilityChanged( true );
}

#include "view.moc"


