Skip to content

Commit 56e8b9c

Browse files
committed
Consolidate some duplicate code
1 parent d6ee375 commit 56e8b9c

9 files changed

+90
-182
lines changed

python/gui/qgscolorschemelist.sip

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ class QgsColorSchemeList: QTreeView
178178
*/
179179
void copyColors();
180180

181+
/** Displays a file picker dialog allowing users to import colors into the list from a file.
182+
* @note added in QGIS 3.0
183+
* @see showExportColorsDialog()
184+
*/
185+
void showImportColorsDialog();
186+
187+
/** Displays a file picker dialog allowing users to export colors from the list into a file.
188+
* @note added in QGIS 3.0
189+
* @see showImportColorsDialog()
190+
*/
191+
void showExportColorsDialog();
192+
181193
signals:
182194

183195
/** Emitted when a color is selected from the list

src/app/qgsoptions.cpp

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,8 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl )
745745
connect( mButtonCopyColors, SIGNAL( clicked() ), mTreeCustomColors, SLOT( copyColors() ) );
746746
connect( mButtonRemoveColor, SIGNAL( clicked() ), mTreeCustomColors, SLOT( removeSelection() ) );
747747
connect( mButtonPasteColors, SIGNAL( clicked() ), mTreeCustomColors, SLOT( pasteColors() ) );
748+
connect( mButtonImportColors, SIGNAL( clicked( bool ) ), mTreeCustomColors, SLOT( showImportColorsDialog() ) );
749+
connect( mButtonExportColors, SIGNAL( clicked( bool ) ), mTreeCustomColors, SLOT( showExportColorsDialog() ) );
748750

749751
//find custom color scheme from registry
750752
QList<QgsCustomColorScheme *> customSchemes;
@@ -2160,64 +2162,6 @@ void QgsOptions::on_mButtonAddColor_clicked()
21602162
mTreeCustomColors->addColor( newColor, QgsSymbolLayerUtils::colorToName( newColor ) );
21612163
}
21622164

2163-
void QgsOptions::on_mButtonImportColors_clicked()
2164-
{
2165-
QSettings s;
2166-
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
2167-
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" );
2168-
activateWindow();
2169-
if ( filePath.isEmpty() )
2170-
{
2171-
return;
2172-
}
2173-
2174-
//check if file exists
2175-
QFileInfo fileInfo( filePath );
2176-
if ( !fileInfo.exists() || !fileInfo.isReadable() )
2177-
{
2178-
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) );
2179-
return;
2180-
}
2181-
2182-
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
2183-
QFile file( filePath );
2184-
bool importOk = mTreeCustomColors->importColorsFromGpl( file );
2185-
if ( !importOk )
2186-
{
2187-
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, no colors found in palette file" ) );
2188-
return;
2189-
}
2190-
}
2191-
2192-
void QgsOptions::on_mButtonExportColors_clicked()
2193-
{
2194-
QSettings s;
2195-
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
2196-
QString fileName = QFileDialog::getSaveFileName( this, tr( "Palette file" ), lastDir, "GPL (*.gpl)" );
2197-
activateWindow();
2198-
if ( fileName.isEmpty() )
2199-
{
2200-
return;
2201-
}
2202-
2203-
// ensure filename contains extension
2204-
if ( !fileName.endsWith( ".gpl", Qt::CaseInsensitive ) )
2205-
{
2206-
fileName += ".gpl";
2207-
}
2208-
2209-
QFileInfo fileInfo( fileName );
2210-
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
2211-
2212-
QFile file( fileName );
2213-
bool exportOk = mTreeCustomColors->exportColorsToGpl( file );
2214-
if ( !exportOk )
2215-
{
2216-
QMessageBox::critical( nullptr, tr( "Error exporting" ), tr( "Error writing palette file" ) );
2217-
return;
2218-
}
2219-
}
2220-
22212165
QListWidgetItem* QgsOptions::addScaleToScaleList( const QString &newScale )
22222166
{
22232167
QListWidgetItem* newItem = new QListWidgetItem( newScale );

src/app/qgsoptions.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
199199
void on_mAddDefaultTransformButton_clicked();
200200

201201
void on_mButtonAddColor_clicked();
202-
void on_mButtonImportColors_clicked();
203-
void on_mButtonExportColors_clicked();
204202

205203
private:
206204
QSettings *mSettings;

src/app/qgsprojectproperties.cpp

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,8 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
669669
connect( mButtonCopyColors, SIGNAL( clicked() ), mTreeProjectColors, SLOT( copyColors() ) );
670670
connect( mButtonRemoveColor, SIGNAL( clicked() ), mTreeProjectColors, SLOT( removeSelection() ) );
671671
connect( mButtonPasteColors, SIGNAL( clicked() ), mTreeProjectColors, SLOT( pasteColors() ) );
672+
connect( mButtonImportColors, SIGNAL( clicked( bool ) ), mTreeProjectColors, SLOT( showImportColorsDialog() ) );
673+
connect( mButtonExportColors, SIGNAL( clicked( bool ) ), mTreeProjectColors, SLOT( showExportColorsDialog() ) );
672674

673675
QList<QgsProjectColorScheme *> projectSchemes;
674676
QgsColorSchemeRegistry::instance()->schemes( projectSchemes );
@@ -2033,64 +2035,6 @@ void QgsProjectProperties::on_mButtonAddColor_clicked()
20332035
mTreeProjectColors->addColor( newColor, QgsSymbolLayerUtils::colorToName( newColor ) );
20342036
}
20352037

2036-
void QgsProjectProperties::on_mButtonImportColors_clicked()
2037-
{
2038-
QSettings s;
2039-
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
2040-
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" );
2041-
activateWindow();
2042-
if ( filePath.isEmpty() )
2043-
{
2044-
return;
2045-
}
2046-
2047-
//check if file exists
2048-
QFileInfo fileInfo( filePath );
2049-
if ( !fileInfo.exists() || !fileInfo.isReadable() )
2050-
{
2051-
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) );
2052-
return;
2053-
}
2054-
2055-
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
2056-
QFile file( filePath );
2057-
bool importOk = mTreeProjectColors->importColorsFromGpl( file );
2058-
if ( !importOk )
2059-
{
2060-
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, no colors found in palette file" ) );
2061-
return;
2062-
}
2063-
}
2064-
2065-
void QgsProjectProperties::on_mButtonExportColors_clicked()
2066-
{
2067-
QSettings s;
2068-
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
2069-
QString fileName = QFileDialog::getSaveFileName( this, tr( "Palette file" ), lastDir, "GPL (*.gpl)" );
2070-
activateWindow();
2071-
if ( fileName.isEmpty() )
2072-
{
2073-
return;
2074-
}
2075-
2076-
// ensure filename contains extension
2077-
if ( !fileName.endsWith( ".gpl", Qt::CaseInsensitive ) )
2078-
{
2079-
fileName += ".gpl";
2080-
}
2081-
2082-
QFileInfo fileInfo( fileName );
2083-
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
2084-
2085-
QFile file( fileName );
2086-
bool exportOk = mTreeProjectColors->exportColorsToGpl( file );
2087-
if ( !exportOk )
2088-
{
2089-
QMessageBox::critical( nullptr, tr( "Error exporting" ), tr( "Error writing palette file" ) );
2090-
return;
2091-
}
2092-
}
2093-
20942038
QListWidgetItem* QgsProjectProperties::addScaleToScaleList( const QString &newScale )
20952039
{
20962040
// TODO QGIS3: Rework the scale list widget to be a reusable piece of code, see PR #2558

src/app/qgsprojectproperties.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui:
167167
void projectionSelectorInitialized();
168168

169169
void on_mButtonAddColor_clicked();
170-
void on_mButtonImportColors_clicked();
171-
void on_mButtonExportColors_clicked();
172170

173171
signals:
174172
//! Signal used to inform listeners that the mouse display precision may have changed

src/gui/qgscolorschemelist.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <QMimeData>
2424
#include <QClipboard>
2525
#include <QKeyEvent>
26+
#include <QFileDialog>
27+
#include <QMessageBox>
2628

2729
#ifdef ENABLE_MODELTEST
2830
#include "modeltest.h"
@@ -135,6 +137,64 @@ void QgsColorSchemeList::copyColors()
135137
QApplication::clipboard()->setMimeData( mimeData );
136138
}
137139

140+
void QgsColorSchemeList::showImportColorsDialog()
141+
{
142+
QSettings s;
143+
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
144+
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" );
145+
activateWindow();
146+
if ( filePath.isEmpty() )
147+
{
148+
return;
149+
}
150+
151+
//check if file exists
152+
QFileInfo fileInfo( filePath );
153+
if ( !fileInfo.exists() || !fileInfo.isReadable() )
154+
{
155+
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) );
156+
return;
157+
}
158+
159+
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
160+
QFile file( filePath );
161+
bool importOk = importColorsFromGpl( file );
162+
if ( !importOk )
163+
{
164+
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, no colors found in palette file" ) );
165+
return;
166+
}
167+
}
168+
169+
void QgsColorSchemeList::showExportColorsDialog()
170+
{
171+
QSettings s;
172+
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
173+
QString fileName = QFileDialog::getSaveFileName( this, tr( "Palette file" ), lastDir, "GPL (*.gpl)" );
174+
activateWindow();
175+
if ( fileName.isEmpty() )
176+
{
177+
return;
178+
}
179+
180+
// ensure filename contains extension
181+
if ( !fileName.endsWith( ".gpl", Qt::CaseInsensitive ) )
182+
{
183+
fileName += ".gpl";
184+
}
185+
186+
QFileInfo fileInfo( fileName );
187+
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
188+
189+
QFile file( fileName );
190+
bool exportOk = exportColorsToGpl( file );
191+
if ( !exportOk )
192+
{
193+
QMessageBox::critical( nullptr, tr( "Error exporting" ), tr( "Error writing palette file" ) );
194+
return;
195+
}
196+
}
197+
138198
void QgsColorSchemeList::keyPressEvent( QKeyEvent *event )
139199
{
140200
//listen out for delete/backspace presses and remove selected colors

src/gui/qgscolorschemelist.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,18 @@ class GUI_EXPORT QgsColorSchemeList: public QTreeView
218218
*/
219219
void copyColors();
220220

