106 lines
3.4 KiB
C++
106 lines
3.4 KiB
C++
#include "PerformanceShower_p.h"
|
|
#include <QPainter>
|
|
#include <QVBoxLayout>
|
|
|
|
namespace YSG {
|
|
PerformanceShower::PerformanceShower(QWidget *parent) : QWidget(parent) {
|
|
setAttribute(Qt::WA_OpaquePaintEvent, true);
|
|
auto layout = new QVBoxLayout(this);
|
|
layout->setSpacing(0);
|
|
|
|
}
|
|
|
|
|
|
void PerformanceShower::refreshCounter() {
|
|
for(CounterLine& value : mCounter) {
|
|
value.mCountTimes = 0;
|
|
value.mTotalStart = std::chrono::system_clock::now();
|
|
}
|
|
}
|
|
|
|
void PerformanceShower::counterIncrease(const QString& counterName) {
|
|
if(!mCounter.contains(counterName)) {
|
|
mCounter[counterName].mName = counterName;
|
|
mCounter[counterName].mTotalStart = std::chrono::system_clock::now();
|
|
}
|
|
mCounter[counterName].mCountTimes++;
|
|
}
|
|
|
|
void PerformanceShower::paintEvent(QPaintEvent *event) {
|
|
QPainter painter(this);
|
|
QPoint topLeft = rect().topLeft();
|
|
int x = topLeft.x() + mLeftMargin;
|
|
int y = topLeft.y() + mTopMargin;
|
|
painter.fillRect(rect(), Qt::white);
|
|
painter.setFont(mFont);
|
|
{
|
|
SpinLockGuard lock(&mMutex);
|
|
QFontMetrics fm(mFont);
|
|
int n = mAllInfo.size();
|
|
for (int i = 0; i < n; i++) {
|
|
painter.drawText(topLeft + QPointF(x, y + i * fm.lineSpacing() + fm.lineSpacing()), mAllInfo[i]);
|
|
}
|
|
}
|
|
painter.end();
|
|
}
|
|
|
|
void PerformanceShower::mousePressEvent(QMouseEvent *event) {
|
|
if (mMoving) return;
|
|
if (event->button() == Qt::LeftButton) {
|
|
mMoving = true;
|
|
mStartPos = pos();
|
|
mStartGlobalPos = event->globalPos();
|
|
}
|
|
event->accept();
|
|
}
|
|
|
|
void PerformanceShower::mouseMoveEvent(QMouseEvent *event) {
|
|
if (!mMoving) return;
|
|
move(mStartPos + event->globalPos() - mStartGlobalPos);
|
|
event->accept();
|
|
}
|
|
void PerformanceShower::mouseReleaseEvent(QMouseEvent *event) {
|
|
if (!mMoving) return;
|
|
mMoving = false;
|
|
event->accept();
|
|
}
|
|
|
|
QVector<QString> PerformanceShower::all() {
|
|
QVector<QString> infoList = mFixedInfo;
|
|
for(PerformanceLine& l: mInfoMap) infoList.append(l.toString());
|
|
for(CounterLine& c: mCounter) infoList.append(c.toString());
|
|
for (auto it = mInfos.cbegin(); it != mInfos.cend(); ++it) {
|
|
infoList.append(QString("%1: %2").arg(it.key(), it.value()));
|
|
}
|
|
return infoList;
|
|
}
|
|
|
|
void PerformanceShower::refresh() {
|
|
{
|
|
SpinLockGuard lock(&mMutex);
|
|
mAllInfo = all();
|
|
int n = mAllInfo.size();
|
|
if (mOldSize != n) {
|
|
int maxWidth = 0;
|
|
QFontMetrics fm(mFont);
|
|
for (int i = 0; i < n; i++) maxWidth = qMax(maxWidth, fm.horizontalAdvance(mAllInfo[i]));
|
|
int w = maxWidth + mLeftMargin + mRightMargin;
|
|
int h = fm.lineSpacing() * n + mTopMargin + mBottomMargin;
|
|
QMetaObject::invokeMethod(this, "firstResize", Q_ARG(int, w), Q_ARG(int, h));
|
|
}
|
|
mOldSize = n;
|
|
}
|
|
QMetaObject::invokeMethod(this, "update");
|
|
}
|
|
|
|
|
|
|
|
void PerformanceShower::firstResize(int w, int h) {
|
|
//qDebug() << "PerformanceShower::firstResize(int w, int h) " << w << " " << h;
|
|
resize(w, h);
|
|
}
|
|
void PerformanceShower::resizeEvent(QResizeEvent *event) {
|
|
event->accept();
|
|
}
|
|
}
|