File indexing completed on 2024-05-12 05:47:52

0001 /*
0002  * SPDX-FileCopyrightText: 2010 Peter Penz <peter.penz19@gmail.com>
0003  * SPDX-FileCopyrightText: 2008 Fredrik Höglund <fredrik@kde.org>
0004  * SPDX-FileCopyrightText: 2012 Mark Gaiser <markg85@gmail.com>
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "dolphinfilemetadatawidget.h"
0010 
0011 #include <Baloo/FileMetaDataWidget>
0012 #include <KColorScheme>
0013 #include <KSeparator>
0014 #include <KStringHandler>
0015 
0016 #include <QLabel>
0017 #include <QStyleOptionFrame>
0018 #include <QStylePainter>
0019 #include <QTextDocument>
0020 #include <QTextLayout>
0021 #include <QVBoxLayout>
0022 
0023 DolphinFileMetaDataWidget::DolphinFileMetaDataWidget(QWidget *parent)
0024     : QWidget(parent)
0025     , m_preview(nullptr)
0026     , m_name(nullptr)
0027     , m_fileMetaDataWidget(nullptr)
0028 {
0029     // Create widget for file preview
0030     m_preview = new QLabel(this);
0031     m_preview->setAlignment(Qt::AlignTop);
0032 
0033     // Create widget for file name
0034     m_name = new QLabel(this);
0035     m_name->setForegroundRole(QPalette::ToolTipText);
0036     m_name->setTextFormat(Qt::PlainText);
0037     m_name->setAlignment(Qt::AlignHCenter);
0038 
0039     QFont font = m_name->font();
0040     font.setBold(true);
0041     m_name->setFont(font);
0042 
0043     QFontMetrics fontMetrics(font);
0044     m_name->setMaximumWidth(fontMetrics.averageCharWidth() * 40);
0045 
0046     // Create widget for the meta data
0047     m_fileMetaDataWidget = new Baloo::FileMetaDataWidget(this);
0048     connect(m_fileMetaDataWidget, &Baloo::FileMetaDataWidget::metaDataRequestFinished, this, &DolphinFileMetaDataWidget::metaDataRequestFinished);
0049     connect(m_fileMetaDataWidget, &Baloo::FileMetaDataWidget::urlActivated, this, &DolphinFileMetaDataWidget::urlActivated);
0050     m_fileMetaDataWidget->setForegroundRole(QPalette::ToolTipText);
0051     m_fileMetaDataWidget->setReadOnly(true);
0052 
0053     QVBoxLayout *textLayout = new QVBoxLayout();
0054     textLayout->addWidget(m_name);
0055     textLayout->addWidget(new KSeparator());
0056     textLayout->addWidget(m_fileMetaDataWidget);
0057     textLayout->setAlignment(m_name, Qt::AlignCenter);
0058     textLayout->setAlignment(m_fileMetaDataWidget, Qt::AlignLeft);
0059     // Assure that the text-layout gets top-aligned by adding a stretch.
0060     // Don't use textLayout->setAlignment(Qt::AlignTop) instead, as this does
0061     // not work with the heightForWidth()-size-hint of m_fileMetaDataWidget
0062     // (see bug #241608)
0063     textLayout->addStretch();
0064 
0065     QHBoxLayout *layout = new QHBoxLayout(this);
0066     layout->addWidget(m_preview);
0067     layout->addSpacing(layout->contentsMargins().left());
0068     layout->addLayout(textLayout);
0069 }
0070 
0071 DolphinFileMetaDataWidget::~DolphinFileMetaDataWidget()
0072 {
0073 }
0074 
0075 void DolphinFileMetaDataWidget::setPreview(const QPixmap &pixmap)
0076 {
0077     m_preview->setPixmap(pixmap);
0078 }
0079 
0080 QPixmap DolphinFileMetaDataWidget::preview() const
0081 {
0082     if (!m_preview->pixmap(Qt::ReturnByValue).isNull()) {
0083         return m_preview->pixmap(Qt::ReturnByValue);
0084     }
0085 
0086     return QPixmap();
0087 }
0088 
0089 void DolphinFileMetaDataWidget::setName(const QString &name)
0090 {
0091     QTextOption textOption;
0092     textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
0093 
0094     const QString processedName = Qt::mightBeRichText(name) ? name : KStringHandler::preProcessWrap(name);
0095 
0096     QTextLayout textLayout(processedName);
0097     textLayout.setFont(m_name->font());
0098     textLayout.setTextOption(textOption);
0099 
0100     QString wrappedText;
0101     wrappedText.reserve(processedName.length());
0102 
0103     // wrap the text to fit into the maximum width of m_name
0104     textLayout.beginLayout();
0105     QTextLine line = textLayout.createLine();
0106     while (line.isValid()) {
0107         line.setLineWidth(m_name->maximumWidth());
0108         wrappedText += QStringView(processedName).mid(line.textStart(), line.textLength());
0109 
0110         line = textLayout.createLine();
0111         if (line.isValid()) {
0112             wrappedText += QChar::LineSeparator;
0113         }
0114     }
0115     textLayout.endLayout();
0116 
0117     m_name->setText(wrappedText);
0118 }
0119 
0120 QString DolphinFileMetaDataWidget::name() const
0121 {
0122     return m_name->text();
0123 }
0124 
0125 void DolphinFileMetaDataWidget::setItems(const KFileItemList &items)
0126 {
0127     m_fileMetaDataWidget->setItems(items);
0128 }
0129 
0130 KFileItemList DolphinFileMetaDataWidget::items() const
0131 {
0132     return m_fileMetaDataWidget->items();
0133 }
0134 
0135 #include "moc_dolphinfilemetadatawidget.cpp"