blob: 4c3f2bdc11ecf786dae4bf0ed1f219b494824b94 [file] [log] [blame]
Zack Rusin5cb6b872011-04-10 02:19:59 -04001#include "imageviewer.h"
Zack Rusin66ce10a2013-09-10 20:30:59 -04002#include "pixelwidget.h"
Zack Rusina69f0de2013-09-12 17:21:51 -04003#include "apisurface.h"
4
5#include "image/image.hpp"
Zack Rusin5cb6b872011-04-10 02:19:59 -04006
José Fonsecad562b8e2011-11-25 15:51:09 +00007#include <QDebug>
Zack Rusin2ffe9f82011-09-23 20:25:47 -04008#include <QDesktopWidget>
Zack Rusin5cb6b872011-04-10 02:19:59 -04009#include <QPainter>
10#include <QPixmap>
Zack Rusin2ffe9f82011-09-23 20:25:47 -040011#include <QScrollBar>
Zack Rusin5cb6b872011-04-10 02:19:59 -040012
13ImageViewer::ImageViewer(QWidget *parent)
Zack Rusina69f0de2013-09-12 17:21:51 -040014 : QDialog(parent),
15 m_image(0)
Zack Rusin5cb6b872011-04-10 02:19:59 -040016{
17 setupUi(this);
18
José Fonsecad562b8e2011-11-25 15:51:09 +000019 connect(lowerSpinBox, SIGNAL(valueChanged(double)),
20 SLOT(slotUpdate()));
21 connect(upperSpinBox, SIGNAL(valueChanged(double)),
22 SLOT(slotUpdate()));
23 connect(flipCheckBox, SIGNAL(stateChanged(int)),
24 SLOT(slotUpdate()));
José Fonsecafe13f772012-03-21 08:49:40 +000025 connect(opaqueCheckBox, SIGNAL(stateChanged(int)),
26 SLOT(slotUpdate()));
gregory8f0aa382012-07-01 17:18:04 +020027 connect(alphaCheckBox, SIGNAL(stateChanged(int)),
28 SLOT(slotUpdate()));
José Fonsecad562b8e2011-11-25 15:51:09 +000029
Zack Rusin5cb6b872011-04-10 02:19:59 -040030 QPixmap px(32, 32);
31 QPainter p(&px);
32 p.fillRect(0, 0, 32, 32, Qt::white);
33 p.fillRect(0, 0, 16, 16, QColor(193, 193, 193));
34 p.fillRect(16, 16, 16, 16, QColor(193, 193, 193));
35 p.end();
36 QPalette pal = scrollAreaWidgetContents->palette();
37 pal.setBrush(QPalette::Background,
38 QBrush(px));
39 pal.setBrush(QPalette::Base,
40 QBrush(px));
41 scrollAreaWidgetContents->setPalette(pal);
Zack Rusin66ce10a2013-09-10 20:30:59 -040042
43 m_pixelWidget = new PixelWidget(scrollAreaWidgetContents);
44 verticalLayout_2->addWidget(m_pixelWidget);
45
46 rectLabel->hide();
47 pixelLabel->hide();
48
49 connect(m_pixelWidget, SIGNAL(zoomChanged(int)),
50 zoomSpinBox, SLOT(setValue(int)));
51 connect(zoomSpinBox, SIGNAL(valueChanged(int)),
52 m_pixelWidget, SLOT(setZoom(int)));
53 connect(m_pixelWidget, SIGNAL(mousePosition(int, int)),
54 this, SLOT(showPixel(int, int)));
55 connect(m_pixelWidget, SIGNAL(gridGeometry(const QRect &)),
56 this, SLOT(showGrid(const QRect &)));
Zack Rusin5cb6b872011-04-10 02:19:59 -040057}
58
Zack Rusina69f0de2013-09-12 17:21:51 -040059ImageViewer::~ImageViewer()
Zack Rusin5cb6b872011-04-10 02:19:59 -040060{
Zack Rusina69f0de2013-09-12 17:21:51 -040061 delete m_image;
62}
63
64void ImageViewer::setBase64Data(const QByteArray &base64)
65{
66 delete m_image;
67 m_image = ApiSurface::imageFromBase64(base64);
68 m_convertedImage = ApiSurface::qimageFromRawImage(m_image);
69 m_pixelWidget->setSurface(m_convertedImage);
Zack Rusin2ffe9f82011-09-23 20:25:47 -040070 updateGeometry();
71}
72
José Fonsecad562b8e2011-11-25 15:51:09 +000073void ImageViewer::slotUpdate()
74{
Zack Rusina69f0de2013-09-12 17:21:51 -040075 m_convertedImage =
76 m_convertedImage.mirrored(false, flipCheckBox->isChecked());
José Fonsecad562b8e2011-11-25 15:51:09 +000077
78 double lowerValue = lowerSpinBox->value();
79 double upperValue = upperSpinBox->value();
80
José Fonsecafe13f772012-03-21 08:49:40 +000081 bool opaque = opaqueCheckBox->isChecked();
gregory8f0aa382012-07-01 17:18:04 +020082 bool alpha = alphaCheckBox->isChecked();
José Fonsecafe13f772012-03-21 08:49:40 +000083
José Fonseca36509be2013-09-17 15:22:50 +010084 m_convertedImage = ApiSurface::qimageFromRawImage(m_image,
85 lowerValue, upperValue,
86 opaque, alpha);
José Fonsecad562b8e2011-11-25 15:51:09 +000087
José Fonseca36509be2013-09-17 15:22:50 +010088 if (flipCheckBox->isChecked()) {
89 m_convertedImage = m_convertedImage.mirrored(false, true);
José Fonsecad562b8e2011-11-25 15:51:09 +000090 }
91
Zack Rusina69f0de2013-09-12 17:21:51 -040092 m_pixelWidget->setSurface(m_convertedImage);
José Fonsecad562b8e2011-11-25 15:51:09 +000093
94 updateGeometry();
95}
96
Zack Rusin2ffe9f82011-09-23 20:25:47 -040097QSize ImageViewer::sizeHint() const
98{
99 QSize size;
100
101 int vScrollWidth = scrollArea->verticalScrollBar() ?
102 scrollArea->verticalScrollBar()->width() : 0;
103 int hScrollHeight = scrollArea->horizontalScrollBar() ?
104 scrollArea->horizontalScrollBar()->height() : 0;
Zack Rusina69f0de2013-09-12 17:21:51 -0400105 QSize optimalWindowSize(m_convertedImage.width() + vScrollWidth,
106 m_convertedImage.height() + hScrollHeight);
Zack Rusin2ffe9f82011-09-23 20:25:47 -0400107
108 QRect screenRect = QApplication::desktop()->availableGeometry();
José Fonseca5d7fc052012-11-13 19:53:47 +0000109 const float maxPercentOfDesktopSpace = 0.8f;
Zack Rusin2ffe9f82011-09-23 20:25:47 -0400110 QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(),
111 maxPercentOfDesktopSpace * screenRect.height());
112
113 return QSize(qMin(optimalWindowSize.width(), maxAvailableSize.width()),
114 qMin(optimalWindowSize.height(), maxAvailableSize.height()));
Zack Rusin5cb6b872011-04-10 02:19:59 -0400115}
116
Zack Rusin66ce10a2013-09-10 20:30:59 -0400117void ImageViewer::resizeEvent(QResizeEvent *e)
118{
119 QWidget::resizeEvent(e);
120}
121
Zack Rusina69f0de2013-09-12 17:21:51 -0400122template <class T>
123QString createPixelLabel(image::Image *img, int x, int y)
124{
125 QString pixelLabel;
126 unsigned char *pixelLocation = 0;
127 T *pixel;
128
129 pixelLocation = img->pixels + img->stride() * y;
130 pixelLocation += x * img->bytesPerPixel;
131 pixel = ((T*)pixelLocation);
132
133 pixelLabel += QLatin1String("[");
José Fonseca8c3fa762014-02-28 14:20:26 +0000134 pixelLabel += QString::fromLatin1("%1").arg((double)pixel[0], 0, 'g', 9);
Zack Rusina69f0de2013-09-12 17:21:51 -0400135
136 for (int channel = 1; channel < img->channels; ++channel) {
José Fonseca8c3fa762014-02-28 14:20:26 +0000137 pixelLabel += QString::fromLatin1(", %1").arg((double)pixel[channel], 0, 'g', 9);
Zack Rusina69f0de2013-09-12 17:21:51 -0400138 }
139 pixelLabel += QLatin1String("]");
140
141 return pixelLabel;
142}
143
Zack Rusin66ce10a2013-09-10 20:30:59 -0400144void ImageViewer::showPixel(int x, int y)
145{
146 xSpinBox->setValue(x);
147 ySpinBox->setValue(y);
Zack Rusina69f0de2013-09-12 17:21:51 -0400148
149 if (!m_image)
150 return;
151
152 QString label = tr("Pixel: ");
153
Zack Rusin7ec54f92013-10-11 18:32:25 -0400154 /* If the image is flipped, substitute y to match */
155 if (flipCheckBox->isChecked()) {
156 y = m_convertedImage.height() - y - 1;
157 }
158
Zack Rusina69f0de2013-09-12 17:21:51 -0400159 if (m_image->channelType == image::TYPE_UNORM8) {
160 label += createPixelLabel<unsigned char>(m_image, x, y);
161 } else {
162 label += createPixelLabel<float>(m_image, x, y);
163 }
164
165 pixelLabel->setText(label);
Zack Rusin66ce10a2013-09-10 20:30:59 -0400166 pixelLabel->show();
167}
168
169void ImageViewer::showGrid(const QRect &rect)
170{
171 if (rect.isEmpty()) {
172 rectLabel->hide();
173 return;
174 }
175 rectLabel->setText(tr("Grid: [%1, %2, %3, %4]")
176 .arg(rect.x())
177 .arg(rect.y())
178 .arg(rect.width())
179 .arg(rect.height()));
180 rectLabel->show();
181}
182
Zack Rusin5cb6b872011-04-10 02:19:59 -0400183#include "imageviewer.moc"