-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenshot.cpp
More file actions
151 lines (111 loc) · 4.48 KB
/
Copy pathscreenshot.cpp
File metadata and controls
151 lines (111 loc) · 4.48 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
140
141
142
143
144
145
146
147
148
149
150
151
// ===========================================================================
/// <summary>
/// screenshot.cpp
/// QtIntroduction
/// created by Mehrdad Soleimanimajd on 17.09.2019
/// </summary>
/// <created>ʆϒʅ, 17.09.2019</created>
/// <changed>ʆϒʅ, 27.06.2023</changed>
// ===========================================================================
#include "screenshot.h"
ScreenShot::ScreenShot ( QWidget* mainWindow, AppStyle* styleObj )
: appStyle ( styleObj ), initialized ( false )
{
this->setParent ( mainWindow, Qt::Window );
lableScrShot = new QLabel ( this );
lableScrShot->setSizePolicy ( QSizePolicy::Expanding, QSizePolicy::Expanding );
lableScrShot->setAlignment ( Qt::AlignCenter );
const QRect screenGeo = QApplication::primaryScreen ()->geometry ();
lableScrShot->setMinimumSize ( screenGeo.width () / 8, screenGeo.height () / 8 );
QVBoxLayout* layoutWindow = new QVBoxLayout ( this );
layoutWindow->setContentsMargins ( 5, 5, 5, 5 );
layoutWindow->addWidget ( lableScrShot );
optionsGroupBox = new QGroupBox ( tr ( "Options" ), this );
QGridLayout* layoutOptionsGroupBox = new QGridLayout ( optionsGroupBox );
buttonNewScrShot = new QPushButton ( tr ( "New Screenshot" ), this );
connect ( buttonNewScrShot, &QPushButton::clicked, this, &ScreenShot::shootScrShot );
layoutOptionsGroupBox->addWidget ( buttonNewScrShot, 0, 0 );
buttonSaveScrShot = new QPushButton ( tr ( "Save Screenshot" ), this );
connect ( buttonSaveScrShot, &QPushButton::clicked, this, &ScreenShot::saveScrShot );
layoutOptionsGroupBox->addWidget ( buttonSaveScrShot, 0, 1 );
layoutWindow->addWidget ( optionsGroupBox );
this->setLayout ( layoutWindow );
shootScrShot ();
setWindowTitle ( tr ( "Screenshot" ) );
setMinimumSize ( 240, 160 );
resize ( 240, 160 );
this->setStyleSheet ( appStyle->theme.form );
this->optionsGroupBox->setStyleSheet ( appStyle->theme.menu );
initialized = true;
};
//ScreenShot::~ScreenShot ( void )
//{
//
//};
const bool ScreenShot::getInitialized ( void )
{
return initialized;
};
void ScreenShot::resizeEvent ( QResizeEvent* event )
{
QSize scaledSize = pixmapShot.size ();
scaledSize.scale ( lableScrShot->size (), Qt::KeepAspectRatio );
if (!lableScrShot->pixmap () || scaledSize != lableScrShot->pixmap ().size ())
{
updateScrShot ();
}
};
void ScreenShot::updateScrShot ( void )
{
lableScrShot->setPixmap ( pixmapShot.scaled ( lableScrShot->size (),
Qt::KeepAspectRatio,
Qt::SmoothTransformation ) );
};
void ScreenShot::saveScrShot ( void )
{
const QString fileFormat = "png";
QString initialPath = QStandardPaths::writableLocation ( QStandardPaths::DesktopLocation );
if (initialPath.isEmpty ())
initialPath = QDir::currentPath ();
QFileDialog fileDialog ( this, tr ( "Save As" ), initialPath );
fileDialog.setAcceptMode ( QFileDialog::AcceptSave );
fileDialog.setFileMode ( QFileDialog::AnyFile );
fileDialog.setDirectory ( initialPath );
initialPath += tr ( "/untitled." ) + fileFormat;
fileDialog.selectFile ( tr ( "/untitled." ) + fileFormat );
QStringList mimeTypes;
const QList<QByteArray>byteMimeTypes = QImageWriter::supportedMimeTypes ();
for (const QByteArray& byteEntity : byteMimeTypes)
mimeTypes.append ( QLatin1String ( byteEntity ) );
fileDialog.setMimeTypeFilters ( mimeTypes );
fileDialog.selectMimeTypeFilter ( "image/" + fileFormat );
fileDialog.setDefaultSuffix ( fileFormat );
if (fileDialog.exec () != QDialog::Accepted)
return;
const QString fileName = fileDialog.selectedFiles ().first ();
if (!pixmapShot.save ( fileName ))
{
QMessageBox::warning ( this, tr ( "Save Error" ),
tr ( "The image could not be saved to \"%1\"." )
.arg ( QDir::toNativeSeparators ( fileName ) ) );
}
};
void ScreenShot::shootScrShot ( void )
{
QScreen* screen = QGuiApplication::primaryScreen ();
if (const QWindow* window = windowHandle ())
{
screen = window->screen ();
}
if (!screen)
return;
QApplication::beep ();
pixmapShot = screen->grabWindow ( 0 );
updateScrShot ();
};
void ScreenShot::updateStyle ( void )
{
this->setStyleSheet ( appStyle->theme.form );
this->optionsGroupBox->setStyleSheet ( appStyle->theme.menu );
};