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