kwave  18.07.70
Kwave::ImageView Class Reference

#include <ImageView.h>

Inheritance diagram for Kwave::ImageView:
Inheritance graph
Collaboration diagram for Kwave::ImageView:
Collaboration graph

Public Slots

void setImage (QImage image)
 
void setHorizOffset (int offset)
 
void setVertOffset (int offset)
 

Signals

void viewInfo (int, int, int)
 
void sigCursorPos (const QPoint pos)
 

Public Member Functions

 ImageView (QWidget *parent=Q_NULLPTR, bool fit_width=true, bool fit_height=true)
 
virtual ~ImageView () Q_DECL_OVERRIDE
 
QRect imageRect ()
 

Protected Member Functions

virtual void mouseMoveEvent (QMouseEvent *e) Q_DECL_OVERRIDE
 
virtual void mousePressEvent (QMouseEvent *e) Q_DECL_OVERRIDE
 
virtual void paintEvent (QPaintEvent *) Q_DECL_OVERRIDE
 

Private Attributes

QPoint m_offset
 
QRect m_last_rect
 
QImage m_image
 
bool m_fit_width
 
bool m_fit_height
 
double m_scale_x
 
double m_scale_y
 

Detailed Description

Simple widget class for displaying a QImage.

Definition at line 33 of file ImageView.h.

Constructor & Destructor Documentation

◆ ImageView()

Kwave::ImageView::ImageView ( QWidget *  parent = Q_NULLPTR,
bool  fit_width = true,
bool  fit_height = true 
)
explicit

Constructor.

Parameters
parentpointer to the parent widget, can be 0
fit_widthif set to true, the image will be scaled to fit horizontally into the widget; if false the image will be cut off at the edge and should be scrolled
fit_heightif set to true, the image will be scaled to fit vertically into the widget; if false the image will be cut off at the edge and should be scrolled

Definition at line 28 of file ImageView.cpp.

29  :QWidget(parent), m_offset(0,0), m_last_rect(0,0,0,0),
30  m_image(), m_fit_width(fit_width), m_fit_height(fit_height),
31  m_scale_x(1.0), m_scale_y(1.0)
32 {
33  setCursor(Qt::CrossCursor);
34  setMouseTracking(true);
35 }
double m_scale_x
Definition: ImageView.h:118
QPoint m_offset
Definition: ImageView.h:95
double m_scale_y
Definition: ImageView.h:125

◆ ~ImageView()

Kwave::ImageView::~ImageView ( )
virtual

Destructor

Definition at line 38 of file ImageView.cpp.

39 {
40 }

Member Function Documentation

◆ imageRect()

QRect Kwave::ImageView::imageRect ( )

Returns the position and size of the current image, packed into a QRect object.

Returns
rectangle with the image coordinates and size

Definition at line 82 of file ImageView.cpp.

References m_image, and m_offset.

Referenced by paintEvent().

83 {
84  return QRect(m_offset.x(), m_offset.y(),
85  m_image.width(), m_image.height());
86 }
QPoint m_offset
Definition: ImageView.h:95
Here is the caller graph for this function:

◆ mouseMoveEvent()

void Kwave::ImageView::mouseMoveEvent ( QMouseEvent *  e)
protectedvirtual

Reimplemented in Kwave::OverViewWidget.

Definition at line 43 of file ImageView.cpp.

References m_image, m_offset, m_scale_x, m_scale_y, sigCursorPos(), and Kwave::toInt().

Referenced by mousePressEvent().

44 {
45  Q_ASSERT(e);
46  if (!e) return;
47 
48  if (!m_image.width() || !m_image.height())
49  return; // image not yet loaded
50 
51  int x = e->pos().x();
52  int y = e->pos().y();
53 
54  if ((x > width()) || (x < 0)) return;
55  if ((y > height()) || (y < 0)) return;
56 
57  /*
58  * re-transform the coordinates from the screen (pixmap) coordinates to
59  * the original image coordinates and emit a cursor position signal
60  */
61  x = m_offset.x() + Kwave::toInt((!qFuzzyIsNull(m_scale_x)) ?
62  (static_cast<double>(x) / m_scale_x) : 0);
63  y = m_offset.y() + Kwave::toInt((!qFuzzyIsNull(m_scale_y)) ?
64  (static_cast<double>(y) / m_scale_y) : 0);
65  emit sigCursorPos(QPoint(x, y));
66 }
void sigCursorPos(const QPoint pos)
double m_scale_x
Definition: ImageView.h:118
QPoint m_offset
Definition: ImageView.h:95
int toInt(T x)
Definition: Utils.h:127
double m_scale_y
Definition: ImageView.h:125
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mousePressEvent()

void Kwave::ImageView::mousePressEvent ( QMouseEvent *  e)
protectedvirtual

Reimplemented in Kwave::OverViewWidget.

Definition at line 69 of file ImageView.cpp.

References mouseMoveEvent().

70 {
71  mouseMoveEvent(e);
72 }
virtual void mouseMoveEvent(QMouseEvent *e) Q_DECL_OVERRIDE
Definition: ImageView.cpp:43
Here is the call graph for this function:

◆ paintEvent()

