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
|
#include "imageviewer.h"
#include <QDesktopWidget>
#include <QPainter>
#include <QPixmap>
#include <QScrollBar>
ImageViewer::ImageViewer(QWidget *parent)
: QDialog(parent)
{
setupUi(this);
QPixmap px(32, 32);
QPainter p(&px);
p.fillRect(0, 0, 32, 32, Qt::white);
p.fillRect(0, 0, 16, 16, QColor(193, 193, 193));
p.fillRect(16, 16, 16, 16, QColor(193, 193, 193));
p.end();
QPalette pal = scrollAreaWidgetContents->palette();
pal.setBrush(QPalette::Background,
QBrush(px));
pal.setBrush(QPalette::Base,
QBrush(px));
scrollAreaWidgetContents->setPalette(pal);
}
void ImageViewer::setImage(const QImage &image)
{
m_image = image;
QPixmap px = QPixmap::fromImage(image);
imageLabel->setPixmap(px);
updateGeometry();
}
QSize ImageViewer::sizeHint() const
{
QSize size;
int vScrollWidth = scrollArea->verticalScrollBar() ?
scrollArea->verticalScrollBar()->width() : 0;
int hScrollHeight = scrollArea->horizontalScrollBar() ?
scrollArea->horizontalScrollBar()->height() : 0;
QSize optimalWindowSize(m_image.width() + vScrollWidth,
m_image.height() + hScrollHeight);
QRect screenRect = QApplication::desktop()->availableGeometry();
const float maxPercentOfDesktopSpace = 0.8;
QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(),
maxPercentOfDesktopSpace * screenRect.height());
return QSize(qMin(optimalWindowSize.width(), maxAvailableSize.width()),
qMin(optimalWindowSize.height(), maxAvailableSize.height()));
}
#include "imageviewer.moc"
|