qtsmartgraphics  2.1.1
A simple library that improves the user experience when displaying images by Qt's QLabel and QGraphicsView.
qsmartgraphicsview.h
Go to the documentation of this file.
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 // Written by Qt 5.3
3 // Original by Shih-Jhong Yu, Revised by KKyang.
4 // Protected under LPGLv3 license.
5 //M*/
6 
7 #ifndef QSMARTGRAPHICSVIEW_H
8 #define QSMARTGRAPHICSVIEW_H
9 
10 #include <QApplication>
11 #include <QClipboard>
12 #include <QFileInfo>
13 #include <QGraphicsItem>
14 #include <QGraphicsPixmapItem>
15 #include <QGraphicsView>
16 #include <QList>
17 #include <QMimeData>
18 #include <QPushButton>
19 #include <QWheelEvent>
20 #include <QMessageBox>
21 #include <QVector>
22 #include <vector>
23 
24 #include <QDebug>
25 #ifdef HAVE_OPENCV
26 #include "opencv2/opencv.hpp"
27 #endif
28 
29 #ifdef QT_OPENGL_LIB
30 #include <QGL>
31 #endif
32 
33 #ifndef NO_SIDEMENU
34 #include <QButtonGroup>
35 #include <QGraphicsOpacityEffect>
36 #include <QPropertyAnimation>
37 #include <QStyleOption>
38 #ifdef QT_SVG_LIB
39 #include <QtSvg>
40 #endif
41 class QSideButtonBar;
42 class customQGraphicsItem;
43 #endif
44 
46 class QSmartGraphicsView : public QGraphicsView
47 {
48  Q_OBJECT
49 public:
50  explicit QSmartGraphicsView(QWidget *parent = 0);
52  void initialize(const int _img_num, const int width, const int height, int changeRow = 4);
53  //Set images.
54 #ifdef HAVE_OPENCV
55  void setImage(const cv::Mat &img);
56  void setImage(const std::vector<cv::Mat> &imgs);
57 #endif
58  void setImagefromQImage(const QImage &qimg);
59  void setImagefromQImage(const std::vector<QImage> &qimgs);
60 
61  bool isInitial() const{return _initial;}
62  int getImgNum(){return img_num;}
63 protected:
64  void wheelEvent(QWheelEvent *event);
65  void mousePressEvent(QMouseEvent *event);
66  void mouseDoubleClickEvent(QMouseEvent *event);
67  void mouseMoveEvent(QMouseEvent *event);
68  void mouseReleaseEvent(QMouseEvent *event);
69 #ifndef NO_SIDEMENU
70  void keyPressEvent(QKeyEvent *event);
71 #endif
72 signals:
73  void sendItemMouXY(const double x, const double y);
74  void sendMousePress();
75 #ifndef NO_SIDEMENU
76  void sendSelectedRegion(const QImage &img);
77 #endif
78 public slots:
79  void updateImg();
80 private slots:
81  void on_saveAction_triggered();
82  void on_copyToClipboardAction_triggered();
83 #ifndef NO_SIDEMENU
84  void on_normal_triggered();
85  void on_selectRegion_triggered();
86  void on_hideSideBar_triggered();
87  void on_copySelectedRegionAction_triggered();
88  void onAnimationFinished();
89 #endif
90 private:
91  QAction *hideSideBar, *saveAction, *copyToClipBoardAction, *copySelectedRegion;
92  QClipboard *clipboard;
93  QGraphicsScene *scene;
94  QVector<QGraphicsPixmapItem*> pix_item_vec;
95 
96  QPoint mou_press;
97  int mou_x;
98  int mou_y;
99 
100  bool _initial = false;
101  int img_num;
102 
103 #ifndef NO_SIDEMENU
104  QSideButtonBar *sbtn;
105  int mouse_status = 0;
106  QPointF select_start, select_item_bound, select_item_start;
107  bool is_item = false;
108  QRubberBand *rubberBand = 0;
109 
110  void fadeOut();
111  QGraphicsOpacityEffect* mEffect;
112 #endif
113 };
114 
115 #ifndef NO_SIDEMENU
116 
117 struct btnID{
118  QPushButton* btn;
119  QString ID;
120 };
121 
123 class QSideButtonBar : public QWidget
124 {
125  Q_OBJECT
126 public:
127  QSideButtonBar(QWidget *parent = 0) : QWidget(parent){
128  this->setStyleSheet("QSideButtonBar{border: 2px solid grey;}");
129  }
130  QSideButtonBar(int button_num, QWidget *parent = 0) : QWidget(parent){
131  this->setStyleSheet("QSideButtonBar{border: 2px solid grey;}");
132  btn.resize(button_num);
133  for(int i = 0; i < btn.size(); i++)
134  {
135  btn[i].btn = new QPushButton(this);
136  btn[i].btn->setCheckable(true);
137  btn[i].btn->setGeometry(0, i * 31 + 0, 30, 30);
138  }
139  }
140 
142  {
143  btn.clear();
144  }
145 
146  void addButton()
147  {
148  btnID newb;
149  newb.ID = "";
150  newb.btn = new QPushButton(this);
151  btn.push_back(newb);
152  }
153 
154  void renameButtonText(QString text, int btn_num)
155  {
156  if(btn_num >= 0 && btn_num < btn.size())
157  btn[btn_num].btn->setText(text);
158  }
159 
160  void renameButtonID(QString ID, int btn_num)
161  {
162  if(btn_num >= 0 && btn_num < btn.size())
163  btn[btn_num].ID = ID;
164  }
165 
166  int findButtonID(QString name)
167  {
168  int i = 0;
169  for(i = 0; i < btn.size(); i++)
170  {
171  if(!btn[i].ID.compare(name))
172  return i;
173  }
174 
175  return -1;
176  }
177 
178  QPushButton* returnButtonByName(QString name)
179  {
180  int i = 0;
181  for(i = 0; i < btn.size(); i++)
182  {
183  if(!btn[i].ID.compare(name))
184  return btn[i].btn;
185  }
186 
187  return NULL;
188  }
189 
190  void setButtonIcon(QString name, const QIcon &icon)
191  {
192  int i = 0;
193  for(i = 0; i < btn.size(); i++)
194  {
195  if(!btn[i].ID.compare(name))
196  {
197  btn[i].btn->setIcon(icon);
198  return;
199  }
200  }
201 
202  return;
203  }
204 
205  void setButtonCheckable(QString name, const bool c)
206  {
207  int i = 0;
208  for(i = 0; i < btn.size(); i++)
209  {
210  if(!btn[i].ID.compare(name))
211  {
212  btn[i].btn->setCheckable(c);
213  return;
214  }
215  }
216 
217  return;
218  }
219 
220  std::vector<btnID> btn;
221 private:
222  void paintEvent(QPaintEvent *)
223  {
224  QStyleOption opt;
225  opt.init(this);
226  QPainter p(this);
227  style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
228  }
229 };
230 #endif
231 
232 
233 #endif // SMARTGRAPHICSVIEW_H
void sendMousePress()
Sends signal if the mouse is pressed.
void mouseReleaseEvent(QMouseEvent *event)
void keyPressEvent(QKeyEvent *event)
void mouseDoubleClickEvent(QMouseEvent *event)
std::vector< btnID > btn
QPushButton * btn
void sendSelectedRegion(const QImage &img)
Send selected region selected by the rubberBand.
void renameButtonText(QString text, int btn_num)
void setImagefromQImage(const QImage &qimg)
Set a single image (QImage) to QSmartGraphicsView.
void sendItemMouXY(const double x, const double y)
Sends the mouse coordinate of the image when clicked the image.
QString ID
void mousePressEvent(QMouseEvent *event)
QSideButtonBar(int button_num, QWidget *parent=0)
void renameButtonID(QString ID, int btn_num)
int findButtonID(QString name)
void initialize(const int _img_num, const int width, const int height, int changeRow=4)
Must initial before setting any image to QSmartGraphicsView.
int getImgNum()
Returns the number of images set in QSmartGraphicsView.
void mouseMoveEvent(QMouseEvent *event)
void setButtonIcon(QString name, const QIcon &icon)
QPushButton * returnButtonByName(QString name)
QSideButtonBar(QWidget *parent=0)
void wheelEvent(QWheelEvent *event)
bool isInitial() const
Returns true if initialized before.
QSmartGraphicsView(QWidget *parent=0)
void setButtonCheckable(QString name, const bool c)