Skip to content
Permalink
Browse files

Allow more customisation of QgsNewNameDialog

  • Loading branch information
nyalldawson committed Aug 20, 2015
1 parent 0df7102 commit ca04bb6409b5b829ed06459a850052f6e3a27719
Showing with 179 additions and 4 deletions.
  1. +1 −0 python/gui/gui.sip
  2. +95 −0 python/gui/qgsnewnamedialog.sip
  3. +37 −4 src/gui/qgsnewnamedialog.cpp
  4. +46 −0 src/gui/qgsnewnamedialog.h
@@ -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
@@ -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 );
};

@@ -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 );
@@ -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 );

@@ -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>";
@@ -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;
}
}
@@ -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
*/
@@ -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

0 comments on commit ca04bb6

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