// -*- c++ -*-

/*
 *
 * Copyright (C) 2002 Richard Moore <rich@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifndef QTVISIONWIDGET_H
#define QTVISIONWIDGET_H

#include <qhbox.h>
#include <qvbox.h>

#include "qtvision.h"
#include "qtvisionviewiface.h"

class QPopupMenu;
class QtVisionView;
class QtVisionWidget;

/**
 * A container for the floating TV window that catches close events
 * and restores the default view mode.
 *
 * @author Richard Moore, rich@kde.org
*/
class QtVisionWidgetFrame : public QVBox
{
    Q_OBJECT

public:
    QtVisionWidgetFrame( QtVisionWidget *parent, const char *name=0 );
    ~QtVisionWidgetFrame();

protected:
    virtual void closeEvent( QCloseEvent *ev );
    virtual void moveEvent( QMoveEvent *ev );
    virtual void resizeEvent( QResizeEvent *ev );

private:
    QtVisionWidget *parent;
};

/**
 * A widget that displays a television picture.
 *
 * @author Richard Moore, rich@kde.org
 */
class QtVisionWidget : public QHBox, virtual public QtVisionViewIface
{
    Q_OBJECT

friend class QtVisionWidgetFrame;
public:
    /**
     * Identifiers for the various view modes.
     */
    enum ViewMode { 
	ViewNormal=0, ViewFullScreen=1, ViewTopLevel=2,
	ViewMaxIterable=ViewTopLevel,
	ViewNone=128, ViewVideoDesktop=129
    };

    /**
     * Creates a TV widget.
     */
    QtVisionWidget( QWidget *parent=0, const char *name=0, int wflags=0 );

    /**
     * Creates a TV widget with a specified driver.
     */
    QtVisionWidget( QtVision *qtv, QWidget *parent=0, const char *name=0, int wflags=0 );

    /**
     * Cleans up.
     */
    virtual ~QtVisionWidget();

    /**
     * Returns the QtVision object driving the TV picture.
     */
    QtVision *driver() const { return qtv; }

    /**
     * Returns the internal screen widget.
     */
    QtVisionView *view() const { return screen; }

    /**
     * Returns a DCOP reference to the driver object.
     */
    DCOPRef driverIface() const;

    /**
     * Returns the current channel store.
     */
    ChannelStore *channels() const { return (qtv ? qtv->channels() : 0); }

    /**
     * Returns the current view manager.
     */
    ViewManager *views() const { return (qtv ? qtv->views() : 0); }

    /**
     * Adds the specified context menu to the screen widget.
     *
     * @see KContextMenuManager
     */
    void addContextMenu( QPopupMenu *pop );

    /**
     * Returns the current view mode.
     */
    int viewMode() const { return mode; }

    /**
     * Returns true if the view mode is ViewNormal.
     */
    bool isShowNormal() const { return mode == ViewNormal; }

    /**
     * Returns true if the view mode is ViewTopLevel.
     */
    bool isShowTopLevel() const { return mode == ViewTopLevel; }

    /**
     * Returns true if the view mode is ViewFullScreen.
     */
    bool isShowFullScreen() const { return mode == ViewFullScreen; }

    /**
     * Returns true if the view mode is ViewVideoDesktop.
     */
    bool isShowVideoDesktop() const { return mode == ViewVideoDesktop; }

    /**
     * Returns true if the view mode is ViewNone.
     */
    bool isShowNone() const { return mode == ViewNone; }

    /**
     * Returns true if the screen widget has been detached from its
     * parent as a floating window.
     */
    bool isFloating() const { return floating; }

public slots:
    /**
     * Sets the QtVision object that drives the TV picture.
     */
    void setDriver( QtVision *qtv );

    /**
     * Forces a return to normal mode.
     */
    void resetViewMode();

    /**
     * Switches to the next view mode.
     */
    void nextViewMode();

    /**
     * Toggle full screen.
     * Change to fullscreen, and store the old mode.
     * If already in fullscreen change back to Normal mode, or to the stored old mode.
     */
    void toggleFullScreen();

    /**
     * Activates the specified view mode.
     */
    void setViewMode( int mode );

    /**
     * If active is true, this method enables the view mode
     * 'None'. This is a special mode that disables the video feed
     * allowing another application (eg. the QtVision screensaver to
     * take over the video source.
     */
    void setShowNone( bool active );

    void setShowNormal( bool active );
    void setShowTopLevel( bool active );
    void setShowFullScreen( bool active );
    void setShowVideoDesktop( bool active );

    /**
     * Changes stay on top of frame.
     */
    void stayOnTopCfgChanged(bool);

    /**
     * Brings window to front.
     */
    void bringToFront();

signals:
    void viewModeChanged( int mode );

    void showNone( bool enabled );
    void showNormal( bool enabled );
    void showTopLevel( bool enabled );
    void showFullScreen( bool enabled );
    void showVideoDesktop( bool enabled );

    void floatingAboutToChange( bool oldval );
    void floatingChanged( bool newval );
    void frameCloseEvent();


protected slots:
    void setFloating( bool enable );

protected:
    void attachScreen();
    void detachScreen();

private:
    QtVision *qtv;
    QtVisionWidgetFrame *frame;
    QtVisionView *screen;
    int mode;
    bool floating;
    bool reparenting;
    int oldMode;
    bool staysOnTop;
};

#endif // QTVISIONWIDGET_H
