]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/filemetadatatooltip.cpp
Port tooltips to KToolTipWidget
[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 * Copyright (C) 2012 by Mark Gaiser <markg85@gmail.com> *
5 * *
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. *
10 * *
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. *
15 * *
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 ***************************************************************************/
21
22 #include "filemetadatatooltip.h"
23
24 #include <KColorScheme>
25 #include <KSeparator>
26 // For the blurred tooltip background
27 #include <KWindowEffects>
28 #include <KStringHandler>
29 #include <QTextDocument>
30
31 #include <QLabel>
32 #include <QStyleOptionFrame>
33 #include <QStylePainter>
34 #include <QVBoxLayout>
35 #include <QTextLayout>
36 #include <QTextLine>
37
38 #ifndef HAVE_BALOO
39 #include <KFileMetaDataWidget>
40 #else
41 #include <Baloo/FileMetaDataWidget>
42 #endif
43
44 FileMetaDataToolTip::FileMetaDataToolTip(QWidget* parent) :
45 QWidget(parent),
46 m_preview(0),
47 m_name(0),
48 m_fileMetaDataWidget(0)
49 {
50 // Create widget for file preview
51 m_preview = new QLabel(this);
52 m_preview->setAlignment(Qt::AlignTop);
53
54 // Create widget for file name
55 m_name = new QLabel(this);
56 m_name->setForegroundRole(QPalette::ToolTipText);
57 m_name->setTextFormat(Qt::PlainText);
58 m_name->setAlignment(Qt::AlignHCenter);
59
60 QFont font = m_name->font();
61 font.setBold(true);
62 m_name->setFont(font);
63
64 QFontMetrics fontMetrics(font);
65 m_name->setMaximumWidth(fontMetrics.averageCharWidth() * 40);
66
67 // Create widget for the meta data
68 #ifndef HAVE_BALOO
69 m_fileMetaDataWidget = new KFileMetaDataWidget(this);
70 connect(m_fileMetaDataWidget, &KFileMetaDataWidget::metaDataRequestFinished,
71 this, &FileMetaDataToolTip::metaDataRequestFinished);
72 #else
73 m_fileMetaDataWidget = new Baloo::FileMetaDataWidget(this);
74 connect(m_fileMetaDataWidget, &Baloo::FileMetaDataWidget::metaDataRequestFinished,
75 this, &FileMetaDataToolTip::metaDataRequestFinished);
76 #endif
77 m_fileMetaDataWidget->setForegroundRole(QPalette::ToolTipText);
78 m_fileMetaDataWidget->setReadOnly(true);
79
80 QVBoxLayout* textLayout = new QVBoxLayout();
81 textLayout->addWidget(m_name);
82 textLayout->addWidget(new KSeparator());
83 textLayout->addWidget(m_fileMetaDataWidget);
84 textLayout->setAlignment(m_name, Qt::AlignCenter);
85 textLayout->setAlignment(m_fileMetaDataWidget, Qt::AlignLeft);
86 // Assure that the text-layout gets top-aligned by adding a stretch.
87 // Don't use textLayout->setAlignment(Qt::AlignTop) instead, as this does
88 // not work with the heightForWidth()-size-hint of m_fileMetaDataWidget
89 // (see bug #241608)
90 textLayout->addStretch();
91
92 QHBoxLayout* tipLayout = new QHBoxLayout(this);
93 tipLayout->addWidget(m_preview);
94 tipLayout->addSpacing(tipLayout->margin());
95 tipLayout->addLayout(textLayout);
96 }
97
98 FileMetaDataToolTip::~FileMetaDataToolTip()
99 {
100 }
101
102 void FileMetaDataToolTip::setPreview(const QPixmap& pixmap)
103 {
104 m_preview->setPixmap(pixmap);
105 }
106
107 QPixmap FileMetaDataToolTip::preview() const
108 {
109 if (m_preview->pixmap()) {
110 return *m_preview->pixmap();
111 }
112 return QPixmap();
113 }
114
115 void FileMetaDataToolTip::setName(const QString& name)
116 {
117 QTextOption textOption;
118 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
119
120 const QString processedName = Qt::mightBeRichText(name) ? name : KStringHandler::preProcessWrap(name);
121
122 QTextLayout textLayout(processedName);
123 textLayout.setFont(m_name->font());
124 textLayout.setTextOption(textOption);
125
126 QString wrappedText;
127 wrappedText.reserve(processedName.length());
128
129 // wrap the text to fit into the maximum width of m_name
130 textLayout.beginLayout();
131 QTextLine line = textLayout.createLine();
132 while (line.isValid()) {
133 line.setLineWidth(m_name->maximumWidth());
134 wrappedText += processedName.midRef(line.textStart(), line.textLength());
135
136 line = textLayout.createLine();
137 if (line.isValid()) {
138 wrappedText += QChar::LineSeparator;
139 }
140 }
141 textLayout.endLayout();
142
143 m_name->setText(wrappedText);
144 }
145
146 QString FileMetaDataToolTip::name() const
147 {
148 return m_name->text();
149 }
150
151 void FileMetaDataToolTip::setItems(const KFileItemList& items)
152 {
153 m_fileMetaDataWidget->setItems(items);
154 }
155
156 KFileItemList FileMetaDataToolTip::items() const
157 {
158 return m_fileMetaDataWidget->items();
159 }
160