[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdegames/lskat
From:       Martin Heni <martin () heni-online ! de>
Date:       2007-04-22 16:48:31
Message-ID: 1177260511.038865.28686.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 656896 by heni:

Support of SVG cardsets.


 M  +3 -1      grafix/default.rc  
 M  +0 -5      src/config_two.cpp  
 M  +3 -2      src/mainwindow.cpp  
 M  +110 -16   src/thememanager.cpp  
 M  +29 -3     src/thememanager.h  


--- trunk/KDE/kdegames/lskat/grafix/default.rc #656895:656896
@@ -2,8 +2,10 @@
 [general]
 # SVG file to load (path relative to this file) which contains all SVG items
 svgfile=default.svg
-# Game apsecit ration
+# Game aspect ratio
 aspect-ratio=1.6
+# Card aspect ratio
+card-aspect-ratio=0.75
 
 # The 'game display' itself
 [display_two]
--- trunk/KDE/kdegames/lskat/src/config_two.cpp #656895:656896
@@ -68,8 +68,6 @@
   KConfigGroup group = cfg->group("LSkatData");
   group.writeEntry("input0", (int)mInputTypes[0]);
   group.writeEntry("input1", (int)mInputTypes[1]);
-  kDebug() << "Save input 0 = " << (int)mInputTypes[0] << endl;
-  kDebug() << "Save input 1 = " << (int)mInputTypes[1] << endl;
 
   // Save player
   QHashIterator<int,Player*> it = playerIterator();
@@ -91,10 +89,8 @@
   KConfigGroup group = cfg->group("LSkatData");
   int num;
   num = group.readEntry("input0", (int)mInputTypes[0]);
-  kDebug() << "Load input 0 = " << num << endl;
   setInputType(0, (InputDeviceType)num);
   num = group.readEntry("input1", (int)mInputTypes[1]);
-  kDebug() << "Load input 1 = " << num << endl;
   setInputType(1, (InputDeviceType)num);
 
   // Load player
@@ -133,7 +129,6 @@
 // Set the input type for a given players
 void ConfigTwo::setInputType(int no, InputDeviceType type)
 {
-  kDebug() << "Setinput " << no << " to " << (int)type<< endl;
   mInputTypes[no] = type;
   emit signalInputType(no, type);
 }
--- trunk/KDE/kdegames/lskat/src/mainwindow.cpp #656895:656896
@@ -403,7 +403,8 @@
   QString s1,s2;
   int result;
 
-  result=KCardDialog::getCardDeck(s1,s2);
+  KCardDialog::CardFlags flags = \
KCardDialog::CardFlags(KCardDialog::Both|KCardDialog::SVGCards); +  \
result=KCardDialog::getCardDeck(s1,s2, this, flags);  if (result==QDialog::Accepted)
   {
     kDebug() << "NEW CARDDECK: " << s1 << " and " << s2 << endl;
@@ -420,7 +421,7 @@
     }
     if (change)
     {
-      mTheme->updatePixmapCardTheme(mCardDir, mDeckGrafix);
+      mTheme->updateCardTheme(mCardDir, mDeckGrafix);
       mView->update(); // Be on the safe side and update
     }
   }
--- trunk/KDE/kdegames/lskat/src/thememanager.cpp #656895:656896
@@ -34,6 +34,7 @@
 
 // Local includes
 #include "thememanager.h"
+#include "deck.h"
 
 // Constructor for the theme manager
 ThemeManager::ThemeManager(QString cards, QString deck, bool svg, QString themefile, \
QObject* parent, int initialSize) @@ -41,11 +42,15 @@
 {
   mScale = initialSize;
   mAspectRatio = 1.0;
+  mCardAspectRatio = 1.0;
   mSVGCards = svg;
   mCardFile = cards;
   mDeckFile = deck;
+  mRenderer = 0;
+  mCardRenderer = 0;
 
-  updateTheme(themefile);
+  // updateTheme(themefile);
+  updateCardTheme(themefile, cards, deck);
 }
 
 
@@ -71,15 +76,42 @@
 
 
 // Update a pixmap card deck 
