Skip to content

Commit 8dbd60b

Browse files
支持托盘功能,能正常显示托盘
1 parent 0ef23b7 commit 8dbd60b

File tree

13 files changed

+257
-42
lines changed

13 files changed

+257
-42
lines changed

qt_screenshot/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ target_sources(
109109

110110
target_include_directories(${PROJECT_NAME}
111111
PUBLIC ./
112-
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_autogen/include
113-
${CMAKE_CURRENT_SOURCE_DIR}/3rd)
112+
${CMAKE_CURRENT_SOURCE_DIR}/include
113+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_autogen/include
114+
${CMAKE_CURRENT_SOURCE_DIR}/3rd)
114115

115116

116117
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)

qt_screenshot/config/tray_setting.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ Q_OBJECT
1919
SettingDlg(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
2020
~SettingDlg() override;
2121

22+
signals:
23+
void SaveHotKeyConfig();
24+
void UpdateHotKeyText(uint32_t value);
25+
void UpdateHotKeyResult(bool success);
26+
void InitHotKeyValue(uint32_t value);
2227

2328
private:
2429
Ui::Settings * ui;

qt_screenshot/core/screen_list.cpp

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,153 @@
33
//
44

55
#include "screen_list.h"
6+
7+
#include "utils/spdlog_wrapper.h"
8+
9+
#include <QCursor>
10+
#include <QPainter>
11+
#include <QRect>
12+
#include <QApplication>
13+
14+
ScreenList::ScreenList(QList<ScreenInfo> list)
15+
{
16+
this->m_List = list;
17+
this->initParams();
18+
}
19+
20+
QRect ScreenList::allBoundary(bool isGlobal) const
21+
{
22+
if(isGlobal == true)
23+
return m_allBoundary;
24+
else
25+
return QRect(0,0,m_allBoundary.width(),m_allBoundary.height());
26+
}
27+
28+
QPixmap ScreenList::allPixMap() const
29+
{
30+
return *m_allPixMap;
31+
}
32+
33+
QRect ScreenList::boundaryAt(int screenIndex,bool isGlobal)
34+
{
35+
if(isGlobal == true)
36+
return m_List[screenIndex].boundary;
37+
else
38+
return toLocal(m_List[screenIndex].boundary);
39+
}
40+
41+
QScreen *ScreenList::screenAt(int screenIndex)
42+
{
43+
return m_List[screenIndex].object;
44+
}
45+
46+
QPixmap ScreenList::pixmapAt(int screenIndex)
47+
{
48+
return m_List[screenIndex].pixmap;
49+
}
50+
51+
void ScreenList::initParams()
52+
{
53+
int minX = 0;
54+
int minY = 0;
55+
int maxX = 0;
56+
int maxY = 0;
57+
58+
if(m_List.size() >0)
59+
{
60+
minX = m_List[0].boundary.x();
61+
minY = m_List[0].boundary.y();
62+
63+
maxX = minX + m_List[0].boundary.width();
64+
maxY = minY + m_List[0].boundary.height();
65+
}
66+
67+
for(int i=1;i<m_List.size();i++)
68+
{
69+
ScreenInfo info = m_List[i];
70+
71+
minX = qMin(minX,info.boundary.x());
72+
maxX = qMax(maxX,info.boundary.x()+info.boundary.width());
73+
74+
minY = qMin(minY,info.boundary.y());
75+
maxY = qMax(maxY,info.boundary.y()+info.boundary.height());
76+
}
77+
78+
m_allBoundary.setRect(minX,minY, maxX-minX,maxY-minY);
79+
80+
float pixelRetio = 1;
81+
if(m_List.count() == 1)
82+
{
83+
pixelRetio = m_List.at(0).object->devicePixelRatio();
84+
}
85+
86+
m_allPixMap.reset(new QPixmap(m_allBoundary.width() * pixelRetio, m_allBoundary.height() * pixelRetio));
87+
QRect i_allBoundary(m_allBoundary.x() * pixelRetio,
88+
m_allBoundary.y() * pixelRetio,
89+
m_allBoundary.width() * pixelRetio,
90+
m_allBoundary.height() * pixelRetio);
91+
92+
QPainter painter(m_allPixMap.get());
93+
94+
for(ScreenInfo& info:m_List)
95+
{
96+
QRect mRect(info.boundary.x() * pixelRetio,
97+
info.boundary.y() * pixelRetio,
98+
info.boundary.width() * pixelRetio,
99+
info.boundary.height() * pixelRetio);
100+
const auto& mPixmap = info.pixmap;
101+
102+
QRect rRect(mRect.x()-i_allBoundary.x(),mRect.y()-i_allBoundary.y(),mRect.width(),mRect.height());
103+
104+
SPD_TRACE("rRect: [{0}, {1}, {2}, {3}], mPixmap: {4}, {5}", rRect.left(), rRect.top(), rRect.right(), rRect.bottom(), mPixmap.width(), mPixmap.height());
105+
106+
painter.drawPixmap(rRect,mPixmap);
107+
}
108+
}
109+
110+
QRect ScreenList::toLocal(QRect rect)
111+
{
112+
return QRect(rect.x()-m_allBoundary.x(),rect.y()-m_allBoundary.y(),rect.width(),rect.height());
113+
}
114+
115+
//float ScreenList::scaleAt(int screenIndex)
116+
//{
117+
// QScreen *screen = screenAt(screenIndex);
118+
119+
// float refDpi = 120.;
120+
121+
// float dpi = screen->logicalDotsPerInch();
122+
123+
// float v = dpi/refDpi;
124+
125+
// return v;
126+
//}
127+
128+
float ScreenList::scale()
129+
{
130+
QScreen *screen = m_List.size() == 1 ? m_List[0].object : QApplication::primaryScreen();
131+
float dpi = screen->logicalDotsPerInch();
132+
133+
float pixel120ToMM = 1.0f/96.0f * 2.54f * 10.0f;
134+
float MMToDpiPixel = 1.0f/2.54f*dpi/10.0f;
135+
136+
return pixel120ToMM * MMToDpiPixel * screen->devicePixelRatio();
137+
138+
//return 1.0;
139+
}
140+
141+
void ScreenList::draw(QPainter &painter)
142+
{
143+
painter.drawPixmap(allBoundary(), *m_allPixMap);
144+
}
145+
146+
void ScreenList::draw(QPainter &painter, QPainterPath maskPath, QBrush maskBrush)
147+
{
148+
QRect rect = allBoundary();
149+
SPD_FUNCTION();
150+
SPD_TRACE("rect left: {0}, top: {1}, right:{2}, bottom:{3}", rect.left(), rect.top(), rect.right(), rect.bottom());
151+
152+
painter.drawPixmap(rect, *m_allPixMap);
153+
painter.fillPath(maskPath,maskBrush);
154+
}
155+

qt_screenshot/core/screen_shot.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ void ScreenShot::rasie() {
2525
void ScreenShot::init(QWidget *parent) {
2626

2727
}
28+
29+
void ScreenShot::finishShot(int code) {
30+
31+
}
32+
33+
void ScreenShot::finishConfirmArea() {
34+
35+
}

qt_screenshot/core/screen_shot.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ScreenShot : public QObject {
2929
//QList<Widget*>* m_unused_widgets;
3030

3131
signals:
32-
void ShotDone(Starter*);
32+
void ShotDone(ScreenShot*);
3333
void ShotResult(std::shared_ptr<QPixmap> pixmap);
3434

3535
private slots:

qt_screenshot/core/widget.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
Widget::Widget(QWidget* parent)
1414
: QWidget(parent)
15-
, m_workspace(new Workspace(this))
1615
, m_screenlist(nullptr)
1716
, m_status("unknown")
1817
{
@@ -42,7 +41,6 @@ void Widget::start(std::shared_ptr<ScreenList> list, int index)
4241
setCursor(Qt::ArrowCursor);
4342

4443
m_screenlist = list;
45-
m_workspace->start(m_screenlist, index);
4644
}
4745

4846
void Widget::cleanup()
@@ -53,22 +51,17 @@ void Widget::cleanup()
5351
SPD_TRACE("is visable = {0} && w: {1}, h: {2} @ {3}", this->isVisible(), size().width(), size().height(), __FUNCTION__);
5452

5553
m_status = "unknown";
56-
m_workspace->cleanup();
5754

5855
update();
5956
}
6057

61-
Workspace* Widget::workspace() const
62-
{
63-
return m_workspace;
64-
}
6558

6659
void Widget::finishConfirmArea()
6760
{
68-
if (m_workspace->areaConfirmed() == true)
69-
m_status = "active";
70-
else
71-
m_status = "giveup";
61+
//if (m_workspace->areaConfirmed() == true)
62+
// m_status = "active";
63+
//else
64+
// m_status = "giveup";
7265
}
7366

7467
void Widget::showEvent(QShowEvent* event)
@@ -101,7 +94,7 @@ void Widget::hideEvent(QHideEvent* event)
10194

10295
void Widget::closeEvent(QCloseEvent* event)
10396
{
104-
event->ignore();
97+
//event->ignore();
10598

10699
SPD_TRACE("{0} stat = {1}", __FUNCTION__, m_status.toStdString().c_str());
107100

@@ -110,26 +103,20 @@ void Widget::closeEvent(QCloseEvent* event)
110103

111104
void Widget::mousePressEvent(QMouseEvent* event)
112105
{
113-
if (m_status != "giveup")
114-
m_workspace->onMousePress(event);
115106
}
116107

117108
void Widget::mouseMoveEvent(QMouseEvent* event)
118109
{
119110
SPD_TRACE("{0} stat = {1}", __FUNCTION__, m_status.toStdString().c_str());
120-
if (m_status != "giveup")
121-
m_workspace->onMouseMove(event);
122111
}
123112

124113
void Widget::mouseReleaseEvent(QMouseEvent* event)
125114
{
126-
if (m_status != "giveup")
127-
m_workspace->onMouseRelease(event);
115+
//m_workspace->onMouseRelease(event);
128116
}
129117

130118
void Widget::keyPressEvent(QKeyEvent* event)
131119
{
132-
m_workspace->onKeyPress(event);
133120
}
134121

135122
void Widget::paintEvent(QPaintEvent* event)
@@ -140,7 +127,6 @@ void Widget::paintEvent(QPaintEvent* event)
140127

141128
QPainter painter(this);
142129

143-
m_workspace->draw(painter);
144130
}
145131

146132
void Widget::enterEvent(QEvent* event)

qt_screenshot/core/widget.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99

1010
#include <QWidget>
11-
#include <screen/workspace.h>
1211
#include <QPixmap>
1312
#include <QPainter>
14-
15-
#include "core/screenlist.h"
16-
1713
#include <memory>
14+
#include "screen_list.h"
15+
1816

1917
class Widget : public QWidget
2018
{
@@ -44,7 +42,6 @@ Q_OBJECT
4442

4543
private:
4644
QString m_status; //unknown, active, giveup
47-
Workspace* m_workspace;
4845
std::shared_ptr<ScreenList> m_screenlist;
4946
};
5047

qt_screenshot/fireshot.cpp

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@
1010

1111
#include "fireshot.h"
1212
#include "utils/spdlog_wrapper.h"
13-
14-
13+
#include "fireshot_errno.h"
1514

1615
FireShot::FireShot(QWidget *parent) :
1716
QDialog(parent), m_lpTrayIcon(new QSystemTrayIcon(this)),
1817
m_lpTrayIconMenu(new QMenu(this)),
19-
m_lpSettingDlg(new SettingDlg(this),
20-
m_bShotting(false)) {
21-
18+
m_lpSettingDlg(new SettingDlg(this)),
19+
m_bShotting(false) {
20+
CreateActions();
21+
CreateTrayWithIcon();
22+
//
23+
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
24+
// 透明背景
25+
setAttribute(Qt::WA_TranslucentBackground, true);
26+
27+
m_lpTrayIcon->show();
2228
}
2329

2430
FireShot::~FireShot() {
@@ -72,3 +78,36 @@ void FireShot::OnStartShot() {
7278

7379
}
7480

81+
void FireShot::OnShotDone(ScreenShot *starer) {
82+
83+
}
84+
85+
void FireShot::OnExitShot() {
86+
87+
}
88+
89+
int32_t FireShot::CreateTrayWithIcon() {
90+
91+
m_lpTrayIconMenu->addAction(m_lpShotAction);
92+
m_lpTrayIconMenu->addAction(m_lpSettingAction);
93+
m_lpTrayIconMenu->addAction(m_lpQuitAction);
94+
95+
m_lpTrayIcon->setContextMenu(m_lpTrayIconMenu);
96+
m_lpTrayIcon->setIcon(QIcon(":/images/image/fire.png"));
97+
m_lpTrayIcon->setToolTip(tr("FireShot"));
98+
99+
return 0;
100+
}
101+
102+
void FireShot::ConstructSignal() {
103+
connect(this, &FireShot::SatrtShot, this, &FireShot::OnStartShot);
104+
connect(this, &FireShot::CheckHotKey, m_lpSettingDlg, &SettingDlg::InitHotKeyValue);
105+
connect(m_lpTrayIcon, &QSystemTrayIcon::activated, this, &FireShot::OnIconActivated);
106+
}
107+
108+
void FireShot::OnIconActivated(QSystemTrayIcon::ActivationReason reason) {
109+
110+
}
111+
112+
113+

0 commit comments

Comments
 (0)