Skip to content

Commit ca04bb6

Browse files
committed
Allow more customisation of QgsNewNameDialog
1 parent 0df7102 commit ca04bb6

File tree

4 files changed

+179
-4
lines changed

4 files changed

+179
-4
lines changed

python/gui/gui.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
%Include qgsmessageviewer.sip
9595
%Include qgsnewhttpconnection.sip
9696
%Include qgsnewmemorylayerdialog.sip
97+
%Include qgsnewnamedialog.sip
9798
%Include qgsnewvectorlayerdialog.sip
9899
%Include qgsnumericsortlistviewitem.sip
99100
%Include qgsoptionsdialogbase.sip

python/gui/qgsnewnamedialog.sip

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/** \ingroup gui
2+
* New name, for example new layer name dialog. If existing names are provided,
3+
* the dialog warns users if an entered name already exists.
4+
* @note added in 2.10
5+
*/
6+
class QgsNewNameDialog : QgsDialog
7+
{
8+
%TypeHeaderCode
9+
#include <qgsnewnamedialog.h>
10+
%End
11+
12+
public:
13+
/** New dialog constructor.
14+
* @param source original data source name, e.g. original layer name of the layer to be copied
15+
* @param initial initial name
16+
* @param extensions base name extensions, e.g. raster base name band extensions or vector layer type extensions
17+
* @param existing existing names
18+
* @param regexp regular expression to be used as validator, for example db tables should have "[A-Za-z_][A-Za-z0-9_]+"
19+
* @param cs case sensitivity for new name to existing names comparison
20+
* @param parent
21+
* @param flags
22+
*/
23+
QgsNewNameDialog( const QString& source = QString::null, const QString& initial = QString::null,
24+
const QStringList& extensions = QStringList(), const QStringList& existing = QStringList(),
25+
const QRegExp& regexp = QRegExp(), Qt::CaseSensitivity cs = Qt::CaseSensitive,
26+
QWidget *parent /TransferThis/ = 0, Qt::WindowFlags flags = QgisGui::ModalDialogFlags );
27+
28+
/** Sets the hint string for the dialog (the text shown above the name
29+
* input box).
30+
* @param hintString hint text
31+
* @see hintString()
32+
* @note added in QGIS 2.12
33+
*/
34+
void setHintString( const QString& hintString );
35+
36+
/** Returns the hint string for the dialog (the text shown above the name
37+
* input box).
38+
* @see setHintString()
39+
* @note added in QGIS 2.12
40+
*/
41+
QString hintString() const;
42+
43+
/** Sets whether users are permitted to overwrite existing names. If true, then
44+
* the dialog will reflect that the new name will overwrite an existing name. If false,
45+
* then the dialog will not accept names which already exist.
46+
* @note added in QGIS 2.12
47+
* @see overwriteEnabled()
48+
*/
49+
void setOverwriteEnabled( bool enabled );
50+
51+
/** Returns whether users are permitted to overwrite existing names.
52+
* @note added in QGIS 2.12
53+
* @see setOverwriteEnabled()
54+
*/
55+
bool overwriteEnabled() const;
56+
57+
/** Sets the string used for warning users if a conflicting name exists.
58+
* @param string warning string. If empty a default warning string will be used.
59+
* @note added in QGIS 2.12
60+
* @see conflictingNameWarning()
61+
*/
62+
void setConflictingNameWarning( const QString& string );
63+
64+
/** Returns the string used for warning users if a conflicting name exists.
65+
* @note added in QGIS 2.12
66+
* @see setConflictingNameWarning()
67+
*/
68+
QString conflictingNameWarning() const;
69+
70+
/** Name entered by user.
71+
* @return new name
72+
*/
73+
QString name() const;
74+
75+
/** Test if name or name with at least one extension exists.
76+
* @param name name or base name
77+
* @param extensions base name extensions
78+
* @param existing existing names
79+
* @param cs case sensitivity for new name to existing names comparison
80+
* @return true if name exists
81+
*/
82+
static bool exists( const QString& name, const QStringList& extensions,
83+
const QStringList& existing, Qt::CaseSensitivity cs = Qt::CaseSensitive );
84+
public slots:
85+
void nameChanged();
86+
87+
protected:
88+
89+
QString highlightText( const QString& text );
90+
static QStringList fullNames( const QString& name, const QStringList& extensions );
91+
// get list of existing names
92+
static QStringList matching( const QStringList& newNames, const QStringList& existingNames,
93+
Qt::CaseSensitivity cs = Qt::CaseSensitive );
94+
};
95+

