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

@ -320,7 +320,7 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
TankInfoModel *tanks = TankInfoModel::instance();
QModelIndexList matches = tanks->match(tanks->index(0, 0), Qt::DisplayRole, cyl->type.description);
cyl->type.size = string_to_volume(vString.toUtf8().data(), cyl->type.workingpressure);
cyl->type.size = string_to_volume(qPrintable(vString), cyl->type.workingpressure);
mark_divelist_changed(true);
if (!matches.isEmpty())
tanks->setData(tanks->index(matches.first().row(), TankInfoModel::ML), cyl->type.size.mliter);
@ -331,7 +331,7 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
if (CHANGED()) {
TankInfoModel *tanks = TankInfoModel::instance();
QModelIndexList matches = tanks->match(tanks->index(0, 0), Qt::DisplayRole, cyl->type.description);
cyl->type.workingpressure = string_to_pressure(vString.toUtf8().data());
cyl->type.workingpressure = string_to_pressure(qPrintable(vString));
if (!matches.isEmpty())
tanks->setData(tanks->index(matches.first().row(), TankInfoModel::BAR), cyl->type.workingpressure.mbar / 1000.0);
changed = true;
@ -339,20 +339,20 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
break;
case START:
if (CHANGED()) {
cyl->start = string_to_pressure(vString.toUtf8().data());
cyl->start = string_to_pressure(qPrintable(vString));
changed = true;
}
break;
case END:
if (CHANGED()) {
//&& (!cyl->start.mbar || string_to_pressure(vString.toUtf8().data()).mbar <= cyl->start.mbar)) {
cyl->end = string_to_pressure(vString.toUtf8().data());
//&& (!cyl->start.mbar || string_to_pressure(qPrintable(vString)).mbar <= cyl->start.mbar)) {
cyl->end = string_to_pressure(qPrintable(vString));
changed = true;
}
break;
case O2:
if (CHANGED()) {
cyl->gasmix.o2 = string_to_fraction(vString.toUtf8().data());
cyl->gasmix.o2 = string_to_fraction(qPrintable(vString));
// fO2 + fHe must not be greater than 1
if (get_o2(&cyl->gasmix) + get_he(&cyl->gasmix) > 1000)
cyl->gasmix.he.permille = 1000 - get_o2(&cyl->gasmix);
@ -369,7 +369,7 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
break;
case HE:
if (CHANGED()) {
cyl->gasmix.he = string_to_fraction(vString.toUtf8().data());
cyl->gasmix.he = string_to_fraction(qPrintable(vString));
// fO2 + fHe must not be greater than 1
if (get_o2(&cyl->gasmix) + get_he(&cyl->gasmix) > 1000)
cyl->gasmix.o2.permille = 1000 - get_he(&cyl->gasmix);
@ -379,20 +379,20 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
break;
case DEPTH:
if (CHANGED()) {
cyl->depth = string_to_depth(vString.toUtf8().data());
cyl->depth = string_to_depth(qPrintable(vString));
changed = true;
}
break;
case MOD:
if (CHANGED()) {
if (QString::compare(vString.toUtf8().data(), "*") == 0) {
if (QString::compare(qPrintable(vString), "*") == 0) {
cyl->bestmix_o2 = true;
// Calculate fO2 for max. depth
cyl->gasmix.o2 = best_o2(displayed_dive.maxdepth, &displayed_dive);
} else {
cyl->bestmix_o2 = false;
// Calculate fO2 for input depth
cyl->gasmix.o2 = best_o2(string_to_depth(vString.toUtf8().data()), &displayed_dive);
cyl->gasmix.o2 = best_o2(string_to_depth(qPrintable(vString)), &displayed_dive);
}
pressure_t modpO2;
modpO2.mbar = prefs.decopo2;
@ -402,14 +402,14 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
break;
case MND:
if (CHANGED()) {
if (QString::compare(vString.toUtf8().data(), "*") == 0) {
if (QString::compare(qPrintable(vString), "*") == 0) {
cyl->bestmix_he = true;
// Calculate fO2 for max. depth
cyl->gasmix.he = best_he(displayed_dive.maxdepth, &displayed_dive);
} else {
cyl->bestmix_he = false;
// Calculate fHe for input depth
cyl->gasmix.he = best_he(string_to_depth(vString.toUtf8().data()), &displayed_dive);
cyl->gasmix.he = best_he(string_to_depth(qPrintable(vString)), &displayed_dive);
}
changed = true;
}

View file

@ -161,7 +161,7 @@ void DivePictureModel::removePicture(const QString &fileUrl, bool last)
int i;
struct dive *dive;
for_each_dive (i, dive) {
if (dive->selected && dive_remove_picture(dive, fileUrl.toUtf8().data()))
if (dive->selected && dive_remove_picture(dive, qPrintable(fileUrl)))
break;
}
if (last) {

View file

@ -160,7 +160,7 @@ void DivePlannerPointsModel::setupCylinders()
}
if (cylinder_none(&displayed_dive.cylinder[0])) {
// roughly an AL80
displayed_dive.cylinder[0].type.description = strdup(tr("unknown").toUtf8().constData());
displayed_dive.cylinder[0].type.description = strdup(qPrintable(tr("unknown")));
displayed_dive.cylinder[0].type.size.mliter = 11100;
displayed_dive.cylinder[0].type.workingpressure.mbar = 207000;
}
@ -1063,9 +1063,9 @@ void DivePlannerPointsModel::computeVariations(struct diveplan *original_plan, s
restore_deco_state(save, &ds, false);
char buf[200];
sprintf(buf, ", %s: + %d:%02d /%s + %d:%02d /min", tr("Stop times").toUtf8().data(),
FRACTION(analyzeVariations(shallower, original, deeper, depth_units.toUtf8().data()), 60), depth_units.toUtf8().data(),
FRACTION(analyzeVariations(shorter, original, longer, time_units.toUtf8().data()), 60));
sprintf(buf, ", %s: + %d:%02d /%s + %d:%02d /min", qPrintable(tr("Stop times")),
FRACTION(analyzeVariations(shallower, original, deeper, qPrintable(depth_units)), 60), qPrintable(depth_units),
FRACTION(analyzeVariations(shorter, original, longer, qPrintable(time_units)), 60));
emit variationsComputed(QString(buf));
#ifdef DEBUG_STOPVAR
@ -1123,7 +1123,7 @@ void DivePlannerPointsModel::createPlan(bool replanCopy)
// Deal with line breaks
oldnotes.replace("\n", "<br>");
oldnotes.append(displayed_dive.notes);
displayed_dive.notes = strdup(oldnotes.toUtf8().data());
displayed_dive.notes = strdup(qPrintable(oldnotes));
// If we save as new create a copy of the dive here
if (replanCopy) {
struct dive *copy = alloc_dive();

View file

@ -88,7 +88,7 @@ QVariant FilterModelBase::data(const QModelIndex &index, int role) const
return checkState[index.row()] ? Qt::Checked : Qt::Unchecked;
} else if (role == Qt::DisplayRole) {
QString value = stringList()[index.row()];
int count = countDives((index.row() == rowCount() - 1) ? "" : value.toUtf8().data());
int count = countDives((index.row() == rowCount() - 1) ? "" : qPrintable(value));
return value + QString(" (%1)").arg(count);
}
return QVariant();

View file

@ -42,7 +42,7 @@ void MessageHandlerModel::addLog(QtMsgType type, const QString& message)
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_data.append({message, type});
endInsertRows();
INFO("%s", message.toUtf8().constData());
INFO("%s", qPrintable(message));
#if defined (Q_OS_ANDROID)
writeToAppLogFile(message);
#endif

View file

@ -114,14 +114,14 @@ bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int r
}
}
if (ws_info[i].name == NULL) // didn't find a match
ws->description = strdup(vString.toUtf8().constData());
ws->description = strdup(qPrintable(vString));
changed = true;
}
}
break;
case WEIGHT:
if (CHANGED()) {
ws->weight = string_to_weight(vString.toUtf8().data());
ws->weight = string_to_weight(qPrintable(vString));
// now update the ws_info
changed = true;
WSInfoModel *wsim = WSInfoModel::instance();