QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  , mNamesLabel( 0 )
35  , mRegexp( regexp )
36 {
37  setWindowTitle( tr( "New name" ) );
38  QDialog::layout()->setSizeConstraint( QLayout::SetMinimumSize );
39  layout()->setSizeConstraint( QLayout::SetMinimumSize );
40  layout()->setSpacing( 6 );
41  mOkString = buttonBox()->button( QDialogButtonBox::Ok )->text();
42  QString hintString;
43  QString nameDesc = mExtensions.isEmpty() ? tr( "name" ) : tr( "base name" );
44  if ( source.isEmpty() )
45  {
46  hintString = tr( "Enter new %1" ).arg( nameDesc );
47  }
48  else
49  {
50  hintString = tr( "Enter new %1 for %2" ).arg( nameDesc ).arg( source );
51  }
52  QLabel* hintLabel = new QLabel( hintString, this );
53  layout()->addWidget( hintLabel );
54 
55  mLineEdit = new QLineEdit( initial, this );
56  if ( !regexp.isEmpty() )
57  {
58  QRegExpValidator *validator = new QRegExpValidator( regexp, this );
59  mLineEdit->setValidator( validator );
60  }
61  connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( nameChanged() ) );
63 
64  mNamesLabel = new QLabel( " ", this );
65  mNamesLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
66  if ( !mExtensions.isEmpty() )
67  {
68  mNamesLabel->setWordWrap( true );
70  }
71 
72  mErrorLabel = new QLabel( " ", this );
73  mErrorLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
74  mErrorLabel->setWordWrap( true );
76 
77  nameChanged();
78 }
79 
81 {
82  return "<b>" + text + "</b>";
83 }
84 
86 {
87  QgsDebugMsg( "entered" );
88 
89  QString namesString = tr( "Full names" ) + ": ";
90  if ( !mExtensions.isEmpty() )
91  {
92  mNamesLabel->setText( namesString );
93  }
94  mErrorLabel->setText( " " ); // space to keep vertical space
95  QPushButton* okButton = buttonBox()->button( QDialogButtonBox::Ok );
96  okButton->setText( mOkString );
97  okButton->setEnabled( true );
98 
99  QString newName = name();
100 
101  if ( newName.length() == 0 || ( !mRegexp.isEmpty() && !mRegexp.exactMatch( newName ) ) )
102  {
103  //mErrorLabel->setText( highlightText( tr( "Enter new name" ) );
104  okButton->setEnabled( false );
105  return;
106  }
107 
108  QStringList newNames = fullNames( newName, mExtensions );
109  if ( !mExtensions.isEmpty() )
110  {
111  namesString += " " + newNames.join( ", " );
112  mNamesLabel->setText( namesString );
113  }
114 
115  QStringList conflicts = matching( newNames, mExiting, mCaseSensitivity );
116 
117  if ( !conflicts.isEmpty() )
118  {
119  mErrorLabel->setText( highlightText( tr( "%n Name(s) %1 exists", 0, conflicts.size() ).arg( conflicts.join( ", " ) ) ) );
120  okButton->setText( tr( "Overwrite" ) );
121  return;
122  }
123 }
124 
126 {
127  return mLineEdit->text().trimmed();
128 }
129 
131 {
132  QStringList list;
133  foreach ( QString ext, extensions )
134  {
135  list << name + ext;
136 
137  }
138  if ( list.isEmpty() )
139  {
140  list << name;
141  }
142  return list;
143 }
144 
145 QStringList QgsNewNameDialog::matching( const QStringList& newNames, const QStringList& existingNames,
146  Qt::CaseSensitivity cs )
147 {
148  QStringList list;
149 
150  foreach ( QString newName, newNames )
151  {
152  foreach ( QString existingName, existingNames )
153  {
154  if ( existingName.compare( newName, cs ) == 0 )
155  {
156  list << existingName;
157  }
158  }
159  }
160  return list;
161 }
162 
163 bool QgsNewNameDialog::exists( const QString& name, const QStringList& extensions,
164  const QStringList& existing, Qt::CaseSensitivity cs )
165 {
166  QStringList newNames = fullNames( name, extensions );
167  QStringList conflicts = matching( newNames, existing, cs );
168  return conflicts.size() > 0;
169 }
QLayout * layout() const
QString name() const
Name entered by user.
Qt::CaseSensitivity mCaseSensitivity
bool isEmpty() const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
QStringList mExiting
void setSizeConstraint(SizeConstraint)
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
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 setEnabled(bool)
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
bool isEmpty() const
bool isEmpty() const
void setText(const QString &)
void setSizePolicy(QSizePolicy)
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
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=0, Qt::WindowFlags flags=QgisGui::ModalDialogFlags)
New dialog constructor.
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)
QLineEdit * mLineEdit