Skip to content

Commit 8fa8167

Browse files
committed
Add a simple dialog for configuring text formatting
1 parent 4282474 commit 8fa8167

File tree

4 files changed

+116
-3
lines changed

4 files changed

+116
-3
lines changed

python/gui/qgstextformatwidget.sip

+34-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @note Added in QGIS 3.0
1515
*/
1616

17-
class QgsTextFormatWidget : public QgsPanelWidget
17+
class QgsTextFormatWidget : QWidget
1818
{
1919
%TypeHeaderCode
2020
#include <qgstextformatwidget.h>
@@ -86,3 +86,36 @@ class QgsTextFormatWidget : public QgsPanelWidget
8686
//! Updates label placement options to reflect current state of widget
8787
void updatePlacementWidgets();
8888
};
89+
90+
/** \class QgsTextFormatDialog
91+
* \ingroup gui
92+
* A simple dialog for customising text formatting settings.
93+
*
94+
* QgsTextFormatDialog provides a dialog for controlling the appearance of text rendered
95+
* using QgsTextRenderer. The dialog includes all settings contained within
96+
* a QgsTextFormat, including shadow, background and buffer.
97+
*
98+
* @note Added in QGIS 3.0
99+
*/
100+
101+
class QgsTextFormatDialog : QDialog
102+
{
103+
%TypeHeaderCode
104+
#include <qgstextformatwidget.h>
105+
%End
106+
public:
107+
108+
/** Constructor for QgsTextFormatDialog.
109+
* @param format initial format settings to show in dialog
110+
* @param mapCanvas optional associated map canvas
111+
* @param parent parent widget
112+
* @param fl window flags for dialog
113+
*/
114+
QgsTextFormatDialog( const QgsTextFormat& format, QgsMapCanvas* mapCanvas = nullptr, QWidget* parent /TransferThis/ = nullptr, Qt::WindowFlags fl = QgisGui::ModalDialogFlags );
115+
116+
virtual ~QgsTextFormatDialog();
117+
118+
/** Returns the current formatting settings defined by the widget.
119+
*/
120+
QgsTextFormat format() const;
121+
};

src/gui/qgstextformatwidget.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -1376,3 +1376,40 @@ void QgsTextFormatWidget::enableDataDefinedAlignment( bool enable )
13761376
}
13771377

13781378

1379+
//
1380+
// QgsTextFormatDialog
1381+
//
1382+
1383+
QgsTextFormatDialog::QgsTextFormatDialog( const QgsTextFormat& format, QgsMapCanvas* mapCanvas, QWidget* parent, Qt::WindowFlags fl )
1384+
: QDialog( parent, fl )
1385+
{
1386+
setWindowTitle( tr( "Text settings" ) );
1387+
1388+
mFormatWidget = new QgsTextFormatWidget( format, mapCanvas, this );
1389+
mFormatWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
1390+
1391+
QVBoxLayout *layout = new QVBoxLayout( this );
1392+
layout->addWidget( mFormatWidget );
1393+
1394+
QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this );
1395+
layout->addWidget( buttonBox );
1396+
1397+
setLayout( layout );
1398+
1399+
QSettings settings;
1400+
restoreGeometry( settings.value( "/Windows/TextFormatDialog/geometry" ).toByteArray() );
1401+
1402+
connect( buttonBox->button( QDialogButtonBox::Ok ), SIGNAL( clicked() ), this, SLOT( accept() ) );
1403+
connect( buttonBox->button( QDialogButtonBox::Cancel ), SIGNAL( clicked() ), this, SLOT( reject() ) );
1404+
}
1405+
1406+
QgsTextFormatDialog::~QgsTextFormatDialog()
1407+
{
1408+
QSettings settings;
1409+
settings.setValue( "/Windows/TextFormatDialog/geometry", saveGeometry() );
1410+
}
1411+
1412+
QgsTextFormat QgsTextFormatDialog::format() const
1413+
{
1414+
return mFormatWidget->format();
1415+
}

src/gui/qgstextformatwidget.h

+39-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <ui_qgstextformatwidgetbase.h>
2121
#include "qgstextrenderer.h"
2222
#include "qgsstringutils.h"
23+
#include "qgisgui.h"
2324
#include <QFontDatabase>
2425

2526
class QgsMapCanvas;
@@ -31,7 +32,7 @@ class QgsCharacterSelectorDialog;
3132
* A widget for customising text formatting settings.
3233
*
3334
* QgsTextFormatWidget provides a widget for controlling the appearance of text rendered
34-
* using QgsTextRenderer. The preview includes all settings contained within
35+
* using QgsTextRenderer. The widget includes all settings contained within
3536
* a QgsTextFormat, including shadow, background and buffer.
3637
*
3738
* Additionally, the widget can handle labeling settings due to the large overlap between
@@ -193,6 +194,43 @@ class GUI_EXPORT QgsTextFormatWidget : public QWidget, protected Ui::QgsTextForm
193194
void updateSvgWidgets( const QString& svgPath );
194195
};
195196

197+
198+
/** \class QgsTextFormatDialog
199+
* \ingroup gui
200+
* A simple dialog for customising text formatting settings.
201+
*
202+
* QgsTextFormatDialog provides a dialog for controlling the appearance of text rendered
203+
* using QgsTextRenderer. The dialog includes all settings contained within
204+
* a QgsTextFormat, including shadow, background and buffer.
205+
*
206+
* @note Added in QGIS 3.0
207+
*/
208+
209+
class GUI_EXPORT QgsTextFormatDialog : public QDialog
210+
{
211+
Q_OBJECT
212+
213+
public:
214+
215+
/** Constructor for QgsTextFormatDialog.
216+
* @param format initial format settings to show in dialog
217+
* @param mapCanvas optional associated map canvas
218+
* @param parent parent widget
219+
* @param fl window flags for dialog
220+
*/
221+
QgsTextFormatDialog( const QgsTextFormat& format, QgsMapCanvas* mapCanvas = nullptr, QWidget* parent = nullptr, Qt::WindowFlags fl = QgisGui::ModalDialogFlags );
222+
223+
virtual ~QgsTextFormatDialog();
224+
225+
/** Returns the current formatting settings defined by the widget.
226+
*/
227+
QgsTextFormat format() const;
228+
229+
private:
230+
231+
QgsTextFormatWidget* mFormatWidget;
232+
};
233+
196234
#endif //QGSTEXTFORMATWIDGET_H
197235

198236

tests/src/python/test_qgstextformatwidget.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
QgsTextFormat,
2222
QgsUnitTypes,
2323
QgsMapUnitScale)
24-
from qgis.gui import (QgsTextFormatWidget)
24+
from qgis.gui import (QgsTextFormatWidget, QgsTextFormatDialog)
2525
from qgis.PyQt.QtGui import (QColor, QPainter, QFont, QImage, QBrush, QPen)
2626
from qgis.PyQt.QtCore import (Qt, QSizeF, QPointF, QRectF, QDir)
2727
from qgis.testing import unittest, start_app
@@ -184,6 +184,11 @@ def testSettings(self):
184184
w = QgsTextFormatWidget(s)
185185
self.checkTextFormat(w.format())
186186

187+
def testDialogSettings(self):
188+
# test that dialog correctly sets and returns matching settings
189+
s = self.createFormatSettings()
190+
d = QgsTextFormatDialog(s)
191+
self.checkTextFormat(d.format())
187192

188193
if __name__ == '__main__':
189194
unittest.main()

0 commit comments

Comments
 (0)