kcarddialog.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the KDE games library
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License version 2 as published by the Free Software Foundation.
00007 
00008     This library is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011     Library General Public License for more details.
00012 
00013     You should have received a copy of the GNU Library General Public License
00014     along with this library; see the file COPYING.LIB.  If not, write to
00015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016     Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "kcarddialog.h"
00020 #include "ui_kgamecardselector.h"
00021 
00022 #include <QPixmap>
00023 #include <QListWidgetItem>
00024 #include <QFileInfo>
00025 #include <QDir>
00026 
00027 #include <klocale.h>
00028 #include <kstandarddirs.h>
00029 #include <krandom.h>
00030 #include <kdebug.h>
00031 
00032 #include "carddeckinfo.h"
00033 #include "carddeckinfo_p.h"
00034 
00035 // KConfig entries
00036 #define CONF_GROUP  QString::fromLatin1("KCardDialog")
00037 #define CONF_LOCKING QString::fromLatin1("Locking")
00038 #define CONF_ALLOW_FIXED_CARDS QString::fromLatin1("AllowFixed")
00039 #define CONF_ALLOW_SCALED_CARDS QString::fromLatin1("AllowScaled")
00040 #define CONF_SHOWONLY_FIXED_CARDS QString::fromLatin1("ShowFixedOnly")
00041 #define CONF_SHOWONLY_SCALED_CARDS QString::fromLatin1("ShowScaledOnly")
00042 #define CONF_CARD QString::fromLatin1("Cardname")
00043 #define CONF_DECK QString::fromLatin1("Deckname")
00044 
00048 class KCardDialogPrivate
00049 {
00050   public:
00051 
00054     KCardDialogPrivate()
00055     {
00056        // Set default values
00057        useSVGOnly = false;
00058        usePNGOnly = false;
00059        useLocking = true;
00060     }
00061 
00067     bool filterOutCard(const KCardThemeInfo& v)
00068     {
00069       if (usePNGOnly && !v.svgfile.isNull()) return true;
00070       if (useSVGOnly && v.svgfile.isNull()) return true;
00071       return false;
00072     }
00073 
00076     QString currentBack;
00077 
00080     QString currentFront;
00081 
00084     Ui::KGameCardSelectorBase ui;
00085 
00088     bool useSVGOnly;
00089 
00092     bool usePNGOnly;
00093 
00096     bool allowSVG;
00097 
00100     bool allowPNG;
00101 
00104     bool useLocking;
00105 };
00106 
00107 // Create the dialog
00108 KCardDialog::KCardDialog(QWidget *parent, bool pAllowSVG, bool pAllowPNG, bool pLock, QString defFront, QString defBack)
00109            : KDialog( parent ), d( new KCardDialogPrivate )
00110 {
00111   // Copy parameter
00112   d->useLocking = pLock;
00113   d->allowPNG = pAllowPNG;
00114   d->allowSVG = pAllowSVG;
00115   d->currentFront = defFront;
00116   d->currentBack = defBack;
00117 
00118   // GUI
00119   setupGUI();
00120 }
00121 
00122 // Create the dialog from a config group
00123 KCardDialog::KCardDialog(KConfigGroup& group, QWidget* parent)
00124            : KDialog( parent ), d( new KCardDialogPrivate )
00125 {
00126   d->useLocking  = group.readEntry(CONF_LOCKING, true);
00127   d->allowPNG    = group.readEntry(CONF_ALLOW_FIXED_CARDS, true);
00128   d->allowSVG    = group.readEntry(CONF_ALLOW_SCALED_CARDS, true);
00129   d->usePNGOnly  = group.readEntry(CONF_SHOWONLY_FIXED_CARDS, false);
00130   d->useSVGOnly  = group.readEntry(CONF_SHOWONLY_SCALED_CARDS, false);
00131   d->currentFront = group.readEntry(CONF_CARD, QString());
00132   d->currentBack = group.readEntry(CONF_DECK, QString());
00133 
00134   // GUI
00135   setupGUI();
00136 }
00137 
00138 
00139 // Store the config group settings
00140 void KCardDialog::saveSettings(KConfigGroup& group)
00141 {
00142   group.writeEntry(CONF_LOCKING, d->useLocking);
00143   group.writeEntry(CONF_ALLOW_FIXED_CARDS, d->allowPNG );
00144   group.writeEntry(CONF_ALLOW_SCALED_CARDS, d->allowSVG );
00145   group.writeEntry(CONF_SHOWONLY_FIXED_CARDS, d->usePNGOnly );
00146   group.writeEntry(CONF_SHOWONLY_SCALED_CARDS, d->useSVGOnly );
00147   group.writeEntry(CONF_CARD, d->currentFront);
00148   group.writeEntry(CONF_DECK, d->currentBack);
00149 }
00150 
00151 
00152 // Setup the user interface
00153 void KCardDialog::setupGUI()
00154 {
00155   // Dialog
00156   setCaption(i18n("Carddeck Selection"));
00157   setButtons(Ok|Cancel);
00158   setDefaultButton(Ok);
00159   setModal(true);
00160   showButtonSeparator(true);
00161   //adjustSize();
00162 
00163   // Inner widget
00164   Ui::KGameCardSelectorBase* ui = &(d->ui);
00165   d->ui.setupUi(mainWidget());
00166   
00167   // Game does not allow fixed sized cards
00168   if (!d->allowPNG)
00169   {
00170     ui->checkBoxPNG->setEnabled(false);
00171     ui->checkBoxSVG->setEnabled(false);
00172     ui->checkBoxPNG->setCheckState(Qt::Unchecked);
00173     d->usePNGOnly = false;
00174     d->useSVGOnly = true;
00175   }
00176 
00177   // Game does not allow scaled cards
00178   if (!d->allowSVG)
00179   {
00180     ui->checkBoxSVG->setEnabled(false);
00181     ui->checkBoxPNG->setEnabled(false);
00182     ui->checkBoxSVG->setCheckState(Qt::Unchecked);
00183     d->useSVGOnly = false;
00184     d->usePNGOnly = true;
00185   }
00186 
00187   // Set checkboxes
00188   if (d->useLocking) ui->checkBoxLock->setCheckState(Qt::Checked);
00189   if (d->useSVGOnly) ui->checkBoxSVG->setCheckState(Qt::Checked);
00190   if (d->usePNGOnly) ui->checkBoxPNG->setCheckState(Qt::Checked);
00191 
00192   // Game wants locked backsides?
00193   ui->backList->setEnabled(!d->useLocking);
00194 
00195   // Set lists and preview
00196    if (d->currentFront.isNull()) d->currentFront = CardDeckInfo::defaultFrontName(d->allowSVG, d->allowPNG);
00197    if (d->currentBack.isNull()) d->currentBack = CardDeckInfo::defaultBackName(d->allowSVG, d->allowPNG);
00198   insertCardIcons();
00199   insertDeckIcons();
00200   updateFront(d->currentFront);
00201   updateBack(d->currentBack);
00202 
00203   
00204   // Connect signals
00205   connect(ui->frontList, SIGNAL(itemSelectionChanged()),
00206           this, SLOT(updateFront()));
00207   connect(ui->backList, SIGNAL(itemSelectionChanged()),
00208           this, SLOT(updateBack()));
00209   connect(ui->checkBoxLock, SIGNAL(stateChanged(int)), this, SLOT(updateLocking(int)));
00210   connect(ui->checkBoxSVG, SIGNAL(stateChanged(int)), this, SLOT(updateSVG(int)));
00211   connect(ui->checkBoxPNG, SIGNAL(stateChanged(int)), this, SLOT(updatePNG(int)));
00212 
00213   // Debug
00214   // kDebug() << "DEFAULT DECK: " << defaultDeckName(pAllowSVG, pAllowPNG);
00215   // kDebug() << "DEFAULT CARD: " << defaultCardName(pAllowSVG, pAllowPNG);
00216   // kDebug() << "RANDOM DECK: " << randomDeckName(pAllowSVG, pAllowPNG);
00217   // kDebug() << "RANDOM CARD: " << randomCardName(pAllowSVG, pAllowPNG);
00218 }
00219 
00220 
00221 // Destroy the dialog
00222 KCardDialog::~KCardDialog()
00223 {
00224   delete d;
00225 }
00226 
00227 
00228 
00229 // Retrieve card set and deck from dialog.
00230 int KCardDialog::getCardDeck(QString &pFrontName,
00231                              QString &pBackName,
00232                              QWidget *pParent,
00233                              bool pAllowSVG,
00234                              bool pAllowPNG,
00235                              bool pLock, 
00236                              bool pRandom 
00237                              )
00238 {
00239   // If random cards we need no dialog (KDE 3.x compatibility)
00240   if (pRandom)
00241   {
00242     pFrontName = CardDeckInfo::randomFrontName();
00243     pBackName  = CardDeckInfo::randomBackName();
00244     return QDialog::Accepted;
00245   }
00246 
00247   KCardDialog dlg(pParent, pAllowSVG, pAllowPNG, pLock, pFrontName, pBackName);
00248 
00249 
00250   int result=dlg.exec();
00251   if (result==QDialog::Accepted)
00252   {
00253     pFrontName = dlg.frontName();
00254     pBackName  = dlg.backName();
00255   }
00256   return result;
00257 }
00258 
00259 
00260 // Retrieve selected deck name
00261 QString KCardDialog::backName() const
00262 {
00263   return d->currentBack;
00264 }
00265 
00266 
00267 // Retrieve selected card name
00268 QString KCardDialog::frontName() const
00269 { 
00270   return d->currentFront;
00271 }
00272 
00273 // Build list widget
00274 void KCardDialog::insertCardIcons()
00275 {
00276 
00277     // Clear GUI
00278     d->ui.frontList->clear();
00279 
00280     // Rebuild list
00281     QSize itemSize;
00282     foreach( QString name, CardDeckInfo::frontNames() )
00283     {
00284         KCardThemeInfo v = CardDeckInfo::frontInfo( name );
00285         // Show only SVG files?
00286         if (d->filterOutCard(v)) continue;
00287 
00288         QPixmap previewPixmap = v.preview.scaled(QSize(32,43), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
00289 
00290         QListWidgetItem *item = new QListWidgetItem(v.name, d->ui.frontList);
00291         item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00292         item->setToolTip(v.name);
00293         item->setData(Qt::DecorationRole, previewPixmap);
00294         itemSize = itemSize.expandedTo(previewPixmap.size());
00295     }
00296     d->ui.frontList->setIconSize(itemSize);
00297     
00298     // Prevent empty preview
00299     if (d->useSVGOnly && !CardDeckInfo::isSVGFront(d->currentFront))
00300         updateFront(CardDeckInfo::defaultFrontName(!d->usePNGOnly, !d->useSVGOnly));
00301     else if (d->usePNGOnly && CardDeckInfo::isSVGFront(d->currentFront))
00302         updateFront(CardDeckInfo::defaultFrontName(!d->usePNGOnly, !d->useSVGOnly));
00303     else
00304         updateFront(d->currentFront);
00305 }
00306 
00307 
00308 // Update front preview
00309 void KCardDialog::updateFront()
00310 {
00311   QList<QListWidgetItem*> l = d->ui.frontList->selectedItems();
00312   if( !l.isEmpty() )
00313       updateFront(l.first()->text());
00314 }
00315 
00316 
00317 // Update front preview
00318 
00319 void KCardDialog::updateFront(QString item)
00320 {
00321   // Clear item?
00322   if (item.isNull())
00323   {
00324     QList<QListWidgetItem*> items = d->ui.frontList->selectedItems();
00325     if( !items.isEmpty() )
00326         items.first()->setSelected( false );
00327     d->ui.frontPreview->setPixmap(QPixmap());
00328     d->ui.cardName->setText(QString());
00329     d->ui.cardDescription->setText(QString());
00330   }
00331   else
00332   {
00333     QList<QListWidgetItem*> items = d->ui.frontList->findItems(item, Qt::MatchExactly );
00334     if( !items.isEmpty() )
00335         items.first()->setSelected( true );
00336     KCardThemeInfo info = CardDeckInfo::frontInfo(item);
00337     QFont font;
00338     font.setBold(true);
00339     d->ui.cardName->setText(info.name);
00340     d->ui.cardName->setFont(font);
00341 
00342     d->ui.cardDescription->setText(info.comment);
00343     QPixmap pixmap= info.preview;
00344     if (pixmap.height() > d->ui.frontPreview->height())
00345       pixmap = pixmap.scaledToHeight(d->ui.frontPreview->height(), Qt::SmoothTransformation);
00346     if (pixmap.width() > d->ui.frontPreview->width())
00347       pixmap = pixmap.scaledToWidth(d->ui.frontPreview->width(), Qt::SmoothTransformation);
00348     d->ui.frontPreview->setPixmap(pixmap);
00349 
00350     // Lock front and back side?
00351     if (d->useLocking && !info.back.isNull())
00352     {
00353       updateBack(info.back);
00354     }
00355     else if (d->useLocking)
00356     {
00357       // QMap<QString, KCardThemeInfo>::const_iterator it = d->deckInfo.constBegin();
00358       QString name = CardDeckInfo::defaultBackName(!d->usePNGOnly, !d->useSVGOnly);
00359       updateBack(name);
00360     }
00361   }
00362   d->currentFront = item;
00363 }
00364 
00365 
00366 // Retrieve default card directory
00367 QString KCardDialog::getDefaultCardDir(bool pAllowSVG, bool pAllowPNG)
00368 {
00369   QString name = CardDeckInfo::defaultFrontName(pAllowSVG, pAllowPNG);
00370   return CardDeckInfo::frontDir(name);
00371 }
00372 
00373 
00374 // Retrieve default deck file name
00375 QString KCardDialog::getDefaultDeck(bool pAllowSVG, bool pAllowPNG)
00376 {
00377   QString name = CardDeckInfo::defaultBackName(pAllowSVG, pAllowPNG);
00378   return CardDeckInfo::backFilename(name);
00379 }
00380 
00381 
00382 // Update the locking filter
00383 void KCardDialog::updateLocking(int state)
00384 {
00385   if (state == Qt::Checked)
00386   {
00387     d->useLocking = true;
00388     // Update previews
00389     updateFront(d->currentFront);
00390   }
00391   else
00392   {
00393     d->useLocking = false;
00394   }
00395   d->ui.backList->setEnabled(!d->useLocking);
00396 }
00397 
00398 
00399 // Update the SVG status filter
00400 void KCardDialog::updateSVG(int state)
00401 {
00402   if (state == Qt::Checked)
00403   {
00404     d->useSVGOnly = true;
00405   }
00406   else
00407   {
00408     d->useSVGOnly = false;
00409   }
00410   //Prevent filtering out everything
00411   if (d->usePNGOnly && d->useSVGOnly)
00412   { 
00413     d->usePNGOnly = false;
00414     d->ui.checkBoxPNG->setCheckState(Qt::Unchecked);
00415   }
00416   insertCardIcons();
00417   insertDeckIcons();
00418 }
00419 
00420 
00421 // Update the PNG status filter
00422 void KCardDialog::updatePNG(int state)
00423 {
00424   if (state == Qt::Checked)
00425   {
00426     d->usePNGOnly = true;
00427   }
00428   else
00429   {
00430     d->usePNGOnly = false;
00431   }
00432   //Prevent filtering out everything
00433   if (d->usePNGOnly && d->useSVGOnly)
00434   { 
00435     d->useSVGOnly = false;
00436     d->ui.checkBoxSVG->setCheckState(Qt::Unchecked);
00437   }
00438   insertCardIcons();
00439   insertDeckIcons();
00440 }
00441 
00442 
00443 // Update the back preview
00444 void KCardDialog::updateBack()
00445 {
00446   QList<QListWidgetItem*> l = d->ui.backList->selectedItems();
00447   if( !l.isEmpty() )
00448       updateBack(l.first()->text());
00449 }
00450 
00451 
00452 // Update the back preview
00453 void KCardDialog::updateBack(QString item)
00454 {
00455   if (item.isNull())
00456   {
00457     QList<QListWidgetItem*> items = d->ui.backList->selectedItems();
00458     if( !items.isEmpty() )
00459         items.first()->setSelected( false );
00460     d->ui.backPreview->setPixmap(QPixmap());
00461   }
00462   else
00463   {
00464     QList<QListWidgetItem*> items = d->ui.backList->findItems(item, Qt::MatchExactly );
00465     if( !items.isEmpty() )
00466         items.first()->setSelected( true );
00467     KCardThemeInfo info = CardDeckInfo::backInfo(item);
00468     QPixmap pixmap= info.preview;
00469     if (pixmap.height() > d->ui.backPreview->height())
00470       pixmap = pixmap.scaledToHeight(d->ui.backPreview->height(), Qt::SmoothTransformation);
00471     if (pixmap.width() > d->ui.backPreview->width())
00472       pixmap = pixmap.scaledToWidth(d->ui.backPreview->width(), Qt::SmoothTransformation);
00473     d->ui.backPreview->setPixmap(pixmap);
00474   }
00475   d->currentBack = item;
00476 }
00477 
00478 
00479 // Insert the deck icons into the list widget
00480 void KCardDialog::insertDeckIcons()
00481 {
00482     // Clear GUI
00483     d->ui.backList->clear();
00484 
00485     // Rebuild list
00486     QSize itemSize;
00487     foreach( QString name, CardDeckInfo::backNames() )
00488     {
00489         KCardThemeInfo v = CardDeckInfo::backInfo( name );
00490         // Show only SVG files?
00491         if (d->filterOutCard(v)) continue;
00492         QPixmap previewPixmap = v.preview.scaled(QSize(32,43), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
00493 
00494         QListWidgetItem *item = new QListWidgetItem(v.name, d->ui.backList);
00495         item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00496         item->setToolTip(v.name);
00497         item->setData(Qt::DecorationRole, previewPixmap);
00498         itemSize = itemSize.expandedTo(previewPixmap.size());
00499     }
00500     d->ui.backList->setIconSize(itemSize);
00501     
00502     // Prevent empty preview
00503     if (d->useSVGOnly && !CardDeckInfo::isSVGBack(d->currentBack))
00504         updateBack(CardDeckInfo::defaultBackName(!d->usePNGOnly, !d->useSVGOnly));
00505     else if (d->usePNGOnly && CardDeckInfo::isSVGBack(d->currentBack))
00506         updateBack(CardDeckInfo::defaultBackName(!d->usePNGOnly, !d->useSVGOnly));
00507     else
00508         updateBack(d->currentBack);
00509 
00510 }
00511 
00512 #include "kcarddialog.moc"

Generated on Sun Mar 16 08:02:52 2008 for Libkdegames by  doxygen 1.5.3