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