Add some micro-optimizations in the painting process

This commit is contained in:
lupoDharkael
2017-08-10 14:57:34 +02:00
parent c3bb1814c4
commit 6ed8ad5597
4 changed files with 30 additions and 17 deletions

View File

@@ -59,6 +59,7 @@ CaptureButton::CaptureButton(const ButtonType t, QWidget *parent) : QPushButton(
}
void CaptureButton::initButton() {
setMouseTracking(true);
m_tool = ToolFactory().CreateTool(m_buttonType, this);
connect(this, &CaptureButton::pressed, m_tool, &CaptureTool::onPressed);
@@ -146,8 +147,14 @@ void CaptureButton::mousePressEvent(QMouseEvent *) {
void CaptureButton::animatedShow() {
show();
emergeAnimation->start();
if(!isVisible()) {
setMouseTracking(false);
show();
emergeAnimation->start();
connect(emergeAnimation, &QPropertyAnimation::finished, this, [this](){
setMouseTracking(true);
});
}
}
CaptureButton::ButtonType CaptureButton::buttonType() const {

View File

@@ -131,7 +131,6 @@ void CaptureWidget::updateButtons() {
void CaptureWidget::paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// if we are creating a new modification to the screenshot we just draw
// a temporal modification without antialiasing in the pencil tool for
@@ -190,6 +189,7 @@ void CaptureWidget::paintEvent(QPaintEvent *) {
painter.drawRect(r);
// paint handlers
painter.setRenderHint(QPainter::Antialiasing);
updateHandles();
painter.setBrush(m_uiColor);
for(auto r: handleMask().rects()) {
@@ -225,7 +225,6 @@ void CaptureWidget::mousePressEvent(QMouseEvent *e) {
}
}
updateCursor();
//update();
}
void CaptureWidget::mouseMoveEvent(QMouseEvent *e) {
@@ -237,6 +236,7 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent *e) {
}
if (m_newSelection) {
m_selection = QRect(m_dragStartPoint, m_mousePos).normalized();
update();
} else if (!m_mouseOverHandle) {
// Moving the whole selection
QRect r = rect().normalized();
@@ -255,6 +255,7 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent *e) {
} if (!r.contains(QPoint(r.center().x(), m_selection.bottom()))) {
m_selection.setBottom(r.bottom());
}
update();
} else {
// Dragging a handle
QRect r = m_selectionBeforeDrag;
@@ -294,9 +295,12 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent *e) {
}
}
m_selection = r.normalized();
update();
}
} else if (m_mouseIsClicked) {
} else if (m_mouseIsClicked && m_state != CaptureButton::TYPE_MOVESELECTION) {
// drawing with a tool
m_modifications.last()->addPoint(e->pos());
update();
// hides the group of buttons under the mouse, if you leave
if (m_buttonHandler->buttonsAreInside()) {
bool containsMouse = m_buttonHandler->contains(m_mousePos);
@@ -308,7 +312,6 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent *e) {
}
} else {
if (m_selection.isNull()) {
update();
return;
}
bool found = false;
@@ -324,7 +327,6 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent *e) {
}
updateCursor();
}
update();
}
void CaptureWidget::mouseReleaseEvent(QMouseEvent *e) {
@@ -335,6 +337,7 @@ void CaptureWidget::mouseReleaseEvent(QMouseEvent *e) {
// register the last point and add the whole modification to the screenshot
} else if (m_mouseIsClicked && m_state != CaptureButton::TYPE_MOVESELECTION) {
m_screenshot->paintModification(m_modifications.last());
update();
}
if (!m_buttonHandler->isVisible() && !m_selection.isNull()) {
@@ -347,7 +350,6 @@ void CaptureWidget::mouseReleaseEvent(QMouseEvent *e) {
m_grabbing = false;
updateCursor();
update();
}
void CaptureWidget::keyPressEvent(QKeyEvent *e) {
@@ -356,18 +358,24 @@ void CaptureWidget::keyPressEvent(QKeyEvent *e) {
} else if (e->key() == Qt::Key_Up
&& m_selection.top() > rect().top()) {
m_selection.moveTop(m_selection.top()-1);
m_buttonHandler->updatePosition(m_selection, rect());
update();
} else if (e->key() == Qt::Key_Down
&& m_selection.bottom() < rect().bottom()) {
m_selection.moveBottom(m_selection.bottom()+1);
m_buttonHandler->updatePosition(m_selection, rect());
update();
} else if (e->key() == Qt::Key_Left
&& m_selection.left() > rect().left()) {
m_selection.moveLeft(m_selection.left()-1);
m_buttonHandler->updatePosition(m_selection, rect());
update();
} else if (e->key() == Qt::Key_Right
&& m_selection.right() < rect().right()) {
m_selection.moveRight(m_selection.right()+1);
m_buttonHandler->updatePosition(m_selection, rect());
update();
}
m_buttonHandler->updatePosition(m_selection, rect());
update();
}
QString CaptureWidget::saveScreenshot(bool toClipboard) {
@@ -460,7 +468,7 @@ bool CaptureWidget::undo() {
if (!m_modifications.isEmpty()) {
m_modifications.last()->deleteLater();
m_modifications.pop_back();
m_screenshot->paintBaseModifications(m_modifications);
m_screenshot->overrideModifications(m_modifications);
update();
itemRemoved = true;
}
@@ -524,7 +532,6 @@ void CaptureWidget::handleButtonSignal(CaptureTool::Request r) {
default:
break;
}
update();
}
void CaptureWidget::leaveButton() {

View File

@@ -122,7 +122,6 @@ QString Screenshot::fileSave(bool &ok, const QRect &selection) const {
QPixmap Screenshot::paintModification(const CaptureModification *modification) {
QPainter painter(&m_modifiedScreenshot);
painter.setRenderHint(QPainter::Antialiasing);
paintInPainter(painter, modification);
return m_modifiedScreenshot;
}
@@ -132,7 +131,7 @@ QPixmap Screenshot::paintModification(const CaptureModification *modification) {
QPixmap Screenshot::paintTemporalModification(
const CaptureModification *modification)
{
QPixmap tempPix = m_modifiedScreenshot;
QPixmap tempPix(m_modifiedScreenshot);
QPainter painter(&tempPix);
if (modification->buttonType() != CaptureButton::TYPE_PENCIL) {
painter.setRenderHint(QPainter::Antialiasing);
@@ -143,7 +142,7 @@ QPixmap Screenshot::paintTemporalModification(
// paintBaseModifications overrides the modifications of the screenshot
// with new ones.
QPixmap Screenshot::paintBaseModifications(
QPixmap Screenshot::overrideModifications(
const QVector<CaptureModification*> &m)
{
m_modifiedScreenshot = m_baseScreenshot;

View File

@@ -45,14 +45,14 @@ public:
const QRect &selection = QRect());
QPixmap paintModification(const CaptureModification*);
QPixmap paintTemporalModification(const CaptureModification*);
QPixmap paintBaseModifications(const QVector<CaptureModification*> &);
QPixmap overrideModifications(const QVector<CaptureModification*> &);
private:
QPixmap m_baseScreenshot;
QPixmap m_modifiedScreenshot;
QPointer<QNetworkAccessManager> m_accessManager;
void paintInPainter(QPainter &, const CaptureModification *);
inline void paintInPainter(QPainter &, const CaptureModification *);
};