Skip to content

Commit d88f77f

Browse files
committed
new class QgsNewNameDialog - ask for a new name showing conflicts with existing names
1 parent ece9a92 commit d88f77f

File tree

2 files changed

+247
-0
lines changed

2 files changed

+247
-0
lines changed

src/gui/qgsnewnamedialog.cpp

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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, Qt::WindowFlags flags )
30+
: QgsDialog(parent, flags, QDialogButtonBox::Ok | QDialogButtonBox::Cancel)
31+
, mExiting(existing)
32+
, mExtensions(extensions)
33+
, mCaseSensitivity(cs)
34+
, mNamesLabel(0)
35+
{
36+
setWindowTitle( tr("New name") );
37+
QDialog::layout()->setSizeConstraint(QLayout::SetMinimumSize);
38+
layout()->setSizeConstraint(QLayout::SetMinimumSize);
39+
mOkString = buttonBox()->button(QDialogButtonBox::Ok)->text();
40+
QString hintString;
41+
QString nameDesc = mExtensions.isEmpty() ? tr("name") : tr("base name");
42+
if ( source.isEmpty() )
43+
{
44+
hintString = tr("Enter new %1").arg(nameDesc);
45+
}
46+
else
47+
{
48+
hintString = tr("Enter new %1 for %2").arg(nameDesc).arg(source);
49+
}
50+
QLabel* hintLabel = new QLabel( hintString, this );
51+
layout()->addWidget( hintLabel );
52+
53+
mLineEdit = new QLineEdit( initial, this );
54+
if ( !regexp.isEmpty() )
55+
{
56+
QRegExpValidator *validator = new QRegExpValidator( regexp, this );
57+
mLineEdit->setValidator( validator );
58+
}
59+
connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( nameChanged() ) );
60+
layout()->addWidget( mLineEdit );
61+
62+
mNamesLabel = new QLabel( " ", this );
63+
mNamesLabel->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
64+
if ( !mExtensions.isEmpty() )
65+
{
66+
mNamesLabel->setWordWrap(true);
67+
layout()->addWidget( mNamesLabel );
68+
}
69+
70+
mErrorLabel = new QLabel( " ", this );
71+
mErrorLabel->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
72+
mErrorLabel->setWordWrap(true);
73+
layout()->addWidget( mErrorLabel );
74+
75+
nameChanged();
76+
}
77+
78+
QString QgsNewNameDialog::highlightText(const QString& text)
79+
{
80+
return "<b>" + text + "</b>";
81+
}
82+
83+
void QgsNewNameDialog::nameChanged()
84+
{
85+
QgsDebugMsg( "entered" );
86+
87+
QString namesString = tr("Full names") + ": ";
88+
if ( !mExtensions.isEmpty() )
89+
{
90+
mNamesLabel->setText( namesString );
91+
}
92+
mErrorLabel->setText( " " ); // space to keep vertical space
93+
QPushButton* okButton = buttonBox()->button(QDialogButtonBox::Ok);
94+
okButton->setText( mOkString );
95+
okButton->setEnabled( true );
96+
97+
QString newName = name();
98+
99+
if ( newName.length() == 0 )
100+
{
101+
//mErrorLabel->setText( highlightText( tr( "Enter new name" ) );
102+
okButton->setEnabled( false );
103+
return;
104+
}
105+
106+
QStringList newNames = fullNames(newName, mExtensions);
107+
if ( !mExtensions.isEmpty() )
108+
{
109+
namesString += " " + newNames.join(", ");
110+
mNamesLabel->setText( namesString );
111+
}
112+
113+
QStringList conflicts = matching(newNames, mExiting, mCaseSensitivity );
114+
115+
if ( !conflicts.isEmpty() )
116+
{
117+
mErrorLabel->setText( highlightText( tr( "Name(s) %1 exists", 0, conflicts.size()).arg( conflicts.join(", ") ) ) );
118+
okButton->setText( tr( "Overwrite" ) );
119+
return;
120+
}
121+
}
122+
123+
QString QgsNewNameDialog::name() const
124+
{
125+
return mLineEdit->text().trimmed();
126+
}
127+
128+
QStringList QgsNewNameDialog::fullNames(const QString& name, const QStringList& extensions)
129+
{
130+
QStringList list;
131+
foreach ( QString ext, extensions)
132+
{
133+
list << name + ext;
134+
135+
}
136+
if ( list.isEmpty() )
137+
{
138+
list << name;
139+
}
140+
return list;
141+
}
142+
143+
QStringList QgsNewNameDialog::matching(const QStringList& newNames, const QStringList& existingNames,
144+
Qt::CaseSensitivity cs)
145+
{
146+
QStringList list;
147+
148+
foreach ( QString newName, newNames)
149+
{
150+
foreach ( QString existingName, existingNames)
151+
{
152+
if ( existingName.compare(newName, cs) == 0 )
153+
{
154+
list << existingName;
155+
}
156+
}
157+
}
158+
return list;
159+
}
160+
161+
bool QgsNewNameDialog::exists(const QString& name, const QStringList& extensions,
162+
const QStringList& existing, Qt::CaseSensitivity cs)
163+
{
164+
QStringList newNames = fullNames(name, extensions);
165+
QStringList conflicts = matching(newNames, existing, cs );
166+
return conflicts.size() > 0;
167+
}

