Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow more customisation of QgsNewNameDialog
  • Loading branch information
nyalldawson committed Aug 20, 2015
1 parent 0df7102 commit ca04bb6
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 4 deletions.
1 change: 1 addition & 0 deletions python/gui/gui.sip
Expand Up @@ -94,6 +94,7 @@
%Include qgsmessageviewer.sip
%Include qgsnewhttpconnection.sip
%Include qgsnewmemorylayerdialog.sip
%Include qgsnewnamedialog.sip
%Include qgsnewvectorlayerdialog.sip
%Include qgsnumericsortlistviewitem.sip
%Include qgsoptionsdialogbase.sip
Expand Down
95 changes: 95 additions & 0 deletions python/gui/qgsnewnamedialog.sip
@@ -0,0 +1,95 @@
/** \ingroup gui
* New name, for example new layer name dialog. If existing names are provided,
* the dialog warns users if an entered name already exists.
* @note added in 2.10
*/
class QgsNewNameDialog : QgsDialog
{
%TypeHeaderCode
#include <qgsnewnamedialog.h>
%End

public:
/** New dialog constructor.
* @param source original data source name, e.g. original layer name of the layer to be copied
* @param initial initial name
* @param extensions base name extensions, e.g. raster base name band extensions or vector layer type extensions
* @param existing existing names
* @param regexp regular expression to be used as validator, for example db tables should have "[A-Za-z_][A-Za-z0-9_]+"
* @param cs case sensitivity for new name to existing names comparison
* @param parent
* @param flags
*/
QgsNewNameDialog( const QString& source = QString::null, const QString& initial = QString::null,
const QStringList& extensions = QStringList(), const QStringList& existing = QStringList(),
const QRegExp& regexp = QRegExp(), Qt::CaseSensitivity cs = Qt::CaseSensitive,
QWidget *parent /TransferThis/ = 0, Qt::WindowFlags flags = QgisGui::ModalDialogFlags );

/** Sets the hint string for the dialog (the text shown above the name
* input box).
* @param hintString hint text
* @see hintString()
* @note added in QGIS 2.12
*/
void setHintString( const QString& hintString );

/** Returns the hint string for the dialog (the text shown above the name
* input box).
* @see setHintString()
* @note added in QGIS 2.12
*/
QString hintString() const;

/** Sets whether users are permitted to overwrite existing names. If true, then
* the dialog will reflect that the new name will overwrite an existing name. If false,
* then the dialog will not accept names which already exist.
* @note added in QGIS 2.12
* @see overwriteEnabled()
*/
void setOverwriteEnabled( bool enabled );

/** Returns whether users are permitted to overwrite existing names.
* @note added in QGIS 2.12
* @see setOverwriteEnabled()
*/
bool overwriteEnabled() const;

/** Sets the string used for warning users if a conflicting name exists.
* @param string warning string. If empty a default warning string will be used.
* @note added in QGIS 2.12
* @see conflictingNameWarning()
*/
void setConflictingNameWarning( const QString& string );

/** Returns the string used for warning users if a conflicting name exists.
* @note added in QGIS 2.12
* @see setConflictingNameWarning()
*/
QString conflictingNameWarning() const;

/** Name entered by user.
* @return new name
*/
QString name() const;

/** Test if name or name with at least one extension exists.
* @param name name or base name
* @param extensions base name extensions
* @param existing existing names
* @param cs case sensitivity for new name to existing names comparison
* @return true if name exists
*/
static bool exists( const QString& name, const QStringList& extensions,
const QStringList& existing, Qt::CaseSensitivity cs = Qt::CaseSensitive );
public slots:
void nameChanged();

protected:

QString highlightText( const QString& text );
static QStringList fullNames( const QString& name, const QStringList& extensions );
// get list of existing names
static QStringList matching( const QStringList& newNames, const QStringList& existingNames,
Qt::CaseSensitivity cs = Qt::CaseSensitive );
};

