Skip to content

Commit

Permalink
Consolidate some duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 14, 2016
1 parent d6ee375 commit 56e8b9c
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 182 deletions.
12 changes: 12 additions & 0 deletions python/gui/qgscolorschemelist.sip
Expand Up @@ -178,6 +178,18 @@ class QgsColorSchemeList: QTreeView
*/
void copyColors();

/** Displays a file picker dialog allowing users to import colors into the list from a file.
* @note added in QGIS 3.0
* @see showExportColorsDialog()
*/
void showImportColorsDialog();

/** Displays a file picker dialog allowing users to export colors from the list into a file.
* @note added in QGIS 3.0
* @see showImportColorsDialog()
*/
void showExportColorsDialog();

signals:

/** Emitted when a color is selected from the list
Expand Down
60 changes: 2 additions & 58 deletions src/app/qgsoptions.cpp
Expand Up @@ -745,6 +745,8 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl )
connect( mButtonCopyColors, SIGNAL( clicked() ), mTreeCustomColors, SLOT( copyColors() ) );
connect( mButtonRemoveColor, SIGNAL( clicked() ), mTreeCustomColors, SLOT( removeSelection() ) );
connect( mButtonPasteColors, SIGNAL( clicked() ), mTreeCustomColors, SLOT( pasteColors() ) );
connect( mButtonImportColors, SIGNAL( clicked( bool ) ), mTreeCustomColors, SLOT( showImportColorsDialog() ) );
connect( mButtonExportColors, SIGNAL( clicked( bool ) ), mTreeCustomColors, SLOT( showExportColorsDialog() ) );

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

void QgsOptions::on_mButtonImportColors_clicked()
{
QSettings s;
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" );
activateWindow();
if ( filePath.isEmpty() )
{
return;
}

//check if file exists
QFileInfo fileInfo( filePath );
if ( !fileInfo.exists() || !fileInfo.isReadable() )
{
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) );
return;
}

s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
QFile file( filePath );
bool importOk = mTreeCustomColors->importColorsFromGpl( file );
if ( !importOk )
{
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, no colors found in palette file" ) );
return;
}
}

void QgsOptions::on_mButtonExportColors_clicked()
{
QSettings s;
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
QString fileName = QFileDialog::getSaveFileName( this, tr( "Palette file" ), lastDir, "GPL (*.gpl)" );
activateWindow();
if ( fileName.isEmpty() )
{
return;
}

// ensure filename contains extension
if ( !fileName.endsWith( ".gpl", Qt::CaseInsensitive ) )
{
fileName += ".gpl";
}

QFileInfo fileInfo( fileName );
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );

QFile file( fileName );
bool exportOk = mTreeCustomColors->exportColorsToGpl( file );
if ( !exportOk )
{
QMessageBox::critical( nullptr, tr( "Error exporting" ), tr( "Error writing palette file" ) );
return;
}
}

QListWidgetItem* QgsOptions::addScaleToScaleList( const QString &newScale )
{
QListWidgetItem* newItem = new QListWidgetItem( newScale );
Expand Down
2 changes: 0 additions & 2 deletions src/app/qgsoptions.h
Expand Up @@ -199,8 +199,6 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
void on_mAddDefaultTransformButton_clicked();

void on_mButtonAddColor_clicked();
void on_mButtonImportColors_clicked();
void on_mButtonExportColors_clicked();

private:
QSettings *mSettings;
Expand Down
60 changes: 2 additions & 58 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -669,6 +669,8 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
connect( mButtonCopyColors, SIGNAL( clicked() ), mTreeProjectColors, SLOT( copyColors() ) );
connect( mButtonRemoveColor, SIGNAL( clicked() ), mTreeProjectColors, SLOT( removeSelection() ) );
connect( mButtonPasteColors, SIGNAL( clicked() ), mTreeProjectColors, SLOT( pasteColors() ) );
connect( mButtonImportColors, SIGNAL( clicked( bool ) ), mTreeProjectColors, SLOT( showImportColorsDialog() ) );
connect( mButtonExportColors, SIGNAL( clicked( bool ) ), mTreeProjectColors, SLOT( showExportColorsDialog() ) );

QList<QgsProjectColorScheme *> projectSchemes;
QgsColorSchemeRegistry::instance()->schemes( projectSchemes );
Expand Down Expand Up @@ -2033,64 +2035,6 @@ void QgsProjectProperties::on_mButtonAddColor_clicked()
mTreeProjectColors->addColor( newColor, QgsSymbolLayerUtils::colorToName( newColor ) );
}

void QgsProjectProperties::on_mButtonImportColors_clicked()
{
QSettings s;
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" );
activateWindow();
if ( filePath.isEmpty() )
{
return;
}

//check if file exists
QFileInfo fileInfo( filePath );
if ( !fileInfo.exists() || !fileInfo.isReadable() )
{
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) );
return;
}

s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
QFile file( filePath );
bool importOk = mTreeProjectColors->importColorsFromGpl( file );
if ( !importOk )
{
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, no colors found in palette file" ) );
return;
}
}

void QgsProjectProperties::on_mButtonExportColors_clicked()
{
QSettings s;
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
QString fileName = QFileDialog::getSaveFileName( this, tr( "Palette file" ), lastDir, "GPL (*.gpl)" );
activateWindow();
if ( fileName.isEmpty() )
{
return;
}

// ensure filename contains extension
if ( !fileName.endsWith( ".gpl", Qt::CaseInsensitive ) )
{
fileName += ".gpl";
}

QFileInfo fileInfo( fileName );
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );

QFile file( fileName );
bool exportOk = mTreeProjectColors->exportColorsToGpl( file );
if ( !exportOk )
{
QMessageBox::critical( nullptr, tr( "Error exporting" ), tr( "Error writing palette file" ) );
return;
}
}

QListWidgetItem* QgsProjectProperties::addScaleToScaleList( const QString &newScale )
{
// TODO QGIS3: Rework the scale list widget to be a reusable piece of code, see PR #2558
Expand Down
2 changes: 0 additions & 2 deletions src/app/qgsprojectproperties.h
Expand Up @@ -167,8 +167,6 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui:
void projectionSelectorInitialized();

void on_mButtonAddColor_clicked();
void on_mButtonImportColors_clicked();
void on_mButtonExportColors_clicked();

signals:
//! Signal used to inform listeners that the mouse display precision may have changed
Expand Down
60 changes: 60 additions & 0 deletions src/gui/qgscolorschemelist.cpp
Expand Up @@ -23,6 +23,8 @@
#include <QMimeData>
#include <QClipboard>
#include <QKeyEvent>
#include <QFileDialog>
#include <QMessageBox>

#ifdef ENABLE_MODELTEST
#include "modeltest.h"
Expand Down Expand Up @@ -135,6 +137,64 @@ void QgsColorSchemeList::copyColors()
QApplication::clipboard()->setMimeData( mimeData );
}

void QgsColorSchemeList::showImportColorsDialog()
{
QSettings s;
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" );
activateWindow();
if ( filePath.isEmpty() )
{
return;
}

//check if file exists
QFileInfo fileInfo( filePath );
if ( !fileInfo.exists() || !fileInfo.isReadable() )
{
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) );
return;
}

s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
QFile file( filePath );
bool importOk = importColorsFromGpl( file );
if ( !importOk )
{
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, no colors found in palette file" ) );
return;
}
}

void QgsColorSchemeList::showExportColorsDialog()
{
QSettings s;
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
QString fileName = QFileDialog::getSaveFileName( this, tr( "Palette file" ), lastDir, "GPL (*.gpl)" );
activateWindow();
if ( fileName.isEmpty() )
{
return;
}

// ensure filename contains extension
if ( !fileName.endsWith( ".gpl", Qt::CaseInsensitive ) )
{
fileName += ".gpl";
}

QFileInfo fileInfo( fileName );
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );

QFile file( fileName );
bool exportOk = exportColorsToGpl( file );
if ( !exportOk )
{
QMessageBox::critical( nullptr, tr( "Error exporting" ), tr( "Error writing palette file" ) );
return;
}
}

void QgsColorSchemeList::keyPressEvent( QKeyEvent *event )
{
//listen out for delete/backspace presses and remove selected colors
Expand Down
12 changes: 12 additions & 0 deletions src/gui/qgscolorschemelist.h
Expand Up @@ -218,6 +218,18 @@ class GUI_EXPORT QgsColorSchemeList: public QTreeView
*/
void copyColors();

