QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsnewnamedialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsnewnamedialog.cpp
3 -------------------
4 begin : May, 2015
5 copyright : (C) 2015 Radim Blazek
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include <QLabel>
18#include <QLineEdit>
19#include <QPushButton>
20#include <QRegularExpressionValidator>
21#include <QSizePolicy>
22
23#include "qgslogger.h"
24#include "qgsnewnamedialog.h"
25#include "moc_qgsnewnamedialog.cpp"
26
27QgsNewNameDialog::QgsNewNameDialog( const QString &source, const QString &initial, const QStringList &extensions, const QStringList &existing, Qt::CaseSensitivity cs, QWidget *parent, Qt::WindowFlags flags )
28 : QgsDialog( parent, flags, QDialogButtonBox::Ok | QDialogButtonBox::Cancel )
29 , mExiting( existing )
30 , mExtensions( extensions )
31 , mCaseSensitivity( cs )
32{
33 setWindowTitle( tr( "New Name" ) );
34 QgsDialog::layout()->setSizeConstraint( QLayout::SetMinimumSize );
35 layout()->setSizeConstraint( QLayout::SetMinimumSize );
36 layout()->setSpacing( 6 );
37 mOkString = buttonBox()->button( QDialogButtonBox::Ok )->text();
38 QString hintString;
39 const QString nameDesc = mExtensions.isEmpty() ? tr( "name" ) : tr( "base name" );
40 if ( source.isEmpty() )
41 {
42 hintString = tr( "Enter new %1" ).arg( nameDesc );
43 }
44 else
45 {
46 hintString = tr( "Enter new %1 for %2" ).arg( nameDesc, source );
47 }
48 mHintLabel = new QLabel( hintString, this );
49 layout()->addWidget( mHintLabel );
50
51 mLineEdit = new QLineEdit( initial, this );
52 mLineEdit->setMinimumWidth( mLineEdit->fontMetrics().horizontalAdvance( 'x' ) * 44 );
53
54 connect( mLineEdit, &QLineEdit::textChanged, this, &QgsNewNameDialog::nameChanged );
55 connect( mLineEdit, &QLineEdit::textChanged, this, &QgsNewNameDialog::newNameChanged );
56 layout()->addWidget( mLineEdit );
57
58 mNamesLabel = new QLabel( QStringLiteral( " " ), this );
59 mNamesLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
60 if ( !mExtensions.isEmpty() )
61 {
62 mNamesLabel->setWordWrap( true );
63 layout()->addWidget( mNamesLabel );
64 }
65
66 mErrorLabel = new QLabel( QStringLiteral( " " ), this );
67 mErrorLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
68 mErrorLabel->setWordWrap( true );
69 layout()->addWidget( mErrorLabel );
70
71 mLineEdit->setFocus();
72 mLineEdit->selectAll();
73
75}
76
77void QgsNewNameDialog::setHintString( const QString &hintString )
78{
79 mHintLabel->setText( hintString );
80}
81
83{
84 return mHintLabel->text();
85}
86
88{
89 mOverwriteEnabled = enabled;
90 nameChanged(); //update UI
91}
92
94{
95 mAllowEmptyName = allowed;
96 nameChanged(); //update UI
97}
98
100{
102 nameChanged(); //update UI
103}
104
105void QgsNewNameDialog::setRegularExpression( const QString &expression )
106{
107 if ( !expression.isEmpty() )
108 {
109 mRegularExpression = QRegularExpression( QRegularExpression::anchoredPattern( expression ) );
110 QRegularExpressionValidator *validator = new QRegularExpressionValidator( mRegularExpression, this );
111 mLineEdit->setValidator( validator );
112 }
113 else
114 {
115 mRegularExpression = QRegularExpression();
116 mLineEdit->setValidator( nullptr );
117 }
118 nameChanged();
119}
120
121QString QgsNewNameDialog::highlightText( const QString &text )
122{
123 return "<b>" + text + "</b>";
124}
125
127{
128 QString namesString = tr( "Full names" ) + ": ";
129 if ( !mExtensions.isEmpty() )
130 {
131 mNamesLabel->setText( namesString );
132 }
133 mErrorLabel->setText( QStringLiteral( " " ) ); // space to keep vertical space
134 QPushButton *okButton = buttonBox()->button( QDialogButtonBox::Ok );
135 okButton->setText( mOkString );
136 okButton->setEnabled( true );
137
138 const QString newName = name();
139
140 if ( newName.length() == 0 || ( !mRegularExpression.pattern().isEmpty() && !mRegularExpression.match( newName ).hasMatch() ) )
141 {
142 //mErrorLabel->setText( highlightText( tr( "Enter new name" ) );
143 okButton->setEnabled( mAllowEmptyName );
144 return;
145 }
146
147 const QStringList newNames = fullNames( newName, mExtensions );
148 if ( !mExtensions.isEmpty() )
149 {
150 namesString += ' ' + newNames.join( QLatin1String( ", " ) );
151 mNamesLabel->setText( namesString );
152 }
153
154 const QStringList conflicts = matching( newNames, mExiting, mCaseSensitivity );
155
156 if ( !conflicts.isEmpty() )
157 {
158 const QString warning = !mConflictingNameWarning.isEmpty() ? mConflictingNameWarning
159 : tr( "%n Name(s) %1 exists", nullptr, conflicts.size() ).arg( conflicts.join( QLatin1String( ", " ) ) );
160 mErrorLabel->setText( highlightText( warning ) );
161 if ( mOverwriteEnabled )
162 {
163 okButton->setText( tr( "Overwrite" ) );
164 }
165 else
166 {
167 okButton->setEnabled( false );
168 }
169 return;
170 }
171}
172
174{
175 return mLineEdit->text().trimmed();
176}
177
178QStringList QgsNewNameDialog::fullNames( const QString &name, const QStringList &extensions )
179{
180 QStringList list;
181 const auto constExtensions = extensions;
182 for ( const QString &ext : constExtensions )
183 {
184 list << name + ext;
185 }
186 if ( list.isEmpty() )
187 {
188 list << name;
189 }
190 return list;
191}
192
193QStringList QgsNewNameDialog::matching( const QStringList &newNames, const QStringList &existingNames, Qt::CaseSensitivity cs )
194{
195 QStringList list;
196
197 const auto constNewNames = newNames;
198 for ( const QString &newName : constNewNames )
199 {
200 const auto constExistingNames = existingNames;
201 for ( const QString &existingName : constExistingNames )
202 {
203 if ( existingName.compare( newName, cs ) == 0 )
204 {
205 list << existingName;
206 }
207 }
208 }
209 return list;
210}
211
212bool QgsNewNameDialog::exists( const QString &name, const QStringList &extensions, const QStringList &existing, Qt::CaseSensitivity cs )
213{
214 const QStringList newNames = fullNames( name, extensions );
215 const QStringList conflicts = matching( newNames, existing, cs );
216 return !conflicts.isEmpty();
217}
A generic dialog with layout and button box.
Definition qgsdialog.h:34
QDialogButtonBox * buttonBox()
Returns the button box.
Definition qgsdialog.h:45
QVBoxLayout * layout()
Returns the central layout. Widgets added to it must have this dialog as parent.
Definition qgsdialog.h:43
void setConflictingNameWarning(const QString &string)
Sets the string used for warning users if a conflicting name exists.
static QStringList fullNames(const QString &name, const QStringList &extensions)
static bool exists(const QString &name, const QStringList &extensions, const QStringList &existing, Qt::CaseSensitivity cs=Qt::CaseSensitive)
Test if name or name with at least one extension exists.
QgsNewNameDialog(const QString &source=QString(), const QString &initial=QString(), const QStringList &extensions=QStringList(), const QStringList &existing=QStringList(), Qt::CaseSensitivity cs=Qt::CaseSensitive, QWidget *parent=nullptr, Qt::WindowFlags flags=QgsGuiUtils::ModalDialogFlags)
New dialog constructor.
Qt::CaseSensitivity mCaseSensitivity
void newNameChanged()
Emitted when the name is changed in the dialog.
QRegularExpression mRegularExpression
static QStringList matching(const QStringList &newNames, const QStringList &existingNames, Qt::CaseSensitivity cs=Qt::CaseSensitive)
QStringList mExtensions
void setOverwriteEnabled(bool enabled)
Sets whether users are permitted to overwrite existing names.
QLabel * mNamesLabel
List of names with extensions.
void setRegularExpression(const QString &expression)
Sets a regular expression to use for validating user-entered names in the dialog.
QString highlightText(const QString &text)
QString hintString() const
Returns the hint string for the dialog (the text shown above the name input box).
QString name() const
Name entered by user.
QString mConflictingNameWarning
void setAllowEmptyName(bool allowed)
Sets whether users are permitted to leave the widget empty.
void setHintString(const QString &hintString)
Sets the hint string for the dialog (the text shown above the name input box).