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> *
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. *
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. *
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 ***************************************************************************/
22 #include "filemetadatatooltip.h"
24 #include <KColorScheme>
26 #include <KWindowSystem>
27 // For the blurred tooltip background
28 #include <KWindowEffects>
29 #include <KStringHandler>
32 #include <QStyleOptionFrame>
33 #include <QStylePainter>
34 #include <QVBoxLayout>
35 #include <QTextDocument>
36 #include <QTextLayout>
40 #include <KFileMetaDataWidget>
42 #include <baloo/filemetadatawidget.h>
45 FileMetaDataToolTip::FileMetaDataToolTip(QWidget
* parent
) :
49 m_fileMetaDataWidget(0)
51 setAttribute(Qt::WA_TranslucentBackground
);
52 setWindowFlags(Qt::ToolTip
| Qt::FramelessWindowHint
);
54 // Create widget for file preview
55 m_preview
= new QLabel(this);
56 m_preview
->setAlignment(Qt::AlignTop
);
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
);
64 QFont font
= m_name
->font();
66 m_name
->setFont(font
);
68 QFontMetrics
fontMetrics(font
);
69 m_name
->setMaximumWidth(fontMetrics
.averageCharWidth() * 40);
71 // Create widget for the meta data
73 m_fileMetaDataWidget
= new KFileMetaDataWidget(this);
75 m_fileMetaDataWidget
= new Baloo::FileMetaDataWidget(this);
77 m_fileMetaDataWidget
->setForegroundRole(QPalette::ToolTipText
);
78 m_fileMetaDataWidget
->setReadOnly(true);
79 connect(m_fileMetaDataWidget
, SIGNAL(metaDataRequestFinished(KFileItemList
)),
80 this, SIGNAL(metaDataRequestFinished(KFileItemList
)));
82 QVBoxLayout
* textLayout
= new QVBoxLayout();
83 textLayout
->addWidget(m_name
);
84 textLayout
->addWidget(new KSeparator());
85 textLayout
->addWidget(m_fileMetaDataWidget
);
86 textLayout
->setAlignment(m_name
, Qt::AlignCenter
);
87 textLayout
->setAlignment(m_fileMetaDataWidget
, Qt::AlignLeft
);
88 // Assure that the text-layout gets top-aligned by adding a stretch.
89 // Don't use textLayout->setAlignment(Qt::AlignTop) instead, as this does
90 // not work with the heightForWidth()-size-hint of m_fileMetaDataWidget
92 textLayout
->addStretch();
94 QHBoxLayout
* tipLayout
= new QHBoxLayout(this);
95 tipLayout
->addWidget(m_preview
);
96 tipLayout
->addSpacing(tipLayout
->margin());
97 tipLayout
->addLayout(textLayout
);
100 FileMetaDataToolTip::~FileMetaDataToolTip()
104 void FileMetaDataToolTip::setPreview(const QPixmap
& pixmap
)
106 m_preview
->setPixmap(pixmap
);
109 QPixmap
FileMetaDataToolTip::preview() const
111 if (m_preview
->pixmap()) {
112 return *m_preview
->pixmap();
117 void FileMetaDataToolTip::setName(const QString
& name
)
119 QTextOption textOption
;
120 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
122 const QString processedName
= Qt::mightBeRichText(name
) ? name
: KStringHandler::preProcessWrap(name
);
124 QTextLayout
textLayout(processedName
);
125 textLayout
.setFont(m_name
->font());
126 textLayout
.setTextOption(textOption
);
129 wrappedText
.reserve(processedName
.length());
131 // wrap the text to fit into the maximum width of m_name
132 textLayout
.beginLayout();
133 QTextLine line
= textLayout
.createLine();
134 while (line
.isValid()) {
135 line
.setLineWidth(m_name
->maximumWidth());
136 wrappedText
+= processedName
.mid(line
.textStart(), line
.textLength());
138 line
= textLayout
.createLine();
139 if (line
.isValid()) {
140 wrappedText
+= QChar::LineSeparator
;
143 textLayout
.endLayout();
145 m_name
->setText(wrappedText
);
148 QString
FileMetaDataToolTip::name() const
150 return m_name
->text();
153 void FileMetaDataToolTip::setItems(const KFileItemList
& items
)
155 m_fileMetaDataWidget
->setItems(items
);
158 KFileItemList
FileMetaDataToolTip::items() const
160 return m_fileMetaDataWidget
->items();
163 void FileMetaDataToolTip::paintEvent(QPaintEvent
* event
)
165 QStylePainter
painter(this);
166 QStyleOptionFrame option
;
168 painter
.drawPrimitive(QStyle::PE_PanelTipLabel
, option
);
171 QWidget::paintEvent(event
);
174 void FileMetaDataToolTip::showEvent(QShowEvent
*)
176 #pragma message("TODO: port Plasma::WindowEffects::overrideShadow")
177 //Plasma::WindowEffects::overrideShadow(winId(), true);
178 KWindowEffects::enableBlurBehind(winId(), true, mask());
181 #include "filemetadatatooltip.moc"