1 //===========================================
2 //  Lumina-DE source code
3 //  Copyright (c) 2016, Ken Moore
4 //  Available under the 3-clause BSD license
5 //  See the LICENSE file for full details
6 //===========================================
7 // This is a simple widget for viewing a QImage and allowing the user to
8 //   highlight/select portions of the image with possible actions on the selection
9 //===========================================
10 #ifndef _LUMINA_SCREENSHOT_IMAGE_EDITOR_H
11 #define _LUMINA_SCREENSHOT_IMAGE_EDITOR_H
12 
13 #include <QImage>
14 #include <QWidget>
15 #include <QRect>
16 #include <QMouseEvent>
17 #include <QPaintEvent>
18 #include <QMenu>
19 
20 class ImageEditor : public QWidget{
21 	Q_OBJECT
22 public:
23 	ImageEditor(QWidget*parent = 0);
24 	~ImageEditor();
25 
26 	void LoadImage(QImage img);
27 	void setDefaultSize(QSize sz);
28 	bool hasSelection();
29 	QImage image();
30 
31 	int getScalingValue();
32 
33 private:
34 	QImage fullIMG, scaledIMG; //Note: the aspect ratio between the two images must be preserved!!
35 	QSize defaultSize; //for loading new images
36 	QRect selRect; //selection rectangle (scaledIMG coordinates)
37 	QPoint selPoint; //initial selection point (used during click/drags)
38 	QMenu *contextMenu;
39 
40 	//simplification functions
getScaleFactor()41 	qreal getScaleFactor(){
42          //return the scale factor between the full/scaled images
43         return ( ( (qreal) scaledIMG.height()) / ( (qreal) fullIMG.height()) );
44 	}
45 
rescaleImage(qreal scfactor)46 	void rescaleImage(qreal scfactor){
47         if(fullIMG.isNull()){ return; }
48 	  scaledIMG = fullIMG.scaled( fullIMG.width()*scfactor, fullIMG.height()*scfactor, Qt::KeepAspectRatio,Qt::SmoothTransformation);
49 	  selRect = QRect();
50 	  emit selectionChanged(false);
51         this->update(); //trigger a repaint event
52 	}
53 
54 private slots:
55 	void showMenu();
56 
57 public slots:
58 	void setScaling(int perc); //10% <--> 200% range is valid
59 	void scaleUp(int val = 10); //10% change by default
60 	void scaleDown(int val = 10); //10% change by default
61 
62 	void cropImage();
63 	void resizeImage();
64 
65 protected:
66 	void mousePressEvent(QMouseEvent *ev);
67 	void mouseMoveEvent(QMouseEvent *ev);
68 	void mouseReleaseEvent(QMouseEvent *);
69 	void paintEvent(QPaintEvent*);
70 
71 signals:
72 	void selectionChanged(bool); //true if there is a selection
73 	void scaleFactorChanged(int);
74 };
75 #endif
76