diff --git a/gui/libraryaddfunctiondialog.cpp b/gui/libraryaddfunctiondialog.cpp index 941fb2fb4..d2dfb3944 100644 --- a/gui/libraryaddfunctiondialog.cpp +++ b/gui/libraryaddfunctiondialog.cpp @@ -4,10 +4,6 @@ #include #include -#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) : QDialog(parent), ui(new Ui::LibraryAddFunctionDialog) diff --git a/gui/libraryaddfunctiondialog.h b/gui/libraryaddfunctiondialog.h index de63f1d37..465c567af 100644 --- a/gui/libraryaddfunctiondialog.h +++ b/gui/libraryaddfunctiondialog.h @@ -3,6 +3,10 @@ #include +#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 { class LibraryAddFunctionDialog; } diff --git a/gui/librarydialog.cpp b/gui/librarydialog.cpp index 037fbe8fc..a34a392c4 100644 --- a/gui/librarydialog.cpp +++ b/gui/librarydialog.cpp @@ -29,6 +29,20 @@ // 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) : QDialog(parent), ui(new Ui::LibraryDialog), @@ -50,11 +64,7 @@ CppcheckLibraryData::Function *LibraryDialog::currentFunction() QList selitems = ui->functions->selectedItems(); if (selitems.count() != 1) return nullptr; - for (int row = 0; row < data.functions.size(); ++row) { - if (data.functions[row].name == selitems.front()->text()) - return &data.functions[row]; - } - return nullptr; + return dynamic_cast(selitems.first())->function; } void LibraryDialog::openCfg() @@ -80,8 +90,10 @@ void LibraryDialog::openCfg() ui->buttonSave->setEnabled(false); ui->filter->clear(); ui->functions->clear(); - foreach(const struct CppcheckLibraryData::Function &function, data.functions) { - ui->functions->addItem(function.name); + for (struct CppcheckLibraryData::Function &function : data.functions) { + ui->functions->addItem(new FunctionListItem(ui->functions, + &function, + false)); } ui->sortFunctions->setEnabled(!data.functions.empty()); ui->filter->setEnabled(!data.functions.empty()); @@ -117,7 +129,7 @@ void LibraryDialog::addFunction() f.args.append(arg); } 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->sortFunctions->setEnabled(!data.functions.empty()); ui->filter->setEnabled(!data.functions.empty()); @@ -125,6 +137,24 @@ void LibraryDialog::addFunction() delete d; } +void LibraryDialog::editFunctionName(QListWidgetItem* item) +{ + if (ignoreChanges) + return; + QString functionName = item->text(); + CppcheckLibraryData::Function * const function = dynamic_cast(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() { const CppcheckLibraryData::Function * const function = currentFunction(); @@ -147,17 +177,16 @@ void LibraryDialog::selectFunction() void LibraryDialog::sortFunctions(bool sort) { - if (sort) + if (sort) { ui->functions->sortItems(); - else { + } else { ignoreChanges = true; CppcheckLibraryData::Function *selfunction = currentFunction(); ui->functions->clear(); - foreach(const struct CppcheckLibraryData::Function &function, data.functions) { - QListWidgetItem *item = new QListWidgetItem(ui->functions); - item->setText(function.name); - item->setSelected(selfunction == &function); - ui->functions->addItem(item); + for (struct CppcheckLibraryData::Function &function : data.functions) { + ui->functions->addItem(new FunctionListItem(ui->functions, + &function, + selfunction == &function)); } if (!ui->filter->text().isEmpty()) filterFunctions(ui->filter->text()); diff --git a/gui/librarydialog.h b/gui/librarydialog.h index 07dfda5ba..46472e195 100644 --- a/gui/librarydialog.h +++ b/gui/librarydialog.h @@ -43,6 +43,7 @@ private slots: void addFunction(); void changeFunction(); void editArg(); + void editFunctionName(QListWidgetItem*); void filterFunctions(QString); void selectFunction(); void sortFunctions(bool); diff --git a/gui/librarydialog.ui b/gui/librarydialog.ui index 020796516..460bf5d11 100644 --- a/gui/librarydialog.ui +++ b/gui/librarydialog.ui @@ -89,7 +89,7 @@ - QAbstractItemView::NoEditTriggers + QAbstractItemView::DoubleClicked QAbstractItemView::SelectRows @@ -313,8 +313,8 @@ editArg() - 488 - 580 + 519 + 575 497 @@ -361,8 +361,8 @@ filterFunctions(QString) - 257 - 565 + 429 + 575 273 @@ -402,6 +402,22 @@ + + functions + itemChanged(QListWidgetItem*) + LibraryDialog + editFunctionName(QListWidgetItem*) + + + 217 + 104 + + + 235 + 104 + + + addFunction() @@ -413,5 +429,6 @@ saveCfg() selectFunction() sortFunctions(bool) + editFunctionName(QListWidgetItem*)