Cleanup: consistently use qPrintable()

Replace constructs of the kind
  s.toUtf8().data(),
  s.toUtf8().constData(),
  s.toLocal8Bit().data(),
  s.toLocal8Bit.constData() or
  qUtf8Printable(s)
by
  qPrintable(s).

This is concise, consistent and - in principle - more performant than
the .data() versions.

Sadly, owing to a suboptimal implementation, qPrintable(s) currently
is a pessimization compared to s.toUtf8().data(). A fix is scheduled for
new Qt versions: https://codereview.qt-project.org/#/c/221331/

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-02-25 13:51:41 +01:00 committed by Lubomir I. Ivanov
parent 4e44fe7598
commit b72cc1f317
33 changed files with 166 additions and 162 deletions

View file

@ -910,9 +910,9 @@ void ConfigureDiveComputerDialog::getDeviceData()
#else
QString device = ui.device->currentText();
#endif
device_data.devname = strdup(device.toUtf8().data());
device_data.vendor = strdup(selected_vendor.toUtf8().data());
device_data.product = strdup(selected_product.toUtf8().data());
device_data.devname = strdup(qPrintable(device));
device_data.vendor = strdup(qPrintable(selected_vendor));
device_data.product = strdup(qPrintable(selected_product));
device_data.descriptor = descriptorLookup.value(selected_vendor + selected_product);
device_data.deviceid = device_data.diveid = 0;
@ -1479,7 +1479,7 @@ void ConfigureDiveComputerDialog::pickLogFile()
filename, tr("Log files") + " (*.log)");
if (!logFile.isEmpty()) {
free(logfile_name);
logfile_name = strdup(logFile.toUtf8().data());
logfile_name = strdup(qPrintable(logFile));
}
}

View file

@ -949,7 +949,7 @@ void DiveListView::matchImagesToDives(QStringList fileNames)
for_each_dive (j, dive) {
if (!dive->selected)
continue;
dive_create_picture(dive, copy_string(fileName.toUtf8().data()), shiftDialog.amount(), shiftDialog.matchAll());
dive_create_picture(dive, copy_string(qPrintable(fileName)), shiftDialog.amount(), shiftDialog.matchAll());
}
}

View file

