Skip to content

Commit 750d868

Browse files
committed
Use QgsStyleModel in style import/export dialog
Fixes hidpi icon size/text truncation issues
1 parent 2b1cbf4 commit 750d868

File tree

3 files changed

+36
-75
lines changed

3 files changed

+36
-75
lines changed

src/gui/symbology/qgsstyleexportimportdialog.cpp

Lines changed: 31 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgsguiutils.h"
2727
#include "qgssettings.h"
2828
#include "qgsgui.h"
29+
#include "qgsstylemodel.h"
2930

3031
#include <QInputDialog>
3132
#include <QCloseEvent>
@@ -53,12 +54,7 @@ QgsStyleExportImportDialog::QgsStyleExportImportDialog( QgsStyle *style, QWidget
5354
buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
5455
connect( pb, &QAbstractButton::clicked, this, &QgsStyleExportImportDialog::clearSelection );
5556

56-
QStandardItemModel *model = new QStandardItemModel( listItems );
57-
listItems->setModel( model );
58-
connect( listItems->selectionModel(), &QItemSelectionModel::selectionChanged,
59-
this, &QgsStyleExportImportDialog::selectionChanged );
60-
61-
mTempStyle = new QgsStyle();
57+
mTempStyle = qgis::make_unique< QgsStyle >();
6258
mTempStyle->createMemoryDatabase();
6359

6460
// TODO validate
@@ -92,6 +88,9 @@ QgsStyleExportImportDialog::QgsStyleExportImportDialog( QgsStyle *style, QWidget
9288

9389
label->setText( tr( "Select items to import" ) );
9490
buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Import" ) );
91+
92+
mModel = new QgsStyleModel( mTempStyle.get(), this );
93+
listItems->setModel( mModel );
9594
}
9695
else
9796
{
@@ -113,12 +112,19 @@ QgsStyleExportImportDialog::QgsStyleExportImportDialog( QgsStyle *style, QWidget
113112
tagHintLabel->setHidden( true );
114113

115114
buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Export" ) );
116-
if ( !populateStyles( mStyle ) )
117-
{
118-
QApplication::postEvent( this, new QCloseEvent() );
119-
}
120115

116+
mModel = new QgsStyleModel( mStyle, this );
121117
}
118+
119+
double iconSize = Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 10;
120+
listItems->setIconSize( QSize( static_cast< int >( iconSize ), static_cast< int >( iconSize * 0.9 ) ) ); // ~100, 90 on low dpi
121+
122+
mModel->addDesiredIconSize( listItems->iconSize() );
123+
listItems->setModel( mModel );
124+
125+
connect( listItems->selectionModel(), &QItemSelectionModel::selectionChanged,
126+
this, &QgsStyleExportImportDialog::selectionChanged );
127+
122128
// use Ok button for starting import and export operations
123129
disconnect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
124130
connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsStyleExportImportDialog::doExportImport );
@@ -155,7 +161,7 @@ void QgsStyleExportImportDialog::doExportImport()
155161
mFileName = fileName;
156162

157163
mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
158-
moveStyles( &selection, mStyle, mTempStyle );
164+
moveStyles( &selection, mStyle, mTempStyle.get() );
159165
if ( !mTempStyle->exportXml( mFileName ) )
160166
{
161167
mCursorOverride.reset();
@@ -175,11 +181,8 @@ void QgsStyleExportImportDialog::doExportImport()
175181
else // import
176182
{
177183
mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
178-
moveStyles( &selection, mTempStyle, mStyle );
184+
moveStyles( &selection, mTempStyle.get(), mStyle );
179185

180-
// clear model
181-
QStandardItemModel *model = qobject_cast<QStandardItemModel *>( listItems->model() );
182-
model->clear();
183186
accept();
184187
mCursorOverride.reset();
185188
}
@@ -188,60 +191,19 @@ void QgsStyleExportImportDialog::doExportImport()
188191
mTempStyle->clear();
189192
}
190193

191-
bool QgsStyleExportImportDialog::populateStyles( QgsStyle *style )
194+
bool QgsStyleExportImportDialog::populateStyles()
192195
{
193196
QgsTemporaryCursorOverride override( Qt::WaitCursor );
194197

195198
// load symbols and color ramps from file
196-
if ( mDialogMode == Import )
197-
{
198-
// NOTE mTempStyle is style here
199-
if ( !style->importXml( mFileName ) )
200-
{
201-
override.release();
202-
QMessageBox::warning( this, tr( "Import Symbols or Color Ramps" ),
203-
tr( "An error occurred during import:\n%1" ).arg( style->errorString() ) );
204-
return false;
205-
}
206-
}
207-
208-
QStandardItemModel *model = qobject_cast<QStandardItemModel *>( listItems->model() );
209-
model->clear();
210-
211-
// populate symbols
212-
QStringList styleNames = style->symbolNames();
213-
QString name;
214-
215-
for ( int i = 0; i < styleNames.count(); ++i )
216-
{
217-
name = styleNames[i];
218-
QStringList tags = style->tagsOfSymbol( QgsStyle::SymbolEntity, name );
219-
std::unique_ptr< QgsSymbol > symbol( style->symbol( name ) );
220-
if ( !symbol )
221-
continue;
222-
QStandardItem *item = new QStandardItem( name );
223-
QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( symbol.get(), listItems->iconSize(), 15 );
224-
item->setIcon( icon );
225-
item->setToolTip( QStringLiteral( "<b>%1</b><br><i>%2</i>" ).arg( name, tags.count() > 0 ? tags.join( QStringLiteral( ", " ) ) : tr( "Not tagged" ) ) );
226-
// Set font to 10points to show reasonable text
227-
QFont itemFont = item->font();
228-
itemFont.setPointSize( 10 );
229-
item->setFont( itemFont );
230-
model->appendRow( item );
231-
}
232-
233-
// and color ramps
234-
styleNames = style->colorRampNames();
235-
236-
for ( int i = 0; i < styleNames.count(); ++i )
199+
// NOTE mTempStyle is style here
200+
mTempStyle->clear();
201+
if ( !mTempStyle->importXml( mFileName ) )
237202
{
238-
name = styleNames[i];
239-
std::unique_ptr< QgsColorRamp > ramp( style->colorRamp( name ) );
240-
241-
QStandardItem *item = new QStandardItem( name );
242-
QIcon icon = QgsSymbolLayerUtils::colorRampPreviewIcon( ramp.get(), listItems->iconSize(), 15 );
243-
item->setIcon( icon );
244-
model->appendRow( item );
203+
override.release();
204+
QMessageBox::warning( this, tr( "Import Symbols or Color Ramps" ),
205+
tr( "An error occurred during import:\n%1" ).arg( mTempStyle->errorString() ) );
206+
return false;
245207
}
246208
return true;
247209
}
@@ -264,7 +226,7 @@ void QgsStyleExportImportDialog::moveStyles( QModelIndexList *selection, QgsStyl
264226
for ( int i = 0; i < selection->size(); ++i )
265227
{
266228
index = selection->at( i );
267-
symbolName = index.model()->data( index, 0 ).toString();
229+
symbolName = mModel->data( mModel->index( index.row(), QgsStyleModel::Name ), Qt::DisplayRole ).toString();
268230
std::unique_ptr< QgsSymbol > symbol( src->symbol( symbolName ) );
269231
std::unique_ptr< QgsColorRamp > ramp;
270232

@@ -405,7 +367,6 @@ void QgsStyleExportImportDialog::moveStyles( QModelIndexList *selection, QgsStyl
405367
QgsStyleExportImportDialog::~QgsStyleExportImportDialog()
406368
{
407369
delete mTempFile;
408-
delete mTempStyle;
409370
delete mGroupSelectionDlg;
410371
}
411372

@@ -423,7 +384,7 @@ void QgsStyleExportImportDialog::selectSymbols( const QStringList &symbolNames )
423384
{
424385
Q_FOREACH ( const QString &symbolName, symbolNames )
425386
{
426-
QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0, 0 ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
387+
QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0, QgsStyleModel::Name ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
427388
Q_FOREACH ( const QModelIndex &index, indexes )
428389
{
429390
listItems->selectionModel()->select( index, QItemSelectionModel::Select );
@@ -435,7 +396,7 @@ void QgsStyleExportImportDialog::deselectSymbols( const QStringList &symbolNames
435396
{
436397
Q_FOREACH ( const QString &symbolName, symbolNames )
437398
{
438-
QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0, 0 ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
399+
QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0, QgsStyleModel::Name ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
439400
Q_FOREACH ( const QModelIndex &index, indexes )
440401
{
441402
QItemSelection deselection( index, index );
@@ -536,7 +497,7 @@ void QgsStyleExportImportDialog::importFileChanged( const QString &path )
536497
if ( QFileInfo::exists( mFileName ) )
537498
{
538499
mTempStyle->clear();
539-
populateStyles( mTempStyle );
500+
populateStyles();
540501
mImportFileWidget->setDefaultRoot( pathInfo.absolutePath() );
541502
QgsSettings settings;
542503
settings.setValue( QStringLiteral( "StyleManager/lastImportDir" ), pathInfo.absolutePath(), QgsSettings::Gui );
@@ -596,7 +557,7 @@ void QgsStyleExportImportDialog::httpFinished()
596557
mTempFile->flush();
597558
mTempFile->close();
598559
mTempStyle->clear();
599-
populateStyles( mTempStyle );
560+
populateStyles();
600561
}
601562
}
602563

src/gui/symbology/qgsstyleexportimportdialog.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
class QgsStyle;
3434
class QgsStyleGroupSelectionDialog;
3535
class QgsTemporaryCursorOverride;
36+
class QgsStyleModel;
3637

3738
/**
3839
* \ingroup gui
@@ -131,7 +132,7 @@ class GUI_EXPORT QgsStyleExportImportDialog : public QDialog, private Ui::QgsSty
131132
};
132133

133134
void downloadStyleXml( const QUrl &url );
134-
bool populateStyles( QgsStyle *style );
135+
bool populateStyles();
135136
void moveStyles( QModelIndexList *selection, QgsStyle *src, QgsStyle *dst );
136137

137138
QProgressDialog *mProgressDlg = nullptr;
@@ -143,8 +144,10 @@ class GUI_EXPORT QgsStyleExportImportDialog : public QDialog, private Ui::QgsSty
143144
QString mFileName;
144145
Mode mDialogMode;
145146

147+
QgsStyleModel *mModel = nullptr;
148+
146149
QgsStyle *mStyle = nullptr;
147-
QgsStyle *mTempStyle = nullptr;
150+
std::unique_ptr< QgsStyle > mTempStyle;
148151
std::unique_ptr< QgsTemporaryCursorOverride > mCursorOverride;
149152
};
150153

src/ui/qgsstyleexportimportdialogbase.ui

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,6 @@
172172
<property name="viewMode">
173173
<enum>QListView::IconMode</enum>
174174
</property>
175-
<property name="uniformItemSizes">
176-
<bool>true</bool>
177-
</property>
178175
<property name="wordWrap">
179176
<bool>true</bool>
180177
</property>

0 commit comments

Comments
 (0)