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