forked from jurplel/qView
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More fixes when fitting image with display scaling
Extend logical pixel "rounding" to handle case where menu bar is visible at top of window which influences the calculation.
- Loading branch information
Showing
7 changed files
with
148 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "logicalpixelfitter.h" | ||
#include <QtMath> | ||
|
||
LogicalPixelFitter::LogicalPixelFitter(const qreal logicalScale, const QPoint offset) | ||
: logicalScale(logicalScale), offset(offset) | ||
{ | ||
} | ||
|
||
int LogicalPixelFitter::snapWidth(const qreal value) const | ||
{ | ||
return snap(value + offset.x(), logicalScale) - offset.x(); | ||
} | ||
|
||
int LogicalPixelFitter::snapHeight(const qreal value) const | ||
{ | ||
return snap(value + offset.y(), logicalScale) - offset.y(); | ||
} | ||
|
||
QSize LogicalPixelFitter::snapSize(const QSizeF size) const | ||
{ | ||
return QSize(snapWidth(size.width()), snapHeight(size.height())); | ||
} | ||
|
||
qreal LogicalPixelFitter::unsnapWidth(const int value) const | ||
{ | ||
return unsnap(value + offset.x(), logicalScale) - offset.x(); | ||
} | ||
|
||
qreal LogicalPixelFitter::unsnapHeight(const int value) const | ||
{ | ||
return unsnap(value + offset.y(), logicalScale) - offset.y(); | ||
} | ||
|
||
QSizeF LogicalPixelFitter::unsnapSize(const QSize size) const | ||
{ | ||
return QSizeF(unsnapWidth(size.width()), unsnapHeight(size.height())); | ||
} | ||
|
||
int LogicalPixelFitter::snap(const qreal value, const qreal logicalScale) | ||
{ | ||
const int valueRoundedDown = qFloor(qAbs(value)); | ||
const int valueRoundedUp = valueRoundedDown + 1; | ||
const int physicalPixelsDrawn = qRound(qAbs(value) * logicalScale); | ||
const int physicalPixelsShownIfRoundingUp = qRound(valueRoundedUp * logicalScale); | ||
const int result = physicalPixelsDrawn >= physicalPixelsShownIfRoundingUp ? valueRoundedUp : valueRoundedDown; | ||
return result * (value >= 0 ? 1 : -1); | ||
} | ||
|
||
qreal LogicalPixelFitter::unsnap(const int value, const qreal logicalScale) | ||
{ | ||
// For a given input value, its physical pixels fall within [value-0.5,value+0.5), so | ||
// calculate the first physical pixel of the next value (rounding up if between pixels), | ||
// and the pixel prior to that is the last one within the current value. | ||
const int maxPhysicalPixelForValue = qCeil((qAbs(value) + 0.5) * logicalScale) - 1; | ||
const qreal result = maxPhysicalPixelForValue / logicalScale; | ||
return result * (value >= 0 ? 1 : -1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef LOGICALPIXELFITTER_H | ||
#define LOGICALPIXELFITTER_H | ||
|
||
#include <QPoint> | ||
#include <QSize> | ||
|
||
class LogicalPixelFitter | ||
{ | ||
public: | ||
LogicalPixelFitter(const qreal logicalScale, const QPoint offset); | ||
|
||
int snapWidth(const qreal value) const; | ||
|
||
int snapHeight(const qreal value) const; | ||
|
||
QSize snapSize(const QSizeF size) const; | ||
|
||
qreal unsnapWidth(const int value) const; | ||
|
||
qreal unsnapHeight(const int value) const; | ||
|
||
QSizeF unsnapSize(const QSize size) const; | ||
|
||
static int snap(const qreal value, const qreal logicalScale); | ||
|
||
static qreal unsnap(const int value, const qreal logicalScale); | ||
|
||
private: | ||
const qreal logicalScale; | ||
const QPoint offset; | ||
}; | ||
|
||
#endif // LOGICALPIXELFITTER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters