Commit 1c715d38 authored by Ralf Habacker's avatar Ralf Habacker
Browse files

Add additional c++ code generation policies to fix unexpected code generation

- Getters with/without 'get' prefix
- Remove prefix '[a-zA-Z]_' from accessor methods
- Accessor methods start with/without capital letters

BUG:413991
FIXED-IN:2.29.80 (KDE Applications 19.11.80)
parent 66b708ef
......@@ -67,6 +67,18 @@ CPPCodeGenerationForm::CPPCodeGenerationForm(QWidget *parent, const char *name)
new QListWidgetItem(i18n("Accessors are public"), ui_generalOptionsListWidget);
m_optionAccessorsArePublic->setFlags(flags);
m_optionGetterWithGetPrefix =
new QListWidgetItem(i18n("Create getters with get prefix"), ui_generalOptionsListWidget);
m_optionGetterWithGetPrefix->setFlags(flags);
m_optionRemovePrefixFromAccessorMethodName =
new QListWidgetItem(i18n("Remove prefix '[a-zA-Z]_' from accessor method names"), ui_generalOptionsListWidget);
m_optionRemovePrefixFromAccessorMethodName->setFlags(flags);
m_optionAccessorMethodsStartWithUpperCase =
new QListWidgetItem(i18n("Accessor methods start with capital letters"), ui_generalOptionsListWidget);
m_optionAccessorMethodsStartWithUpperCase->setFlags(flags);
m_optionDocToolTag =
new QListWidgetItem(i18n("Use '\\' as documentation tag instead of '@'"), ui_generalOptionsListWidget);
m_optionDocToolTag->setFlags(flags);
......@@ -146,10 +158,16 @@ void CPPCodeGenerationForm::generalOptionsListWidgetClicked(QListWidgetItem *pSe
(m_optionGenerateAccessorMethods->checkState() == Qt::Unchecked);
m_optionAccessorsAreInline->setHidden(dontGenerateAccessorMethods);
m_optionAccessorsArePublic->setHidden(dontGenerateAccessorMethods);
m_optionGetterWithGetPrefix->setHidden(dontGenerateAccessorMethods);
m_optionRemovePrefixFromAccessorMethodName->setHidden(dontGenerateAccessorMethods);
m_optionAccessorMethodsStartWithUpperCase->setHidden(dontGenerateAccessorMethods);
// reset the value if needed
if (dontGenerateAccessorMethods) {
m_optionAccessorsAreInline->setCheckState(Qt::Unchecked);
m_optionAccessorsArePublic->setCheckState(Qt::Unchecked);
m_optionGetterWithGetPrefix->setCheckState(Qt::Unchecked);
m_optionRemovePrefixFromAccessorMethodName->setCheckState(Qt::Unchecked);
m_optionAccessorMethodsStartWithUpperCase->setHidden(Qt::Unchecked);
}
#if 0
KMessageBox::error(0, "CPPCodeGenerationForm::generalOptionsListViewClicked(): "
......@@ -250,6 +268,33 @@ void CPPCodeGenerationForm::setAccessorsArePublic(bool bFlag)
m_optionAccessorsArePublic->setCheckState(toCheckState(bFlag));
}
/**
* Set the display state of the related checkbox
* @param flag the flag to set
*/
void CPPCodeGenerationForm::setGetterWithoutGetPrefix(bool bFlag)
{
m_optionGetterWithGetPrefix->setCheckState(toCheckState(toCheckState(bFlag)));
}
/**
* Set the display state of the related checkbox
* @param flag the flag to set
*/
void CPPCodeGenerationForm::setRemovePrefixFromAccessorMethodName(bool bFlag)
{
m_optionRemovePrefixFromAccessorMethodName->setCheckState(toCheckState(toCheckState(bFlag)));
}
/**
* Set the display state of the related checkbox
* @param flag the flag to set
*/
void CPPCodeGenerationForm::setAccessorMethodsStartWithUpperCase(bool bFlag)
{
m_optionAccessorMethodsStartWithUpperCase->setCheckState(toCheckState(toCheckState(bFlag)));
}
/**
* Set the doc display state of option "Doc Tool Tag".
* @param value the value of the tag
......@@ -322,6 +367,37 @@ bool CPPCodeGenerationForm::getAccessorsArePublic()
return m_optionAccessorsArePublic->checkState() == Qt::Checked;
}
/**
* Get the display state of the related option
* @return the state of the flag
*/
bool CPPCodeGenerationForm::getGettersWithGetPrefix()
{
return m_optionGetterWithGetPrefix->checkState() == Qt::Checked;
}
/**
* Get the display state of the related option
* @return the state of the flag
*/
bool CPPCodeGenerationForm::getRemovePrefixFromAccessorMethodName()
{
return m_optionRemovePrefixFromAccessorMethodName->checkState() == Qt::Checked;
}
/**
* Get the display state of the related option
* @return the state of the flag
*/
bool CPPCodeGenerationForm::getAccessorMethodsStartWithUpperCase()
{
return m_optionAccessorMethodsStartWithUpperCase->checkState() == Qt::Checked;
}
/**
* Get the display state of the related option
* @return the state of the flag
*/
QString CPPCodeGenerationForm::getDocToolTag()
{
return m_optionDocToolTag->checkState() == Qt::Checked ? QLatin1String("\\") : QLatin1String("@");
......
......@@ -35,6 +35,9 @@ public:
void setAccessorsArePublic(bool bFlag = true);
void setGenerateMakefileDocument(bool bFlag = true);
void setDocToolTag(const QString &value);
void setGetterWithoutGetPrefix(bool bFlag = true);
void setRemovePrefixFromAccessorMethodName(bool bFlag = true);
void setAccessorMethodsStartWithUpperCase(bool bFlag);
bool getPackageIsANamespace();
bool getVirtualDestructors();
......@@ -45,6 +48,9 @@ public:
bool getAccessorsArePublic();
bool getGenerateMakefileDocument();
QString getDocToolTag();
bool getGettersWithGetPrefix();
bool getRemovePrefixFromAccessorMethodName();
bool getAccessorMethodsStartWithUpperCase();
public slots:
......@@ -67,6 +73,9 @@ private:
QListWidgetItem *m_optionAccessorsAreInline;
QListWidgetItem *m_optionAccessorsArePublic;
QListWidgetItem *m_optionDocToolTag;
QListWidgetItem *m_optionGetterWithGetPrefix;
QListWidgetItem *m_optionRemovePrefixFromAccessorMethodName;
QListWidgetItem *m_optionAccessorMethodsStartWithUpperCase;
static Qt::CheckState toCheckState(bool value);
......
......@@ -121,6 +121,39 @@ bool CPPCodeGenerationPolicy::getDestructorsAreVirtual()
return Settings::optionState().codeGenerationState.cppCodeGenerationState.virtualDestructors;
}
void CPPCodeGenerationPolicy::setGetterWithGetPrefix(bool var)
{
Settings::optionState().codeGenerationState.cppCodeGenerationState.getterWithGetPrefix = var;
UMLApp::app()->commonPolicy()->emitModifiedCodeContentSig();
}
bool CPPCodeGenerationPolicy::getGetterWithGetPrefix()
{
return Settings::optionState().codeGenerationState.cppCodeGenerationState.getterWithGetPrefix;
}
void CPPCodeGenerationPolicy::setRemovePrefixFromAccessorMethods(bool var)
{
Settings::optionState().codeGenerationState.cppCodeGenerationState.removePrefixFromAccessorMethods = var;
UMLApp::app()->commonPolicy()->emitModifiedCodeContentSig();
}
bool CPPCodeGenerationPolicy::getRemovePrefixFromAccessorMethods()
{
return Settings::optionState().codeGenerationState.cppCodeGenerationState.removePrefixFromAccessorMethods;
}
void CPPCodeGenerationPolicy::setAccessorMethodsStartWithUpperCase(bool var)
{
Settings::optionState().codeGenerationState.cppCodeGenerationState.accessorMethodsStartWithUpperCase = var;
UMLApp::app()->commonPolicy()->emitModifiedCodeContentSig();
}
bool CPPCodeGenerationPolicy::getAccessorMethodsStartWithUpperCase()
{
return Settings::optionState().codeGenerationState.cppCodeGenerationState.accessorMethodsStartWithUpperCase;
}
/**
* Set the value of m_packageIsNamespace.
* @param var the new value
......@@ -320,6 +353,9 @@ void CPPCodeGenerationPolicy::setDefaults(CPPCodeGenerationPolicy * cppclone, bo
setAccessorsAreInline(cppclone->getAccessorsAreInline());
setOperationsAreInline(cppclone->getOperationsAreInline());
setDestructorsAreVirtual(cppclone->getDestructorsAreVirtual());
setGetterWithGetPrefix(cppclone->getGetterWithGetPrefix());
setRemovePrefixFromAccessorMethods(cppclone->getRemovePrefixFromAccessorMethods());
setAccessorMethodsStartWithUpperCase(cppclone->getAccessorMethodsStartWithUpperCase());
setPackageIsNamespace(cppclone->getPackageIsNamespace());
setStringClassName(cppclone->getStringClassName());
......@@ -354,6 +390,9 @@ void CPPCodeGenerationPolicy::setDefaults(bool emitUpdateSignal)
setAccessorsArePublic(UmbrelloSettings::publicAccessors());
setOperationsAreInline(UmbrelloSettings::inlineOps());
setDestructorsAreVirtual(UmbrelloSettings::virtualDestructors());
setGetterWithGetPrefix(UmbrelloSettings::getterWithGetPrefix());
setRemovePrefixFromAccessorMethods(UmbrelloSettings::removePrefixFromAccessorMethods());
setAccessorMethodsStartWithUpperCase(UmbrelloSettings::accessorMethodsStartWithUpperCase());
setPackageIsNamespace(UmbrelloSettings::packageIsNamespace());
setStringClassName(UmbrelloSettings::stringClassName());
......@@ -401,6 +440,9 @@ void CPPCodeGenerationPolicy::init()
setAccessorsArePublic(optionState.codeGenerationState.cppCodeGenerationState.publicAccessors);
setOperationsAreInline(optionState.codeGenerationState.cppCodeGenerationState.inlineOps);
setDestructorsAreVirtual(optionState.codeGenerationState.cppCodeGenerationState.virtualDestructors);
setGetterWithGetPrefix(optionState.codeGenerationState.cppCodeGenerationState.getterWithGetPrefix);
setRemovePrefixFromAccessorMethods(optionState.codeGenerationState.cppCodeGenerationState.removePrefixFromAccessorMethods);
setAccessorMethodsStartWithUpperCase(optionState.codeGenerationState.cppCodeGenerationState.accessorMethodsStartWithUpperCase);
setPackageIsNamespace(optionState.codeGenerationState.cppCodeGenerationState.packageIsNamespace);
setStringClassName(optionState.codeGenerationState.cppCodeGenerationState.stringClassName);
......
......@@ -50,6 +50,15 @@ public:
void setAccessorsArePublic(bool var);
bool getAccessorsArePublic();
void setGetterWithGetPrefix(bool var);
bool getGetterWithGetPrefix();
void setRemovePrefixFromAccessorMethods(bool var);
bool getRemovePrefixFromAccessorMethods();
bool getAccessorMethodsStartWithUpperCase();
void setAccessorMethodsStartWithUpperCase(bool var);
/**
* We want to be flexible about which classes are allowed for generation
* of the CPP code. In the next 4 method pairs, we give accessors that allow setting and getting
......@@ -88,7 +97,6 @@ public:
CodeGenerationPolicyPage * createPage (QWidget *parent = 0, const char * name = 0);
protected:
void init();
private:
......
......@@ -40,6 +40,9 @@ CPPCodeGenerationPolicyPage::CPPCodeGenerationPolicyPage(QWidget *parent, const
form->setOperationsAreInline(policy->getOperationsAreInline());
form->setAccessorsAreInline(policy->getAccessorsAreInline());
form->setAccessorsArePublic(policy->getAccessorsArePublic());
form->setGetterWithoutGetPrefix(policy->getGetterWithGetPrefix());
form->setRemovePrefixFromAccessorMethodName(policy->getRemovePrefixFromAccessorMethods());
form->setAccessorMethodsStartWithUpperCase(policy->getAccessorMethodsStartWithUpperCase());
form->setDocToolTag(policy->getDocToolTag());
form->ui_stringClassHCombo->setCurrentItem(policy->getStringClassName(), true);
......@@ -79,6 +82,9 @@ void CPPCodeGenerationPolicyPage::apply()
parent->setAccessorsAreInline(form->getAccessorsAreInline());
parent->setOperationsAreInline(form->getOperationsAreInline());
parent->setAccessorsArePublic(form->getAccessorsArePublic());
parent->setGetterWithGetPrefix(form->getGettersWithGetPrefix());
parent->setRemovePrefixFromAccessorMethods(form->getRemovePrefixFromAccessorMethodName());
parent->setAccessorMethodsStartWithUpperCase(form->getAccessorMethodsStartWithUpperCase());
parent->setDocToolTag(form->getDocToolTag());
parent->setStringClassName(form->ui_stringClassHCombo->currentText());
......
......@@ -607,12 +607,7 @@ void CppWriter::writeAttributeMethods(UMLAttributeList attribs,
QString varName = getAttributeVariableName(at);
QString methodBaseName = cleanName(at->name());
// force capitalizing the field name, this is silly,
// from what I can tell, this IS the default behavior for
// cleanName. I dunno why it is not working -b.t.
methodBaseName = methodBaseName.trimmed();
methodBaseName.replace(0, 1, methodBaseName.at(0).toUpper());
writeSingleAttributeAccessorMethods(at->getTypeName(), varName,
methodBaseName, at->doc(), Uml::Changeability::Changeable, isHeaderMethod,
at->isStatic(), writeMethodBody, stream);
......@@ -936,7 +931,12 @@ void CppWriter::writeSingleAttributeAccessorMethods(
return;
QString className = fixTypeName(fieldClassName);
QString fldName = Codegen_Utils::capitalizeFirstLetter(fieldName);
QString fldName = fieldName;
if (policyExt()->getRemovePrefixFromAccessorMethods())
fldName.replace(QRegExp(QLatin1String("^[a-zA-Z]_")), QLatin1String(""));
QString setFldName = Codegen_Utils::capitalizeFirstLetter(fldName);
if (policyExt()->getAccessorMethodsStartWithUpperCase())
Codegen_Utils::capitalizeFirstLetter(fldName);
QString indnt = indent();
QString varName = QLatin1String("new_var");
QString fullVarName = varName;
......@@ -981,7 +981,9 @@ void CppWriter::writeSingleAttributeAccessorMethods(
stream << indnt << className << " ";
if (!isHeaderMethod)
stream << className_ << "::";
stream << "get" << fldName << "()";
if (!policyExt()->getRemovePrefixFromAccessorMethods())
stream << "get";
stream << fldName << "()";
if (writeMethodBody) {
stream << m_endl << indnt << "{" << m_endl;
......
......@@ -311,6 +311,9 @@ namespace Settings {
UmbrelloSettings::setPublicAccessors(cppCodeGenerationState.publicAccessors);
UmbrelloSettings::setInlineOps(cppCodeGenerationState.inlineOps);
UmbrelloSettings::setVirtualDestructors(cppCodeGenerationState.virtualDestructors);
UmbrelloSettings::setGetterWithGetPrefix(cppCodeGenerationState.getterWithGetPrefix);
UmbrelloSettings::setRemovePrefixFromAccessorMethods(cppCodeGenerationState.removePrefixFromAccessorMethods);
UmbrelloSettings::setAccessorMethodsStartWithUpperCase(cppCodeGenerationState.accessorMethodsStartWithUpperCase);
UmbrelloSettings::setPackageIsNamespace(cppCodeGenerationState.packageIsNamespace);
UmbrelloSettings::setStringClassName(cppCodeGenerationState.stringClassName);
......
......@@ -183,7 +183,10 @@ public:
publicAccessors(false),
stringIncludeIsGlobal(false),
vectorIncludeIsGlobal(false),
virtualDestructors(false)
virtualDestructors(false),
getterWithGetPrefix(false),
removePrefixFromAccessorMethods(false),
accessorMethodsStartWithUpperCase(false)
{
}
......@@ -200,6 +203,9 @@ public:
QString docToolTag;
bool vectorIncludeIsGlobal;
bool virtualDestructors;
bool getterWithGetPrefix;
bool removePrefixFromAccessorMethods;
bool accessorMethodsStartWithUpperCase;
};
class DCodeGenerationState {
......
......@@ -519,6 +519,21 @@
<whatsthis>Enables/Disables virtual destructors</whatsthis>
<default>true</default>
</entry>
<entry name="getterWithGetPrefix" type="Bool">
<label>Getters with 'get' prefix</label>
<whatsthis>If enabled, create getters with 'get' prefix</whatsthis>
<default>true</default>
</entry>
<entry name="removePrefixFromAccessorMethods" type="Bool">
<label>Remove prefix '[a-zA-Z]_' from accessor methods</label>
<whatsthis>If enabled, remove prefix '[a-zA-Z]_' from accessor methods</whatsthis>
<default>false</default>
</entry>
<entry name="accessorMethodsStartWithUpperCase" type="Bool">
<label>Accessor methods start with capital letters</label>
<whatsthis>If enabled, accessor methods start with capital letters</whatsthis>
<default>true</default>
</entry>
<entry name="docToolTag" type="String">
<label>Documentation tags</label>
<whatsthis>The tag used for generating doxygen documentation (\\, @)</whatsthis>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment