]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/dolphinfilemetadatawidget.cpp
80d22d837f82a722914b4681e20d589d7a27ef8f
[dolphin.git] / src / views / tooltips / dolphinfilemetadatawidget.cpp
1 /*
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>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "dolphinfilemetadatawidget.h"
10
11 #include <KColorScheme>
12 #include <KSeparator>
13 #include <KStringHandler>
14 #include <Baloo/FileMetaDataWidget>
15
16 #include <QLabel>
17 #include <QStyleOptionFrame>
18 #include <QStylePainter>
19 #include <QTextDocument>
20 #include <QTextLayout>
21 #include <QVBoxLayout>
22
23 DolphinFileMetaDataWidget::DolphinFileMetaDataWidget(QWidget* parent) :
24 QWidget(parent),
25 m_preview(nullptr),
26 m_name(nullptr),
27 m_fileMetaDataWidget(nullptr)
28 {
29 // Create widget for file preview
30 m_preview = new QLabel(this);
31 m_preview->setAlignment(Qt::AlignTop);
32
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);
38
39 QFont font = m_name->font();
40 font.setBold(true);
41 m_name->setFont(font);
42
43 QFontMetrics fontMetrics(font);
44 m_name->setMaximumWidth(fontMetrics.averageCharWidth() * 40);
45
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);
54
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
64 // (see bug #241608)
65 textLayout->addStretch();
66
67 QHBoxLayout* layout = new QHBoxLayout(this);
68 layout->addWidget(m_preview);
69 layout->addSpacing(layout->contentsMargins().left());
70 layout->addLayout(textLayout);
71 }
72
73 DolphinFileMetaDataWidget::~DolphinFileMetaDataWidget()
74 {
75 }
76
77 void DolphinFileMetaDataWidget::setPreview(const QPixmap& pixmap)
78 {
79 m_preview->setPixmap(pixmap);
80 }
81
82 QPixmap DolphinFileMetaDataWidget::preview() const
83 {
84 if (!m_preview->pixmap(Qt::ReturnByValue).isNull()) {
85 return m_preview->pixmap(Qt::ReturnByValue);
86 }
87
88 return QPixmap();
89 }
90
91 void DolphinFileMetaDataWidget::setName(const QString& name)
92 {
93 QTextOption textOption;
94 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
95
96 const QString processedName = Qt::mightBeRichText(name) ? name : KStringHandler::preProcessWrap(name);
97
98 QTextLayout textLayout(processedName);
99 textLayout.setFont(m_name->font());
100 textLayout.setTextOption(textOption);
101
102 QString wrappedText;
103 wrappedText.reserve(processedName.length());
104
105 // wrap the text to fit into the maximum width of m_name
106 textLayout.beginLayout();
107 QTextLine line = textLayout.createLine();
108 while (line.isValid()) {
109 line.setLineWidth(m_name->maximumWidth());
110 wrappedText += QStringView(processedName).mid(line.textStart(), line.textLength());
111
112 line = textLayout.createLine();
113 if (line.isValid()) {
114 wrappedText += QChar::LineSeparator;
115 }
116 }
117 textLayout.endLayout();
118
119 m_name->setText(wrappedText);
120 }
121
122 QString DolphinFileMetaDataWidget::name() const
123 {
124 return m_name->text();
125 }
126
127 void DolphinFileMetaDataWidget::setItems(const KFileItemList& items)
128 {
129 m_fileMetaDataWidget->setItems(items);
130 }
131
132 KFileItemList DolphinFileMetaDataWidget::items() const
133 {
134 return m_fileMetaDataWidget->items();
135 }
136