@ -154,7 +154,7 @@ void DiveLogExportDialog::on_buttonBox_accepted()
filename = QFileDialog::getSaveFileName(this, tr("Export world map"), lastDir,
tr("HTML files") + " (*.html)");
if (!filename.isNull() && !filename.isEmpty())
export_worldmap_HTML(filename.toUtf8().data(), ui->exportSelected->isChecked());
export_worldmap_HTML(qPrintable(filename), ui->exportSelected->isChecked());
} else if (ui->exportSubsurfaceXML->isChecked()) {
filename = QFileDialog::getSaveFileName(this, tr("Export Subsurface XML"), lastDir,
tr("Subsurface files") + " (*.ssrf *.xml)");
@ -167,11 +167,11 @@ void DiveLogExportDialog::on_buttonBox_accepted()
} else if (ui->exportImageDepths->isChecked()) {
filename = QFileDialog::getSaveFileName(this, tr("Save image depths"), lastDir);
if (!filename.isNull() && !filename.isEmpty())
export_depths(filename.toUtf8().data(), ui->exportSelected->isChecked());
export_depths(qPrintable(filename), ui->exportSelected->isChecked());
} else if (ui->exportTeX->isChecked()) {
filename = QFileDialog::getSaveFileName(this, tr("Export to TeX file"), lastDir, tr("TeX files") + " (*.tex)");
if (!filename.isNull() && !filename.isEmpty())
export_TeX(filename.toUtf8().data(), ui->exportSelected->isChecked());
export_TeX(qPrintable(filename), ui->exportSelected->isChecked());
}
break;
case 1:
@ -226,7 +226,7 @@ void DiveLogExportDialog::export_depths(const char *filename, const bool selecte
f = subsurface_fopen(filename, "w+");
if (!f) {
report_error(tr("Can't open file %s").toUtf8().data(), filename);
report_error(qPrintable(tr("Can't open file %s")), filename);
} else {
flush_buffer(&buf, f); /*check for writing errors? */
fclose(f);
@ -325,7 +325,7 @@ void DiveLogExportDialog::export_TeX(const char *filename, const bool selected_o
site ? put_format(&buf, "\\def\\gpslat{%f}\n", site->latitude.udeg / 1000000.0) : put_format(&buf, "\\def\\gpslat{}\n");
site ? put_format(&buf, "\\def\\gpslon{%f}\n", site->longitude.udeg / 1000000.0) : put_format(&buf, "\\def\\gpslon{}\n");
put_format(&buf, "\\def\\computer{%s}\n", dive->dc.model);
put_format(&buf, "\\def\\country{%s}\n", country.toUtf8().data());
put_format(&buf, "\\def\\country{%s}\n", qPrintable(country));
put_format(&buf, "\\def\\time{%u:%02u}\n", FRACTION(dive->duration.seconds, 60));
put_format(&buf, "\n%% Dive Profile Details:\n");
@ -337,8 +337,8 @@ void DiveLogExportDialog::export_TeX(const char *filename, const bool selected_o
dive->meandepth.mm ? put_format(&buf, "\\def\\meandepth{%.1f\\depthunit}\n", get_depth_units(dive->meandepth.mm, NULL, &unit)) : put_format(&buf, "\\def\\meandepth{}\n");
put_format(&buf, "\\def\\type{%s}\n", dive->tag_list ? dive->tag_list->tag->name : "");
put_format(&buf, "\\def\\viz{%s}\n", viz.toUtf8().data());
put_format(&buf, "\\def\\rating{%s}\n", rating.toUtf8().data());
put_format(&buf, "\\def\\viz{%s}\n", qPrintable(viz));
put_format(&buf, "\\def\\rating{%s}\n", qPrintable(rating));
put_format(&buf, "\\def\\plot{\\includegraphics[width=9cm,height=4cm]{profile%d}}\n", dive->number);
put_format(&buf, "\\def\\comment{%s}\n", dive->notes ? dive->notes : "");
put_format(&buf, "\\def\\buddy{%s}\n", dive->buddy ? dive->buddy : "");
@ -408,7 +408,7 @@ void DiveLogExportDialog::export_TeX(const char *filename, const bool selected_o
f = subsurface_fopen(filename, "w+");
if (!f) {
report_error(tr("Can't open file %s").toUtf8().data(), filename);
report_error(qPrintable(tr("Can't open file %s")), filename);
} else {
flush_buffer(&buf, f); /*check for writing errors? */
fclose(f);

View file

@ -788,10 +788,10 @@ int DiveLogImportDialog::setup_csv_params(QStringList r, char **params, int pnr)
params[pnr++] = intdup(ui->CSVUnits->currentIndex());
if (hw.length()) {
params[pnr++] = strdup("hw");
params[pnr++] = strdup(hw.toUtf8().data());
params[pnr++] = strdup(qPrintable(hw));
} else if (ui->knownImports->currentText().length() > 0) {
params[pnr++] = strdup("hw");
params[pnr++] = strdup(ui->knownImports->currentText().prepend("\"").append("\"").toUtf8().data());
params[pnr++] = strdup(qPrintable(ui->knownImports->currentText().prepend("\"").append("\"")));
}
params[pnr++] = NULL;
@ -850,7 +850,7 @@ void DiveLogImportDialog::on_buttonBox_accepted()
for (int i = 0; i < fileNames.size(); ++i) {
if (ui->knownImports->currentText() == "Seabear CSV") {
parse_seabear_log(fileNames[i].toUtf8().data());
parse_seabear_log(qPrintable(fileNames[i]));
} else {
char *params[49];
@ -866,8 +866,8 @@ void DiveLogImportDialog::on_buttonBox_accepted()
params[pnr++] = strdup("1" + apdRe.cap(2).toLatin1());
}
pnr = setup_csv_params(r, params, pnr);
parse_csv_file(fileNames[i].toUtf8().data(), params, pnr - 1,
specialCSV.contains(ui->knownImports->currentIndex()) ? CSVApps[ui->knownImports->currentIndex()].name.toUtf8().data() : "csv");
parse_csv_file(qPrintable(fileNames[i]), params, pnr - 1,
specialCSV.contains(ui->knownImports->currentIndex()) ? qPrintable(CSVApps[ui->knownImports->currentIndex()].name) : "csv");
}
}
} else {
@ -927,7 +927,7 @@ void DiveLogImportDialog::on_buttonBox_accepted()
params[pnr++] = intdup(r.indexOf(tr("Water temp.")));
params[pnr++] = NULL;
parse_manual_file(fileNames[i].toUtf8().data(), params, pnr - 1);
parse_manual_file(qPrintable(fileNames[i]), params, pnr - 1);
} else {
char *params[51];
int pnr = 0;
@ -942,8 +942,8 @@ void DiveLogImportDialog::on_buttonBox_accepted()
params[pnr++] = strdup("1" + apdRe.cap(2).toLatin1());
}
pnr = setup_csv_params(r, params, pnr);
parse_csv_file(fileNames[i].toUtf8().data(), params, pnr - 1,
specialCSV.contains(ui->knownImports->currentIndex()) ? CSVApps[ui->knownImports->currentIndex()].name.toUtf8().data() : "csv");
parse_csv_file(qPrintable(fileNames[i]), params, pnr - 1,
specialCSV.contains(ui->knownImports->currentIndex()) ? qPrintable(CSVApps[ui->knownImports->currentIndex()].name) : "csv");
}
}
}

View file

@ -297,7 +297,7 @@ void DownloadFromDCWidget::on_downloadCancelRetryButton_clicked()
#endif
if (data->vendor() == "Uemis") {
char *colon;
char *devname = strdup(ui.device->currentText().toUtf8().data());
char *devname = strdup(qPrintable(ui.device->currentText()));
if ((colon = strstr(devname, ":\\ (UEMISSDA)")) != NULL) {
*(colon + 2) = '\0';
@ -362,7 +362,7 @@ void DownloadFromDCWidget::pickLogFile()
filename, tr("Log files") + " (*.log)");
if (!logFile.isEmpty()) {
free(logfile_name);
logfile_name = copy_string(logFile.toUtf8().data());
logfile_name = copy_string(qPrintable(logFile));
}
}
@ -388,7 +388,7 @@ void DownloadFromDCWidget::pickDumpFile()
filename, tr("Dump files") + " (*.bin)");
if (!dumpFile.isEmpty()) {
free(dumpfile_name);
dumpfile_name = copy_string(dumpFile.toUtf8().data());
dumpfile_name = copy_string(qPrintable(dumpFile));
}
}

View file

@ -158,7 +158,7 @@ void LocationInformationWidget::acceptChanges()
{
char *uiString;
struct dive_site *currentDs;
uiString = copy_string(ui.diveSiteName->text().toUtf8().data());
uiString = copy_string(qPrintable(ui.diveSiteName->text()));
if (get_dive_site_by_uuid(displayed_dive_site.uuid) != NULL) {
currentDs = get_dive_site_by_uuid(displayed_dive_site.uuid);
} else {
@ -175,14 +175,14 @@ void LocationInformationWidget::acceptChanges()
} else {
free(uiString);
}
uiString = copy_string(ui.diveSiteDescription->text().toUtf8().data());
uiString = copy_string(qPrintable(ui.diveSiteDescription->text()));
if (!same_string(uiString, currentDs->description)) {
free(currentDs->description);
currentDs->description = uiString;
} else {
free(uiString);
}
uiString = copy_string(ui.diveSiteCountry->text().toUtf8().data());
uiString = copy_string(qPrintable(ui.diveSiteCountry->text()));
// if the user entered a different contriy, first update the taxonomy
// for the displayed dive site; this below will get copied into the currentDs
if (!same_string(uiString, taxonomy_get_country(&displayed_dive_site.taxonomy)) &&
@ -193,7 +193,7 @@ void LocationInformationWidget::acceptChanges()
// now update the currentDs (which we then later copy back ontop of displayed_dive_site
copy_dive_site_taxonomy(&displayed_dive_site, currentDs);
uiString = copy_string(ui.diveSiteNotes->document()->toPlainText().toUtf8().data());
uiString = copy_string(qPrintable(ui.diveSiteNotes->document()->toPlainText()));
if (!same_string(uiString, currentDs->notes)) {
free(currentDs->notes);
currentDs->notes = uiString;

View file

@ -623,12 +623,12 @@ void MainWindow::on_actionCloudstoragesave_triggered()
information()->acceptChanges();
showProgressBar();
int error = save_dives(filename.toUtf8().data());
int error = save_dives(qPrintable(filename));
hideProgressBar();
if (error)
return;
setCurrentFile(filename.toUtf8().data());
setCurrentFile(qPrintable(filename));
mark_divelist_changed(false);
}
@ -935,7 +935,7 @@ void MainWindow::updateVariations(QString variations)
{
QString notes = QString(displayed_dive.notes);
free(displayed_dive.notes);
displayed_dive.notes = strdup(notes.replace("VARIATIONS", variations).toUtf8().data());
displayed_dive.notes = strdup(qPrintable(notes.replace("VARIATIONS", variations)));
plannerDetails()->divePlanOutput()->setHtml(displayed_dive.notes);
}
@ -1683,10 +1683,10 @@ int MainWindow::file_save_as(void)
if (information()->isEditing())
information()->acceptChanges();
if (save_dives(filename.toUtf8().data()))
if (save_dives(qPrintable(filename)))
return -1;
setCurrentFile(filename.toUtf8().data());
setCurrentFile(qPrintable(filename));
mark_divelist_changed(false);
addRecentFile(filename, true);
return 0;

View file

@ -324,7 +324,7 @@ void TankUseDelegate::setEditorData(QWidget * editor, const QModelIndex & index)
{
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
QString indexString = index.data().toString();
comboBox->setCurrentIndex(cylinderuse_from_text(indexString.toUtf8().data()));
comboBox->setCurrentIndex(cylinderuse_from_text(qPrintable(indexString)));
}
void TankUseDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const

View file

@ -319,7 +319,7 @@ void ShiftImageTimesDialog::syncCameraClicked()
scene->addPixmap(picture.scaled(ui.DCImage->size()));
ui.DCImage->setScene(scene);
dcImageEpoch = picture_get_timestamp(fileNames.at(0).toUtf8().data());
dcImageEpoch = picture_get_timestamp(qPrintable(fileNames.at(0)));
QDateTime dcDateTime = QDateTime::fromTime_t(dcImageEpoch, Qt::UTC);
ui.dcTime->setDateTime(dcDateTime);
connect(ui.dcTime, SIGNAL(dateTimeChanged(const QDateTime &)), this, SLOT(dcDateTimeChanged(const QDateTime &)));
@ -393,11 +393,11 @@ void ShiftImageTimesDialog::updateInvalid()
ui.invalidFilesText->append(tr("\nFiles with inappropriate date/time") + ":");
Q_FOREACH (const QString &fileName, fileNames) {
if (picture_check_valid(fileName.toUtf8().data(), m_amount))
if (picture_check_valid(qPrintable(fileName), m_amount))
continue;
// We've found invalid image
timestamp = picture_get_timestamp(fileName.toUtf8().data());
timestamp = picture_get_timestamp(qPrintable(fileName));
time_first.setTime_t(timestamp + m_amount);
if (timestamp == 0)
ui.invalidFilesText->append(fileName + " - " + tr("No Exif date/time found"));

View file

@ -335,7 +335,7 @@ void SubsurfaceWebServices::buttonClicked(QAbstractButton *button)
bool d_eq_f = (qDialogUid == qFileUid);
if (!d_eq_f || s_eq_d)
s.setValue("subsurface_webservice_uid", qDialogUid);
set_userid(qDialogUid.toLocal8Bit().data());
set_userid(qPrintable(qDialogUid));
}
s.sync();
hide();

View file

@ -1082,7 +1082,7 @@ void MainTab::on_buddy_textChanged()
if (editMode == IGNORE || acceptingEdit == true)
return;
if (same_string(displayed_dive.buddy, ui.buddy->toPlainText().toUtf8().data()))
if (same_string(displayed_dive.buddy, qPrintable(ui.buddy->toPlainText())))
return;
QStringList text_list = ui.buddy->toPlainText().split(",", QString::SkipEmptyParts);
@ -1090,7 +1090,7 @@ void MainTab::on_buddy_textChanged()
text_list[i] = text_list[i].trimmed();
QString text = text_list.join(", ");
free(displayed_dive.buddy);
displayed_dive.buddy = strdup(text.toUtf8().data());
displayed_dive.buddy = strdup(qPrintable(text));
markChangedWidget(ui.buddy);
}
@ -1099,7 +1099,7 @@ void MainTab::on_divemaster_textChanged()
if (editMode == IGNORE || acceptingEdit == true)
return;
if (same_string(displayed_dive.divemaster, ui.divemaster->toPlainText().toUtf8().data()))
if (same_string(displayed_dive.divemaster, qPrintable(ui.divemaster->toPlainText())))
return;
QStringList text_list = ui.divemaster->toPlainText().split(",", QString::SkipEmptyParts);
@ -1107,7 +1107,7 @@ void MainTab::on_divemaster_textChanged()
text_list[i] = text_list[i].trimmed();
QString text = text_list.join(", ");
free(displayed_dive.divemaster);
displayed_dive.divemaster = strdup(text.toUtf8().data());
displayed_dive.divemaster = strdup(qPrintable(text));
markChangedWidget(ui.divemaster);
}
@ -1241,7 +1241,7 @@ void MainTab::saveTags()
taglist_free(displayed_dive.tag_list);
displayed_dive.tag_list = NULL;
Q_FOREACH (const QString& tag, ui.tagWidget->getBlockStringList())
taglist_add_tag(&displayed_dive.tag_list, tag.toUtf8().data());
taglist_add_tag(&displayed_dive.tag_list, qPrintable(tag));
taglist_cleanup(&displayed_dive.tag_list);
// figure out which tags were added and which tags were removed
@ -1352,7 +1352,7 @@ void MainTab::on_tagWidget_textChanged()
return;
taglist_get_tagstring(displayed_dive.tag_list, buf, 1024);
if (same_string(buf, ui.tagWidget->toPlainText().toUtf8().data()))
if (same_string(buf, qPrintable(ui.tagWidget->toPlainText())))
return;
markChangedWidget(ui.tagWidget);
@ -1407,7 +1407,7 @@ void MainTab::on_suit_textChanged(const QString &text)
if (editMode == IGNORE || acceptingEdit == true)
return;
free(displayed_dive.suit);
displayed_dive.suit = strdup(text.toUtf8().data());
displayed_dive.suit = strdup(qPrintable(text));
markChangedWidget(ui.suit);
}
@ -1416,18 +1416,18 @@ void MainTab::on_notes_textChanged()
if (editMode == IGNORE || acceptingEdit == true)
return;
if (currentTrip) {
if (same_string(displayedTrip.notes, ui.notes->toPlainText().toUtf8().data()))
if (same_string(displayedTrip.notes, qPrintable(ui.notes->toPlainText())))
return;
free(displayedTrip.notes);
displayedTrip.notes = strdup(ui.notes->toPlainText().toUtf8().data());
displayedTrip.notes = strdup(qPrintable(ui.notes->toPlainText()));
} else {
if (same_string(displayed_dive.notes, ui.notes->toPlainText().toUtf8().data()))
if (same_string(displayed_dive.notes, qPrintable(ui.notes->toPlainText())))
return;
free(displayed_dive.notes);
if (ui.notes->toHtml().indexOf("<table") != -1)
displayed_dive.notes = strdup(ui.notes->toHtml().toUtf8().data());
displayed_dive.notes = strdup(qPrintable(ui.notes->toHtml()));
else
displayed_dive.notes = strdup(ui.notes->toPlainText().toUtf8().data());
displayed_dive.notes = strdup(qPrintable(ui.notes->toPlainText()));
}
markChangedWidget(ui.notes);
}

View file

@ -250,7 +250,7 @@ void TemplateLayout::writeTemplate(QString template_name, QString grantlee_templ
{
QFile qfile(getPrintingTemplatePathUser() + QDir::separator() + template_name);
if (qfile.open(QFile::ReadWrite | QFile::Text)) {
qfile.write(grantlee_template.toUtf8().data());
qfile.write(qPrintable(grantlee_template));
qfile.resize(qfile.pos());
qfile.close();
}