-
Notifications
You must be signed in to change notification settings - Fork 11
/
toolboxwindow.cpp
168 lines (139 loc) · 4.84 KB
/
toolboxwindow.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2020-2024 XMuli
// SPDX-GitHub: https://github.com/XMuli/shotx
// SPDX-Author: XMuli <xmulitech@gmail.com>
#include "screenshots.h"
#include "toolboxwindow.h"
#include <QIcon>
#include <QToolButton>
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QDebug>
#include <QPainter>
#include <QClipboard>
#include <QApplication>
#include <QDate>
#include <QFileDialog>
class screenshots;
#define CURR_TIME QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss")
/*!
* \brief The ToolBoxWindow class 透明工具栏窗口,默认依附在截图窗口的右下角
*/
ToolBoxWindow::ToolBoxWindow()
{
init();
}
void ToolBoxWindow::init()
{
setWindowFlags(Qt::FramelessWindowHint);
// setAttribute(Qt::WA_TranslucentBackground);
setContentsMargins(0, 0, 0, 0);
QHBoxLayout *hBoxLayout = new QHBoxLayout(this);
hBoxLayout->setContentsMargins(4, 4, 4, 4);
m_staPos = m_endPos = QPoint();
var.fill(nullptr, 2); // 初始化
m_listName /*<< "rectangle"
<< "ellipse"
<< "line"
<< "arrow"
<< "pen"
<< "text"
<< "mosaic"
<< "revoke"
<< "update"*/
<< "download"
<< "copy";
QStringList listToolTip = {/*tr("矩形(⌘ + 1)")
, tr("椭圆(⌘ + 2)")
, tr("直线(⌘ + 3)")
, tr("箭头(⌘ + 4)")
, tr("笔(⌘ + 5)")
, tr("文字(⌘ + 6)")
, tr("模糊(⌘ + 7)")
, tr("撤销(⌘ + z)")
, tr("上传(⌃ + ⇧ + 1)")
, */tr("保存(⌘ + s)")
, tr("复制到剪切板(return)") };
for (int i = 0; i < m_listName.count(); ++i) {
QString name = ":/icons/normal/" + m_listName[i] + ".svg";
var[i] = new QToolButton();
var[i]->setToolButtonStyle(Qt::ToolButtonIconOnly);
var[i]->setAutoRaise(true); // 自动浮动模式
var[i]->setIcon(QIcon(name));
var[i]->setIconSize(QSize(16, 16));
var[i]->setToolTip(listToolTip[i]);
// if (i < 7)
// var[i]->setCheckable(true);
// else
var[i]->setCheckable(false);
hBoxLayout->addWidget(var[i]);
}
connect(var[0], &QToolButton::released, this, &ToolBoxWindow::onDownload);
connect(var[1], &QToolButton::released, this, &ToolBoxWindow::onCopy);
}
void ToolBoxWindow::mousePressEvent(QMouseEvent *event)
{
m_endPos = m_staPos = event->globalPos();
m_topLeftPos = pos();
}
void ToolBoxWindow::mouseMoveEvent(QMouseEvent *event)
{
if (!m_staPos.isNull()) {
m_endPos = event->globalPos();
move(m_topLeftPos + m_endPos - m_staPos);
}
}
void ToolBoxWindow::mouseReleaseEvent(QMouseEvent *event)
{
Q_UNUSED(event)
m_endPos = m_staPos = QPoint();
}
void ToolBoxWindow::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
updateToolBtnIcon();
QPainter pa(this);
pa.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
pa.setPen(Qt::NoPen);
pa.setBrush(QColor(255, 255, 255, 0.7 * 255));
const int round = 4;
pa.drawRoundedRect(contentsRect().adjusted(1, 1, -1, -1), round, round);
}
/*!
* \brief ToolBoxWindow::updateToolBtnIcon 依据 toolBtn 是否被按下,选用不同颜色 svg 图片
*/
void ToolBoxWindow::updateToolBtnIcon()
{
for (int i = 0; i < var.count(); ++i) {
QString normal = ":/icons/normal/" + m_listName[i] + ".svg";
QString pressed = ":/icons/pressed/" + m_listName[i] + ".svg";
if (var[i]->isChecked()) {
var[i]->setIcon(QIcon(pressed));
} else {
var[i]->setIcon(QIcon(normal));
}
}
}
/*!
* \brief ToolBoxWindow::onDownload 保存本地
*/
void ToolBoxWindow::onDownload()
{
QString fileter = "图片文件(*.png);;图片文件(*.jpg);;所有文件(*.*)";
QString fileNmae = QFileDialog::getSaveFileName(this, tr("保存图片"), "ShotX" + CURR_TIME + ".png", fileter);
ScreenShots *screenShot = static_cast<ScreenShots *>(parent());
screenShot->savePixmap().save( CURR_TIME + ".png");
if (!fileNmae.isEmpty())
screenShot->savePixmap().save( fileNmae);
qDebug()<<"--------------onDownload"<<parent()<<parent()->parent()<<fileNmae;
}
/*!
* \brief ToolBoxWindow::onCopy 点击复制,会将截图保存在剪切板上
*/
void ToolBoxWindow::onCopy()
{
ScreenShots *screenShot = static_cast<ScreenShots *>(parent());
QClipboard *clipboard = QApplication::clipboard();
clipboard->setPixmap(screenShot->savePixmap());
qDebug()<<"--------------onCopy"<<parent()<<parent()->parent();
}