-void ThemeManager::updatePixmapCardTheme(QString cards, QString deck)
+void ThemeManager::updateCardTheme(QString cards, QString deck)
 {
+  updateCardTheme(mThemeFile, cards, deck);
+}
+void ThemeManager::updateCardTheme(QString themefile, QString cards, QString deck)
+{
   kDebug() << "ThemeManager Pixmap cards: "<< endl;
   kDebug() << "  Cards = " << cards << endl;
   kDebug() << "  Deck  = " << deck << endl;
-  mSVGCards = false;
+
+  KConfig cardInfo( cards+"/index.desktop", KConfig::OnlyLocal);
+  KConfigGroup group(&cardInfo, "KDE Backdeck");
+  QPointF cardSize   = group.readEntry("BackSize", QPointF(1.0,1.0));
+  QString svg        = group.readEntry("SVG", QString());
+  kDebug() << "cardSize = " << cardSize << endl;
+
+  mSVGCards = !svg.isNull();
+  kDebug() << "SVG = " << svg << " is " << mSVGCards << endl;
   mCardFile = cards;
   mDeckFile = deck;
-  updateTheme(mThemeFile);
+
+  QString svgfile = cards+"/"+svg;
+
+  mCardRenderer = 0;
+  if (mSVGCards)
+  {
+    mCardRenderer = new KSvgRenderer(this);
+    bool result   = mCardRenderer->load(svgfile);
+    if (!result) 
+    {
+      kFatal() << "Cannot open file " << svgfile << endl;
+    }
+    kDebug() << "Rendering " << svgfile << endl;
+  }
+
+  updateTheme(themefile);
 }
 
 
@@ -101,18 +133,18 @@
   svgfile = KStandardDirs::locate("data", svgfile);
   kDebug() << "Reading SVG master file  = " << svgfile << endl;
 
-  mAspectRatio =  config("general").readEntry("aspect-ratio", 1.0);
-  kDebug() << "Aspect ration = " << mAspectRatio << endl;
+  mAspectRatio     =  config("general").readEntry("aspect-ratio", 1.0);
+  mCardAspectRatio =  config("general").readEntry("card-aspect-ratio", 1.0);
+  kDebug() << "Aspect ration = " << mAspectRatio << " Cards aspect=" << \
mCardAspectRatio<< endl;  
 
 
-  mRenderer = new QSvgRenderer(this);
+  mRenderer = new KSvgRenderer(this);
   bool result = mRenderer->load(svgfile);
   if (!result) 
   {
     kFatal() << "Cannot open file " << svgfile << endl;
   }
-  kDebug() << "Renderer " << mRenderer<<" = " << result << endl;
 
   // Notify all theme objects of a change
   QHashIterator<Themable*, int> it(mObjects);
@@ -156,17 +188,63 @@
    return grp;
 }
 
+
+QString ThemeManager::calcCardSVGId(int no)
+{
+    const static char *ids[] = { "back",
+                               "1_club", "1_spade", "1_heart", "1_diamond",
+                               "king_club", "king_spade", "king_heart", \
"king_diamond", +                               "queen_club", "queen_spade", \
"queen_heart", "queen_diamond", +                               "jack_club", \
"jack_spade", "jack_heart", "jack_diamond", +                               \
"10_club", "10_spade", "10_heart", "10_diamond", +                               \
"9_club", "9_spade", "9_heart", "9_diamond", +                               \
"8_club", "8_spade", "8_heart", "8_diamond", +                               \
"7_club", "7_spade", "7_heart", "7_diamond", +                               \
"6_club", "6_spade", "6_heart", "6_diamond", +                               \
"5_club", "5_spade", "5_heart", "5_diamond", +                               \
"4_club", "4_spade", "4_heart", "4_diamond", +                               \
"3_club", "3_spade", "3_heart", "3_diamond", +                               \
"2_club", "2_spade", "2_heart", "2_diamond", +                               0 };
+  return QString(ids[no]);
+}
+
 const QPixmap ThemeManager::getCard(int suite, int cardtype, double width)
 {
-  QString dir = mCardFile;
+  // Card no
   int no = 4*cardtype+suite+1;
-  QString file = QString("%1.png").arg(no);
+
   QPixmap pm;
-  if (!pm.load(dir+"/"+file))
+  // SVG cards
+  if (mSVGCards)
   {
-    kError() << "Cannot load card file " << dir+file << endl;
+    QString svgid = calcCardSVGId(no);
+    QSize size = QSize(int(width), int(width/mCardAspectRatio) );
+    // kDebug() << "Card " << Deck::name((Suite)suite, (CardType)cardtype) << " => \
"<< svgid << "S="<<size<< endl; +    pm = getPixmap(mCardRenderer, svgid, size);
   }
-  return pm.scaledToWidth(int(width), Qt::SmoothTransformation);
+  // Pixmap cards
+  else
+  {
+    QString dir = mCardFile;
+    QString file = QString("%1.png").arg(no);
+    if (!pm.load(dir+"/"+file))
+    {
+      kError() << "Cannot load card file " << dir+file << endl;
+    }
+  }
+
+  // Scale to card aspect ratio if severly wrong
+  double aspect = double(pm.width())/double(pm.height());
+  if (aspect/mCardAspectRatio >1.05 || aspect/mCardAspectRatio < 0.95)
+  {
+    //kWarning() << "Wrong card aspect ratio " << aspect << " vs " << \
mCardAspectRatio << endl; +    return pm.scaled(int(width), \
int(width/mCardAspectRatio), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); +  }
+  else
+  {
+    return pm.scaledToWidth(int(width), Qt::SmoothTransformation);
+  }
 }
 
 const QPixmap ThemeManager::getCardback(double width)
