QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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, const Qt::WindowFlags& flags )
30  : QgsDialog( parent, flags, QDialogButtonBox::Ok | QDialogButtonBox::Cancel )
31  , mExiting( existing )
32  , mExtensions( extensions )
33  , mCaseSensitivity( cs )
34  , mNamesLabel( nullptr )
35  , mRegexp( regexp )
36  , mOverwriteEnabled( true )
37 {
38  setWindowTitle( tr( "New name" ) );
39  QDialog::layout()->setSizeConstraint( QLayout::SetMinimumSize );
40  layout()->setSizeConstraint( QLayout::SetMinimumSize );
41  layout()->setSpacing( 6 );
42  mOkString = buttonBox()->button( QDialogButtonBox::Ok )->text();
44  QString nameDesc = mExtensions.isEmpty() ? tr( "name" ) : tr( "base name" );
45  if ( source.isEmpty() )
46  {
47  hintString = tr( "Enter new %1" ).arg( nameDesc );
48  }
49  else
50  {
51  hintString = tr( "Enter new %1 for %2" ).arg( nameDesc, source );
52  }
53  mHintLabel = new QLabel( hintString, this );
55 
56  mLineEdit = new QLineEdit( initial, this );
57  if ( !regexp.isEmpty() )
58  {
59  QRegExpValidator *validator = new QRegExpValidator( regexp, this );
60  mLineEdit->setValidator( validator );
61  }
62  mLineEdit->setMinimumWidth( mLineEdit->fontMetrics().width( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ) );
63  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( nameChanged() ) );
65 
66  mNamesLabel = new QLabel( " ", this );
67  mNamesLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
68  if ( !mExtensions.isEmpty() )
69  {
70  mNamesLabel->setWordWrap( true );
72  }
73 
74  mErrorLabel = new QLabel( " ", this );
75  mErrorLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
76  mErrorLabel->setWordWrap( true );
78 
81 
82  nameChanged();
83 }
84 
86 {
87  mHintLabel->setText( hintString );
88 }
89 
91 {
92  return mHintLabel->text();
93 }
94 
96 {
98  nameChanged(); //update UI
99 }
100 
102 {
103  mConflictingNameWarning = string;
104  nameChanged(); //update UI
105 }
106 
108 {
109  return "<b>" + text + "</b>";
110 }
111 
113 {
114 
115  QString namesString = tr( "Full names" ) + ": ";
116  if ( !mExtensions.isEmpty() )
117  {
118  mNamesLabel->setText( namesString );
119  }
120  mErrorLabel->setText( " " ); // space to keep vertical space
121  QPushButton* okButton = buttonBox()->button( QDialogButtonBox::Ok );
122  okButton->setText( mOkString );
123  okButton->setEnabled( true );
124 
125  QString newName = name();
126 
127  if ( newName.length() == 0 || ( !mRegexp.isEmpty() && !mRegexp.exactMatch( newName ) ) )
128  {
129  //mErrorLabel->setText( highlightText( tr( "Enter new name" ) );
130  okButton->setEnabled( false );
131  return;
132  }
133 
134  QStringList newNames = fullNames( newName, mExtensions );
135  if ( !mExtensions.isEmpty() )
136  {
137  namesString += ' ' + newNames.join( ", " );
138  mNamesLabel->setText( namesString );
139  }
140 
141  QStringList conflicts = matching( newNames, mExiting, mCaseSensitivity );
142 
143  if ( !conflicts.isEmpty() )
144  {
146  : tr( "%n Name(s) %1 exists", nullptr, conflicts.size() ).arg( conflicts.join( ", " ) );
147  mErrorLabel->setText( highlightText( warning ) );
148  if ( mOverwriteEnabled )
149  {
150  okButton->setText( tr( "Overwrite" ) );
151  }
152  else
153  {
154  okButton->setEnabled( false );
155  }
156  return;
157  }
158 }
159 
161 {
162  return mLineEdit->text().trimmed();
163 }
164 
166 {
167  QStringList list;
168  Q_FOREACH ( const QString& ext, extensions )
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  Q_FOREACH ( const QString& newName, newNames )
186  {
187  Q_FOREACH ( const QString& existingName, existingNames )
188  {
189  if ( existingName.compare( newName, cs ) == 0 )
190  {
191  list << existingName;
192  }
193  }
194  }
195  return list;
196 }
197 
198 bool QgsNewNameDialog::exists( const QString& name, const QStringList& extensions,
199  const QStringList& existing, Qt::CaseSensitivity cs )
200 {
201  QStringList newNames = fullNames( name, extensions );
202  QStringList conflicts = matching( newNames, existing, cs );
203  return !conflicts.isEmpty();
204 }
QLayout * layout() const
void setOverwriteEnabled(bool enabled)
Sets whether users are permitted to overwrite existing names.
Qt::CaseSensitivity mCaseSensitivity
bool isEmpty() const
QStringList mExiting
void setSizeConstraint(SizeConstraint)
void setMinimumWidth(int minw)
A generic dialog with layout and button box.
Definition: qgsdialog.h:30
QStringList mExtensions
static QStringList matching(const QStringList &newNames, const QStringList &existingNames, Qt::CaseSensitivity cs=Qt::CaseSensitive)
QString join(const QString &separator) const
QgsNewNameDialog(const QString &source=QString::null, const QString &initial=QString::null, const QStringList &extensions=QStringList(), const QStringList &existing=QStringList(), const QRegExp &regexp=QRegExp(), Qt::CaseSensitivity cs=Qt::CaseSensitive, QWidget *parent=nullptr, const Qt::WindowFlags &flags=QgisGui::ModalDialogFlags)
New dialog constructor.
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 tr(const char *sourceText, const char *disambiguation, int n)
int size() const
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:42
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QString mConflictingNameWarning
QString name() const
Name entered by user.
void setFocus()
bool isEmpty() const
bool isEmpty() const
void setText(const QString &)
void selectAll()
void setSizePolicy(QSizePolicy)
void setHintString(const QString &hintString)
Sets the hint string for the dialog (the text shown above the name input box).
int width(const QString &text, int len) const
QFontMetrics fontMetrics() const
void setWindowTitle(const QString &)
QVBoxLayout * layout()
Returns the central layout. Widgets added to it must have this dialog as parent.
Definition: qgsdialog.h:40
int length() const
QPushButton * button(StandardButton which) const
typedef WindowFlags
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
int compare(const QString &other) const
QString highlightText(const QString &text)
bool exactMatch(const QString &str) const
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void setValidator(const QValidator *v)
void setWordWrap(bool on)
void setSpacing(int spacing)
void setConflictingNameWarning(const QString &string)
Sets the string used for warning users if a conflicting name exists.
QLineEdit * mLineEdit