Repository navigation
Expand file tree
/
Copy pathimageview.cpp
More file actions
139 lines (114 loc) · 3.34 KB
/
Copy pathimageview.cpp
File metadata and controls
139 lines (114 loc) · 3.34 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
134
135
136
137
138
139
#include "imageview.h"
#include <QDebug>
#include <QIcon>
#include <QFile>
ImageView::ImageView(QWidget *parent):
QScrollArea(parent),
imageLabel(new QLabel),
printButton(new QPushButton(this)),
cartButton(new QPushButton(this))
{
// Image Label settings
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true);
// printButton
printButton->hide();
printButton->setGeometry(20,20,50,50);
if(QFile::exists("print.png"))
{
printButton->setIcon(QIcon("print.png"));
printButton->setIconSize(QSize(40,40));
}
else
{
printButton->setText("Print");
}
connect(printButton,SIGNAL(clicked()), this, SLOT(on_printButton_clicked()));
// cartButton
cartButton->hide();
cartButton->setGeometry(20,90,50,50);
if(QFile::exists("cart.png"))
{
cartButton->setIcon(QIcon("cart.png"));
cartButton->setIconSize(QSize(40,40));
}
else
{
cartButton->setText("CART");
}
connect(cartButton,SIGNAL(clicked()), this, SLOT(on_cartButton_clicked()));
this->setBackgroundRole(QPalette::Dark);
this->setWidget(imageLabel);
this->setWidgetResizable(true);
this->setFrameShape(QFrame::NoFrame); // this removes the white border!
cutHeight=false;
}
ImageView::~ImageView()
{
delete imageLabel;
}
void ImageView::resize(QSize size)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
if(!imageLabel->pixmap(Qt::ReturnByValueConstant()).isNull()) // if no image is loaded don't do this
#else
if(imageLabel->pixmap() != NULL) // if no image is loaded don't do this
#endif
{
QSize imgSize = image.size();
if(imgSize.width() == 0){
qDebug() << "rezize with image size = 0";
return;
}
QSize tempSize = size;
if(cutHeight)
{ // modify the height to "oversize" the image --> the upper and lower borders will be cut when the image is centered in the window.
tempSize.setHeight((size.width()*imgSize.height())/imgSize.width());
}
QScrollArea::resize(imgSize.scaled(tempSize,Qt::KeepAspectRatio));
int x = size.width() - this->size().width();
int y = size.height() - this->size().height();
this->move(x/2, y/2); // center the scrolArea. (--> the image)
// buttons to on the top left corner:
int button_x = 20; // left + 20
int button_y = 0; // top -
// well we don't know the window size here :-/
}
else
{
QScrollArea::resize(size);
}
}
void ImageView::setImage(const QImage &newImage)
{
image = newImage;
imageLabel->setPixmap(QPixmap::fromImage(image));
imageLabel->adjustSize();
this->resize(this->size());
}
void ImageView::setPrintButtonVisible(bool state)
{
printButton->setVisible(state);
}
void ImageView::setCartButtonVisible(bool state)
{
cartButton->setVisible(state);
}
void ImageView::mousePressEvent(QMouseEvent *event)
{
qDebug() << "ImageView::mousePressEvent";
emit Mouse_pressed();
}
void ImageView::on_printButton_clicked()
{
emit printButton_Pressed();
}
void ImageView::on_cartButton_clicked()
{
emit cartButton_Pressed();
}
void ImageView::setGreyedOut(bool val)
{
imageLabel->setDisabled(val);
}