QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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 "qgsnewnamedialog.h"
18
19#include "qgslogger.h"
20
21#include <QCompleter>
22#include <QLabel>
23#include <QLineEdit>
24#include <QPushButton>
25#include <QRegularExpressionValidator>
26#include <QSizePolicy>
27
28#include "moc_qgsnewnamedialog.cpp"
29
30QgsNewNameDialog::QgsNewNameDialog( const QString &source, const QString &initial, const QStringList &extensions, const QStringList &existing, Qt::CaseSensitivity cs, QWidget *parent, Qt::WindowFlags flags )
31 : QgsDialog( parent, flags, QDialogButtonBox::Ok | QDialogButtonBox::Cancel )
32 , mExiting( existing )
33 , mExtensions( extensions )
34 , mCaseSensitivity( cs )
35{
36 setWindowTitle( tr( "New Name" ) );
37 QgsDialog::layout()->setSizeConstraint( QLayout::SetMinimumSize );
38 layout()->setSizeConstraint( QLayout::SetMinimumSize );
39 layout()->setSpacing( 6 );
40 mOkString = buttonBox()->button( QDialogButtonBox::Ok )->text();
41 QString hintString;
42 const QString nameDesc = mExtensions.isEmpty() ? tr( "name" ) : tr( "base name" );
43 if ( source.isEmpty() )
44 {
45 hintString = tr( "Enter new %1" ).arg( nameDesc );
46 }
47 else
48 {
49 hintString = tr( "Enter new %1 for %2" ).arg( nameDesc, source );
50 }
51 mHintLabel = new QLabel( hintString, this );
52 layout()->addWidget( mHintLabel );
53
54 mLineEdit = new QLineEdit( initial, this );
55 mLineEdit->setMinimumWidth( mLineEdit->fontMetrics().horizontalAdvance( 'x' ) * 44 );
56
57 connect( mLineEdit, &QLineEdit::textChanged, this, &QgsNewNameDialog::nameChanged );
58 connect( mLineEdit, &QLineEdit::textChanged, this, &QgsNewNameDialog::newNameChanged );
59 layout()->addWidget( mLineEdit );
60
61 mNamesLabel = new QLabel( QStringLiteral( " " ), this );
62 mNamesLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
63 if ( !mExtensions.isEmpty() )
64 {
65 mNamesLabel->setWordWrap( true );
66 layout()->addWidget( mNamesLabel );
67 }
68
69 mErrorLabel = new QLabel( QStringLiteral( " " ), this );
70 mErrorLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
71 mErrorLabel->setWordWrap( true );
72 layout()->addWidget( mErrorLabel );
73
74 mLineEdit->setFocus();
75 mLineEdit->selectAll();
76
78}
79
81{
82 mHintLabel->setText( hintString );
83}
84
86{
87 return mHintLabel->text();
88}
89
91{
92 mOverwriteEnabled = enabled;
93 nameChanged(); //update UI
94}
95
97{
98 mAllowEmptyName = allowed;
99 nameChanged(); //update UI
100}
101
103{
105 nameChanged(); //update UI
106}
107
108void QgsNewNameDialog::setRegularExpression( const QString &expression )
109{
110 if ( !expression.isEmpty() )
111 {
112 mRegularExpression = QRegularExpression( QRegularExpression::anchoredPattern( expression ) );
113 QRegularExpressionValidator *validator = new QRegularExpressionValidator( mRegularExpression, this );
114 mLineEdit->setValidator( validator );
115 }
116 else
117 {
118 mRegularExpression = QRegularExpression();
119 mLineEdit->setValidator( nullptr );
120 }
121 nameChanged();
122}
123
125{
126 if ( !show )
127 {
128 mLineEdit->setCompleter( nullptr );
129 return;
130 }
131
132 QCompleter *completer = new QCompleter( mExiting, this );
133 completer->setCaseSensitivity( Qt::CaseInsensitive );
134 completer->setCompletionMode( QCompleter::UnfilteredPopupCompletion );
135 mLineEdit->setCompleter( completer );
136}
137
138QString QgsNewNameDialog::highlightText( const QString &text )
139{
140 return "<b>" + text + "</b>";
141}
142
144{
145 QString namesString = tr( "Full names" ) + ": ";
146 if ( !mExtensions.isEmpty() )
147 {
148 mNamesLabel->setText( namesString );
149 }
150 mErrorLabel->setText( QStringLiteral( " " ) ); // space to keep vertical space
151 QPushButton *okButton = buttonBox()->button( QDialogButtonBox::Ok );
152 okButton->setText( mOkString );
153 okButton->setEnabled( true );
154
155 const QString newName = name();
156
157 if ( newName.length() == 0 || ( !mRegularExpression.pattern().isEmpty() && !mRegularExpression.match( newName ).hasMatch() ) )
158 {
159 //mErrorLabel->setText( highlightText( tr( "Enter new name" ) );
160 okButton->setEnabled( mAllowEmptyName );
161 return;
162 }
163
164 const QStringList newNames = fullNames( newName, mExtensions );
165 if ( !mExtensions.isEmpty() )
166 {
167 namesString += ' ' + newNames.join( QLatin1String( ", " ) );
168 mNamesLabel->setText( namesString );
169 }
170
171 const QStringList conflicts = matching( newNames, mExiting, mCaseSensitivity );
172
173 if ( !conflicts.isEmpty() )
174 {
175 const QString warning = !mConflictingNameWarning.isEmpty() ? mConflictingNameWarning
176 : tr( "%n Name(s) %1 exists", nullptr, conflicts.size() ).arg( conflicts.join( QLatin1String( ", " ) ) );
177 mErrorLabel->setText( highlightText( warning ) );
178 if ( mOverwriteEnabled )
179 {
180 okButton->setText( tr( "Overwrite" ) );
181 }
182 else
183 {
184 okButton->setEnabled( false );
185 }
186 return;
187 }
188}
189
191{
192 return mLineEdit->text().trimmed();
193}
194
195QStringList QgsNewNameDialog::fullNames( const QString &name, const QStringList &extensions )
196{
197 QStringList list;
198 const auto constExtensions = extensions;
199 for ( const QString &ext : constExtensions )
200 {
201 list << name + ext;
202 }
203 if ( list.isEmpty() )
204 {
205 list << name;
206 }
207 return list;
208}
209
210QStringList QgsNewNameDialog::matching( const QStringList &newNames, const QStringList &existingNames, Qt::CaseSensitivity cs )
211{
212 QStringList list;
213
214 const auto constNewNames = newNames;
215 for ( const QString &newName : constNewNames )
216 {
217 const auto constExistingNames = existingNames;
218 for ( const QString &existingName : constExistingNames )
219 {
220 if ( existingName.compare( newName, cs ) == 0 )
221 {
222 list << existingName;
223 }
224 }
225 }
226 return list;
227}
228
229bool QgsNewNameDialog::exists( const QString &name, const QStringList &extensions, const QStringList &existing, Qt::CaseSensitivity cs )
230{
231 const QStringList newNames = fullNames( name, extensions );
232 const QStringList conflicts = matching( newNames, existing, cs );
233 return !conflicts.isEmpty();
234}
QDialogButtonBox * buttonBox()
Returns the button box.
Definition qgsdialog.h:46
QgsDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags, QDialogButtonBox::StandardButtons buttons=QDialogButtonBox::Close, Qt::Orientation orientation=Qt::Horizontal)
Constructor for QgsDialog.
Definition qgsdialog.cpp:22
QVBoxLayout * layout()
Returns the central layout. Widgets added to it must have this dialog as parent.
Definition qgsdialog.h:44
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
void setShowExistingNamesCompleter(bool show)
Sets whether a completer for existing names should be used in the line edit.
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.
static 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).