/***************************************************************************
   This file is part of LinTV
   lintvview.h
   The main widget in LinTV.

   Author: Jesse L. Zamora <xtremek2009@gmail.com>

   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.
 ***************************************************************************/

#ifndef LINTVVIEW_H
#define LINTVVIEW_H

#define LINTV_DEFAULT_SIZE QSize(860, 568)

/* LinTV Includes */
#include "lintvplay.h"
#include "lintvrecord.h"
#include "lintvdeviceinfo.h"

/* KDE Includes */
#include <kxmlguiwindow.h>

/* Ui Includes */
#include "ui_lintvview_base.h"

/* Qt Includes */
#include <QWidget>

class ScreenshotListWidget;
class ScreenshotViewer;
class LinTVStatusWidget;
class LinTVConfigDialog;

class KMenu;
class KXMLGUIBuilder;
class KXMLGUIFactory;

class QTimer;
class QActionGroup;

/**
 * This is the main view class for LinTV.  Most of the non-menu,
 * non-toolbar, and non-statusbar (e.g., non frame) GUI code should go
 * here.
 *
 * @short Main view
 * @author Jesse L. Zamora <xtremek2008@aim.com>
 * @version 0.1
 */

class LinTVView : public KXmlGuiWindow
{
    Q_OBJECT
public:
    /**
     * Default constructor
     */
    LinTVView(QStringList devices);

    /**
     * Destructor
     */
    virtual ~LinTVView();

    void saveSettings();

    float aspectRatio() { return m_aspectRatio; }

    void setChannel(int );
    void setVolume(int );
    void setMute(bool );
    void setInput(int value);
    void setRecordButton(bool );
    void setRecordMode(bool );
    void setDeviceIndex(int );

    void checkSize();

    void setInputs(QStringList );

    QLabel *videoOutputWidget() { return videoOutput; }
    LinTVStatusWidget *statusWidget() { return m_statusWidget; }
    ScreenshotListWidget *screenshotWidget() { return m_screenshotListWidget; }

signals:
    /**
     * Use this signal to change the content of the caption
     */
    void signalChangeCaption(const QString& text);

    void volumeChanged(int );
    void muteToggled(bool );
    void pauseToggled(bool );

    void startRecordingToggled(bool );
    void inputChanged(int );

    void deviceNameSelected(const QString & );

    void channelUp();
    void channelDown();
    void settingsChanged();

    void showColorDialog();
    void showDeviceInfoDialog();

    void exitEvent();

protected:
    /** When the user presses Esc, this returns to regular mode */
    void keyPressEvent(QKeyEvent *);
    /** This resizes the LinTVPlay widget to the correct size & aspect. */
    void resizeEvent(QResizeEvent *);
    /** Handles the right click event on the LinTVPlay widget */
    void contextMenuEvent(QContextMenuEvent *);

    /** If the user presses the mouse, then LinTV enters drag mode */
    void mousePressEvent(QMouseEvent *);
    /** If the user has the mouse pressed down, this moves the LinTV
    window */
    void mouseMoveEvent(QMouseEvent *);
    /** When the user releases the mouse, LinTV exits drag mode */
    void mouseReleaseEvent(QMouseEvent *);
    /** LinTV will enter fullscreen mode or exit it on a mouse
    double click event */
    void mouseDoubleClickEvent(QMouseEvent *);

    void closeEvent(QCloseEvent *event);

private slots:

    /** Toggle fullscreen mode **/
    void slotToggleTVMode();
    /** Set the window on top of all others */
    void slotSetAlwaysOnTop(bool );
    /** Select the device to view from **/
    void slotSelectDevice(QAction *);
    /** Set the window to the default size **/
    void slotSetDefaultSize();
    /** Set the window size based on the action clicked by the user
    in the Video menu */
    void slotSetWindowSize(QAction *);
    /** Show the LinTV Configure dialog **/
    void slotShowConfigureDialog();

    void slotSetVolume(int );
    void slotToggleMute(bool );
    /** Pause the video playing (works with TV too!) **/
    void slotTogglePause(bool );

    /** Start recording from the current device and input **/
    void slotRecordButtonToggled(bool );

    /** This is called every 1.5 seconds. */
    void slotCursorHideTimer();

    /** Set the channel +1 the current channel **/
    void slotChannelUp();
    /** Set the channel -1 the current channel **/
    void slotChannelDown();

    /** When the user clicks "View Screenshot" or double clicks
        a screenshot in the ScreeshotWidget, this opens it in the
        main window on top of the playing video. */
    void slotDisplayScreenshot(QPixmap image);
    /** If the user deletes a screenshot while the user is viewing 
        it in the main window, this is called to load a different 
        screenshot into the main window. */
    void slotRemovedScreenshot(QPixmap newImage);
    /** When the user clicks the "Return to TV" button, this closes
        the ScreenshotViewer */
    void slotReturnToTV();

    /** When the user clicks "View Device Info" action, just
        display the LinTVDeviceInfoDialog. */
    void slotShowDevicesInfo();

    void slotSetInput(int );

    void slotTakeScreenshot();

    void quit();

private:
    void setupLinTVUi();
    void connectSignalsSlots();
    void checkInput();

    void updateChannelLabel();

private:
    LinTVConfigDialog *m_configDialog;
    LinTVStatusWidget *m_statusWidget;

    QStringList m_devices;
    QWidget *widget;
    QLabel *videoOutput;

    ScreenshotListWidget *m_screenshotListWidget;
    ScreenshotViewer *m_screenshotViewer;

    Ui::lintvview_base view;

    KXMLGUIFactory  *m_guiFactory;
    KXMLGUIBuilder  *m_guiBuilder;

    KMenu *m_contextMenu;
    KMenu *m_devicesMenu;
    KMenu *m_videoSizeMenu;
    KMenu *m_screenshotContextMenu;
    KMenu *m_trayContextMenu;

    QActionGroup *m_devicesActionGroup;
  
    QTimer *m_cursorHideTimer;

    QPoint m_dragOffset;
    QPoint m_lastPos;

    static bool m_initConfigDialog;

    float m_aspectRatio;
    bool m_fullscreen;
    bool m_dragging;
    bool m_cursorHidden;
    bool m_mouseIsMoving;
    bool m_recordMode;
};

#endif // LinTVVIEW_H
