Skip to content

Commit

Permalink
Optimize repainting with very large paintings
Browse files Browse the repository at this point in the history
  • Loading branch information
schorsch1976 committed Jun 23, 2019
1 parent 40ce3f3 commit 231b8be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ void MainWindow::OnAbout()
void MainWindow::ApplySettings()
{
std::vector<QToolButton *> buttons = {
ui->btnNew, ui->btnRedo, ui->btnUndo,
ui->btnAbout, ui->btnExport, ui->btnImport,
ui->btnNew, ui->btnRedo, ui->btnUndo,
ui->btnAbout, ui->btnExport, ui->btnImport,
ui->btnSettings,

ui->btnToolLine, ui->btnToolMove, ui->btnToolText,
ui->btnToolArrow, ui->btnToolClass, ui->btnToolErase,
ui->btnToolFreehand, ui->btnToolRectangle, ui->btnToolResize};
ui->btnToolLine, ui->btnToolMove, ui->btnToolText,
ui->btnToolArrow, ui->btnToolClass, ui->btnToolErase,
ui->btnToolFreehand, ui->btnToolRectangle, ui->btnToolResize};

QSize size(m_preferences.icon_size, m_preferences.icon_size);
Qt::ToolButtonStyle style =
Expand Down
35 changes: 22 additions & 13 deletions src/qasciiart.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "qasciiart.h"

#include <QChar>
#include <QDebug>
#include <QFontDatabase>
#include <QMouseEvent>
#include <QPainter>
Expand Down Expand Up @@ -72,22 +73,30 @@ void QAsciiArt::SetSlideStart(QPoint point) { m_slide_start = point; }

// Override from QWidget

void QAsciiArt::paintEvent(QPaintEvent * /* event */)
void QAsciiArt::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.eraseRect(0, 0, width() - 1, height() - 1);
QRect area = event->rect();
qDebug() << "Repainting: " << area;

painter.eraseRect(area);
painter.setRenderHint(QPainter::Antialiasing, true);

QPoint TopLeft = ScreenToText(QPoint(area.x(), area.y()));
QPoint DownRight =
ScreenToText(QPoint(area.x() + area.width(), area.y() + area.height()));
qDebug() << "TopLeft: " << TopLeft;
qDebug() << "DownRight: " << DownRight;

// background
{
QBrush brush;
brush.setColor(Qt::white);
brush.setStyle(Qt::BrushStyle::SolidPattern);
painter.setBrush(brush);

QPoint screenpos1 = TextToScreen(QPoint(0, 0));
QPoint screenpos2 =
TextToScreen(QPoint(m_data.Width(), m_data.Height()));
QPoint screenpos1 = TextToScreen(TopLeft);
QPoint screenpos2 = TextToScreen(DownRight);

QRect screenpos(screenpos1.x(), screenpos1.y(),
screenpos2.x() - screenpos1.x(),
Expand All @@ -102,17 +111,17 @@ void QAsciiArt::paintEvent(QPaintEvent * /* event */)
pen.setWidthF(0.5);
painter.setPen(pen);

for (int x = 0; x < m_data.Width(); ++x)
for (int x = TopLeft.x(); x < DownRight.x(); ++x)
{
QPoint screenpos1 = TextToScreen(QPoint(x, 0));
QPoint screenpos2 = TextToScreen(QPoint(x, m_data.Height()));
QPoint screenpos1 = TextToScreen(QPoint(x, TopLeft.y()));
QPoint screenpos2 = TextToScreen(QPoint(x, DownRight.y()));
painter.drawLine(screenpos1, screenpos2);
}

for (int y = 0; y < m_data.Height(); ++y)
for (int y = TopLeft.y(); y < DownRight.y(); ++y)
{
QPoint screenpos1 = TextToScreen(QPoint(0, y));
QPoint screenpos2 = TextToScreen(QPoint(m_data.Width(), y));
QPoint screenpos1 = TextToScreen(QPoint(TopLeft.x(), y));
QPoint screenpos2 = TextToScreen(QPoint(DownRight.x(), y));
painter.drawLine(screenpos1, screenpos2);
}
}
Expand Down Expand Up @@ -154,9 +163,9 @@ void QAsciiArt::paintEvent(QPaintEvent * /* event */)
pen.setWidthF(1);
painter.setPen(pen);

for (int y = 0; y < m_data.Height(); ++y)
for (int y = TopLeft.y(); y < DownRight.y(); ++y)
{
for (int x = 0; x < m_data.Width(); ++x)
for (int x = TopLeft.x(); x < DownRight.x(); ++x)
{
QPoint screenpos = TextToScreen(QPoint(x, y));

Expand Down

0 comments on commit 231b8be

Please sign in to comment.