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