Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QFileDialog>
- #include <QInputDialog>
- #include <QMouseEvent>
- #include <stdio.h>
- #include <iostream>
- #include <QPainter>
- #include <QRect>
- #include <QPoint>
- #include <QRubberBand>
- #include <QStack>
- #include <QScrollArea>
- using namespace std;
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- ui->label->setText("");
- ui->label = new QLabel;
- ui->label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
- ui->label->setScaledContents(true);
- ui->scrollArea = new QScrollArea;
- ui->scrollArea->setWidget(ui->label);
- setCentralWidget(ui->scrollArea);
- }
- int angle= 0;
- double scaleFactor= 1;
- QString original;
- QPoint startDraw, endDraw;
- QRubberBand *rubberBand;
- bool isCropping= false, isZooming= false;
- QStack <QPixmap> undo;
- QStack <QPixmap> redo;
- QStack <int> undoAngle;
- QStack <int> redoAngle;
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- void MainWindow::on_actionOpen_triggered()
- {
- // Opens browse Dialog to take the picture path from the user.
- original= QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("PNG (*.png) ;; JPG (*.jpg) ;; BMP (*.bmp)"));
- ui->label->setPixmap(open(original));
- ui->label->adjustSize();
- angle= 0;
- scaleFactor= 1;
- }
- void MainWindow::on_actionSave_triggered()
- {
- // Opens a saving browser for the user to choose where he wants to save the file and its name.
- QString fileName = QFileDialog::getSaveFileName(this, tr("Save Picture"), "", tr("Files (*.*)"));
- QPixmap picture = *ui->label->pixmap();
- // Checks if the user added an extension to the picture, if he didn't, the default saving is .jpg.
- if(!(fileName.contains(".png") || fileName.contains(".jpg") || fileName.contains(".bmp"))) fileName.append(".jpg");
- picture.save(fileName);
- }
- void MainWindow::on_actionRotate_triggered()
- {
- undoAction("rotate");
- angle+= QInputDialog::getInt(this, "Enter Angle", "Rotation angle: ");
- QPixmap pixmap(open(original));
- ui->label->setPixmap(rotate(angle, pixmap));
- ui->label->adjustSize();
- redoAction();
- }
- void MainWindow::on_actionCrop_triggered()
- {
- isCropping= true;
- }
- void MainWindow::mousePressEvent(QMouseEvent *e)
- {
- cout << "CurrentMouseWindow= " << e->pos().x() << " " << e->pos().y() << endl;
- QPoint pt= windowToLabel(e->pos());
- cout << "CurrentMouseLabel= " << pt.x() << " " << pt.y() << endl;
- if((isCropping || isZooming) && ui->label->underMouse()){
- // The top left point of the rubberband we use for selection.
- startDraw.setX(e->pos().x());
- startDraw.setY(e->pos().y());
- rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
- rubberBand->show();
- }
- }
- void MainWindow::mouseMoveEvent(QMouseEvent *e)
- {
- if((isCropping || isZooming) && ui->label->underMouse()){
- rubberBand->setGeometry(QRect(startDraw, e->pos()));
- }
- }
- void MainWindow::mouseReleaseEvent(QMouseEvent *e)
- {
- if(ui->label->underMouse()){
- if(isCropping){
- undoAction("crop");
- // To get the current mouse point and the left courner of the rubberband point
- // relative to the label to be able to crop.
- QPoint a= windowToLabel(startDraw);
- QPoint b= windowToLabel(e->pos());
- ui->label->setPixmap(crop(QRect(a, b), *ui->label->pixmap()));
- ui->label->adjustSize();
- isCropping= false;
- rubberBand->hide();
- redoAction();
- }
- if(isZooming){
- ui->label->setPixmap(open(original));
- ui->label->adjustSize();
- QPixmap pixmap(open(original));
- pixmap = zoom(scaleFactor, pixmap);
- QPoint mouse, newStart;
- mouse= windowToLabel(e->pos());
- newStart= windowToLabel(startDraw);
- newStart.setX(newStart.x()*scaleFactor);
- newStart.setY(newStart.y()*scaleFactor);
- mouse.setX(mouse.x()*scaleFactor);
- mouse.setY(mouse.y()*scaleFactor);
- ui->label->setPixmap(pixmap);
- ui->label->adjustSize();
- QRect rect(newStart, mouse);
- ui->scrollArea->ensureVisible(rect.center().x(), rect.center().y(), 200, 200);
- isZooming= false;
- rubberBand->hide();
- }
- }
- }
- void MainWindow::on_actionZoom_In_triggered()
- {
- scaleFactor*= 2;
- isZooming= true;
- }
- void MainWindow::on_actionZoom_Out_triggered()
- {
- scaleFactor*= .5;
- isZooming= true;
- }
- void MainWindow::on_actionUndo_triggered()
- {
- if(!undo.empty()){
- redo.push(*ui->label->pixmap());
- redoAngle.push(angle);
- QPixmap pix(undo.pop());
- angle= undoAngle.pop();
- ui->label->setPixmap(pix);
- ui->label->adjustSize();
- }
- }
- void MainWindow::on_actionRedo_triggered()
- {
- if(!redo.empty()){
- undo.push(*ui->label->pixmap());
- undoAngle.push(angle);
- QPixmap pix(redo.pop());
- angle= redoAngle.pop();
- ui->label->setPixmap(pix);
- ui->label->adjustSize();
- }
- }
- /* The redo procesdure.
- */
- void MainWindow::redoAction(){
- redo.clear();
- redoAngle.clear();
- }
- /* The undo procesdure.
- *
- * parameters: (QString) str: The action name.
- */
- void MainWindow::undoAction(QString str){
- undo.push(*ui->label->pixmap());
- undoAngle.push(angle);
- }
- /* Opens a picture using its path.
- *
- * parameters: (QString) fileName: Path of the picture to be edited.
- * return: (QPixmap) The Pixmap of the picture.
- */
- QPixmap MainWindow::open(QString fileName){
- QPixmap picture;
- picture.load(fileName);
- return picture;
- }
- /* Rotates the current pixmap in the qlabel, by the absolute angle of rotation.
- * Absolute angle of rotation is the angle of the rotation from the starting postion.
- *
- * parameters: (int) rotationAngle: The absolute angle of rotation.
- * (QPixmap) pix: The pixmap we are actually rotating.
- * return: (QPixmap) The rotated pixmap.
- *
- * Notes: We rotate the original picture to avoid image pixelization.
- */
- QPixmap MainWindow::rotate(int rotationAngle, QPixmap pix){
- QMatrix rm;
- rm.rotate(rotationAngle);
- rm.scale(scaleFactor, scaleFactor);
- return pix.transformed(rm);
- }
- /* Crops a given pixmap into a given rectangle.
- *
- * parameters: (QRect) rect: The dimensions we want to crop from the pixmap.
- * (QPixmap) pix: The pixmap we are actually cropping.
- * return: (QPixmap) Cropped pixmap.
- */
- QPixmap MainWindow::crop(QRect rect, QPixmap pix){
- return pix.copy(rect);
- }
- /* Takes a point relative to the main window frame and converts it to a point relative to
- * Image Label frame.
- *
- * parameters: (QPoint) a: The point in the main window frame.
- * return: (QPoint) The point in the label frame.
- */
- QPoint MainWindow::windowToLabel(QPoint a){
- a = mapToGlobal(a);
- return ui->label->mapFromGlobal(a);
- }
- /* Zooms into a given pixmap with a given scalerFactor.
- *
- * parameters: (int) zoomFactor: The value we want to zoom with.
- * (QPixmap) pix: The pixmap we are actually zooming.
- * return: (QPixmap) Zoomed pixmap.
- */
- QPixmap MainWindow::zoom(int zoomFactor, QPixmap pix){
- QMatrix rm;
- rm.rotate(angle);
- rm.scale(zoomFactor, zoomFactor);
- return pix.transformed(rm);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement