]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/filemetadatatooltip.cpp
Make tooltip background blurred.
[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 <kfilemetadatawidget.h>
26 #include <KSeparator>
27 #include <KWindowSystem>
28
29 #include <QLabel>
30 #include <QStyleOptionFrame>
31 #include <QStylePainter>
32 #include <QVBoxLayout>
33
34 // For the blurred tooltip background
35 #include <plasma/windoweffects.h>
36
37 FileMetaDataToolTip::FileMetaDataToolTip(QWidget* parent) :
38 QWidget(parent),
39 m_preview(0),
40 m_name(0),
41 m_fileMetaDataWidget(0)
42 {
43 setAttribute(Qt::WA_TranslucentBackground);
44 setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);
45
46 // Create widget for file preview
47 m_preview = new QLabel(this);
48 m_preview->setAlignment(Qt::AlignTop);
49
50 // Create widget for file name
51 m_name = new QLabel(this);
52 m_name->setForegroundRole(QPalette::ToolTipText);
53 QFont font = m_name->font();
54 font.setBold(true);
55 m_name->setFont(font);
56
57 // Create widget for the meta data
58 m_fileMetaDataWidget = new KFileMetaDataWidget(this);
59 m_fileMetaDataWidget->setForegroundRole(QPalette::ToolTipText);
60 m_fileMetaDataWidget->setReadOnly(true);
61 connect(m_fileMetaDataWidget, SIGNAL(metaDataRequestFinished(KFileItemList)),
62 this, SIGNAL(metaDataRequestFinished(KFileItemList)));
63
64 QVBoxLayout* textLayout = new QVBoxLayout();
65 textLayout->addWidget(m_name);
66 textLayout->addWidget(new KSeparator());
67 textLayout->addWidget(m_fileMetaDataWidget);
68 textLayout->setAlignment(m_name, Qt::AlignCenter);
69 textLayout->setAlignment(m_fileMetaDataWidget, Qt::AlignLeft);
70 // Assure that the text-layout gets top-aligned by adding a stretch.
71 // Don't use textLayout->setAlignment(Qt::AlignTop) instead, as this does
72 // not work with the heightForWidth()-size-hint of m_fileMetaDataWidget
73 // (see bug #241608)
74 textLayout->addStretch();
75
76 QHBoxLayout* tipLayout = new QHBoxLayout(this);
77 tipLayout->addWidget(m_preview);
78 tipLayout->addSpacing(tipLayout->margin());
79 tipLayout->addLayout(textLayout);
80 }
81
82 FileMetaDataToolTip::~FileMetaDataToolTip()
83 {
84 }
85
86 void FileMetaDataToolTip::setPreview(const QPixmap& pixmap)
87 {
88 m_preview->setPixmap(pixmap);
89 }
90
91 QPixmap FileMetaDataToolTip::preview() const
92 {
93 if (m_preview->pixmap()) {
94 return *m_preview->pixmap();
95 }
96 return QPixmap();
97 }
98
99 void FileMetaDataToolTip::setName(const QString& name)
100 {
101 m_name->setText(name);
102 }
103
104 QString FileMetaDataToolTip::name() const
105 {
106 return m_name->text();
107 }
108
109 void FileMetaDataToolTip::setItems(const KFileItemList& items)
110 {
111 m_fileMetaDataWidget->setItems(items);
112 }
113
114 KFileItemList FileMetaDataToolTip::items() const
115 {
116 return m_fileMetaDataWidget->items();
117 }
118
119 void FileMetaDataToolTip::paintEvent(QPaintEvent* event)
120 {
121 QStylePainter painter(this);
122 QStyleOptionFrame option;
123 option.init(this);
124 painter.drawPrimitive(QStyle::PE_PanelTipLabel, option);
125 painter.end();
126
127 QWidget::paintEvent(event);
128 }
129
130 void FileMetaDataToolTip::showEvent(QShowEvent *)
131 {
132 Plasma::WindowEffects::overrideShadow(winId(), true);
133 Plasma::WindowEffects::enableBlurBehind(winId(), true, mask());
134 }
135
136 #include "filemetadatatooltip.moc"