GUI: Library editor - rename function

This commit is contained in:
Daniel Marjamäki 2015-09-08 15:38:02 +02:00
parent 7d229f082c
commit a32aa03035
5 changed files with 71 additions and 24 deletions

View File

@ -4,10 +4,6 @@
#include <QRegExp> #include <QRegExp>
#include <QValidator> #include <QValidator>
#define SIMPLENAME "[_a-zA-Z][_a-zA-Z0-9]*" // just a name
#define SCOPENAME SIMPLENAME "(::" SIMPLENAME ")*" // names with optional scope
#define NAMES SCOPENAME "(," SCOPENAME ")*" // names can be separated by comma
LibraryAddFunctionDialog::LibraryAddFunctionDialog(QWidget *parent) : LibraryAddFunctionDialog::LibraryAddFunctionDialog(QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::LibraryAddFunctionDialog) ui(new Ui::LibraryAddFunctionDialog)

View File

@ -3,6 +3,10 @@
#include <QDialog> #include <QDialog>
#define SIMPLENAME "[_a-zA-Z][_a-zA-Z0-9]*" // just a name
#define SCOPENAME SIMPLENAME "(::" SIMPLENAME ")*" // names with optional scope
#define NAMES SCOPENAME "(," SCOPENAME ")*" // names can be separated by comma
namespace Ui { namespace Ui {
class LibraryAddFunctionDialog; class LibraryAddFunctionDialog;
} }

View File

