]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/filemetadatatooltip.cpp
Sourcecode hierarchy cleanup: Move folders "tooltips" and "versioncontrol" into ...
[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.h>
24 #include <kfilemetadatawidget.h>
25 #include <kseparator.h>
26 #include <kwindowsystem.h>
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 {
39 setAttribute(Qt::WA_TranslucentBackground);
40 setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);
41
42 // Create widget for file preview
43 m_preview = new QLabel(this);
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 m_name->setAlignment(Qt::AlignHCenter);
51
52 // Create widget for the meta data
53 m_fileMetaDataWidget = new KFileMetaDataWidget();
54 m_fileMetaDataWidget->setForegroundRole(QPalette::ToolTipText);
55 m_fileMetaDataWidget->setReadOnly(true);
56
57 // The stretchwidget allows the metadata widget to be top aligned and fills
58 // the remaining vertical space
59 QWidget* stretchWidget = new QWidget(this);
60 stretchWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
61
62 QWidget* textContainer = new QWidget(this);
63 QVBoxLayout* textLayout = new QVBoxLayout(textContainer);
64 textLayout->addWidget(m_name);
65 textLayout->addWidget(new KSeparator());
66 textLayout->addWidget(m_fileMetaDataWidget);
67 textLayout->addWidget(stretchWidget);
68
69 QHBoxLayout* tipLayout = new QHBoxLayout(this);
70 tipLayout->addWidget(m_preview);
71 tipLayout->addWidget(textContainer);
72
73 tipLayout->setSizeConstraint(QLayout::SetFixedSize);
74 }
75
76 FileMetaDataToolTip::~FileMetaDataToolTip()
77 {
78 }
79
80 void FileMetaDataToolTip::setPreview(const QPixmap& pixmap)
81 {
82 m_preview->setPixmap(pixmap);
83 }
84
85 const QPixmap* FileMetaDataToolTip::preview() const
86 {
87 return m_preview->pixmap();
88 }
89
90 void FileMetaDataToolTip::setName(const QString& name)
91 {
92 m_name->setText(name);
93 }
94
95 QString FileMetaDataToolTip::name() const
96 {
97 return m_name->text();
98 }
99
100 void FileMetaDataToolTip::setItems(const KFileItemList& items)
101 {
102 m_fileMetaDataWidget->setItems(items);
103 }
104
105 KFileItemList FileMetaDataToolTip::items() const
106 {
107 return m_fileMetaDataWidget->items();
108 }
109
110 void FileMetaDataToolTip::paintEvent(QPaintEvent* event)
111 {
112 Q_UNUSED(event);
113
114 QPainter painter(this);
115
116 QColor toColor = palette().brush(QPalette::ToolTipBase).color();
117 QColor fromColor = KColorScheme::shade(toColor, KColorScheme::LightShade, 0.2);
118
119 const bool haveAlphaChannel = KWindowSystem::compositingActive();
120 if (haveAlphaChannel) {
121 painter.setRenderHint(QPainter::Antialiasing);
122 painter.translate(0.5, 0.5);
123 toColor.setAlpha(220);
124 fromColor.setAlpha(220);
125 }
126
127 QLinearGradient gradient(QPointF(0.0, 0.0), QPointF(0.0, height()));
128 gradient.setColorAt(0.0, fromColor);
129 gradient.setColorAt(1.0, toColor);
130 painter.setPen(Qt::NoPen);
131 painter.setBrush(gradient);
132
133 const QRect rect(0, 0, width(), height());
134 if (haveAlphaChannel) {
135 const qreal radius = 5.0;
136
137 QPainterPath path;
138 path.moveTo(rect.left(), rect.top() + radius);
139 arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90);
140 arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90);
141 arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90);
142 arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90);
143 path.closeSubpath();
144
145 painter.drawPath(path);
146 } else {
147 painter.drawRect(rect);
148 }
149 }
150
151 void FileMetaDataToolTip::arc(QPainterPath& path,
152 qreal cx, qreal cy,
153 qreal radius, qreal angle,
154 qreal sweepLength)
155 {
156 path.arcTo(cx-radius, cy-radius, radius * 2, radius * 2, angle, sweepLength);
157 }
158
159 #include "filemetadatatooltip.moc"