Skip to content

Commit 0ef23b7

Browse files
add screen shot code
1 parent 0e645ef commit 0ef23b7

10 files changed

+391
-4
lines changed

qt_screenshot/core/screen_list.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//
2+
// Created by wangyz38535 on 2023/10/12.
3+
//
4+
5+
#include "screen_list.h"

qt_screenshot/core/screen_list.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// Created by wangyz38535 on 2023/10/12.
3+
//
4+
5+
#ifndef FIRESHOT_SCREEN_LIST_H
6+
#define FIRESHOT_SCREEN_LIST_H
7+
8+
9+
10+
#include <QPainter>
11+
#include <QRect>
12+
13+
#include <memory>
14+
#include <QScreen>
15+
#include <QPixmap>
16+
#include <QRect>
17+
18+
struct ScreenInfo{
19+
QScreen *object;
20+
QRect boundary;
21+
QPixmap pixmap;
22+
};
23+
24+
/**
25+
* @class : ScreenList
26+
* @brief : 屏幕列表集合
27+
* @note : 屏幕列表集合基本功能
28+
*/
29+
class ScreenList
30+
{
31+
public:
32+
ScreenList(QList<ScreenInfo> list);
33+
34+
QRect allBoundary(bool isGlobal=false) const;
35+
QPixmap allPixMap() const;
36+
37+
QRect boundaryAt(int screenIndex,bool isGlobal=false);
38+
QScreen *screenAt(int screenIndex);
39+
QPixmap pixmapAt(int screenIndex);
40+
41+
QRect toLocal(QRect rect);
42+
float scale();
43+
44+
void draw(QPainter &painter);
45+
void draw(QPainter &painter,QPainterPath maskPath,QBrush maskBrush);
46+
47+
private:
48+
QRect m_allBoundary;
49+
std::shared_ptr<QPixmap> m_allPixMap;
50+
QList<ScreenInfo> m_List;
51+
52+
void initParams();
53+
};
54+
55+
56+
57+
#endif //FIRESHOT_SCREEN_LIST_H

qt_screenshot/core/screen_shot.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
//
22
// Created by wangyz38535 on 2023/10/12.
33
//
4-
4+
#include "utils/spdlog_wrapper.h"
55
#include "screen_shot.h"
6+
#include "fireshot.h"
7+
8+
ScreenShot::ScreenShot(FireShot *lpFireShot) : QObject(lpFireShot), m_lpFireShot(lpFireShot)
9+
{
10+
11+
}
12+
13+
ScreenShot::~ScreenShot() {
14+
SPD_FUNCTION();
15+
}
16+
17+
void ScreenShot::cleanup() {
18+
19+
}
20+
21+
void ScreenShot::rasie() {
22+
23+
}
24+
25+
void ScreenShot::init(QWidget *parent) {
26+
27+
}

qt_screenshot/core/screen_shot.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,39 @@
55
#ifndef FIRESHOT_SCREEN_SHOT_H
66
#define FIRESHOT_SCREEN_SHOT_H
77

8+
#include <QObject>
9+
#include <QWidget>
10+
#include <QList>
11+
//#include "widget.h"
812