41 changes: 37 additions & 4 deletions src/gui/qgsnewnamedialog.cpp
Expand Up @@ -33,6 +33,7 @@ QgsNewNameDialog::QgsNewNameDialog( const QString& source, const QString& initia
, mCaseSensitivity( cs )
, mNamesLabel( 0 )
, mRegexp( regexp )
, mOverwriteEnabled( true )
{
setWindowTitle( tr( "New name" ) );
QDialog::layout()->setSizeConstraint( QLayout::SetMinimumSize );
Expand All @@ -49,15 +50,16 @@ QgsNewNameDialog::QgsNewNameDialog( const QString& source, const QString& initia
{
hintString = tr( "Enter new %1 for %2" ).arg( nameDesc ).arg( source );
}
QLabel* hintLabel = new QLabel( hintString, this );
layout()->addWidget( hintLabel );
mHintLabel = new QLabel( hintString, this );
layout()->addWidget( mHintLabel );

mLineEdit = new QLineEdit( initial, this );
if ( !regexp.isEmpty() )
{
QRegExpValidator *validator = new QRegExpValidator( regexp, this );
mLineEdit->setValidator( validator );
}
mLineEdit->setMinimumWidth( mLineEdit->fontMetrics().width( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ) );
connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( nameChanged() ) );
layout()->addWidget( mLineEdit );

Expand All @@ -77,6 +79,28 @@ QgsNewNameDialog::QgsNewNameDialog( const QString& source, const QString& initia
nameChanged();
}

void QgsNewNameDialog::setHintString( const QString &hintString )
{
mHintLabel->setText( hintString );
}

QString QgsNewNameDialog::hintString() const
{
return mHintLabel->text();
}

void QgsNewNameDialog::setOverwriteEnabled( bool enabled )
{
mOverwriteEnabled = enabled;
nameChanged(); //update UI
}

void QgsNewNameDialog::setConflictingNameWarning( const QString& string )
{
mConflictingNameWarning = string;
nameChanged(); //update UI
}

QString QgsNewNameDialog::highlightText( const QString& text )
{
return "<b>" + text + "</b>";
Expand Down Expand Up @@ -116,8 +140,17 @@ void QgsNewNameDialog::nameChanged()

if ( !conflicts.isEmpty() )
{
mErrorLabel->setText( highlightText( tr( "%n Name(s) %1 exists", 0, conflicts.size() ).arg( conflicts.join( ", " ) ) ) );
okButton->setText( tr( "Overwrite" ) );
QString warning = !mConflictingNameWarning.isEmpty() ? mConflictingNameWarning
: tr( "%n Name(s) %1 exists", 0, conflicts.size() ).arg( conflicts.join( ", " ) );
mErrorLabel->setText( highlightText( warning ) );
if ( mOverwriteEnabled )
{
okButton->setText( tr( "Overwrite" ) );
}
else
{
okButton->setEnabled( false );
}
return;
}
}
Expand Down
46 changes: 46 additions & 0 deletions src/gui/qgsnewnamedialog.h
Expand Up @@ -46,6 +46,48 @@ class GUI_EXPORT QgsNewNameDialog : public QgsDialog
const QRegExp& regexp = QRegExp(), Qt::CaseSensitivity cs = Qt::CaseSensitive,
QWidget *parent = 0, Qt::WindowFlags flags = QgisGui::ModalDialogFlags );

/** Sets the hint string for the dialog (the text shown above the name
* input box).
* @param hintString hint text
* @see hintString()
* @note added in QGIS 2.12
*/
void setHintString( const QString& hintString );

/** Returns the hint string for the dialog (the text shown above the name
* input box).
* @see setHintString()
* @note added in QGIS 2.12
*/
QString hintString() const;

/** Sets whether users are permitted to overwrite existing names. If true, then
* the dialog will reflect that the new name will overwrite an existing name. If false,
* then the dialog will not accept names which already exist.
* @note added in QGIS 2.12
* @see overwriteEnabled()
*/
void setOverwriteEnabled( bool enabled );

/** Returns whether users are permitted to overwrite existing names.
* @note added in QGIS 2.12
* @see setOverwriteEnabled()
*/
bool overwriteEnabled() const { return mOverwriteEnabled; }

/** Sets the string used for warning users if a conflicting name exists.
* @param string warning string. If empty a default warning string will be used.
* @note added in QGIS 2.12
* @see conflictingNameWarning()
*/
void setConflictingNameWarning( const QString& string );

/** Returns the string used for warning users if a conflicting name exists.
* @note added in QGIS 2.12
* @see setConflictingNameWarning()
*/
QString conflictingNameWarning() const { return mConflictingNameWarning; }

/** Name entered by user.
* @return new name
*/
Expand All @@ -67,11 +109,15 @@ class GUI_EXPORT QgsNewNameDialog : public QgsDialog
QStringList mExiting;
QStringList mExtensions;
Qt::CaseSensitivity mCaseSensitivity;
QLabel* mHintLabel;
QLineEdit *mLineEdit;
QLabel *mNamesLabel; // list of names with extensions
QLabel *mErrorLabel;
QString mOkString;
QRegExp mRegexp;
bool mOverwriteEnabled;
QString mConflictingNameWarning;

QString highlightText( const QString& text );
static QStringList fullNames( const QString& name, const QStringList& extensions );
// get list of existing names
Expand Down

0 comments on commit ca04bb6

Please sign in to comment.