Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions cr3qt/src/cr3widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <QDesktopServices>
#include <QLocale>

#include <math.h>

/// to hide non-qt implementation, place all crengine-related fields here
class CR3View::DocViewData
{
Expand Down Expand Up @@ -314,6 +316,13 @@ CR3View::CR3View( QWidget *parent)
, _lastBatteryChargingConn(CR_BATTERY_CHARGER_NO)
, _lastBatteryChargeLevel(0)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
_dpr = screen()->devicePixelRatio();
#elif QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
_dpr = QGuiApplication::primaryScreen()->devicePixelRatio();
#else
_dpr = 1.0;
#endif
#if WORD_SELECTOR_ENABLED==1
_wordSelector = NULL;
#endif
Expand Down Expand Up @@ -362,14 +371,22 @@ void CR3View::updateDefProps()
_data->_props->setStringDef( PROP_WINDOW_SHOW_STATUSBAR, "0" );
_data->_props->setStringDef( PROP_APP_START_ACTION, "0" );

if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) {
QScreen* screen = QGuiApplication::primaryScreen();
lString32 str;
str.appendDecimal((int)screen->logicalDotsPerInch());
_data->_props->setString( PROP_RENDER_DPI, str );
// But we don't apply PROP_RENDER_SCALE_FONT_WITH_DPI property,
// since for now we are setting the font size in pixels.
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QScreen* screen = this->screen();
#else
QScreen* screen = QGuiApplication::primaryScreen();
#endif
lString32 str;
int d = (int)screen->logicalDotsPerInch();
// special workaround for MacOS
if (72 == d)
d = 96;
str.appendDecimal(d);
_data->_props->setString( PROP_RENDER_DPI, str );
// But we don't apply PROP_RENDER_SCALE_FONT_WITH_DPI property,
// since for now we are setting the font size in pixels.
#endif

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

void CR3View::resizeEvent ( QResizeEvent * event )
{

QSize sz = event->size();
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
_dpr = screen()->devicePixelRatio();
#endif
sz *= _dpr;
#endif
_docview->Resize( sz.width(), sz.height() );
}

Expand Down Expand Up @@ -561,6 +583,9 @@ static bool getBatteryState(int& state, int& chargingConn, int& level)
void CR3View::paintEvent ( QPaintEvent * event )
{
QPainter painter(this);
qreal dpr = painter.device()->devicePixelRatioF();
if (fabs(dpr - _dpr) >= 0.01)
CRLog::error("Device pixel ratio is changed! (prev=%.1f, now=%.1f)", _dpr, dpr);
QRect rc = rect();
int newBatteryState;
int newChargingConn;
Expand All @@ -585,6 +610,7 @@ void CR3View::paintEvent ( QPaintEvent * event )
int bpp = buf->GetBitsPerPixel();
if (bpp == 4 || bpp == 3) {
QImage img(dx, dy, QImage::Format_RGB16 );
img.setDevicePixelRatio(_dpr);
for ( int i=0; i<dy; i++ ) {
unsigned char * dst = img.scanLine( i );
unsigned char * src = buf->GetScanLine(i);
Expand All @@ -602,6 +628,7 @@ void CR3View::paintEvent ( QPaintEvent * event )
painter.drawImage( rc, img );
} else if (bpp == 2) {
QImage img(dx, dy, QImage::Format_RGB16 );
img.setDevicePixelRatio(_dpr);
for ( int i=0; i<dy; i++ ) {
unsigned char * dst = img.scanLine( i );
unsigned char * src = buf->GetScanLine(i);
Expand All @@ -628,6 +655,7 @@ void CR3View::paintEvent ( QPaintEvent * event )

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

} else if (bpp == 16) {
QImage img(dx, dy, QImage::Format_RGB16 );
img.setDevicePixelRatio(_dpr);
for ( int i=0; i<dy; i++ ) {
unsigned char * dst = img.scanLine( i );
unsigned char * src = buf->GetScanLine(i);
Expand All @@ -667,6 +696,7 @@ void CR3View::paintEvent ( QPaintEvent * event )
painter.drawImage( rc, img );
} else if (bpp == 32) {
QImage img(dx, dy, QImage::Format_RGB32 );
img.setDevicePixelRatio(_dpr);
for ( int i=0; i<dy; i++ ) {
unsigned char * dst = img.scanLine( i );
unsigned char * src = buf->GetScanLine(i);
Expand All @@ -680,6 +710,7 @@ void CR3View::paintEvent ( QPaintEvent * event )
}
painter.drawImage( rc, img );
}
_dpr = dpr;
if ( _editMode ) {
// draw caret
lvRect cursorRc;
Expand Down Expand Up @@ -1015,7 +1046,7 @@ void CR3View::mouseMoveEvent ( QMouseEvent * event )
//bool left = (event->buttons() & Qt::LeftButton);
//bool right = (event->buttons() & Qt::RightButton);
//bool mid = (event->buttons() & Qt::MidButton);
lvPoint pt ( event->x(), event->y() );
lvPoint pt ( event->x()*_dpr, event->y()*_dpr );
ldomXPointer p = _docview->getNodeByPoint( pt );
lString32 path;
lString32 href;
Expand Down Expand Up @@ -1157,7 +1188,7 @@ void CR3View::mousePressEvent ( QMouseEvent * event )
bool left = event->button() == Qt::LeftButton;
//bool right = event->button() == Qt::RightButton;
bool mid = event->button() == Qt::MidButton;
lvPoint pt (event->x(), event->y());
lvPoint pt (event->x()*_dpr, event->y()*_dpr);
ldomXPointer p = _docview->getNodeByPoint( pt );
// test imageByPoint
LVImageSourceRef img = _docview->getImageByPoint(pt);
Expand Down Expand Up @@ -1201,7 +1232,7 @@ void CR3View::mouseReleaseEvent ( QMouseEvent * event )
bool left = event->button() == Qt::LeftButton;
//bool right = event->button() == Qt::RightButton;
//bool mid = event->button() == Qt::MidButton;
lvPoint pt (event->x(), event->y());
lvPoint pt (event->x()*_dpr, event->y()*_dpr);
ldomXPointer p = _docview->getNodeByPoint( pt );
lString32 path;
lString32 href;
Expand Down
1 change: 1 addition & 0 deletions cr3qt/src/cr3widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class CR3View : public QWidget, public LVDocViewCallback
DocViewData * _data; // to hide non-qt implementation
LVDocView * _docview;
QScrollBar * _scroll;
qreal _dpr; // screen display pixel ratio (for HiDPI screens)
PropsChangeCallback * _propsCallback;
QStringList _hyphDicts;
QCursor _normalCursor;
Expand Down
7 changes: 0 additions & 7 deletions cr3qt/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,6 @@ int main(int argc, char *argv[])
// return 3;
//}
{
if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) {
// Not allowed to scale widgets for HiDPI.
// crengine renders text to image using anti-aliasing,
// so scaling is prohibited after this rendering!
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, false);
QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling, true);
}
QApplication a(argc, argv);
#if MAC == 1
QString exeDir = QDir::toNativeSeparators(qApp->applicationDirPath() + "/Contents/Resources/"); //QDir::separator();
Expand Down
1 change: 1 addition & 0 deletions cr3qt/src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ void SettingsDlg::setBackground( QWidget * wnd, QColor cl )
{
QPalette pal( wnd->palette() );
pal.setColor( QPalette::Window, cl );
pal.setColor( QPalette::Base, cl );
wnd->setPalette( pal );
}

Expand Down