Subversion Repositories seema-scanner

Rev

Rev 220 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
201 jakw 1
#include "SMVideoZoomWidget.h"
2
 
3
#include "cvtools.h"
4
 
5
#include <QScrollBar>
6
#include <QMenu>
7
 
8
SMVideoZoomWidget::SMVideoZoomWidget(QWidget *parent) : QGraphicsView(parent)
9
{
10
    scene = new QGraphicsScene(this);
11
    item = new QGraphicsPixmapItem;
12
 
13
    scene->addItem(item);
14
 
15
    setScene(scene);
16
 
17
    // Causes weird scroll bars
18
    //this->setStyleSheet("background: transparent");
19
 
20
    setContextMenuPolicy(Qt::CustomContextMenu);
21
    connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint&)));
22
 
23
}
24
 
25
 
26
static QImage cvMat2qImage(cv::Mat mat){
27
 
28
    // 8-bits unsigned, raw bayer image
29
    if(mat.type()==CV_8UC1) {
30
        cv::Mat rgb;
31
        cv::cvtColor(mat, rgb, CV_BayerBG2RGB);
32
        QImage img((const uchar*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_RGB888);
33
        img = img.copy();
34
        return img;
35
    // 8-bit unsigned rgb image
36
    } else if(mat.type()==CV_8UC3) {
37
        // Copy input Mat
38
        QImage img((const uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
39
        return img;
40
    // 16-bit unsigned, raw bayer image
41
    } else if(mat.type()==CV_16UC1) {
42
        cv::Mat mat8bit = mat.clone();
43
        cvtools::rshift(mat8bit, 8);
44
        mat8bit.convertTo(mat8bit, CV_8UC1);
45
        cv::Mat rgb;
46
        cv::cvtColor(mat8bit, rgb, CV_BayerBG2RGB);
47
        QImage img((const uchar*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_RGB888);
48
        img = img.copy();
49
        return img;
50
    // 16-bit unsigned rgb image
51
    } else if(mat.type()==CV_16UC3) {
52
        mat.convertTo(mat, CV_8UC3, 1.0/256.0);
53
        return cvMat2qImage(mat);
54
    // 32bit floating point gray-scale image
55
    } else if(mat.type()==CV_32FC1) {
56
        cv::Mat rgb(mat.size(), CV_32FC3);
57
        cv::cvtColor(mat, rgb, cv::COLOR_GRAY2RGB);
58
        QImage img((const uchar*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_RGB32);
59
        img = img.copy();
60
        return img;
61
    } else {
62
        std::cerr << "SMVideoWidget: cv::Mat could not be converted to QImage!";
63
        return QImage();
64
    }
65
 
66
    // 8-bit unsigned gray-scale image
67
//    } else if(mat.type()==CV_8UC1) {
68
//        // Set the color table (used to tranMVate colour indexes to qRgb values)
69
//        QVector<QRgb> colorTable;
70
//        for (int i=0; i<256; i++)
71
//            colorTable.push_back(qRgb(i,i,i));
72
//        // Copy input Mat
73
//        QImage img((const uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
74
//        img.setColorTable(colorTable);
75
//        return img;
76
}
77
 
78
 
79
void SMVideoZoomWidget::showImageCV(cv::Mat image){
80
 
228 jakw 81
    if(image.empty())
82
        return;
83
 
201 jakw 84
    QImage qimage = cvMat2qImage(image);
85
 
86
    // If first ever camera image, fit in view
87
    if(item->pixmap().isNull()){
88
        item->setPixmap(QPixmap::fromImage(qimage));
89
        this->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
90
    } else {
91
        item->setPixmap(QPixmap::fromImage(qimage));
92
    }
93
 
94
}
95
 
96
 
97
void SMVideoZoomWidget::mousePressEvent(QMouseEvent *event)
98
{
99
//    // Drag mode : change the cursor's shape
100
//    if (event->button() == Qt::LeftButton)
101
//        this->setDragMode(QGraphicsView::ScrollHandDrag);
102
 
103
    QGraphicsView::mousePressEvent(event);
104
}
105
 
106
// Called when a mouse button is pressed
107
void SMVideoZoomWidget::mouseReleaseEvent(QMouseEvent *event)
108
{
109
//    // Exit drag mode : change the cursor's shape
110
//    if (event->button() == Qt::LeftButton) this->setDragMode(QGraphicsView::NoDrag);
111
    QGraphicsView::mouseReleaseEvent(event);
112
}
113
 
114
 
115
void SMVideoZoomWidget::wheelEvent(QWheelEvent *event)
116
{
117
    // When zooming, the view stay centered over the mouse
118
    this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
119
 
120
    double factor = 1.3;
121
    if(event->delta() > 0)
122
        // Zoom in
123
        scale(factor, factor);
124
    else
125
        // Zooming out
126
        scale(1.0 / factor, 1.0 / factor);
127
 
128
    // The event is processed
129
    event->accept();
130
 
131
    if(this->horizontalScrollBar()->isVisible() || this->verticalScrollBar()->isVisible())
132
        this->setDragMode(QGraphicsView::ScrollHandDrag);
133
    else
134
        this->setDragMode(QGraphicsView::NoDrag);
135
}
136
 
137
// Overloaded functionthat catch the resize event
138
void SMVideoZoomWidget::resizeEvent(QResizeEvent *event)
139
{
140
//    // First call, the scene is created
141
//    if(event->oldSize().width() == -1 || event->oldSize().height() == -1) return;
142
 
143
//    // Get the previous rectangle of the scene in the viewport
144
//    QPointF P1=mapToScene(QPoint(0,0));
145
//    QPointF P2=mapToScene(QPoint(event->oldSize().width(),event->oldSize().height()));
146
 
147
//    // Stretch the rectangle around the scene
148
//    if (P1.x()<0) P1.setX(0);
149
//    if (P1.y()<0) P1.setY(0);
150
//    if (P2.x()>scene->width()) P2.setX(scene->width());
151
//    if (P2.y()>scene->height()) P2.setY(scene->height());
152
 
153
//    // Fit the previous area in the scene
154
//    this->fitInView(QRect(P1.toPoint(),P2.toPoint()),Qt::KeepAspectRatio);
155
 
156
    if(this->horizontalScrollBar()->isVisible() || this->verticalScrollBar()->isVisible())
157
        this->setDragMode(QGraphicsView::ScrollHandDrag);
158
    else
159
        this->setDragMode(QGraphicsView::NoDrag);
160
 
161
    QGraphicsView::resizeEvent(event);
162
}
163
 
164
// Display contextual menu
165
void SMVideoZoomWidget::showContextMenu(const QPoint &pos)
166
{
167
    // Get the mouse position in the scene
168
    QPoint globalPos = mapToGlobal(pos);
169
    // Create the menu and add action
170
    QMenu contextMenu;
171
    contextMenu.addAction("Reset view", this, SLOT(fitImage()));
172
    // Display the menu
173
    contextMenu.exec(globalPos);
174
}
175
 
176
void SMVideoZoomWidget::fitImage(){
177
 
178
    // Turn scroll bars off temporarily to get correct widget size
179
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
180
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
181
 
182
    this->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
183
 
184
    // Restore scroll bar policy
185
    setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
186
    setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
187
}
188
 
189
SMVideoZoomWidget::~SMVideoZoomWidget(){
220 flgw 190
    if(item) delete item;
191
    if(scene) delete scene;
201 jakw 192
}