#include "render.h"
#include "ui_render.h"

#include <iostream>


render::render(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::render)
{
    ui->setupUi(this);
    item=NULL;
    scene=NULL;
    image=NULL;

QString f=this->windowTitle();
std::cout<<"ono ma nazev "<<f.toStdString()<<"\n"<<std::endl;


    scene=new QGraphicsScene(this);
}


render::~render()
{
    delete ui;

    if(item!=NULL)
        delete item;
    if(scene!=NULL)
        delete scene;
    if(image!=NULL)
        delete image;
}

void render::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void render::show(QString path)
{
    if(item!=NULL)
        delete item;

    QPixmap i =QPixmap(path);

    QString text="";
    text=QString::number(i.size().width());
    text.append(" x ");
    text.append(QString::number(i.size().height()));

    this->File_Path="";

    this->ui->label_size->setText(text);

    int w=this->size().width()*0.95;//-10;
    int h=this->size().height()*0.85;//-30;
    //*/
    /*int w=ui->graphicsView->size().width();
    int h=ui->graphicsView->size().height();*/


    //QPixmap sized=i.scaled(QSize(w,h), Qt::KeepAspectRatioByExpanding);
    QPixmap sized=i.scaled(QSize(w,h), Qt::KeepAspectRatio);

    if(i.size().width()<w)
        item=new QGraphicsPixmapItem(i);
    else
        item=new QGraphicsPixmapItem(sized);

    scene->addItem(item);
    ui->graphicsView->setScene(scene);

    //this->show();
    this->setVisible(true);
}

void render::mousePressEvent(QMouseEvent * e)
{
        // ignorujeme dvojkliky jiným než pravým tlačítkem myši
        if(e->buttons() != Qt::LeftButton)
                return;

        QString fileName = QFileDialog::getOpenFileName(
                                this,
                                tr("Nacti obraz"),
                                "",
                                tr("Image (*.jpg *.png);;JPEG (*.jpg *.jpeg);;PNG (*.png)")
                                );

        if(fileName.length()==0)
            return;

        this->Load_Image(fileName);
       /* this->show(fileName);
        this->createMyImage(fileName);
        this->File_Path=fileName;
        emit this->new_image_load(this);//*/
}


void render::createMyImage(QString path){
    if(image!=NULL)
        delete image;
    image=load_image(path.toAscii());
}

void render::Load_Image(QString fileName){
    if(!QFile::exists(fileName))
        return;
    this->show(fileName);
    this->createMyImage(fileName);
    this->File_Path=fileName;
    emit this->new_image_load(this);//*/
}
