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