Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QFileDialog>
- #include <QPixmap>
- #include <QBitmap>
- #include <QDesktopWidget>
- #include <QTransform>
- #include <QInputDialog>
- #include <QPainter>
- #include <QMouseEvent>
- #include <QDebug>
- #include <QPointF>
- #include <QSize>
- #include <cmath>
- #include <vector>
- #include <math.h>
- #include <stack>
- #include <QPainter>
- #include <QPen>
- #include <QPaintEvent>
- #include <QGraphicsScene>
- #include <QGraphicsView>
- #include <QAbstractSlider>
- #include <QScrollBar>
- #define PI 3.14159265359
- using namespace std;
- QPixmap *tempGlobalImage;
- QPixmap *globalImage;
- QPixmap *beforeSelect;
- QString fileName;
- int cnt = 7;
- bool select=false;
- bool alreadySelected=false;
- bool zoomOutAfterSel=false;
- bool zoomedIn=false;
- double factor=1;
- struct param {
- int cnt ;
- bool select;
- bool alreadySelected;
- bool zoomOutAfterSel;
- bool zoomedIn;
- double factor;
- void set(int cnt, bool select, bool alreadySelected,bool zoomOutAfterSel,bool zoomedIn,double factor) {
- this->cnt=cnt;
- this->select=select;
- this->alreadySelected=alreadySelected;
- this->zoomOutAfterSel=zoomOutAfterSel;
- this->zoomedIn=zoomedIn;
- this->factor=factor;
- }
- };
- stack<pair < QPixmap*,param*> > stack1,stack2;
- int pnt;
- void myPush() {
- while ( ! stack2.empty())
- stack2.pop();
- pair < QPixmap*,param*> *t= new pair < QPixmap*,param*> ();
- t->first=(new QPixmap(*globalImage));
- param * p= new param();
- p->set( cnt, select, alreadySelected, zoomOutAfterSel,zoomedIn, factor);
- t->second=p;
- stack1.push(*t);
- qDebug()<<"IN MY PUSH"<<stack1.size()<<" "<<stack2.size()<<endl;
- }
- //rotate around center of current image
- pair<int, int> rotate(double x, double y, double theta) {
- QSize qz = globalImage->size();
- double w = qz.width(), h = qz.height();
- x -= (w / 2.0), y -= (h / 2.0);
- theta = theta * (PI / 180.0);
- double x1 = (x * cos(theta)) - (y * sin(theta));
- double y1 = (x * sin(theta)) + (y * cos(theta));
- return make_pair(x1 + (w / 2.0), y1 + (h / 2.0));
- }
- pair<QSize, pair<int, int> > getNewSize(double theta) {
- QSize qz = globalImage->size();
- double w = qz.width(), h = qz.height();
- vector<pair<int, int> > v;
- v.push_back(rotate(0, 0, theta));
- v.push_back(rotate(w, 0, theta));
- v.push_back(rotate(0, h, theta));
- v.push_back(rotate(w, h, theta));
- int newHeight = INT_MIN;
- int newWidth = INT_MIN;
- int dx = INT_MAX;
- int dy = INT_MAX;
- for (int i = 0; i < 4; i++) {
- dx = min(dx, v[i].first);
- dy = min(dy, v[i].second);
- for (int j = i + 1; j < 4; j++) {
- newHeight = max(newHeight, abs(v[i].second - v[j].second));
- newWidth = max(newWidth, abs(v[i].first - v[j].first));
- }
- }
- pair<QSize, pair<int, int> > ret;
- ret.first = QSize(newWidth, newHeight);
- ret.second.first = dx*-1;
- ret.second.second = dy*-1;
- return ret;
- }
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent), ui(new Ui::MainWindow) {
- globalImage = new QPixmap();
- tempGlobalImage=new QPixmap();
- beforeSelect=new QPixmap();
- //tempGlobalImage=new QPixmap();
- ui->setupUi(this);
- }
- MainWindow::~MainWindow() {
- delete ui;
- }
- void MainWindow::on_pushButton_clicked() {//righth rotate
- bool ok;
- if(select || alreadySelected){
- select=false;
- alreadySelected=false;
- removeSelectRect();
- }
- int deg = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
- tr("Degree:"), 180, 0, 360, 1, &ok);
- if (ok) {
- myPush();
- pair<QSize, pair<int, int> > temp = getNewSize(deg);
- QPixmap rotate(temp.first);
- QPainter p(&rotate);
- rotate.fill(Qt::white);
- p.setBackgroundMode(Qt::TransparentMode);
- p.setRenderHint(QPainter::Antialiasing);
- p.setRenderHint(QPainter::SmoothPixmapTransform);
- p.setRenderHint(QPainter::HighQualityAntialiasing);
- p.translate(rotate.size().width() / 2, rotate.size().height() / 2);
- p.rotate(deg);
- p.translate(-rotate.size().width() / 2, -rotate.size().height() / 2);
- qDebug()<<temp.second.first<<" "<< temp.second.second<<endl;
- p.drawPixmap(temp.second.first, temp.second.second, *globalImage);
- p.end();
- *globalImage=rotate;
- ui->edit_label->setPixmap(rotate);
- }
- }
- void MainWindow::on_pushButton_2_clicked() {
- QLabel* edit_label = (ui->edit_label);
- fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),
- "C:/Users/moustafa/Desktop", tr("Image Files (*.png *.jpg *.bmp)"));
- globalImage->load(fileName);
- tempGlobalImage->load(fileName);
- edit_label->setPixmap(*globalImage);
- edit_label->setMask((globalImage)->mask());
- edit_label->show();
- if(fileName!=NULL)
- myPush();
- }
- void MainWindow::on_pushButton_3_clicked() {//left rotate
- if(select || alreadySelected){
- select=false;
- alreadySelected=false;
- removeSelectRect();
- }
- bool ok;
- int deg = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
- tr("Degree:"), 180, 0, 360, 1, &ok);
- if (ok) {
- myPush();
- pair<QSize, pair<int, int> > temp = getNewSize(deg*-1);
- QPixmap rotate(temp.first);
- QPainter p(&rotate);
- rotate.fill(Qt::white);
- p.setBackgroundMode(Qt::TransparentMode);
- p.setRenderHint(QPainter::Antialiasing);
- p.setRenderHint(QPainter::SmoothPixmapTransform);
- p.setRenderHint(QPainter::HighQualityAntialiasing);
- p.translate(rotate.size().width() / 2, rotate.size().height() / 2);
- p.rotate(-1*deg);
- p.translate(-rotate.size().width() / 2, -rotate.size().height() / 2);
- qDebug()<<temp.second.first<<" "<< temp.second.second<<endl;
- p.drawPixmap(temp.second.first, temp.second.second, *globalImage);
- p.end();
- // globalImage->copy(rotate.rect());
- *globalImage=rotate;
- ui->edit_label->setPixmap(rotate);
- }
- }
- void MainWindow::on_pushButton_4_clicked() {
- if (cnt < 1) {
- return;
- }
- cnt--;
- QLabel* edit_label = (ui->edit_label);
- int w = edit_label->width();
- int h = edit_label->height();
- if(select && alreadySelected){
- zoomedIn=true;
- int x = min(x1, x2);
- int y = min(y1, y2);
- zoomOutAfterSel=true;
- QRect rect(x, y, abs(x2 - x1), abs(y2 - y1));
- QPixmap cropped = globalImage->copy(rect);
- //globalImage=&cropped;
- qDebug()<<(globalImage->height()/cropped.height());
- ui->edit_label->setPixmap(cropped.scaled(w,h,Qt::KeepAspectRatio));
- globalImage->scaled(w * (globalImage->width()/cropped.width()), h * (globalImage->height()/cropped.height()), Qt::KeepAspectRatio);
- select=false;
- alreadySelected=false;
- return;
- }
- if(zoomOutAfterSel){
- removeSelectRect();
- zoomOutAfterSel=false;
- }
- edit_label->setPixmap(
- globalImage->scaled(w * 1.25, h * 1.25, Qt::KeepAspectRatio));
- }
- //zoomin();
- void MainWindow::on_pushButton_5_clicked() {
- if (cnt > 13) {
- return;
- }
- cnt++;
- int w = ui->edit_label->width();
- int h = ui->edit_label->height();
- if(zoomedIn){
- ui-> edit_label->setPixmap(
- globalImage->scaled(w * abs(cnt)*.75, h *abs(cnt)* .75, Qt::KeepAspectRatio));
- removeSelectRect();
- zoomedIn=false;
- return;
- }
- //QPixmap globalPixmap(fileName);
- if(zoomOutAfterSel){
- removeSelectRect();
- zoomOutAfterSel=false;
- }
- removeSelectRect();
- QLabel* edit_label = (ui->edit_label);
- edit_label->setPixmap(
- globalImage->scaled(w * .75, h * .75, Qt::KeepAspectRatio));
- }
- void MainWindow::zoomin(){
- QLabel* edit_label = (ui->edit_label);
- int w = edit_label->width();
- int h = edit_label->height();
- edit_label->setPixmap(
- globalImage->scaled(w * 1.25, h * 1.25, Qt::KeepAspectRatio));
- ui->scrollArea->verticalScrollBar()->setValue(min(y1,y2)+factor*globalImage->height());
- ui->scrollArea->horizontalScrollBar()->setValue(min(x1,x2)+factor*globalImage->width());
- qDebug()<<factor<<"hrrrrrrrrrrrrrrrrrrr";
- qDebug()<< ui->scrollArea->verticalScrollBar()->value()<<"value";
- ui->scrollArea->update();
- }
- void MainWindow::on_pushButton_6_clicked() {
- myPush();
- QLabel* edit_label = (ui->edit_label);
- QPixmap pixmap(fileName);
- select=false;
- alreadySelected=false;
- zoomOutAfterSel=false;
- x1=x2=y1=y2=0;
- cnt=7;
- *globalImage=pixmap;
- edit_label->setPixmap(pixmap);
- edit_label->setMask(pixmap.mask());
- edit_label->show();
- }
- void MainWindow::on_pushButton_7_clicked() {
- QString fileName2 = QFileDialog::getSaveFileName(this, tr("Open Image"),
- "C:/Users/moustafa/Desktop", tr("Image Files (*.png *.jpg *.bmp)"));
- QFile file(fileName2);
- file.open(QIODevice::WriteOnly);
- ui->edit_label->pixmap()->save(&file, "PNG");
- }
- void MainWindow::on_pushButton_8_clicked() {//crop
- myPush();
- int x = min(x1, x2);
- int y = min(y1, y2);
- QRect rect(x, y, abs(x2 - x1), abs(y2 - y1));
- QPixmap cropped = globalImage->copy(rect);
- *globalImage=cropped;
- *beforeSelect=cropped;
- ui->edit_label->setPixmap(*globalImage);
- }
- void MainWindow::mousePressEvent(QMouseEvent *ev) {
- QString x = QString::number(ev->x());
- QString y = QString::number(ev->y());
- x1 = x.toInt()-ui->scrollArea->x()-ui->edit_label->x()+ui->scrollArea->horizontalScrollBar()->value();
- y1 = y.toInt()-ui->scrollArea->y()-ui->edit_label->y()-ui->pushButton_2->y()+ui->scrollArea->verticalScrollBar()->value();
- qDebug() << x << "," << y;
- }
- void MainWindow::mouseReleaseEvent(QMouseEvent *ev) {
- qDebug() << checkIn(ev);
- QString x0 = QString::number(ev->x());
- QString y0 = QString::number(ev->y());
- x2 = x0.toInt()- ui->scrollArea->x()-ui->edit_label->x()+ui->scrollArea->horizontalScrollBar()->value();
- y2 = y0.toInt()-ui->scrollArea->y()-ui->edit_label->y()-ui->pushButton_2->y()+ui->scrollArea->verticalScrollBar()->value();
- if(select && !alreadySelected){
- QGraphicsScene *scene = new QGraphicsScene(ui->edit_label);
- QGraphicsView view(scene, ui->edit_label);
- view.show();
- *beforeSelect=*globalImage;
- const QPixmap *temp=ui->edit_label->pixmap();
- *globalImage=*temp;
- QPainter *painter=new QPainter(globalImage);
- painter->setPen(Qt::DashLine);
- painter->drawRect(min(x1,x2),min(y1,y2),abs(x1-x2),abs(y1-y2));
- scene->addPixmap(*globalImage);
- painter->end();
- alreadySelected=true;
- }
- }
- bool MainWindow::checkIn(QMouseEvent *ev) {
- const QPointF x = ev->localPos();
- const QPoint y = x.toPoint();
- return globalImage->rect().contains(y, false);
- }
- void MainWindow::on_pushButton_9_clicked()
- {
- select=true;
- }
- void MainWindow::on_pushButton_10_clicked()
- {
- *globalImage=*tempGlobalImage;
- ui->edit_label->setPixmap(*globalImage);
- alreadySelected=false;
- }
- void MainWindow::removeSelectRect(){
- *globalImage=*beforeSelect;
- select=false;
- alreadySelected=false;
- x1=x2=y1=y2=0;
- ui->edit_label->setPixmap(*globalImage);
- }
- void MainWindow::on_pushButton_11_clicked()//undo pnt--
- {
- if(stack1.size()!=0){
- pair < QPixmap*,param*> temp =stack1.top();
- stack1.pop();
- //----------
- pair < QPixmap*,param*> *t= new pair < QPixmap*,param*> ();
- t->first=(new QPixmap(*globalImage));
- param * p= new param();
- p->set( cnt, select, alreadySelected, zoomOutAfterSel,zoomedIn, factor);
- t->second=p;
- stack2.push(*t);
- //----------
- qDebug()<<"IN UNDO\n";
- globalImage= new QPixmap(*temp.first);
- ui->edit_label->setPixmap(*globalImage);
- param * y=new param(*temp.second);
- cnt=y->cnt;
- select=y->select;
- alreadySelected=y->alreadySelected;
- zoomOutAfterSel=y->zoomOutAfterSel;
- zoomedIn=y->zoomedIn;
- factor=y->factor;
- qDebug()<<pnt<<endl;
- }
- }
- void MainWindow::on_pushButton_12_clicked()//do pnt++
- {
- if(stack2.size()!=0){
- pair < QPixmap*,param*> temp =stack2.top();
- stack2.pop();
- //-----------------
- pair < QPixmap*,param*> *t= new pair < QPixmap*,param*> ();
- t->first=(new QPixmap(*globalImage));
- param * p= new param();
- p->set( cnt, select, alreadySelected, zoomOutAfterSel,zoomedIn, factor);
- t->second=p;
- stack1.push(*t);
- //-----------------
- qDebug()<<"IN DO\n";
- globalImage= new QPixmap(*temp.first);
- ui->edit_label->setPixmap(*globalImage);
- param * y=new param(*temp.second);
- cnt=y->cnt;
- select=y->select;
- alreadySelected=y->alreadySelected;
- zoomOutAfterSel=y->zoomOutAfterSel;
- zoomedIn=y->zoomedIn;
- factor=y->factor;
- qDebug()<<pnt<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement