Skip to content

Commit a32aa03

Browse files
committed
GUI: Library editor - rename function
1 parent 7d229f0 commit a32aa03

5 files changed

Lines changed: 71 additions & 24 deletions

File tree

gui/libraryaddfunctiondialog.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
#include <QRegExp>
55
#include <QValidator>
66

7-
#define SIMPLENAME "[_a-zA-Z][_a-zA-Z0-9]*" // just a name
8-
#define SCOPENAME SIMPLENAME "(::" SIMPLENAME ")*" // names with optional scope
9-
#define NAMES SCOPENAME "(," SCOPENAME ")*" // names can be separated by comma
10-
117
LibraryAddFunctionDialog::LibraryAddFunctionDialog(QWidget *parent) :
128
QDialog(parent),
139
ui(new Ui::LibraryAddFunctionDialog)

gui/libraryaddfunctiondialog.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
#include <QDialog>
55

6+
#define SIMPLENAME "[_a-zA-Z][_a-zA-Z0-9]*" // just a name
7+
#define SCOPENAME SIMPLENAME "(::" SIMPLENAME ")*" // names with optional scope
8+
#define NAMES SCOPENAME "(," SCOPENAME ")*" // names can be separated by comma
9+
610
namespace Ui {
711
class LibraryAddFunctionDialog;
812
}

gui/librarydialog.cpp

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@
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+
3246
LibraryDialog::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

6070
void 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+
128158
void LibraryDialog::selectFunction()
129159
{
130160
const CppcheckLibraryData::Function * const function = currentFunction();
@@ -147,17 +177,16 @@ void LibraryDialog::selectFunction()
147177

148178
void 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());

gui/librarydialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ private slots:
4343
void addFunction();
4444
void changeFunction();
4545
void editArg();
46+
void editFunctionName(QListWidgetItem*);
4647
void filterFunctions(QString);
4748
void selectFunction();
4849
void sortFunctions(bool);

gui/librarydialog.ui

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
<item>
9090
<widget class="QListWidget" name="functions">
9191
<property name="editTriggers">
92-
<set>QAbstractItemView::NoEditTriggers</set>
92+
<set>QAbstractItemView::DoubleClicked</set>
9393
</property>
9494
<property name="selectionBehavior">
9595
<enum>QAbstractItemView::SelectRows</enum>
@@ -313,8 +313,8 @@
313313
<slot>editArg()</slot>
314314
<hints>
315315
<hint type="sourcelabel">
316-
<x>488</x>
317-
<y>580</y>
316+
<x>519</x>
317+
<y>575</y>
318318
</hint>
319319
<hint type="destinationlabel">
320320
<x>497</x>
@@ -361,8 +361,8 @@
361361
<slot>filterFunctions(QString)</slot>
362362
<hints>
363363
<hint type="sourcelabel">
364-
<x>257</x>
365-
<y>565</y>
364+
<x>429</x>
365+
<y>575</y>
366366
</hint>
367367
<hint type="destinationlabel">
368368
<x>273</x>
@@ -402,6 +402,22 @@
402402
</hint>
403403
</hints>
404404
</connection>
405+
<connection>
406+
<sender>functions</sender>
407+
<signal>itemChanged(QListWidgetItem*)</signal>
408+
<receiver>LibraryDialog</receiver>
409+
<slot>editFunctionName(QListWidgetItem*)</slot>
410+
<hints>
411+
<hint type="sourcelabel">
412+
<x>217</x>
413+
<y>104</y>
414+
</hint>
415+
<hint type="destinationlabel">
416+
<x>235</x>
417+
<y>104</y>
418+
</hint>
419+
</hints>
420+
</connection>
405421
</connections>
406422
<slots>
407423
<slot>addFunction()</slot>
@@ -413,5 +429,6 @@
413429
<slot>saveCfg()</slot>
414430
<slot>selectFunction()</slot>
415431
<slot>sortFunctions(bool)</slot>
432+
<slot>editFunctionName(QListWidgetItem*)</slot>
416433
</slots>
417434
</ui>

0 commit comments

Comments
 (0)