Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[ui] Improvements to the font marker character widget
- Declare a minimum height to avoid narrow selection area
- Use application color palette instead of hardcoded values
- Decrease number of columns to better fit in the style dock
  • Loading branch information
nirvn committed Apr 29, 2019
1 parent c0b2ba1 commit 405efd8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/gui/symbology/characterwidget.cpp
Expand Up @@ -43,6 +43,9 @@
**
****************************************************************************/

#include "characterwidget.h"
#include "qgsapplication.h"

#include <QFontDatabase>
#include <QMouseEvent>
#include <QPaintEvent>
Expand All @@ -51,8 +54,6 @@
#include <QPoint>
#include <QToolTip>

#include "characterwidget.h"

CharacterWidget::CharacterWidget( QWidget *parent )
: QWidget( parent )
{
Expand All @@ -62,15 +63,15 @@ CharacterWidget::CharacterWidget( QWidget *parent )
void CharacterWidget::setFont( const QFont &font )
{
mDisplayFont.setFamily( font.family() );
mSquareSize = std::max( 24, QFontMetrics( mDisplayFont ).xHeight() * 3 );
mSquareSize = std::max( 34, QFontMetrics( mDisplayFont ).xHeight() * 3 );
adjustSize();
update();
}

void CharacterWidget::setFontSize( double fontSize )
{
mDisplayFont.setPointSizeF( fontSize );
mSquareSize = std::max( 24, QFontMetrics( mDisplayFont ).xHeight() * 3 );
mSquareSize = std::max( 34, QFontMetrics( mDisplayFont ).xHeight() * 3 );
adjustSize();
update();
}
Expand All @@ -81,7 +82,7 @@ void CharacterWidget::setFontStyle( const QString &fontStyle )
const QFont::StyleStrategy oldStrategy = mDisplayFont.styleStrategy();
mDisplayFont = fontDatabase.font( mDisplayFont.family(), fontStyle, mDisplayFont.pointSize() );
mDisplayFont.setStyleStrategy( oldStrategy );
mSquareSize = std::max( 24, QFontMetrics( mDisplayFont ).xHeight() * 3 );
mSquareSize = std::max( 34, QFontMetrics( mDisplayFont ).xHeight() * 3 );
adjustSize();
update();
}
Expand Down Expand Up @@ -144,7 +145,8 @@ void CharacterWidget::mousePressEvent( QMouseEvent *event )
void CharacterWidget::paintEvent( QPaintEvent *event )
{
QPainter painter( this );
painter.fillRect( event->rect(), QBrush( Qt::white ) );
QPalette palette = qApp->palette();
painter.fillRect( event->rect(), QBrush( palette.color( QPalette::Base ) ) );
painter.setFont( mDisplayFont );

QRect redrawRect = event->rect();
Expand All @@ -153,7 +155,7 @@ void CharacterWidget::paintEvent( QPaintEvent *event )
int beginColumn = redrawRect.left() / mSquareSize;
int endColumn = redrawRect.right() / mSquareSize;

painter.setPen( QPen( Qt::gray ) );
painter.setPen( QPen( palette.color( QPalette::Mid ) ) );
for ( int row = beginRow; row <= endRow; ++row )
{
for ( int column = beginColumn; column <= endColumn; ++column )
Expand All @@ -163,18 +165,16 @@ void CharacterWidget::paintEvent( QPaintEvent *event )
}

QFontMetrics fontMetrics( mDisplayFont );
painter.setPen( QPen( Qt::black ) );
for ( int row = beginRow; row <= endRow; ++row )
{

for ( int column = beginColumn; column <= endColumn; ++column )
{

int key = row * mColumns + column;
painter.setClipRect( column * mSquareSize, row * mSquareSize, mSquareSize, mSquareSize );
painter.setPen( QPen( palette.color( key == mLastKey ? QPalette::HighlightedText : QPalette::WindowText ) ) );

if ( key == mLastKey )
painter.fillRect( column * mSquareSize + 1, row * mSquareSize + 1, mSquareSize, mSquareSize, QBrush( Qt::red ) );
painter.fillRect( column * mSquareSize + 1, row * mSquareSize + 1, mSquareSize, mSquareSize, QBrush( palette.color( QPalette::Highlight ) ) );

painter.drawText( column * mSquareSize + ( mSquareSize / 2 ) - fontMetrics.width( QChar( key ) ) / 2,
row * mSquareSize + 4 + fontMetrics.ascent(),
Expand Down
2 changes: 1 addition & 1 deletion src/gui/symbology/characterwidget.h
Expand Up @@ -154,7 +154,7 @@ class GUI_EXPORT CharacterWidget : public QWidget

private:
QFont mDisplayFont;
int mColumns = 16;
int mColumns = 13;
int mLastKey = -1;
int mSquareSize = 24;
};
Expand Down
1 change: 1 addition & 0 deletions src/gui/symbology/qgssymbollayerwidget.cpp
Expand Up @@ -3207,6 +3207,7 @@ QgsFontMarkerSymbolLayerWidget::QgsFontMarkerSymbolLayerWidget( QgsVectorLayer *
<< QgsUnitTypes::RenderPoints << QgsUnitTypes::RenderInches );
mOffsetUnitWidget->setUnits( QgsUnitTypes::RenderUnitList() << QgsUnitTypes::RenderMillimeters << QgsUnitTypes::RenderMetersInMapUnits << QgsUnitTypes::RenderMapUnits << QgsUnitTypes::RenderPixels
<< QgsUnitTypes::RenderPoints << QgsUnitTypes::RenderInches );

widgetChar = new CharacterWidget;
scrollArea->setWidget( widgetChar );

Expand Down
6 changes: 6 additions & 0 deletions src/ui/symbollayer/widget_fontmarker.ui
Expand Up @@ -321,6 +321,12 @@
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" rowspan="2">
<widget class="QgsScrollArea" name="scrollArea">
<property name="minimumSize">
<size>
<width>0</width>
<height>158</height>
</size>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
Expand Down

0 comments on commit 405efd8

Please sign in to comment.