src/gui/qgsnewnamedialog.h

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/***************************************************************************
2+
qgsnewnamedialog.h
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+
#ifndef QGSNEWNAMEDIALOG_H
18+
#define QGSNEWNAMEDIALOG_H
19+
20+
class QLabel;
21+
class QLineEdit;
22+
23+
#include "qgsdialog.h"
24+
25+
/** \ingroup gui
26+
* New name, for example new layer name dialog. If existing names are provided,
27+
* the dialog warns users if an entered name already exists.
28+
* @note added in 2.10
29+
*/
30+
class GUI_EXPORT QgsNewNameDialog : public QgsDialog
31+
{
32+
Q_OBJECT
33+
public:
34+
/** New dialog constructor.
35+
* @param source original data source name, e.g. original layer name of the layer to be copied
36+
* @param initial initial name
37+
* @param extensions base name extensions, e.g. raster base name band extensions or vector layer type extensions
38+
* @param existing existing names
39+
* @param regexp regular expression to be used as validator, for example db tables should have "[A-Za-z_][A-Za-z0-9_]+"
40+
* @param cs case sensitivity for new name to existing names comparison
41+
* @param parent
42+
* @param flags
43+
*/
44+
QgsNewNameDialog( const QString& source = QString::null, const QString& initial = QString::null,
45+
const QStringList& extensions = QStringList(), const QStringList& existing = QStringList(),
46+
const QRegExp& regexp = QRegExp(), Qt::CaseSensitivity cs = Qt::CaseSensitive,
47+
QWidget *parent = 0, Qt::WindowFlags flags = QgisGui::ModalDialogFlags );
48+
49+
/** Name entered by user.
50+
* @return new name
51+
*/
52+
QString name() const;
53+
54+
/** Test if name or name with at least one extension exists.
55+
* @param name name or base name
56+
* @param extensions base name extensions
57+
* @param existing existing names
58+
* @return true if name exists
59+
*/
60+
static bool exists(const QString& name, const QStringList& extensions,
61+
const QStringList& existing, Qt::CaseSensitivity cs = Qt::CaseSensitive );
62+
public slots:
63+
void nameChanged();
64+
65+
protected:
66+
QStringList mExiting;
67+
QStringList mExtensions;
68+
Qt::CaseSensitivity mCaseSensitivity;
69+
QLineEdit *mLineEdit;
70+
QLabel *mNamesLabel; // list of names with extensions
71+
QLabel *mErrorLabel;
72+
QString mOkString;
73+
QString highlightText(const QString& text);
74+
static QStringList fullNames(const QString& name, const QStringList& extensions);
75+
// get list of existing names
76+
static QStringList matching(const QStringList& newNames, const QStringList& existingNames,
77+
Qt::CaseSensitivity cs = Qt::CaseSensitive);
78+
};
79+
80+
#endif // QGSNEWNAMEDIALOG_H

0 commit comments

Comments
 (0)