File indexing completed on 2024-03-24 15:43:12

0001 /*
0002  * Copyright 1999 by Martin R. Jones <mjones@kde.org>
0003  * Copyright 2010 by Stefan Böhmann <kde@hilefoks.org>
0004  *
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0018  */
0019 #include "amorthememanager.h"
0020 #include "amoranimation.h"
0021 #include "amorpixmapmanager.h"
0022 
0023 #include <QFile>
0024 #include <QSettings>
0025 #include <QStandardPaths>
0026 #include <QRandomGenerator>
0027 
0028 AmorThemeManager::AmorThemeManager()
0029   : mConfig( 0 ),
0030     mMaximumSize(0, 0)
0031 {
0032 }
0033 
0034 
0035 AmorThemeManager::~AmorThemeManager()
0036 {
0037     for( const AmorAnimationGroup &group : mAnimations ) {
0038         qDeleteAll( group );
0039     }
0040     delete mConfig;
0041 }
0042 
0043 
0044 bool AmorThemeManager::setTheme(const QString & file)
0045 {
0046     if (QFile::exists(file)) {
0047         mPath = file;
0048     } else {
0049         mPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, file);
0050     }
0051 
0052     if (mPath.isEmpty() || !QFile::exists(mPath)) {
0053         return false;
0054     }
0055 
0056     delete mConfig;
0057     mConfig = new QSettings(mPath, QSettings::IniFormat);
0058     mConfig->beginGroup(QLatin1String("Config"));
0059 
0060     // Get the directory where the pixmaps are stored and tell the pixmap manager.
0061     QString pixmapPath = mConfig->value(QLatin1String("PixmapPath")).toString();
0062     if( pixmapPath.isEmpty() ) {
0063         return false;
0064     }
0065 
0066     if( pixmapPath[0] == QLatin1Char( '/' ) ) {
0067         mPath = pixmapPath; // absolute path to pixmaps
0068     }
0069     else {
0070         mPath.truncate( mPath.lastIndexOf( QLatin1Char( '/' ) ) + 1 ); // relative to config file
0071         mPath += pixmapPath;
0072     }
0073 
0074     mStatic = mConfig->value(QLatin1String("Static")).toBool();
0075 
0076     mMaximumSize.setWidth( 0 );
0077     mMaximumSize.setHeight( 0 );
0078 
0079     for( const AmorAnimationGroup &group : mAnimations ) {
0080         qDeleteAll( group );
0081     }
0082     mAnimations.clear();
0083     mConfig->endGroup();
0084 
0085     return true;
0086 }
0087 
0088 
0089 AmorAnimation *AmorThemeManager::random(const QString & group)
0090 {
0091     QString grp = mStatic ? QLatin1String( "Base" ) : group;
0092 
0093     const QHash<QString, AmorAnimationGroup>::const_iterator it = mAnimations.constFind( grp );
0094 
0095     if( it != mAnimations.constEnd() ) {
0096         int idx = QRandomGenerator::global()->bounded(it->count());
0097         return it->at( idx );
0098     }
0099 
0100     return 0;
0101 }
0102 
0103 
0104 bool AmorThemeManager::readGroup(const QString & seq)
0105 {
0106     AmorPixmapManager::manager()->setPixmapDir( mPath );
0107     AmorAnimationGroup animList;
0108 
0109     // Read the list of available animations.
0110     mConfig->beginGroup(QLatin1String("Config"));
0111     QStringList list = mConfig->value(seq).toStringList();
0112     mConfig->endGroup();
0113 
0114     // Read each individual animation
0115     for(int i = 0; i < list.count(); ++i) {
0116         mConfig->beginGroup(list[i]);
0117         AmorAnimation *anim = new AmorAnimation( mConfig );
0118         animList.append( anim );
0119         mMaximumSize = mMaximumSize.expandedTo( anim->maximumSize() );
0120         mConfig->endGroup();
0121     }
0122 
0123     int entries = list.count();
0124     if ( entries == 0) {    // If no animations were available for this group, just add the base anim
0125         mConfig->beginGroup(QLatin1String("Base"));
0126         AmorAnimation *anim = new AmorAnimation( mConfig);
0127         if( anim ) {
0128             animList.append( anim );
0129             mMaximumSize = mMaximumSize.expandedTo( anim->maximumSize() );
0130             ++entries;
0131         }
0132         mConfig->endGroup();
0133     }
0134 
0135     // Couldn't read any entries at all
0136     if( entries == 0 ) {
0137         return false;
0138     }
0139 
0140     mAnimations[seq] = animList;
0141 
0142     return true;
0143 }
0144 
0145 
0146 bool AmorThemeManager::isStatic() const
0147 {
0148     return mStatic;
0149 }
0150 
0151 
0152 QSize AmorThemeManager::maximumSize() const
0153 {
0154     return mMaximumSize;
0155 }
0156 
0157 
0158 // kate: word-wrap off; encoding utf-8; indent-width 4; tab-width 4; line-numbers on; mixed-indent off; remove-trailing-space-save on; replace-tabs-save on; replace-tabs on; space-indent on;
0159 // vim:set spell et sw=4 ts=4 nowrap cino=l1,cs,U1: