|
| 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 | +} |
0 commit comments