/** Displays a file picker dialog allowing users to import colors into the list from a file.
* @note added in QGIS 3.0
* @see showExportColorsDialog()
*/
void showImportColorsDialog();

/** Displays a file picker dialog allowing users to export colors from the list into a file.
* @note added in QGIS 3.0
* @see showImportColorsDialog()
*/
void showExportColorsDialog();

signals:

/** Emitted when a color is selected from the list
Expand Down
62 changes: 2 additions & 60 deletions src/gui/qgscompoundcolorwidget.cpp
Expand Up @@ -77,8 +77,8 @@ QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor& c

connect( mActionCopyColors, SIGNAL( triggered() ), mSchemeList, SLOT( copyColors() ) );
connect( mActionPasteColors, SIGNAL( triggered() ), mSchemeList, SLOT( pasteColors() ) );
connect( mActionExportColors, SIGNAL( triggered() ), this, SLOT( exportColors() ) );
connect( mActionImportColors, SIGNAL( triggered() ), this, SLOT( importColors() ) );
connect( mActionExportColors, SIGNAL( triggered() ), mSchemeList, SLOT( showExportColorsDialog() ) );
connect( mActionImportColors, SIGNAL( triggered() ), mSchemeList, SLOT( showImportColorsDialog() ) );
connect( mActionImportPalette, SIGNAL( triggered() ), this, SLOT( importPalette() ) );
connect( mActionRemovePalette, SIGNAL( triggered() ), this, SLOT( removePalette() ) );
connect( mActionNewPalette, SIGNAL( triggered() ), this, SLOT( newPalette() ) );
Expand Down Expand Up @@ -261,35 +261,6 @@ void QgsCompoundColorWidget::setAllowAlpha( const bool allowAlpha )
}
}

void QgsCompoundColorWidget::importColors()
{
QSettings s;
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" );
activateWindow();
if ( filePath.isEmpty() )
{
return;
}

//check if file exists
QFileInfo fileInfo( filePath );
if ( !fileInfo.exists() || !fileInfo.isReadable() )
{
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) );
return;
}

s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
QFile file( filePath );
bool importOk = mSchemeList->importColorsFromGpl( file );
if ( !importOk )
{
QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, no colors found in palette file" ) );
return;
}
}

void QgsCompoundColorWidget::refreshSchemeComboBox()
{
mSchemeComboBox->blockSignals( true );
Expand Down Expand Up @@ -445,35 +416,6 @@ QString QgsCompoundColorWidget::gplFilePath()
return palettesDir;
}

void QgsCompoundColorWidget::exportColors()
{
QSettings s;
QString lastDir = s.value( "/UI/lastGplPaletteDir", QDir::homePath() ).toString();
QString fileName = QFileDialog::getSaveFileName( this, tr( "Palette file" ), lastDir, "GPL (*.gpl)" );
activateWindow();
if ( fileName.isEmpty() )
{
return;
}

// ensure filename contains extension
if ( !fileName.endsWith( ".gpl", Qt::CaseInsensitive ) )
{
fileName += ".gpl";
}

QFileInfo fileInfo( fileName );
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );

QFile file( fileName );
bool exportOk = mSchemeList->exportColorsToGpl( file );
if ( !exportOk )
{
QMessageBox::critical( nullptr, tr( "Error exporting" ), tr( "Error writing palette file" ) );
return;
}
}

void QgsCompoundColorWidget::schemeIndexChanged( int index )
{
//save changes to scheme
Expand Down
2 changes: 0 additions & 2 deletions src/gui/qgscompoundcolorwidget.h
Expand Up @@ -101,8 +101,6 @@ class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::Qgs

void on_mAddColorToSchemeButton_clicked();

void exportColors();
void importColors();
void importPalette();
void removePalette();
void newPalette();
Expand Down

0 comments on commit 56e8b9c

Please sign in to comment.