src/gui/qgsnewnamedialog.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ QgsNewNameDialog::QgsNewNameDialog( const QString& source, const QString& initia
3333
, mCaseSensitivity( cs )
3434
, mNamesLabel( 0 )
3535
, mRegexp( regexp )
36+
, mOverwriteEnabled( true )
3637
{
3738
setWindowTitle( tr( "New name" ) );
3839
QDialog::layout()->setSizeConstraint( QLayout::SetMinimumSize );
@@ -49,15 +50,16 @@ QgsNewNameDialog::QgsNewNameDialog( const QString& source, const QString& initia
4950
{
5051
hintString = tr( "Enter new %1 for %2" ).arg( nameDesc ).arg( source );
5152
}
52-
QLabel* hintLabel = new QLabel( hintString, this );
53-
layout()->addWidget( hintLabel );
53+
mHintLabel = new QLabel( hintString, this );
54+
layout()->addWidget( mHintLabel );
5455

5556
mLineEdit = new QLineEdit( initial, this );
5657
if ( !regexp.isEmpty() )
5758
{
5859
QRegExpValidator *validator = new QRegExpValidator( regexp, this );
5960
mLineEdit->setValidator( validator );
6061
}
62+
mLineEdit->setMinimumWidth( mLineEdit->fontMetrics().width( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ) );
6163
connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( nameChanged() ) );
6264
layout()->addWidget( mLineEdit );
6365

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

82+
void QgsNewNameDialog::setHintString( const QString &hintString )
83+
{
84+
mHintLabel->setText( hintString );
85+
}
86+
87+
QString QgsNewNameDialog::hintString() const
88+
{
89+
return mHintLabel->text();
90+
}
91+
92+
void QgsNewNameDialog::setOverwriteEnabled( bool enabled )
93+
{
94+
mOverwriteEnabled = enabled;
95+
nameChanged(); //update UI
96+
}
97+
98+
void QgsNewNameDialog::setConflictingNameWarning( const QString& string )
99+
{
100+
mConflictingNameWarning = string;
101+
nameChanged(); //update UI
102+
}
103+
80104
QString QgsNewNameDialog::highlightText( const QString& text )
81105
{
82106
return "<b>" + text + "</b>";
@@ -116,8 +140,17 @@ void QgsNewNameDialog::nameChanged()
116140

117141
if ( !conflicts.isEmpty() )
118142
{
119-
mErrorLabel->setText( highlightText( tr( "%n Name(s) %1 exists", 0, conflicts.size() ).arg( conflicts.join( ", " ) ) ) );
120-
okButton->setText( tr( "Overwrite" ) );
143+
QString warning = !mConflictingNameWarning.isEmpty() ? mConflictingNameWarning
144+
: tr( "%n Name(s) %1 exists", 0, conflicts.size() ).arg( conflicts.join( ", " ) );
145+
mErrorLabel->setText( highlightText( warning ) );
146+
if ( mOverwriteEnabled )
147+
{
148+
okButton->setText( tr( "Overwrite" ) );
149+
}
150+
else
151+
{
152+
okButton->setEnabled( false );
153+
}
121154
return;
122155
}
123156
}

src/gui/qgsnewnamedialog.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,48 @@ class GUI_EXPORT QgsNewNameDialog : public QgsDialog
4646
const QRegExp& regexp = QRegExp(), Qt::CaseSensitivity cs = Qt::CaseSensitive,
4747
QWidget *parent = 0, Qt::WindowFlags flags = QgisGui::ModalDialogFlags );
4848

49+
/** Sets the hint string for the dialog (the text shown above the name
50+
* input box).
51+
* @param hintString hint text
52+
* @see hintString()
53+
* @note added in QGIS 2.12
54+
*/
55+
void setHintString( const QString& hintString );
56+
57+
/** Returns the hint string for the dialog (the text shown above the name
58+
* input box).
59+
* @see setHintString()
60+
* @note added in QGIS 2.12
61+
*/
62+
QString hintString() const;
63+
64+
/** Sets whether users are permitted to overwrite existing names. If true, then
65+
* the dialog will reflect that the new name will overwrite an existing name. If false,
66+
* then the dialog will not accept names which already exist.
67+
* @note added in QGIS 2.12
68+
* @see overwriteEnabled()
69+
*/
70+
void setOverwriteEnabled( bool enabled );
71+
72+
/** Returns whether users are permitted to overwrite existing names.
73+
* @note added in QGIS 2.12
74+
* @see setOverwriteEnabled()
75+
*/
76+
bool overwriteEnabled() const { return mOverwriteEnabled; }
77+
78+
/** Sets the string used for warning users if a conflicting name exists.
79+
* @param string warning string. If empty a default warning string will be used.
80+
* @note added in QGIS 2.12
81+
* @see conflictingNameWarning()
82+
*/
83+
void setConflictingNameWarning( const QString& string );
84+
85+
/** Returns the string used for warning users if a conflicting name exists.
86+
* @note added in QGIS 2.12
87+
* @see setConflictingNameWarning()
88+
*/
89+
QString conflictingNameWarning() const { return mConflictingNameWarning; }
90+
4991
/** Name entered by user.
5092
* @return new name
5193
*/
@@ -67,11 +109,15 @@ class GUI_EXPORT QgsNewNameDialog : public QgsDialog
67109
QStringList mExiting;
68110
QStringList mExtensions;
69111
Qt::CaseSensitivity mCaseSensitivity;
112+
QLabel* mHintLabel;
70113
QLineEdit *mLineEdit;
71114
QLabel *mNamesLabel; // list of names with extensions
72115
QLabel *mErrorLabel;
73116
QString mOkString;
74117
QRegExp mRegexp;
118+
bool mOverwriteEnabled;
119+
QString mConflictingNameWarning;
120+
75121
QString highlightText( const QString& text );
76122
static QStringList fullNames( const QString& name, const QStringList& extensions );
77123
// get list of existing names

0 commit comments

Comments
 (0)