Skip to content
Permalink
Browse files

Consolidate some duplicate code

  • Loading branch information
nyalldawson committed Sep 14, 2016
1 parent d6ee375 commit 56e8b9cff5e21134f8c60cb6e60155b82c49892e
@@ -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
@@ -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;
@@ -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 );
@@ -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;
@@ -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 );
@@ -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
@@ -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
@@ -23,6 +23,8 @@
#include <QMimeData>
#include <QClipboard>
#include <QKeyEvent>
#include <QFileDialog>
#include <QMessageBox>

#ifdef ENABLE_MODELTEST
#include "modeltest.h"
@@ -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
@@ -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
@@ -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() ) );
@@ -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 );
@@ -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
@@ -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();

0 comments on commit 56e8b9c

Please sign in to comment.
You can’t perform that action at this time.