Changeset View
Changeset View
Standalone View
Standalone View
src/widgetfactory.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | Copyright (C) 2012-2014 Vishesh Handa <vhanda@kde.org> | 2 | Copyright (C) 2012-2014 Vishesh Handa <vhanda@kde.org> | ||
3 | Copyright (C) 2018 Michael Heidelbach <ottwolt@gmail.com> | ||||
3 | 4 | | |||
4 | Code largely copied/adapted from KFileMetadataProvider | 5 | Code largely copied/adapted from KFileMetadataProvider | ||
5 | Copyright (C) 2010 by Peter Penz <peter.penz@gmx.at> | 6 | Copyright (C) 2010 by Peter Penz <peter.penz@gmx.at> | ||
6 | 7 | | |||
7 | This library is free software; you can redistribute it and/or | 8 | This library is free software; you can redistribute it and/or | ||
8 | modify it under the terms of the GNU Lesser General Public | 9 | modify it under the terms of the GNU Lesser General Public | ||
9 | License as published by the Free Software Foundation; either | 10 | License as published by the Free Software Foundation; either | ||
10 | version 2.1 of the License, or (at your option) any later version. | 11 | version 2.1 of the License, or (at your option) any later version. | ||
Show All 16 Lines | |||||
27 | 28 | | |||
28 | #include <KFileMetaData/UserMetaData> | 29 | #include <KFileMetaData/UserMetaData> | ||
29 | 30 | | |||
30 | #include <QtWidgets/QLabel> | 31 | #include <QtWidgets/QLabel> | ||
31 | #include <QtCore/QCollator> | 32 | #include <QtCore/QCollator> | ||
32 | #include <QtCore/QTime> | 33 | #include <QtCore/QTime> | ||
33 | #include <QtCore/QUrl> | 34 | #include <QtCore/QUrl> | ||
34 | #include <QtCore/QLocale> | 35 | #include <QtCore/QLocale> | ||
36 | #include <QRegularExpression> | ||||
35 | 37 | | |||
36 | #include <KJob> | 38 | #include <KJob> | ||
37 | #include <KFormat> | 39 | #include <KFormat> | ||
38 | #include <KLocalizedString> | 40 | #include <KLocalizedString> | ||
39 | 41 | | |||
40 | namespace { | 42 | namespace { | ||
41 | static QString plainText(const QString& richText) | 43 | static QString plainText(const QString& richText) | ||
42 | { | 44 | { | ||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 82 | switch (value.type()) { | |||
95 | default: | 97 | default: | ||
96 | return value.toString(); | 98 | return value.toString(); | ||
97 | } | 99 | } | ||
98 | } | 100 | } | ||
99 | 101 | | |||
100 | QWidget* WidgetFactory::createWidget(const QString& prop, const QVariant& value, QWidget* parent) | 102 | QWidget* WidgetFactory::createWidget(const QString& prop, const QVariant& value, QWidget* parent) | ||
101 | { | 103 | { | ||
102 | QWidget* widget = 0; | 104 | QWidget* widget = 0; | ||
105 | auto persons = QStringList{ | ||||
106 | QStringLiteral("author"), | ||||
107 | QStringLiteral("composer"), | ||||
108 | QStringLiteral("lyricist"), | ||||
109 | QStringLiteral("artist"), | ||||
110 | QStringLiteral("albumArtist") | ||||
111 | }; | ||||
112 | auto tokens = QStringList{ | ||||
113 | QStringLiteral("genre"), | ||||
114 | QStringLiteral("keywords"), | ||||
115 | QStringLiteral("subject") | ||||
116 | }; | ||||
103 | 117 | | |||
104 | if (prop == QLatin1String("rating")) { | 118 | if (prop == QLatin1String("rating")) { | ||
105 | widget = createRatingWidget( value.toInt(), parent ); | 119 | widget = createRatingWidget( value.toInt(), parent ); | ||
106 | } | 120 | } | ||
107 | else if (prop == QLatin1String("userComment")) { | 121 | else if (prop == QLatin1String("userComment")) { | ||
108 | widget = createCommentWidget( value.toString(), parent ); | 122 | widget = createCommentWidget( value.toString(), parent ); | ||
109 | } | 123 | } | ||
110 | else if (prop == QLatin1String("tags")) { | 124 | else if (prop == QLatin1String("tags")) { | ||
111 | QStringList tags = value.toStringList(); | 125 | QStringList tags = value.toStringList(); | ||
112 | QCollator coll; | 126 | QCollator coll; | ||
113 | coll.setNumericMode(true); | 127 | coll.setNumericMode(true); | ||
114 | std::sort(tags.begin(), tags.end(), [&](const QString& s1, const QString& s2){ return coll.compare(s1, s2) < 0; }); | 128 | std::sort(tags.begin(), tags.end(), [&](const QString& s1, const QString& s2){ return coll.compare(s1, s2) < 0; }); | ||
115 | widget = createTagWidget( tags, parent ); | 129 | widget = createTagWidget(prop, tags, m_readOnly, parent ); | ||
130 | | ||||
131 | } else if (tokens.contains(prop)) { | ||||
132 | auto values = value.type() == QVariant::Type::StringList | ||||
133 | ? value.toStringList() | ||||
134 | : value.toString().split(QRegularExpression{",\\s*|;\\s*"}, QString::SplitBehavior::SkipEmptyParts); | ||||
135 | QCollator coll; | ||||
136 | coll.setNumericMode(true); | ||||
137 | std::sort(values.begin(), values.end(), [&](const QString& s1, const QString& s2){ | ||||
138 | return coll.compare(s1, s2) < 0; | ||||
139 | }); | ||||
140 | widget = createTagWidget(prop, values, true, parent); | ||||
141 | | ||||
142 | } else if (persons.contains(prop)) { | ||||
143 | auto values = value.type() == QVariant::Type::StringList | ||||
144 | ? value.toStringList() | ||||
145 | : value.toString().split(QRegularExpression{"\\s*&\\s*"}); | ||||
146 | widget = createTagWidget(prop, values, true, parent); | ||||
147 | } else if (prop == QLatin1String("releaseYear")) { | ||||
148 | // FIXME: This is misuse | ||||
149 | widget = createTagWidget(prop, {value.toString()}, true, parent); | ||||
150 | | ||||
116 | } | 151 | } | ||
117 | else { | 152 | else { | ||
118 | KFormat form; | 153 | KFormat form; | ||
119 | // vHanda: FIXME: Add links! Take m_noLinks into consideration | 154 | // vHanda: FIXME: Add links! Take m_noLinks into consideration | ||
120 | QString valueString; | 155 | QString valueString; | ||
121 | 156 | | |||
122 | if (prop == QLatin1String("duration")) { | 157 | if (prop == QLatin1String("duration")) { | ||
123 | valueString = form.formatDuration(value.toInt() * 1000); | 158 | valueString = form.formatDuration(value.toInt() * 1000); | ||
Show All 29 Lines | |||||
153 | } | 188 | } | ||
154 | 189 | | |||
155 | widget->setForegroundRole(parent->foregroundRole()); | 190 | widget->setForegroundRole(parent->foregroundRole()); | ||
156 | widget->setFont(parent->font()); | 191 | widget->setFont(parent->font()); | ||
157 | widget->setObjectName(prop); | 192 | widget->setObjectName(prop); | ||
158 | return widget; | 193 | return widget; | ||
159 | } | 194 | } | ||
160 | 195 | | |||
161 | QWidget* WidgetFactory::createTagWidget(const QStringList& tags, QWidget* parent) | 196 | QWidget* WidgetFactory::createTagWidget(const QString& name, const QStringList& tags, bool readOnly, QWidget* parent) | ||
162 | { | 197 | { | ||
163 | TagWidget* tagWidget = new TagWidget(parent); | 198 | TagWidget* tagWidget = new TagWidget(parent); | ||
164 | tagWidget->setReadyOnly(m_readOnly); | 199 | tagWidget->setReadyOnly(readOnly); | ||
200 | tagWidget->setPropertyName(name); | ||||
201 | tagWidget->setDisplayAsLinks(!m_noLinks || name == QLatin1String("tags")); | ||||
165 | tagWidget->setSelectedTags(tags); | 202 | tagWidget->setSelectedTags(tags); | ||
166 | 203 | | |||
167 | connect(tagWidget, &TagWidget::selectionChanged, this, &WidgetFactory::slotTagsChanged); | 204 | connect(tagWidget, &TagWidget::selectionChanged, this, &WidgetFactory::slotTagsChanged); | ||
168 | connect(tagWidget, &TagWidget::tagClicked, this, &WidgetFactory::slotTagClicked); | 205 | connect(tagWidget, &TagWidget::tagClicked, this, &WidgetFactory::slotTagClicked); | ||
169 | 206 | | |||
170 | m_tagWidget = tagWidget; | 207 | m_tagWidget = tagWidget; | ||
171 | m_prevTags = tags; | 208 | m_prevTags = tags; | ||
172 | 209 | | |||
▲ Show 20 Lines • Show All 123 Lines • ▼ Show 20 Line(s) | |||||
296 | // Notifications | 333 | // Notifications | ||
297 | // | 334 | // | ||
298 | 335 | | |||
299 | void WidgetFactory::slotLinkActivated(const QString& url) | 336 | void WidgetFactory::slotLinkActivated(const QString& url) | ||
300 | { | 337 | { | ||
301 | emit urlActivated(QUrl::fromUserInput(url)); | 338 | emit urlActivated(QUrl::fromUserInput(url)); | ||
302 | } | 339 | } | ||
303 | 340 | | |||
304 | void WidgetFactory::slotTagClicked(const QString& tag) | 341 | void WidgetFactory::slotTagClicked(const QUrl& url) | ||
305 | { | 342 | { | ||
306 | QUrl url; | | |||
307 | url.setScheme("tags"); | | |||
308 | url.setPath(tag); | | |||
309 | | ||||
310 | emit urlActivated(url); | 343 | emit urlActivated(url); | ||
311 | } | 344 | } | ||
312 | 345 | | |||
313 | 346 | | |||
314 | // | 347 | // | ||
315 | // Accessor Methods | 348 | // Accessor Methods | ||
316 | // | 349 | // | ||
317 | void WidgetFactory::setReadOnly(bool value) | 350 | void WidgetFactory::setReadOnly(bool value) | ||
Show All 14 Lines |