#include "widgetViewPhoto.h"
#include "ui_widgetViewPhoto.h"

#include <QFile>
#include <QDesktopWidget>

#include "CAppInfo.h"
#include "logproc.h"

widgetViewPhoto::widgetViewPhoto(int nIndex, QStringList list, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::widgetViewPhoto), m_list(list)
{
    ui->setupUi(this);
	
    
	m_nIdex = nIndex;

    setStyleSheet(g_appInfoPtr->m_sQssStr);

    initUI();
}

widgetViewPhoto::~widgetViewPhoto()
{
    delete ui;
}

void widgetViewPhoto::initUI()
{
    QDesktopWidget *dekwiget = QApplication::desktop();
    setGeometry(0, 0, dekwiget->width(), dekwiget->height());
    ui->widget_vp_bg->setGeometry(0, 0, width(), height());
    ui->widget_vp_client->setGeometry(g_appInfoPtr->m_fRate*80, g_appInfoPtr->m_fRate*54, width() - g_appInfoPtr->m_fRate*80*2, height() - g_appInfoPtr->m_fRate*54*2);
    ui->btn_vp_close->setGeometry(ui->widget_vp_client->width() - g_appInfoPtr->m_fRate*10 - g_appInfoPtr->m_fRate*24, g_appInfoPtr->m_fRate*10, g_appInfoPtr->m_fRate*24, g_appInfoPtr->m_fRate*24);
    ui->label_vp_photo->setGeometry(g_appInfoPtr->m_fRate*82, g_appInfoPtr->m_fRate*40, ui->widget_vp_client->width() - g_appInfoPtr->m_fRate*82*2, ui->widget_vp_client->height() - g_appInfoPtr->m_fRate*40*2);
    ui->btn_prev_photo->setGeometry(g_appInfoPtr->m_fRate*20, (ui->widget_vp_client->height() - g_appInfoPtr->m_fRate*40)/2, g_appInfoPtr->m_fRate*22, g_appInfoPtr->m_fRate*40);
    ui->btn_next_photo->setGeometry(ui->widget_vp_client->width() - g_appInfoPtr->m_fRate*20 - ui->btn_prev_photo->width(), ui->btn_prev_photo->y(),
                                    ui->btn_prev_photo->width(), ui->btn_prev_photo->height());

    QString sFileName = m_list[m_nIdex].right(m_list[m_nIdex].length() - m_list[m_nIdex].lastIndexOf("/") - 1);
    sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
	showImage(sFileName);
}

void widgetViewPhoto::on_btn_prev_photo_clicked()
{
    if(m_nIdex > 0)
    {
        m_nIdex--;

        QString sFileName = m_list[m_nIdex].right(m_list[m_nIdex].length() - m_list[m_nIdex].lastIndexOf("/") - 1);
        sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
		showImage(sFileName);
    }
}

void widgetViewPhoto::on_btn_next_photo_clicked()
{
    if(m_nIdex != m_list.count() - 1)
    {
        m_nIdex++;

        QString sFileName = m_list[m_nIdex].right(m_list[m_nIdex].length() - m_list[m_nIdex].lastIndexOf("/") - 1);
        sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;

		showImage(sFileName);		
    }
}

void widgetViewPhoto::showImage(QString sImage)
{

    int nWidth = ui->widget_vp_client->width() - g_appInfoPtr->m_fRate*82*2;
    int nHeight = ui->widget_vp_client->height() - g_appInfoPtr->m_fRate*40*2;

	QPixmap pix(sImage);
	if (pix.isNull())
	{
		return;
	}
	
	int pixWidth = pix.width();
	int pixHeight = pix.height();
	double fWRate = (double)(nWidth)/pixWidth;
	double fHRate = (double)(nHeight) / pixHeight;
	
    if (fWRate > fHRate)
	{
        nWidth = nHeight* pix.width() / pix.height();
	}
	else
	{
        nHeight = nWidth * pix.height() / pix.width();
	}

	ui->label_vp_photo->setGeometry((ui->widget_vp_client->width() - nWidth) / 2,
		(ui->widget_vp_client->height() - nHeight) / 2, nWidth, nHeight);
	ui->label_vp_photo->setPixmap(pix.scaled(nWidth, nHeight,
		Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

void widgetViewPhoto::on_btn_vp_close_clicked()
{
    emit viewImgClose();
}
