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