QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
6 email : radim.blazek@gmail.com
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
28 const QString &initial,
29 const QStringList &extensions,
30 const QStringList &existing,
31 Qt::CaseSensitivity cs,
32 QWidget *parent,
33 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( QStringLiteral( " " ), 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( QStringLiteral( " " ), 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
83void QgsNewNameDialog::setHintString( const QString &hintString )
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
127QString QgsNewNameDialog::highlightText( const QString &text )
128{
129 return "<b>" + text + "</b>";
130}
131
133{
134 QString namesString = tr( "Full names" ) + ": ";
135 if ( !mExtensions.isEmpty() )
136 {
137 mNamesLabel->setText( namesString );
138 }
139 mErrorLabel->setText( QStringLiteral( " " ) ); // space to keep vertical space
140 QPushButton *okButton = buttonBox()->button( QDialogButtonBox::Ok );
141 okButton->setText( mOkString );
142 okButton->setEnabled( true );
143
144 const QString newName = name();
145
146 if ( newName.length() == 0 || ( !mRegularExpression.pattern().isEmpty() && !mRegularExpression.match( newName ).hasMatch() ) )
147 {
148 //mErrorLabel->setText( highlightText( tr( "Enter new name" ) );
149 okButton->setEnabled( mAllowEmptyName );
150 return;
151 }
152
153 const QStringList newNames = fullNames( newName, mExtensions );
154 if ( !mExtensions.isEmpty() )
155 {
156 namesString += ' ' + newNames.join( QLatin1String( ", " ) );
157 mNamesLabel->setText( namesString );
158 }
159
160 const QStringList conflicts = matching( newNames, mExiting, mCaseSensitivity );
161
162 if ( !conflicts.isEmpty() )
163 {
164 const QString warning = !mConflictingNameWarning.isEmpty() ? mConflictingNameWarning
165 : tr( "%n Name(s) %1 exists", nullptr, conflicts.size() ).arg( conflicts.join( QLatin1String( ", " ) ) );
166 mErrorLabel->setText( highlightText( warning ) );
167 if ( mOverwriteEnabled )
168 {
169 okButton->setText( tr( "Overwrite" ) );
170 }
171 else
172 {
173 okButton->setEnabled( false );
174 }
175 return;
176 }
177}
178
180{
181 return mLineEdit->text().trimmed();
182}
183
184QStringList QgsNewNameDialog::fullNames( const QString &name, const QStringList &extensions )
185{
186 QStringList list;
187 const auto constExtensions = extensions;
188 for ( const QString &ext : constExtensions )
189 {
190 list << name + ext;
191
192 }
193 if ( list.isEmpty() )
194 {
195 list << name;
196 }
197 return list;
198}
199
200QStringList QgsNewNameDialog::matching( const QStringList &newNames, const QStringList &existingNames,
201 Qt::CaseSensitivity cs )
202{
203 QStringList list;
204
205 const auto constNewNames = newNames;
206 for ( const QString &newName : constNewNames )
207 {
208 const auto constExistingNames = existingNames;
209 for ( const QString &existingName : constExistingNames )
210 {
211 if ( existingName.compare( newName, cs ) == 0 )
212 {
213 list << existingName;
214 }
215 }
216 }
217 return list;
218}
219
220bool QgsNewNameDialog::exists( const QString &name, const QStringList &extensions,
221 const QStringList &existing, Qt::CaseSensitivity cs )
222{
223 const QStringList newNames = fullNames( name, extensions );
224 const QStringList conflicts = matching( newNames, existing, cs );
225 return !conflicts.isEmpty();
226}
A generic dialog with layout and button box.
Definition qgsdialog.h:34
QDialogButtonBox * buttonBox()
Returns the button box.
Definition qgsdialog.h:48
QVBoxLayout * layout()
Returns the central layout. Widgets added to it must have this dialog as parent.
Definition qgsdialog.h:46
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).