Skip to content

Commit c108b68

Browse files
authored
Merge pull request #323 from efermi/hidpi
Improve HiDPI support for Qt5
2 parents 730879b + 22f5a48 commit c108b68

4 files changed

Lines changed: 45 additions & 19 deletions

File tree

cr3qt/src/cr3widget.cpp

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include <QDesktopServices>
3232
#include <QLocale>
3333

34+
#include <math.h>
35+
3436
/// to hide non-qt implementation, place all crengine-related fields here
3537
class CR3View::DocViewData
3638
{
@@ -314,6 +316,13 @@ CR3View::CR3View( QWidget *parent)
314316
, _lastBatteryChargingConn(CR_BATTERY_CHARGER_NO)
315317
, _lastBatteryChargeLevel(0)
316318
{
319+
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
320+
_dpr = screen()->devicePixelRatio();
321+
#elif QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
322+
_dpr = QGuiApplication::primaryScreen()->devicePixelRatio();
323+
#else
324+
_dpr = 1.0;
325+
#endif
317326
#if WORD_SELECTOR_ENABLED==1
318327
_wordSelector = NULL;
319328
#endif
@@ -362,14 +371,22 @@ void CR3View::updateDefProps()
362371
_data->_props->setStringDef( PROP_WINDOW_SHOW_STATUSBAR, "0" );
363372
_data->_props->setStringDef( PROP_APP_START_ACTION, "0" );
364373

365-
if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) {
366-
QScreen* screen = QGuiApplication::primaryScreen();
367-
lString32 str;
368-
str.appendDecimal((int)screen->logicalDotsPerInch());
369-
_data->_props->setString( PROP_RENDER_DPI, str );
370-
// But we don't apply PROP_RENDER_SCALE_FONT_WITH_DPI property,
371-
// since for now we are setting the font size in pixels.
372-
}
374+
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
375+
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
376+
QScreen* screen = this->screen();
377+
#else
378+
QScreen* screen = QGuiApplication::primaryScreen();
379+
#endif
380+
lString32 str;
381+
int d = (int)screen->logicalDotsPerInch();
382+
// special workaround for MacOS
383+
if (72 == d)
384+
d = 96;
385+
str.appendDecimal(d);
386+
_data->_props->setString( PROP_RENDER_DPI, str );
387+
// But we don't apply PROP_RENDER_SCALE_FONT_WITH_DPI property,
388+
// since for now we are setting the font size in pixels.
389+
#endif
373390

374391
QStringList styles = QStyleFactory::keys();
375392
QStyle * s = QApplication::style();
@@ -525,8 +542,13 @@ void CR3View::wheelEvent( QWheelEvent * event )
525542

526543
void CR3View::resizeEvent ( QResizeEvent * event )
527544
{
528-
529545
QSize sz = event->size();
546+
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
547+
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
548+
_dpr = screen()->devicePixelRatio();
549+
#endif
550+
sz *= _dpr;
551+
#endif
530552
_docview->Resize( sz.width(), sz.height() );
531553
}
532554

@@ -561,6 +583,9 @@ static bool getBatteryState(int& state, int& chargingConn, int& level)
561583
void CR3View::paintEvent ( QPaintEvent * event )
562584
{
563585
QPainter painter(this);
586+
qreal dpr = painter.device()->devicePixelRatioF();
587+
if (fabs(dpr - _dpr) >= 0.01)
588+
CRLog::error("Device pixel ratio is changed! (prev=%.1f, now=%.1f)", _dpr, dpr);
564589
QRect rc = rect();
565590
int newBatteryState;
566591
int newChargingConn;
@@ -585,6 +610,7 @@ void CR3View::paintEvent ( QPaintEvent * event )
585610
int bpp = buf->GetBitsPerPixel();
586611
if (bpp == 4 || bpp == 3) {
587612
QImage img(dx, dy, QImage::Format_RGB16 );
613+
img.setDevicePixelRatio(_dpr);
588614
for ( int i=0; i<dy; i++ ) {
589615
unsigned char * dst = img.scanLine( i );
590616
unsigned char * src = buf->GetScanLine(i);
@@ -602,6 +628,7 @@ void CR3View::paintEvent ( QPaintEvent * event )
602628
painter.drawImage( rc, img );
603629
} else if (bpp == 2) {
604630
QImage img(dx, dy, QImage::Format_RGB16 );
631+
img.setDevicePixelRatio(_dpr);
605632
for ( int i=0; i<dy; i++ ) {
606633
unsigned char * dst = img.scanLine( i );
607634
unsigned char * src = buf->GetScanLine(i);
@@ -628,6 +655,7 @@ void CR3View::paintEvent ( QPaintEvent * event )
628655

629656
} else if (bpp == 1) {
630657
QImage img(dx, dy, QImage::Format_RGB16 );
658+
img.setDevicePixelRatio(_dpr);
631659
for ( int i=0; i<dy; i++ ) {
632660
unsigned char * dst = img.scanLine( i );
633661
unsigned char * src = buf->GetScanLine(i);
@@ -653,6 +681,7 @@ void CR3View::paintEvent ( QPaintEvent * event )
653681

654682
} else if (bpp == 16) {
655683
QImage img(dx, dy, QImage::Format_RGB16 );
684+
img.setDevicePixelRatio(_dpr);
656685
for ( int i=0; i<dy; i++ ) {
657686
unsigned char * dst = img.scanLine( i );
658687
unsigned char * src = buf->GetScanLine(i);
@@ -667,6 +696,7 @@ void CR3View::paintEvent ( QPaintEvent * event )
667696
painter.drawImage( rc, img );
668697
} else if (bpp == 32) {
669698
QImage img(dx, dy, QImage::Format_RGB32 );
699+
img.setDevicePixelRatio(_dpr);
670700
for ( int i=0; i<dy; i++ ) {
671701
unsigned char * dst = img.scanLine( i );
672702
unsigned char * src = buf->GetScanLine(i);
@@ -680,6 +710,7 @@ void CR3View::paintEvent ( QPaintEvent * event )
680710
}
681711
painter.drawImage( rc, img );
682712
}
713+
_dpr = dpr;
683714
if ( _editMode ) {
684715
// draw caret
685716
lvRect cursorRc;
@@ -1015,7 +1046,7 @@ void CR3View::mouseMoveEvent ( QMouseEvent * event )
10151046
//bool left = (event->buttons() & Qt::LeftButton);
10161047
//bool right = (event->buttons() & Qt::RightButton);
10171048
//bool mid = (event->buttons() & Qt::MidButton);
1018-
lvPoint pt ( event->x(), event->y() );
1049+
lvPoint pt ( event->x()*_dpr, event->y()*_dpr );
10191050
ldomXPointer p = _docview->getNodeByPoint( pt );
10201051
lString32 path;
10211052
lString32 href;
@@ -1157,7 +1188,7 @@ void CR3View::mousePressEvent ( QMouseEvent * event )
11571188
bool left = event->button() == Qt::LeftButton;
11581189
//bool right = event->button() == Qt::RightButton;
11591190
bool mid = event->button() == Qt::MidButton;
1160-
lvPoint pt (event->x(), event->y());
1191+
lvPoint pt (event->x()*_dpr, event->y()*_dpr);
11611192
ldomXPointer p = _docview->getNodeByPoint( pt );
11621193
// test imageByPoint
11631194
LVImageSourceRef img = _docview->getImageByPoint(pt);
@@ -1201,7 +1232,7 @@ void CR3View::mouseReleaseEvent ( QMouseEvent * event )
12011232
bool left = event->button() == Qt::LeftButton;
12021233
//bool right = event->button() == Qt::RightButton;
12031234
//bool mid = event->button() == Qt::MidButton;
1204-
lvPoint pt (event->x(), event->y());
1235+
lvPoint pt (event->x()*_dpr, event->y()*_dpr);
12051236
ldomXPointer p = _docview->getNodeByPoint( pt );
12061237
lString32 path;
12071238
lString32 href;

cr3qt/src/cr3widget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class CR3View : public QWidget, public LVDocViewCallback
173173
DocViewData * _data; // to hide non-qt implementation
174174
LVDocView * _docview;
175175
QScrollBar * _scroll;
176+
qreal _dpr; // screen display pixel ratio (for HiDPI screens)
176177
PropsChangeCallback * _propsCallback;
177178
QStringList _hyphDicts;
178179
QCursor _normalCursor;

cr3qt/src/main.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,6 @@ int main(int argc, char *argv[])
175175
// return 3;
176176
//}
177177
{
178-
if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) {
179-
// Not allowed to scale widgets for HiDPI.
180-
// crengine renders text to image using anti-aliasing,
181-
// so scaling is prohibited after this rendering!
182-
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, false);
183-
QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling, true);
184-
}
185178
QApplication a(argc, argv);
186179
#if MAC == 1
187180
QString exeDir = QDir::toNativeSeparators(qApp->applicationDirPath() + "/Contents/Resources/"); //QDir::separator();

cr3qt/src/settings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ void SettingsDlg::setBackground( QWidget * wnd, QColor cl )
887887
{
888888
QPalette pal( wnd->palette() );
889889
pal.setColor( QPalette::Window, cl );
890+
pal.setColor( QPalette::Base, cl );
890891
wnd->setPalette( pal );
891892
}
892893

0 commit comments

Comments
 (0)