No Description

mainwindow.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QShortcut>
  4. #include <QMessageBox>
  5. MainWindow::MainWindow(QWidget *parent) :
  6. QMainWindow(parent),
  7. ui(new Ui::MainWindow)
  8. {
  9. ui->setupUi(this);
  10. ///
  11. /// Keyboard shortcuts
  12. /// Atajos del teclado
  13. /// Closes window with cmd + w
  14. /// Cierra la ventana con cmd + w
  15. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this, SLOT(close()));
  16. ///
  17. /// Loads a image with cmd + o
  18. /// Carga una imagen con cmd + o
  19. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_O), this, SLOT(on_btnLoadImages_clicked()));
  20. ///
  21. /// Removes noise from a image with cmd + r
  22. /// Elimina el ruido de una imagen con cmd + r
  23. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_R), this, SLOT(on_btnRemoveNoise_clicked()));
  24. ///
  25. /// Saves an image without noise with cmd + s
  26. /// Guarda una imagen sin ruido con cmd + s
  27. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_S), this, SLOT(on_btnSaveImage_clicked()));
  28. }
  29. MainWindow::~MainWindow(){
  30. delete ui;
  31. }
  32. void MainWindow::loadImages(vector<QImage> & images, QString path){
  33. QDir dir(path); // Create a QDir with the given path
  34. // Crea un QDir con el path recibido
  35. dir.setFilter(QDir::Files); // Set a filter so only files are looked for
  36. // Pone un filtro para ver solo archivos
  37. dir.setSorting(QDir::Name); // Set sorting by name
  38. // Para ordenar por nombre
  39. // Get the list of files in the directory
  40. // Obtiene la lista de los archivos en el directorio
  41. QFileInfoList list = dir.entryInfoList();
  42. QFileInfo fileInfo; // Holds the info of the next file to be checked
  43. // Guarda la informacion de el proximo archivo a revisar
  44. // String to store the path of the current file being checked
  45. // Cadena para guardar el camino del archivo actual en revisión
  46. QString filePath;
  47. // String to store the name of the current file being checked
  48. // Cadena para guardar el nombre del archivo actual a revisar
  49. QString fileName;
  50. for(int i = 0; i < list.size(); i++){
  51. fileInfo = list.at(i); // Get the file info at possition i
  52. // Obtiene la informacion del archivo en posicion i
  53. filePath = fileInfo.filePath(); // Get the file path
  54. // Obtiene el camino al archivo
  55. fileName = fileInfo.fileName(); // Get the file name
  56. // Obtiene el nombre del archivo
  57. QImage image(filePath); // Load the file as an image
  58. // Carga el archivo como una imagen
  59. // If the file cant be loaded continue
  60. // Si no puede cargar el archivo continua al proximo
  61. if(image.isNull())
  62. continue;
  63. // Add the image to the image_value vector
  64. // Anade la imagen al vector de imagenes.
  65. else
  66. images.push_back(image) ;
  67. }
  68. }
  69. void MainWindow::on_btnLoadImages_clicked(){
  70. // Clearing vectors if they are already populated with previous images
  71. // Limpiando los vectores si han sido llenado con imagenes previas
  72. images.clear();
  73. // Display a browse folder dialog. Asign the selected path to directory.
  74. // Despliega un buscador de carpetas. Asigna el camino seleccionado a un directorio.
  75. QString directory = QFileDialog::getExistingDirectory(this, tr("Find Files"), QDir::currentPath());
  76. // Vector that contains the labels that will hold the images
  77. // Vector que contiene las etiquetas que desplegaran las imagenes en el GUI.
  78. QVector<QLabel *> labels;
  79. // Restricted to a maximum of 9 images in the GUI
  80. // Restringido a un maximo de 9 imagenes en el GUI
  81. labels.append(ui->lblImage1);
  82. labels.append(ui->lblImage2);
  83. labels.append(ui->lblImage3);
  84. labels.append(ui->lblImage4);
  85. labels.append(ui->lblImage5);
  86. labels.append(ui->lblImage6);
  87. labels.append(ui->lblImage7);
  88. labels.append(ui->lblImage8);
  89. labels.append(ui->lblImage9);
  90. // Loads the images to the image vector
  91. // Carga las imagenes al vector de imagenes.
  92. loadImages(images, directory);
  93. // Setting the image labels with the orinal images
  94. // Ajusta la imagen a las etiquetas.
  95. if(images.empty()){
  96. QMessageBox::information(this,"title goes here","Directory doesn't contain images");
  97. }
  98. else if (images.size() >= 1 && images.size() <= 2){
  99. images.clear();
  100. QMessageBox::information(this,"title goes here","ERROR: Directory must contain at least 3 images");
  101. }
  102. else{
  103. if (images.size() <= 9)
  104. for(unsigned int i = 0; i<images.size(); i++)
  105. labels[i]->setPixmap(QPixmap::fromImage(images[i]));
  106. else
  107. QMessageBox::information(this,"title goes here","ERROR: Directory contains more than 9 images");
  108. }
  109. }
  110. void MainWindow::on_btnRemoveNoise_clicked(){
  111. if(!images.empty()){
  112. finalImage = images[0] ;
  113. RemoveNoise(images, finalImage) ;
  114. // Displaying a new image without noise
  115. ui->lblFinalImage->setPixmap(QPixmap::fromImage(finalImage.scaledToWidth(ui->lblFinalImage->width())));
  116. }
  117. }
  118. void MainWindow::on_btnSaveImage_clicked(){
  119. if(!images.empty()){
  120. QPixmap out = QPixmap::grabWidget(this,361,10,481,481);
  121. QString fname = QFileDialog::getSaveFileName(this, tr("Save Edited Image"), (""), tr("PNG (*.png)" ));
  122. finalImage.save(fname, "PNG");
  123. }
  124. }
  125. void MainWindow::on_actLoad_Images_triggered(){
  126. on_btnLoadImages_clicked();
  127. }
  128. void MainWindow::on_actRemoveNoise_triggered(){
  129. on_btnRemoveNoise_clicked();
  130. }
  131. void MainWindow::on_actSaveImage_triggered(){
  132. on_btnSaveImage_clicked();
  133. }