void Kwave::ImageView::paintEvent ( QPaintEvent *  )
protectedvirtual

Definition at line 107 of file ImageView.cpp.

References imageRect(), m_fit_height, m_fit_width, m_image, m_last_rect, m_offset, m_scale_x, m_scale_y, and Kwave::toInt().

108 {
109  if (m_image.isNull() || !m_image.width() || !m_image.height()) return;
110 
111  QPainter p(this);
112  QMatrix matrix;
113  QPixmap newmap = QPixmap::fromImage(m_image,
114  Qt::ColorOnly | Qt::ThresholdDither | Qt::AvoidDither);
115 
116  m_scale_x = m_fit_width ? static_cast<double>(width()) /
117  static_cast<double>(m_image.width()) : 1.0;
118  m_scale_y = m_fit_height ? static_cast<double>(height()) /
119  static_cast<double>(m_image.height()) : 1.0;
120 
121  if (m_offset.x() + m_scale_x * m_image.width() > width())
122  m_offset.setX(Kwave::toInt(m_scale_x * m_image.width() - width()));
123  if (m_offset.y() + m_scale_y * m_image.height() > height())
124  m_offset.setY(Kwave::toInt(
125  m_scale_y * m_image.height() - height()));
126 
127  matrix.scale(m_scale_x, m_scale_y);
128  QPixmap pixmap = newmap.transformed(matrix);
129 
130  p.drawPixmap(0, 0, pixmap, m_offset.x(), m_offset.y(),
131  width(), height()
132  );
133 
135 }
double m_scale_x
Definition: ImageView.h:118
QPoint m_offset
Definition: ImageView.h:95
int toInt(T x)
Definition: Utils.h:127
QRect imageRect()
Definition: ImageView.cpp:82
double m_scale_y
Definition: ImageView.h:125
Here is the call graph for this function:

◆ setHorizOffset

void Kwave::ImageView::setHorizOffset ( int  offset)
slot

sets a new horizontal offset, useful for scrolling

Definition at line 89 of file ImageView.cpp.

References m_offset.

90 {
91  if (m_offset.x() != offset) {
92  m_offset.setX(offset);
93  repaint();
94  }
95 }
QPoint m_offset
Definition: ImageView.h:95

◆ setImage

void Kwave::ImageView::setImage ( QImage  image)
slot

Sets a new QImage for displaying.

Parameters
imagea pointer to the image

Definition at line 75 of file ImageView.cpp.

References m_image.

Referenced by Kwave::OverViewWidget::OverViewWidget(), Kwave::SonagramWindow::refresh_view(), and Kwave::SonagramWindow::setOverView().

76 {
77  m_image = image;
78  update();
79 }
Here is the caller graph for this function:

◆ setVertOffset

void Kwave::ImageView::setVertOffset ( int  offset)
slot

sets a new vertical offset, useful for scrolling

Definition at line 98 of file ImageView.cpp.

References m_offset.

99 {
100  if (m_offset.y() != offset) {
101  m_offset.setY(offset);
102  repaint();
103  }
104 }
QPoint m_offset
Definition: ImageView.h:95

◆ sigCursorPos

void Kwave::ImageView::sigCursorPos ( const QPoint  pos)
signal

Referenced by mouseMoveEvent().

Here is the caller graph for this function:

◆ viewInfo

void Kwave::ImageView::viewInfo ( int  ,
int  ,
int   
)
signal

Member Data Documentation

◆ m_fit_height

bool Kwave::ImageView::m_fit_height
private

if true, scale to fit vertically

Definition at line 111 of file ImageView.h.

Referenced by paintEvent().

◆ m_fit_width

bool Kwave::ImageView::m_fit_width
private

if true, scale to fit horizontally

Definition at line 108 of file ImageView.h.

Referenced by paintEvent().

◆ m_image

QImage Kwave::ImageView::m_image
private

pointer to the QImage to be displayed

Definition at line 105 of file ImageView.h.

Referenced by imageRect(), mouseMoveEvent(), paintEvent(), and setImage().

◆ m_last_rect

QRect Kwave::ImageView::m_last_rect
private

last displayed image rectangle. Note that the left and top coordinates are unscaled, but the width and height might be scaled to screen coordinates!

Definition at line 102 of file ImageView.h.

Referenced by paintEvent().

◆ m_offset

QPoint Kwave::ImageView::m_offset
private

offset of the image, in original unscaled coordinates of the internal QImage

Definition at line 95 of file ImageView.h.

Referenced by imageRect(), mouseMoveEvent(), paintEvent(), setHorizOffset(), and setVertOffset().

◆ m_scale_x

double Kwave::ImageView::m_scale_x
private

scale factor in horizontal direction, will be (width of the image / width of the widget) if m_fit_width is true, or 1.0 else

Definition at line 118 of file ImageView.h.

Referenced by mouseMoveEvent(), and paintEvent().

◆ m_scale_y

double Kwave::ImageView::m_scale_y
private

scale factor in vertical direction, will be (height of the image / height of the widget) if m_fit_height is true, or 1.0 else

Definition at line 125 of file ImageView.h.

Referenced by mouseMoveEvent(), and paintEvent().


The documentation for this class was generated from the following files: