IVT
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;
80  m_nRectX0 = m_nRectY0 = m_nRectX1 = m_nRectY1 = 0;
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 
110  if (m_bMarkRectangle && m_bGetRect)
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 
154  m_type = CByteImage::eGrayScale;
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 
168  m_type = CByteImage::eRGB24;
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 
183  if (m_pWindowEventInterface)
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;
221  m_nRectX1 = m_nRectX0;
222  m_nRectX0 = temp;
223  }
224 
225  if (m_nRectY1 < m_nRectY0)
226  {
227  int temp = m_nRectY1;
228  m_nRectY1 = m_nRectY0;
229  m_nRectY0 = temp;
230  }
231 
232  if (m_pWindowEventInterface)
233  m_pWindowEventInterface->RectSelected(m_nRectX0, m_nRectY0, m_nRectX1, m_nRectY1);
234 }
virtual void PointClicked(int x, int y)
void Hide()
Definition: QTWindow.cpp:124
void DrawImage(const CByteImage *pImage, int x=0, int y=0)
Definition: QTWindow.cpp:129
virtual void RectSelected(int x0, int y0, int x1, int y1)
ImageType type
The type of the image.
Definition: ByteImage.h:292
int width
The width of the image in pixels.
Definition: ByteImage.h:257
int height
The height of the image in pixels.
Definition: ByteImage.h:264
CQTWindow(int nWidth, int nHeight, CWindowEventInterface *pWindowEventInterface=0, QWidget *pParent=0)
Definition: QTWindow.cpp:67
~CQTWindow()
Definition: QTWindow.cpp:85
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
void Show()
Definition: QTWindow.cpp:119