Fix wrong icon name (reported by Krazy of the EBN fame)
[kphotoalbum.git] / Browser / AbstractCategoryModel.cpp
blobd021be9181e18a49ac9e2b7fc4025d0f935fcee4
1 /* Copyright (C) 2003-2009 Jesper K. Pedersen <blackie@kde.org>
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public
5 License as published by the Free Software Foundation; either
6 version 2 of the License, or (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "AbstractCategoryModel.h"
20 #include "BrowserWidget.h"
21 #include <QApplication>
22 #include <klocale.h>
23 #include <DB/ImageDB.h>
24 #include <DB/MemberMap.h>
25 #include <KIcon>
26 #include "enums.h"
28 Browser::AbstractCategoryModel::AbstractCategoryModel( const DB::CategoryPtr& category, const DB::ImageSearchInfo& info )
29 : _category( category ), _info( info )
31 _images = DB::ImageDB::instance()->classify( info, _category->name(), DB::Image );
32 _videos = DB::ImageDB::instance()->classify( info, _category->name(), DB::Video );
35 bool Browser::AbstractCategoryModel::hasNoneEntry() const
37 int imageCount = _images[DB::ImageDB::NONE()];
38 int videoCount = _videos[DB::ImageDB::NONE()];
39 return (imageCount + videoCount != 0);
42 QString Browser::AbstractCategoryModel::text( const QString& name ) const
44 if ( name == DB::ImageDB::NONE() ) {
45 if ( _info.categoryMatchText(_category->name()).length() == 0 )
46 return i18n( "None" );
47 else
48 return i18n( "No other" );
50 else if ( name == QString::fromLatin1( "Video" ) )
51 return i18n("Video");
52 else if ( name == QString::fromLatin1( "Image" ) )
53 return i18n("Image");
55 else {
56 if ( _category->name() == QString::fromLatin1( "Folder" ) ) {
57 QRegExp rx( QString::fromLatin1( "(.*/)(.*)$") );
58 QString value = name;
59 value.replace( rx, QString::fromLatin1("\\2") );
60 return value;
61 } else {
62 return name;
67 QPixmap Browser::AbstractCategoryModel::icon( const QString& name ) const
69 if ( BrowserWidget::isResizing() ) {
70 const int size = _category->thumbnailSize();
71 QPixmap res( size, 3.0/4.0 * size );
72 res.fill( Qt::white );
73 return res;
76 if ( _category->viewType() == DB::Category::TreeView || _category->viewType() == DB::Category::IconView ) {
77 if ( DB::ImageDB::instance()->memberMap().isGroup( _category->name(), name ) )
78 return KIcon( QString::fromLatin1( "folder-image" ) ).pixmap(22);
79 else {
80 return _category->icon();
83 else {
84 // The category images are screenshot from the size of the viewer (Which might very well be considered a bug)
85 // This is the reason for asking for the thumbnail height being 3/4 of its width.
86 return _category->categoryImage( _category->name(), name, _category->thumbnailSize(), _category->thumbnailSize() * 3.0 / 4.0 );
90 QVariant Browser::AbstractCategoryModel::data( const QModelIndex & index, int role) const
92 if ( !index.isValid() )
93 return QVariant();
94 const QString name = indexToName( index );
95 const int column = index.column();
97 if ( role == Qt::DisplayRole ) {
98 switch( column ) {
99 case 0: return text(name);
100 case 1: return i18np("1 image", "%1 images", _images[name]);
101 case 2: return i18np("1 video", "%1 videos", _videos[name]);
105 else if ( role == Qt::DecorationRole && column == 0) {
106 return icon( name );
109 else if ( role == Qt::ToolTipRole )
110 return text(name);
112 else if ( role == ItemNameRole )
113 return name;
115 else if ( role == ValueRole ) {
116 switch ( column ) {
117 case 0: return name; // Notice we sort by **None** rather than None, which makes it show up at the top for less than searches.
118 case 1: return _images[name];
119 case 2: return _videos[name];
123 return QVariant();
126 Qt::ItemFlags Browser::AbstractCategoryModel::flags( const QModelIndex& index ) const
128 return index.isValid() ? Qt::ItemIsSelectable | Qt::ItemIsEnabled : Qt::ItemFlags();
131 QVariant Browser::AbstractCategoryModel::headerData( int section, Qt::Orientation orientation, int role) const
133 if ( orientation == Qt::Vertical || role != Qt::DisplayRole )
134 return QVariant();
136 switch ( section ) {
137 case 0: return _category->text();
138 case 1: return i18n("Images");
139 case 2: return i18n("Videos");
142 return QVariant();