-
Notifications
You must be signed in to change notification settings - Fork 20
/
Preview.cpp
136 lines (111 loc) · 3.35 KB
/
Preview.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
/*
Copyright (C) 2010-2017 Christopher Brochtrup
This file is part of Capture2Text.
Capture2Text is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Capture2Text is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Capture2Text. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDesktopWidget>
#include <QApplication>
#include "Preview.h"
Preview::Preview()
: QWidget(0, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Dialog | Qt::Tool),
previewText(""),
textColor(QColor(128, 128, 128, 255)),
backgroundColor(QColor(8, 8, 8, 255)),
borderColor(QColor(128, 128, 128, 255)),
textFont(QFont("Arial", 16)),
infoTimer(this)
{
setAttribute(Qt::WA_TranslucentBackground);
setFixedSize(1, 1);
connect(&infoTimer, &QTimer::timeout, this, &Preview::infoTimeout);
}
void Preview::infoTimeout()
{
infoTimer.stop();
hide();
}
void Preview::showInfo(QString text)
{
setText(text);
QFontMetrics metrics(textFont);
QPoint pt = QApplication::desktop()->availableGeometry().bottomRight();
pt.setX(pt.x() - metrics.width(previewText) - horizontalPadding);
pt.setY(pt.y() - getBoxHeight());
move(pt);
show();
raise();
update();
infoTimer.start(1500);
}
void Preview::setText(const QString &text)
{
previewText = text;
}
QString Preview::getText()
{
return previewText;
}
void Preview::setTextColor(const QColor &color)
{
textColor = color;
}
void Preview::setBackgroundColor(const QColor &color)
{
backgroundColor = color;
}
void Preview::setBorderColor(const QColor &color)
{
borderColor = color;
}
void Preview::setTextFont(const QFont &font)
{
textFont = font;
}
int Preview::getBoxHeight()
{
QFontMetrics metrics(textFont);
return metrics.height() + verticalPadding;
}
void Preview::hideAndReset()
{
hide();
previewText = "";
}
void Preview::paintEvent(QPaintEvent *)
{
if(previewText == "")
{
return;
}
QPainter painter(this);
// Fit dialog to text
QDesktopWidget desktopWidget;
QRect screenRect = desktopWidget.screenGeometry(pos());
QFontMetrics metrics(textFont);
int textWidth = qMin(metrics.width(previewText) + horizontalPadding, screenRect.width());
int textHeight = metrics.height() + verticalPadding;
setFixedSize(textWidth, textHeight);
// Background
painter.fillRect(0, 0, width(), height(), backgroundColor);
QPen pen;
// Border
pen.setColor(borderColor);
painter.setPen(pen);
painter.drawRect(0, 0, width() - 1, height() - 1);
// Text
pen.setColor(textColor);
painter.setPen(pen);
painter.setFont(textFont);
painter.drawText(QRect(0 + horizontalPadding / 2, 0, width(), height()),
Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine,
previewText);
}