QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
62 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
63  mLineEdit->setMinimumWidth( mLineEdit->fontMetrics().width( QStringLiteral( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ) ) );
64 #else
65  mLineEdit->setMinimumWidth( mLineEdit->fontMetrics().horizontalAdvance( 'x' ) * 44 );
66 #endif
67  connect( mLineEdit, &QLineEdit::textChanged, this, &QgsNewNameDialog::nameChanged );
68  connect( mLineEdit, &QLineEdit::textChanged, this, &QgsNewNameDialog::newNameChanged );
69  layout()->addWidget( mLineEdit );
70 
71  mNamesLabel = new QLabel( QStringLiteral( " " ), this );
72  mNamesLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
73  if ( !mExtensions.isEmpty() )
74  {
75  mNamesLabel->setWordWrap( true );
76  layout()->addWidget( mNamesLabel );
77  }
78 
79  mErrorLabel = new QLabel( QStringLiteral( " " ), this );
80  mErrorLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
81  mErrorLabel->setWordWrap( true );
82  layout()->addWidget( mErrorLabel );
83 
84  mLineEdit->setFocus();
85  mLineEdit->selectAll();
86 
87  nameChanged();
88 }
89 
90 void QgsNewNameDialog::setHintString( const QString &hintString )
91 {
92  mHintLabel->setText( hintString );
93 }
94 
96 {
97  return mHintLabel->text();
98 }
99 
101 {
102  mOverwriteEnabled = enabled;
103  nameChanged(); //update UI
104 }
105 
107 {
108  mAllowEmptyName = allowed;
109  nameChanged(); //update UI
110 }
111 
112 void QgsNewNameDialog::setConflictingNameWarning( const QString &string )
113 {
114  mConflictingNameWarning = string;
115  nameChanged(); //update UI
116 }
117 
118 QString QgsNewNameDialog::highlightText( const QString &text )
119 {
120  return "<b>" + text + "</b>";
121 }
122 
124 {
125 
126  QString namesString = tr( "Full names" ) + ": ";
127  if ( !mExtensions.isEmpty() )
128  {
129  mNamesLabel->setText( namesString );
130  }
131  mErrorLabel->setText( QStringLiteral( " " ) ); // space to keep vertical space
132  QPushButton *okButton = buttonBox()->button( QDialogButtonBox::Ok );
133  okButton->setText( mOkString );
134  okButton->setEnabled( true );
135 
136  QString newName = name();
137 
138  if ( newName.length() == 0 || ( !mRegexp.isEmpty() && !mRegexp.exactMatch( newName ) ) )
139  {
140  //mErrorLabel->setText( highlightText( tr( "Enter new name" ) );
141  okButton->setEnabled( mAllowEmptyName );
142  return;
143  }
144 
145  QStringList newNames = fullNames( newName, mExtensions );
146  if ( !mExtensions.isEmpty() )
147  {
148  namesString += ' ' + newNames.join( QLatin1String( ", " ) );
149  mNamesLabel->setText( namesString );
150  }
151 
152  QStringList conflicts = matching( newNames, mExiting, mCaseSensitivity );
153 
154  if ( !conflicts.isEmpty() )
155  {
156  QString warning = !mConflictingNameWarning.isEmpty() ? mConflictingNameWarning
157  : tr( "%n Name(s) %1 exists", nullptr, conflicts.size() ).arg( conflicts.join( QLatin1String( ", " ) ) );
158  mErrorLabel->setText( highlightText( warning ) );
159  if ( mOverwriteEnabled )
160  {
161  okButton->setText( tr( "Overwrite" ) );
162  }
163  else
164  {
165  okButton->setEnabled( false );
166  }
167  return;
168  }
169 }
170 
171 QString QgsNewNameDialog::name() const
172 {
173  return mLineEdit->text().trimmed();
174 }
175 
176 QStringList QgsNewNameDialog::fullNames( const QString &name, const QStringList &extensions )
177 {
178  QStringList list;
179  const auto constExtensions = extensions;
180  for ( const QString &ext : constExtensions )
181  {
182  list << name + ext;
183 
184  }
185  if ( list.isEmpty() )
186  {
187  list << name;
188  }
189  return list;
190 }
191 
192 QStringList QgsNewNameDialog::matching( const QStringList &newNames, const QStringList &existingNames,
193  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 
212 bool QgsNewNameDialog::exists( const QString &name, const QStringList &extensions,
213  const QStringList &existing, Qt::CaseSensitivity cs )
214 {
215  QStringList newNames = fullNames( name, extensions );
216  QStringList conflicts = matching( newNames, existing, cs );
217  return !conflicts.isEmpty();
218 }
QgsNewNameDialog::exists
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.
Definition: qgsnewnamedialog.cpp:212
QgsDialog::buttonBox
QDialogButtonBox * buttonBox()
Returns the button box.
Definition: qgsdialog.h:48
QgsNewNameDialog::mAllowEmptyName
bool mAllowEmptyName
Definition: qgsnewnamedialog.h:159
QgsNewNameDialog::mErrorLabel
QLabel * mErrorLabel
Definition: qgsnewnamedialog.h:155
qgsnewnamedialog.h
QgsNewNameDialog::name
QString name() const
Name entered by user.
Definition: qgsnewnamedialog.cpp:171
QgsNewNameDialog::mOverwriteEnabled
bool mOverwriteEnabled
Definition: qgsnewnamedialog.h:158
QgsNewNameDialog::mNamesLabel
QLabel * mNamesLabel
List of names with extensions.
Definition: qgsnewnamedialog.h:154
QgsNewNameDialog::newNameChanged
void newNameChanged()
Emitted when the name is changed in the dialog.
QgsNewNameDialog::mRegexp
QRegExp mRegexp
Definition: qgsnewnamedialog.h:157
QgsNewNameDialog::mHintLabel
QLabel * mHintLabel
Definition: qgsnewnamedialog.h:151
QgsDialog::layout
QVBoxLayout * layout()
Returns the central layout. Widgets added to it must have this dialog as parent.
Definition: qgsdialog.h:46
QgsNewNameDialog::nameChanged
void nameChanged()
Definition: qgsnewnamedialog.cpp:123
QgsDialog
A generic dialog with layout and button box.
Definition: qgsdialog.h:34
QgsNewNameDialog::QgsNewNameDialog
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.
Definition: qgsnewnamedialog.cpp:26
QgsNewNameDialog::mOkString
QString mOkString
Definition: qgsnewnamedialog.h:156
QgsNewNameDialog::mExiting
QStringList mExiting
Definition: qgsnewnamedialog.h:148
QgsNewNameDialog::matching
static QStringList matching(const QStringList &newNames, const QStringList &existingNames, Qt::CaseSensitivity cs=Qt::CaseSensitive)
Definition: qgsnewnamedialog.cpp:192
QgsNewNameDialog::setConflictingNameWarning
void setConflictingNameWarning(const QString &string)
Sets the string used for warning users if a conflicting name exists.
Definition: qgsnewnamedialog.cpp:112
QgsNewNameDialog::fullNames
static QStringList fullNames(const QString &name, const QStringList &extensions)
Definition: qgsnewnamedialog.cpp:176
QgsNewNameDialog::mLineEdit
QLineEdit * mLineEdit
Definition: qgsnewnamedialog.h:152
QgsNewNameDialog::setOverwriteEnabled
void setOverwriteEnabled(bool enabled)
Sets whether users are permitted to overwrite existing names.
Definition: qgsnewnamedialog.cpp:100
QgsNewNameDialog::highlightText
QString highlightText(const QString &text)
Definition: qgsnewnamedialog.cpp:118
QgsNewNameDialog::hintString
QString hintString() const
Returns the hint string for the dialog (the text shown above the name input box).
Definition: qgsnewnamedialog.cpp:95
QgsNewNameDialog::mCaseSensitivity
Qt::CaseSensitivity mCaseSensitivity
Definition: qgsnewnamedialog.h:150
QgsNewNameDialog::mConflictingNameWarning
QString mConflictingNameWarning
Definition: qgsnewnamedialog.h:160
qgslogger.h
QgsNewNameDialog::setAllowEmptyName
void setAllowEmptyName(bool allowed)
Sets whether users are permitted to leave the widget empty.
Definition: qgsnewnamedialog.cpp:106
QgsNewNameDialog::setHintString
void setHintString(const QString &hintString)
Sets the hint string for the dialog (the text shown above the name input box).
Definition: qgsnewnamedialog.cpp:90
QgsNewNameDialog::mExtensions
QStringList mExtensions
Definition: qgsnewnamedialog.h:149