-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwidget.cpp
More file actions
429 lines (368 loc) · 9.82 KB
/
widget.cpp
File metadata and controls
429 lines (368 loc) · 9.82 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include <QChart>
using namespace QtCharts;
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QCloseEvent>
#include <QTimer>
#include <QMessageBox>
#include <QLineSeries>
#include <QCategoryAxis>
#include <QDateTimeAxis>
#include <QScatterSeries>
#include "battery.h"
#include "batteryevent.h"
#include "icontool.h"
#include "config.h"
#include "batteryrecord.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
btevt_=new BatteryEvent();
timer_update_battery_=new QTimer();
menu_=new QMenu();
cld_c_=new QColorDialog();
cld_c_->setOption(QColorDialog::ShowAlphaChannel);
cld_b_=new QColorDialog();
cld_b_->setOption(QColorDialog::ShowAlphaChannel);
cld_f_u_=new QColorDialog();
cld_f_u_->setOption(QColorDialog::ShowAlphaChannel);
cld_f_c_=new QColorDialog();
cld_f_c_->setOption(QColorDialog::ShowAlphaChannel);
//初始化托盘
inittray();
//初始化chart
init_chart();
//重复检测电量
connect(timer_update_battery_,&QTimer::timeout,this,&Widget::updatebtshow);
//插拔电源的时候更新
connect(btevt_,&BatteryEvent::PowerChanged,this,&Widget::updatebtshow);
connect(btevt_,&BatteryEvent::PowerChanged,this,&Widget::save_record);
//选择颜色
connect(cld_c_,&QColorDialog::colorSelected,this,&Widget::selected_bkc_c);
connect(cld_b_,&QColorDialog::colorSelected,this,&Widget::selected_bkc_b);
connect(cld_f_u_,&QColorDialog::colorSelected,this,&Widget::selected_fc_u);
connect(cld_f_c_,&QColorDialog::colorSelected,this,&Widget::selected_fc_c);
timer_update_battery_->setInterval(1000);
timer_update_battery_->start();
updatebtshow();
init_bt_rec();
timer_update_chart_=new QTimer();
timer_update_chart_->setInterval(1000*1);
timer_update_chart_->start();
connect(timer_update_chart_,&QTimer::timeout,this,&Widget::update_chart_need);
}
Widget::~Widget()
{
delete ui;
delete sti_;
sti_=nullptr;
delete timer_update_battery_;
timer_update_battery_=nullptr;
delete timer_update_chart_;
timer_update_chart_=nullptr;
delete menu_;
menu_=nullptr;
delete cld_c_;
cld_c_=nullptr;
delete cld_b_;
cld_b_=nullptr;
delete cld_f_u_;
cld_f_u_=nullptr;
delete cld_f_c_;
cld_f_c_=nullptr;
delete timer_save_record_;
timer_save_record_=nullptr;
delete chart_;
chart_=nullptr;
}
void Widget::updatebtshow()
{
Battery::Update();
showbtinfo();
}
void Widget::showbtinfo()
{
auto bp=Battery::percent;
ui->lb_bp->setText(QString::number(bp));
QString st;
switch(Battery::status){
case Battery::Status::UNCHARGING:
st=u8"使用电池";
break;
case Battery::Status::CHARGING:
st=u8"正在充电";
break;
case Battery::Status::UNKNOW:
st=u8"未知";
break;
}
ui->lb_st->setText(st);
st+="\n"+QString::number(bp)+"%";
sti_->setToolTip(st);
sti_->setIcon(IconTool::GenIcon(bp,Battery::status==Battery::Status::CHARGING));
}
void Widget::inittray()
{
sti_=new QSystemTrayIcon();
//双击显示
connect(sti_,&QSystemTrayIcon::activated,this,&Widget::onactivetray);
//右键菜单
auto act1 = new QAction(menu_);
auto act2 = new QAction(menu_);
act1->setText(u8"显示");
act2->setText(u8"退出");
menu_->addAction(act1);
menu_->addAction(act2);
connect(act1, &QAction::triggered, this, &Widget::showmain);
connect(act2, &QAction::triggered, this, [this](auto){
close_by_tray_ = true;
close();
close_by_tray_ = false;
});
sti_->setContextMenu(menu_);
//在系统托盘显示
sti_->show();
}
void Widget::init_bt_rec()
{
BatteryRecord::Init();
timer_save_record_=new QTimer();
//保存电量记录
connect(timer_save_record_,&QTimer::timeout,this,&Widget::save_record);
timer_save_record_->setInterval(1000*60);
timer_save_record_->start();
Widget::save_record();
// auto recs=BatteryRecord::GetInstance()->GetRecords(1552541519,1552541885);
// for(const auto& p:recs){
// qDebug()<<"time:"<<std::get<0>(p)
// <<" per:"<<std::get<1>(p)
// <<"status:"<<std::get<2>(p);
// }
}
void Widget::init_chart()
{
chart_ = new QtCharts::QChart();
chart_->legend()->hide(); // 隐藏图例
chart_->setTitle(u8"24小时的电量记录"); // 设置图表的标题
ui->widget->setChart(chart_);
ui->widget->setRenderHint(QPainter::Antialiasing); //抗锯齿
}
void Widget::showmain()
{
show();
if(this->isMinimized()){
this->showNormal();
}
if(ui->tabWidget->currentIndex()==1){
update_chart();
}
}
void Widget::update_chart_need()
{
auto idx=ui->tabWidget->currentIndex();
// qDebug()<<"idx:"<<idx;
if(idx != 1){
return;
}
if(this->isMinimized()||this->isHidden()){
return;
}
update_chart();
}
void Widget::update_chart()
{
// qDebug()<<"update chart";
auto dt=QDateTime::currentDateTime();
auto now=static_cast<time_t>(dt.toTime_t());
auto recs=BatteryRecord::GetInstance()->GetRecords(now-86400,now);
//删除原来的线
for(const auto&s:chart_->series()){
chart_->removeSeries(s);
}
for(const auto &a:chart_->axes()){
chart_->removeAxis(a);
}
QPen pen;
{
pen.setStyle(Qt::SolidLine);
pen.setWidth(3);
pen.setColor(Qt::blue);
}
QPen pen_charge;
{
pen_charge.setStyle(Qt::SolidLine);
pen_charge.setWidth(3);
pen_charge.setColor(Qt::green);
}
//电量的折线
std::vector<QLineSeries*> lines;
int last_stat=-1;
for(auto it=recs.cbegin();it!=recs.cend();++it){
const auto& rec=*it;
int st=std::get<2>(rec);//0用电池,1充电
QLineSeries* line;
if(st==last_stat){
line=lines.back();
}else{
line = new QLineSeries();
//连接上一个点防止看起来断掉
if(it!=recs.cbegin()){
auto r=*(it-1);
line->append(std::get<0>(r)*1000,std::get<1>(r));
}
if(st==1){//充电
line->setPen(pen_charge);
}else{//用电
line->setPen(pen);
}
lines.emplace_back(line);
}
line->append(std::get<0>(rec)*1000,std::get<1>(rec));
last_stat=st;
}
for(auto &line:lines){
chart_->addSeries(line);
}
dt.addSecs(3600*-12).toString("HH:mm");
QDateTimeAxis *axisX = new QDateTimeAxis;
{
axisX->setTickCount(9);
axisX->setRange(dt.addDays(-1),dt);
axisX->setFormat("HH:mm");
}
chart_->addAxis(axisX,Qt::AlignBottom);
for(auto &line:lines){
line->attachAxis(axisX);
}
QValueAxis *axisY = new QValueAxis;
{
axisY->setRange(0, 100);
axisY->setMin(0);
axisY->setMax(100);
axisY->setLabelFormat("%d");
axisY->setTickCount(11);//10格子
}
chart_->addAxis(axisY,Qt::AlignLeft);
for(auto &line:lines){
line->attachAxis(axisY);
}
}
void Widget::closeEvent(QCloseEvent *e)
{
if(!close_by_tray_){
hide();
e->ignore();
}else{
e->accept();
QWidget::closeEvent(e);
}
return;
}
void Widget::changeEvent(QEvent *event)
{
if(event->type()==QEvent::WindowStateChange){
if(windowState() & Qt::WindowMinimized){
// 最小化不再隐藏
// hide();
}
}
QWidget::changeEvent(event);
}
void Widget::onactivetray(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:// 单击
break;
case QSystemTrayIcon::DoubleClick:// 双击
{
if(this->isHidden()){
showmain();
}else{
this->hide();
}
break;
}
default:
break;
}
}
void Widget::selected_bkc_c(const QColor &color)
{
auto cf=Config::GetInstance();
cf->color_charging=color;
cf->Save("config.ini");
IconTool::ClearCache();
}
void Widget::selected_bkc_b(const QColor &color)
{
auto cf=Config::GetInstance();
cf->color_us_bt=color;
cf->Save("config.ini");
IconTool::ClearCache();
}
void Widget::selected_fc_u(const QColor &color)
{
auto cf=Config::GetInstance();
cf->color_font_u=color;
cf->Save("config.ini");
IconTool::ClearCache();
}
void Widget::selected_fc_c(const QColor &color)
{
auto cf=Config::GetInstance();
cf->color_font_c=color;
cf->Save("config.ini");
IconTool::ClearCache();
}
void Widget::save_record()
{
BatteryRecord::GetInstance()->AddRecord(
time(nullptr),
Battery::percent,
Battery::status==Battery::Status::CHARGING
);
}
//充电背景色
void Widget::on_btn_scbc_clicked()
{
auto cf=Config::GetInstance();
cld_c_->setCurrentColor(cf->color_charging);
// qDebug()<<"show "<<QString::number(cf->color_charging.rgba(),16);
cld_c_->show();
}
//使用电池的背景色
void Widget::on_btn_subc_clicked()
{
auto cf=Config::GetInstance();
cld_b_->setCurrentColor(cf->color_us_bt);
cld_b_->show();
}
void Widget::on_tabWidget_currentChanged(int index)
{
// qDebug()<<index;
if(index==1){
on_select_tab_rec(index);
}
}
void Widget::on_select_tab_rec(int )
{
update_chart();
}
//点击选择使用电池时的字体颜色
void Widget::on_btn_color_font_u_clicked()
{
auto cf=Config::GetInstance();
cld_f_u_->setCurrentColor(cf->color_font_u);
cld_f_u_->show();
}
//点击选择充电时的字体颜色
void Widget::on_btn_color_font_c_clicked()
{
auto cf =Config::GetInstance();
cld_f_c_->setCurrentColor(cf->color_font_c);
cld_f_c_->show();
}