QTWindow.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: QTWindow.cpp
37 // Author: Pedram Azad
38 // Date: 2005
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Necessary includes
44 // ****************************************************************************
45 
46 #include "QTWindow.h"
48 #include "Image/ByteImage.h"
49 
50 #include <qpainter.h>
51 #include <qimage.h>
52 #include <qlcdnumber.h>
53 #include <qcheckbox.h>
54 
55 #if QT_VERSION >= 0x040000
56 #include <QtGui/QMouseEvent>
57 #endif
58 
59 #include <memory.h>
60 
61 
62 
63 // ****************************************************************************
64 // Constructor / Destructor
65 // ****************************************************************************
66 
67 CQTWindow::CQTWindow(int nWidth, int nHeight, CWindowEventInterface *pWindowEventInterface, QWidget *pParent) : QWidget(pParent)
68 {
69  setFixedSize(nWidth, nHeight);
70 
71  m_nImageWidth = -1;
72  m_nImageHeight = -1;
73 
74  m_pBuffer = 0;
75 
76  m_type = -1;
77  m_bMarkRectangle = pWindowEventInterface != 0;
78 
79  m_bGetRect = false;
81 
82  m_pWindowEventInterface = pWindowEventInterface;
83 }
84 
86 {
87  if (m_pBuffer)
88  delete [] m_pBuffer;
89 }
90 
91 
92 // ****************************************************************************
93 // Methods
94 // ****************************************************************************
95 
96 void CQTWindow::paintEvent(QPaintEvent *e)
97 {
98  if (m_type != -1)
99  {
100  QPainter painter(this);
101 
102  #if QT_VERSION >= 0x040000
103  QImage image(m_pBuffer, m_nImageWidth, m_nImageHeight, QImage::Format_RGB32);
104  #else
105  QImage image(m_pBuffer, m_nImageWidth, m_nImageHeight, 32, 0, 0, QImage::BigEndian);
106  #endif
107 
108  painter.drawImage(m_nImageX, m_nImageY, image);
109 
111  {
112  QPen pen(Qt::blue, 1);
113  painter.setPen(pen);
114  painter.drawRect(m_nRectX0, m_nRectY0, m_nRectX1 - m_nRectX0, m_nRectY1 - m_nRectY0);
115  }
116  }
117 }
118 
120 {
121  show();
122 }
123 
125 {
126  hide();
127 }
128 
129 void CQTWindow::DrawImage(const CByteImage *pImage, int x, int y)
130 {
131  if (m_nImageWidth != pImage->width || m_nImageHeight != pImage->height)
132  {
133  m_nImageWidth = pImage->width;
134  m_nImageHeight = pImage->height;
135 
136  if (m_pBuffer)
137  delete [] m_pBuffer;
138 
139  m_pBuffer = new unsigned char[m_nImageWidth * m_nImageHeight * 4];
140  }
141 
142  m_nImageX = x;
143  m_nImageY = y;
144 
145  if (pImage->type == CByteImage::eGrayScale)
146  {
147  const int nPixels = m_nImageWidth * m_nImageHeight;
148  unsigned char *pixels = pImage->pixels;
149  int *output = (int *) m_pBuffer;
150 
151  for (int i = 0; i < nPixels; i++)
152  output[i] = 255 << 24 | pixels[i] << 16 | pixels[i] << 8 | pixels[i];
153 
155  }
156  else if (pImage->type == CByteImage::eRGB24)
157  {
158  const int nPixels = m_nImageWidth * m_nImageHeight;
159  unsigned char *pixels = pImage->pixels;
160  int *output = (int *) m_pBuffer;
161 
162  for (int offset = 0, i = 0; i < nPixels; i++)
163  {
164  output[i] = 255 << 24 | pixels[offset] << 16 | pixels[offset + 1] << 8 | pixels[offset + 2];
165  offset += 3;
166  }
167 
169  }
170 
171  #if QT_VERSION >= 0x040000
172  repaint(x, y, m_nImageWidth, m_nImageHeight);
173  #else
174  repaint(x, y, m_nImageWidth, m_nImageHeight, false);
175  #endif
176 }
177 
178 void CQTWindow::mousePressEvent(QMouseEvent *e)
179 {
180  if (e->button() != Qt::LeftButton)
181  return;
182 
184  m_pWindowEventInterface->PointClicked(e->x(), e->y());
185 
186  if (m_bGetRect)
187  {
188  // rect is already being captured => abort
189  m_bGetRect = false;
190  return;
191  }
192 
193  m_bGetRect = true;
194 
195  m_nRectX0 = m_nRectX1 = e->x();
196  m_nRectY0 = m_nRectY1 = e->y();
197 }
198 
199 void CQTWindow::mouseMoveEvent(QMouseEvent *e)
200 {
201  if (!m_bGetRect)
202  return;
203 
204  m_nRectX1 = e->x();
205  m_nRectY1 = e->y();
206 }
207 
208 void CQTWindow::mouseReleaseEvent(QMouseEvent *e)
209 {
210  if (!m_bGetRect)
211  return;
212 
213  m_bGetRect = false;
214 
215  if (m_nRectX0 == m_nRectX1 || m_nRectY0 == m_nRectY1)
216  return;
217 
218  if (m_nRectX1 < m_nRectX0)
219  {
220  int temp = m_nRectX1;
222  m_nRectX0 = temp;
223  }
224 
225  if (m_nRectY1 < m_nRectY0)
226  {
227  int temp = m_nRectY1;
229  m_nRectY0 = temp;
230  }
231 
234 }
void mouseReleaseEvent(QMouseEvent *e)
Definition: QTWindow.cpp:208
int m_nImageHeight
Definition: QTWindow.h:98
void mousePressEvent(QMouseEvent *e)
Definition: QTWindow.cpp:178
int m_nRectY1
Definition: QTWindow.h:95
unsigned char * m_pBuffer
Definition: QTWindow.h:89
int width
The width of the image in pixels.
Definition: ByteImage.h:257
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: glext.h:3131
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
int m_nImageY
Definition: QTWindow.h:100
GLintptr offset
Definition: glext.h:3389
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
~CQTWindow()
Definition: QTWindow.cpp:85
GLenum GLint x
Definition: glext.h:3125
void paintEvent(QPaintEvent *pPaintEvent)
Definition: QTWindow.cpp:96
CQTWindow(int nWidth, int nHeight, CWindowEventInterface *pWindowEventInterface=0, QWidget *pParent=0)
Definition: QTWindow.cpp:67
int m_nRectY0
Definition: QTWindow.h:93
virtual void RectSelected(int x0, int y0, int x1, int y1)
void mouseMoveEvent(QMouseEvent *e)
Definition: QTWindow.cpp:199
void DrawImage(const CByteImage *pImage, int x=0, int y=0)
Definition: QTWindow.cpp:129
void Hide()
Definition: QTWindow.cpp:124
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: glext.h:3154
int m_nImageWidth
Definition: QTWindow.h:97
CWindowEventInterface * m_pWindowEventInterface
Definition: QTWindow.h:105
int height
The height of the image in pixels.
Definition: ByteImage.h:264
int m_nRectX0
Definition: QTWindow.h:92
GLclampf GLclampf blue
Definition: glext.h:3119
int m_nImageX
Definition: QTWindow.h:99
ImageType type
The type of the image.
Definition: ByteImage.h:292
virtual void PointClicked(int x, int y)
GLenum GLint GLint y
Definition: glext.h:3125
bool m_bMarkRectangle
Definition: QTWindow.h:103
void Show()
Definition: QTWindow.cpp:119
int m_nRectX1
Definition: QTWindow.h:94
int m_type
Definition: QTWindow.h:101
bool m_bGetRect
Definition: QTWindow.h:91


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28