Commit ef488d33 authored by David Hurka's avatar David Hurka 🐬 Committed by Albert Astals Cid
Browse files

Generate presentation drawing tool icons in a custom QIconEngine, so...

Generate presentation drawing tool icons in a custom QIconEngine, so checkboxes can be sharp on hidpi

The checkboxes in the drawing tool icons in the presentation toolbar were not sharp on hiDPI, this fixes it.

The toolbar buttons are QActions with QIcons, and the icons were created from 25x25 pixmaps. That is usually smaller than the toolbar’s icon size, so the icons had a nice size. But at hiDPI, the icons were scaled up, so the checkmarks looked blurry.

Now the icons are generated in the correct resolution on the fly, because the painting code from ColorAction::setColor() is moved to a custom QIconEngine inside ColorAction.
parent 5abcbea6
Pipeline #18610 passed with stage
in 19 minutes and 18 seconds
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <KLocalizedString> #include <KLocalizedString>
#include <QAction> #include <QAction>
#include <QIconEngine>
#include <QPainter> #include <QPainter>
class ColorAction : public QAction class ColorAction : public QAction
...@@ -31,28 +32,70 @@ public: ...@@ -31,28 +32,70 @@ public:
void setColor( const QColor &color ) void setColor( const QColor &color )
{ {
QPixmap pm( 25, 25 ); setIcon( QIcon( new ColorActionIconEngine( color ) ) );
pm.fill( color ); }
QIcon icon; protected:
class ColorActionIconEngine : public QIconEngine
{
public:
explicit ColorActionIconEngine( const QColor &color )
: m_color( color )
{
}
icon.addPixmap( pm, QIcon::Normal, QIcon::Off ); ColorActionIconEngine( const ColorActionIconEngine& ) = delete;
ColorActionIconEngine &operator=( const ColorActionIconEngine& ) = delete;
QPixmap pmSel( pm ); // No one needs clone(), but it’s pure virtual
QPainter p( &pmSel ); QIconEngine* clone() const override
QFont font = p.font(); {
font.setPixelSize( pmSel.height() * 0.9 ); return nullptr;
p.setFont( font ); }
// draw check mark QPixmap pixmap( const QSize &size, QIcon::Mode mode, QIcon::State state ) override
const int lightness = ((color.red() * 299) + (color.green() * 587) + (color.blue() * 114)) / 1000; {
p.setPen( lightness < 128 ? Qt::white : Qt::black ); QPixmap pixmap( size );
p.drawText( QRect( QPoint( 0, 0 ), pmSel.size() ), Qt::AlignCenter, QStringLiteral("\u2713") ); pixmap.fill( Qt::transparent );
Q_ASSERT( pixmap.hasAlphaChannel() );
icon.addPixmap( pmSel, QIcon::Normal, QIcon::On ); QPainter painter( &pixmap );
paint( &painter, QRect( QPoint( 0, 0), size ), mode, state );
return pixmap;
}
setIcon( icon ); void paint( QPainter * painter,
} const QRect &rect,
QIcon::Mode mode,
QIcon::State state ) override
{
Q_UNUSED( mode )
// Assume that rect is square and at position (0, 0)
int squareSize = rect.height() * 0.8;
int squareOffset = ( rect.height() - squareSize ) / 2;
painter->fillRect( squareOffset, squareOffset, squareSize, squareSize, m_color );
if ( state == QIcon::On ) {
QFont checkmarkFont = painter->font();
checkmarkFont.setPixelSize( squareSize * 0.9 );
painter->setFont( checkmarkFont );
const int lightness = ( ( m_color.red() * 299 )
+ ( m_color.green() * 587 )
+ ( m_color.blue() * 114 ) ) / 1000;
painter->setPen( lightness < 128 ? Qt::white : Qt::black );
painter->drawText( QRect( squareOffset, squareOffset, squareSize, squareSize ),
Qt::AlignCenter,
QStringLiteral("\u2713") );
}
}
protected:
QColor m_color;
};
}; };
DrawingToolActions::DrawingToolActions( KActionCollection *parent ) DrawingToolActions::DrawingToolActions( KActionCollection *parent )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment