mirror of
https://github.com/subsurface/subsurface.git
synced 2025-01-19 14:25:27 +00:00
core: port filterpreset.cpp to std::string
Less memory management hassle. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
2e1d852e36
commit
119fe908c7
11 changed files with 44 additions and 57 deletions
|
@ -514,7 +514,7 @@ ImportDives::ImportDives(struct divelog *log, int flags, const QString &source)
|
|||
// When encountering filter presets with equal names, check whether they are
|
||||
// the same. If they are, ignore them.
|
||||
for (const filter_preset &preset: *log->filter_presets) {
|
||||
QString name = preset.name;
|
||||
std::string name = preset.name;
|
||||
auto it = std::find_if(divelog.filter_presets->begin(), divelog.filter_presets->end(),
|
||||
[&name](const filter_preset &preset) { return preset.name == name; });
|
||||
if (it != divelog.filter_presets->end() && it->data == preset.data)
|
||||
|
@ -580,7 +580,7 @@ void ImportDives::undoit()
|
|||
// Remove filter presets. Do this in reverse order.
|
||||
for (auto it = filterPresetsToRemove.rbegin(); it != filterPresetsToRemove.rend(); ++it) {
|
||||
int index = *it;
|
||||
QString oldName = filter_preset_name_qstring(index);
|
||||
std::string oldName = filter_preset_name(index);
|
||||
FilterData oldData = filter_preset_get(index);
|
||||
filter_preset_delete(index);
|
||||
emit diveListNotifier.filterPresetRemoved(index);
|
||||
|
|
|
@ -112,7 +112,7 @@ private:
|
|||
|
||||
// For redo
|
||||
std::vector<OwningDiveSitePtr> sitesToAdd;
|
||||
std::vector<std::pair<QString,FilterData>>
|
||||
std::vector<std::pair<std::string,FilterData>>
|
||||
filterPresetsToAdd;
|
||||
|
||||
// For undo
|
||||
|
|
|
@ -6,16 +6,16 @@
|
|||
|
||||
namespace Command {
|
||||
|
||||
static int createFilterPreset(const QString &name, const FilterData &data)
|
||||
static int createFilterPreset(const std::string &name, const FilterData &data)
|
||||
{
|
||||
int index = filter_preset_add(name, data);
|
||||
emit diveListNotifier.filterPresetAdded(index);
|
||||
return index;
|
||||
}
|
||||
|
||||
static std::pair<QString, FilterData> removeFilterPreset(int index)
|
||||
static std::pair<std::string, FilterData> removeFilterPreset(int index)
|
||||
{
|
||||
QString oldName = filter_preset_name_qstring(index);
|
||||
std::string oldName = filter_preset_name(index);
|
||||
FilterData oldData = filter_preset_get(index);
|
||||
filter_preset_delete(index);
|
||||
emit diveListNotifier.filterPresetRemoved(index);
|
||||
|
@ -23,7 +23,7 @@ static std::pair<QString, FilterData> removeFilterPreset(int index)
|
|||
}
|
||||
|
||||
CreateFilterPreset::CreateFilterPreset(const QString &nameIn, const FilterData &dataIn) :
|
||||
name(nameIn), data(dataIn), index(0)
|
||||
name(nameIn.toStdString()), data(dataIn), index(0)
|
||||
{
|
||||
setText(Command::Base::tr("Create filter preset %1").arg(nameIn));
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ void CreateFilterPreset::undo()
|
|||
|
||||
RemoveFilterPreset::RemoveFilterPreset(int indexIn) : index(indexIn)
|
||||
{
|
||||
setText(Command::Base::tr("Delete filter preset %1").arg(filter_preset_name_qstring(index)));
|
||||
setText(Command::Base::tr("Delete filter preset %1").arg(QString(filter_preset_name(index).c_str())));
|
||||
}
|
||||
|
||||
bool RemoveFilterPreset::workToBeDone()
|
||||
|
@ -68,7 +68,7 @@ void RemoveFilterPreset::undo()
|
|||
EditFilterPreset::EditFilterPreset(int indexIn, const FilterData &dataIn) :
|
||||
index(indexIn), data(dataIn)
|
||||
{
|
||||
setText(Command::Base::tr("Edit filter preset %1").arg(filter_preset_name_qstring(index)));
|
||||
setText(Command::Base::tr("Edit filter preset %1").arg(QString(filter_preset_name(index).c_str())));
|
||||
}
|
||||
|
||||
bool EditFilterPreset::workToBeDone()
|
||||
|
|
|
@ -17,7 +17,7 @@ public:
|
|||
CreateFilterPreset(const QString &name, const FilterData &data);
|
||||
private:
|
||||
// for redo
|
||||
QString name;
|
||||
std::string name;
|
||||
FilterData data;
|
||||
|
||||
// for undo
|
||||
|
@ -33,7 +33,7 @@ public:
|
|||
RemoveFilterPreset(int index);
|
||||
private:
|
||||
// for undo
|
||||
QString name;
|
||||
std::string name;
|
||||
FilterData data;
|
||||
|
||||
// for redo
|
||||
|
|
|
@ -14,14 +14,9 @@ extern "C" int filter_presets_count(void)
|
|||
return (int)global_table().size();
|
||||
}
|
||||
|
||||
extern "C" char *filter_preset_name(int preset)
|
||||
extern std::string filter_preset_fulltext_query(int preset)
|
||||
{
|
||||
return copy_qstring(filter_preset_name_qstring(preset));
|
||||
}
|
||||
|
||||
extern "C" char *filter_preset_fulltext_query(int preset)
|
||||
{
|
||||
return copy_qstring(global_table()[preset].data.fullText.originalQuery);
|
||||
return global_table()[preset].data.fullText.originalQuery.toStdString();
|
||||
}
|
||||
|
||||
extern "C" const char *filter_preset_fulltext_mode(int preset)
|
||||
|
@ -73,7 +68,7 @@ extern "C" void filter_preset_set_name(struct filter_preset *preset, const char
|
|||
preset->name = name;
|
||||
}
|
||||
|
||||
static int filter_preset_add_to_table(const QString &name, const FilterData &d, struct filter_preset_table &table)
|
||||
static int filter_preset_add_to_table(const std::string name, const FilterData &d, struct filter_preset_table &table)
|
||||
{
|
||||
// std::lower_bound does a binary search - the vector must be sorted.
|
||||
filter_preset newEntry { name, d };
|
||||
|
@ -85,14 +80,14 @@ static int filter_preset_add_to_table(const QString &name, const FilterData &d,
|
|||
}
|
||||
|
||||
// Take care that the name doesn't already exist by adding numbers
|
||||
static QString get_unique_preset_name(const QString &orig, const struct filter_preset_table &table)
|
||||
static std::string get_unique_preset_name(const std::string &orig, const struct filter_preset_table &table)
|
||||
{
|
||||
QString res = orig;
|
||||
std::string res = orig;
|
||||
int count = 2;
|
||||
while (std::find_if(table.begin(), table.end(),
|
||||
[&res](const filter_preset &preset)
|
||||
{ return preset.name == res; }) != table.end()) {
|
||||
res = orig + "#" + QString::number(count);
|
||||
res = orig + "#" + std::to_string(count);
|
||||
++count;
|
||||
}
|
||||
return res;
|
||||
|
@ -100,7 +95,7 @@ static QString get_unique_preset_name(const QString &orig, const struct filter_p
|
|||
|
||||
extern "C" void add_filter_preset_to_table(const struct filter_preset *preset, struct filter_preset_table *table)
|
||||
{
|
||||
QString name = get_unique_preset_name(preset->name, *table);
|
||||
std::string name = get_unique_preset_name(preset->name, *table);
|
||||
filter_preset_add_to_table(name, preset->data, *table);
|
||||
}
|
||||
|
||||
|
@ -110,14 +105,14 @@ extern "C" void filter_preset_add_constraint(struct filter_preset *preset, const
|
|||
preset->data.constraints.emplace_back(type, string_mode, range_mode, negate, data);
|
||||
}
|
||||
|
||||
int filter_preset_id(const QString &name)
|
||||
int filter_preset_id(const std::string &name)
|
||||
{
|
||||
auto it = std::find_if(global_table().begin(), global_table().end(),
|
||||
[&name] (filter_preset &p) { return p.name == name; });
|
||||
return it != global_table().end() ? it - global_table().begin() : -1;
|
||||
}
|
||||
|
||||
QString filter_preset_name_qstring(int preset)
|
||||
std::string filter_preset_name(int preset)
|
||||
{
|
||||
return global_table()[preset].name;
|
||||
}
|
||||
|
@ -132,9 +127,9 @@ FilterData filter_preset_get(int preset)
|
|||
return global_table()[preset].data;
|
||||
}
|
||||
|
||||
int filter_preset_add(const QString &nameIn, const FilterData &d)
|
||||
int filter_preset_add(const std::string &nameIn, const FilterData &d)
|
||||
{
|
||||
QString name = get_unique_preset_name(nameIn, global_table());
|
||||
std::string name = get_unique_preset_name(nameIn, global_table());
|
||||
return filter_preset_add_to_table(name, d, global_table());
|
||||
}
|
||||
|
||||
|
|
|
@ -18,9 +18,9 @@ struct filter_constraint;
|
|||
#ifdef __cplusplus
|
||||
#include "divefilter.h"
|
||||
#include <vector>
|
||||
#include <QStringList>
|
||||
#include <string>
|
||||
struct filter_preset {
|
||||
QString name;
|
||||
std::string name;
|
||||
FilterData data;
|
||||
};
|
||||
|
||||
|
@ -44,8 +44,6 @@ extern "C" {
|
|||
|
||||
// The C IO code accesses the filter presets via integer indices.
|
||||
extern int filter_presets_count(void);
|
||||
extern char *filter_preset_name(int preset); // name of filter preset - caller must free the result.
|
||||
extern char *filter_preset_fulltext_query(int preset); // fulltext query of filter preset - caller must free the result.
|
||||
extern const char *filter_preset_fulltext_mode(int preset); // string mode of fulltext query. ownership is *not* passed to caller.
|
||||
extern int filter_preset_constraint_count(int preset); // number of constraints in the filter preset.
|
||||
extern const struct filter_constraint *filter_preset_constraint(int preset, int constraint); // get constraint. ownership is *not* passed to caller.
|
||||
|
@ -66,12 +64,13 @@ extern void filter_preset_add_constraint(struct filter_preset *preset, const cha
|
|||
|
||||
struct FilterData;
|
||||
|
||||
int filter_preset_id(const QString &s); // for now, we assume that names are unique. returns -1 if no preset with that name.
|
||||
QString filter_preset_name_qstring(int preset);
|
||||
int filter_preset_id(const std::string &s); // for now, we assume that names are unique. returns -1 if no preset with that name.
|
||||
void filter_preset_set(int preset, const FilterData &d); // this will override a preset if the name already exists.
|
||||
FilterData filter_preset_get(int preset);
|
||||
int filter_preset_add(const QString &name, const FilterData &d); // returns point of insertion
|
||||
int filter_preset_add(const std::string &name, const FilterData &d); // returns point of insertion
|
||||
void filter_preset_delete(int preset);
|
||||
std::string filter_preset_name(int preset); // name of filter preset - caller must free the result.
|
||||
std::string filter_preset_fulltext_query(int preset); // fulltext query of filter preset - caller must free the result.
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -984,18 +984,14 @@ static void format_one_filter_constraint(int preset_id, int constraint_id, struc
|
|||
*/
|
||||
static void format_one_filter_preset(int preset_id, struct membuffer *b)
|
||||
{
|
||||
char *name, *fulltext;
|
||||
std::string name = filter_preset_name(preset_id);
|
||||
show_utf8(b, "name ", name.c_str(), "\n");
|
||||
|
||||
name = filter_preset_name(preset_id);
|
||||
show_utf8(b, "name ", name, "\n");
|
||||
free(name);
|
||||
|
||||
fulltext = filter_preset_fulltext_query(preset_id);
|
||||
if (!empty_string(fulltext)) {
|
||||
std::string fulltext = filter_preset_fulltext_query(preset_id);
|
||||
if (!fulltext.empty()) {
|
||||
show_utf8(b, "fulltext mode=", filter_preset_fulltext_mode(preset_id), "");
|
||||
show_utf8(b, " query=", fulltext, "\n");
|
||||
show_utf8(b, " query=", fulltext.c_str(), "\n");
|
||||
}
|
||||
free(fulltext);
|
||||
|
||||
for (int i = 0; i < filter_preset_constraint_count(preset_id); i++)
|
||||
format_one_filter_constraint(preset_id, i, b);
|
||||
|
|
|
@ -641,20 +641,17 @@ static void save_filter_presets(struct membuffer *b)
|
|||
return;
|
||||
put_format(b, "<filterpresets>\n");
|
||||
for (i = 0; i < filter_presets_count(); i++) {
|
||||
char *name, *fulltext;
|
||||
name = filter_preset_name(i);
|
||||
std::string name = filter_preset_name(i);
|
||||
put_format(b, " <filterpreset");
|
||||
show_utf8(b, name, " name='", "'", 1);
|
||||
show_utf8(b, name.c_str(), " name='", "'", 1);
|
||||
put_format(b, ">\n");
|
||||
free(name);
|
||||
|
||||
fulltext = filter_preset_fulltext_query(i);
|
||||
if (!empty_string(fulltext)) {
|
||||
std::string fulltext = filter_preset_fulltext_query(i);
|
||||
if (!fulltext.empty()) {
|
||||
const char *fulltext_mode = filter_preset_fulltext_mode(i);
|
||||
show_utf8(b, fulltext_mode, " <fulltext mode='", "'>", 1);
|
||||
show_utf8(b, fulltext, "", "</fulltext>\n", 0);
|
||||
show_utf8(b, fulltext.c_str(), "", "</fulltext>\n", 0);
|
||||
}
|
||||
free(fulltext);
|
||||
|
||||
for (int j = 0; j < filter_preset_constraint_count(i); j++) {
|
||||
const struct filter_constraint *constraint = filter_preset_constraint(i, j);
|
||||
|
|
|
@ -227,7 +227,7 @@ void FilterWidget::updatePresetLabel()
|
|||
int presetId = selectedPreset();
|
||||
QString text;
|
||||
if (presetId >= 0) {
|
||||
text = filter_preset_name_qstring(presetId);
|
||||
text = QString(filter_preset_name(presetId).c_str());
|
||||
if (presetModified)
|
||||
text += " (" + tr("modified") + ")";
|
||||
}
|
||||
|
@ -240,13 +240,13 @@ void FilterWidget::on_addSetButton_clicked()
|
|||
// Thus, if the user selects an item and modify the filter,
|
||||
// they can simply overwrite the preset.
|
||||
int presetId = selectedPreset();
|
||||
QString selectedPreset = presetId >= 0 ? filter_preset_name_qstring(presetId) : QString();
|
||||
QString selectedPreset = presetId >= 0 ? QString(filter_preset_name(presetId).c_str()) : QString();
|
||||
|
||||
AddFilterPresetDialog dialog(selectedPreset, this);
|
||||
QString name = dialog.doit();
|
||||
if (name.isEmpty())
|
||||
return;
|
||||
int idx = filter_preset_id(name);
|
||||
int idx = filter_preset_id(name.toStdString());
|
||||
if (idx >= 0)
|
||||
Command::editFilterPreset(idx, createFilterData());
|
||||
else
|
||||
|
|
|
@ -385,7 +385,7 @@ AddFilterPresetDialog::AddFilterPresetDialog(const QString &defaultName, QWidget
|
|||
int count = filter_presets_count();
|
||||
presets.reserve(count);
|
||||
for (int i = 0; i < count; ++i)
|
||||
presets.push_back(filter_preset_name_qstring(i));
|
||||
presets.push_back(QString(filter_preset_name(i).c_str()));
|
||||
QCompleter *completer = new QCompleter(presets, this);
|
||||
completer->setCaseSensitivity(Qt::CaseInsensitive);
|
||||
ui.name->setCompleter(completer);
|
||||
|
@ -395,7 +395,7 @@ void AddFilterPresetDialog::nameChanged(const QString &text)
|
|||
{
|
||||
QString trimmed = text.trimmed();
|
||||
bool isEmpty = trimmed.isEmpty();
|
||||
bool exists = !isEmpty && filter_preset_id(trimmed) >= 0;
|
||||
bool exists = !isEmpty && filter_preset_id(trimmed.toStdString()) >= 0;
|
||||
ui.duplicateWarning->setVisible(exists);
|
||||
ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!isEmpty);
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ QVariant FilterPresetModel::data(const QModelIndex &index, int role) const
|
|||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
if (index.column() == NAME)
|
||||
return filter_preset_name_qstring(index.row());
|
||||
return QString(filter_preset_name(index.row()).c_str());
|
||||
break;
|
||||
case Qt::DecorationRole:
|
||||
if (index.column() == REMOVE)
|
||||
|
|
Loading…
Add table
Reference in a new issue