Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QFileDialog>
  4. #include <QInputDialog>
  5. #include <QMouseEvent>
  6. #include <stdio.h>
  7. #include <iostream>
  8. #include <QPainter>
  9. #include <QRect>
  10. #include <QPoint>
  11. #include <QRubberBand>
  12. #include <QStack>
  13. #include <QScrollArea>
  14.  
  15. using namespace std;
  16.  
  17. MainWindow::MainWindow(QWidget *parent) :
  18. QMainWindow(parent),
  19. ui(new Ui::MainWindow)
  20. {
  21. ui->setupUi(this);
  22. ui->label->setText("");
  23. ui->label = new QLabel;
  24. ui->label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  25. ui->label->setScaledContents(true);
  26.  
  27. ui->scrollArea = new QScrollArea;
  28. ui->scrollArea->setWidget(ui->label);
  29. setCentralWidget(ui->scrollArea);
  30.  
  31. }
  32.  
  33. int angle= 0;
  34. double scaleFactor= 1;
  35. QString original;
  36. QPoint startDraw, endDraw;
  37. QRubberBand *rubberBand;
  38. bool isCropping= false, isZooming= false;
  39.  
  40. QStack <QPixmap> undo;
  41. QStack <QPixmap> redo;
  42.  
  43. QStack <int> undoAngle;
  44. QStack <int> redoAngle;
  45.  
  46. MainWindow::~MainWindow()
  47. {
  48. delete ui;
  49. }
  50.  
  51. void MainWindow::on_actionOpen_triggered()
  52. {
  53. // Opens browse Dialog to take the picture path from the user.
  54. original= QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("PNG (*.png) ;; JPG (*.jpg) ;; BMP (*.bmp)"));
  55. ui->label->setPixmap(open(original));
  56. ui->label->adjustSize();
  57. angle= 0;
  58. scaleFactor= 1;
  59. }
  60.  
  61. void MainWindow::on_actionSave_triggered()
  62. {
  63. // Opens a saving browser for the user to choose where he wants to save the file and its name.
  64. QString fileName = QFileDialog::getSaveFileName(this, tr("Save Picture"), "", tr("Files (*.*)"));
  65. QPixmap picture = *ui->label->pixmap();
  66.  
  67. // Checks if the user added an extension to the picture, if he didn't, the default saving is .jpg.
  68. if(!(fileName.contains(".png") || fileName.contains(".jpg") || fileName.contains(".bmp"))) fileName.append(".jpg");
  69. picture.save(fileName);
  70. }
  71.  
  72.  
  73.  
  74. void MainWindow::on_actionRotate_triggered()
  75. {
  76. undoAction("rotate");
  77. angle+= QInputDialog::getInt(this, "Enter Angle", "Rotation angle: ");
  78. QPixmap pixmap(open(original));
  79. ui->label->setPixmap(rotate(angle, pixmap));
  80. ui->label->adjustSize();
  81. redoAction();
  82. }
  83.  
  84. void MainWindow::on_actionCrop_triggered()
  85. {
  86. isCropping= true;
  87. }
  88.  
  89. void MainWindow::mousePressEvent(QMouseEvent *e)
  90. {
  91. cout << "CurrentMouseWindow= " << e->pos().x() << " " << e->pos().y() << endl;
  92. QPoint pt= windowToLabel(e->pos());
  93. cout << "CurrentMouseLabel= " << pt.x() << " " << pt.y() << endl;
  94. if((isCropping || isZooming) && ui->label->underMouse()){
  95.  
  96. // The top left point of the rubberband we use for selection.
  97. startDraw.setX(e->pos().x());
  98. startDraw.setY(e->pos().y());
  99.  
  100. rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
  101. rubberBand->show();
  102. }
  103. }
  104.  
  105. void MainWindow::mouseMoveEvent(QMouseEvent *e)
  106. {
  107. if((isCropping || isZooming) && ui->label->underMouse()){
  108. rubberBand->setGeometry(QRect(startDraw, e->pos()));
  109. }
  110. }
  111.  
  112. void MainWindow::mouseReleaseEvent(QMouseEvent *e)
  113. {
  114. if(ui->label->underMouse()){
  115. if(isCropping){
  116. undoAction("crop");
  117.  
  118. // To get the current mouse point and the left courner of the rubberband point
  119. // relative to the label to be able to crop.
  120. QPoint a= windowToLabel(startDraw);
  121. QPoint b= windowToLabel(e->pos());
  122.  
  123. ui->label->setPixmap(crop(QRect(a, b), *ui->label->pixmap()));
  124. ui->label->adjustSize();
  125.  
  126. isCropping= false;
  127. rubberBand->hide();
  128. redoAction();
  129. }
  130. if(isZooming){
  131. ui->label->setPixmap(open(original));
  132. ui->label->adjustSize();
  133. QPixmap pixmap(open(original));
  134. pixmap = zoom(scaleFactor, pixmap);
  135. QPoint mouse, newStart;
  136. mouse= windowToLabel(e->pos());
  137. newStart= windowToLabel(startDraw);
  138. newStart.setX(newStart.x()*scaleFactor);
  139. newStart.setY(newStart.y()*scaleFactor);
  140. mouse.setX(mouse.x()*scaleFactor);
  141. mouse.setY(mouse.y()*scaleFactor);
  142.  
  143. ui->label->setPixmap(pixmap);
  144. ui->label->adjustSize();
  145. QRect rect(newStart, mouse);
  146. ui->scrollArea->ensureVisible(rect.center().x(), rect.center().y(), 200, 200);
  147. isZooming= false;
  148. rubberBand->hide();
  149. }
  150. }
  151. }
  152.  
  153. void MainWindow::on_actionZoom_In_triggered()
  154. {
  155. scaleFactor*= 2;
  156. isZooming= true;
  157. }
  158.  
  159. void MainWindow::on_actionZoom_Out_triggered()
  160. {
  161. scaleFactor*= .5;
  162. isZooming= true;
  163. }
  164.  
  165. void MainWindow::on_actionUndo_triggered()
  166. {
  167. if(!undo.empty()){
  168. redo.push(*ui->label->pixmap());
  169. redoAngle.push(angle);
  170. QPixmap pix(undo.pop());
  171. angle= undoAngle.pop();
  172. ui->label->setPixmap(pix);
  173. ui->label->adjustSize();
  174. }
  175. }
  176.  
  177. void MainWindow::on_actionRedo_triggered()
  178. {
  179. if(!redo.empty()){
  180. undo.push(*ui->label->pixmap());
  181. undoAngle.push(angle);
  182. QPixmap pix(redo.pop());
  183. angle= redoAngle.pop();
  184. ui->label->setPixmap(pix);
  185. ui->label->adjustSize();
  186. }
  187. }
  188.  
  189.  
  190. /* The redo procesdure.
  191. */
  192. void MainWindow::redoAction(){
  193. redo.clear();
  194. redoAngle.clear();
  195. }
  196.  
  197. /* The undo procesdure.
  198. *
  199. * parameters: (QString) str: The action name.
  200. */
  201. void MainWindow::undoAction(QString str){
  202. undo.push(*ui->label->pixmap());
  203. undoAngle.push(angle);
  204. }
  205.  
  206. /* Opens a picture using its path.
  207. *
  208. * parameters: (QString) fileName: Path of the picture to be edited.
  209. * return: (QPixmap) The Pixmap of the picture.
  210. */
  211. QPixmap MainWindow::open(QString fileName){
  212. QPixmap picture;
  213. picture.load(fileName);
  214. return picture;
  215. }
  216.  
  217.  
  218. /* Rotates the current pixmap in the qlabel, by the absolute angle of rotation.
  219. * Absolute angle of rotation is the angle of the rotation from the starting postion.
  220. *
  221. * parameters: (int) rotationAngle: The absolute angle of rotation.
  222. * (QPixmap) pix: The pixmap we are actually rotating.
  223. * return: (QPixmap) The rotated pixmap.
  224. *
  225. * Notes: We rotate the original picture to avoid image pixelization.
  226. */
  227. QPixmap MainWindow::rotate(int rotationAngle, QPixmap pix){
  228. QMatrix rm;
  229. rm.rotate(rotationAngle);
  230. rm.scale(scaleFactor, scaleFactor);
  231. return pix.transformed(rm);
  232. }
  233.  
  234.  
  235. /* Crops a given pixmap into a given rectangle.
  236. *
  237. * parameters: (QRect) rect: The dimensions we want to crop from the pixmap.
  238. * (QPixmap) pix: The pixmap we are actually cropping.
  239. * return: (QPixmap) Cropped pixmap.
  240. */
  241. QPixmap MainWindow::crop(QRect rect, QPixmap pix){
  242. return pix.copy(rect);
  243. }
  244.  
  245.  
  246. /* Takes a point relative to the main window frame and converts it to a point relative to
  247. * Image Label frame.
  248. *
  249. * parameters: (QPoint) a: The point in the main window frame.
  250. * return: (QPoint) The point in the label frame.
  251. */
  252. QPoint MainWindow::windowToLabel(QPoint a){
  253. a = mapToGlobal(a);
  254. return ui->label->mapFromGlobal(a);
  255. }
  256.  
  257.  
  258. /* Zooms into a given pixmap with a given scalerFactor.
  259. *
  260. * parameters: (int) zoomFactor: The value we want to zoom with.
  261. * (QPixmap) pix: The pixmap we are actually zooming.
  262. * return: (QPixmap) Zoomed pixmap.
  263. */
  264. QPixmap MainWindow::zoom(int zoomFactor, QPixmap pix){
  265. QMatrix rm;
  266. rm.rotate(angle);
  267. rm.scale(zoomFactor, zoomFactor);
  268. return pix.transformed(rm);
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement