暂无描述

mainwindow.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // RAN 2015-07-03 - Initialized invertColor variable. Eliminated some unneeeded arguments from
  2. // the filter functions. Added the convertToFormat(QImage::Format_ARGB32)
  3. // so that filters work even on images that have a predefined color table.
  4. // Change the name of the variable color to changeColor.
  5. #include "mainwindow.h"
  6. #include "ui_mainwindow.h"
  7. /// \fn MainWindow::MainWindow(QWidget *parent)
  8. /// \~English
  9. /// \brief Constructor
  10. /// \~Spanish
  11. /// \brief Constructor
  12. MainWindow::MainWindow(QWidget *parent) :
  13. QMainWindow(parent),
  14. ui(new Ui::MainWindow)
  15. {
  16. ui->setupUi(this);
  17. ui->thresholdSlider->setDisabled(true);
  18. invertColor = false;
  19. ///
  20. /// KEYBOARD SHORTCUTS
  21. /// Loads a image with cmd + o
  22. /// Carga una imagen con cmd + o
  23. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_O), this, SLOT(on_btnLoadImage_clicked()));
  24. ///
  25. /// Saves a image with cmd + s
  26. /// Guarda una imagen con cmd + s
  27. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_S), this, SLOT(on_btnSaveImage_clicked()));
  28. ///
  29. /// Closes window with cmd + w
  30. /// Cierra la ventana con cmd + w
  31. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this, SLOT(close()));
  32. ///
  33. /// Inverts threshold color with cmd + t
  34. /// Invierte el color umbral con cmd + t
  35. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_T), this, SLOT(on_btnInvertThreshold_clicked()));
  36. ///
  37. /// Flips horizontally a image with cmd + f
  38. /// Rota horizontalmente una imagen con cmd + f
  39. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_F), this, SLOT(on_btnFlipImageHorizontally_clicked()));
  40. ///
  41. /// Flips vertically a image with cmd + v
  42. /// Rota verticalmente una imagen con cmd + v
  43. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_V), this, SLOT(on_btnFlipImageVertically_clicked()));
  44. ///
  45. /// Applies greyscale filter to a image with cmd + g
  46. /// Aplica el filtro de tonos grises a una imagen con cmd + g
  47. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_G), this, SLOT(on_btnGreyScaleFilter_clicked()));
  48. ///
  49. /// Resets an edited image to the orignal one to a image with cmd + r
  50. /// Devuelve la imagen editada a su estado original con cmd + r
  51. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_R), this, SLOT(on_btnRevertImage_clicked()));
  52. ///
  53. /// Display the instructions with cmd + i
  54. /// Despliega las instrucciones con cmd + i
  55. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_I), this, SLOT(instructions()));
  56. }
  57. /// \fn MainWindow::MainWindow(QWidget *parent)
  58. /// \~English
  59. /// \brief Destructor
  60. /// \~Spanish
  61. /// \brief Destructor
  62. MainWindow::~MainWindow(){
  63. delete ui;
  64. }
  65. ///
  66. /// ACTION FUNCTIONS
  67. ///
  68. /// \fn void MainWindow::on_actionLoad_Image_triggered()
  69. /// \~English
  70. /// \brief Invokes function on_btnLoadImage_clicked()
  71. /// \~Spanish
  72. /// \brief Invoca la funcion on_btnLoadImage_clicked()
  73. void MainWindow::on_actionLoad_Image_triggered(){
  74. on_btnLoadImage_clicked();
  75. }
  76. /// \fn void MainWindow::on_actionSave_Image_triggered()
  77. /// \~English
  78. /// \brief Invokes function on_btnSaveImage_clicked()
  79. /// \~Spanish
  80. /// \brief Invoca la funcion on_btnSaveImage_clicked()
  81. void MainWindow::on_actionSave_Image_triggered(){
  82. on_btnSaveImage_clicked();
  83. }
  84. /// \fn void MainWindow::on_actionClose_Image_triggered()
  85. /// \~English
  86. /// \brief Invokes function close()
  87. /// \~Spanish
  88. /// \brief Invoca la funcion close()
  89. void MainWindow::on_actionClose_Window_triggered(){
  90. close();
  91. }
  92. /// \fn void MainWindow::on_actionInvert_Threshold_Colors_triggered()
  93. /// \~English
  94. /// \brief Invokes function on_btnInvertThreshold_clicked()
  95. /// \~Spanish
  96. /// \brief Invoca la funcion on_btnInvertThreshold_clicked()
  97. void MainWindow::on_actionInvert_Threshold_Colors_triggered(){
  98. on_btnInvertThreshold_clicked();
  99. }
  100. /// \fn void MainWindow::on_actionFlip_Image_Horizontally_triggered()
  101. /// \~English
  102. /// \brief Invokes function on_btnFlipImageHorizontally_clicked()
  103. /// \~Spanish
  104. /// \brief Invoca la funcion on_btnFlipImageHorizontally_clicked()
  105. void MainWindow::on_actionFlip_Image_Horizontally_triggered(){
  106. on_btnFlipImageHorizontally_clicked();
  107. }
  108. /// \fn void MainWindow::on_actionFlip_Image_Vertically_triggered()
  109. /// \~English
  110. /// \brief Invokes function on_btnFlipImageVertically_clicked()
  111. /// \~Spanish
  112. /// \brief Invoca la funcion on_btnFlipImageVertically_clicked()
  113. void MainWindow::on_actionFlip_Image_Vertically_triggered(){
  114. on_btnFlipImageVertically_clicked();
  115. }
  116. /// \fn void MainWindow::on_actionApply_Grey_Scale_Filter_triggered()
  117. /// \~English
  118. /// \brief Invokes function on_btnGreyScaleFilter_clicked()
  119. /// \~Spanish
  120. /// \brief Invoca la funcion on_btnGreyScaleFilter_clicked()
  121. void MainWindow::on_actionApply_Grey_Scale_Filter_triggered(){
  122. on_btnGreyScaleFilter_clicked();
  123. }
  124. /// \fn void MainWindow::on_actionRevert_Edited_Image_to_Original_triggered()
  125. /// \~English
  126. /// \brief Invokes function on_btnRevertImage_clicked()
  127. /// \~Spanish
  128. /// \brief Invoca la funcion on_btnRevertImage_clicked()
  129. void MainWindow::on_actionRevert_Edited_Image_to_Original_triggered(){
  130. on_btnRevertImage_clicked();
  131. }
  132. /// \fn void MainWindow::on_actionInstructions_triggered()
  133. /// \~English
  134. /// \brief To display the instructions.
  135. /// \~Spanish
  136. /// \brief Para desplegar las instrucciones.
  137. void MainWindow::on_actionInstructions_triggered(){
  138. instructions();
  139. }
  140. /// \fn void MainWindow::on_chboxThreshold_clicked()
  141. /// \~English
  142. /// \brief To apply the threshold filter and load the edited image to the GUI.
  143. /// \~Spanish
  144. /// \brief Para aplicar el filtro de umbral y cargar la imagen editada al GUI.
  145. void MainWindow::on_chboxThreshold_clicked(){
  146. if (ui->chboxThreshold->isChecked()){
  147. ui->thresholdSlider->setEnabled(true);
  148. applyThresholdFilter();
  149. ui->lblEditedImage->setPixmap(QPixmap::fromImage(editedImage));
  150. }
  151. else
  152. ui->thresholdSlider->setDisabled(true);
  153. }
  154. /// \fn void MainWindow::applyThresholdFilter()
  155. /// \~English
  156. /// \brief To apply the threshold filter.
  157. /// \~Spanish
  158. /// \brief Para aplicar el filtro de umbral.
  159. void MainWindow::applyThresholdFilter(){
  160. ThresholdFilter(originalImage, editedImage, ui->thresholdSlider->value(), invertColor) ;
  161. }
  162. /// \fn void MainWindow::on_thresholdSlider_sliderReleased()
  163. /// \~English
  164. /// \brief To apply the threshold filter and load the edited image to the GUI when
  165. /// the GUI slider is moved.
  166. /// \~Spanish
  167. /// \brief Para aplicar el filtro de umbral y cargar la imagen editada al GUI cuando
  168. /// el deslizador del GUI es movido.
  169. void MainWindow::on_thresholdSlider_sliderReleased(){
  170. applyThresholdFilter();
  171. ui->lblEditedImage->setPixmap(QPixmap::fromImage(editedImage));
  172. }
  173. /// \fn void MainWindow::on_btnInvertThreshold_clicked()
  174. /// \~English
  175. /// \brief To apply the threshold filter and load the edited image to the GUI when
  176. /// the threshold color is inverted.
  177. /// \~Spanish
  178. /// \brief Para aplicar el filtro de umbral y cargar la imagen editada al GUI cuando
  179. /// el color del umbral es invertido.
  180. void MainWindow::on_btnInvertThreshold_clicked(){
  181. invertColor = !invertColor;
  182. if(ui->chboxThreshold->isChecked()){
  183. applyThresholdFilter();
  184. ui->lblEditedImage->setPixmap(QPixmap::fromImage(editedImage));
  185. }
  186. }
  187. /// \fn void MainWindow::on_btnLoadImage_clicked()
  188. /// \~English
  189. /// \brief To load an image to the GUI when the load image option is selected.
  190. /// \~Spanish
  191. /// \brief Para cargar una imagen al GUI cuando se marca opcion de cargar imagen.
  192. void MainWindow::on_btnLoadImage_clicked(){
  193. QString fname = QFileDialog::getOpenFileName(this, tr("Choose an image"), QDir::homePath());
  194. if (!fname.isEmpty()){
  195. QImage image(fname);
  196. if (image.isNull())
  197. QMessageBox::information(this, tr("Choose an image"),tr("Cannot load %1.").arg(fname));
  198. originalImage = image;
  199. // This ensures that the resulting image accepts all the pixel values that
  200. // will be accessed using pixel.
  201. editedImage = image.convertToFormat(QImage::Format_ARGB32);
  202. }
  203. ui->lblOriginalImage->setPixmap(QPixmap::fromImage(originalImage));
  204. ui->lblEditedImage->setPixmap(QPixmap::fromImage(editedImage));
  205. }
  206. /// \fn void MainWindow::on_btnSaveImage_clicked()
  207. /// \~English
  208. /// \brief To save an image to the file system when the save image option is selected.
  209. /// \~Spanish
  210. /// \brief Para guardar una imagen al sistema de archivos cuando se marca opcion de guardar imagen.
  211. void MainWindow::on_btnSaveImage_clicked()
  212. {
  213. QPixmap out = QPixmap::grabWidget(this,361,10,481,481);
  214. QString fname = QFileDialog::getSaveFileName(this, tr("Save Edited Image"), (""), tr("PNG (*.png)" ));
  215. editedImage.save(fname, "PNG");
  216. }
  217. /// \fn void MainWindow::on_btnFlipImageHorizontally_clicked()
  218. /// \~English
  219. /// \brief To call the horizontal flip image filter.
  220. /// \~Spanish
  221. /// \brief Para llamar al filtro de imagen flip horizontal.
  222. void MainWindow::on_btnFlipImageHorizontally_clicked(){
  223. HorizontalFlip(editedImage) ;
  224. ui->lblEditedImage->setPixmap(QPixmap::fromImage(editedImage));
  225. }
  226. /// \fn void MainWindow::on_btnFlipImageVertically_clicked()
  227. /// \~English
  228. /// \brief To call the vertical flip image filter.
  229. /// \~Spanish
  230. /// \brief Para llamar al filtro de imagen flip vertical.
  231. void MainWindow::on_btnFlipImageVertically_clicked(){
  232. VerticalFlip(editedImage) ;
  233. ui->lblEditedImage->setPixmap(QPixmap::fromImage(editedImage));
  234. }
  235. /// \fn void MainWindow::on_btnGreyScaleFilter_clicked()
  236. /// \~English
  237. /// \brief To call the Gray Scale image filter.
  238. /// \~Spanish
  239. /// \brief Para llamar al filtro de imagen de escalas grises.
  240. void MainWindow::on_btnGreyScaleFilter_clicked(){
  241. GrayScale(editedImage) ;
  242. ui->lblEditedImage->setPixmap(QPixmap::fromImage(editedImage));
  243. }
  244. /// \fn void MainWindow::on_btnRevertImage_clicked()()
  245. /// \~English
  246. /// \brief To reset the edited image to the original image.
  247. /// \~Spanish
  248. /// \brief Para reajustar la imagen editada a la imagen original.
  249. void MainWindow::on_btnRevertImage_clicked(){
  250. // This ensures that the resulting image accepts all the pixel values that
  251. // will be accessed using pixel.
  252. editedImage = originalImage.convertToFormat(QImage::Format_ARGB32);
  253. ui->lblEditedImage->setPixmap(QPixmap::fromImage(editedImage));
  254. }
  255. /// \fn void MainWindow::instructions()
  256. /// \~English
  257. /// \brief To display the instructions.
  258. /// \~Spanish
  259. /// \brief Para desplegar las instrucciones.
  260. void MainWindow::instructions(){
  261. QString h[8];
  262. h[0]="1. Load an image by pressing the \"Load Image\" button, this can also be done from the edit menu or using a keyboard shortcut.";
  263. h[1]="\n\n2. To apply a greyscale filter to the image click the \"Greyscale Filter\" button, this can also be done from the edit menu or using a keyboard shortcut.";
  264. h[2]="\n\n3. To apply a threshold filter to the image click the \"Threshold\" check box. If you wish to invert the black and white colors you can press the \"Invert Threshold Colors\" button, this can also be done from the edit menu or using a keyboard shortcut.";
  265. h[3]=" You can change the value of the threshold with the slider below the check box.";
  266. h[4]="\n\n4. To flip the image horizontally click the \"Flip Image Horizontally\" button, this can also be done from the edit menu or using a keyboard shortcut.";
  267. h[5]="\n\n5. To flip the image vertically click the \"Flip Image Horizontally\" button, this can also be done from the edit menu or using a keyboard shortcut.";
  268. h[6]="\n\n6. If you feel like you did not create a masterpiece, you can revert the image back to original by clicking the button \"Revert Image to Original\", this can also be done from the edit menu or using a keyboard shortcut.";
  269. h[7]="\n\n7. When you are satisfied with the results you can save the image as a PNG by clicking the button \" Save edited image\", this can also be done from the edit menu or using a keyboard shortcut.";
  270. QMessageBox mbox;
  271. mbox.setWindowTitle("How to use the Simple Image Editor");
  272. mbox.setText(h[0]+h[1]+h[2]+h[3]+h[4]+h[5]+h[6]+h[7]);
  273. mbox.exec();
  274. }