From d9de39172033c28b8f9a7c1573130cf2124b4f7a Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Tue, 24 Apr 2012 23:16:35 +0200 Subject: Provide additional default groups for the Places Panel If Nepomuk is enabled, it is now possible to easily search for some most common queries by having additional groups. --- src/kitemviews/kfileitemlistgroupheader.cpp | 107 -------------------- src/kitemviews/kfileitemlistgroupheader.h | 55 ----------- src/kitemviews/kfileitemlistview.cpp | 6 -- src/kitemviews/kfileitemlistview.h | 1 - src/kitemviews/kitemlistgroupheader.cpp | 31 ++---- src/kitemviews/kitemlistgroupheader.h | 9 +- src/kitemviews/kitemlistview.cpp | 2 +- src/kitemviews/kitemlistview.h | 2 +- src/kitemviews/kstandarditemlistgroupheader.cpp | 125 ++++++++++++++++++++++++ src/kitemviews/kstandarditemlistgroupheader.h | 57 +++++++++++ src/kitemviews/kstandarditemlistview.cpp | 3 +- src/kitemviews/kstandarditemmodel.cpp | 17 +++- 12 files changed, 211 insertions(+), 204 deletions(-) delete mode 100644 src/kitemviews/kfileitemlistgroupheader.cpp delete mode 100644 src/kitemviews/kfileitemlistgroupheader.h create mode 100644 src/kitemviews/kstandarditemlistgroupheader.cpp create mode 100644 src/kitemviews/kstandarditemlistgroupheader.h (limited to 'src/kitemviews') diff --git a/src/kitemviews/kfileitemlistgroupheader.cpp b/src/kitemviews/kfileitemlistgroupheader.cpp deleted file mode 100644 index 0c940ed28..000000000 --- a/src/kitemviews/kfileitemlistgroupheader.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2011 by Peter Penz * - * * - * Based on the Itemviews NG project from Trolltech Labs: * - * http://qt.gitorious.org/qt-labs/itemviews-ng * - * * - * This program 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 2 of the License, or * - * (at your option) any later version. * - * * - * This program 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 this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - ***************************************************************************/ - -#include "kfileitemlistgroupheader.h" - -#include -#include - -KFileItemListGroupHeader::KFileItemListGroupHeader(QGraphicsWidget* parent) : - KItemListGroupHeader(parent), - m_dirtyCache(true), - m_text(), - m_pixmap() -{ - m_text.setTextFormat(Qt::PlainText); - m_text.setPerformanceHint(QStaticText::AggressiveCaching); -} - -KFileItemListGroupHeader::~KFileItemListGroupHeader() -{ -} - -void KFileItemListGroupHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - KItemListGroupHeader::paint(painter, option, widget); - - if (m_dirtyCache) { - updateCache(); - } - - if (m_pixmap.isNull()) { - painter->setPen(roleColor()); - painter->drawStaticText(roleBounds().topLeft(), m_text); - } else { - painter->drawPixmap(roleBounds().topLeft(), m_pixmap); - } -} - -void KFileItemListGroupHeader::roleChanged(const QByteArray ¤t, const QByteArray &previous) -{ - Q_UNUSED(current); - Q_UNUSED(previous); - m_dirtyCache = true; -} - -void KFileItemListGroupHeader::dataChanged(const QVariant& current, const QVariant& previous) -{ - Q_UNUSED(current); - Q_UNUSED(previous); - m_dirtyCache = true; -} - -void KFileItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent* event) -{ - QGraphicsWidget::resizeEvent(event); - m_dirtyCache = true; -} - -void KFileItemListGroupHeader::updateCache() -{ - Q_ASSERT(m_dirtyCache); - m_dirtyCache = false; - - const qreal maxWidth = size().width() - 4 * styleOption().padding; - - if (role() == "rating") { - m_text = QString(); - - const qreal height = styleOption().fontMetrics.ascent(); - const QSizeF pixmapSize(qMin(height * 5, maxWidth), height); - - m_pixmap = QPixmap(pixmapSize.toSize()); - m_pixmap.fill(Qt::transparent); - - QPainter painter(&m_pixmap); - const QRect rect(0, 0, m_pixmap.width(), m_pixmap.height()); - const int rating = data().toInt(); - KRatingPainter::paintRating(&painter, rect, Qt::AlignJustify | Qt::AlignVCenter, rating); - } else { - m_pixmap = QPixmap(); - - QFontMetricsF fontMetrics(font()); - const QString text = fontMetrics.elidedText(data().toString(), Qt::ElideRight, maxWidth); - m_text.setText(text); - } -} - -#include "kfileitemlistgroupheader.moc" diff --git a/src/kitemviews/kfileitemlistgroupheader.h b/src/kitemviews/kfileitemlistgroupheader.h deleted file mode 100644 index 41a1123ff..000000000 --- a/src/kitemviews/kfileitemlistgroupheader.h +++ /dev/null @@ -1,55 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2011 by Peter Penz * - * * - * This program 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 2 of the License, or * - * (at your option) any later version. * - * * - * This program 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 this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - ***************************************************************************/ - -#ifndef KFILEITEMLISTGROUPHEADER_H -#define KFILEITEMLISTGROUPHEADER_H - -#include - -#include - -#include -#include - -class LIBDOLPHINPRIVATE_EXPORT KFileItemListGroupHeader : public KItemListGroupHeader -{ - Q_OBJECT - -public: - KFileItemListGroupHeader(QGraphicsWidget* parent = 0); - virtual ~KFileItemListGroupHeader(); - - virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0); - -protected: - virtual void roleChanged(const QByteArray ¤t, const QByteArray &previous); - virtual void dataChanged(const QVariant& current, const QVariant& previous); - virtual void resizeEvent(QGraphicsSceneResizeEvent* event); - -private: - void updateCache(); - -private: - bool m_dirtyCache; - QStaticText m_text; - QPixmap m_pixmap; -}; -#endif - - diff --git a/src/kitemviews/kfileitemlistview.cpp b/src/kitemviews/kfileitemlistview.cpp index 7bcc781fa..14547fc7b 100644 --- a/src/kitemviews/kfileitemlistview.cpp +++ b/src/kitemviews/kfileitemlistview.cpp @@ -19,7 +19,6 @@ #include "kfileitemlistview.h" -#include "kfileitemlistgroupheader.h" #include "kfileitemmodelrolesupdater.h" #include "kfileitemlistwidget.h" #include "kfileitemmodel.h" @@ -190,11 +189,6 @@ KItemListWidgetCreatorBase* KFileItemListView::defaultWidgetCreator() const return new KItemListWidgetCreator(); } -KItemListGroupHeaderCreatorBase* KFileItemListView::defaultGroupHeaderCreator() const -{ - return new KItemListGroupHeaderCreator(); -} - void KFileItemListView::onPreviewsShownChanged(bool shown) { Q_UNUSED(shown); diff --git a/src/kitemviews/kfileitemlistview.h b/src/kitemviews/kfileitemlistview.h index c8a3385fd..d795c96b5 100644 --- a/src/kitemviews/kfileitemlistview.h +++ b/src/kitemviews/kfileitemlistview.h @@ -77,7 +77,6 @@ public: protected: virtual KItemListWidgetCreatorBase* defaultWidgetCreator() const; - virtual KItemListGroupHeaderCreatorBase* defaultGroupHeaderCreator() const; virtual void onPreviewsShownChanged(bool shown); virtual void onItemLayoutChanged(ItemLayout current, ItemLayout previous); virtual void onModelChanged(KItemModelBase* current, KItemModelBase* previous); diff --git a/src/kitemviews/kitemlistgroupheader.cpp b/src/kitemviews/kitemlistgroupheader.cpp index bc68cd562..576d20b88 100644 --- a/src/kitemviews/kitemlistgroupheader.cpp +++ b/src/kitemviews/kitemlistgroupheader.cpp @@ -20,7 +20,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ -#include "kitemlistgroupheader.h" +#include "kstandarditemlistgroupheader.h" #include "kitemlistview.h" @@ -37,7 +37,7 @@ KItemListGroupHeader::KItemListGroupHeader(QGraphicsWidget* parent) : m_styleOption(), m_scrollOrientation(Qt::Vertical), m_itemIndex(-1), - m_lineColor(), + m_separatorColor(), m_roleColor(), m_roleBounds() { @@ -124,6 +124,7 @@ Qt::Orientation KItemListGroupHeader::scrollOrientation() const void KItemListGroupHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { + Q_UNUSED(painter); Q_UNUSED(option); Q_UNUSED(widget); @@ -131,28 +132,8 @@ void KItemListGroupHeader::paint(QPainter* painter, const QStyleOptionGraphicsIt updateCache(); } - if (m_itemIndex == 0) { - // No top- or left-line should be drawn for the first group-header - return; - } - - painter->setPen(m_lineColor); - - if (m_scrollOrientation == Qt::Horizontal) { - painter->drawLine(0, 0, 0, size().height() - 1); - } else { - painter->drawLine(0, 0, size().width() - 1, 0); - } -} - -QRectF KItemListGroupHeader::roleBounds() const -{ - return m_roleBounds; -} - -QColor KItemListGroupHeader::roleColor() const -{ - return m_roleColor; + paintSeparator(painter, m_separatorColor); + paintRole(painter, m_roleBounds, m_roleColor); } void KItemListGroupHeader::roleChanged(const QByteArray& current, const QByteArray& previous) @@ -201,7 +182,7 @@ void KItemListGroupHeader::updateCache() // performance reasons. const QColor c1 = m_styleOption.palette.text().color(); const QColor c2 = m_styleOption.palette.base().color(); - m_lineColor = mixedColor(c1, c2, 10); + m_separatorColor = mixedColor(c1, c2, 10); m_roleColor = mixedColor(c1, c2, 70); const int padding = qMax(1, m_styleOption.padding); diff --git a/src/kitemviews/kitemlistgroupheader.h b/src/kitemviews/kitemlistgroupheader.h index c996a4870..e19ab4871 100644 --- a/src/kitemviews/kitemlistgroupheader.h +++ b/src/kitemviews/kitemlistgroupheader.h @@ -68,11 +68,8 @@ public: virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0); protected: - /** @return Bounding rectangle where the role should be drawn into. */ - QRectF roleBounds() const; - - /** @return Primary color that should be used for drawing the role. */ - QColor roleColor() const; + virtual void paintRole(QPainter* painter, const QRectF& roleBounds, const QColor& color) = 0; + virtual void paintSeparator(QPainter* painter, const QColor& color) = 0; /** * Is called after the role has been changed and allows the derived class @@ -120,7 +117,7 @@ private: Qt::Orientation m_scrollOrientation; int m_itemIndex; - QColor m_lineColor; + QColor m_separatorColor; QColor m_roleColor; QRectF m_roleBounds; }; diff --git a/src/kitemviews/kitemlistview.cpp b/src/kitemviews/kitemlistview.cpp index 6d2d98e93..8ecd1e212 100644 --- a/src/kitemviews/kitemlistview.cpp +++ b/src/kitemviews/kitemlistview.cpp @@ -1850,7 +1850,7 @@ void KItemListView::updateGroupHeaderForWidget(KItemListWidget* widget) } const QList > groups = model()->groups(); - if (groups.isEmpty()) { + if (groups.isEmpty() || !groupHeaderCreator()) { return; } diff --git a/src/kitemviews/kitemlistview.h b/src/kitemviews/kitemlistview.h index 3c47e95c6..dd67b941b 100644 --- a/src/kitemviews/kitemlistview.h +++ b/src/kitemviews/kitemlistview.h @@ -25,7 +25,7 @@ #include -#include +#include #include #include #include diff --git a/src/kitemviews/kstandarditemlistgroupheader.cpp b/src/kitemviews/kstandarditemlistgroupheader.cpp new file mode 100644 index 000000000..3a5ddd944 --- /dev/null +++ b/src/kitemviews/kstandarditemlistgroupheader.cpp @@ -0,0 +1,125 @@ +/*************************************************************************** + * Copyright (C) 2011 by Peter Penz * + * * + * Based on the Itemviews NG project from Trolltech Labs: * + * http://qt.gitorious.org/qt-labs/itemviews-ng * + * * + * This program 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 2 of the License, or * + * (at your option) any later version. * + * * + * This program 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 this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#include "kstandarditemlistgroupheader.h" + +#include +#include + +KStandardItemListGroupHeader::KStandardItemListGroupHeader(QGraphicsWidget* parent) : + KItemListGroupHeader(parent), + m_dirtyCache(true), + m_text(), + m_pixmap() +{ + m_text.setTextFormat(Qt::PlainText); + m_text.setPerformanceHint(QStaticText::AggressiveCaching); +} + +KStandardItemListGroupHeader::~KStandardItemListGroupHeader() +{ +} + +void KStandardItemListGroupHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) +{ + if (m_dirtyCache) { + updateCache(); + } + KItemListGroupHeader::paint(painter, option, widget); +} + +void KStandardItemListGroupHeader::paintRole(QPainter* painter, const QRectF& roleBounds, const QColor& color) +{ + if (m_pixmap.isNull()) { + painter->setPen(color); + painter->drawStaticText(roleBounds.topLeft(), m_text); + } else { + painter->drawPixmap(roleBounds.topLeft(), m_pixmap); + } +} + +void KStandardItemListGroupHeader::paintSeparator(QPainter* painter, const QColor& color) +{ + if (itemIndex() == 0) { + // No top- or left-line should be drawn for the first group-header + return; + } + + painter->setPen(color); + + if (scrollOrientation() == Qt::Horizontal) { + painter->drawLine(0, 0, 0, size().height() - 1); + } else { + painter->drawLine(0, 0, size().width() - 1, 0); + } +} + +void KStandardItemListGroupHeader::roleChanged(const QByteArray ¤t, const QByteArray &previous) +{ + Q_UNUSED(current); + Q_UNUSED(previous); + m_dirtyCache = true; +} + +void KStandardItemListGroupHeader::dataChanged(const QVariant& current, const QVariant& previous) +{ + Q_UNUSED(current); + Q_UNUSED(previous); + m_dirtyCache = true; +} + +void KStandardItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent* event) +{ + QGraphicsWidget::resizeEvent(event); + m_dirtyCache = true; +} + +void KStandardItemListGroupHeader::updateCache() +{ + Q_ASSERT(m_dirtyCache); + m_dirtyCache = false; + + const qreal maxWidth = size().width() - 4 * styleOption().padding; + + if (role() == "rating") { + m_text = QString(); + + const qreal height = styleOption().fontMetrics.ascent(); + const QSizeF pixmapSize(qMin(height * 5, maxWidth), height); + + m_pixmap = QPixmap(pixmapSize.toSize()); + m_pixmap.fill(Qt::transparent); + + QPainter painter(&m_pixmap); + const QRect rect(0, 0, m_pixmap.width(), m_pixmap.height()); + const int rating = data().toInt(); + KRatingPainter::paintRating(&painter, rect, Qt::AlignJustify | Qt::AlignVCenter, rating); + } else { + m_pixmap = QPixmap(); + + QFontMetricsF fontMetrics(font()); + const QString text = fontMetrics.elidedText(data().toString(), Qt::ElideRight, maxWidth); + m_text.setText(text); + } +} + +#include "kstandarditemlistgroupheader.moc" diff --git a/src/kitemviews/kstandarditemlistgroupheader.h b/src/kitemviews/kstandarditemlistgroupheader.h new file mode 100644 index 000000000..26158d6ba --- /dev/null +++ b/src/kitemviews/kstandarditemlistgroupheader.h @@ -0,0 +1,57 @@ +/*************************************************************************** + * Copyright (C) 2011 by Peter Penz * + * * + * This program 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 2 of the License, or * + * (at your option) any later version. * + * * + * This program 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 this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#ifndef KSTANDARDITEMLISTGROUPHEADER_H +#define KSTANDARDITEMLISTGROUPHEADER_H + +#include + +#include + +#include +#include + +class LIBDOLPHINPRIVATE_EXPORT KStandardItemListGroupHeader : public KItemListGroupHeader +{ + Q_OBJECT + +public: + KStandardItemListGroupHeader(QGraphicsWidget* parent = 0); + virtual ~KStandardItemListGroupHeader(); + + virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0); + +protected: + virtual void paintRole(QPainter* painter, const QRectF& roleBounds, const QColor& color); + virtual void paintSeparator(QPainter* painter, const QColor& color); + virtual void roleChanged(const QByteArray ¤t, const QByteArray &previous); + virtual void dataChanged(const QVariant& current, const QVariant& previous); + virtual void resizeEvent(QGraphicsSceneResizeEvent* event); + +private: + void updateCache(); + +private: + bool m_dirtyCache; + QStaticText m_text; + QPixmap m_pixmap; +}; +#endif + + diff --git a/src/kitemviews/kstandarditemlistview.cpp b/src/kitemviews/kstandarditemlistview.cpp index bd5da9eb0..f4d05dcf6 100644 --- a/src/kitemviews/kstandarditemlistview.cpp +++ b/src/kitemviews/kstandarditemlistview.cpp @@ -22,6 +22,7 @@ #include #include #include "kstandarditemlistwidget.h" +#include "kstandarditemlistgroupheader.h" KStandardItemListView::KStandardItemListView(QGraphicsWidget* parent) : KItemListView(parent), @@ -82,7 +83,7 @@ KItemListWidgetCreatorBase* KStandardItemListView::defaultWidgetCreator() const KItemListGroupHeaderCreatorBase* KStandardItemListView::defaultGroupHeaderCreator() const { - return 0; // TODO: new KItemListGroupHeaderCreator() + return new KItemListGroupHeaderCreator(); } void KStandardItemListView::initializeItemListWidget(KItemListWidget* item) diff --git a/src/kitemviews/kstandarditemmodel.cpp b/src/kitemviews/kstandarditemmodel.cpp index 86ef9563b..897267df6 100644 --- a/src/kitemviews/kstandarditemmodel.cpp +++ b/src/kitemviews/kstandarditemmodel.cpp @@ -123,7 +123,22 @@ QString KStandardItemModel::roleDescription(const QByteArray& role) const QList > KStandardItemModel::groups() const { - return QList >(); + QList > groups; + + const QByteArray role = sortRole(); + bool isFirstGroupValue = true; + QString groupValue; + const int maxIndex = count() - 1; + for (int i = 0; i <= maxIndex; ++i) { + const QString newGroupValue = m_items.at(i)->dataValue(role).toString(); + if (newGroupValue != groupValue || isFirstGroupValue) { + groupValue = newGroupValue; + groups.append(QPair(i, newGroupValue)); + isFirstGroupValue = false; + } + } + + return groups; } #include "kstandarditemmodel.moc" -- cgit v1.3.1