2 * SPDX-FileCopyrightText: 2010 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2008 Fredrik Höglund <fredrik@kde.org>
4 * SPDX-FileCopyrightText: 2012 Mark Gaiser <markg85@gmail.com>
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #include "dolphinfilemetadatawidget.h"
11 #include <KColorScheme>
13 #include <KStringHandler>
14 #include <Baloo/FileMetaDataWidget>
17 #include <QStyleOptionFrame>
18 #include <QStylePainter>
19 #include <QTextDocument>
20 #include <QTextLayout>
21 #include <QVBoxLayout>
23 DolphinFileMetaDataWidget::DolphinFileMetaDataWidget(QWidget
* parent
) :
27 m_fileMetaDataWidget(nullptr)
29 // Create widget for file preview
30 m_preview
= new QLabel(this);
31 m_preview
->setAlignment(Qt::AlignTop
);
33 // Create widget for file name
34 m_name
= new QLabel(this);
35 m_name
->setForegroundRole(QPalette::ToolTipText
);
36 m_name
->setTextFormat(Qt::PlainText
);
37 m_name
->setAlignment(Qt::AlignHCenter
);
39 QFont font
= m_name
->font();
41 m_name
->setFont(font
);
43 QFontMetrics
fontMetrics(font
);
44 m_name
->setMaximumWidth(fontMetrics
.averageCharWidth() * 40);
46 // Create widget for the meta data
47 m_fileMetaDataWidget
= new Baloo::FileMetaDataWidget(this);
48 connect(m_fileMetaDataWidget
, &Baloo::FileMetaDataWidget::metaDataRequestFinished
,
49 this, &DolphinFileMetaDataWidget::metaDataRequestFinished
);
50 connect(m_fileMetaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
51 this, &DolphinFileMetaDataWidget::urlActivated
);
52 m_fileMetaDataWidget
->setForegroundRole(QPalette::ToolTipText
);
53 m_fileMetaDataWidget
->setReadOnly(true);
55 QVBoxLayout
* textLayout
= new QVBoxLayout();
56 textLayout
->addWidget(m_name
);
57 textLayout
->addWidget(new KSeparator());
58 textLayout
->addWidget(m_fileMetaDataWidget
);
59 textLayout
->setAlignment(m_name
, Qt::AlignCenter
);
60 textLayout
->setAlignment(m_fileMetaDataWidget
, Qt::AlignLeft
);
61 // Assure that the text-layout gets top-aligned by adding a stretch.
62 // Don't use textLayout->setAlignment(Qt::AlignTop) instead, as this does
63 // not work with the heightForWidth()-size-hint of m_fileMetaDataWidget
65 textLayout
->addStretch();
67 QHBoxLayout
* layout
= new QHBoxLayout(this);
68 layout
->addWidget(m_preview
);
69 layout
->addSpacing(layout
->contentsMargins().left());
70 layout
->addLayout(textLayout
);
73 DolphinFileMetaDataWidget::~DolphinFileMetaDataWidget()
77 void DolphinFileMetaDataWidget::setPreview(const QPixmap
& pixmap
)
79 m_preview
->setPixmap(pixmap
);
82 QPixmap
DolphinFileMetaDataWidget::preview() const
84 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
85 if (m_preview
->pixmap()) {
86 return *m_preview
->pixmap();
89 if (!m_preview
->pixmap(Qt::ReturnByValue
).isNull()) {
90 return m_preview
->pixmap(Qt::ReturnByValue
);
97 void DolphinFileMetaDataWidget::setName(const QString
& name
)
99 QTextOption textOption
;
100 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
102 const QString processedName
= Qt::mightBeRichText(name
) ? name
: KStringHandler::preProcessWrap(name
);
104 QTextLayout
textLayout(processedName
);
105 textLayout
.setFont(m_name
->font());
106 textLayout
.setTextOption(textOption
);
109 wrappedText
.reserve(processedName
.length());
111 // wrap the text to fit into the maximum width of m_name
112 textLayout
.beginLayout();
113 QTextLine line
= textLayout
.createLine();
114 while (line
.isValid()) {
115 line
.setLineWidth(m_name
->maximumWidth());
116 wrappedText
+= processedName
.midRef(line
.textStart(), line
.textLength());
118 line
= textLayout
.createLine();
119 if (line
.isValid()) {
120 wrappedText
+= QChar::LineSeparator
;
123 textLayout
.endLayout();
125 m_name
->setText(wrappedText
);
128 QString
DolphinFileMetaDataWidget::name() const
130 return m_name
->text();
133 void DolphinFileMetaDataWidget::setItems(const KFileItemList
& items
)
135 m_fileMetaDataWidget
->setItems(items
);
138 KFileItemList
DolphinFileMetaDataWidget::items() const
140 return m_fileMetaDataWidget
->items();