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 "dolphinfilemetadatawidget.h"
24 #include <KColorScheme>
26 #include <KStringHandler>
28 #include <KFileMetaDataWidget>
30 #include <Baloo/FileMetaDataWidget>
34 #include <QStyleOptionFrame>
35 #include <QStylePainter>
36 #include <QTextDocument>
37 #include <QTextLayout>
38 #include <QVBoxLayout>
40 DolphinFileMetaDataWidget::DolphinFileMetaDataWidget(QWidget
* parent
) :
44 m_fileMetaDataWidget(nullptr)
46 // Create widget for file preview
47 m_preview
= new QLabel(this);
48 m_preview
->setAlignment(Qt::AlignTop
);
50 // Create widget for file name
51 m_name
= new QLabel(this);
52 m_name
->setForegroundRole(QPalette::ToolTipText
);
53 m_name
->setTextFormat(Qt::PlainText
);
54 m_name
->setAlignment(Qt::AlignHCenter
);
56 QFont font
= m_name
->font();
58 m_name
->setFont(font
);
60 QFontMetrics
fontMetrics(font
);
61 m_name
->setMaximumWidth(fontMetrics
.averageCharWidth() * 40);
63 // Create widget for the meta data
65 m_fileMetaDataWidget
= new KFileMetaDataWidget(this);
66 connect(m_fileMetaDataWidget
, &KFileMetaDataWidget::metaDataRequestFinished
,
67 this, &DolphinFileMetaDataWidget::metaDataRequestFinished
);
68 connect(m_fileMetaDataWidget
, &KFileMetaDataWidget::urlActivated
,
69 this, &DolphinFileMetaDataWidget::urlActivated
);
71 m_fileMetaDataWidget
= new Baloo::FileMetaDataWidget(this);
72 connect(m_fileMetaDataWidget
, &Baloo::FileMetaDataWidget::metaDataRequestFinished
,
73 this, &DolphinFileMetaDataWidget::metaDataRequestFinished
);
74 connect(m_fileMetaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
75 this, &DolphinFileMetaDataWidget::urlActivated
);
77 m_fileMetaDataWidget
->setForegroundRole(QPalette::ToolTipText
);
78 m_fileMetaDataWidget
->setReadOnly(true);
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
90 textLayout
->addStretch();
92 QHBoxLayout
* layout
= new QHBoxLayout(this);
93 layout
->addWidget(m_preview
);
94 layout
->addSpacing(layout
->margin());
95 layout
->addLayout(textLayout
);
98 DolphinFileMetaDataWidget::~DolphinFileMetaDataWidget()
102 void DolphinFileMetaDataWidget::setPreview(const QPixmap
& pixmap
)
104 m_preview
->setPixmap(pixmap
);
107 QPixmap
DolphinFileMetaDataWidget::preview() const
109 if (m_preview
->pixmap()) {
110 return *m_preview
->pixmap();
115 void DolphinFileMetaDataWidget::setName(const QString
& name
)
117 QTextOption textOption
;
118 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
120 const QString processedName
= Qt::mightBeRichText(name
) ? name
: KStringHandler::preProcessWrap(name
);
122 QTextLayout
textLayout(processedName
);
123 textLayout
.setFont(m_name
->font());
124 textLayout
.setTextOption(textOption
);
127 wrappedText
.reserve(processedName
.length());
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());
136 line
= textLayout
.createLine();
137 if (line
.isValid()) {
138 wrappedText
+= QChar::LineSeparator
;
141 textLayout
.endLayout();
143 m_name
->setText(wrappedText
);
146 QString
DolphinFileMetaDataWidget::name() const
148 return m_name
->text();
151 void DolphinFileMetaDataWidget::setItems(const KFileItemList
& items
)
153 m_fileMetaDataWidget
->setItems(items
);
156 KFileItemList
DolphinFileMetaDataWidget::items() const
158 return m_fileMetaDataWidget
->items();