Skip to content

Commit

Permalink
Fix text elide in suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaopengLin committed Sep 2, 2024
1 parent e3bfa0f commit f0cec36
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/searchbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ MultiZimButton::MultiZimButton(QWidget *parent) : QToolButton(parent), mp_button
mp_buttonList = new QListWidget(this);
auto popupAction = new QWidgetAction(menu());
popupAction->setDefaultWidget(mp_buttonList);
mp_buttonList->setTextElideMode(Qt::ElideNone);
menu()->addAction(popupAction);
mp_buttonList->setMinimumWidth(menu()->sizeHint().width());
mp_buttonList->setMinimumHeight(42 * 7);
Expand Down
21 changes: 20 additions & 1 deletion src/suggestionlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ QVariant SuggestionListModel::data(const QModelIndex &index, int role) const
switch (role)
{
case Qt::DisplayRole:
{
if (row < m_suggestions.size())
{
QString suggestionText = m_suggestions.at(row);
auto& searchBar = KiwixApp::instance()->getMainWindow()->getTopWidget()->getSearchBar();
auto width = searchBar.getLineEdit().width() - 53;

QFontMetrics metrics(QFont("Selawik", 16));
QString elidedText = metrics.elidedText(suggestionText, Qt::ElideRight, width);
if (suggestionText != elidedText)
{
elidedText.chop(5);
KiwixApp::isRightToLeft() == elidedText.isRightToLeft() ? elidedText.append("(...)") : elidedText.prepend("(...)");
return elidedText;
}
return suggestionText;
}
break;
}
case Qt::EditRole:
if (row < m_suggestions.size())
return m_suggestions.at(row);
Expand Down Expand Up @@ -88,7 +107,7 @@ void SuggestionListModel::resetSuggestions(const QStringList &suggestions)
{
beginResetModel();
m_suggestions.clear();
for (const auto suggestion : suggestions)
for (const auto& suggestion : suggestions)
m_suggestions.append(suggestion);
endResetModel();
}
Expand Down
1 change: 1 addition & 0 deletions src/suggestiontreeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void SuggestionTreeView::postSetPopUpInitialization()
setRootIsDecorated(false);
setStyleSheet(KiwixApp::instance()->parseStyleFromFile(":/css/popup.css"));
setIconSize(QSize(32, 32));
setTextElideMode(Qt::TextElideMode::ElideNone);
connect(model(), &QAbstractItemModel::modelReset, [=](){
// auto& searchBar = KiwixApp::instance()->getMainWindow()->getTopWidget()->getSearchBar();
// //10 padding
Expand Down

0 comments on commit f0cec36

Please sign in to comment.