]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/dolphinfilemetadatawidget.cpp
Merge branch 'Applications/18.08'
[dolphin.git] / src / views / tooltips / dolphinfilemetadatawidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
4 * Copyright (C) 2012 by Mark Gaiser <markg85@gmail.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 #include "dolphinfilemetadatawidget.h"
23
24 #include <KColorScheme>
25 #include <KSeparator>
26 #include <KStringHandler>
27 #include <Baloo/FileMetaDataWidget>
28
29 #include <QLabel>
30 #include <QStyleOptionFrame>
31 #include <QStylePainter>
32 #include <QTextDocument>
33 #include <QTextLayout>
34 #include <QVBoxLayout>
35
36 DolphinFileMetaDataWidget::DolphinFileMetaDataWidget(QWidget* parent) :
37 QWidget(parent),
38 m_preview(nullptr),
39 m_name(nullptr),
40 m_fileMetaDataWidget(nullptr)
41 {
42 // Create widget for file preview
43 m_preview = new QLabel(this);
44 m_preview->setAlignment(Qt::AlignTop);
45
46 // Create widget for file name
47 m_name = new QLabel(this);
48 m_name->setForegroundRole(QPalette::ToolTipText);
49 m_name->setTextFormat(Qt::PlainText);
50 m_name->setAlignment(Qt::AlignHCenter);
51
52 QFont font = m_name->font();
53 font.setBold(true);
54 m_name->setFont(font);
55
56 QFontMetrics fontMetrics(font);
57 m_name->setMaximumWidth(fontMetrics.averageCharWidth() * 40);
58
59 // Create widget for the meta data
60 m_fileMetaDataWidget = new Baloo::FileMetaDataWidget(this);
61 connect(m_fileMetaDataWidget, &Baloo::FileMetaDataWidget::metaDataRequestFinished,
62 this, &DolphinFileMetaDataWidget::metaDataRequestFinished);
63 connect(m_fileMetaDataWidget, &Baloo::FileMetaDataWidget::urlActivated,
64 this, &DolphinFileMetaDataWidget::urlActivated);
65 m_fileMetaDataWidget->setForegroundRole(QPalette::ToolTipText);
66 m_fileMetaDataWidget->setReadOnly(true);
67
68 QVBoxLayout* textLayout = new QVBoxLayout();
69 textLayout->addWidget(m_name);
70 textLayout->addWidget(new KSeparator());
71 textLayout->addWidget(m_fileMetaDataWidget);
72 textLayout->setAlignment(m_name, Qt::AlignCenter);
73 textLayout->setAlignment(m_fileMetaDataWidget, Qt::AlignLeft);
74 // Assure that the text-layout gets top-aligned by adding a stretch.
75 // Don't use textLayout->setAlignment(Qt::AlignTop) instead, as this does
76 // not work with the heightForWidth()-size-hint of m_fileMetaDataWidget
77 // (see bug #241608)
78 textLayout->addStretch();
79
80 QHBoxLayout* layout = new QHBoxLayout(this);
81 layout->addWidget(m_preview);
82 layout->addSpacing(layout->margin());
83 layout->addLayout(textLayout);
84 }
85
86 DolphinFileMetaDataWidget::~DolphinFileMetaDataWidget()
87 {
88 }
89
90 void DolphinFileMetaDataWidget::setPreview(const QPixmap& pixmap)
91 {
92 m_preview->setPixmap(pixmap);
93 }
94
95 QPixmap DolphinFileMetaDataWidget::preview() const
96 {
97 if (m_preview->pixmap()) {
98 return *m_preview->pixmap();
99 }
100 return QPixmap();
101 }
102
103 void DolphinFileMetaDataWidget::setName(const QString& name)
104 {
105 QTextOption textOption;
106 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
107
108 const QString processedName = Qt::mightBeRichText(name) ? name : KStringHandler::preProcessWrap(name);
109
110 QTextLayout textLayout(processedName);
111 textLayout.setFont(m_name->font());
112 textLayout.setTextOption(textOption);
113
114 QString wrappedText;
115 wrappedText.reserve(processedName.length());
116
117 // wrap the text to fit into the maximum width of m_name
118 textLayout.beginLayout();
119 QTextLine line = textLayout.createLine();
120 while (line.isValid()) {
121 line.setLineWidth(m_name->maximumWidth());
122 wrappedText += processedName.midRef(line.textStart(), line.textLength());
123
124 line = textLayout.createLine();
125 if (line.isValid()) {
126 wrappedText += QChar::LineSeparator;
127 }
128 }
129 textLayout.endLayout();
130
131 m_name->setText(wrappedText);
132 }
133
134 QString DolphinFileMetaDataWidget::name() const
135 {
136 return m_name->text();
137 }
138
139 void DolphinFileMetaDataWidget::setItems(const KFileItemList& items)
140 {
141 m_fileMetaDataWidget->setItems(items);
142 }
143
144 KFileItemList DolphinFileMetaDataWidget::items() const
145 {
146 return m_fileMetaDataWidget->items();
147 }
148