#include "videoview2.h"
#include "api/defines.h"
#include <QtGui/qpainter.h>
#include <QtGui/qbitmap.h>
#include "nativewindow.h"

///
CVideoView2::CVideoView2(QWidget* parent)
    : QWidget(parent), m_videoRenderer(0) {
	this->setAttribute(Qt::WA_PaintOnScreen, true);
	this->setFocusPolicy(Qt::StrongFocus);
	this->show();
}
CVideoView2::~CVideoView2() {
	if (m_videoRenderer) {
		avd_render_destory(m_videoRenderer);
		m_videoRenderer = 0;
	}
}
void CVideoView2::updateViewSize(int width, int height) {
    resize(width, height);
    if (0 == m_videoRenderer) {
#if defined (RT_WIN32)
    HWND testHwnd;
    TeeAvdCreateWindow(testHwnd, 0, size().width(), size().height(), (HWND)winId());
    m_window = (void*)testHwnd;
#elif defined(RT_LINUX)
    Window testWindow;
    Display* display;
    TeeAvdCreateWindow(&testWindow, &display, 0, size().width(), size().height(), (Window)winId());
    m_window = (void*)testWindow;
#endif // RT_LINUX
        avd_render_create(m_videoRenderer, m_window, false, false);
        avd_render_setScalingType(m_videoRenderer, Scale_Fill);
    }
}
bool CVideoView2::setScalingType(AVDScalingType type) {
	return avd_render_setScalingType(m_videoRenderer, type);
}

void CVideoView2::paintEvent(QPaintEvent *event) {
	QPainter dc(this);
	QFont font(dc.font());
	font.setPixelSize(15);
	font.setFamily(QString("Microsoft YaHei"));
	QPen pen(QColor(255,0,0));
	dc.setPen(pen);
	dc.setFont(font);
	dc.drawText(10, 0, rect().width()-10, 30, Qt::AlignVCenter || Qt::AlignRight, _user_name);
	QRect borderRect(0, 0, rect().width() - 1, rect().height() - 1);
}
void CVideoView2::resizeEvent(QResizeEvent* event) {
    QWidget::resizeEvent(event);
	if (m_videoRenderer) {
		avd_render_updateSize(m_videoRenderer);
	}
}