221+
/** Displays a file picker dialog allowing users to import colors into the list from a file.
222+
* @note added in QGIS 3.0
223+
* @see showExportColorsDialog()
224+
*/
225+
void showImportColorsDialog();
226+
227+
/** Displays a file picker dialog allowing users to export colors from the list into a file.
228+
* @note added in QGIS 3.0
229+
* @see showImportColorsDialog()
230+
*/
231+
void showExportColorsDialog();
232+
221233
signals:
222234

223235
/** Emitted when a color is selected from the list

src/gui/qgscompoundcolorwidget.cpp

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor& c
7777

7878
connect( mActionCopyColors, SIGNAL( triggered() ), mSchemeList, SLOT( copyColors() ) );
7979
connect( mActionPasteColors, SIGNAL( triggered() ), mSchemeList, SLOT( pasteColors() ) );
80-
connect( mActionExportColors, SIGNAL( triggered() ), this, SLOT( exportColors() ) );
81-
connect( mActionImportColors, SIGNAL( triggered() ), this, SLOT( importColors() ) );
80+
connect( mActionExportColors, SIGNAL( triggered() ), mSchemeList, SLOT( showExportColorsDialog() ) );
81+
connect( mActionImportColors, SIGNAL( triggered() ), mSchemeList, SLOT( showImportColorsDialog() ) );
8282
connect( mActionImportPalette, SIGNAL( triggered() ), this, SLOT( importPalette() ) );
8383
connect( mActionRemovePalette, SIGNAL( triggered() ), this, SLOT( removePalette() ) );
8484
connect( mActionNewPalette, SIGNAL( triggered() ), this, SLOT( newPalette() ) );
@@ -261,35 +261,6 @@ void QgsCompoundColorWidget::setAllowAlpha( const bool allowAlpha )
261261
}
262262
}
263263

264-
void QgsCompoundColorWidget::importColors()
265-
{
266-
QSettings s;
267-
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
268-
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" );
269-
activateWindow();
270-
if ( filePath.isEmpty() )
271-
{
272-
return;
273-
}
274-
275-
//check if file exists
276-
QFileInfo fileInfo( filePath );
277-
if ( !fileInfo.exists() || !fileInfo.isReadable() )
278-
{
279-
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) );
280-
return;
281-
}
282-
283-
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
284-
QFile file( filePath );
285-
bool importOk = mSchemeList->importColorsFromGpl( file );
286-
if ( !importOk )
287-
{
288-
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, no colors found in palette file" ) );
289-
return;
290-
}
291-
}
292-
293264
void QgsCompoundColorWidget::refreshSchemeComboBox()
294265
{
295266
mSchemeComboBox->blockSignals( true );
@@ -445,35 +416,6 @@ QString QgsCompoundColorWidget::gplFilePath()
445416
return palettesDir;
446417
}
447418

448-
void QgsCompoundColorWidget::exportColors()
449-
{
450-
QSettings s;
451-
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
452-
QString fileName = QFileDialog::getSaveFileName( this, tr( "Palette file" ), lastDir, "GPL (*.gpl)" );
453-
activateWindow();
454-
if ( fileName.isEmpty() )
455-
{
456-
return;
457-
}
458-
459-
// ensure filename contains extension
460-
if ( !fileName.endsWith( ".gpl", Qt::CaseInsensitive ) )
461-
{
462-
fileName += ".gpl";
463-
}
464-
465-
QFileInfo fileInfo( fileName );
466-
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
467-
468-
QFile file( fileName );
469-
bool exportOk = mSchemeList->exportColorsToGpl( file );
470-
if ( !exportOk )
471-
{
472-
QMessageBox::critical( nullptr, tr( "Error exporting" ), tr( "Error writing palette file" ) );
473-
return;
474-
}
475-
}
476-
477419
void QgsCompoundColorWidget::schemeIndexChanged( int index )
478420
{
479421
//save changes to scheme

src/gui/qgscompoundcolorwidget.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::Qgs
101101

102102
void on_mAddColorToSchemeButton_clicked();
103103

104-
void exportColors();
105-
void importColors();
106104
void importPalette();
107105
void removePalette();
108106
void newPalette();

0 commit comments

Comments
 (0)