File indexing completed on 2024-04-28 15:40:11

0001 // SPDX-FileCopyrightText: 2003-2010 Jesper K. Pedersen <blackie@kde.org>
0002 // SPDX-FileCopyrightText: 2021-2022 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006 #include "BreadcrumbViewer.h"
0007 
0008 #include <QTextDocument>
0009 
0010 void MainWindow::BreadcrumbViewer::setBreadcrumbs(const Browser::BreadcrumbList &list)
0011 {
0012     m_activeCrumbs = list.latest();
0013     updateText();
0014 }
0015 
0016 void MainWindow::BreadcrumbViewer::linkClicked(const QString &link)
0017 {
0018     Q_EMIT widenToBreadcrumb(m_activeCrumbs[link.toInt()]);
0019 }
0020 
0021 MainWindow::BreadcrumbViewer::BreadcrumbViewer()
0022 {
0023     connect(this, &BreadcrumbViewer::linkActivated, this, &BreadcrumbViewer::linkClicked);
0024 }
0025 
0026 /**
0027  * Format the text with hyperlinks. The tricky part is to handle the situation where all the text doesn't fit in.
0028  * The by far best solution would be to compress at a letter level, but this code is really only used in the rare
0029  * situation where the user chooses a very long path, as his window usually is somewhat wide.
0030  */
0031 void MainWindow::BreadcrumbViewer::updateText()
0032 {
0033     if (m_activeCrumbs.isEmpty())
0034         return;
0035 
0036     QStringList htmlList;
0037 
0038     for (int i = 0; i < m_activeCrumbs.count() - 1; ++i)
0039         htmlList.append(QString::fromLatin1("<a href=\"%1\">%2</a>").arg(i).arg(m_activeCrumbs[i].text()));
0040     if (!m_activeCrumbs[m_activeCrumbs.count() - 1].isView())
0041         htmlList.append(m_activeCrumbs[m_activeCrumbs.count() - 1].text());
0042 
0043     QTextDocument doc;
0044     doc.setDefaultFont(font());
0045 
0046     QString res = htmlList.last();
0047     const QString ellipses = QChar(0x2026) + QString::fromLatin1(" > ");
0048     for (int i = htmlList.count() - 2; i >= 0; --i) {
0049         // If we can't fit it in, then add ellipses
0050         const QString tmp = htmlList[i] + QString::fromLatin1(" > ") + res;
0051         doc.setHtml(tmp);
0052         if (doc.size().width() > width()) {
0053             res = ellipses + res;
0054             break;
0055         }
0056 
0057         // now check that we can fit in ellipses if this was the last token
0058         const QString tmp2 = ellipses + tmp;
0059         doc.setHtml(tmp2);
0060         if (doc.size().width() > width() && i != 0) {
0061             // Nope, so better stop here
0062             res = ellipses + res;
0063             break;
0064         }
0065 
0066         res = tmp;
0067     }
0068 
0069     setText(res);
0070 }
0071 
0072 void MainWindow::BreadcrumbViewer::resizeEvent(QResizeEvent *event)
0073 {
0074     QLabel::resizeEvent(event);
0075     updateText();
0076 }
0077 
0078 QSize MainWindow::BreadcrumbViewer::minimumSizeHint() const
0079 {
0080     return QSize(100, QLabel::minimumSizeHint().height());
0081 }
0082 // vi:expandtab:tabstop=4 shiftwidth=4:
0083 
0084 #include "moc_BreadcrumbViewer.cpp"