Subversion Repositories seema-scanner

Rev

Rev 201 | Rev 228 | 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
 
81
    QImage qimage = cvMat2qImage(image);
82
 
83
    // If first ever camera image, fit in view
84
    if(item->pixmap().isNull()){
85
        item->setPixmap(QPixmap::fromImage(qimage));
86
        this->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
87
    } else {
88
        item->setPixmap(QPixmap::fromImage(qimage));
89
    }
90
 
91
}
92
 
93
 
94
void SMVideoZoomWidget::mousePressEvent(QMouseEvent *event)
95
{
96
//    // Drag mode : change the cursor's shape
97
//    if (event->button() == Qt::LeftButton)
98
//        this->setDragMode(QGraphicsView::ScrollHandDrag);
99
 
100
    QGraphicsView::mousePressEvent(event);
101
}
102
 
103
// Called when a mouse button is pressed
104
void SMVideoZoomWidget::mouseReleaseEvent(QMouseEvent *event)
105
{
106
//    // Exit drag mode : change the cursor's shape
107
//    if (event->button() == Qt::LeftButton) this->setDragMode(QGraphicsView::NoDrag);
108
    QGraphicsView::mouseReleaseEvent(event);
109
}
110
 
111
 
112
void SMVideoZoomWidget::wheelEvent(QWheelEvent *event)
113
{
114
    // When zooming, the view stay centered over the mouse
115
    this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
116
 
117
    double factor = 1.3;
118
    if(event->delta() > 0)
119
        // Zoom in
120
        scale(factor, factor);
121
    else
122
        // Zooming out
123
        scale(1.0 / factor, 1.0 / factor);
124
 
125
    // The event is processed
126
    event->accept();
127
 
128
    if(this->horizontalScrollBar()->isVisible() || this->verticalScrollBar()->isVisible())
129
        this->setDragMode(QGraphicsView::ScrollHandDrag);
130
    else
131
        this->setDragMode(QGraphicsView::NoDrag);
132
}
133
 
134
// Overloaded functionthat catch the resize event
135
void SMVideoZoomWidget::resizeEvent(QResizeEvent *event)
136
{
137
//    // First call, the scene is created
138
//    if(event->oldSize().width() == -1 || event->oldSize().height() == -1) return;
139
 
140
//    // Get the previous rectangle of the scene in the viewport
141
//    QPointF P1=mapToScene(QPoint(0,0));
142
//    QPointF P2=mapToScene(QPoint(event->oldSize().width(),event->oldSize().height()));
143
 
144
//    // Stretch the rectangle around the scene
145
//    if (P1.x()<0) P1.setX(0);
146
//    if (P1.y()<0) P1.setY(0);
147
//    if (P2.x()>scene->width()) P2.setX(scene->width());
148
//    if (P2.y()>scene->height()) P2.setY(scene->height());
149
 
150
//    // Fit the previous area in the scene
151
//    this->fitInView(QRect(P1.toPoint(),P2.toPoint()),Qt::KeepAspectRatio);
152
 
153
    if(this->horizontalScrollBar()->isVisible() || this->verticalScrollBar()->isVisible())
154
        this->setDragMode(QGraphicsView::ScrollHandDrag);
155
    else
156
        this->setDragMode(QGraphicsView::NoDrag);
157
 
158
    QGraphicsView::resizeEvent(event);
159
}
160
 
161
// Display contextual menu
162
void SMVideoZoomWidget::showContextMenu(const QPoint &pos)
163
{
164
    // Get the mouse position in the scene
165
    QPoint globalPos = mapToGlobal(pos);
166
    // Create the menu and add action
167
    QMenu contextMenu;
168
    contextMenu.addAction("Reset view", this, SLOT(fitImage()));
169
    // Display the menu
170
    contextMenu.exec(globalPos);
171
}
172
 
173
void SMVideoZoomWidget::fitImage(){
174
 
175
    // Turn scroll bars off temporarily to get correct widget size
176
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
177
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
178
 
179
    this->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
180
 
181
    // Restore scroll bar policy
182
    setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
183
    setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
184
}
185
 
186
SMVideoZoomWidget::~SMVideoZoomWidget(){
220 flgw 187
    if(item) delete item;
188
    if(scene) delete scene;
201 jakw 189
}