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