@ -29,6 +29,20 @@
// TODO: get/compare functions from header // TODO: get/compare functions from header
class FunctionListItem : public QListWidgetItem {
public:
FunctionListItem(QListWidget *view,
CppcheckLibraryData::Function *function,
bool selected)
: QListWidgetItem(view), function(function)
{
setText(function->name);
setFlags(flags() | Qt::ItemIsEditable);
setSelected(selected);
}
CppcheckLibraryData::Function *function;
};
LibraryDialog::LibraryDialog(QWidget *parent) : LibraryDialog::LibraryDialog(QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::LibraryDialog), ui(new Ui::LibraryDialog),
@ -50,11 +64,7 @@ CppcheckLibraryData::Function *LibraryDialog::currentFunction()
QList<QListWidgetItem *> selitems = ui->functions->selectedItems(); QList<QListWidgetItem *> selitems = ui->functions->selectedItems();
if (selitems.count() != 1) if (selitems.count() != 1)
return nullptr; return nullptr;
for (int row = 0; row < data.functions.size(); ++row) { return dynamic_cast<FunctionListItem *>(selitems.first())->function;
if (data.functions[row].name == selitems.front()->text())
return &data.functions[row];
}
return nullptr;
} }
void LibraryDialog::openCfg() void LibraryDialog::openCfg()
@ -80,8 +90,10 @@ void LibraryDialog::openCfg()
ui->buttonSave->setEnabled(false); ui->buttonSave->setEnabled(false);
ui->filter->clear(); ui->filter->clear();
ui->functions->clear(); ui->functions->clear();
foreach(const struct CppcheckLibraryData::Function &function, data.functions) { for (struct CppcheckLibraryData::Function &function : data.functions) {
ui->functions->addItem(function.name); ui->functions->addItem(new FunctionListItem(ui->functions,
&function,
false));
} }
ui->sortFunctions->setEnabled(!data.functions.empty()); ui->sortFunctions->setEnabled(!data.functions.empty());
ui->filter->setEnabled(!data.functions.empty()); ui->filter->setEnabled(!data.functions.empty());
@ -117,7 +129,7 @@ void LibraryDialog::addFunction()
f.args.append(arg); f.args.append(arg);
} }
data.functions.append(f); data.functions.append(f);
ui->functions->addItem(f.name); ui->functions->addItem(new FunctionListItem(ui->functions, &data.functions.back(), false));
ui->buttonSave->setEnabled(true); ui->buttonSave->setEnabled(true);
ui->sortFunctions->setEnabled(!data.functions.empty()); ui->sortFunctions->setEnabled(!data.functions.empty());
ui->filter->setEnabled(!data.functions.empty()); ui->filter->setEnabled(!data.functions.empty());
@ -125,6 +137,24 @@ void LibraryDialog::addFunction()
delete d; delete d;
} }
void LibraryDialog::editFunctionName(QListWidgetItem* item)
{
if (ignoreChanges)
return;
QString functionName = item->text();
CppcheckLibraryData::Function * const function = dynamic_cast<FunctionListItem*>(item)->function;
if (functionName != function->name) {
if (QRegExp(NAMES).exactMatch(functionName)) {
function->name = functionName;
ui->buttonSave->setEnabled(true);
} else {
ignoreChanges = true;
item->setText(function->name);
ignoreChanges = false;
}
}
}
void LibraryDialog::selectFunction() void LibraryDialog::selectFunction()
{ {
const CppcheckLibraryData::Function * const function = currentFunction(); const CppcheckLibraryData::Function * const function = currentFunction();
@ -147,17 +177,16 @@ void LibraryDialog::selectFunction()
void LibraryDialog::sortFunctions(bool sort) void LibraryDialog::sortFunctions(bool sort)
{ {
if (sort) if (sort) {
ui->functions->sortItems(); ui->functions->sortItems();
else { } else {
ignoreChanges = true; ignoreChanges = true;
CppcheckLibraryData::Function *selfunction = currentFunction(); CppcheckLibraryData::Function *selfunction = currentFunction();
ui->functions->clear(); ui->functions->clear();
foreach(const struct CppcheckLibraryData::Function &function, data.functions) { for (struct CppcheckLibraryData::Function &function : data.functions) {
QListWidgetItem *item = new QListWidgetItem(ui->functions); ui->functions->addItem(new FunctionListItem(ui->functions,
item->setText(function.name); &function,
item->setSelected(selfunction == &function); selfunction == &function));
ui->functions->addItem(item);
} }
if (!ui->filter->text().isEmpty()) if (!ui->filter->text().isEmpty())
filterFunctions(ui->filter->text()); filterFunctions(ui->filter->text());

View File

@ -43,6 +43,7 @@ private slots:
void addFunction(); void addFunction();
void changeFunction(); void changeFunction();
void editArg(); void editArg();
void editFunctionName(QListWidgetItem*);
void filterFunctions(QString); void filterFunctions(QString);
void selectFunction(); void selectFunction();
void sortFunctions(bool); void sortFunctions(bool);

View File

@ -89,7 +89,7 @@
<item> <item>
<widget class="QListWidget" name="functions"> <widget class="QListWidget" name="functions">
<property name="editTriggers"> <property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set> <set>QAbstractItemView::DoubleClicked</set>
</property> </property>
<property name="selectionBehavior"> <property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum> <enum>QAbstractItemView::SelectRows</enum>
@ -313,8 +313,8 @@
<slot>editArg()</slot> <slot>editArg()</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>488</x> <x>519</x>
<y>580</y> <y>575</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>497</x> <x>497</x>
@ -361,8 +361,8 @@
<slot>filterFunctions(QString)</slot> <slot>filterFunctions(QString)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>257</x> <x>429</x>
<y>565</y> <y>575</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>273</x> <x>273</x>
@ -402,6 +402,22 @@
</hint> </hint>
</hints> </hints>
</connection> </connection>
<connection>
<sender>functions</sender>
<signal>itemChanged(QListWidgetItem*)</signal>
<receiver>LibraryDialog</receiver>
<slot>editFunctionName(QListWidgetItem*)</slot>
<hints>
<hint type="sourcelabel">
<x>217</x>
<y>104</y>
</hint>
<hint type="destinationlabel">
<x>235</x>
<y>104</y>
</hint>
</hints>
</connection>
</connections> </connections>
<slots> <slots>
<slot>addFunction()</slot> <slot>addFunction()</slot>
@ -413,5 +429,6 @@
<slot>saveCfg()</slot> <slot>saveCfg()</slot>
<slot>selectFunction()</slot> <slot>selectFunction()</slot>
<slot>sortFunctions(bool)</slot> <slot>sortFunctions(bool)</slot>
<slot>editFunctionName(QListWidgetItem*)</slot>
</slots> </slots>
</ui> </ui>