]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/dolphinfilemetadatawidget.cpp
fcf51a240731db70520164f01d8dbf03b1d3add3
[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 <QTextDocument>
28
29 #include <QLabel>
30 #include <QStyleOptionFrame>
31 #include <QStylePainter>
32 #include <QVBoxLayout>
33 #include <QTextLayout>
34
35 #ifndef HAVE_BALOO
36 #include <KFileMetaDataWidget>
37 #else
38 #include <Baloo/FileMetaDataWidget>
39 #endif
40
41 DolphinFileMetaDataWidget::DolphinFileMetaDataWidget(QWidget* parent) :
42 QWidget(parent),
43 m_preview(nullptr),
44 m_name(nullptr),
45 m_fileMetaDataWidget(nullptr)
46 {
47 // Create widget for file preview
48 m_preview = new QLabel(this);
49 m_preview->setAlignment(Qt::AlignTop);
50
51 // Create widget for file name
52 m_name = new QLabel(this);
53 m_name->setForegroundRole(QPalette::ToolTipText);
54 m_name->setTextFormat(Qt::PlainText);
55 m_name->setAlignment(Qt::AlignHCenter);
56
57 QFont font = m_name->font();
58 font.setBold(true);
59 m_name->setFont(font);
60
61 QFontMetrics fontMetrics(font);
62 m_name->setMaximumWidth(fontMetrics.averageCharWidth() * 40);
63
64 // Create widget for the meta data
65 #ifndef HAVE_BALOO
66 m_fileMetaDataWidget = new KFileMetaDataWidget(this);
67 connect(m_fileMetaDataWidget, &KFileMetaDataWidget::metaDataRequestFinished,
68 this, &DolphinFileMetaDataWidget::metaDataRequestFinished);
69 connect(m_fileMetaDataWidget, &KFileMetaDataWidget::urlActivated,
70 this, &DolphinFileMetaDataWidget::urlActivated);
71 #else
72 m_fileMetaDataWidget = new Baloo::FileMetaDataWidget(this);
73 connect(m_fileMetaDataWidget, &Baloo::FileMetaDataWidget::metaDataRequestFinished,
74 this, &DolphinFileMetaDataWidget::metaDataRequestFinished);
75 connect(m_fileMetaDataWidget, &Baloo::FileMetaDataWidget::urlActivated,
76 this, &DolphinFileMetaDataWidget::urlActivated);
77 #endif
78 m_fileMetaDataWidget->setForegroundRole(QPalette::ToolTipText);
79 m_fileMetaDataWidget->setReadOnly(true);
80
81 QVBoxLayout* textLayout = new QVBoxLayout();
82 textLayout->addWidget(m_name);
83 textLayout->addWidget(new KSeparator());
84 textLayout->addWidget(m_fileMetaDataWidget);
85 textLayout->setAlignment(m_name, Qt::AlignCenter);
86 textLayout->setAlignment(m_fileMetaDataWidget, Qt::AlignLeft);
87 // Assure that the text-layout gets top-aligned by adding a stretch.
88 // Don't use textLayout->setAlignment(Qt::AlignTop) instead, as this does
89 // not work with the heightForWidth()-size-hint of m_fileMetaDataWidget
90 // (see bug #241608)
91 textLayout->addStretch();
92
93 QHBoxLayout* layout = new QHBoxLayout(this);
94 layout->addWidget(m_preview);
95 layout->addSpacing(layout->margin());
96 layout->addLayout(textLayout);
97 }
98
99 DolphinFileMetaDataWidget::~DolphinFileMetaDataWidget()
100 {
101 }
102
103 void DolphinFileMetaDataWidget::setPreview(const QPixmap& pixmap)
104 {
105 m_preview->setPixmap(pixmap);
106 }
107
108 QPixmap DolphinFileMetaDataWidget::preview() const
109 {
110 if (m_preview->pixmap()) {
111 return *m_preview->pixmap();
112 }
113 return QPixmap();
114 }
115
116 void DolphinFileMetaDataWidget::setName(const QString& name)
117 {
118 QTextOption textOption;
119 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
120
121 const QString processedName = Qt::mightBeRichText(name) ? name : KStringHandler::preProcessWrap(name);
122
123 QTextLayout textLayout(processedName);
124 textLayout.setFont(m_name->font());
125 textLayout.setTextOption(textOption);
126
127 QString wrappedText;
128 wrappedText.reserve(processedName.length());
129
130 // wrap the text to fit into the maximum width of m_name
131 textLayout.beginLayout();
132 QTextLine line = textLayout.createLine();
133 while (line.isValid()) {
134 line.setLineWidth(m_name->maximumWidth());
135 wrappedText += processedName.midRef(line.textStart(), line.textLength());
136
137 line = textLayout.createLine();
138 if (line.isValid()) {
139 wrappedText += QChar::LineSeparator;
140 }
141 }
142 textLayout.endLayout();
143
144 m_name->setText(wrappedText);
145 }
146
147 QString DolphinFileMetaDataWidget::name() const
148 {
149 return m_name->text();
150 }
151
152 void DolphinFileMetaDataWidget::setItems(const KFileItemList& items)
153 {
154 m_fileMetaDataWidget->setItems(items);
155 }
156
157 KFileItemList DolphinFileMetaDataWidget::items() const
158 {
159 return m_fileMetaDataWidget->items();
160 }
161