27 #include "ui_librarydialog.h"
32 #include <QFileDialog>
37 #include <QListWidget>
38 #include <QListWidgetItem>
39 #include <QMessageBox>
40 #include <QPlainTextEdit>
41 #include <QPushButton>
42 #include <QRegularExpression>
43 #include <QTextStream>
51 class FunctionListItem :
public QListWidgetItem {
53 FunctionListItem(QListWidget *view,
56 : QListWidgetItem(view), function(function) {
57 setText(function->name);
58 setFlags(flags() | Qt::ItemIsEditable);
59 setSelected(selected);
70 mUi->buttonSave->setEnabled(
false);
71 mUi->buttonSaveAs->setEnabled(
false);
72 mUi->sortFunctions->setEnabled(
false);
73 mUi->filter->setEnabled(
false);
74 mUi->addFunction->setEnabled(
false);
87 QList<QListWidgetItem *> selitems =
mUi->functions->selectedItems();
88 if (selitems.count() != 1)
90 return static_cast<FunctionListItem *
>(selitems.first())->
function;
97 QString selectedFilter;
98 const QString filter(tr(
"Library files (*.cfg)"));
99 const QString selectedFile = QFileDialog::getOpenFileName(
this,
100 tr(
"Open library file"),
105 if (selectedFile.isEmpty())
108 QFile file(selectedFile);
109 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
110 QMessageBox msg(QMessageBox::Critical,
112 tr(
"Cannot open file %1.").arg(selectedFile),
120 const QString errmsg = tempdata.
open(file);
121 if (!errmsg.isNull()) {
122 QMessageBox msg(QMessageBox::Critical,
124 tr(
"Failed to load %1. %2.").arg(selectedFile).arg(errmsg),
134 mUi->buttonSave->setEnabled(
false);
135 mUi->buttonSaveAs->setEnabled(
true);
136 mUi->filter->clear();
137 mUi->functions->clear();
139 mUi->functions->addItem(
new FunctionListItem(
mUi->functions,
145 mUi->addFunction->setEnabled(
true);
154 if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
155 QTextStream ts(&file);
157 mUi->buttonSave->setEnabled(
false);
159 QMessageBox msg(QMessageBox::Critical,
161 tr(
"Cannot save file %1.").arg(
mFileName),
170 const QString filter(tr(
"Library files (*.cfg)"));
172 QString selectedFile = QFileDialog::getSaveFileName(
this,
173 tr(
"Save the library as"),
176 if (selectedFile.isEmpty())
179 if (!selectedFile.endsWith(
".cfg", Qt::CaseInsensitive))
180 selectedFile +=
".cfg";
189 if (d->exec() == QDialog::Accepted && !d->functionName().isEmpty()) {
192 f.
name = d->functionName();
193 const int args = d->numberOfArguments();
195 for (
int i = 1; i <= args; i++) {
202 mUi->buttonSave->setEnabled(
true);
213 QString functionName = item->text();
215 if (functionName != function->name) {
216 const QRegularExpressionMatch matchRes = QRegularExpression(
"^" NAMES "$").match(functionName);
217 if (matchRes.hasMatch()) {
218 function->
name = functionName;
219 mUi->buttonSave->setEnabled(
true);
222 item->setText(function->name);
232 if (
function ==
nullptr) {
233 mUi->comments->clear();
234 mUi->comments->setEnabled(
false);
236 mUi->noreturn->setCurrentIndex(0);
237 mUi->noreturn->setEnabled(
false);
239 mUi->useretval->setChecked(
false);
240 mUi->useretval->setEnabled(
false);
242 mUi->leakignore->setChecked(
false);
243 mUi->leakignore->setEnabled(
false);
245 mUi->arguments->clear();
246 mUi->arguments->setEnabled(
false);
248 mUi->editArgButton->setEnabled(
false);
253 mUi->comments->setPlainText(function->comments);
254 mUi->comments->setEnabled(
true);
256 mUi->noreturn->setCurrentIndex(function->noreturn);
257 mUi->noreturn->setEnabled(
true);
259 mUi->useretval->setChecked(function->useretval);
260 mUi->useretval->setEnabled(
true);
262 mUi->leakignore->setChecked(function->leakignore);
263 mUi->leakignore->setEnabled(
true);
266 mUi->arguments->setEnabled(
true);
268 mUi->editArgButton->setEnabled(
true);
275 mUi->functions->sortItems();
279 mUi->functions->clear();
281 mUi->functions->addItem(
new FunctionListItem(
mUi->functions,
283 selfunction == &
function));
285 if (!
mUi->filter->text().isEmpty())
293 QList<QListWidgetItem *> allItems =
mUi->functions->findItems(QString(), Qt::MatchContains);
295 if (filter.isEmpty()) {
296 for (QListWidgetItem *item : allItems) {
297 item->setHidden(
false);
300 for (QListWidgetItem *item : allItems) {
301 item->setHidden(!item->text().startsWith(filter));
315 function->comments =
mUi->comments->toPlainText();
317 function->useretval =
mUi->useretval->isChecked();
318 function->leakignore =
mUi->leakignore->isChecked();
320 mUi->buttonSave->setEnabled(
true);
329 if (
mUi->arguments->selectedItems().count() != 1)
334 if (d.exec() == QDialog::Accepted) {
335 const unsigned number = arg.
nr;
338 mUi->arguments->selectedItems().first()->setText(
getArgText(arg));
340 mUi->buttonSave->setEnabled(
true);
347 s += QString::number(arg.
nr);
354 s +=
"\n valid: " + QString(arg.
valid.isEmpty() ?
"any" : arg.
valid);
356 s +=
"\n minsize: " + minsize.
type +
" " + minsize.
arg +
" " + minsize.
arg2;
363 mUi->arguments->clear();
QString open(QIODevice &file)
QList< Function > functions
void swap(CppcheckLibraryData &other)
static QString getArgText(const CppcheckLibraryData::Function::Arg &arg)
~LibraryDialog() override
CppcheckLibraryData::Function * currentFunction()
CppcheckLibraryData mData
void editFunctionName(QListWidgetItem *)
void updateArguments(const CppcheckLibraryData::Function &function)
LibraryDialog(QWidget *parent=nullptr)
void filterFunctions(const QString &)
CppcheckLibraryData::Function::Arg getArg() const
static std::string getPathFromFilename(const std::string &filename)
Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')
QString getDataDir()
Get configured data dir.
static const unsigned int ANY
QList< MinSize > minsizes
static const char * bool_to_string(bool b)