QGIS API Documentation  2.14.0-Essen
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 <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 
79  nameChanged();
80 }
81 
83 {
84  mHintLabel->setText( hintString );
85 }
86 
88 {
89  return mHintLabel->text();
90 }
91 
93 {
95  nameChanged(); //update UI
96 }
97 
99 {
100  mConflictingNameWarning = string;
101  nameChanged(); //update UI
102 }
103 
105 {
106  return "<b>" + text + "</b>";
107 }
108 
110 {
111  QgsDebugMsg( "entered" );
112 
113  QString namesString = tr( "Full names" ) + ": ";
114  if ( !mExtensions.isEmpty() )
115  {
116  mNamesLabel->setText( namesString );
117  }
118  mErrorLabel->setText( " " ); // space to keep vertical space
119  QPushButton* okButton = buttonBox()->button( QDialogButtonBox::Ok );
120  okButton->setText( mOkString );
121  okButton->setEnabled( true );
122 
123  QString newName = name();
124 
125  if ( newName.length() == 0 || ( !mRegexp.isEmpty() && !mRegexp.exactMatch( newName ) ) )
126  {
127  //mErrorLabel->setText( highlightText( tr( "Enter new name" ) );
128  okButton->setEnabled( false );
129  return;
130  }
131 
132  QStringList newNames = fullNames( newName, mExtensions );
133  if ( !mExtensions.isEmpty() )
134  {
135  namesString += ' ' + newNames.join( ", " );
136  mNamesLabel->setText( namesString );
137  }
138 
139  QStringList conflicts = matching( newNames, mExiting, mCaseSensitivity );
140 
141  if ( !conflicts.isEmpty() )
142  {
144  : tr( "%n Name(s) %1 exists", nullptr, conflicts.size() ).arg( conflicts.join( ", " ) );
145  mErrorLabel->setText( highlightText( warning ) );
146  if ( mOverwriteEnabled )
147  {
148  okButton->setText( tr( "Overwrite" ) );
149  }
150  else
151  {
152  okButton->setEnabled( false );
153  }
154  return;
155  }
156 }
157 
159 {
160  return mLineEdit->text().trimmed();
161 }
162 
164 {
165  QStringList list;
166  Q_FOREACH ( const QString& ext, extensions )
167  {
168  list << name + ext;
169 
170  }
171  if ( list.isEmpty() )
172  {
173  list << name;
174  }
175  return list;
176 }
177 
178 QStringList QgsNewNameDialog::matching( const QStringList& newNames, const QStringList& existingNames,
179  Qt::CaseSensitivity cs )
180 {
181  QStringList list;
182 
183  Q_FOREACH ( const QString& newName, newNames )
184  {
185  Q_FOREACH ( const QString& existingName, existingNames )
186  {
187  if ( existingName.compare( newName, cs ) == 0 )
188  {
189  list << existingName;
190  }
191  }
192  }
193  return list;
194 }
195 
196 bool QgsNewNameDialog::exists( const QString& name, const QStringList& extensions,
197  const QStringList& existing, Qt::CaseSensitivity cs )
198 {
199  QStringList newNames = fullNames( name, extensions );
200  QStringList conflicts = matching( newNames, existing, cs );
201  return !conflicts.isEmpty();
202 }
QLayout * layout() const
QString name() const
Name entered by user.
void setOverwriteEnabled(bool enabled)
Sets whether users are permitted to overwrite existing names.
Qt::CaseSensitivity mCaseSensitivity
bool isEmpty() const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
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
QDialogButtonBox * buttonBox()
Returns the button box.
Definition: qgsdialog.h:42
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QString mConflictingNameWarning
bool isEmpty() const
bool isEmpty() const
void setText(const QString &)
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
QString hintString() const
Returns the hint string for the dialog (the text shown above the name input box). ...
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