@@ -177,12 +255,23 @@
   {
     kError() << "Cannot load deck file " << file << endl;
   }
-  return pm.scaledToWidth(int(width), Qt::SmoothTransformation);
+
+  // Scale to card aspect ratio if severly wrong
+  double aspect = double(pm.width())/double(pm.height());
+  if (aspect/mCardAspectRatio >1.05 || aspect/mCardAspectRatio < 0.95)
+  {
+    //kWarning() << "Wrong deck aspect ratio " << aspect << " vs " << \
mCardAspectRatio << endl; +    return pm.scaled(int(width), \
int(width/mCardAspectRatio), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); +  }
+  else
+  {
+    return pm.scaledToWidth(int(width), Qt::SmoothTransformation);
+  }
 }
 
 
 // Get a pixmap when its size is given (this can distort the image)
-const QPixmap ThemeManager::getPixmap(QString svgid, QSize size)
+const QPixmap ThemeManager::getPixmap(KSvgRenderer* renderer, QString svgid, QSize \
size)  {
   if (size.width() < 1 || size.height() < 1) 
     kFatal() << "ThemeManager::getPixmap Cannot create svgid ID " << svgid << " with \
zero size " << size << endl; @@ -203,7 +292,7 @@
   QImage image(size, QImage::Format_ARGB32_Premultiplied);
   image.fill(0);
   QPainter p(&image);
-  mRenderer->render(&p, svgid);
+  renderer->render(&p, svgid);
   pixmap = QPixmap::fromImage(image);
   if (pixmap.isNull())
     kFatal() << "ThemeManager::getPixmap Cannot load svgid ID " << svgid << endl;
@@ -214,6 +303,11 @@
   return pixmap;
 }
 
+// Get a pixmap when its size is given (this can distort the image)
+const QPixmap ThemeManager::getPixmap(QString svgid, QSize size)
+{
+  return getPixmap(mRenderer, svgid, size);
+}
 
 // Get a pixmap when only width is given (this keeps the aspect ratio)
 const QPixmap ThemeManager::getPixmap(QString svgid, double width)
--- trunk/KDE/kdegames/lskat/src/thememanager.h #656895:656896
@@ -22,11 +22,11 @@
 
 // Qt includes
 #include <QObject>
-#include <QSvgRenderer>
 #include <QHash>
 
 // KDE includes
 #include <kconfig.h>
+#include <KSvgRenderer>
 
 
 class ThemeManager;
@@ -176,7 +176,7 @@
       * @param cards The card dir
       * @param deck  The deck file
       */
-    void updatePixmapCardTheme(QString cards, QString deck);
+    void updateCardTheme(QString cards, QString deck);
     
     /** Forces an update to all theme objects. That is their
       * changeTheme() method is called. Before this a (new)
@@ -199,10 +199,32 @@
       */
     double aspectRatio() {return mAspectRatio;}
     
+   protected:
+    /** Load a pixmap from the SVG theme file. Its filename is given in the
+      * "general" section of the theme file as "svgfile". The pixmap is scaled
+      * to the given size.
+      * @param renderer The SVG renderer to use
+      * @param svgid    The ID of the SVG item to be rendered as pixmap
+      * @param size     The size of the resulting pixmap
+      * @return The new pixmap.
+      */
+    const QPixmap getPixmap(KSvgRenderer* renderer, QString svgid, QSize size);
 
+    /** Forces an update to the card theme objects. 
+      * @param themefile The theme rc file
+      * @param cards     The card dir
+      * @param deck      The deck file
+      */
+    void updateCardTheme(QString themefile, QString cards, QString deck);
+
+    QString calcCardSVGId(int no);
+
    private:
      // The used SVG rendered
-     QSvgRenderer* mRenderer;
+     KSvgRenderer* mRenderer;
+
+     // The card SVG rendered
+     KSvgRenderer* mCardRenderer;
      
      // Storage of all theme objects [object,1] [TODO: Alist might suffice]
      QHash<Themable*,int> mObjects;
@@ -228,6 +250,10 @@
      // The aspect ration
      double mAspectRatio;
 
+     // The card aspect ration
+     double mCardAspectRatio;
+
+
      // The theme file
      QString mThemeFile;
      


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic