Using scaled dither pattern pixmaps in the UI elements

This commit is contained in:
Matthias Koefferlein 2022-09-18 17:37:49 +02:00
parent 48e3643a87
commit e5fabb2b13
5 changed files with 58 additions and 80 deletions

View File

@ -560,7 +560,7 @@ DitherPatternInfo::operator< (const DitherPatternInfo &d) const
// TODO including a scaling algorithm in this formula, or give more resolution to the dither
QBitmap
DitherPatternInfo::get_bitmap (int width, int height) const
DitherPatternInfo::get_bitmap (int width, int height, int frame_width) const
{
if (height < 0) {
height = 36;
@ -568,6 +568,7 @@ DitherPatternInfo::get_bitmap (int width, int height) const
if (width < 0) {
width = 34;
}
unsigned int fw = frame_width < 0 ? 1 : frame_width;
const uint32_t * const *p = pattern ();
unsigned int stride = (width + 7) / 8;
@ -575,17 +576,14 @@ DitherPatternInfo::get_bitmap (int width, int height) const
unsigned char *data = new unsigned char[stride * height];
memset (data, 0x00, size_t (stride * height));
for (unsigned int i = 1; i < (unsigned int)(height - 1); ++i) {
for (unsigned int j = 0; j < stride; ++j) {
data [i * stride + j] = 0xff;
for (unsigned int i = 0; i < (unsigned int) height; ++i) {
uint32_t w = 0xffffffff;
if (i >= fw && i < (unsigned int) height - fw) {
w = *(p [(height - 1 - i) % m_height]);
}
}
for (unsigned int i = 0; i < (unsigned int)(height - 4); ++i) {
uint32_t w = *(p [(height - 5 - i) % m_height]);
for (unsigned int j = 0; j < (unsigned int)(width - 2); ++j) {
if (! (w & (1 << (j % m_width)))) {
data [stride * (i + 2) + (j + 1) / 8] &= ~(1 << ((j + 1) % 8));
for (unsigned int j = 0; j < (unsigned int) width; ++j) {
if (j < fw || j >= (unsigned int) width - fw || (w & (1 << (j % m_width))) != 0) {
data [stride * i + j / 8] |= (1 << (j % 8));
}
}
}
@ -965,18 +963,6 @@ DitherPattern::operator= (const DitherPattern &p)
return *this;
}
#if defined(HAVE_QT)
QBitmap
DitherPattern::get_bitmap (unsigned int i, int width, int height) const
{
if (i < count ()) {
return m_pattern [i].get_bitmap (width, height);
} else {
return m_pattern [1].get_bitmap (width, height);
}
}
#endif
const DitherPatternInfo &
DitherPattern::pattern (unsigned int i) const
{

View File

@ -135,7 +135,7 @@ public:
* @param width The desired width (-1 for default)
* @param height The desired height (-1 for default)
*/
QBitmap get_bitmap (int width = -1, int height = -1) const;
QBitmap get_bitmap (int width = -1, int height = -1, int frame_width = -1) const;
#endif
/**
@ -287,19 +287,6 @@ public:
return m_pattern != p.m_pattern;
}
#if defined(HAVE_QT)
/**
* @brief Gets a monochrome bitmap object for this pattern
*
* If the index is not valid, an empty bitmap is returned.
*
* @param i The index of the pattern to get the bitmap of
* @param width The desired width (-1 for default)
* @param height The desired height (-1 for default)
*/
QBitmap get_bitmap (unsigned int i, int width = -1, int height = -1) const;
#endif
/**
* @brief Deliver the pattern with the given index
*

View File

@ -193,7 +193,7 @@ LCPDitherPalette::LCPDitherPalette (QWidget *parent, const char *name)
void
LCPDitherPalette::create_pixmap_for (LCPActiveLabel *b, int n)
{
const lay::DitherPattern &pattern = !mp_view ? lay::DitherPattern::default_pattern () : mp_view->dither_pattern ();
lay::DitherPattern pattern = !mp_view ? lay::DitherPattern::default_pattern () : mp_view->dither_pattern ();
QColor color0 = b->palette ().color (QPalette::Normal, b->backgroundRole ());
QColor color1 = b->palette ().color (QPalette::Normal, b->foregroundRole ());
@ -207,23 +207,22 @@ LCPDitherPalette::create_pixmap_for (LCPActiveLabel *b, int n)
unsigned int dpr = 1;
#endif
pattern.scale_pattern (dpr);
QImage image (w * dpr, h * dpr, QImage::Format_RGB32);
image.fill (color0.rgb ());
#if QT_VERSION > 0x050000
image.setDevicePixelRatio (dpr);
#endif
// TODO include a scaling algorithm in get_bitmap, because it looks small in highDPI screens
QBitmap bitmap = pattern.pattern (n).get_bitmap (w * dpr, h * dpr);
QBitmap bitmap = pattern.pattern (n).get_bitmap (w * dpr, h * dpr, dpr);
QPainter painter (&image);
painter.setPen (QPen (color1));
painter.setBackgroundMode (Qt::TransparentMode);
painter.drawPixmap (0, 0, w, h, bitmap);
QPixmap pixmap = QPixmap::fromImage (image); // Qt 4.6.0 workaround
#if QT_VERSION > 0x050000
pixmap.setDevicePixelRatio (dpr);
#endif
QPixmap pixmap = QPixmap::fromImage (image);
b->setPixmap (pixmap);
}
@ -632,31 +631,22 @@ LCPStylePalette::create_pixmap_for_line_style (LCPActiveLabel *b, int n)
QColor color0 = b->palette ().color (QPalette::Normal, b->backgroundRole ());
QColor color1 = b->palette ().color (QPalette::Normal, b->foregroundRole ());
// NOTE: we intentionally don't apply devicePixelRatio here as this way, the
// image looks more like the style applied on the layout canvas.
const unsigned int h = 14;
const unsigned int w = 24;
#if QT_VERSION > 0x050000
unsigned int dpr = devicePixelRatio ();
#else
unsigned int dpr = 1;
#endif
QImage image (dpr * w, dpr * h, QImage::Format_RGB32);
QImage image (w, h, QImage::Format_RGB32);
image.fill (color0.rgb ());
#if QT_VERSION > 0x050000
image.setDevicePixelRatio (dpr);
#endif
QBitmap bitmap = styles.style (n).get_bitmap (dpr * w, dpr * h);
QBitmap bitmap = styles.style (n).get_bitmap (w, h);
QPainter painter (&image);
painter.setPen (QPen (color1));
painter.setBackgroundMode (Qt::TransparentMode);
painter.drawPixmap (0, 0, w, h, bitmap);
QPixmap pixmap = QPixmap::fromImage (image); // Qt 4.6.0 workaround
#if QT_VERSION > 0x050000
pixmap.setDevicePixelRatio (dpr);
#endif
QPixmap pixmap = QPixmap::fromImage (image);
b->setPixmap (pixmap);
}

View File

@ -1239,15 +1239,17 @@ LayoutViewConfigPage6::update ()
image.fill (color0.rgb ());
// copying code from layLayerToolbox.cc
QBitmap bitmap = m_pattern.pattern ((unsigned int) s).get_bitmap (w * dpr, h * dpr);
lay::DitherPatternInfo info = m_pattern.pattern ((unsigned int) s);
info.scale_pattern (dpr);
QBitmap bitmap = info.get_bitmap (w * dpr, h * dpr, dpr);
QPainter painter (&image);
painter.setPen (QPen (color1));
painter.setBackgroundMode (Qt::TransparentMode);
painter.drawPixmap (0, 0, w, h, bitmap);
painter.setPen (QPen (palette ().color (QPalette::Active, QPalette::Text), 1.0 / dpr));
painter.setPen (QPen (palette ().color (QPalette::Active, QPalette::Text), 1.0));
QRectF r (0, 0, w - painter.pen ().widthF (), h - painter.pen ().widthF ());
painter.drawRect (r);
painter.setFont (font ());
painter.drawText (r, Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextSingleLine, text);
@ -1425,22 +1427,16 @@ LayoutViewConfigPage6a::update ()
QColor color0 = b->palette ().color (QPalette::Normal, b->backgroundRole ());
QColor color1 = b->palette ().color (QPalette::Normal, b->foregroundRole ());
// NOTE: we intentionally don't apply devicePixelRatio here as this way, the
// image looks more like the style applied on the layout canvas.
const unsigned int h = 26;
const unsigned int w = 26;
#if QT_VERSION > 0x050000
unsigned int dpr = devicePixelRatio ();
#else
unsigned int dpr = 1;
#endif
QImage image (w * dpr, h * dpr, QImage::Format_RGB32);
QImage image (w, h, QImage::Format_RGB32);
image.fill (color0.rgb ());
#if QT_VERSION > 0x050000
image.setDevicePixelRatio (dpr);
#endif
QBitmap bitmap = m_style.style (s).get_bitmap (w * dpr, h * dpr);
QBitmap bitmap = m_style.style (s).get_bitmap (w, h);
QPainter painter (&image);
painter.setPen (QPen (color1));
painter.setBackgroundMode (Qt::TransparentMode);

View File

@ -151,9 +151,18 @@ DitherPatternSelectionButton::update_pattern ()
QPushButton::setIconSize (QSize (rt.width (), rt.height ()));
#if QT_VERSION >= 0x050000
double dpr = devicePixelRatio ();
#else
double dpr = 1.0;
#endif
if (m_dither_pattern < 0) {
QPixmap pixmap (rt.width (), rt.height ());
QPixmap pixmap (rt.width () * dpr, rt.height () * dpr);
#if QT_VERSION >= 0x050000
pixmap.setDevicePixelRatio (dpr);
#endif
pixmap.fill (QColor (0, 0, 0, 0));
QPainter pxpainter (&pixmap);
@ -161,20 +170,27 @@ DitherPatternSelectionButton::update_pattern ()
QColor text_color = palette ().color (QPalette::Active, QPalette::Text);
pxpainter.setPen (QPen (text_color));
QRect r (0, 0, pixmap.width () - 1, pixmap.height () - 1);
QRectF r (0, 0, rt.width () - pxpainter.pen ().widthF (), rt.height () - pxpainter.pen ().widthF ());
pxpainter.drawText (r, Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextSingleLine, QObject::tr ("None"));
QPushButton::setIcon (QIcon (pixmap));
} else {
lay::DitherPatternInfo dp_info;
if (mp_view) {
QPushButton::setIcon (QIcon (mp_view->dither_pattern ().get_bitmap ((unsigned int) m_dither_pattern, rt.width (), rt.height ())));
dp_info = mp_view->dither_pattern ().pattern ((unsigned int) m_dither_pattern);
} else {
lay::DitherPattern default_pattern;
QPushButton::setIcon (QIcon (default_pattern.get_bitmap ((unsigned int) m_dither_pattern, rt.width (), rt.height ())));
static lay::DitherPattern default_pattern;
dp_info= default_pattern.pattern ((unsigned int) m_dither_pattern);
}
if (dpr > 1) {
dp_info.scale_pattern (dpr);
}
QPushButton::setIcon (dp_info.get_bitmap (rt.width () * dpr, rt.height () * dpr, dpr));
}
}
@ -208,14 +224,17 @@ DitherPatternSelectionButton::update_menu ()
unsigned int n = palette.stipple_by_index (i);
if (int (n) < std::distance (patterns.begin (), patterns.end ())) {
const lay::DitherPatternInfo &info = patterns.begin () [n];
lay::DitherPatternInfo info = patterns.begin () [n];
#if QT_VERSION > 0x050000
info.scale_pattern (devicePixelRatio ());
#endif
std::string name (info.name ());
if (name.empty ()) {
name = tl::sprintf ("#%d", n);
}
menu ()->addAction (QIcon (info.get_bitmap ()), tl::to_qstring (name), this, SLOT (menu_selected ()))->setData (n);
menu ()->addAction (QIcon (info.get_bitmap (-1, -1, devicePixelRatio ())), tl::to_qstring (name), this, SLOT (menu_selected ()))->setData (n);
}
}