Skip to content

Commit

Permalink
Nicer behaviour of QgsCharacterSelectorDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 2, 2019
1 parent 25f20f4 commit 1d258d5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
8 changes: 7 additions & 1 deletion python/gui/auto_generated/qgscharacterselectordialog.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ A dialog for selecting a single character from a single font
QgsCharacterSelectorDialog( QWidget *parent /TransferThis/ = 0, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );

public slots:
const QChar &selectCharacter( bool *gotChar, const QFont &font, const QString &style );

QChar selectCharacter( bool *gotChar, const QFont &font, const QString &style, QChar initialSelection = QChar() );
%Docstring
Opens the dialog modally and returns when the user has selected a character.

If ``initialSelection`` is specified, then that character will be initially selected in the dialog.
%End

protected:
};
Expand Down
4 changes: 3 additions & 1 deletion src/gui/qgscharacterselectordialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ QgsCharacterSelectorDialog::QgsCharacterSelectorDialog( QWidget *parent, Qt::Win
connect( mCharWidget, &CharacterWidget::characterSelected, this, &QgsCharacterSelectorDialog::setCharacter );
}

const QChar &QgsCharacterSelectorDialog::selectCharacter( bool *gotChar, const QFont &font, const QString &style )
QChar QgsCharacterSelectorDialog::selectCharacter( bool *gotChar, const QFont &font, const QString &style, QChar initialSelection )
{
mCharSelectLabelFont->setText( QStringLiteral( "%1 %2" ).arg( font.family(), style ) );
mCharWidget->setFont( font );
mCharWidget->setFontStyle( style );
mCharWidget->setFontSize( 22.0 );
mCharSelectScrollArea->viewport()->update();

mCharWidget->setCharacter( initialSelection );

QApplication::setOverrideCursor( Qt::ArrowCursor );
int res = exec();
QApplication::restoreOverrideCursor();
Expand Down
8 changes: 7 additions & 1 deletion src/gui/qgscharacterselectordialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ class GUI_EXPORT QgsCharacterSelectorDialog : public QDialog, private Ui::QgsCha
QgsCharacterSelectorDialog( QWidget *parent SIP_TRANSFERTHIS = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );

public slots:
const QChar &selectCharacter( bool *gotChar, const QFont &font, const QString &style );

/**
* Opens the dialog modally and returns when the user has selected a character.
*
* If \a initialSelection is specified, then that character will be initially selected in the dialog.
*/
QChar selectCharacter( bool *gotChar, const QFont &font, const QString &style, QChar initialSelection = QChar() );

private slots:
void setCharacter( QChar chr );
Expand Down
7 changes: 5 additions & 2 deletions src/gui/qgstextformatwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,9 @@ void QgsTextFormatWidget::mPreviewBackgroundBtn_colorChanged( const QColor &colo
void QgsTextFormatWidget::mDirectSymbLeftToolBtn_clicked()
{
bool gotChar = false;
QChar dirSymb = mCharDlg->selectCharacter( &gotChar, mRefFont, mFontDB.styleString( mRefFont ) );

const QChar initial = !mDirectSymbLeftLineEdit->text().isEmpty() ? mDirectSymbLeftLineEdit->text().at( 0 ) : QChar();
QChar dirSymb = mCharDlg->selectCharacter( &gotChar, mRefFont, mFontDB.styleString( mRefFont ), initial );

if ( !gotChar )
return;
Expand All @@ -1423,7 +1425,8 @@ void QgsTextFormatWidget::mDirectSymbLeftToolBtn_clicked()
void QgsTextFormatWidget::mDirectSymbRightToolBtn_clicked()
{
bool gotChar = false;
QChar dirSymb = mCharDlg->selectCharacter( &gotChar, mRefFont, mFontDB.styleString( mRefFont ) );
const QChar initial = !mDirectSymbRightLineEdit->text().isEmpty() ? mDirectSymbRightLineEdit->text().at( 0 ) : QChar();
QChar dirSymb = mCharDlg->selectCharacter( &gotChar, mRefFont, mFontDB.styleString( mRefFont ), initial );

if ( !gotChar )
return;
Expand Down
4 changes: 2 additions & 2 deletions src/gui/symbology/characterwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void CharacterWidget::setColumns( int columns )
void CharacterWidget::setCharacter( QChar character )
{
const bool changed = character != mLastKey;
mLastKey = character.unicode();
mLastKey = character.isNull() ? -1 : character.unicode();
QWidget *widget = parentWidget();
if ( widget )
{
Expand All @@ -123,7 +123,7 @@ void CharacterWidget::setCharacter( QChar character )
}
}
if ( changed )
emit characterSelected( mLastKey );
emit characterSelected( mLastKey >= 0 ? QChar( mLastKey ) : QChar() );

update();
}
Expand Down

0 comments on commit 1d258d5

Please sign in to comment.