diff --git a/common/utils.cpp b/common/utils.cpp --- a/common/utils.cpp +++ b/common/utils.cpp @@ -33,22 +33,34 @@ QString Utils::outputName(const KScreen::Output *output) { + const auto concatWithSpace = [](const QString ¤t, const QString &toAppend) -> QString { + const auto toAppendTrimmed = toAppend.trimmed(); + // If we already have some text in "current", append a space and the new text. + if (!current.isEmpty() && !toAppendTrimmed.isEmpty()) { + return current + QLatin1Char(' ') + toAppendTrimmed; + // If we already have some text, but there's nothing to append, return the original text. + } else if (!current.isEmpty() && toAppendTrimmed.isEmpty()) { + return current; + } + + // Otherwise return just the new text (might be an empty string, but that's OK) + return toAppendTrimmed; + }; + if (output->type() == KScreen::Output::Panel) { return i18n("Laptop Screen"); } if (output->edid()) { // The name will be "VendorName ModelName (ConnectorName)", // but some components may be empty. QString name; - if (!(output->edid()->vendor().isEmpty())) { - name = output->edid()->vendor() + QLatin1Char(' '); - } - if (!output->edid()->name().isEmpty()) { - name += output->edid()->name() + QLatin1Char(' '); - } - if (!name.trimmed().isEmpty()) { - return name + QLatin1Char('(') + output->name() + QLatin1Char(')'); + name = concatWithSpace(name, output->edid()->vendor()); + name = concatWithSpace(name, output->edid()->name()); + + // Don't append (ConnectorName) if its the same as "VendorName ModelName". + if (!name.isEmpty() && name != output->name()) { + return concatWithSpace(name, QLatin1Char('(') + output->name() + QLatin1Char(')')); } } return output->name();