]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/filemetadatatooltip.cpp
Remove the workaround to show the tooltip temporary on a hidden position, to have...
[dolphin.git] / src / views / tooltips / filemetadatatooltip.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz@gmx.at> *
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.h>
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
44 // Create widget for file name
45 m_name = new QLabel(this);
46 QFont font = m_name->font();
47 font.setBold(true);
48 m_name->setFont(font);
49
50 // Create widget for the meta data
51 m_fileMetaDataWidget = new KFileMetaDataWidget();
52 m_fileMetaDataWidget->setForegroundRole(QPalette::ToolTipText);
53 m_fileMetaDataWidget->setReadOnly(true);
54 connect(m_fileMetaDataWidget, SIGNAL(metaDataRequestFinished()),
55 this, SIGNAL(metaDataRequestFinished()));
56
57 QVBoxLayout* textLayout = new QVBoxLayout();
58 textLayout->setAlignment(Qt::AlignTop);
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
65 QHBoxLayout* tipLayout = new QHBoxLayout(this);
66 tipLayout->addWidget(m_preview);
67 tipLayout->addLayout(textLayout);
68 }
69
70 FileMetaDataToolTip::~FileMetaDataToolTip()
71 {
72 }
73
74 void FileMetaDataToolTip::setPreview(const QPixmap& pixmap)
75 {
76 m_preview->setPixmap(pixmap);
77 }
78
79 QPixmap FileMetaDataToolTip::preview() const
80 {
81 if (m_preview->pixmap() != 0) {
82 return *m_preview->pixmap();
83 }
84 return QPixmap();
85 }
86
87 void FileMetaDataToolTip::setName(const QString& name)
88 {
89 m_name->setText(name);
90 }
91
92 QString FileMetaDataToolTip::name() const
93 {
94 return m_name->text();
95 }
96
97 void FileMetaDataToolTip::setItems(const KFileItemList& items)
98 {
99 m_fileMetaDataWidget->setItems(items);
100 }
101
102 KFileItemList FileMetaDataToolTip::items() const
103 {
104 return m_fileMetaDataWidget->items();
105 }
106
107 void FileMetaDataToolTip::paintEvent(QPaintEvent* event)
108 {
109 Q_UNUSED(event);
110
111 QPainter painter(this);
112
113 QColor toColor = palette().brush(QPalette::ToolTipBase).color();
114 QColor fromColor = KColorScheme::shade(toColor, KColorScheme::LightShade, 0.2);
115
116 const bool haveAlphaChannel = KWindowSystem::compositingActive();
117 if (haveAlphaChannel) {
118 painter.setRenderHint(QPainter::Antialiasing);
119 painter.translate(0.5, 0.5);
120 toColor.setAlpha(220);
121 fromColor.setAlpha(220);
122 }
123
124 QLinearGradient gradient(QPointF(0.0, 0.0), QPointF(0.0, height()));
125 gradient.setColorAt(0.0, fromColor);
126 gradient.setColorAt(1.0, toColor);
127 painter.setPen(Qt::NoPen);
128 painter.setBrush(gradient);
129
130 const QRect rect(0, 0, width(), height());
131 if (haveAlphaChannel) {
132 const qreal radius = 5.0;
133
134 QPainterPath path;
135 path.moveTo(rect.left(), rect.top() + radius);
136 arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90);
137 arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90);
138 arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90);
139 arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90);
140 path.closeSubpath();
141
142 painter.drawPath(path);
143 } else {
144 painter.drawRect(rect);
145 }
146 }
147
148 void FileMetaDataToolTip::arc(QPainterPath& path,
149 qreal cx, qreal cy,
150 qreal radius, qreal angle,
151 qreal sweepLength)
152 {
153 path.arcTo(cx-radius, cy-radius, radius * 2, radius * 2, angle, sweepLength);
154 }
155
156 #include "filemetadatatooltip.moc"