File indexing completed on 2024-04-21 04:05:21

0001 /*
0002     This file is part of the KDE games library
0003     SPDX-FileCopyrightText: 2008 Andreas Pakulat <apaku@gmx.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #ifndef CARDCACHE_H
0009 #define CARDCACHE_H
0010 
0011 #include <QFlags>
0012 
0013 class QPixmap;
0014 class QString;
0015 class QSize;
0016 class QSizeF;
0017 
0018 /**
0019  * \class KCardInfo cardcache.h <KCardCache>
0020  */
0021 class KCardInfo
0022 {
0023 public:
0024     enum Suit { None, Diamond, Heart, Club, Spade };
0025     enum Card { Joker, Ace, King, Queen, Jack, Ten, Nine, Eight, Seven, Six, Five, Four, Three, Two };
0026 
0027     KCardInfo(Suit s, Card c);
0028 
0029     void setCard(Card c);
0030     Card card() const;
0031 
0032     void setSuit(Suit s);
0033     Suit suit() const;
0034 
0035     QString svgName() const;
0036     bool operator==(KCardInfo c) const;
0037 private:
0038     Suit m_suit;
0039     Card m_card;
0040 };
0041 
0042 /**
0043  * \class KCardCache cardcache.h <KCardCache>
0044  *
0045  * This class implements a kdegames wide cache for cards.
0046  *
0047  * Card games such as lskat or kpat should use this cache
0048  * to load the various decks into QPixmaps instead of inventing
0049  * their own. It uses KImageCache behind the scenes, set up to
0050  * use disk and memory caching. Thus a SVG card deck that was loaded
0051  * by kpat for the size 100x200 does not need re-rendering when
0052  * requested from lskat.
0053  *
0054  * Usage is quite simple. During initialization of the game the
0055  * cache object should be created and it should be told to load the
0056  * currently selected theme.
0057  * <code>
0058  * myCache = new KCardCache();
0059  * myCache->loadTheme(myTheme);
0060  * </code>
0061  *
0062  * Later when actually drawing the cards the getter methods can be used to
0063  * get the pixmap of a specific card at a specific size from a given theme.
0064  * <code>
0065  * myCache->getCard(myTheme, KCardCache::Club, KCardCache::Ace, calculatedSize);
0066  * </code>
0067  *
0068  */
0069 class KCardCache
0070 {
0071 public:
0072     /**
0073      * Can be used to load only parts of a theme, in case
0074      * the user chose to have front and backside from different
0075      * themes
0076      */
0077     enum LoadInfo
0078     {
0079         LoadFrontSide = 1 << 0 /**< Load only the front sides of the theme. */,
0080         LoadBackSide = 1 << 2 /**< Load only the back side of the theme. */,
0081         Load52Cards = 1 << 3 /**< Load a 52 card deck, ranges from Ace down to two */,
0082         Load32Cards = 1 << 4 /**< Load a 32 card deck, ranges from Ace down to seven */,
0083         Load53Cards = 1 << 5 /**< Load a 52 card deck as above, but include Jolly Joker */
0084     };
0085     Q_DECLARE_FLAGS(LoadInfos, LoadInfo)
0086 
0087     /**
0088      * Constructor creates and initializes a KImageCache
0089      * card games
0090      */
0091     KCardCache();
0092 
0093     /**
0094      * Cleans up the cache
0095      */
0096     ~KCardCache();
0097 
0098     /**
0099      * Set the size of rendered pixmaps.
0100      *
0101      * Make sure to set a reasonable size, before fetching pixmaps from the
0102      * cache.
0103      *
0104      * @param size the new size for rendering cards and backsides
0105      */
0106     void setSize(const QSize &size);
0107 
0108     /**
0109      * Returns the currently used size to render cards and backsides.
0110      *
0111      * @returns the size of pixmaps for rendering.
0112      */
0113     QSize size() const;
0114 
0115     /**
0116      * Set the theme to be used to render the frontside of cards.
0117      *
0118      * Make sure to set a proper theme before fetching frontside pixmaps from
0119      * the cache.
0120      *
0121      * @param theme the name of the theme to be use for rendering frontsides
0122      */
0123     void setDeckName(const QString &theme);
0124 
0125     /**
0126      * Return the currently used frontside theme
0127      * @returns the name of the frontside theme
0128      */
0129     QString deckName() const;
0130 
0131     /**
0132      * Retrieve the backside of the given theme @p theme at the specified
0133      * size @p size
0134      *
0135      * Make sure to set a reasonable size and theme, before calling this
0136      * function.
0137      *
0138      * @param variant which variant, like a back with another color,
0139      * to use for rendering. Defaults to -1 which means no variant.
0140      *
0141      * @returns a QPixmap with the card backside rendered
0142      *
0143      * @see setBackTheme
0144      * @see setSize
0145      */
0146     QPixmap backside() const;
0147 
0148     /**
0149      * Invalidates all cached images in the current size for the current
0150      * backside theme
0151      */
0152     void invalidateBackside();
0153 
0154     /**
0155      * Retrieve the frontside pixmap.
0156      *
0157      * The @p info parameter is used to determine which frontside to load.
0158      * Make sure to set a reasonable size and theme, before calling this
0159      * function.
0160      *
0161      * @param info A combination of CardInfo flags to identify what type of card
0162      * to load. There are of course only certain combinations that make sense,
0163      * like King | Heart, some flags are used standalone, like Joker.
0164      *
0165      * @returns a QPixmap with the card frontside rendered
0166      *
0167      * @see setBackTheme
0168      * @see setSize
0169      * @see CardInfo
0170      */
0171     QPixmap frontside(KCardInfo info) const;
0172 
0173     /**
0174      * Invalidates all cached images in the current size for the current
0175      * frontside theme
0176      */
0177     void invalidateCache();
0178 
0179     /**
0180      * Loads a whole theme in the background.
0181      *
0182      * Depending on the value of @p type only parts may be rendered.
0183      *
0184      * @param infos whether to load all entries in the theme or just the front
0185      * or back sides. Also its possible to specify a different deck, like a
0186      * 32 card deck.
0187      */
0188     void loadTheme(LoadInfos infos = LoadInfos(LoadFrontSide | LoadBackSide | Load53Cards));
0189 private:
0190     class KCardCachePrivate *const d;
0191 };
0192 
0193 Q_DECLARE_OPERATORS_FOR_FLAGS(KCardCache::LoadInfos)
0194 
0195 #endif