2 ******************************************************************************
4 * @file classnamevalidatinglineedit.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "classnamevalidatinglineedit.h"
31 #include <utils/qtcassert.h>
33 #include <QtCore/QDebug>
34 #include <QtCore/QRegExp>
37 struct ClassNameValidatingLineEditPrivate
{
38 ClassNameValidatingLineEditPrivate();
40 const QRegExp m_nameRegexp
;
41 const QString m_namespaceDelimiter
;
42 bool m_namespacesEnabled
;
43 bool m_lowerCaseFileName
;
46 // Match something like "Namespace1::Namespace2::ClassName".
47 ClassNameValidatingLineEditPrivate::ClassNameValidatingLineEditPrivate() :
48 m_nameRegexp(QLatin1String("[a-zA-Z_][a-zA-Z0-9_]*(::[a-zA-Z_][a-zA-Z0-9_]*)*")),
49 m_namespaceDelimiter(QLatin1String("::")),
50 m_namespacesEnabled(false),
51 m_lowerCaseFileName(true)
53 QTC_ASSERT(m_nameRegexp
.isValid(), return );
56 // --------------------- ClassNameValidatingLineEdit
57 ClassNameValidatingLineEdit::ClassNameValidatingLineEdit(QWidget
*parent
) :
58 Utils::BaseValidatingLineEdit(parent
),
59 m_d(new ClassNameValidatingLineEditPrivate
)
62 ClassNameValidatingLineEdit::~ClassNameValidatingLineEdit()
67 bool ClassNameValidatingLineEdit::namespacesEnabled() const
69 return m_d
->m_namespacesEnabled
;
72 void ClassNameValidatingLineEdit::setNamespacesEnabled(bool b
)
74 m_d
->m_namespacesEnabled
= b
;
77 bool ClassNameValidatingLineEdit::validate(const QString
&value
, QString
*errorMessage
) const
79 if (!m_d
->m_namespacesEnabled
&& value
.contains(QLatin1Char(':'))) {
81 *errorMessage
= tr("The class name must not contain namespace delimiters.");
84 } else if (value
.isEmpty()) {
86 *errorMessage
= tr("Please enter a class name.");
89 } else if (!m_d
->m_nameRegexp
.exactMatch(value
)) {
91 *errorMessage
= tr("The class name contains invalid characters.");
98 void ClassNameValidatingLineEdit::slotChanged(const QString
&t
)
100 Utils::BaseValidatingLineEdit::slotChanged(t
);
103 // Suggest file names, strip namespaces
104 QString fileName
= m_d
->m_lowerCaseFileName
? t
.toLower() : t
;
105 if (m_d
->m_namespacesEnabled
) {
106 const int namespaceIndex
= fileName
.lastIndexOf(m_d
->m_namespaceDelimiter
);
107 if (namespaceIndex
!= -1) {
108 fileName
.remove(0, namespaceIndex
+ m_d
->m_namespaceDelimiter
.size());
111 emit
updateFileName(fileName
);
115 QString
ClassNameValidatingLineEdit::createClassName(const QString
&name
)
117 // Remove spaces and convert the adjacent characters to uppercase
118 QString className
= name
;
119 QRegExp
spaceMatcher(QLatin1String(" +(\\w)"), Qt::CaseSensitive
, QRegExp::RegExp2
);
121 QTC_ASSERT(spaceMatcher
.isValid(), /**/);
123 while ((pos
= spaceMatcher
.indexIn(className
)) != -1) {
124 className
.replace(pos
, spaceMatcher
.matchedLength(),
125 spaceMatcher
.cap(1).toUpper());
128 // Filter out any remaining invalid characters
129 className
.remove(QRegExp(QLatin1String("[^a-zA-Z0-9_]")));
131 // If the first character is numeric, prefix the name with a "_"
132 if (className
.at(0).isNumber()) {
133 className
.prepend(QLatin1Char('_'));
135 // Convert the first character to uppercase
136 className
.replace(0, 1, className
.left(1).toUpper());
142 bool ClassNameValidatingLineEdit::lowerCaseFileName() const
144 return m_d
->m_lowerCaseFileName
;
147 void ClassNameValidatingLineEdit::setLowerCaseFileName(bool v
)
149 m_d
->m_lowerCaseFileName
= v
;