-
Notifications
You must be signed in to change notification settings - Fork 0
/
addqadialog.cpp
137 lines (110 loc) · 3.67 KB
/
addqadialog.cpp
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
#include "addqadialog.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QStandardPaths>
#include <QBuffer>
#include <QRadioButton>
#include <QGroupBox>
#include <QSplitter>
#include <QDebug>
#include <QKeyEvent>
#include <QShortcut>
#include <QApplication>
#include <QMimeData>
#include <QClipboard>
AddQADialog::AddQADialog(QWidget *parent, Qt::WindowFlags f)
: QDialog(parent, f)
, image_byte_array(nullptr)
{
QLabel *line_header = new QLabel("Текст вопроса или ответа:");
line = new QLineEdit;
line->setClearButtonEnabled(true);
QPushButton *pb_ok = new QPushButton("Ок");
pb_ok->setDefault(true);
connect(pb_ok, SIGNAL(clicked()), this, SLOT(accept()));
QPushButton *pb_cancel = new QPushButton("Отмена");
connect(pb_cancel, SIGNAL(clicked()), this, SLOT(reject()));
QPushButton *pb_add_image = new QPushButton("Добавить изображение");
connect(pb_add_image, SIGNAL(clicked()), this, SLOT(slotAddImage()));
QHBoxLayout *hbx_layuot = new QHBoxLayout;
hbx_layuot->addWidget(pb_ok);
hbx_layuot->addWidget(pb_cancel);
QVBoxLayout *vbx_layuot = new QVBoxLayout;
vbx_layuot->addStretch(1);
// vbx_layuot->addWidget(gbx_rb);
vbx_layuot->addWidget(line_header);
vbx_layuot->addWidget(line);
vbx_layuot->addWidget(pb_add_image);
vbx_layuot->addLayout(hbx_layuot);
QGroupBox *gbx_vbx = new QGroupBox;
gbx_vbx->setLayout(vbx_layuot);
label = new QLabel("Чтобы добавить картинку\nнажмите на этот текст,\nа затем Ctrl+V\n\nили на кнопку\n\"Добавить изображение\"");
label->setMinimumSize(200, 200);
label->setAlignment(Qt::AlignCenter);
QHBoxLayout *hbx_layout = new QHBoxLayout(this);
hbx_layout->addWidget(gbx_vbx);
hbx_layout->addWidget(label);
QShortcut *sc = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_V), this);
connect(sc, SIGNAL(activated()), this, SLOT(slotCtrlV()));
setFocusPolicy(Qt::ClickFocus);
}
void AddQADialog::slotAddImage()
{
QStringList desktop_path = QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
image_path = QFileDialog::getOpenFileName(this, "Открыть изображение", desktop_path.first(), "Image Files (*.jpg *.png *.bmp *.jpeg)");
if (image_path.isEmpty())
return;
QPixmap pix(image_path);
if (saveToBuffer(pix))
setLabel(pix);
}
void AddQADialog::slotCtrlV()
{
qDebug() << "Ctrl+V pressed!";
const QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData();
if (mimeData->hasImage()) {
QPixmap pix = qvariant_cast<QPixmap>(mimeData->imageData());
if (saveToBuffer(pix))
setLabel(pix);
}
}
bool AddQADialog::saveToBuffer(QPixmap &pix)
{
QByteArray in_byte_array;
QBuffer in_buffer(&in_byte_array);
in_buffer.open(QIODevice::WriteOnly);
if (pix.save(&in_buffer, "PNG")) {
image_byte_array = new QByteArray(in_byte_array);
return true;
}
return false;
}
void AddQADialog::setLabel(QPixmap &pix)
{
if (pix.height() > pix.width()) {
pix = pix.scaledToHeight(200, Qt::SmoothTransformation);
} else {
pix = pix.scaledToWidth(200, Qt::SmoothTransformation);
}
label->setPixmap(pix);
}
QString AddQADialog::getImagePath()
{
return image_path;
}
QString AddQADialog::getQA()
{
return line->text();
}
QByteArray *AddQADialog::getImageByteArray()
{
return image_byte_array;
}
AddQADialog::~AddQADialog()
{
if (image_byte_array)
delete image_byte_array;
}