-
Notifications
You must be signed in to change notification settings - Fork 1
/
abstractview.cpp
executable file
·180 lines (125 loc) · 4.56 KB
/
abstractview.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
/*
* Copyright 2012 Yury Makarevich
*
* This file is part of Tomodoro.
*
* Tomodoro 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.
*
* Tomodoro 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 Tomodoro. If not, see <http://www.gnu.org/licenses/>.
*/
#include "abstractview.h"
#include "settings.h"
#include "customlabel.h"
#include <QMouseEvent>
#include <QApplication>
#include <QDesktopWidget>
AbstractView::AbstractView(QWidget *parent) :
QWidget(parent, Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint | Qt::Dialog),
m_mouse_anchor(),
m_win_anchor(),
m_conf_buzz_dev(5),
m_op_normal(50),
m_op_focused(80),
m_custom_label(new CustomLabel),
m_current(0),
m_total(100)
//m_text("--:--")
{
setAttribute(Qt::WA_TranslucentBackground);
setContextMenuPolicy(Qt::CustomContextMenu);
//update_settings(); // delegate that to descendants
}
void AbstractView::mousePressEvent(QMouseEvent* event) {
if(event->button() == Qt::LeftButton) {
m_mouse_anchor = event->globalPos();
}
}
void AbstractView::mouseMoveEvent(QMouseEvent* event) {
if(event->buttons() & Qt::LeftButton) {
QPoint n = event->globalPos();
QPoint win_pos = is_buzzing() ? m_win_anchor : pos();
// shift the win position
win_pos += n - m_mouse_anchor;
// crop the movement
QRect scr = QApplication::desktop()->screenGeometry();
scr.adjust(0, 0, -width(), -height());
if (win_pos.x() < scr.left()) win_pos.setX(scr.left());
else if(win_pos.x() > scr.right()) win_pos.setX(scr.right());
if (win_pos.y() < scr.top()) win_pos.setY(scr.top());
else if(win_pos.y() > scr.bottom()) win_pos.setY(scr.bottom());
if(is_buzzing()) {
m_win_anchor = win_pos;
} else {
move(win_pos);
}
// memorize the mouse position
m_mouse_anchor = n;
}
}
void AbstractView::enterEvent(QEvent *) {
setWindowOpacity(static_cast<double>(m_op_focused) / 100.0);
}
void AbstractView::leaveEvent(QEvent *) {
setWindowOpacity(static_cast<double>(m_op_normal) / 100.0);
}
//////////////////////////////////////////
void AbstractView::tick(int current, int total) {
//
// Handle buzzing
//
const bool buzzing = is_buzzing(); // current state
m_current = current;
m_total = total;
const bool about_to_buzz = is_buzzing(); // new state
if(about_to_buzz) {
// If we're gonna buzz, then offset the window
if(buzzing) {
#define RAND_DEV ((qrand() % (m_conf_buzz_dev * 2 + 1)) - m_conf_buzz_dev)
move(m_win_anchor + QPoint(RAND_DEV, RAND_DEV));
#undef RAND_DEV
}else {
m_win_anchor = pos();
}
} else if(buzzing) {
// Buzzing has just turned off.
move(m_win_anchor);
}
//
// Handle repaint
//
// Update text and redraw unless we're in the middle of buzzing
if( !(buzzing && about_to_buzz) ) {
int remaining = m_total - m_current;
QString text = QString("%1:%2")
.arg(QString::number(remaining / 60), 2, '0')
.arg(QString::number(remaining % 60), 2, '0');
m_custom_label->setText(text);
update();
}
}
void AbstractView::update_settings() {
Settings s;
m_conf_buzz_dev = s.view.buzz_dev;
m_op_normal = s.view.op_normal;
m_op_focused = s.view.op_focused;
// emulate leave event to set the opacity
leaveEvent(NULL);
// other view settings
m_settings.flags = s.view.main_dir;
if(s.view.inverted != 0)
m_settings.flags |= VIEW_INVERTED;
m_settings.border = s.defaultToText(this, (QColor)s.view.border);
m_settings.filling = s.defaultToButton(this, (QColor)s.view.filling);
// propagate the event to the custom label
m_custom_label->update_settings();
update();
}