mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-03 17:34:12 +02:00
3e34a6d55f
QGraphicsView::render() paints only what the scene draws, so a scene without a background brush left the off-screen buffer uninitialized and the inversion turned that memory into noise. The buffer is now filled white first, which the inversion turns into the Base color. listenToScene() connected a new receiver on every setScene() call and never dropped the previous scene's. It now keeps a single connection and replaces it. Test in tst_qetpalette: paletteViewFillsWhatTheSceneLeavesBlank.
74 lines
2.7 KiB
C++
74 lines
2.7 KiB
C++
/*
|
|
Copyright 2006-2026 The QElectroTech Team
|
|
This file is part of QElectroTech.
|
|
|
|
QElectroTech is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
QElectroTech is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#ifndef PALETTE_GRAPHICS_VIEW_H
|
|
#define PALETTE_GRAPHICS_VIEW_H
|
|
|
|
#include <QGraphicsView>
|
|
|
|
class QPainter;
|
|
|
|
/**
|
|
A QGraphicsView that shows its scene with inverted lightness while its
|
|
palette is dark: white becomes the palette's Base color, black its Text
|
|
color, and colored strokes keep their hue. The scene itself is left as
|
|
drawn, so printing and exporting it still give black on white. On a
|
|
light palette the view paints exactly as QGraphicsView does.
|
|
|
|
The view paints through QGraphicsView::render() into an image and blits
|
|
the inverted image. QGraphicsView delivers scene updates straight to
|
|
its viewport when nobody listens to QGraphicsScene::changed(), and in
|
|
that mode the scene clears its "update everything" flag only when the
|
|
items are painted straight onto the viewport, which never happens
|
|
here: from the second QGraphicsScene::update() on, every scene update
|
|
and every item update would wait for an unrelated repaint. So the view
|
|
listens to changed() on every scene it is given, which makes the scene
|
|
clear the flag before it emits. Set the scene through this class, not
|
|
through a QGraphicsView pointer.
|
|
*/
|
|
class PaletteGraphicsView : public QGraphicsView
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
explicit PaletteGraphicsView(QWidget *parent = nullptr);
|
|
explicit PaletteGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr);
|
|
|
|
void setScene(QGraphicsScene *scene);
|
|
bool invertsLightness() const;
|
|
|
|
protected:
|
|
bool eventFilter(QObject *watched, QEvent *event) override;
|
|
void paintEvent(QPaintEvent *event) override;
|
|
/**
|
|
Called with true right before the scene is rendered for an
|
|
inverted display and with false right after, so a scene can
|
|
adapt what it draws (a softer grid, for instance). Does
|
|
nothing by default.
|
|
*/
|
|
virtual void paintingInverted(bool inverted);
|
|
|
|
private:
|
|
void listenToScene(QGraphicsScene *scene);
|
|
void paintInverted(const QRect &area);
|
|
void drawRubberBand(QPainter &painter);
|
|
|
|
QMetaObject::Connection m_scene_connection;
|
|
};
|
|
|
|
#endif
|