kwave  18.07.70
ImageView.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ImageView.cpp - simple widget class for displaying a QImage
3  -------------------
4  begin : 1999
5  copyright : (C) 1999 by Martin Wilz
6  email : Martin Wilz <mwilz@ernie.mi.uni-koeln.de>
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <QCursor>
21 #include <QImage>
22 #include <QMouseEvent>
23 
24 #include "libgui/ImageView.h"
25 #include "libkwave/Utils.h"
26 
27 //****************************************************************************
28 Kwave::ImageView::ImageView(QWidget *parent, bool fit_width, bool fit_height)
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 }
36 
37 //****************************************************************************
39 {
40 }
41 
42 //****************************************************************************
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 }
67 
68 //****************************************************************************
70 {
71  mouseMoveEvent(e);
72 }
73 
74 //****************************************************************************
75 void Kwave::ImageView::setImage(QImage image)
76 {
77  m_image = image;
78  update();
79 }
80 
81 //****************************************************************************
83 {
84  return QRect(m_offset.x(), m_offset.y(),
85  m_image.width(), m_image.height());
86 }
87 
88 //****************************************************************************
90 {
91  if (m_offset.x() != offset) {
92  m_offset.setX(offset);
93  repaint();
94  }
95 }
96 
97 //****************************************************************************
99 {
100  if (m_offset.y() != offset) {
101  m_offset.setY(offset);
102  repaint();
103  }
104 }
105 
106 //****************************************************************************
107 void Kwave::ImageView::paintEvent(QPaintEvent *)
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 }
136 
137 //***************************************************************************
138 //***************************************************************************
void sigCursorPos(const QPoint pos)
double m_scale_x
Definition: ImageView.h:118
virtual ~ImageView() Q_DECL_OVERRIDE
Definition: ImageView.cpp:38
void setHorizOffset(int offset)
Definition: ImageView.cpp:89
QPoint m_offset
Definition: ImageView.h:95
ImageView(QWidget *parent=Q_NULLPTR, bool fit_width=true, bool fit_height=true)
Definition: ImageView.cpp:28
virtual void mouseMoveEvent(QMouseEvent *e) Q_DECL_OVERRIDE
Definition: ImageView.cpp:43
int toInt(T x)
Definition: Utils.h:127
virtual void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE
Definition: ImageView.cpp:69
virtual void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE
Definition: ImageView.cpp:107
void setVertOffset(int offset)
Definition: ImageView.cpp:98
void setImage(QImage image)
Definition: ImageView.cpp:75
QRect imageRect()
Definition: ImageView.cpp:82
double m_scale_y
Definition: ImageView.h:125