Skip to content

Commit 1d258d5

Browse files
committed
Nicer behaviour of QgsCharacterSelectorDialog
1 parent 25f20f4 commit 1d258d5

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

python/gui/auto_generated/qgscharacterselectordialog.sip.in

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ A dialog for selecting a single character from a single font
2323
QgsCharacterSelectorDialog( QWidget *parent /TransferThis/ = 0, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
2424

2525
public slots:
26-
const QChar &selectCharacter( bool *gotChar, const QFont &font, const QString &style );
26+
27+
QChar selectCharacter( bool *gotChar, const QFont &font, const QString &style, QChar initialSelection = QChar() );
28+
%Docstring
29+
Opens the dialog modally and returns when the user has selected a character.
30+
31+
If ``initialSelection`` is specified, then that character will be initially selected in the dialog.
32+
%End
2733

2834
protected:
2935
};

src/gui/qgscharacterselectordialog.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ QgsCharacterSelectorDialog::QgsCharacterSelectorDialog( QWidget *parent, Qt::Win
2929
connect( mCharWidget, &CharacterWidget::characterSelected, this, &QgsCharacterSelectorDialog::setCharacter );
3030
}
3131

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

40+
mCharWidget->setCharacter( initialSelection );
41+
4042
QApplication::setOverrideCursor( Qt::ArrowCursor );
4143
int res = exec();
4244
QApplication::restoreOverrideCursor();

src/gui/qgscharacterselectordialog.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ class GUI_EXPORT QgsCharacterSelectorDialog : public QDialog, private Ui::QgsCha
3939
QgsCharacterSelectorDialog( QWidget *parent SIP_TRANSFERTHIS = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
4040

4141
public slots:
42-
const QChar &selectCharacter( bool *gotChar, const QFont &font, const QString &style );
42+
43+
/**
44+
* Opens the dialog modally and returns when the user has selected a character.
45+
*
46+
* If \a initialSelection is specified, then that character will be initially selected in the dialog.
47+
*/
48+
QChar selectCharacter( bool *gotChar, const QFont &font, const QString &style, QChar initialSelection = QChar() );
4349

4450
private slots:
4551
void setCharacter( QChar chr );

src/gui/qgstextformatwidget.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,9 @@ void QgsTextFormatWidget::mPreviewBackgroundBtn_colorChanged( const QColor &colo
14111411
void QgsTextFormatWidget::mDirectSymbLeftToolBtn_clicked()
14121412
{
14131413
bool gotChar = false;
1414-
QChar dirSymb = mCharDlg->selectCharacter( &gotChar, mRefFont, mFontDB.styleString( mRefFont ) );
1414+
1415+
const QChar initial = !mDirectSymbLeftLineEdit->text().isEmpty() ? mDirectSymbLeftLineEdit->text().at( 0 ) : QChar();
1416+
QChar dirSymb = mCharDlg->selectCharacter( &gotChar, mRefFont, mFontDB.styleString( mRefFont ), initial );
14151417

14161418
if ( !gotChar )
14171419
return;
@@ -1423,7 +1425,8 @@ void QgsTextFormatWidget::mDirectSymbLeftToolBtn_clicked()
14231425
void QgsTextFormatWidget::mDirectSymbRightToolBtn_clicked()
14241426
{
14251427
bool gotChar = false;
1426-
QChar dirSymb = mCharDlg->selectCharacter( &gotChar, mRefFont, mFontDB.styleString( mRefFont ) );
1428+
const QChar initial = !mDirectSymbRightLineEdit->text().isEmpty() ? mDirectSymbRightLineEdit->text().at( 0 ) : QChar();
1429+
QChar dirSymb = mCharDlg->selectCharacter( &gotChar, mRefFont, mFontDB.styleString( mRefFont ), initial );
14271430

14281431
if ( !gotChar )
14291432
return;

src/gui/symbology/characterwidget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void CharacterWidget::setColumns( int columns )
112112
void CharacterWidget::setCharacter( QChar character )
113113
{
114114
const bool changed = character != mLastKey;
115-
mLastKey = character.unicode();
115+
mLastKey = character.isNull() ? -1 : character.unicode();
116116
QWidget *widget = parentWidget();
117117
if ( widget )
118118
{
@@ -123,7 +123,7 @@ void CharacterWidget::setCharacter( QChar character )
123123
}
124124
}
125125
if ( changed )
126-
emit characterSelected( mLastKey );
126+
emit characterSelected( mLastKey >= 0 ? QChar( mLastKey ) : QChar() );
127127

128128
update();
129129
}

0 commit comments

Comments
 (0)