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> *
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. *
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. *
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 ***************************************************************************/
22 #include "filemetadatatooltip.h"
24 #include <KColorScheme>
26 #include <KWindowSystem>
27 #include <KStringHandler>
30 #include <QStyleOptionFrame>
31 #include <QStylePainter>
32 #include <QVBoxLayout>
33 #include <QTextDocument>
34 #include <QTextLayout>
38 #include <KFileMetaDataWidget>
40 #include <baloo/filemetadatawidget.h>
43 // For the blurred tooltip background
44 #include <plasma/windoweffects.h>
46 FileMetaDataToolTip::FileMetaDataToolTip(QWidget
* parent
) :
50 m_fileMetaDataWidget(0)
52 setAttribute(Qt::WA_TranslucentBackground
);
53 setWindowFlags(Qt::ToolTip
| Qt::FramelessWindowHint
);
55 // Create widget for file preview
56 m_preview
= new QLabel(this);
57 m_preview
->setAlignment(Qt::AlignTop
);
59 // Create widget for file name
60 m_name
= new QLabel(this);
61 m_name
->setForegroundRole(QPalette::ToolTipText
);
62 m_name
->setTextFormat(Qt::PlainText
);
63 m_name
->setAlignment(Qt::AlignHCenter
);
65 QFont font
= m_name
->font();
67 m_name
->setFont(font
);
69 QFontMetrics
fontMetrics(font
);
70 m_name
->setMaximumWidth(fontMetrics
.averageCharWidth() * 40);
72 // Create widget for the meta data
74 m_fileMetaDataWidget
= new KFileMetaDataWidget(this);
76 m_fileMetaDataWidget
= new Baloo::FileMetaDataWidget(this);
78 m_fileMetaDataWidget
->setForegroundRole(QPalette::ToolTipText
);
79 m_fileMetaDataWidget
->setReadOnly(true);
80 connect(m_fileMetaDataWidget
, SIGNAL(metaDataRequestFinished(KFileItemList
)),
81 this, SIGNAL(metaDataRequestFinished(KFileItemList
)));
83 QVBoxLayout
* textLayout
= new QVBoxLayout();
84 textLayout
->addWidget(m_name
);
85 textLayout
->addWidget(new KSeparator());
86 textLayout
->addWidget(m_fileMetaDataWidget
);
87 textLayout
->setAlignment(m_name
, Qt::AlignCenter
);
88 textLayout
->setAlignment(m_fileMetaDataWidget
, Qt::AlignLeft
);
89 // Assure that the text-layout gets top-aligned by adding a stretch.
90 // Don't use textLayout->setAlignment(Qt::AlignTop) instead, as this does
91 // not work with the heightForWidth()-size-hint of m_fileMetaDataWidget
93 textLayout
->addStretch();
95 QHBoxLayout
* tipLayout
= new QHBoxLayout(this);
96 tipLayout
->addWidget(m_preview
);
97 tipLayout
->addSpacing(tipLayout
->margin());
98 tipLayout
->addLayout(textLayout
);
101 FileMetaDataToolTip::~FileMetaDataToolTip()
105 void FileMetaDataToolTip::setPreview(const QPixmap
& pixmap
)
107 m_preview
->setPixmap(pixmap
);
110 QPixmap
FileMetaDataToolTip::preview() const
112 if (m_preview
->pixmap()) {
113 return *m_preview
->pixmap();
118 void FileMetaDataToolTip::setName(const QString
& name
)
120 QTextOption textOption
;
121 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
123 const QString processedName
= Qt::mightBeRichText(name
) ? name
: KStringHandler::preProcessWrap(name
);
125 QTextLayout
textLayout(processedName
);
126 textLayout
.setFont(m_name
->font());
127 textLayout
.setTextOption(textOption
);
130 wrappedText
.reserve(processedName
.length());
132 // wrap the text to fit into the maximum width of m_name
133 textLayout
.beginLayout();
134 QTextLine line
= textLayout
.createLine();
135 while (line
.isValid()) {
136 line
.setLineWidth(m_name
->maximumWidth());
137 wrappedText
+= processedName
.mid(line
.textStart(), line
.textLength());
139 line
= textLayout
.createLine();
140 if (line
.isValid()) {
141 wrappedText
+= QChar::LineSeparator
;
144 textLayout
.endLayout();
146 m_name
->setText(wrappedText
);
149 QString
FileMetaDataToolTip::name() const
151 return m_name
->text();
154 void FileMetaDataToolTip::setItems(const KFileItemList
& items
)
156 m_fileMetaDataWidget
->setItems(items
);
159 KFileItemList
FileMetaDataToolTip::items() const
161 return m_fileMetaDataWidget
->items();
164 void FileMetaDataToolTip::paintEvent(QPaintEvent
* event
)
166 QStylePainter
painter(this);
167 QStyleOptionFrame option
;
169 painter
.drawPrimitive(QStyle::PE_PanelTipLabel
, option
);
172 QWidget::paintEvent(event
);
175 void FileMetaDataToolTip::showEvent(QShowEvent
*)
177 Plasma::WindowEffects::overrideShadow(winId(), true);
178 Plasma::WindowEffects::enableBlurBehind(winId(), true, mask());
181 #include "filemetadatatooltip.moc"