* Elides string in @p path, e.g. "VEEERY/LONG/PATH" -> ".../LONG/PATH" * - probably much faster than QFontMetrics::elidedText() * - we do not need a widget context * - takes path separators into account * * @p width Maximum number of characters * * TODO: Move to kdevutil? */
| 54 | * TODO: Move to kdevutil? |
| 55 | */ |
| 56 | QString elidedPathLeft(const QString& path, int width) |
| 57 | { |
| 58 | const QLatin1String placeholder("..."); |
| 59 | |
| 60 | if (path.size() <= width) { |
| 61 | return path; |
| 62 | } |
| 63 | |
| 64 | int start = (path.size() - width) + placeholder.size(); |
| 65 | int pos = path.indexOf(QDir::separator(), start); |
| 66 | if (pos == -1) { |
| 67 | pos = start; // no separator => just cut off the path at the beginning |
| 68 | } |
| 69 | Q_ASSERT(path.size() - pos >= 0 && path.size() - pos <= width); |
| 70 | |
| 71 | const auto elidedText = QStringView{path}.last(path.size() - pos); |
| 72 | return placeholder + elidedText; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return true if @p url is non-empty, valid and has a clean path, false otherwise. |
no test coverage detected