QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 : [email protected]
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 <QRegExpValidator>
21 #include <QSizePolicy>
22 
23 #include "qgslogger.h"
24 #include "qgsnewnamedialog.h"
25 
26 QgsNewNameDialog::QgsNewNameDialog( const QString &source, const QString &initial,
27  const QStringList &extensions, const QStringList &existing,
28  const QRegExp &regexp, Qt::CaseSensitivity cs,
29  QWidget *parent, Qt::WindowFlags flags )
30  : QgsDialog( parent, flags, QDialogButtonBox::Ok | QDialogButtonBox::Cancel )
31  , mExiting( existing )
32  , mExtensions( extensions )
33  , mCaseSensitivity( cs )
34  , mRegexp( regexp )
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  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  if ( !regexp.isEmpty() )
56  {
57  QRegExpValidator *validator = new QRegExpValidator( regexp, this );
58  mLineEdit->setValidator( validator );
59  }
60 
61  mLineEdit->setMinimumWidth( mLineEdit->fontMetrics().horizontalAdvance( 'x' ) * 44 );
62 
63  connect( mLineEdit, &QLineEdit::textChanged, this, &QgsNewNameDialog::nameChanged );
64  connect( mLineEdit, &QLineEdit::textChanged, this, &QgsNewNameDialog::newNameChanged );
65  layout()->addWidget( mLineEdit );
66 
67  mNamesLabel = new QLabel( QStringLiteral( " " ), this );
68  mNamesLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
69  if ( !mExtensions.isEmpty() )
70  {
71  mNamesLabel->setWordWrap( true );
72  layout()->addWidget( mNamesLabel );
73  }
74 
75  mErrorLabel = new QLabel( QStringLiteral( " " ), this );
76  mErrorLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
77  mErrorLabel->setWordWrap( true );
78  layout()->addWidget( mErrorLabel );
79 
80  mLineEdit->setFocus();
81  mLineEdit->selectAll();
82 
83  nameChanged();
84 }
85 
86 void QgsNewNameDialog::setHintString( const QString &hintString )
87 {
88  mHintLabel->setText( hintString );
89 }
90 
92 {
93  return mHintLabel->text();
94 }
95 
97 {
98  mOverwriteEnabled = enabled;
99  nameChanged(); //update UI
100 }
101 
103 {
104  mAllowEmptyName = allowed;
105  nameChanged(); //update UI
106 }
107 
108 void QgsNewNameDialog::setConflictingNameWarning( const QString &string )
109 {
110  mConflictingNameWarning = string;
111  nameChanged(); //update UI
112 }
113 
114 QString QgsNewNameDialog::highlightText( const QString &text )
115 {
116  return "<b>" + text + "</b>";
117 }
118 
120 {
121 
122  QString namesString = tr( "Full names" ) + ": ";
123  if ( !mExtensions.isEmpty() )
124  {
125  mNamesLabel->setText( namesString );
126  }
127  mErrorLabel->setText( QStringLiteral( " " ) ); // space to keep vertical space
128  QPushButton *okButton = buttonBox()->button( QDialogButtonBox::Ok );
129  okButton->setText( mOkString );
130  okButton->setEnabled( true );
131 
132  QString newName = name();
133 
134  if ( newName.length() == 0 || ( !mRegexp.isEmpty() && !mRegexp.exactMatch( newName ) ) )
135  {
136  //mErrorLabel->setText( highlightText( tr( "Enter new name" ) );
137  okButton->setEnabled( mAllowEmptyName );
138  return;
139  }
140 
141  QStringList newNames = fullNames( newName, mExtensions );
142  if ( !mExtensions.isEmpty() )
143  {
144  namesString += ' ' + newNames.join( QLatin1String( ", " ) );
145  mNamesLabel->setText( namesString );
146  }
147 
148  QStringList conflicts = matching( newNames, mExiting, mCaseSensitivity );
149 
150  if ( !conflicts.isEmpty() )
151  {
152  QString warning = !mConflictingNameWarning.isEmpty() ? mConflictingNameWarning
153  : tr( "%n Name(s) %1 exists", nullptr, conflicts.size() ).arg( conflicts.join( QLatin1String( ", " ) ) );
154  mErrorLabel->setText( highlightText( warning ) );
155  if ( mOverwriteEnabled )
156  {
157  okButton->setText( tr( "Overwrite" ) );
158  }
159  else
160  {
161  okButton->setEnabled( false );
162  }
163  return;
164  }
165 }
166 
167 QString QgsNewNameDialog::name() const
168 {
169  return mLineEdit->text().trimmed();
170 }
171 
172 QStringList QgsNewNameDialog::fullNames( const QString &name, const QStringList &extensions )
173 {
174  QStringList list;
175  const auto constExtensions = extensions;
176  for ( const QString &ext : constExtensions )
177  {
178  list << name + ext;
179 
180  }
181  if ( list.isEmpty() )
182  {
183  list << name;
184  }
185  return list;
186 }
187 
188 QStringList QgsNewNameDialog::matching( const QStringList &newNames, const QStringList &existingNames,
189  Qt::CaseSensitivity cs )
190 {
191  QStringList list;
192 
193  const auto constNewNames = newNames;
194  for ( const QString &newName : constNewNames )
195  {
196  const auto constExistingNames = existingNames;
197  for ( const QString &existingName : constExistingNames )
198  {
199  if ( existingName.compare( newName, cs ) == 0 )
200  {
201  list << existingName;
202  }
203  }
204  }
205  return list;
206 }
207 
208 bool QgsNewNameDialog::exists( const QString &name, const QStringList &extensions,
209  const QStringList &existing, Qt::CaseSensitivity cs )
210 {
211  QStringList newNames = fullNames( name, extensions );
212  QStringList conflicts = matching( newNames, existing, cs );
213  return !conflicts.isEmpty();
214 }
A generic dialog with layout and button box.
Definition: qgsdialog.h:34
QVBoxLayout * layout()
Returns the central layout. Widgets added to it must have this dialog as parent.
Definition: qgsdialog.h:46
QDialogButtonBox * buttonBox()
Returns the button box.
Definition: qgsdialog.h:48
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(), const QRegExp &regexp=QRegExp(), 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.
QLineEdit * mLineEdit
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.
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.
QStringList mExiting
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).