9-
class screen_shot {
13+
class FireShot;
1014

15+
class ScreenShot : public QObject {
16+
Q_OBJECT
17+
public:
18+
explicit ScreenShot(FireShot *lpFireShot);
19+
~ScreenShot() override;
20+
21+
void init(QWidget* parent);
22+
23+
void cleanup();
24+
25+
void rasie();
26+
27+
private:
28+
//QList<Widget*>* m_widgets;
29+
//QList<Widget*>* m_unused_widgets;
30+
31+
signals:
32+
void ShotDone(Starter*);
33+
void ShotResult(std::shared_ptr<QPixmap> pixmap);
34+
35+
private slots:
36+
void finishShot(int code);
37+
void finishConfirmArea();
38+
39+
private:
40+
FireShot *m_lpFireShot;
1141
};
1242

1343

qt_screenshot/core/screen_widget.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Created by wangyz38535 on 2023/10/12.
3+
//
4+
5+
6+
#include "screen_widget.h"
7+
8+
ScreenWidgets::ScreenWidgets(QWidget *parent) : QWidget(parent) {
9+
10+
}
11+
12+
ScreenWidgets::~ScreenWidgets() {
13+
14+
}

qt_screenshot/core/screen_widget.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Created by wangyz38535 on 2023/10/12.
3+
//
4+
5+
#ifndef FIRESHOT_SCREEN_WIDGET_H
6+
#define FIRESHOT_SCREEN_WIDGET_H
7+
8+
#include <QWidget>
9+
#include <QPixmap>
10+
#include <QString>
11+
#include <QPainter>
12+
13+
14+
class ScreenWidgets : public QWidget {
15+
Q_OBJECT
16+
public:
17+
explicit ScreenWidgets(QWidget *parent);
18+
~ScreenWidgets() override;
19+
20+
21+
22+
private:
23+
24+
};
25+
26+
27+
#endif //FIRESHOT_SCREEN_WIDGET_H

qt_screenshot/core/widget.cpp

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
//
2+
// Created by wangyz38535 on 2023/10/12.
3+
//
4+
5+
#include "widget.h"
6+
#include <QPainter>
7+
#include <QBitmap>
8+
#include "utils/spdlog_wrapper.h"
9+
10+
#define UNVISIABLE_X -819200
11+
#define UNVISIABLE_Y -819200
12+
13+
Widget::Widget(QWidget* parent)
14+
: QWidget(parent)
15+
, m_workspace(new Workspace(this))
16+
, m_screenlist(nullptr)
17+
, m_status("unknown")
18+
{
19+
#ifndef USE_SPDLOG_
20+
setWindowFlags(Qt::Tool);
21+
setAttribute(Qt::WA_TranslucentBackground);
22+
#endif // USE_SPDLOG_
23+
}
24+
25+
Widget::~Widget()
26+
{
27+
SPD_ERROR("{0}", __FUNCTION__);
28+
}
29+
30+
void Widget::start(std::shared_ptr<ScreenList> list, int index)
31+
{
32+
SPD_FUNCTION();
33+
34+
QRect geometry = list->allBoundary(true);
35+
setGeometry(geometry);
36+
37+
showFullScreen();
38+
39+
raise();
40+
activateWindow();
41+
42+
setCursor(Qt::ArrowCursor);
43+
44+
m_screenlist = list;
45+
m_workspace->start(m_screenlist, index);
46+
}
47+
48+
void Widget::cleanup()
49+
{
50+
SPD_FUNCTION();
51+
hide();
52+
53+
SPD_TRACE("is visable = {0} && w: {1}, h: {2} @ {3}", this->isVisible(), size().width(), size().height(), __FUNCTION__);
54+
55+
m_status = "unknown";
56+
m_workspace->cleanup();
57+
58+
update();
59+
}
60+
61+
Workspace* Widget::workspace() const
62+
{
63+
return m_workspace;
64+
}
65+
66+
void Widget::finishConfirmArea()
67+
{
68+
if (m_workspace->areaConfirmed() == true)
69+
m_status = "active";
70+
else
71+
m_status = "giveup";
72+
}
73+
74+
void Widget::showEvent(QShowEvent* event)
75+
{
76+
SPD_FUNCTION();
77+
78+
raise();
79+
activateWindow();
80+
81+
setMouseTracking(true);
82+
83+
QWidget::showEvent(event);
84+
SPD_WARN("is visable = {0} && x: {1}, y: {2}, w: {3}, h: {4} @ {5} --- {6}"
85+
, this->isVisible(), pos().x(), pos().y(), size().width(), size().height(), m_status.toStdString().c_str()
86+
, __FUNCTION__);
87+
}
88+
89+
void Widget::hideEvent(QHideEvent* event)
90+
{
91+
setMouseTracking(false);
92+
93+
resize(0, 0);
94+
95+
QWidget::hideEvent(event);
96+
97+
SPD_INFO("is visable = {0} && x: {1}, y: {2}, w: {3}, h: {4} @ {5} --- {6}"
98+
, this->isVisible(), pos().x(), pos().y(), size().width(), size().height(), m_status.toStdString().c_str()
99+
, __FUNCTION__);
100+
}
101+
102+
void Widget::closeEvent(QCloseEvent* event)
103+
{
104+
event->ignore();
105+
106+
SPD_TRACE("{0} stat = {1}", __FUNCTION__, m_status.toStdString().c_str());
107+
108+
hide();
109+
}
110+
111+
void Widget::mousePressEvent(QMouseEvent* event)
112+
{
113+
if (m_status != "giveup")
114+
m_workspace->onMousePress(event);
115+
}
116+
117+
void Widget::mouseMoveEvent(QMouseEvent* event)
118+
{
119+
SPD_TRACE("{0} stat = {1}", __FUNCTION__, m_status.toStdString().c_str());
120+
if (m_status != "giveup")
121+
m_workspace->onMouseMove(event);
122+
}
123+
124+
void Widget::mouseReleaseEvent(QMouseEvent* event)
125+
{
126+
if (m_status != "giveup")
127+
m_workspace->onMouseRelease(event);
128+
}
129+
130+
void Widget::keyPressEvent(QKeyEvent* event)
131+
{
132+
m_workspace->onKeyPress(event);
133+
}
134+
135+
void Widget::paintEvent(QPaintEvent* event)
136+
{
137+
SPD_FUNCTION();
138+
139+
Q_UNUSED(event);
140+
141+
QPainter painter(this);
142+
143+
m_workspace->draw(painter);
144+
}
145+
146+
void Widget::enterEvent(QEvent* event)
147+
{
148+
SPD_FUNCTION();
149+
QWidget::enterEvent(event);
150+
}
151+
152+
void Widget::leaveEvent(QEvent* event)
153+
{
154+
SPD_FUNCTION();
155+
// if (m_workspace->areaConfirmed() == false)
156+
// {
157+
// m_workspace->setAreaBoundary(QRect(0,0,0,0));
158+
// update();
159+
// }
160+
161+
QWidget::leaveEvent(event);
162+
}
163+

qt_screenshot/core/widget.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// Created by wangyz38535 on 2023/10/12.
3+
//
4+
5+
#ifndef FIRESHOT_WIDGET_H
6+
#define FIRESHOT_WIDGET_H
7+
8+
9+
10+
#include <QWidget>
11+
#include <screen/workspace.h>
12+
#include <QPixmap>
13+
#include <QPainter>
14+
15+
#include "core/screenlist.h"
16+
17+
#include <memory>
18+
19+
class Widget : public QWidget
20+
{
21+
Q_OBJECT
22+
23+
public:
24+
Widget(QWidget *parent = nullptr);
25+
~Widget();
26+
27+
void start(std::shared_ptr<ScreenList> list, int index);
28+
void cleanup();
29+
30+
void finishConfirmArea();
31+
32+
protected:
33+
void showEvent(QShowEvent* event) override;
34+
void hideEvent(QHideEvent* event) override;
35+
void closeEvent(QCloseEvent* event) override;
36+
37+
void mousePressEvent(QMouseEvent *) override;
38+
void mouseReleaseEvent(QMouseEvent *) override;
39+
void mouseMoveEvent(QMouseEvent *) override;
40+
void keyPressEvent(QKeyEvent *) override;
41+
void paintEvent(QPaintEvent *) override;
42+
void enterEvent(QEvent* event) override;
43+
void leaveEvent(QEvent *event) override;
44+
45+
private:
46+
QString m_status; //unknown, active, giveup
47+
Workspace* m_workspace;
48+
std::shared_ptr<ScreenList> m_screenlist;
49+
};
50+
51+
52+
#endif //FIRESHOT_WIDGET_H

0 commit comments

Comments
 (0)