File: ImageView.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (140 lines) | stat: -rw-r--r-- 4,858 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/***************************************************************************
           ImageView.cpp  -  simple widget class for displaying a QImage
                             -------------------
    begin                : 1999
    copyright            : (C) 1999 by Martin Wilz
    email                : Martin Wilz <mwilz@ernie.mi.uni-koeln.de>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#include <QCursor>
#include <QMouseEvent>
#include <QTransform>

#include "libgui/ImageView.h"
#include "libkwave/Utils.h"

//****************************************************************************
Kwave::ImageView::ImageView(QWidget *parent, bool fit_width, bool fit_height)
    :QWidget(parent), m_offset(0,0), m_last_rect(0,0,0,0),
     m_image(), m_fit_width(fit_width), m_fit_height(fit_height),
     m_scale_x(1.0), m_scale_y(1.0)
{
    setCursor(Qt::CrossCursor);
    setMouseTracking(true);
}

//****************************************************************************
Kwave::ImageView::~ImageView()
{
}

//****************************************************************************
void Kwave::ImageView::mouseMoveEvent(QMouseEvent *e)
{
    Q_ASSERT(e);
    if (!e) return;

    if (!m_image.width() || !m_image.height())
        return; // image not yet loaded

    int x = e->pos().x();
    int y = e->pos().y();

    if ((x > width()) || (x < 0)) return;
    if ((y > height()) || (y < 0)) return;

    /*
     * re-transform the coordinates from the screen (pixmap) coordinates to
     * the original image coordinates and emit a cursor position signal
     */
    x = m_offset.x() + Kwave::toInt((!qFuzzyIsNull(m_scale_x)) ?
        (static_cast<double>(x) / m_scale_x) : 0);
    y = m_offset.y() + Kwave::toInt((!qFuzzyIsNull(m_scale_y)) ?
        (static_cast<double>(y) / m_scale_y) : 0);
    emit sigCursorPos(QPoint(x, y));
}

//****************************************************************************
void Kwave::ImageView::mousePressEvent(QMouseEvent *e)
{
    mouseMoveEvent(e);
}

//****************************************************************************
void Kwave::ImageView::setImage(QImage image)
{
    m_image = image;
    update();
}

//****************************************************************************
QRect Kwave::ImageView::imageRect()
{
    return QRect(m_offset.x(), m_offset.y(),
                 m_image.width(), m_image.height());
}

//****************************************************************************
void Kwave::ImageView::setHorizOffset(int offset)
{
    if (m_offset.x() != offset) {
        m_offset.setX(offset);
        repaint();
    }
}

//****************************************************************************
void Kwave::ImageView::setVertOffset(int offset)
{
    if (m_offset.y() != offset) {
        m_offset.setY(offset);
        repaint();
    }
}

//****************************************************************************
void Kwave::ImageView::paintEvent(QPaintEvent *)
{
    if (m_image.isNull() || !m_image.width() || !m_image.height()) return;

    QPainter p(this);
    QPixmap newmap = QPixmap::fromImage(m_image,
        Qt::ColorOnly | Qt::ThresholdDither | Qt::AvoidDither);

    m_scale_x = m_fit_width  ? static_cast<double>(width())  /
                               static_cast<double>(m_image.width()) : 1.0;
    m_scale_y = m_fit_height ? static_cast<double>(height()) /
                               static_cast<double>(m_image.height()) : 1.0;

    if (m_offset.x() + m_scale_x * m_image.width() > width())
        m_offset.setX(Kwave::toInt(m_scale_x * m_image.width() - width()));
    if (m_offset.y() + m_scale_y * m_image.height() > height())
        m_offset.setY(Kwave::toInt(
            m_scale_y * m_image.height() - height()));

    QPixmap pixmap = newmap.transformed(
        QTransform::fromScale(m_scale_x, m_scale_y),
        Qt::FastTransformation
    );

    p.drawPixmap(0, 0, pixmap, m_offset.x(), m_offset.y(),
        width(), height()
    );

    m_last_rect = imageRect();
}

//***************************************************************************
//***************************************************************************

#include "moc_ImageView.cpp"