1 #include "imageviewer.h"
2 #include "pixelwidget.h"
3 #include "apisurface.h"
5 #include "image/image.hpp"
8 #include <QDesktopWidget>
13 ImageViewer::ImageViewer(QWidget *parent)
19 connect(lowerSpinBox, SIGNAL(valueChanged(double)),
21 connect(upperSpinBox, SIGNAL(valueChanged(double)),
23 connect(flipCheckBox, SIGNAL(stateChanged(int)),
25 connect(opaqueCheckBox, SIGNAL(stateChanged(int)),
27 connect(alphaCheckBox, SIGNAL(stateChanged(int)),
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));
36 QPalette pal = scrollAreaWidgetContents->palette();
37 pal.setBrush(QPalette::Background,
39 pal.setBrush(QPalette::Base,
41 scrollAreaWidgetContents->setPalette(pal);
43 m_pixelWidget = new PixelWidget(scrollAreaWidgetContents);
44 verticalLayout_2->addWidget(m_pixelWidget);
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 &)));
59 ImageViewer::~ImageViewer()
64 void ImageViewer::setBase64Data(const QByteArray &base64)
67 m_image = ApiSurface::imageFromBase64(base64);
68 m_convertedImage = ApiSurface::qimageFromRawImage(m_image);
69 m_pixelWidget->setSurface(m_convertedImage);
73 void ImageViewer::slotUpdate()
76 m_convertedImage.mirrored(false, flipCheckBox->isChecked());
78 double lowerValue = lowerSpinBox->value();
79 double upperValue = upperSpinBox->value();
81 bool opaque = opaqueCheckBox->isChecked();
82 bool alpha = alphaCheckBox->isChecked();
84 m_convertedImage = ApiSurface::qimageFromRawImage(m_image,
85 lowerValue, upperValue,
88 if (flipCheckBox->isChecked()) {
89 m_convertedImage = m_convertedImage.mirrored(false, true);
92 m_pixelWidget->setSurface(m_convertedImage);
97 QSize ImageViewer::sizeHint() const
101 int vScrollWidth = scrollArea->verticalScrollBar() ?
102 scrollArea->verticalScrollBar()->width() : 0;
103 int hScrollHeight = scrollArea->horizontalScrollBar() ?
104 scrollArea->horizontalScrollBar()->height() : 0;
105 QSize optimalWindowSize(m_convertedImage.width() + vScrollWidth,
106 m_convertedImage.height() + hScrollHeight);
108 QRect screenRect = QApplication::desktop()->availableGeometry();
109 const float maxPercentOfDesktopSpace = 0.8f;
110 QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(),
111 maxPercentOfDesktopSpace * screenRect.height());
113 return QSize(qMin(optimalWindowSize.width(), maxAvailableSize.width()),
114 qMin(optimalWindowSize.height(), maxAvailableSize.height()));
117 void ImageViewer::resizeEvent(QResizeEvent *e)
119 QWidget::resizeEvent(e);
123 QString createPixelLabel(image::Image *img, int x, int y)
126 unsigned char *pixelLocation = 0;
129 pixelLocation = img->pixels + img->stride() * y;
130 pixelLocation += x * img->bytesPerPixel;
131 pixel = ((T*)pixelLocation);
133 pixelLabel += QLatin1String("[");
134 pixelLabel += QString::fromLatin1("%1").arg(pixel[0]);
136 for (int channel = 1; channel < img->channels; ++channel) {
137 pixelLabel += QString::fromLatin1(", %1").arg(pixel[channel]);
139 pixelLabel += QLatin1String("]");
144 void ImageViewer::showPixel(int x, int y)
146 xSpinBox->setValue(x);
147 ySpinBox->setValue(y);
152 QString label = tr("Pixel: ");
154 /* If the image is flipped, substitute y to match */
155 if (flipCheckBox->isChecked()) {
156 y = m_convertedImage.height() - y - 1;
159 if (m_image->channelType == image::TYPE_UNORM8) {
160 label += createPixelLabel<unsigned char>(m_image, x, y);
162 label += createPixelLabel<float>(m_image, x, y);
165 pixelLabel->setText(label);
169 void ImageViewer::showGrid(const QRect &rect)
171 if (rect.isEmpty()) {
175 rectLabel->setText(tr("Grid: [%1, %2, %3, %4]")
179 .arg(rect.height()));
183 #include "imageviewer.moc"