]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/filemetadatatooltip.cpp
Use capitalized includes of recently committed kdelibs headers
[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 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "filemetadatatooltip.h"
22
23 #include <KColorScheme>
24 #include <KFileMetaDataWidget>
25 #include <KSeparator>
26 #include <KWindowSystem>
27
28 #include <QLabel>
29 #include <QPainter>
30 #include <QVBoxLayout>
31
32 FileMetaDataToolTip::FileMetaDataToolTip(QWidget* parent) :
33 QWidget(parent),
34 m_preview(0),
35 m_name(0),
36 m_fileMetaDataWidget(0)
37 {
38 setAttribute(Qt::WA_TranslucentBackground);
39 setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);
40
41 // Create widget for file preview
42 m_preview = new QLabel(this);
43 m_preview->setAlignment(Qt::AlignTop);
44
45 // Create widget for file name
46 m_name = new QLabel(this);
47 QFont font = m_name->font();
48 font.setBold(true);
49 m_name->setFont(font);
50
51 // Create widget for the meta data
52 m_fileMetaDataWidget = new KFileMetaDataWidget();
53 m_fileMetaDataWidget->setForegroundRole(QPalette::ToolTipText);
54 m_fileMetaDataWidget->setReadOnly(true);
55 connect(m_fileMetaDataWidget, SIGNAL(metaDataRequestFinished(KFileItemList)),
56 this, SIGNAL(metaDataRequestFinished(KFileItemList)));
57
58 QVBoxLayout* textLayout = new QVBoxLayout();
59 textLayout->addWidget(m_name);
60 textLayout->addWidget(new KSeparator());
61 textLayout->addWidget(m_fileMetaDataWidget);
62 textLayout->setAlignment(m_name, Qt::AlignCenter);
63 textLayout->setAlignment(m_fileMetaDataWidget, Qt::AlignLeft);
64 // Assure that the text-layout gets top-aligned by adding a stretch.
65 // Don't use textLayout->setAlignment(Qt::AlignTop) instead, as this does
66 // not work with the heightForWidth()-size-hint of m_fileMetaDataWidget
67 // (see bug #241608)
68 textLayout->addStretch();
69
70 QHBoxLayout* tipLayout = new QHBoxLayout(this);
71 tipLayout->addWidget(m_preview);
72 tipLayout->addSpacing(tipLayout->margin());
73 tipLayout->addLayout(textLayout);
74 }
75
76 FileMetaDataToolTip::~FileMetaDataToolTip()
77 {
78 }
79
80 void FileMetaDataToolTip::setPreview(const QPixmap& pixmap)
81 {
82 m_preview->setPixmap(pixmap);
83 }
84
85 QPixmap FileMetaDataToolTip::preview() const
86 {
87 if (m_preview->pixmap() != 0) {
88 return *m_preview->pixmap();
89 }
90 return QPixmap();
91 }
92
93 void FileMetaDataToolTip::setName(const QString& name)
94 {
95 m_name->setText(name);
96 }
97
98 QString FileMetaDataToolTip::name() const
99 {
100 return m_name->text();
101 }
102
103 void FileMetaDataToolTip::setItems(const KFileItemList& items)
104 {
105 m_fileMetaDataWidget->setItems(items);
106 }
107
108 KFileItemList FileMetaDataToolTip::items() const
109 {
110 return m_fileMetaDataWidget->items();
111 }
112
113 void FileMetaDataToolTip::paintEvent(QPaintEvent* event)
114 {
115 Q_UNUSED(event);
116
117 QPainter painter(this);
118
119 QColor toColor = palette().brush(QPalette::ToolTipBase).color();
120 QColor fromColor = KColorScheme::shade(toColor, KColorScheme::LightShade, 0.2);
121
122 const bool haveAlphaChannel = KWindowSystem::compositingActive();
123 if (haveAlphaChannel) {
124 painter.setRenderHint(QPainter::Antialiasing);
125 painter.translate(0.5, 0.5);
126 toColor.setAlpha(220);
127 fromColor.setAlpha(220);
128 }
129
130 QLinearGradient gradient(QPointF(0.0, 0.0), QPointF(0.0, height()));
131 gradient.setColorAt(0.0, fromColor);
132 gradient.setColorAt(1.0, toColor);
133 painter.setPen(Qt::NoPen);
134 painter.setBrush(gradient);
135
136 const QRect rect(0, 0, width(), height());
137 if (haveAlphaChannel) {
138 const qreal radius = 5.0;
139
140 QPainterPath path;
141 path.moveTo(rect.left(), rect.top() + radius);
142 arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90);
143 arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90);
144 arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90);
145 arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90);
146 path.closeSubpath();
147
148 painter.drawPath(path);
149 } else {
150 painter.drawRect(rect);
151 }
152 }
153
154 void FileMetaDataToolTip::arc(QPainterPath& path,
155 qreal cx, qreal cy,
156 qreal radius, qreal angle,
157 qreal sweepLength)
158 {
159 path.arcTo(cx-radius, cy-radius, radius * 2, radius * 2, angle, sweepLength);
160 }
161
162 #include "filemetadatatooltip.moc"