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