#include "mainwindowimpl.h"
#include <QFile>
#include <QPainter>
#include <QFileDialog>
#include <QKeyEvent>
QString version_i = "v1.0";
QString repertoire_images;
int current_num_image;
int w_ecran, h_ecran;
int largeur_image, hauteur_image;
MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f)
: QMainWindow(parent, f)
{
setupUi(this);
this->setWindowState(Qt::WindowMaximized);
setWindowTitle("visuRelief: " + version_i);
largeur_image = 640;
hauteur_image = 480;
imageLabel1->setGeometry(0,0,largeur_image,hauteur_image);
imageLabel2->setGeometry(largeur_image+1,0,largeur_image,hauteur_image);
repertoire_images = "/media/";
current_num_image = 1;
}
void MainWindowImpl::choix_repertoire_images(void)
{
QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
options |= QFileDialog::DontUseNativeDialog;
QString directory = QFileDialog::getExistingDirectory(this,
tr("Choix repertoire images"), repertoire_images, options);
if (!directory.isEmpty())
{
repertoire_images = directory + "/";
current_num_image = 0;
on_bouton2_clicked();
}
}
void MainWindowImpl::supprimer_images(int n)
{
QString fileName1;
QString fileName2;
QString str_n;
str_n.setNum(n); // conversion num -> txt
fileName1 = "00"+str_n;
fileName1 = repertoire_images + fileName1.right(3);
fileName1 += "-a.jpg";
fileName2 = "00"+str_n;
fileName2 = repertoire_images + fileName2.right(3);
fileName2 += "-b.jpg";
QFile file1(fileName1);
if (file1.exists() ) { file1.remove(); }
QFile file2(fileName2);
if (file2.exists() ) { file2.remove(); }
}
int MainWindowImpl::charger_images(int n)
{
QString fileName1;
QString fileName2;
QString str_n;
int valeur = 0;
str_n.setNum(n); // conversion num -> txt
fileName1 = "00"+str_n;
fileName1 = repertoire_images + fileName1.right(3);
fileName1 += "-a.jpg";
fileName2 = "00"+str_n;
fileName2 = repertoire_images + fileName2.right(3);
fileName2 += "-b.jpg";
statusBar1->showMessage(fileName1 + " - " + fileName2);
QFile file1(fileName1);
if (file1.exists() )
{
QImage image1(fileName1);
image1 = image1.scaledToWidth(largeur_image);
if (case_miroirA->isChecked()) { image1 = image1.mirrored(true, false);}
imageLabel1->setPixmap(QPixmap::fromImage(image1));
}
else
{
QPixmap pixmap1(largeur_image,hauteur_image);
pixmap1.fill(Qt::darkGray);
imageLabel1->setPixmap(pixmap1);
valeur = 1;
}
QFile file2(fileName2);
if (file2.exists() )
{
QImage image2(fileName2);
image2 = image2.scaledToWidth(largeur_image);
if (case_miroirB->isChecked()) { image2 = image2.mirrored(true, false);}
imageLabel2->setPixmap(QPixmap::fromImage(image2));
}
else
{
QPixmap pixmap1(largeur_image,hauteur_image);
pixmap1.fill(Qt::darkGray);
imageLabel2->setPixmap(pixmap1);
valeur = 2;
}
return valeur;
}
void MainWindowImpl::keyPressEvent(QKeyEvent *event)
{
if ( event->key() == Qt::Key_A) { on_bouton1_clicked(); }
else if ( event->key() == Qt::Key_Z) { on_bouton2_clicked(); }
else if ( event->key() == Qt::Key_X) { supprimer_images(current_num_image); }
}
void MainWindowImpl::on_bouton1_clicked()
{
QString tx1;
int valeur;
if (current_num_image == 0) return;
do
{
current_num_image --;
tx1.setNum(current_num_image); // conversion num -> txt
tx1 = "00"+tx1;
tx1 = tx1.right(3);
Edit1->setText(tx1);
valeur = charger_images(current_num_image);
}
while ((valeur != 0) && (current_num_image > 0));
if (current_num_image < 0) {current_num_image = 0; }
}
void MainWindowImpl::on_bouton2_clicked()
{
QString tx1;
int valeur;
if (current_num_image >= 999)
{
current_num_image = 999;
return;
}
do
{
current_num_image ++;
tx1.setNum(current_num_image); // conversion num -> txt
tx1 = "00"+tx1;
tx1 = tx1.right(3);
Edit1->setText(tx1);
valeur = charger_images(current_num_image);
}
while ((valeur != 0) && (current_num_image < 999) );
if (current_num_image > 999) {current_num_image = 999; }
}
void MainWindowImpl::on_case_permut_AB_toggled(bool checked)
{
if (checked)
{
imageLabel1->setGeometry(largeur_image+1,0,largeur_image,hauteur_image);
imageLabel2->setGeometry(0,0,largeur_image,hauteur_image);
}
else
{
imageLabel1->setGeometry(0,0,largeur_image,hauteur_image);
imageLabel2->setGeometry(largeur_image+1,0,largeur_image,hauteur_image);
}
}
void MainWindowImpl::on_case_miroirA_toggled(bool)
{
charger_images(current_num_image);
}
void MainWindowImpl::on_case_miroirB_toggled(bool)
{
charger_images(current_num_image);
}
void MainWindowImpl::on_actionChoix_du_repertoire_images_activated()
{
choix_repertoire_images();
}
void MainWindowImpl::on_bouton0_clicked()
{
QString tx1;
current_num_image = 0;
tx1.setNum(current_num_image); // conversion num -> txt
tx1 = "00"+tx1;
tx1 = tx1.right(3);
Edit1->setText(tx1);
charger_images(current_num_image);
}
void MainWindowImpl::on_bouton4_clicked()
{
QString tx1;
current_num_image = 999;
tx1.setNum(current_num_image); // conversion num -> txt
tx1 = "00"+tx1;
tx1 = tx1.right(3);
Edit1->setText(tx1);
charger_images(current_num_image);
}
void MainWindowImpl::resizeEvent(QResizeEvent *event)
{
w_ecran = event->size().width();
h_ecran = event->size().height();
event->accept();
largeur_image = w_ecran/2;
hauteur_image = (largeur_image * 3) / 4;
if (case_permut_AB->isChecked())
{
imageLabel1->setGeometry(largeur_image+1,0,largeur_image,hauteur_image);
imageLabel2->setGeometry(0,0,largeur_image,hauteur_image);
}
else
{
imageLabel1->setGeometry(0,0,largeur_image,hauteur_image);
imageLabel2->setGeometry(largeur_image+1,0,largeur_image,hauteur_image);
}
charger_images(current_num_image);
}