2 #include <QStyleOptionGraphicsItem>
6 #include <QGraphicsSceneMouseEvent>
7 #include <QtConcurrent/QtConcurrentMap>
10 #include <QReadWriteLock>
11 extern QReadWriteLock
lock;
16 qRegisterMetaType<std::vector<QPixmap>>(
"std::vector<QPixmap>");
17 this->setTransformationAnchor(QGraphicsView::NoAnchor);
18 this->setMouseTracking(
true);
19 clipboard = QApplication::clipboard();
20 saveAction =
new QAction(
"Save Image",
this);
21 copyToClipBoardAction =
new QAction(
"Copy to Clipboard",
this);
22 connect(saveAction, SIGNAL(triggered()),
this, SLOT(on_saveAction_triggered()));
23 connect(copyToClipBoardAction, SIGNAL(triggered()),
this, SLOT(on_copyToClipboardAction_triggered()));
26 hideSideBar =
new QAction(
"Hide Side Bar",
this);
27 hideSideBar->setCheckable(
true);
28 copySelectedRegion =
new QAction(
"Copy Selected Region",
this);
29 connect(hideSideBar, SIGNAL(triggered()),
this, SLOT(on_hideSideBar_triggered()));
30 connect(copySelectedRegion, SIGNAL(triggered()),
this, SLOT(on_copySelectedRegionAction_triggered()));
33 scene =
new QGraphicsScene;
41 QIcon normalsvg(
":/d/dark/eye.svg");
42 QIcon selectsvg(
":/d/dark/alignment-align.svg");
43 if(normalsvg.isNull() || selectsvg.isNull())
58 connect(sbtn->
returnButtonByName(
"Normal"), SIGNAL(clicked()),
this, SLOT(on_normal_triggered()));
59 connect(sbtn->
returnButtonByName(
"Select"), SIGNAL(clicked()),
this, SLOT(on_selectRegion_triggered()));
61 QButtonGroup* group =
new QButtonGroup(
this);
64 group->setExclusive(
true);
65 mEffect =
new QGraphicsOpacityEffect(sbtn);
66 mEffect->setOpacity(1.0);
67 sbtn->setGraphicsEffect(mEffect);
68 scene->addWidget(sbtn);
70 this->setScene(scene);
72 this->setViewport(
new QGLWidget(QGLFormat(QGL::SampleBuffers | QGL::DirectRendering)));
74 this->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
85 const int CHANGE = changeRow > _img_num ? _img_num : changeRow;
87 const size_t CAP_NUM = img_num;
93 for(
size_t i = 0; i < CAP_NUM; ++i){
94 QGraphicsPixmapItem *pix_item = scene->addPixmap(QPixmap(width, height));
95 pix_item_vec.push_back(pix_item);
98 int hori_spacing = 0, verti_spacing = 0;
99 pix_item_vec[0]->setPos(0, 0);
100 QPointF p = pix_item_vec[0]->pos();
101 for(
size_t i = 1; i < CAP_NUM; i++)
103 hori_spacing = (i % CHANGE) == 0 ? 0 : 30;
104 verti_spacing = (i < CHANGE) ? 0 : 30;
106 pix_item_vec[i]->setPos(p.x() + width * (i % CHANGE) + hori_spacing * (i % CHANGE), p.y() + (int)(i / CHANGE) * height + verti_spacing);
108 this->fitInView(0, 0, width * CHANGE, height * CHANGE + verti_spacing * (CAP_NUM / CHANGE), Qt::KeepAspectRatio);
113 void QSmartGraphicsView::setImage(
const cv::Mat &img)
116 QImage img_temp = QImage((uchar*)img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
125 pix_item_vec[0]->setPixmap(QPixmap::fromImage(img_temp.rgbSwapped()));
128 void QSmartGraphicsView::setImage(
const std::vector<cv::Mat> &imgs)
131 for(
size_t i = 0; i < imgs.size(); ++i){
132 QImage img_temp(imgs[i].cols, imgs[i].rows, QImage::Format_RGB888);
133 for(
int y = 0; y < imgs[i].rows; ++y){
134 memcpy(img_temp.scanLine(y), imgs[i].data + y * imgs[i].cols * 3, imgs[i].cols * 3);
136 pix_item_vec[i]->setPixmap(QPixmap::fromImage(img_temp.rgbSwapped()));
139 QList<QGraphicsItem *> item_list = this->items(this->rect());
140 for(
int i = 0; i < item_list.size()/2; ++i){
141 item_list.at(i)->update();
148 pix_item_vec[0]->setPixmap(QPixmap::fromImage(qimg));
153 QList<QGraphicsItem *> item_list = this->items(this->rect());
154 for(
int i = 0; i < item_list.size(); ++i){
155 QGraphicsPixmapItem *item =
dynamic_cast<QGraphicsPixmapItem*
>(item_list[i]);
164 for(
size_t i = 0; i < qimgs.size(); ++i)
165 pix_item_vec[i]->setPixmap(QPixmap::fromImage(qimgs[i]));
167 QList<QGraphicsItem *> item_list = this->items(this->rect());
168 for(
int i = 0; i < item_list.size()/2; ++i){
169 item_list.at(i)->update();
175 if(event->delta() == 0)
177 QList<QGraphicsItem*> list = this->items();
181 QPointF pt = this->mapToScene(event->pos());
183 if(event->delta() > 0)
185 else if(event->delta() < 0)
195 this->scale(factor, factor);
202 if((mouse_status == 0) && event->buttons() == Qt::LeftButton){
204 this->translate(( -mou_x + event->x())/1.0, ( -mou_y + event->y())/1.0);
205 this->setCursor(Qt::ClosedHandCursor);
207 this->setCursor(Qt::OpenHandCursor);
210 else if((mouse_status == 1) && (event->modifiers() & Qt::ControlModifier) && event->buttons() == Qt::LeftButton){
211 this->translate(( -mou_x + event->x())/1.0, ( -mou_y + event->y())/1.0);
212 this->setCursor(Qt::ClosedHandCursor);
214 this->setCursor(Qt::OpenHandCursor);
216 else if((mouse_status == 1) && event->buttons() == Qt::LeftButton)
220 QGraphicsPixmapItem *item =
dynamic_cast<QGraphicsPixmapItem *
>(this->itemAt(event->pos()));
223 select_item_start = this->mapFromScene(item->mapToScene(0, 0));
224 select_item_bound = this->mapFromScene(item->mapToScene(item->boundingRect().width(),item->boundingRect().height()));
225 int&& pos_x = (
event->pos().x()) > select_item_bound.x() ? select_item_bound.x() : (
event->pos().x() < select_item_start.x() ? select_item_start.x() :
event->pos().x());
226 int&& pos_y = (
event->pos().y()) > select_item_bound.y() ? select_item_bound.y() : (
event->pos().y() < select_item_start.y() ? select_item_start.y() :
event->pos().y());
227 select_start = QPoint(pos_x, pos_y);
228 rubberBand->setGeometry(QRect(select_start.toPoint(), QSize()));
234 int&& pos_x = (
event->pos().x()) > select_item_bound.x() ? select_item_bound.x() : (
event->pos().x() < select_item_start.x() ? select_item_start.x() :
event->pos().x());
235 int&& pos_y = (
event->pos().y()) > select_item_bound.y() ? select_item_bound.y() : (
event->pos().y() < select_item_start.y() ? select_item_start.y() :
event->pos().y());
236 rubberBand->setGeometry(QRect(select_start.toPoint(), QPoint(pos_x, pos_y)).normalized());
251 (mouse_status == 0) &&
253 (event->button() == Qt::LeftButton))
254 this->setCursor(Qt::ClosedHandCursor);
256 else if(_initial && (mouse_status == 1) && event->buttons() == Qt::LeftButton)
259 rubberBand =
new QRubberBand(QRubberBand::Rectangle,
this);
260 this->setDragMode(QGraphicsView::RubberBandDrag);
262 QGraphicsPixmapItem *item =
dynamic_cast<QGraphicsPixmapItem *
>(this->itemAt(event->pos()));
269 select_item_start = this->mapFromScene(item->mapToScene(0, 0));
270 select_item_bound = this->mapFromScene(item->mapToScene(item->boundingRect().width(),item->boundingRect().height()));
271 int&& pos_x = (
event->pos().x()) > select_item_bound.x() ? select_item_bound.x() : (
event->pos().x() < select_item_start.x() ? select_item_start.x() :
event->pos().x());
272 int&& pos_y = (
event->pos().y()) > select_item_bound.y() ? select_item_bound.y() : (
event->pos().y() < select_item_start.y() ? select_item_start.y() :
event->pos().y());
273 select_start = QPoint(pos_x, pos_y);
274 rubberBand->setGeometry(QRect(select_start.toPoint(), QSize()));
279 else if(event->button() == Qt::MidButton)
287 this->fitInView(0, 0, this->sceneRect().width(), this->sceneRect().height(), Qt::KeepAspectRatio);
294 QGraphicsPixmapItem *item =
dynamic_cast<QGraphicsPixmapItem *
>(this->itemAt(event->pos()));
297 QPointF local_pt = item->mapFromScene(this->mapToScene(event->pos()));
298 qDebug () << local_pt;
299 if(this->pix_item_vec.size() == 1)
305 this->setCursor(Qt::ArrowCursor);
306 if(event->button() == Qt::RightButton){
308 m.addAction(saveAction);
309 m.addAction(copyToClipBoardAction);
311 if(mouse_status == 1)
313 m.addAction(copySelectedRegion);
316 m.addAction(hideSideBar);
318 mou_press =
event->pos();
319 m.exec(event->globalPos());
322 else if(event->button() == Qt::LeftButton && this->dragMode() == QGraphicsView::RubberBandDrag)
325 if(event->pos() == select_start)
328 this->setDragMode(QGraphicsView::NoDrag);
336 if(event->key() == Qt::Key_H)
338 hideSideBar->trigger();
343 void QSmartGraphicsView::on_saveAction_triggered()
345 bool isError =
false;
346 if(img_num == 0) {
return;}
349 d.setConfirmOverwrite(
false);
350 QFileInfo file_name(d.getSaveFileName(0,
"Img",0,
"PNG (*.png);;BMP (*.bmp);;JPG (*.jpg)"));
351 if(file_name.fileName().isNull()) {
355 for(
int i = 0; i < img_num; ++i)
357 if(!pix_item_vec[i]->pixmap().isNull())
360 if(file_name.exists())
362 while(QFile::exists(file_name.absolutePath() +
"/" + file_name.completeBaseName() +
"_" + QString::number(i + num_index)+
"."+file_name.suffix()))
364 if(i == 0 && num_index == 0)
365 pix_item_vec[i]->pixmap().save(file_name.absoluteFilePath());
367 pix_item_vec[i]->pixmap().save(file_name.absolutePath() +
"/" + file_name.completeBaseName() +
"_"+QString::number(i + num_index)+
"."+file_name.suffix());
369 else{isError =
true;}
373 if(!pix_item_vec[0]->pixmap().isNull()){pix_item_vec[0]->pixmap().save(file_name.absoluteFilePath());}
374 else{isError =
true;}
376 if(isError){QMessageBox::information(0, 0,
"Can Not Save Image!!");}
379 void QSmartGraphicsView::on_copyToClipboardAction_triggered()
381 bool isError =
false;
382 if(img_num == 0) {
return;}
386 QGraphicsPixmapItem *item =
dynamic_cast<QGraphicsPixmapItem *
>(this->itemAt(mou_press));
388 if(!item){isError =
true;}
389 else if(!item->pixmap().isNull()){clipboard->setPixmap(item->pixmap());}
390 else{isError =
true;}
392 if(isError){QMessageBox::information(0, 0,
"Can Not Save Image!!");}
396 void QSmartGraphicsView::on_normal_triggered()
398 if(mouse_status == 0)
405 void QSmartGraphicsView::on_selectRegion_triggered()
407 if(mouse_status == 1)
415 void QSmartGraphicsView::on_hideSideBar_triggered()
420 void QSmartGraphicsView::on_copySelectedRegionAction_triggered()
424 if(rubberBand->isHidden())
427 QGraphicsPixmapItem *item =
dynamic_cast<QGraphicsPixmapItem *
>(this->itemAt(rubberBand->pos()));
431 QPointF point = item->mapFromScene(this->mapToScene(rubberBand->pos()));
432 QPointF point2 = item->mapFromScene(this->mapToScene(rubberBand->pos().x() + rubberBand->rect().width(), rubberBand->pos().y() + rubberBand->rect().height()));
434 QImage cpy_img = item->pixmap().copy(QRect(point.toPoint(), point2.toPoint() - QPoint(1, 1))).toImage();
438 void QSmartGraphicsView::fadeOut()
440 QPropertyAnimation* animation =
new QPropertyAnimation(mEffect,
"opacity");
441 animation->setDuration(500);
442 if(sbtn->isVisible())
444 animation->setStartValue(1.0);
445 animation->setEndValue(0.0);
446 connect(animation,SIGNAL(finished()),
this,SLOT(onAnimationFinished()));
450 sbtn->setVisible(
true);
451 animation->setStartValue(0.0);
452 animation->setEndValue(1.0);
454 animation->start(QAbstractAnimation::DeleteWhenStopped);
457 void QSmartGraphicsView::onAnimationFinished()
459 sbtn->setVisible(
false);
void sendMousePress()
Sends signal if the mouse is pressed.
void mouseReleaseEvent(QMouseEvent *event)
void keyPressEvent(QKeyEvent *event)
void mouseDoubleClickEvent(QMouseEvent *event)
void sendSelectedRegion(const QImage &img)
Send selected region selected by the rubberBand.
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.
void mousePressEvent(QMouseEvent *event)
void initialize(const int _img_num, const int width, const int height, int changeRow=4)
Must initial before setting any image to QSmartGraphicsView.
void mouseMoveEvent(QMouseEvent *event)
void wheelEvent(QWheelEvent *event)
QSmartGraphicsView(QWidget *parent=0)