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 <Baloo/FileMetaDataWidget>
12 #include <KColorScheme>
14 #include <KStringHandler>
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
, this, &DolphinFileMetaDataWidget::metaDataRequestFinished
);
49 connect(m_fileMetaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
, this, &DolphinFileMetaDataWidget::urlActivated
);
50 m_fileMetaDataWidget
->setForegroundRole(QPalette::ToolTipText
);
51 m_fileMetaDataWidget
->setReadOnly(true);
53 QVBoxLayout
*textLayout
= new QVBoxLayout();
54 textLayout
->addWidget(m_name
);
55 textLayout
->addWidget(new KSeparator());
56 textLayout
->addWidget(m_fileMetaDataWidget
);
57 textLayout
->setAlignment(m_name
, Qt::AlignCenter
);
58 textLayout
->setAlignment(m_fileMetaDataWidget
, Qt::AlignLeft
);
59 // Assure that the text-layout gets top-aligned by adding a stretch.
60 // Don't use textLayout->setAlignment(Qt::AlignTop) instead, as this does
61 // not work with the heightForWidth()-size-hint of m_fileMetaDataWidget
63 textLayout
->addStretch();
65 QHBoxLayout
*layout
= new QHBoxLayout(this);
66 layout
->addWidget(m_preview
);
67 layout
->addSpacing(layout
->contentsMargins().left());
68 layout
->addLayout(textLayout
);
71 DolphinFileMetaDataWidget::~DolphinFileMetaDataWidget()
75 void DolphinFileMetaDataWidget::setPreview(const QPixmap
&pixmap
)
77 m_preview
->setPixmap(pixmap
);
80 QPixmap
DolphinFileMetaDataWidget::preview() const
82 if (!m_preview
->pixmap(Qt::ReturnByValue
).isNull()) {
83 return m_preview
->pixmap(Qt::ReturnByValue
);
89 void DolphinFileMetaDataWidget::setName(const QString
&name
)
91 QTextOption textOption
;
92 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
94 const QString processedName
= Qt::mightBeRichText(name
) ? name
: KStringHandler::preProcessWrap(name
);
96 QTextLayout
textLayout(processedName
);
97 textLayout
.setFont(m_name
->font());
98 textLayout
.setTextOption(textOption
);
101 wrappedText
.reserve(processedName
.length());
103 // wrap the text to fit into the maximum width of m_name
104 textLayout
.beginLayout();
105 QTextLine line
= textLayout
.createLine();
106 while (line
.isValid()) {
107 line
.setLineWidth(m_name
->maximumWidth());
108 wrappedText
+= QStringView(processedName
).mid(line
.textStart(), line
.textLength());
110 line
= textLayout
.createLine();
111 if (line
.isValid()) {
112 wrappedText
+= QChar::LineSeparator
;
115 textLayout
.endLayout();
117 m_name
->setText(wrappedText
);
120 QString
DolphinFileMetaDataWidget::name() const
122 return m_name
->text();
125 void DolphinFileMetaDataWidget::setItems(const KFileItemList
&items
)
127 m_fileMetaDataWidget
->setItems(items
);
130 KFileItemList
DolphinFileMetaDataWidget::items() const
132 return m_fileMetaDataWidget
->items();
135 #include "moc_dolphinfilemetadatawidget.cpp"