Skip to content

Commit

Permalink
Fix menu header appearance on hidpi displays
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent 53c6308 commit ca4c34d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
17 changes: 9 additions & 8 deletions src/gui/qgscolorswatchgrid.cpp
Expand Up @@ -27,8 +27,6 @@
#define RIGHT_MARGIN 6 //margin between right edge and last swatch
#define TOP_MARGIN 6 //margin between label and first swatch
#define BOTTOM_MARGIN 6 //margin between last swatch row and end of widget
#define LABEL_SIZE 20 //label rect height
#define LABEL_MARGIN 4 //spacing between label box and text

QgsColorSwatchGrid::QgsColorSwatchGrid( QgsColorScheme *scheme, const QString &context, QWidget *parent )
: QWidget( parent )
Expand All @@ -46,6 +44,9 @@ QgsColorSwatchGrid::QgsColorSwatchGrid( QgsColorScheme *scheme, const QString &c
setFocusPolicy( Qt::StrongFocus );
setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );

mLabelHeight = fontMetrics().height();
mLabelMargin = fontMetrics().width( QStringLiteral( "." ) );

//calculate widget width
mWidth = NUMBER_COLORS_PER_ROW * SWATCH_SIZE + ( NUMBER_COLORS_PER_ROW - 1 ) * SWATCH_SPACING + LEFT_MARGIN + RIGHT_MARGIN;

Expand Down Expand Up @@ -240,7 +241,7 @@ void QgsColorSwatchGrid::focusOutEvent( QFocusEvent *event )
int QgsColorSwatchGrid::calculateHeight() const
{
int numberRows = ceil( ( double )mColors.length() / NUMBER_COLORS_PER_ROW );
return numberRows * ( SWATCH_SIZE ) + ( numberRows - 1 ) * SWATCH_SPACING + TOP_MARGIN + LABEL_SIZE + BOTTOM_MARGIN;
return numberRows * ( SWATCH_SIZE ) + ( numberRows - 1 ) * SWATCH_SPACING + TOP_MARGIN + mLabelHeight + 0.5 * mLabelMargin + BOTTOM_MARGIN;
}

void QgsColorSwatchGrid::draw( QPainter &painter )
Expand All @@ -253,11 +254,11 @@ void QgsColorSwatchGrid::draw( QPainter &painter )
//draw header background
painter.setBrush( headerBgColor );
painter.setPen( Qt::NoPen );
painter.drawRect( QRect( 0, 0, width(), LABEL_SIZE ) );
painter.drawRect( QRect( 0, 0, width(), mLabelHeight + 0.5 * mLabelMargin ) );

//draw header text
painter.setPen( headerTextColor );
painter.drawText( QRect( LABEL_MARGIN, 0, width() - 2 * LABEL_MARGIN, LABEL_SIZE ),
painter.drawText( QRect( mLabelMargin, 0.25 * mLabelMargin, width() - 2 * mLabelMargin, mLabelHeight ),
Qt::AlignLeft | Qt::AlignVCenter, mScheme->schemeName() );

//draw color swatches
Expand All @@ -269,7 +270,7 @@ void QgsColorSwatchGrid::draw( QPainter &painter )
int column = index % NUMBER_COLORS_PER_ROW;

QRect swatchRect = QRect( column * ( SWATCH_SIZE + SWATCH_SPACING ) + LEFT_MARGIN,
row * ( SWATCH_SIZE + SWATCH_SPACING ) + TOP_MARGIN + LABEL_SIZE,
row * ( SWATCH_SIZE + SWATCH_SPACING ) + TOP_MARGIN + mLabelHeight + 0.5 * mLabelMargin,
SWATCH_SIZE, SWATCH_SIZE );

if ( mCurrentHoverBox == index )
Expand Down Expand Up @@ -336,8 +337,8 @@ int QgsColorSwatchGrid::swatchForPosition( QPoint position ) const
int box = -1;
int column = ( position.x() - LEFT_MARGIN ) / ( SWATCH_SIZE + SWATCH_SPACING );
int xRem = ( position.x() - LEFT_MARGIN ) % ( SWATCH_SIZE + SWATCH_SPACING );
int row = ( position.y() - TOP_MARGIN - LABEL_SIZE ) / ( SWATCH_SIZE + SWATCH_SPACING );
int yRem = ( position.y() - TOP_MARGIN - LABEL_SIZE ) % ( SWATCH_SIZE + SWATCH_SPACING );
int row = ( position.y() - TOP_MARGIN - mLabelHeight ) / ( SWATCH_SIZE + SWATCH_SPACING );
int yRem = ( position.y() - TOP_MARGIN - mLabelHeight ) % ( SWATCH_SIZE + SWATCH_SPACING );

if ( xRem <= SWATCH_SIZE + 1 && yRem <= SWATCH_SIZE + 1 && column < NUMBER_COLORS_PER_ROW )
{
Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgscolorswatchgrid.h
Expand Up @@ -117,6 +117,10 @@ class GUI_EXPORT QgsColorSwatchGrid : public QWidget
int mCurrentFocusBox;

int mWidth;
//! Label rect height
int mLabelHeight = 0;
//! Spacing between label box and text
int mLabelMargin = 0;

bool mPressedOnWidget;

Expand Down
15 changes: 7 additions & 8 deletions src/gui/qgsmenuheader.cpp
Expand Up @@ -19,27 +19,26 @@
#include <QPainter>
#include <QApplication>

#define LABEL_SIZE 20 //label rect height
#define LABEL_MARGIN 4 //spacing between label box and text

QgsMenuHeader::QgsMenuHeader( const QString &text, QWidget *parent )
: QWidget( parent )
, mText( text )
{
int textMinWidth = fontMetrics().width( mText );
mMinWidth = 2 * LABEL_MARGIN + textMinWidth;
mTextHeight = fontMetrics().height();
mLabelMargin = fontMetrics().width( QStringLiteral( "." ) );
mMinWidth = 2 * mLabelMargin + textMinWidth;
setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
updateGeometry();
}

QSize QgsMenuHeader::minimumSizeHint() const
{
return QSize( mMinWidth, LABEL_SIZE );
return QSize( mMinWidth, mTextHeight + 0.5 * mLabelMargin );
}

QSize QgsMenuHeader::sizeHint() const
{
return QSize( mMinWidth, LABEL_SIZE );
return QSize( mMinWidth, mTextHeight + 0.5 * mLabelMargin );
}

void QgsMenuHeader::paintEvent( QPaintEvent * )
Expand All @@ -52,11 +51,11 @@ void QgsMenuHeader::paintEvent( QPaintEvent * )
//draw header background
painter.setBrush( headerBgColor );
painter.setPen( Qt::NoPen );
painter.drawRect( QRect( 0, 0, width(), LABEL_SIZE ) );
painter.drawRect( QRect( 0, 0, width(), mTextHeight + 0.5 * mLabelMargin ) );

//draw header text
painter.setPen( headerTextColor );
painter.drawText( QRect( LABEL_MARGIN, 0, width() - 2 * LABEL_MARGIN, LABEL_SIZE ),
painter.drawText( QRect( mLabelMargin, 0.25 * mLabelMargin, width() - 2 * mLabelMargin, mTextHeight ),
Qt::AlignLeft | Qt::AlignVCenter, mText );
painter.end();
}
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsmenuheader.h
Expand Up @@ -49,6 +49,8 @@ class GUI_EXPORT QgsMenuHeader : public QWidget
private:
int mMinWidth = 0;
QString mText;
int mTextHeight = 0;
int mLabelMargin = 0;

};

Expand Down

0 comments on commit ca4c34d

Please sign in to comment.