Verified Commit 8f44e846 authored by Eugene Popov's avatar Eugene Popov 🇺🇦 Committed by Fushan Wen
Browse files

A better fix for BUG 431673

parent 970f9538
......@@ -18,6 +18,7 @@ set(libklipper_common_SRCS
actionstreewidget.cpp
editactiondialog.cpp
clipcommandprocess.cpp
utils.cpp
)
ecm_qt_declare_logging_category(libklipper_common_SRCS HEADER klipper_debug.h IDENTIFIER KLIPPER_LOG CATEGORY_NAME org.kde.klipper DESCRIPTION "klipper" EXPORT KLIPPER)
......
......@@ -17,3 +17,12 @@ target_link_libraries(testHistoryModel
)
add_test(NAME klipper-testHistoryModel COMMAND testHistoryModel)
ecm_mark_as_test(testHistoryModel)
# Test Utils
add_executable(testKlipperUtils utilstest.cpp)
target_link_libraries(testKlipperUtils
Qt::Test
libklipper_common_static
)
add_test(NAME klipper-testUtils COMMAND testKlipperUtils)
ecm_mark_as_test(testKlipperUtils)
#include "../utils.h"
#include <QObject>
#include <QtTest>
class UtilsTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testSimplifiedText();
};
void UtilsTest::testSimplifiedText()
{
const QString text = QStringLiteral(" Some text\n to\t\ttest ");
QCOMPARE(Utils::simplifiedText(text, 1000), QStringLiteral("Some text to test"));
QCOMPARE(Utils::simplifiedText(text, 17), QStringLiteral("Some text to test"));
QCOMPARE(Utils::simplifiedText(text, 10), QStringLiteral("Some text"));
QCOMPARE(Utils::simplifiedText(text, 0), QStringLiteral(""));
}
QTEST_MAIN(UtilsTest)
#include "utilstest.moc"
......@@ -13,6 +13,7 @@
#include "historyitem.h"
#include "klipperpopup.h"
#include "utils.h"
PopupProxy::PopupProxy(KlipperPopup *parent, int menu_height, int menu_width)
: QObject(parent)
......@@ -77,7 +78,7 @@ void PopupProxy::tryInsertItem(HistoryItem const *const item, int &remainingHeig
QPixmap image(item->image());
if (image.isNull()) {
// Squeeze text strings so that do not take up the entire screen (or more)
QString text = m_proxy_for_menu->fontMetrics().elidedText(item->text().simplified(), Qt::ElideMiddle, m_menu_width);
QString text = m_proxy_for_menu->fontMetrics().elidedText(Utils::simplifiedText(item->text(), 1000), Qt::ElideRight, m_menu_width);
text.replace(QLatin1Char('&'), QLatin1String("&&"));
action->setText(text);
} else {
......
/*
SPDX-FileCopyrightText: 2022 Popov Eugene <popov895@ukr.net>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "utils.h"
QString Utils::simplifiedText(const QString &text, int maxLength)
{
if (text.length() <= maxLength) {
return text.simplified();
}
QString simplifiedText;
simplifiedText.reserve(maxLength);
bool wasSpaceBefore = false;
for (int i = 0, n = text.length(); i < n; ++i) {
if (simplifiedText.length() == maxLength) {
break;
}
const QChar c = text.at(i);
if (c.isSpace()) {
if (wasSpaceBefore || simplifiedText.isEmpty()) {
continue;
}
simplifiedText.append(QLatin1Char(' '));
wasSpaceBefore = true;
} else {
simplifiedText.append(c);
wasSpaceBefore = false;
}
}
if (simplifiedText.endsWith(QLatin1Char(' '))) {
simplifiedText.chop(1);
}
return simplifiedText;
}
/*
SPDX-FileCopyrightText: 2022 Popov Eugene <popov895@ukr.net>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <QString>
class Utils
{
public:
/**
* Returns a simplified text of a maximum length of maxLength
*/
static QString simplifiedText(const QString &text, int maxLength);
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment