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 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
|
/*
logview.cpp
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Giulio Camuffo <giulio.camuffo@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "logview.h"
#include <QMouseEvent>
#include <QScrollBar>
#include <QStaticText>
#include <QPainter>
#include <QScrollArea>
#include <QClipboard>
#include <QApplication>
#include <QtMath>
#include "ringbuffer.h"
namespace GammaRay {
class View : public QWidget
{
public:
explicit View(QWidget *p)
: QWidget(p)
, m_lines(5000)
, m_metrics(QFont())
, m_lineHeight(m_metrics.height())
, m_client(0)
{
resize(0, 0);
setFocusPolicy(Qt::ClickFocus);
setCursor(Qt::IBeamCursor);
}
QSize sizeHint() const override
{
return size();
}
void drawLine(QPainter &painter, const QRect &rect, const QStaticText &line)
{
painter.setPen(palette().color(QPalette::Text));
painter.drawStaticText(0, rect.y(), line);
}
void drawLineSelected(QPainter &painter, const QRect &rect, const QStaticText &line)
{
painter.fillRect(rect, palette().highlight());
painter.setPen(palette().color(QPalette::HighlightedText));
painter.drawStaticText(0, rect.y(), line);
}
void drawLinePartialSelected(QPainter &painter, const QRect &rect, const QStaticText &line, int startSelectChar, int endSelectChar)
{
const QString &text = line.text();
const int startX = m_metrics.horizontalAdvance(text.left(startSelectChar));
const int endX = m_metrics.horizontalAdvance(text.left(endSelectChar));
if (startSelectChar > 0) {
painter.drawText(QRect(rect.x(), rect.y(), startX, rect.height()), Qt::TextDontClip, text.left(startSelectChar));
}
QRect selectRect(rect.x() + startX, rect.y(), endX - startX, rect.height());
painter.fillRect(selectRect, palette().highlight());
painter.setPen(palette().color(QPalette::HighlightedText));
painter.drawText(selectRect, Qt::TextDontClip, text.mid(startSelectChar, endSelectChar - startSelectChar));
if (endSelectChar < text.size()) {
painter.setPen(palette().color(QPalette::Text));
painter.drawText(QRect(rect.x() + endX, rect.y(), m_metrics.horizontalAdvance(text) - endX, rect.height()), text.mid(endSelectChar));
}
}
void selectionBoundaries(QPoint &start, QPoint &end) const
{
bool startBeforeEnd = (m_selectionStart.y() < m_selectionEnd.y()) || (m_selectionStart.y() == m_selectionEnd.y() && m_selectionStart.x() < m_selectionEnd.x());
start = startBeforeEnd ? m_selectionStart : m_selectionEnd;
end = startBeforeEnd ? m_selectionEnd : m_selectionStart;
}
struct LineSelection
{
bool isNull() const
{
return start == end;
}
bool isFull() const
{
return start == 0 && end < 0;
}
int start;
int end;
};
LineSelection lineSelection(int line) const
{
if (m_selectionStart == m_selectionEnd) {
return { 0, 0 };
}
QPoint start, end;
selectionBoundaries(start, end);
if (start.y() < line && line < end.y()) {
return { 0, ( int )m_lines.at(line).text.text().size() };
}
if (start.y() == line || end.y() == line) {
int startChar = 0;
int endChar = m_lines.at(line).text.text().size();
if (start.y() == line)
startChar = start.x();
if (end.y() == line)
endChar = end.x() + 1;
return { startChar, endChar };
}
return { 0, 0 };
}
void paintEvent(QPaintEvent *event) override
{
if (m_lineHeight < 0) {
return;
}
QPainter painter(this);
QRectF drawRect = event->rect();
int startingLine = lineAt(drawRect.y());
int y = linePosAt(drawRect.y());
for (int i = startingLine; i < m_lines.size(); ++i) {
if (m_client && m_lines.at(i).pid != m_client) {
continue;
}
const QStaticText &text = m_lines.at(i).text;
QRect lineRect(QRect(0, y, text.size().width(), m_lineHeight));
painter.fillRect(QRectF(0, y, drawRect.width(), m_lineHeight), i % 2 ? palette().base() : palette().alternateBase());
LineSelection selection = lineSelection(i);
if (selection.isNull()) {
drawLine(painter, lineRect, text);
} else if (selection.isFull()) {
drawLineSelected(painter, lineRect, text);
} else {
drawLinePartialSelected(painter, lineRect, text, selection.start, selection.end);
}
y += m_lineHeight;
if (y >= drawRect.bottom())
break;
}
}
inline int linesCount() const
{
return m_client ? m_linesCount.value(m_client) : m_lines.size();
}
inline int linePosAt(int y) const
{
int line = qMin(y / m_lineHeight, m_lines.size() - 1);
return line * m_lineHeight;
}
inline int lineAt(int y) const
{
int line = qMin(y / m_lineHeight, m_lines.size() - 1);
if (!m_client) {
return line;
}
for (int i = 0, l = 0; i < m_lines.size(); ++i) {
if (m_lines.at(i).pid == m_client) {
if (l++ == line) {
return i;
}
}
}
return line;
}
inline QPoint charPosAt(const QPointF &p) const
{
int line = lineAt(p.y());
int lineX = 0;
const QString &text = m_lines.at(line).text.text();
for (int x = 0, i = 0; i < text.size(); ++i) {
const QChar &c = text.at(i);
if (p.x() >= x) {
lineX = i;
}
x += m_metrics.horizontalAdvance(c);
}
return { lineX, line };
}
void mousePressEvent(QMouseEvent *e) override
{
if (e->button() == Qt::LeftButton) {
m_selectionStart = m_selectionEnd = charPosAt(e->pos());
e->accept();
update();
}
}
void mouseMoveEvent(QMouseEvent *e) override
{
m_selectionEnd = charPosAt(e->pos());
e->accept();
update();
}
QString selectedText() const
{
if (m_selectionStart == m_selectionEnd) {
return QString();
}
QPoint start, end;
selectionBoundaries(start, end);
QString string;
for (int i = start.y(); i <= end.y(); ++i) {
if (m_client && m_lines.at(i).pid != m_client) {
continue;
}
const QStaticText &line = m_lines.at(i).text;
LineSelection selection = lineSelection(i);
string += line.text().mid(selection.start, selection.end - selection.start);
string += QLatin1Char('\n');
}
return string;
}
void keyPressEvent(QKeyEvent *e) override
{
if (e->key() == Qt::Key_C && e->modifiers() == Qt::ControlModifier) {
QApplication::clipboard()->setText(selectedText());
}
}
void resetSelection()
{
m_selectionStart = m_selectionEnd = QPoint();
update();
}
struct Line
{
quint64 pid = 0;
QStaticText text;
int *counter = nullptr;
Line() = default;
Line(quint64 p, const QStaticText &t, int *cnt)
: pid(p)
, text(t)
, counter(cnt)
{
(*counter)++;
}
Line(const Line &l)
: pid(l.pid)
, text(l.text)
, counter(l.counter)
{
(*counter)++;
}
~Line()
{
(*counter)--;
}
Line &operator=(const Line &l)
{
(*counter)--;
pid = l.pid;
text = l.text;
counter = l.counter;
(*counter)++;
return *this;
}
};
RingBuffer<Line> m_lines;
QHash<quint64, int> m_linesCount;
QFontMetricsF m_metrics;
int m_lineHeight;
QPoint m_selectionStart;
QPoint m_selectionEnd;
quint64 m_client;
};
class Messages : public QScrollArea
{
public:
explicit Messages(QWidget *parent)
: QScrollArea(parent)
, m_view(new View(this))
{
m_view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setWidget(m_view);
setWidgetResizable(true);
}
void logMessage(quint64 pid, qint64 time, const QByteArray &msg)
{
auto scrollbar = verticalScrollBar();
bool scroll = scrollbar->value() >= scrollbar->maximum();
add(pid, time, msg);
if (scroll)
scrollbar->setValue(scrollbar->maximum());
}
void reset() const
{
m_view->m_lines.clear();
m_view->resize(0, 0);
}
void updateSize() const
{
QSizeF lineSize = m_view->m_lines.last().text.size();
int w = m_view->width();
int h = m_view->linesCount() * m_view->m_lineHeight;
if (lineSize.width() > w) {
w = lineSize.width();
}
m_view->resize(w, h);
m_view->update();
}
void add(quint64 pid, qint64 time, const QByteArray &m)
{
m_view->m_lines.append(View::Line(pid, QStaticText(QString("[%1ms] %2").arg(QString::number(time / 1e6), QString(m))), &m_view->m_linesCount[pid]));
if (m_view->m_client && pid != m_view->m_client) {
return;
}
updateSize();
}
void setLoggingClient(quint64 pid)
{
m_view->m_client = pid;
auto scrollbar = verticalScrollBar();
qreal v = ( qreal )scrollbar->value() / ( qreal )scrollbar->maximum();
m_view->resetSelection();
updateSize();
// keep the scrollbar at he same percentage
scrollbar->setValue(v * ( qreal )scrollbar->maximum());
}
View *m_view;
};
class Timeline : public QScrollArea
{
public:
class View : public QWidget
{
public:
struct DataPoint
{
qint64 time;
quint64 pid;
QByteArray msg;
};
View()
: m_data(5000)
{
resize(100, 100);
setAttribute(Qt::WA_OpaquePaintEvent);
setMouseTracking(true);
}
QSize sizeHint() const override
{
return size();
}
inline qint64 initialTime() const
{
return m_data.isEmpty() ? 0 : m_data.at(0).time;
}
void paintEvent(QPaintEvent *event) override
{
QPainter painter(this);
QRectF drawRect = event->rect();
const auto palette = this->palette();
painter.fillRect(drawRect, palette.base());
qreal l = 1;
qreal step = l / m_zoom;
while (step < 60) {
l *= 10;
step = l / m_zoom;
}
int substeps = 5;
int mul = 2;
while (step / substeps > 60) {
substeps *= mul;
mul = mul == 2 ? 5 : 2;
}
auto it = initialTime();
auto rit = round(it, -1);
// draw the grid lines
qreal linesSpacing = step / substeps;
int startLine = drawRect.left() / linesSpacing - (rit - it) / m_zoom; // round the starting position so that we have nice numbers'
int s = startLine;
// NOLINTNEXTLINE (clang-analyzer-security.FloatLoopCounter)
for (qreal i = startLine * linesSpacing; i < drawRect.right(); i += linesSpacing, s++) {
bool isStep = s % substeps == 0;
painter.setPen(isStep ? palette.color(QPalette::Highlight) : palette.color(QPalette::Midlight));
int y = 0;
if (isStep) {
int stepN = s / substeps;
y = 15 * (stepN % 2 + 1);
}
painter.drawLine(i, y, i, drawRect.bottom());
}
// draw the text after having drawn all the lines, so we're sure they don't go over it
s = startLine;
painter.setPen(palette.color(QPalette::Highlight));
// NOLINTNEXTLINE (clang-analyzer-security.FloatLoopCounter)
for (qreal i = startLine * linesSpacing; i < drawRect.right(); i += step / substeps, s++) { // krazy:exclude=postfixop
bool isStep = s % substeps == 0;
if (isStep) {
painter.drawText(i - 100, ((s / substeps) % 2) * 15, 200, 200, Qt::AlignHCenter, QString("%1ms").arg(QString::number(qreal(it + i * m_zoom) / 1e6, 'g', 6)));
}
}
// finally draw the event lines
painter.setPen(palette.color(QPalette::Text));
bool hasDrawn = false;
for (int i = 0; i < m_data.size(); ++i) {
const auto &point = m_data.at(i);
if (m_client && point.pid != m_client) {
painter.setPen(palette.color(QPalette::Dark));
} else {
painter.setPen(palette.color(QPalette::Text));
}
qreal offset = point.time - m_start;
qreal x = offset / m_zoom;
qreal y = qMax(qreal(40.), drawRect.y());
if (!drawRect.contains(QPoint(x, y))) {
if (hasDrawn)
break;
else
continue;
}
hasDrawn = true;
painter.drawLine(x, y, x, drawRect.bottom());
}
}
void mouseMoveEvent(QMouseEvent *e) override
{
const QPointF &pos = e->position();
for (int i = 0; i < m_data.size(); ++i) {
qreal timex = (m_data.at(i).time - m_start) / m_zoom;
if (fabs(pos.x() - timex) < 2) {
setToolTip(m_data.at(i).msg);
return;
}
}
}
static qint64 round(qint64 time, int direction)
{
qint64 v = time % 200;
return time + direction * v;
}
void updateSize()
{
if (m_data.size() == 0)
return;
m_start = round(m_data.at(0).time, -1);
m_timespan = round(m_data.last().time, 1) - m_start;
resize(m_timespan / m_zoom, height());
}
RingBuffer<DataPoint> m_data;
qreal m_zoom = 100000;
qint64 m_start = 0;
qint64 m_timespan = 0;
quint64 m_client = 0;
};
explicit Timeline(QWidget *parent)
: QScrollArea(parent)
{
m_view.setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
setWidget(&m_view);
setWidgetResizable(true);
m_view.installEventFilter(this);
}
void logMessage(quint64 pid, qint64 time, const QByteArray &msg)
{
m_view.m_data.append({ time, pid, msg });
m_view.updateSize();
}
void setLoggingClient(quint64 pid)
{
m_view.m_client = pid;
m_view.update();
}
bool eventFilter(QObject *o, QEvent *e) override
{
if (o == &m_view && e->type() == QEvent::Wheel) {
QWheelEvent *we = static_cast<QWheelEvent *>(e);
qreal pos = we->position().x() * m_view.m_zoom;
auto sb = horizontalScrollBar();
int sbvalue = horizontalScrollBar()->value();
m_view.m_zoom += (1. - qPow(5. / 4., qreal(we->angleDelta().y()) / 150.)) * m_view.m_zoom;
if (m_view.m_zoom < 10) {
m_view.m_zoom = 10;
}
m_view.updateSize();
// keep the point under the mouse still, if possible
pos = pos / m_view.m_zoom;
sb->setValue(sbvalue + (0.5 + pos - we->position().x()));
}
return QScrollArea::eventFilter(o, e);
}
View m_view;
};
LogView::LogView(QWidget *p)
: QTabWidget(p)
, m_messages(new Messages(this))
, m_timeline(new Timeline(this))
{
setTabPosition(QTabWidget::West);
addTab(m_messages, tr("Messages"));
addTab(m_timeline, tr("Timeline"));
}
QSize LogView::sizeHint() const
{
return { 200, 200 };
}
void LogView::logMessage(quint64 pid, qint64 time, const QByteArray &msg)
{
m_messages->logMessage(pid, time, msg);
m_timeline->logMessage(pid, time, msg);
}
void LogView::setLoggingClient(quint64 pid)
{
m_messages->setLoggingClient(pid);
m_timeline->setLoggingClient(pid);
}
void LogView::reset()
{
m_messages->reset();
}
}
|