Add a tag widget, and get song tags from last.fm

This commit is contained in:
David Sansome 2010-10-10 18:57:23 +00:00
parent a1cc41ab27
commit aca4ba6073
45 changed files with 652 additions and 2 deletions

35
debian/copyright vendored
View File

@ -39,6 +39,10 @@ Files: src/widgets/fancytabwidget.*
Copyright: 2010, Nokia Corporation
License: Qt Commercial or LGPL-2.1
Files: src/ui/flowlayout.*
Copyright: 2010, Nokia Corporation
License: BSD-Nokia
Files: src/analyzers/analyzerbase.*
src/analyzers/blockanalyzer.*
src/analyzers/baranalyzer.*
@ -55,7 +59,7 @@ License: GPL-2+
Files: 3rdparty/gmock/*
Copyright: 2008, Google Inc.
License: BSD
License: BSD-Google
Files: 3rdparty/qsqlite/*
Copyright: 2009, Nokia Corporation
@ -127,7 +131,7 @@ License: GPL-3
Version 3 can be found in the file
`/usr/share/common-licenses/GPL-3`.
License: BSD
License: BSD-Google
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
@ -154,6 +158,33 @@ License: BSD
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
License: BSD-Nokia
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
License: Qt Commercial
Licensees holding valid Qt Commercial licenses may use this file in
accordance with the Qt Commercial License Agreement provided with the

View File

@ -139,6 +139,7 @@ set(SOURCES
songinfo/songinfoprovider.cpp
songinfo/songinfoview.cpp
songinfo/songplaystats.cpp
songinfo/tagwidget.cpp
songinfo/ultimatelyricsprovider.cpp
songinfo/ultimatelyricsreader.cpp
@ -149,6 +150,7 @@ set(SOURCES
ui/albumcoversearcher.cpp
ui/edittagdialog.cpp
ui/equalizer.cpp
ui/flowlayout.cpp
ui/globalshortcutgrabber.cpp
ui/globalshortcutsconfig.cpp
ui/iconloader.cpp
@ -281,6 +283,7 @@ set(HEADERS
songinfo/songinfoprovider.h
songinfo/songinfoview.h
songinfo/songplaystats.h
songinfo/tagwidget.h
songinfo/ultimatelyricsprovider.h
songinfo/ultimatelyricsreader.h

View File

@ -29,6 +29,9 @@ ArtistInfoView::~ArtistInfoView() {
}
bool ArtistInfoView::NeedsUpdate(const Song& old_metadata, const Song& new_metadata) const {
if (new_metadata.artist().isEmpty())
return false;
return old_metadata.artist() != new_metadata.artist();
}

View File

@ -16,6 +16,7 @@
#include "lastfmtrackinfoprovider.h"
#include "songplaystats.h"
#include "tagwidget.h"
#include "ui/iconloader.h"
#include "widgets/autosizedtextedit.h"
@ -57,6 +58,7 @@ void LastfmTrackInfoProvider::RequestFinished() {
GetPlayCounts(id, query);
GetWiki(id, query);
GetTags(id, query);
} catch (std::runtime_error&) {
}
@ -102,6 +104,9 @@ void LastfmTrackInfoProvider::GetPlayCounts(int id, const lastfm::XmlQuery& q) {
void LastfmTrackInfoProvider::GetWiki(int id, const lastfm::XmlQuery& q) {
// Parse the response
if (q["track"].children("wiki").isEmpty())
return; // No wiki element
const QString content = q["track"]["wiki"]["content"].text();
if (content.isEmpty())
@ -119,3 +124,26 @@ void LastfmTrackInfoProvider::GetWiki(int id, const lastfm::XmlQuery& q) {
emit InfoReady(id, data);
}
void LastfmTrackInfoProvider::GetTags(int id, const lastfm::XmlQuery& q) {
// Parse the response
if (q["track"].children("toptags").isEmpty())
return; // No tag elements
CollapsibleInfoPane::Data data;
data.title_ = tr("Last.fm tags");
data.type_ = CollapsibleInfoPane::Data::Type_Biography;
data.icon_ = QIcon(":/last.fm/icon_tag.png");
TagWidget* widget = new TagWidget;
data.contents_ = widget;
widget->SetIcon(data.icon_);
widget->SetUrlPattern("lastfm://globaltags/%1");
foreach (const lastfm::XmlQuery& e, q["track"]["toptags"].children("tag")) {
widget->AddTag(e["name"].text());
}
emit InfoReady(id, data);
}

View File

@ -37,6 +37,7 @@ private slots:
private:
void GetPlayCounts(int id, const lastfm::XmlQuery& q);
void GetWiki(int id, const lastfm::XmlQuery& q);
void GetTags(int id, const lastfm::XmlQuery& q);
private:
QMap<QNetworkReply*, int> requests_;

View File

@ -61,6 +61,14 @@ void SongInfoView::UltimateLyricsParsed() {
ReloadSettings();
}
bool SongInfoView::NeedsUpdate(const Song& old_metadata, const Song& new_metadata) const {
if (new_metadata.title().isEmpty() || new_metadata.artist().isEmpty())
return false;
return old_metadata.title() != new_metadata.title() ||
old_metadata.artist() != new_metadata.artist();
}
void SongInfoView::ResultReady(int id, const SongInfoFetcher::Result& result) {
if (id != current_request_id_)
return;

View File

@ -38,6 +38,9 @@ public:
public slots:
void ReloadSettings();
protected:
bool NeedsUpdate(const Song& old_metadata, const Song& new_metadata) const;
protected slots:
void ResultReady(int id, const SongInfoFetcher::Result& result);

107
src/songinfo/tagwidget.cpp Normal file
View File

@ -0,0 +1,107 @@
/* This file is part of Clementine.
Clementine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clementine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tagwidget.h"
#include "ui/flowlayout.h"
#include <QPainter>
#include <QPropertyAnimation>
const int TagWidgetTag::kIconSize = 16;
const int TagWidgetTag::kIconTextSpacing = 6;
const int TagWidgetTag::kPadding = 2;
TagWidgetTag::TagWidgetTag(const QIcon& icon, const QString& text, QWidget* parent)
: QWidget(parent),
text_(text),
icon_(icon),
opacity_(0.0),
animation_(new QPropertyAnimation(this, "background_opacity", this))
{
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}
QSize TagWidgetTag::sizeHint() const {
QSize text = fontMetrics().size(0, text_);
QSize icon(kIconSize + kIconTextSpacing, kIconSize);
return QSize(kPadding + icon.width() + text.width() + kPadding,
kPadding + qMax(text.height(), icon.height()) + kPadding);
}
void TagWidgetTag::set_background_opacity(float opacity) {
opacity_ = opacity;
update();
}
void TagWidgetTag::enterEvent(QEvent*) {
animation_->stop();
animation_->setEndValue(1.0);
animation_->setDuration(80);
animation_->start();
}
void TagWidgetTag::leaveEvent(QEvent*) {
animation_->stop();
animation_->setEndValue(0.0);
animation_->setDuration(160);
animation_->start();
}
void TagWidgetTag::paintEvent(QPaintEvent*) {
QPainter p(this);
const QRect tag_rect(rect());
const QRect icon_rect(tag_rect.topLeft() + QPoint(kPadding, kPadding),
QSize(kIconSize, kIconSize));
const QRect text_rect(icon_rect.topRight() + QPoint(kIconTextSpacing, 0),
QSize(tag_rect.width() - icon_rect.right() - kIconTextSpacing - kPadding,
icon_rect.height()));
// Use the tag's opacity
p.setOpacity(0.3 + opacity_ * 0.7);
// Background
QColor background_color = palette().color(QPalette::Highlight);
background_color.setAlpha(128);
p.setRenderHint(QPainter::Antialiasing);
p.setPen(QPen(palette().dark(), 1.0));
p.setBrush(background_color);
p.drawRoundedRect(tag_rect, 5, 5);
// Icon
p.drawPixmap(icon_rect, icon_.pixmap(kIconSize));
// Text
p.setOpacity(1.0);
p.setPen(palette().color(QPalette::Text));
p.drawText(text_rect, text_);
}
TagWidget::TagWidget(QWidget* parent)
: QWidget(parent)
{
setLayout(new FlowLayout);
}
void TagWidget::AddTag(const QString& tag) {
if (tag.isEmpty())
return;
layout()->addWidget(new TagWidgetTag(icon_, tag, this));
}

72
src/songinfo/tagwidget.h Normal file
View File

@ -0,0 +1,72 @@
/* This file is part of Clementine.
Clementine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clementine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TAGWIDGET_H
#define TAGWIDGET_H
#include <QIcon>
#include <QWidget>
class QPropertyAnimation;
class TagWidgetTag : public QWidget {
Q_OBJECT
Q_PROPERTY(float background_opacity
READ background_opacity
WRITE set_background_opacity);
public:
TagWidgetTag(const QIcon& icon, const QString& text, QWidget* parent);
static const int kIconSize;
static const int kIconTextSpacing;
static const int kPadding;
float background_opacity() const { return opacity_; }
void set_background_opacity(float opacity);
QSize sizeHint() const;
protected:
void enterEvent(QEvent*);
void leaveEvent(QEvent*);
void paintEvent(QPaintEvent*);
private:
QString text_;
QIcon icon_;
float opacity_;
QPropertyAnimation* animation_;
};
class TagWidget : public QWidget {
Q_OBJECT
public:
TagWidget(QWidget* parent = 0);
void SetUrlPattern(const QString& pattern) { url_pattern_ = pattern; }
void SetIcon(const QIcon& icon) { icon_ = icon; }
void AddTag(const QString& tag);
private:
QString url_pattern_;
QIcon icon_;
QStringList tags_;
};
#endif // TAGWIDGET_H

View File

@ -1056,6 +1056,9 @@ msgstr ""
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr ""

View File

@ -1057,6 +1057,9 @@ msgstr ""
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr ""

View File

@ -1082,6 +1082,9 @@ msgstr "Contrasenya de Last.fm"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Usuari de Last.fm"

View File

@ -1061,6 +1061,9 @@ msgstr "Heslo k Last.fm"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Uživatelské jméno k Last.fm"

View File

@ -1062,6 +1062,9 @@ msgstr "Last.fm-adgangskode"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Last.fm-brugernavn"

View File

@ -1083,6 +1083,9 @@ msgstr "Last.fm Passwort"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Last.fm Benutzername"

View File

@ -1088,6 +1088,9 @@ msgstr "Last.fm συνθηματικό"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Last.fm όνομα χρήστη"

View File

@ -1060,6 +1060,9 @@ msgstr "Last.fm password"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Last.fm username"

View File

@ -1058,6 +1058,9 @@ msgstr "Last.fm password"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Last.fm username"

View File

@ -1087,6 +1087,9 @@ msgstr "Contraseña"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Usuario"

View File

@ -1058,6 +1058,9 @@ msgstr "Last.fm-salasana"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Last.fm-tunnus"

View File

@ -1091,6 +1091,9 @@ msgstr "Mot de passe"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Nom d'utilisateur"

View File

@ -1063,6 +1063,9 @@ msgstr ""
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr ""

View File

@ -1081,6 +1081,9 @@ msgstr "Last.fm jelszó"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Last.fm felhasználói név"

View File

@ -1090,6 +1090,9 @@ msgstr "Password Last.fm"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Nome utente Last.fm"

View File

@ -1058,6 +1058,9 @@ msgstr ""
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr ""

View File

@ -1057,6 +1057,9 @@ msgstr ""
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr ""

View File

@ -1060,6 +1060,9 @@ msgstr "Last.fm passord"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Last.fm brukernavn"

View File

@ -1085,6 +1085,9 @@ msgstr "Last.fm wachtwoord"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Last.fm gebruikersnaam"

View File

@ -1056,6 +1056,9 @@ msgstr ""
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr ""

View File

@ -1082,6 +1082,9 @@ msgstr "Hasło Last.fm"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Użytkownik Last.fm"

View File

@ -1082,6 +1082,9 @@ msgstr "Senha Last.fm"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Nome de utilizador Last.fm"

View File

@ -1073,6 +1073,9 @@ msgstr "Senha do Last.fm"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Nome de usuário Last.fm"

View File

@ -1057,6 +1057,9 @@ msgstr ""
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr ""

View File

@ -1076,6 +1076,9 @@ msgstr "Пароль Last.fm"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Логин Last.fm"

View File

@ -1079,6 +1079,9 @@ msgstr "Last.fm heslo"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Last.fm použ. meno"

View File

@ -1078,6 +1078,9 @@ msgstr "Geslo Last.fm"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Uporabniško ime Last.fm"

View File

@ -1062,6 +1062,9 @@ msgstr "ЛастФМ лозинка"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "ЛастФМ корисничко име"

View File

@ -1066,6 +1066,9 @@ msgstr "Last.fm lösenord"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Last.fm användarnamn"

View File

@ -1079,6 +1079,9 @@ msgstr "Last.fm parolası"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Last.fm kullanıcı adı"

View File

@ -1047,6 +1047,9 @@ msgstr ""
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr ""

View File

@ -1078,6 +1078,9 @@ msgstr "Пароль Last.fm"
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr "Користувач Last.fm"

View File

@ -1056,6 +1056,9 @@ msgstr ""
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr ""

View File

@ -1062,6 +1062,9 @@ msgstr ""
msgid "Last.fm play counts"
msgstr ""
msgid "Last.fm tags"
msgstr ""
msgid "Last.fm username"
msgstr ""

213
src/ui/flowlayout.cpp Normal file
View File

@ -0,0 +1,213 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtGui>
#include "flowlayout.h"
//! [1]
FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
: QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
{
setContentsMargins(margin, margin, margin, margin);
}
FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing)
: m_hSpace(hSpacing), m_vSpace(vSpacing)
{
setContentsMargins(margin, margin, margin, margin);
}
//! [1]
//! [2]
FlowLayout::~FlowLayout()
{
QLayoutItem *item;
while ((item = takeAt(0)))
delete item;
}
//! [2]
//! [3]
void FlowLayout::addItem(QLayoutItem *item)
{
itemList.append(item);
}
//! [3]
//! [4]
int FlowLayout::horizontalSpacing() const
{
if (m_hSpace >= 0) {
return m_hSpace;
} else {
return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
}
}
int FlowLayout::verticalSpacing() const
{
if (m_vSpace >= 0) {
return m_vSpace;
} else {
return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
}
}
//! [4]
//! [5]
int FlowLayout::count() const
{
return itemList.size();
}
QLayoutItem *FlowLayout::itemAt(int index) const
{
return itemList.value(index);
}
QLayoutItem *FlowLayout::takeAt(int index)
{
if (index >= 0 && index < itemList.size())
return itemList.takeAt(index);
else
return 0;
}
//! [5]
//! [6]
Qt::Orientations FlowLayout::expandingDirections() const
{
return 0;
}
//! [6]
//! [7]
bool FlowLayout::hasHeightForWidth() const
{
return true;
}
int FlowLayout::heightForWidth(int width) const
{
int height = doLayout(QRect(0, 0, width, 0), true);
return height;
}
//! [7]
//! [8]
void FlowLayout::setGeometry(const QRect &rect)
{
QLayout::setGeometry(rect);
doLayout(rect, false);
}
QSize FlowLayout::sizeHint() const
{
return minimumSize();
}
QSize FlowLayout::minimumSize() const
{
QSize size;
QLayoutItem *item;
foreach (item, itemList)
size = size.expandedTo(item->minimumSize());
size += QSize(2*margin(), 2*margin());
return size;
}
//! [8]
//! [9]
int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
{
int left, top, right, bottom;
getContentsMargins(&left, &top, &right, &bottom);
QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
int x = effectiveRect.x();
int y = effectiveRect.y();
int lineHeight = 0;
//! [9]
//! [10]
QLayoutItem *item;
foreach (item, itemList) {
QWidget *wid = item->widget();
int spaceX = horizontalSpacing();
if (spaceX == -1)
spaceX = wid->style()->layoutSpacing(
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
int spaceY = verticalSpacing();
if (spaceY == -1)
spaceY = wid->style()->layoutSpacing(
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
//! [10]
//! [11]
int nextX = x + item->sizeHint().width() + spaceX;
if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
x = effectiveRect.x();
y = y + lineHeight + spaceY;
nextX = x + item->sizeHint().width() + spaceX;
lineHeight = 0;
}
if (!testOnly)
item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
x = nextX;
lineHeight = qMax(lineHeight, item->sizeHint().height());
}
return y + lineHeight - rect.y() + bottom;
}
//! [11]
//! [12]
int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
{
QObject *parent = this->parent();
if (!parent) {
return -1;
} else if (parent->isWidgetType()) {
QWidget *pw = static_cast<QWidget *>(parent);
return pw->style()->pixelMetric(pm, 0, pw);
} else {
return static_cast<QLayout *>(parent)->spacing();
}
}
//! [12]

79
src/ui/flowlayout.h Normal file
View File

@ -0,0 +1,79 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef FLOWLAYOUT_H
#define FLOWLAYOUT_H
#include <QLayout>
#include <QRect>
#include <QStyle>
#include <QWidgetItem>
//! [0]
class FlowLayout : public QLayout
{
public:
FlowLayout(QWidget *parent, int margin = -1, int hSpacing = -1, int vSpacing = -1);
FlowLayout(int margin = -1, int hSpacing = -1, int vSpacing = -1);
~FlowLayout();
void addItem(QLayoutItem *item);
int horizontalSpacing() const;
int verticalSpacing() const;
Qt::Orientations expandingDirections() const;
bool hasHeightForWidth() const;
int heightForWidth(int) const;
int count() const;
QLayoutItem *itemAt(int index) const;
QSize minimumSize() const;
void setGeometry(const QRect &rect);
QSize sizeHint() const;
QLayoutItem *takeAt(int index);
private:
int doLayout(const QRect &rect, bool testOnly) const;
int smartSpacing(QStyle::PixelMetric pm) const;
QList<QLayoutItem *> itemList;
int m_hSpace;
int m_vSpace;
};
//! [0]
#endif