Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.22 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QFileDialog>
  4. #include <QPixmap>
  5. #include <QBitmap>
  6. #include <QDesktopWidget>
  7. #include <QTransform>
  8. #include <QInputDialog>
  9. #include <QPainter>
  10. #include <QMouseEvent>
  11. #include <QDebug>
  12. #include <QPointF>
  13. #include <QSize>
  14. #include <cmath>
  15. #include <vector>
  16. #include <math.h>
  17. #include <stack>
  18. #include <QPainter>
  19. #include <QPen>
  20. #include <QPaintEvent>
  21. #include <QGraphicsScene>
  22. #include <QGraphicsView>
  23. #include <QAbstractSlider>
  24. #include <QScrollBar>
  25. #define PI 3.14159265359
  26. using namespace std;
  27. QPixmap *tempGlobalImage;
  28. QPixmap *globalImage;
  29. QPixmap *beforeSelect;
  30. QString fileName;
  31. int cnt = 7;
  32. bool select=false;
  33. bool alreadySelected=false;
  34. bool zoomOutAfterSel=false;
  35. bool zoomedIn=false;
  36. double factor=1;
  37. struct param {
  38. int cnt ;
  39. bool select;
  40. bool alreadySelected;
  41. bool zoomOutAfterSel;
  42. bool zoomedIn;
  43. double factor;
  44. void set(int cnt, bool select, bool alreadySelected,bool zoomOutAfterSel,bool zoomedIn,double factor) {
  45. this->cnt=cnt;
  46. this->select=select;
  47. this->alreadySelected=alreadySelected;
  48. this->zoomOutAfterSel=zoomOutAfterSel;
  49. this->zoomedIn=zoomedIn;
  50. this->factor=factor;
  51. }
  52. };
  53. stack<pair < QPixmap*,param*> > stack1,stack2;
  54. int pnt;
  55.  
  56. void myPush() {
  57. while ( ! stack2.empty())
  58. stack2.pop();
  59. pair < QPixmap*,param*> *t= new pair < QPixmap*,param*> ();
  60. t->first=(new QPixmap(*globalImage));
  61. param * p= new param();
  62. p->set( cnt, select, alreadySelected, zoomOutAfterSel,zoomedIn, factor);
  63. t->second=p;
  64. stack1.push(*t);
  65. qDebug()<<"IN MY PUSH"<<stack1.size()<<" "<<stack2.size()<<endl;
  66. }
  67. //rotate around center of current image
  68. pair<int, int> rotate(double x, double y, double theta) {
  69. QSize qz = globalImage->size();
  70. double w = qz.width(), h = qz.height();
  71. x -= (w / 2.0), y -= (h / 2.0);
  72. theta = theta * (PI / 180.0);
  73. double x1 = (x * cos(theta)) - (y * sin(theta));
  74. double y1 = (x * sin(theta)) + (y * cos(theta));
  75. return make_pair(x1 + (w / 2.0), y1 + (h / 2.0));
  76. }
  77. pair<QSize, pair<int, int> > getNewSize(double theta) {
  78. QSize qz = globalImage->size();
  79. double w = qz.width(), h = qz.height();
  80. vector<pair<int, int> > v;
  81. v.push_back(rotate(0, 0, theta));
  82. v.push_back(rotate(w, 0, theta));
  83. v.push_back(rotate(0, h, theta));
  84. v.push_back(rotate(w, h, theta));
  85. int newHeight = INT_MIN;
  86. int newWidth = INT_MIN;
  87. int dx = INT_MAX;
  88. int dy = INT_MAX;
  89. for (int i = 0; i < 4; i++) {
  90. dx = min(dx, v[i].first);
  91. dy = min(dy, v[i].second);
  92. for (int j = i + 1; j < 4; j++) {
  93. newHeight = max(newHeight, abs(v[i].second - v[j].second));
  94. newWidth = max(newWidth, abs(v[i].first - v[j].first));
  95. }
  96. }
  97. pair<QSize, pair<int, int> > ret;
  98. ret.first = QSize(newWidth, newHeight);
  99. ret.second.first = dx*-1;
  100. ret.second.second = dy*-1;
  101. return ret;
  102. }
  103. MainWindow::MainWindow(QWidget *parent) :
  104. QMainWindow(parent), ui(new Ui::MainWindow) {
  105. globalImage = new QPixmap();
  106. tempGlobalImage=new QPixmap();
  107. beforeSelect=new QPixmap();
  108. //tempGlobalImage=new QPixmap();
  109. ui->setupUi(this);
  110. }
  111.  
  112. MainWindow::~MainWindow() {
  113. delete ui;
  114. }
  115.  
  116. void MainWindow::on_pushButton_clicked() {//righth rotate
  117. bool ok;
  118. if(select || alreadySelected){
  119. select=false;
  120. alreadySelected=false;
  121. removeSelectRect();
  122. }
  123. int deg = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
  124. tr("Degree:"), 180, 0, 360, 1, &ok);
  125. if (ok) {
  126. myPush();
  127. pair<QSize, pair<int, int> > temp = getNewSize(deg);
  128. QPixmap rotate(temp.first);
  129. QPainter p(&rotate);
  130. rotate.fill(Qt::white);
  131. p.setBackgroundMode(Qt::TransparentMode);
  132. p.setRenderHint(QPainter::Antialiasing);
  133. p.setRenderHint(QPainter::SmoothPixmapTransform);
  134. p.setRenderHint(QPainter::HighQualityAntialiasing);
  135. p.translate(rotate.size().width() / 2, rotate.size().height() / 2);
  136. p.rotate(deg);
  137. p.translate(-rotate.size().width() / 2, -rotate.size().height() / 2);
  138. qDebug()<<temp.second.first<<" "<< temp.second.second<<endl;
  139. p.drawPixmap(temp.second.first, temp.second.second, *globalImage);
  140. p.end();
  141. *globalImage=rotate;
  142. ui->edit_label->setPixmap(rotate);
  143. }
  144. }
  145.  
  146. void MainWindow::on_pushButton_2_clicked() {
  147. QLabel* edit_label = (ui->edit_label);
  148. fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),
  149. "C:/Users/moustafa/Desktop", tr("Image Files (*.png *.jpg *.bmp)"));
  150. globalImage->load(fileName);
  151. tempGlobalImage->load(fileName);
  152. edit_label->setPixmap(*globalImage);
  153. edit_label->setMask((globalImage)->mask());
  154. edit_label->show();
  155. if(fileName!=NULL)
  156. myPush();
  157. }
  158.  
  159. void MainWindow::on_pushButton_3_clicked() {//left rotate
  160. if(select || alreadySelected){
  161. select=false;
  162. alreadySelected=false;
  163. removeSelectRect();
  164. }
  165.  
  166. bool ok;
  167. int deg = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
  168. tr("Degree:"), 180, 0, 360, 1, &ok);
  169. if (ok) {
  170. myPush();
  171. pair<QSize, pair<int, int> > temp = getNewSize(deg*-1);
  172. QPixmap rotate(temp.first);
  173. QPainter p(&rotate);
  174. rotate.fill(Qt::white);
  175. p.setBackgroundMode(Qt::TransparentMode);
  176. p.setRenderHint(QPainter::Antialiasing);
  177. p.setRenderHint(QPainter::SmoothPixmapTransform);
  178. p.setRenderHint(QPainter::HighQualityAntialiasing);
  179. p.translate(rotate.size().width() / 2, rotate.size().height() / 2);
  180. p.rotate(-1*deg);
  181. p.translate(-rotate.size().width() / 2, -rotate.size().height() / 2);
  182. qDebug()<<temp.second.first<<" "<< temp.second.second<<endl;
  183. p.drawPixmap(temp.second.first, temp.second.second, *globalImage);
  184. p.end();
  185. // globalImage->copy(rotate.rect());
  186. *globalImage=rotate;
  187. ui->edit_label->setPixmap(rotate);
  188. }
  189. }
  190.  
  191. void MainWindow::on_pushButton_4_clicked() {
  192. if (cnt < 1) {
  193. return;
  194. }
  195. cnt--;
  196. QLabel* edit_label = (ui->edit_label);
  197. int w = edit_label->width();
  198. int h = edit_label->height();
  199. if(select && alreadySelected){
  200. zoomedIn=true;
  201. int x = min(x1, x2);
  202. int y = min(y1, y2);
  203. zoomOutAfterSel=true;
  204. QRect rect(x, y, abs(x2 - x1), abs(y2 - y1));
  205. QPixmap cropped = globalImage->copy(rect);
  206. //globalImage=&cropped;
  207. qDebug()<<(globalImage->height()/cropped.height());
  208.  
  209. ui->edit_label->setPixmap(cropped.scaled(w,h,Qt::KeepAspectRatio));
  210. globalImage->scaled(w * (globalImage->width()/cropped.width()), h * (globalImage->height()/cropped.height()), Qt::KeepAspectRatio);
  211.  
  212. select=false;
  213. alreadySelected=false;
  214.  
  215.  
  216. return;
  217. }
  218. if(zoomOutAfterSel){
  219. removeSelectRect();
  220. zoomOutAfterSel=false;
  221. }
  222.  
  223.  
  224.  
  225. edit_label->setPixmap(
  226. globalImage->scaled(w * 1.25, h * 1.25, Qt::KeepAspectRatio));
  227.  
  228.  
  229. }
  230. //zoomin();
  231. void MainWindow::on_pushButton_5_clicked() {
  232. if (cnt > 13) {
  233. return;
  234. }
  235. cnt++;
  236. int w = ui->edit_label->width();
  237. int h = ui->edit_label->height();
  238. if(zoomedIn){
  239. ui-> edit_label->setPixmap(
  240. globalImage->scaled(w * abs(cnt)*.75, h *abs(cnt)* .75, Qt::KeepAspectRatio));
  241. removeSelectRect();
  242. zoomedIn=false;
  243. return;
  244. }
  245. //QPixmap globalPixmap(fileName);
  246. if(zoomOutAfterSel){
  247. removeSelectRect();
  248. zoomOutAfterSel=false;
  249. }
  250. removeSelectRect();
  251. QLabel* edit_label = (ui->edit_label);
  252. edit_label->setPixmap(
  253. globalImage->scaled(w * .75, h * .75, Qt::KeepAspectRatio));
  254. }
  255.  
  256. void MainWindow::zoomin(){
  257. QLabel* edit_label = (ui->edit_label);
  258. int w = edit_label->width();
  259. int h = edit_label->height();
  260.  
  261. edit_label->setPixmap(
  262. globalImage->scaled(w * 1.25, h * 1.25, Qt::KeepAspectRatio));
  263.  
  264. ui->scrollArea->verticalScrollBar()->setValue(min(y1,y2)+factor*globalImage->height());
  265. ui->scrollArea->horizontalScrollBar()->setValue(min(x1,x2)+factor*globalImage->width());
  266.  
  267. qDebug()<<factor<<"hrrrrrrrrrrrrrrrrrrr";
  268. qDebug()<< ui->scrollArea->verticalScrollBar()->value()<<"value";
  269. ui->scrollArea->update();
  270. }
  271.  
  272. void MainWindow::on_pushButton_6_clicked() {
  273. myPush();
  274. QLabel* edit_label = (ui->edit_label);
  275. QPixmap pixmap(fileName);
  276. select=false;
  277. alreadySelected=false;
  278. zoomOutAfterSel=false;
  279. x1=x2=y1=y2=0;
  280. cnt=7;
  281.  
  282. *globalImage=pixmap;
  283.  
  284. edit_label->setPixmap(pixmap);
  285. edit_label->setMask(pixmap.mask());
  286. edit_label->show();
  287. }
  288.  
  289. void MainWindow::on_pushButton_7_clicked() {
  290. QString fileName2 = QFileDialog::getSaveFileName(this, tr("Open Image"),
  291. "C:/Users/moustafa/Desktop", tr("Image Files (*.png *.jpg *.bmp)"));
  292. QFile file(fileName2);
  293. file.open(QIODevice::WriteOnly);
  294. ui->edit_label->pixmap()->save(&file, "PNG");
  295. }
  296.  
  297. void MainWindow::on_pushButton_8_clicked() {//crop
  298. myPush();
  299. int x = min(x1, x2);
  300. int y = min(y1, y2);
  301. QRect rect(x, y, abs(x2 - x1), abs(y2 - y1));
  302. QPixmap cropped = globalImage->copy(rect);
  303. *globalImage=cropped;
  304. *beforeSelect=cropped;
  305. ui->edit_label->setPixmap(*globalImage);
  306. }
  307.  
  308. void MainWindow::mousePressEvent(QMouseEvent *ev) {
  309. QString x = QString::number(ev->x());
  310. QString y = QString::number(ev->y());
  311. x1 = x.toInt()-ui->scrollArea->x()-ui->edit_label->x()+ui->scrollArea->horizontalScrollBar()->value();
  312. y1 = y.toInt()-ui->scrollArea->y()-ui->edit_label->y()-ui->pushButton_2->y()+ui->scrollArea->verticalScrollBar()->value();
  313. qDebug() << x << "," << y;
  314. }
  315. void MainWindow::mouseReleaseEvent(QMouseEvent *ev) {
  316. qDebug() << checkIn(ev);
  317. QString x0 = QString::number(ev->x());
  318. QString y0 = QString::number(ev->y());
  319. x2 = x0.toInt()- ui->scrollArea->x()-ui->edit_label->x()+ui->scrollArea->horizontalScrollBar()->value();
  320. y2 = y0.toInt()-ui->scrollArea->y()-ui->edit_label->y()-ui->pushButton_2->y()+ui->scrollArea->verticalScrollBar()->value();
  321. if(select && !alreadySelected){
  322. QGraphicsScene *scene = new QGraphicsScene(ui->edit_label);
  323. QGraphicsView view(scene, ui->edit_label);
  324. view.show();
  325. *beforeSelect=*globalImage;
  326. const QPixmap *temp=ui->edit_label->pixmap();
  327. *globalImage=*temp;
  328. QPainter *painter=new QPainter(globalImage);
  329. painter->setPen(Qt::DashLine);
  330. painter->drawRect(min(x1,x2),min(y1,y2),abs(x1-x2),abs(y1-y2));
  331. scene->addPixmap(*globalImage);
  332. painter->end();
  333. alreadySelected=true;
  334.  
  335. }
  336. }
  337. bool MainWindow::checkIn(QMouseEvent *ev) {
  338. const QPointF x = ev->localPos();
  339. const QPoint y = x.toPoint();
  340. return globalImage->rect().contains(y, false);
  341. }
  342.  
  343. void MainWindow::on_pushButton_9_clicked()
  344. {
  345. select=true;
  346. }
  347.  
  348. void MainWindow::on_pushButton_10_clicked()
  349. {
  350. *globalImage=*tempGlobalImage;
  351. ui->edit_label->setPixmap(*globalImage);
  352. alreadySelected=false;
  353.  
  354. }
  355. void MainWindow::removeSelectRect(){
  356. *globalImage=*beforeSelect;
  357. select=false;
  358. alreadySelected=false;
  359. x1=x2=y1=y2=0;
  360. ui->edit_label->setPixmap(*globalImage);
  361. }
  362.  
  363. void MainWindow::on_pushButton_11_clicked()//undo pnt--
  364. {
  365. if(stack1.size()!=0){
  366. pair < QPixmap*,param*> temp =stack1.top();
  367. stack1.pop();
  368. //----------
  369. pair < QPixmap*,param*> *t= new pair < QPixmap*,param*> ();
  370. t->first=(new QPixmap(*globalImage));
  371. param * p= new param();
  372. p->set( cnt, select, alreadySelected, zoomOutAfterSel,zoomedIn, factor);
  373. t->second=p;
  374. stack2.push(*t);
  375. //----------
  376. qDebug()<<"IN UNDO\n";
  377. globalImage= new QPixmap(*temp.first);
  378. ui->edit_label->setPixmap(*globalImage);
  379. param * y=new param(*temp.second);
  380. cnt=y->cnt;
  381. select=y->select;
  382. alreadySelected=y->alreadySelected;
  383. zoomOutAfterSel=y->zoomOutAfterSel;
  384. zoomedIn=y->zoomedIn;
  385. factor=y->factor;
  386. qDebug()<<pnt<<endl;
  387. }
  388. }
  389.  
  390. void MainWindow::on_pushButton_12_clicked()//do pnt++
  391. {
  392. if(stack2.size()!=0){
  393. pair < QPixmap*,param*> temp =stack2.top();
  394. stack2.pop();
  395. //-----------------
  396. pair < QPixmap*,param*> *t= new pair < QPixmap*,param*> ();
  397. t->first=(new QPixmap(*globalImage));
  398. param * p= new param();
  399. p->set( cnt, select, alreadySelected, zoomOutAfterSel,zoomedIn, factor);
  400. t->second=p;
  401. stack1.push(*t);
  402. //-----------------
  403. qDebug()<<"IN DO\n";
  404. globalImage= new QPixmap(*temp.first);
  405. ui->edit_label->setPixmap(*globalImage);
  406. param * y=new param(*temp.second);
  407. cnt=y->cnt;
  408. select=y->select;
  409. alreadySelected=y->alreadySelected;
  410. zoomOutAfterSel=y->zoomOutAfterSel;
  411. zoomedIn=y->zoomedIn;
  412. factor=y->factor;
  413. qDebug()<<pnt<<endl;
  414. }
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement