[3e4acc]: libgui / ImageView.cpp  Maximize  Restore  History

Download this file

134 lines (110 with data), 4.6 kB

  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
/***************************************************************************
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 "config.h"
#include <QCursor>
#include <QImage>
#include <QMouseEvent>
#include "libgui/ImageView.h"
//****************************************************************************
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);
}
//****************************************************************************
ImageView::~ImageView()
{
}
//****************************************************************************
void 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() + static_cast<int>((m_scale_x != 0) ?
(static_cast<double>(x) / m_scale_x) : 0);
y = m_offset.y() + static_cast<int>((m_scale_y != 0) ?
(static_cast<double>(y) / m_scale_y) : 0);
emit sigCursorPos(QPoint(x, y));
}
//****************************************************************************
void ImageView::setImage(QImage image)
{
m_image = image;
update();
}
//****************************************************************************
QRect ImageView::imageRect()
{
return QRect(m_offset.x(), m_offset.y(),
m_image.width(), m_image.height());
}
//****************************************************************************
void ImageView::setHorizOffset(int offset)
{
if (m_offset.x() != offset) {
m_offset.setX(offset);
repaint();
}
}
//****************************************************************************
void ImageView::setVertOffset(int offset)
{
if (m_offset.y() != offset) {
m_offset.setY(offset);
repaint();
}
}
//****************************************************************************
void ImageView::paintEvent(QPaintEvent *)
{
if (m_image.isNull() || !m_image.width() || !m_image.height()) return;
QPainter p(this);
QMatrix matrix;
QPixmap newmap = QPixmap::fromImage(m_image,
Qt::ColorOnly | Qt::ThresholdDither | Qt::AvoidDither);
m_scale_x = m_fit_width ? static_cast<float>(width()) /
static_cast<float>(m_image.width()) : 1.0;
m_scale_y = m_fit_height ? static_cast<float>(height()) /
static_cast<float>(m_image.height()) : 1.0;
if (m_offset.x() + m_scale_x * m_image.width() > width())
m_offset.setX(static_cast<int>(m_scale_x * m_image.width() - width()));
if (m_offset.y() + m_scale_y * m_image.height() > height())
m_offset.setY(static_cast<int>(
m_scale_y * m_image.height() - height()));
matrix.scale(m_scale_x, m_scale_y);
QPixmap pixmap = newmap.transformed(matrix);
p.drawPixmap(0, 0, pixmap, m_offset.x(), m_offset.y(),
width(), height()
);
m_last_rect = imageRect();
}
//***************************************************************************
#include "ImageView.moc"
//***************************************************************************
//***************************************************************************