/
widget.cpp
282 lines (236 loc) · 8.54 KB
/
widget.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "widget.h"
#include <KGlobalAccel>
#include <QAction>
#include <QVBoxLayout>
#include <QLabel>
#include <QApplication>
#include <QDesktopWidget>
#include <QMouseEvent>
#include <QClipboard>
#include <QMimeData>
#include <QDebug>
#include <QStandardPaths>
#include <QVBoxLayout>
#include <QDir>
#include <QPainter>
Widget::Widget() : QWidget(),
m_label(new QLabel)
{
setLayout(new QVBoxLayout);
layout()->addWidget(m_label);
setWindowFlags(Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint | Qt::WindowDoesNotAcceptFocus | Qt::Tool);
setAttribute(Qt::WA_TranslucentBackground);
m_label->setTextInteractionFlags(Qt::TextSelectableByKeyboard);
m_label->setTextFormat(Qt::PlainText);
m_label->setWordWrap(true);
layout()->setMargin(0);
setMaximumWidth(1000);
setMaximumHeight(1000);
setMinimumWidth(500);
setMinimumHeight(128);
m_label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
m_label->setMargin(5);
QFont font;
font.setPixelSize(15);
setFont(font);
connect(qApp->clipboard(), &QClipboard::dataChanged, &m_updateTimer, [=]() {
if (!m_updateTimer.isActive()) {
m_updateTimer.start(16);
}
});
connect(&m_updateTimer, &QTimer::timeout, this, &Widget::onClipboardUpdated);
m_updateTimer.setSingleShot(true);
m_timer.setSingleShot(true);
connect(&m_timer, &QTimer::timeout, this, &QWidget::hide);
QString stylesheet(
"QLabel {\n"
" border: 3px solid white;\n"
" background-color: rgba(0, 0, 0, 128);\n"
" selection-color: black;\n"
" selection-background-color: white;\n"
"}\n"
);
const QString configLocation = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
QDir configDir(configLocation);
if (!configDir.exists()) {
configDir.mkpath(configLocation);
}
QString stylePath = configDir.absoluteFilePath("pastenotifier.qss");
QFile styleFile(stylePath);
if (styleFile.exists()) {
if (styleFile.open(QIODevice::ReadOnly)) {
qDebug() << "Loading stylesheet" << stylePath;
stylesheet = QString::fromLocal8Bit(styleFile.readAll());
} else {
qWarning() << "Unable to open qss file:" << stylePath << styleFile.errorString();
}
} else {
if (styleFile.open(QIODevice::WriteOnly)) {
styleFile.write(stylesheet.toUtf8());
} else {
qWarning() << "Unable to open qss file for writing:" << stylePath << styleFile.errorString();
}
}
QAction *showAction = new QAction(this);
connect(showAction, &QAction::triggered, [=](){
setWindowOpacity(1);
show();
m_timer.start(5000);
});
showAction->setObjectName("showpastenotifier");
KGlobalAccel::setGlobalShortcut(showAction, QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_V));
setStyleSheet(stylesheet);
onClipboardUpdated();
}
Widget::~Widget()
{
}
void Widget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_timer.stop();
hide();
} else if (event->button()) {
qApp->quit();
}
}
void Widget::enterEvent(QEvent *)
{
setWindowOpacity(0.3);
}
void Widget::leaveEvent(QEvent *)
{
setWindowOpacity(1);
}
void Widget::onClipboardUpdated()
{
QClipboard *clip = qApp->clipboard();
const QMimeData *mimeData = clip->mimeData();
if (mimeData->hasImage()) {
QImage image = qvariant_cast<QImage>(mimeData->imageData());
if (image.isNull()) {
// Workaround for what I think is a bug introduced in qt base with
// commit 730cbad8824bcfcb7ab60371a6563cfb6dd5658d
// It means that the entire image isn't fetched when we first
// request it, so we have to retry later.
if (m_imageRetries++ < 5) {
qWarning() << "Got null image, retry number" << m_imageRetries;
if (!m_updateTimer.isActive()) {
m_updateTimer.start(16);
}
return;
}
qWarning() << "Got null image, too many retries";
m_imageRetries = 0;
QFont f = font();
f.setPixelSize(15);
const QString text = tr("Failed to fetch image from clipboard");
QRect rect = QFontMetrics(f).boundingRect(text).marginsAdded(QMargins(5, 5, 5, 5));
rect.moveTo(2, 2);
image = QImage(rect.size(), QImage::Format_ARGB32);
image.fill(Qt::red);
QPainter p(&image);
p.setFont(f);
f.setBold(true);
p.setPen(Qt::white);
p.drawText(rect, text);
} else {
QSize origSize = image.size();
const int minWidth = minimumWidth() - 20;
const int minHeight = minimumHeight() - 20;
if (image.width() <= image.height() && image.height() < minimumHeight() - 20) {
image = image.scaledToHeight(minHeight);
} else if (image.width() < minWidth) {
image = image.scaledToWidth(minWidth);
}
if (image.width() > maximumWidth() - 10) {
image = image.scaledToWidth(maximumWidth() - 10);
}
if (image.height() > maximumHeight() - 10) {
image = image.scaledToHeight(maximumHeight() - 10);
}
image = image.convertToFormat(QImage::Format_ARGB32);
{
QPainter p(&image);
QFont font;
font.setPixelSize(15);
p.setFont(font);
QFontMetrics metrics(font);
QString text = QString::number(origSize.width()) + "x" + QString::number(origSize.height());
QRect rect = metrics.boundingRect(text).marginsAdded(QMargins(5, 5, 5, 5));
rect.moveTopLeft(QPoint(0, 0));
p.fillRect(rect, QColor(0, 0, 0, 128));
p.setPen(Qt::white);
p.drawText(rect, Qt::AlignCenter, text);
}
}
m_label->setPixmap(QPixmap::fromImage(image));
resize(image.size());
updateGeometry();
m_hasTrimmed = false;
m_imageRetries = 0;
return;
}
m_imageRetries = 0;
QString text = mimeData->text();
const QStringList formats = mimeData->formats();
bool onlyTextPlain = formats.contains("text/plain");
if (onlyTextPlain) {
for (const QString &format : formats) {
// "Special" fake mimetypes to indicate multiselections etc.
if (!format.contains('/') || format.toUpper() == format) {
continue;
}
if (format == "text/plain" || format == "text/html") {
continue;
}
onlyTextPlain = false;
break;
}
QString trimmed = text.trimmed();
if (onlyTextPlain && trimmed != text && !m_hasTrimmed) {
clip->setText(trimmed);
m_hasTrimmed = true;
return;
}
}
m_hasTrimmed = false;
QFontMetrics metrics(font());
QString filteredText;
for (const QChar &c : text) {
if ((c.isPrint() || c == '\n' || c == '\t') && (c.script() == QChar::Script_Latin || c.script() == QChar::Script_Common)) {
filteredText += c;
continue;
}
qDebug() << c << c.script() << c.category();
filteredText += "0x" + QString::number(c.unicode(), 16);
}
text = filteredText;
QRect boundingRect = QRect(0, 0, maximumWidth(), maximumHeight());
boundingRect = metrics.boundingRect(boundingRect, Qt::TextWordWrap, text);
boundingRect = boundingRect.marginsAdded(QMargins(20, 20, 20, 20));
if (boundingRect.height() > maximumHeight() || boundingRect.width() > maximumWidth()) {
m_label->setWordWrap(false);
QString newText;
int h = metrics.height() * 2;
for (const QString &line : text.split('\n')) {
newText.append(metrics.elidedText(line, Qt::ElideRight, maximumWidth() - 20) + '\n');
h += metrics.height();
}
text = newText;
boundingRect.setHeight(h);
} else {
m_label->setWordWrap(true);
}
resize(boundingRect.size());
m_label->setText(text);
m_label->setSelection(0, text.length());
updateGeometry();
}
void Widget::updateGeometry()
{
move(qApp->desktop()->availableGeometry(this).width() - width() - 10, 10);
show();
setWindowOpacity(1);
m_timer.start(5000);
}