2929
3030// TODO: get/compare functions from header
3131
32+ class FunctionListItem : public QListWidgetItem {
33+ public:
34+ FunctionListItem (QListWidget *view,
35+ CppcheckLibraryData::Function *function,
36+ bool selected)
37+ : QListWidgetItem(view), function(function)
38+ {
39+ setText (function->name );
40+ setFlags (flags () | Qt::ItemIsEditable);
41+ setSelected (selected);
42+ }
43+ CppcheckLibraryData::Function *function;
44+ };
45+
3246LibraryDialog::LibraryDialog (QWidget *parent) :
3347 QDialog(parent),
3448 ui(new Ui::LibraryDialog),
@@ -50,11 +64,7 @@ CppcheckLibraryData::Function *LibraryDialog::currentFunction()
5064 QList<QListWidgetItem *> selitems = ui->functions ->selectedItems ();
5165 if (selitems.count () != 1 )
5266 return nullptr ;
53- for (int row = 0 ; row < data.functions .size (); ++row) {
54- if (data.functions [row].name == selitems.front ()->text ())
55- return &data.functions [row];
56- }
57- return nullptr ;
67+ return dynamic_cast <FunctionListItem *>(selitems.first ())->function ;
5868}
5969
6070void LibraryDialog::openCfg ()
@@ -80,8 +90,10 @@ void LibraryDialog::openCfg()
8090 ui->buttonSave ->setEnabled (false );
8191 ui->filter ->clear ();
8292 ui->functions ->clear ();
83- foreach (const struct CppcheckLibraryData ::Function &function, data.functions) {
84- ui->functions ->addItem (function.name );
93+ for (struct CppcheckLibraryData ::Function &function : data.functions) {
94+ ui->functions ->addItem (new FunctionListItem (ui->functions ,
95+ &function,
96+ false ));
8597 }
8698 ui->sortFunctions ->setEnabled (!data.functions .empty ());
8799 ui->filter ->setEnabled (!data.functions .empty ());
@@ -117,14 +129,32 @@ void LibraryDialog::addFunction()
117129 f.args .append (arg);
118130 }
119131 data.functions .append (f);
120- ui->functions ->addItem (f. name );
132+ ui->functions ->addItem (new FunctionListItem (ui-> functions , &data. functions . back (), false ) );
121133 ui->buttonSave ->setEnabled (true );
122134 ui->sortFunctions ->setEnabled (!data.functions .empty ());
123135 ui->filter ->setEnabled (!data.functions .empty ());
124136 }
125137 delete d;
126138}
127139
140+ void LibraryDialog::editFunctionName (QListWidgetItem* item)
141+ {
142+ if (ignoreChanges)
143+ return ;
144+ QString functionName = item->text ();
145+ CppcheckLibraryData::Function * const function = dynamic_cast <FunctionListItem*>(item)->function ;
146+ if (functionName != function->name ) {
147+ if (QRegExp (NAMES).exactMatch (functionName)) {
148+ function->name = functionName;
149+ ui->buttonSave ->setEnabled (true );
150+ } else {
151+ ignoreChanges = true ;
152+ item->setText (function->name );
153+ ignoreChanges = false ;
154+ }
155+ }
156+ }
157+
128158void LibraryDialog::selectFunction ()
129159{
130160 const CppcheckLibraryData::Function * const function = currentFunction ();
@@ -147,17 +177,16 @@ void LibraryDialog::selectFunction()
147177
148178void LibraryDialog::sortFunctions (bool sort)
149179{
150- if (sort)
180+ if (sort) {
151181 ui->functions ->sortItems ();
152- else {
182+ } else {
153183 ignoreChanges = true ;
154184 CppcheckLibraryData::Function *selfunction = currentFunction ();
155185 ui->functions ->clear ();
156- foreach (const struct CppcheckLibraryData ::Function &function, data.functions) {
157- QListWidgetItem *item = new QListWidgetItem (ui->functions );
158- item->setText (function.name );
159- item->setSelected (selfunction == &function);
160- ui->functions ->addItem (item);
186+ for (struct CppcheckLibraryData ::Function &function : data.functions) {
187+ ui->functions ->addItem (new FunctionListItem (ui->functions ,
188+ &function,
189+ selfunction == &function));
161190 }
162191 if (!ui->filter ->text ().isEmpty ())
163192 filterFunctions (ui->filter ->text ());
0 commit comments