Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Modernise CharacterWidget API, move to sipify
  • Loading branch information
nyalldawson committed Apr 3, 2017
1 parent 9b3539a commit 5813eb1
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 67 deletions.
10 changes: 10 additions & 0 deletions doc/api_break.dox
Expand Up @@ -374,6 +374,16 @@ from a project, the new QgsProject.instance().layoutManager() class should be us
Additionally, the new interface methods work with QgsComposerInterface objects instead
of QgsComposerView objects.

CharacterWidget {#qgis_api_break_3_0_CharacterWidget}
-------------------

- getColumns() was renamed to columns()
- getSquareSize() was renamed to squareSize()
- updateFont() was renamed to setFont()
- updateSize() was renamed to setFontSize()
- updateStyle() was renamed to setFontStyle()
- updateColumns() was renamed to setColumns()


QgsAbstractGeometry {#qgis_api_break_3_0_QgsAbstractGeometry}
-------------------
Expand Down
1 change: 0 additions & 1 deletion python/auto_sip.blacklist
Expand Up @@ -527,7 +527,6 @@ gui/raster/qgssinglebandpseudocolorrendererwidget.sip
gui/raster/qgsrendererrasterpropertieswidget.sip
gui/raster/qgsrastertransparencywidget.sip
gui/raster/qgshillshaderendererwidget.sip
gui/symbology-ng/characterwidget.sip
gui/symbology-ng/qgs25drendererwidget.sip
gui/symbology-ng/qgsarrowsymbollayerwidget.sip
gui/symbology-ng/qgsbrushstylecombobox.sip
Expand Down
98 changes: 91 additions & 7 deletions python/gui/symbology-ng/characterwidget.sip
@@ -1,29 +1,113 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/symbology-ng/characterwidget.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class CharacterWidget : QWidget
{
%Docstring

A widget for displaying characters available in a preset font, and allowing
users to select an individual character.
%End

%TypeHeaderCode
#include <characterwidget.h>
#include "characterwidget.h"
%End

public:

CharacterWidget( QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for CharacterWidget.
%End

QSize sizeHint() const;

int getColumns() const;
int getSquareSize() const;
int columns() const;
%Docstring
Returns the number of columns of characters shown in the widget.
%End

int squareSize() const;
%Docstring
Returns the size (in pixels) of the square used to render each character preview.
%End

QChar character() const;
%Docstring
Returns the currently selected character in the widget.
\see setCharacter()
.. versionadded:: 3.0
%End

QFont font() const;
%Docstring
Returns the font shown in the widget
\see setFont()
.. versionadded:: 3.0
%End

public slots:
void updateFont( const QFont &font );
void updateSize( double fontSize );
void updateStyle( const QString &fontStyle );

void setFont( const QFont &font );
%Docstring
Sets the \a font to show in the widget.
\see font()
.. versionadded:: 3.0
%End

void setFontSize( double fontSize );
%Docstring
Sets the font size (in points) to render in the widget.
.. versionadded:: 3.0
%End

void setFontStyle( const QString &fontStyle );
%Docstring
Sets the font style to show in the widget.
.. versionadded:: 3.0
%End

void updateFontMerging( bool enable );
void updateColumns( int cols );

void setColumns( int columns );
%Docstring
Sets the number of columns of characters to show in the widget.
.. versionadded:: 3.0
%End

void setCharacter( QChar character );
%Docstring
Sets the currently selected \a character in the widget.
\see character()
\see characterSelected()
%End

signals:

void characterSelected( QChar character );
%Docstring
Emitted when a character is selected in the widget.
%End

protected:
void mouseMoveEvent( QMouseEvent *event );
void mousePressEvent( QMouseEvent *event );
void paintEvent( QPaintEvent *event );

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/symbology-ng/characterwidget.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
6 changes: 3 additions & 3 deletions src/gui/qgscharacterselectdialog.cpp
Expand Up @@ -31,9 +31,9 @@ QgsCharacterSelectorDialog::QgsCharacterSelectorDialog( QWidget *parent, Qt::Win
const QChar &QgsCharacterSelectorDialog::selectCharacter( bool *gotChar, const QFont &font, const QString &style )
{
mCharSelectLabelFont->setText( QStringLiteral( "%1 %2" ).arg( font.family(), style ) );
mCharWidget->updateFont( font );
mCharWidget->updateStyle( style );
mCharWidget->updateSize( 22.0 );
mCharWidget->setFont( font );
mCharWidget->setFontStyle( style );
mCharWidget->setFontSize( 22.0 );
mCharSelectScrollArea->viewport()->update();

QApplication::setOverrideCursor( Qt::ArrowCursor );
Expand Down
75 changes: 36 additions & 39 deletions src/gui/symbology-ng/characterwidget.cpp
Expand Up @@ -56,76 +56,73 @@
CharacterWidget::CharacterWidget( QWidget *parent )
: QWidget( parent )
{
squareSize = 24;
columns = 16;
lastKey = -1;
setMouseTracking( true );
}

void CharacterWidget::updateFont( const QFont &font )
void CharacterWidget::setFont( const QFont &font )
{
displayFont.setFamily( font.family() );
squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
mDisplayFont.setFamily( font.family() );
mSquareSize = qMax( 24, QFontMetrics( mDisplayFont ).xHeight() * 3 );
adjustSize();
update();
}

void CharacterWidget::updateSize( double fontSize )
void CharacterWidget::setFontSize( double fontSize )
{
displayFont.setPointSizeF( fontSize );
squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
mDisplayFont.setPointSizeF( fontSize );
mSquareSize = qMax( 24, QFontMetrics( mDisplayFont ).xHeight() * 3 );
adjustSize();
update();
}

void CharacterWidget::updateStyle( const QString &fontStyle )
void CharacterWidget::setFontStyle( const QString &fontStyle )
{
QFontDatabase fontDatabase;
const QFont::StyleStrategy oldStrategy = displayFont.styleStrategy();
displayFont = fontDatabase.font( displayFont.family(), fontStyle, displayFont.pointSize() );
displayFont.setStyleStrategy( oldStrategy );
squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
const QFont::StyleStrategy oldStrategy = mDisplayFont.styleStrategy();
mDisplayFont = fontDatabase.font( mDisplayFont.family(), fontStyle, mDisplayFont.pointSize() );
mDisplayFont.setStyleStrategy( oldStrategy );
mSquareSize = qMax( 24, QFontMetrics( mDisplayFont ).xHeight() * 3 );
adjustSize();
update();
}

void CharacterWidget::updateFontMerging( bool enable )
{
if ( enable )
displayFont.setStyleStrategy( QFont::PreferDefault );
mDisplayFont.setStyleStrategy( QFont::PreferDefault );
else
displayFont.setStyleStrategy( QFont::NoFontMerging );
mDisplayFont.setStyleStrategy( QFont::NoFontMerging );
adjustSize();
update();
}

void CharacterWidget::updateColumns( int cols )
void CharacterWidget::setColumns( int columns )
{
if ( columns == cols || cols < 1 )
if ( mColumns == columns || columns < 1 )
return;
columns = cols;
mColumns = columns;
adjustSize();
update();
}

void CharacterWidget::setCharacter( QChar character )
{
lastKey = character.unicode();
mLastKey = character.unicode();
update();
}

QSize CharacterWidget::sizeHint() const
{
return QSize( columns * squareSize, ( 65536 / columns ) * squareSize );
return QSize( mColumns * mSquareSize, ( 65536 / mColumns ) * mSquareSize );
}

void CharacterWidget::mouseMoveEvent( QMouseEvent *event )
{
QPoint widgetPosition = mapFromGlobal( event->globalPos() );
uint key = ( widgetPosition.y() / squareSize ) * columns + widgetPosition.x() / squareSize;
uint key = ( widgetPosition.y() / mSquareSize ) * mColumns + widgetPosition.x() / mSquareSize;

QString text = tr( "<p>Character: <span style=\"font-size: 24pt; font-family: %1\">%2</span><p>Value: 0x%3" )
.arg( displayFont.family() )
.arg( mDisplayFont.family() )
.arg( QChar( key ) )
.arg( key, 16 );
QToolTip::showText( event->globalPos(), text, this );
Expand All @@ -135,9 +132,9 @@ void CharacterWidget::mousePressEvent( QMouseEvent *event )
{
if ( event->button() == Qt::LeftButton )
{
lastKey = ( event->y() / squareSize ) * columns + event->x() / squareSize;
if ( QChar( lastKey ).category() != QChar::Other_NotAssigned )
emit characterSelected( QChar( lastKey ) );
mLastKey = ( event->y() / mSquareSize ) * mColumns + event->x() / mSquareSize;
if ( QChar( mLastKey ).category() != QChar::Other_NotAssigned )
emit characterSelected( QChar( mLastKey ) );
update();
}
else
Expand All @@ -148,39 +145,39 @@ void CharacterWidget::paintEvent( QPaintEvent *event )
{
QPainter painter( this );
painter.fillRect( event->rect(), QBrush( Qt::white ) );
painter.setFont( displayFont );
painter.setFont( mDisplayFont );

QRect redrawRect = event->rect();
int beginRow = redrawRect.top() / squareSize;
int endRow = redrawRect.bottom() / squareSize;
int beginColumn = redrawRect.left() / squareSize;
int endColumn = redrawRect.right() / squareSize;
int beginRow = redrawRect.top() / mSquareSize;
int endRow = redrawRect.bottom() / mSquareSize;
int beginColumn = redrawRect.left() / mSquareSize;
int endColumn = redrawRect.right() / mSquareSize;

painter.setPen( QPen( Qt::gray ) );
for ( int row = beginRow; row <= endRow; ++row )
{
for ( int column = beginColumn; column <= endColumn; ++column )
{
painter.drawRect( column * squareSize, row * squareSize, squareSize, squareSize );
painter.drawRect( column * mSquareSize, row * mSquareSize, mSquareSize, mSquareSize );
}
}

QFontMetrics fontMetrics( displayFont );
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 * columns + column;
painter.setClipRect( column * squareSize, row * squareSize, squareSize, squareSize );
int key = row * mColumns + column;
painter.setClipRect( column * mSquareSize, row * mSquareSize, mSquareSize, mSquareSize );

if ( key == lastKey )
painter.fillRect( column * squareSize + 1, row * squareSize + 1, squareSize, squareSize, QBrush( Qt::red ) );
if ( key == mLastKey )
painter.fillRect( column * mSquareSize + 1, row * mSquareSize + 1, mSquareSize, mSquareSize, QBrush( Qt::red ) );

painter.drawText( column * squareSize + ( squareSize / 2 ) - fontMetrics.width( QChar( key ) ) / 2,
row * squareSize + 4 + fontMetrics.ascent(),
painter.drawText( column * mSquareSize + ( mSquareSize / 2 ) - fontMetrics.width( QChar( key ) ) / 2,
row * mSquareSize + 4 + fontMetrics.ascent(),
QString( QChar( key ) ) );
}
}
Expand Down

0 comments on commit 5813eb1

Please sign in to comment.