kcarddialog.cpp

00001 /*
00002     This file is part of the KDE games library
00003     Copyright (C) 2000 Martin Heni (martin@heni-online.de)
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License version 2 as published by the Free Software Foundation.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 /*
00020     $Id: kcarddialog.cpp 465369 2005-09-29 14:33:08Z mueller $
00021 */
00022 
00023 #include <stdio.h>
00024 #include <assert.h>
00025 
00026 #include <qgroupbox.h>
00027 #include <qlabel.h>
00028 #include <qcheckbox.h>
00029 #include <qlayout.h>
00030 #include <qtooltip.h>
00031 #include <qslider.h>
00032 #include <qwmatrix.h>
00033 
00034 #include <kapplication.h>
00035 #include <klocale.h>
00036 #include <kstandarddirs.h>
00037 #include <kiconview.h>
00038 #include <ksimpleconfig.h>
00039 
00040 #include "kcarddialog.h"
00041 #include <qpushbutton.h>
00042 #include <kdebug.h>
00043 
00044 #define KCARD_DEFAULTDECK QString::fromLatin1("deck0.png")
00045 #define KCARD_DEFAULTCARD QString::fromLatin1("11.png")
00046 #define KCARD_DEFAULTCARDDIR QString::fromLatin1("cards-default/")
00047 
00048 // values for the resize slider
00049 #define SLIDER_MIN 400
00050 #define SLIDER_MAX 3000
00051 
00052 // KConfig entries
00053 #define CONF_GROUP "KCardDialog"
00054 #define CONF_RANDOMDECK QString::fromLatin1("RandomDeck")
00055 #define CONF_DECK QString::fromLatin1("Deck")
00056 #define CONF_CARDDIR QString::fromLatin1("CardDir")
00057 #define CONF_RANDOMCARDDIR QString::fromLatin1("RandomCardDir")
00058 #define CONF_USEGLOBALDECK QString::fromLatin1("GlobalDeck")
00059 #define CONF_USEGLOBALCARDDIR QString::fromLatin1("GlobalCardDir")
00060 #define CONF_SCALE QString::fromLatin1("Scale")
00061 
00062 #define CONF_GLOBAL_GROUP QString::fromLatin1("KCardDialog Settings")
00063 #define CONF_GLOBAL_DECK QString::fromLatin1("GlobalDeck")
00064 #define CONF_GLOBAL_CARDDIR QString::fromLatin1("GlobalCardDir")
00065 #define CONF_GLOBAL_RANDOMDECK QString::fromLatin1("GlobalRandomDeck")
00066 #define CONF_GLOBAL_RANDOMCARDDIR QString::fromLatin1("GlobalRandomCardDir")
00067 
00068 
00069 class KCardDialogPrivate
00070 {
00071 public:
00072     KCardDialogPrivate()
00073     {
00074        deckLabel = 0;
00075        cardLabel = 0;
00076        deckIconView = 0;
00077        cardIconView = 0;
00078        randomDeck = 0;
00079        randomCardDir = 0;
00080        cPreview = 0;
00081        scaleSlider = 0;
00082        globalDeck = 0;
00083        globalCardDir = 0;
00084 
00085        cScale = 1;
00086     }
00087 
00088     QLabel* deckLabel;
00089     QLabel* cardLabel;
00090     KIconView* deckIconView;
00091     KIconView* cardIconView;
00092     QCheckBox* randomDeck;
00093     QCheckBox* randomCardDir;
00094     QCheckBox* globalDeck;
00095     QCheckBox* globalCardDir;
00096 
00097     QSlider* scaleSlider;
00098     QPixmap cPreviewPix;
00099     QLabel* cPreview;
00100 
00101     QMap<QIconViewItem*, QString> deckMap;
00102     QMap<QIconViewItem*, QString> cardMap;
00103     QMap<QString, QString> helpMap;
00104 
00105     //set query variables
00106     KCardDialog::CardFlags cFlags;
00107     QString cDeck;
00108     QString cCardDir;
00109     double cScale;
00110 };
00111 
00112 int KCardDialog::getCardDeck(QString &pDeck, QString &pCardDir, QWidget *pParent,
00113                              CardFlags pFlags, bool* pRandomDeck, bool* pRandomCardDir,
00114                  double* pScale, KConfig* pConf)
00115 {
00116     KCardDialog dlg(pParent, "dlg", pFlags);
00117 
00118     dlg.setDeck(pDeck);
00119     dlg.setCardDir(pCardDir);
00120 
00121     dlg.setupDialog(pScale != 0);
00122     dlg.loadConfig(pConf);
00123     dlg.showRandomDeckBox(pRandomDeck != 0);
00124     dlg.showRandomCardDirBox(pRandomCardDir != 0);
00125     int result=dlg.exec();
00126     if (result==QDialog::Accepted)
00127     {
00128     // TODO check for global cards/decks!!!!
00129         pDeck=dlg.deck();
00130         pCardDir=dlg.cardDir();
00131         if (!pCardDir.isNull() && pCardDir.right(1)!=QString::fromLatin1("/"))
00132         {
00133             pCardDir+=QString::fromLatin1("/");
00134         }
00135         if (pRandomDeck)
00136         {
00137             *pRandomDeck = dlg.isRandomDeck();
00138         }
00139         if (pRandomCardDir)
00140         {
00141             *pRandomCardDir = dlg.isRandomCardDir();
00142         }
00143     if (pScale)
00144     {
00145             *pScale = dlg.cardScale();
00146     }
00147 
00148         if (dlg.isGlobalDeck())
00149     {
00150         kdDebug(11000) << "use global deck" << endl;
00151         bool random;
00152         getGlobalDeck(pDeck, random);
00153         kdDebug(11000) << "use: " << pDeck<< endl;
00154         if (pRandomDeck)
00155         {
00156             *pRandomDeck=random;
00157         if (random)
00158             kdDebug(11000) << "use random deck" << endl;
00159         }
00160     }
00161         if (dlg.isGlobalCardDir())
00162     {
00163         kdDebug(11000) << "use global carddir" << endl;
00164         bool random;
00165         getGlobalCardDir(pCardDir, random);
00166         kdDebug(11000) << "use: " << pCardDir << endl;
00167         if (pRandomCardDir)
00168         {
00169             *pRandomCardDir=random;
00170         if (random)
00171             kdDebug(11000) << "use random carddir" << endl;
00172         }
00173     }
00174     }
00175     dlg.saveConfig(pConf);
00176     return result;
00177 }
00178 
00179 void KCardDialog::getConfigCardDeck(KConfig* conf, QString &pDeck, QString &pCardDir, double& pScale)
00180 {
00181 // TODO check for global cards/decks!!!!
00182  if (!conf) {
00183     return;
00184  }
00185  QString origGroup = conf->group();
00186 
00187  conf->setGroup(CONF_GROUP);
00188  if (conf->readBoolEntry(CONF_RANDOMDECK) || !conf->hasKey(CONF_DECK)) {
00189     pDeck = getRandomDeck();
00190  } else {
00191     pDeck = conf->readEntry(CONF_DECK);
00192  }
00193  if (conf->readBoolEntry(CONF_RANDOMCARDDIR) || !conf->hasKey(CONF_CARDDIR)) {
00194     pCardDir = getRandomCardDir();
00195  } else {
00196     pCardDir = conf->readPathEntry(CONF_CARDDIR);
00197  }
00198  pScale = conf->readDoubleNumEntry(CONF_SCALE, 1.0);
00199 
00200  if (conf->readBoolEntry(CONF_USEGLOBALDECK, false)) {
00201     bool random;
00202     getGlobalDeck(pCardDir, random);
00203     if (random || pDeck.isNull() ) {
00204         pDeck = getRandomDeck();
00205     }
00206  }
00207  if (conf->readBoolEntry(CONF_USEGLOBALCARDDIR, false)) {
00208     bool random;
00209     getGlobalCardDir(pCardDir, random);
00210     if (random || pCardDir.isNull() ) {
00211         pCardDir = getRandomCardDir();
00212     }
00213  }
00214 
00215  conf->setGroup(origGroup);
00216 }
00217 
00218 QString KCardDialog::getDefaultDeck()
00219 {
00220     KCardDialog::init();
00221     return locate("cards", QString::fromLatin1("decks/") + KCARD_DEFAULTDECK);
00222 }
00223 
00224 QString KCardDialog::getDefaultCardDir()
00225 {
00226     KCardDialog::init();
00227 
00228     QString file = KCARD_DEFAULTCARDDIR + KCARD_DEFAULTCARD;
00229     return KGlobal::dirs()->findResourceDir("cards",file) + KCARD_DEFAULTCARDDIR;
00230 }
00231 
00232 QString KCardDialog::getCardPath(const QString &carddir, int index)
00233 {
00234     KCardDialog::init();
00235 
00236     QString entry = carddir + QString::number(index);
00237     if (KStandardDirs::exists(entry + QString::fromLatin1(".png")))
00238         return entry + QString::fromLatin1(".png");
00239 
00240     // rather theoretical
00241     if (KStandardDirs::exists(entry + QString::fromLatin1(".xpm")))
00242         return entry + QString::fromLatin1(".xpm");
00243 
00244     return QString::null;
00245 }
00246 
00247 const QString& KCardDialog::deck() const { return d->cDeck; }
00248 void KCardDialog::setDeck(const QString& file) { d->cDeck=file; }
00249 const QString& KCardDialog::cardDir() const { return d->cCardDir; }
00250 void KCardDialog::setCardDir(const QString& dir) { d->cCardDir=dir; }
00251 KCardDialog::CardFlags KCardDialog::flags() const { return d->cFlags; }
00252 double KCardDialog::cardScale() const { return d->cScale; }
00253 bool KCardDialog::isRandomDeck() const
00254 { return (d->randomDeck ? d->randomDeck->isChecked() : false); }
00255 bool KCardDialog::isRandomCardDir() const
00256 { return (d->randomCardDir ? d->randomCardDir->isChecked() : false); }
00257 bool KCardDialog::isGlobalDeck() const
00258 { return (d->globalDeck ? d->globalDeck->isChecked() : false); }
00259 bool KCardDialog::isGlobalCardDir() const
00260 { return (d->globalCardDir ? d->globalCardDir->isChecked() : false); }
00261 
00262 void KCardDialog::setupDialog(bool showResizeBox)
00263 {
00264   QHBoxLayout* topLayout = new QHBoxLayout(plainPage(), spacingHint());
00265   QVBoxLayout* cardLayout = new QVBoxLayout(topLayout);
00266   QString path, file;
00267   QWMatrix m;
00268   m.scale(0.8,0.8);
00269 
00270   setInitialSize(QSize(600,400));
00271 
00272   if (! (flags() & NoDeck))
00273   {
00274     QHBoxLayout* layout = new QHBoxLayout(cardLayout);
00275 
00276     // Deck iconview
00277     QGroupBox* grp1 = new QGroupBox(1, Horizontal, i18n("Choose Backside"), plainPage());
00278     layout->addWidget(grp1);
00279 
00280     d->deckIconView = new KIconView(grp1,"decks");
00281     d->deckIconView->setSpacing(8);
00282     /*
00283     deckIconView->setGridX(-1);
00284     deckIconView->setGridY(50);
00285     */
00286     d->deckIconView->setGridX(82);
00287     d->deckIconView->setGridY(106);
00288     d->deckIconView->setSelectionMode(QIconView::Single);
00289     d->deckIconView->setResizeMode(QIconView::Adjust);
00290     d->deckIconView->setMinimumWidth(360);
00291     d->deckIconView->setMinimumHeight(170);
00292     d->deckIconView->setWordWrapIconText(false);
00293     d->deckIconView->showToolTips();
00294 
00295     // deck select
00296     QVBoxLayout* l = new QVBoxLayout(layout);
00297     QGroupBox* grp3 = new QGroupBox(i18n("Backside"), plainPage());
00298     grp3->setFixedSize(100, 130);
00299     l->addWidget(grp3, 0, AlignTop|AlignHCenter);
00300     d->deckLabel = new QLabel(grp3);
00301     d->deckLabel->setText(i18n("empty"));
00302     d->deckLabel->setAlignment(AlignHCenter|AlignVCenter);
00303     d->deckLabel->setGeometry(10, 20, 80, 90);
00304 
00305     d->randomDeck = new QCheckBox(plainPage());
00306     d->randomDeck->setChecked(false);
00307     connect(d->randomDeck, SIGNAL(toggled(bool)), this,
00308             SLOT(slotRandomDeckToggled(bool)));
00309     d->randomDeck->setText(i18n("Random backside"));
00310     l->addWidget(d->randomDeck, 0, AlignTop|AlignHCenter);
00311 
00312     d->globalDeck = new QCheckBox(plainPage());
00313     d->globalDeck->setChecked(false);
00314     d->globalDeck->setText(i18n("Use global backside"));
00315     l->addWidget(d->globalDeck, 0, AlignTop|AlignHCenter);
00316 
00317     QPushButton* b = new QPushButton(i18n("Make Backside Global"), plainPage());
00318     connect(b, SIGNAL(pressed()), this, SLOT(slotSetGlobalDeck()));
00319     l->addWidget(b, 0, AlignTop|AlignHCenter);
00320 
00321     connect(d->deckIconView,SIGNAL(clicked(QIconViewItem *)),
00322             this,SLOT(slotDeckClicked(QIconViewItem *)));
00323   }
00324 
00325   if (! (flags() & NoCards))
00326   {
00327     // Cards iconview
00328     QHBoxLayout* layout = new QHBoxLayout(cardLayout);
00329     QGroupBox* grp2 = new QGroupBox(1, Horizontal, i18n("Choose Frontside"), plainPage());
00330     layout->addWidget(grp2);
00331 
00332     d->cardIconView =new KIconView(grp2,"cards");
00333     /*
00334     cardIconView->setGridX(36);
00335     cardIconView->setGridY(50);
00336     */
00337     d->cardIconView->setGridX(82);
00338     d->cardIconView->setGridY(106);
00339     d->cardIconView->setResizeMode(QIconView::Adjust);
00340     d->cardIconView->setMinimumWidth(360);
00341     d->cardIconView->setMinimumHeight(170);
00342     d->cardIconView->setWordWrapIconText(false);
00343     d->cardIconView->showToolTips();
00344 
00345     // Card select
00346     QVBoxLayout* l = new QVBoxLayout(layout);
00347     QGroupBox* grp4 = new QGroupBox(i18n("Frontside"), plainPage());
00348     grp4->setFixedSize(100, 130);
00349     l->addWidget(grp4, 0, AlignTop|AlignHCenter);
00350     d->cardLabel = new QLabel(grp4);
00351     d->cardLabel->setText(i18n("empty"));
00352     d->cardLabel->setAlignment(AlignHCenter|AlignVCenter);
00353     d->cardLabel->setGeometry(10, 20, 80, 90 );
00354 
00355     d->randomCardDir = new QCheckBox(plainPage());
00356     d->randomCardDir->setChecked(false);
00357     connect(d->randomCardDir, SIGNAL(toggled(bool)), this,
00358             SLOT(slotRandomCardDirToggled(bool)));
00359     d->randomCardDir->setText(i18n("Random frontside"));
00360     l->addWidget(d->randomCardDir, 0, AlignTop|AlignHCenter);
00361 
00362     d->globalCardDir = new QCheckBox(plainPage());
00363     d->globalCardDir->setChecked(false);
00364     d->globalCardDir->setText(i18n("Use global frontside"));
00365     l->addWidget(d->globalCardDir, 0, AlignTop|AlignHCenter);
00366 
00367     QPushButton* b = new QPushButton(i18n("Make Frontside Global"), plainPage());
00368     connect(b, SIGNAL(pressed()), this, SLOT(slotSetGlobalCardDir()));
00369     l->addWidget(b, 0, AlignTop|AlignHCenter);
00370 
00371     connect(d->cardIconView,SIGNAL(clicked(QIconViewItem *)),
00372             this,SLOT(slotCardClicked(QIconViewItem *)));
00373   }
00374 
00375   // Insert deck icons
00376   // First find the default or alternate path
00377   if (! (flags() & NoDeck))
00378   {
00379       insertDeckIcons();
00380       d->deckIconView->arrangeItemsInGrid();
00381 
00382       // Set default icons if given
00383       if (!deck().isNull())
00384       {
00385           file=deck();
00386           QPixmap pixmap(file);
00387           pixmap=pixmap.xForm(m);
00388           d->deckLabel->setPixmap(pixmap);
00389           QToolTip::add(d->deckLabel,d->helpMap[file]);
00390       }
00391   }
00392 
00393   // Insert card icons
00394   if (! (flags() & NoCards))
00395   {
00396       insertCardIcons();
00397       d->cardIconView->arrangeItemsInGrid();
00398 
00399     // Set default icons if given
00400     if (!cardDir().isNull())
00401     {
00402         file = cardDir() + KCARD_DEFAULTCARD;
00403         QPixmap pixmap(file);
00404         pixmap = pixmap.xForm(m);
00405         d->cardLabel->setPixmap(pixmap);
00406         QToolTip::add(d->cardLabel,d->helpMap[cardDir()]);
00407     }
00408   }
00409 
00410   // insert resize box
00411   if (showResizeBox)
00412   {
00413     // this part is a little bit...tricky.
00414     // i'm sure there is a cleaner way but i cannot find it.
00415     // whenever the pixmap is resized (aka scaled) the box is resized, too. This
00416     // leads to an always resizing dialog which is *very* ugly. i worked around
00417     // this by using a QWidget which is the only child widget of the group box.
00418     // The other widget are managed inside this QWidget - a stretch area on the
00419     // right ensures that the KIconViews are not resized...
00420 
00421     // note that the dialog is still resized if you you scale the pixmap very
00422     // large. This is desired behaviour as i don't want to make the box even
00423     // larger but i want the complete pixmap to be displayed. the dialog is not
00424     // resized if you make the pixmap smaller again.
00425     QVBoxLayout* layout = new QVBoxLayout(topLayout);
00426     QGroupBox* grp = new QGroupBox(1, Horizontal, i18n("Resize Cards"), plainPage());
00427     layout->setResizeMode(QLayout::Fixed);
00428     layout->addWidget(grp);
00429     QWidget* box = new QWidget(grp);
00430     QHBoxLayout* hbox = new QHBoxLayout(box, 0, spacingHint());
00431     QVBoxLayout* boxLayout = new QVBoxLayout(hbox);
00432     hbox->addStretch(0);
00433 
00434     d->scaleSlider = new QSlider(1, SLIDER_MAX, 1, (-1000+SLIDER_MIN+SLIDER_MAX), Horizontal, box);
00435     d->scaleSlider->setMinValue(SLIDER_MIN);
00436     connect(d->scaleSlider, SIGNAL(valueChanged(int)), this, SLOT(slotCardResized(int)));
00437     boxLayout->addWidget(d->scaleSlider, 0, AlignLeft);
00438 
00439     QPushButton* b = new QPushButton(i18n("Default Size"), box);
00440     connect(b, SIGNAL(pressed()), this, SLOT(slotDefaultSize()));
00441     boxLayout->addWidget(b, 0, AlignLeft);
00442 
00443     QLabel* l = new QLabel(i18n("Preview:"), box);
00444     boxLayout->addWidget(l);
00445     d->cPreviewPix.load(getDefaultDeck());
00446     d->cPreview = new QLabel(box);
00447     boxLayout->addWidget(d->cPreview, 0, AlignCenter|AlignVCenter);
00448 
00449     slotCardResized(d->scaleSlider->value());
00450   }
00451 }
00452 
00453 void KCardDialog::insertCardIcons()
00454 {
00455     QStringList list = KGlobal::dirs()->findAllResources("cards", "card*/index.desktop", false, true);
00456     // kdDebug(11000) << "insert " << list.count() << endl;
00457     if (list.isEmpty())
00458         return;
00459 
00460     // We shrink the icons a little
00461     //
00462     QWMatrix m;
00463     m.scale(0.8,0.8);
00464 
00465     for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
00466     {
00467         KSimpleConfig cfg(*it);
00468         cfg.setGroup(QString::fromLatin1("KDE Backdeck"));
00469         QString path = (*it).left((*it).findRev('/') + 1);
00470         assert(path[path.length() - 1] == '/');
00471         QPixmap pixmap(path + cfg.readEntry("Preview", "12c.png"));
00472 
00473         if (pixmap.isNull())
00474             continue;
00475 
00476         QString name=cfg.readEntry("Name", i18n("unnamed"));
00477         QIconViewItem *item= new QIconViewItem(d->cardIconView, name, pixmap);
00478 
00479         item->setDragEnabled(false);
00480         item->setDropEnabled(false);
00481         item->setRenameEnabled(false);
00482         item->setSelectable(true);
00483 
00484         d->cardMap[item] = path;
00485         d->helpMap[path] = cfg.readEntry("Comment",name);
00486     }
00487 }
00488 
00489 void KCardDialog::insertDeckIcons()
00490 {
00491     QStringList list = KGlobal::dirs()->findAllResources("cards", "decks/*.desktop", false, true);
00492     if (list.isEmpty())
00493         return;
00494 
00495     QString label;
00496 
00497     // We shrink the icons a little
00498     QWMatrix m;
00499     m.scale(0.8,0.8);
00500 
00501     for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
00502     {
00503         KSimpleConfig cfg(*it);
00504         QPixmap pixmap(getDeckName(*it));
00505         if (pixmap.isNull())
00506             continue;
00507 
00508         // pixmap=pixmap.xForm(m);
00509 
00510         cfg.setGroup(QString::fromLatin1("KDE Cards"));
00511         QString name=cfg.readEntry("Name", i18n("unnamed"));
00512         QIconViewItem *item= new QIconViewItem(d->deckIconView,name, pixmap);
00513 
00514         item->setDragEnabled(false);
00515         item->setDropEnabled(false);
00516         item->setRenameEnabled(false);
00517 
00518         d->deckMap[item] = getDeckName(*it);
00519         d->helpMap[d->deckMap[item]] = cfg.readEntry("Comment",name);
00520     }
00521 }
00522 
00523 
00524 KCardDialog::~KCardDialog()
00525 {
00526  delete d;
00527 }
00528 
00529 
00530 // Create the dialog
00531 KCardDialog::KCardDialog( QWidget *parent, const char *name, CardFlags mFlags)
00532     : KDialogBase( Plain, i18n("Carddeck Selection"), Ok|Cancel, Ok, parent, name, true, true)
00533 {
00534     KCardDialog::init();
00535 
00536     d = new KCardDialogPrivate;
00537     d->cFlags = mFlags;
00538 }
00539 
00540 void KCardDialog::slotDeckClicked(QIconViewItem *item)
00541 {
00542     if (item && item->pixmap())
00543     {
00544         d->deckLabel->setPixmap(* (item->pixmap()));
00545         QToolTip::remove( d->deckLabel );
00546         QToolTip::add(d->deckLabel,d->helpMap[d->deckMap[item]]);
00547         setDeck(d->deckMap[item]);
00548     }
00549 }
00550 void KCardDialog::slotCardClicked(QIconViewItem *item)
00551 {
00552     if (item && item->pixmap())
00553     {
00554         d->cardLabel->setPixmap(* (item->pixmap()));
00555         QString path = d->cardMap[item];
00556         QToolTip::remove( d->deckLabel );
00557         QToolTip::add(d->cardLabel,d->helpMap[path]);
00558         setCardDir(path);
00559     }
00560 }
00561 
00562 QString KCardDialog::getDeckName(const QString &desktop)
00563 {
00564     QString entry = desktop.left(desktop.length() - strlen(".desktop"));
00565     if (KStandardDirs::exists(entry + QString::fromLatin1(".png")))
00566         return entry + QString::fromLatin1(".png");
00567 
00568     // rather theoretical
00569     if (KStandardDirs::exists(entry + QString::fromLatin1(".xpm")))
00570         return entry + QString::fromLatin1(".xpm");
00571     return QString::null;
00572 }
00573 
00574 QString KCardDialog::getRandomDeck()
00575 {
00576     KCardDialog::init();
00577 
00578     QStringList list = KGlobal::dirs()->findAllResources("cards", "decks/*.desktop");
00579     if (list.isEmpty())
00580         return QString::null;
00581 
00582     int d = KApplication::random() % list.count();
00583     return getDeckName(*list.at(d));
00584 }
00585 
00586 QString KCardDialog::getRandomCardDir()
00587 {
00588     KCardDialog::init();
00589 
00590     QStringList list = KGlobal::dirs()->findAllResources("cards", "card*/index.desktop");
00591     if (list.isEmpty())
00592         return QString::null;
00593 
00594     int d = KApplication::random() % list.count();
00595     QString entry = *list.at(d);
00596     return entry.left(entry.length() - strlen("index.desktop"));
00597 }
00598 
00599 void KCardDialog::showRandomDeckBox(bool s)
00600 {
00601     if (!d->randomDeck)
00602     return;
00603 
00604     if (s)
00605         d->randomDeck->show();
00606     else
00607         d->randomDeck->hide();
00608 }
00609 
00610 void KCardDialog::showRandomCardDirBox(bool s)
00611 {
00612     if (!d->randomCardDir)
00613     return;
00614 
00615     if (s)
00616         d->randomCardDir->show();
00617     else
00618         d->randomCardDir->hide();
00619 }
00620 
00621 void KCardDialog::slotRandomDeckToggled(bool on)
00622 {
00623   if (on) {
00624     d->deckLabel->setText("random");
00625     setDeck(getRandomDeck());
00626   } else {
00627     d->deckLabel->setText("empty");
00628     setDeck(0);
00629   }
00630 }
00631 
00632 void KCardDialog::slotRandomCardDirToggled(bool on)
00633 {
00634   if (on) {
00635       d->cardLabel->setText("random");
00636       setCardDir(getRandomCardDir());
00637       if (cardDir().length()>0 && cardDir().right(1)!=QString::fromLatin1("/"))  {
00638           setCardDir(cardDir() + QString::fromLatin1("/"));
00639       }
00640   } else {
00641       d->cardLabel->setText("empty");
00642       setCardDir(0);
00643   }
00644 }
00645 
00646 void KCardDialog::loadConfig(KConfig* conf)
00647 {
00648  if (!conf) {
00649     return;
00650  }
00651 
00652  QString origGroup = conf->group();
00653 
00654  conf->setGroup(CONF_GROUP);
00655  if (! (flags() & NoDeck)) {
00656     if (conf->hasKey(CONF_DECK)) {
00657         setDeck(conf->readEntry(CONF_DECK));
00658     }
00659 
00660     bool random = conf->readBoolEntry(CONF_RANDOMDECK, false);
00661     d->randomDeck->setChecked(random);
00662     slotRandomDeckToggled(random);
00663 
00664     if (conf->hasKey(CONF_USEGLOBALDECK) && conf->readBoolEntry(CONF_USEGLOBALDECK)) {
00665         d->globalDeck->setChecked(true);
00666     } else {
00667         d->globalDeck->setChecked(false);
00668     }
00669  }
00670  if (! (flags() & NoCards)) {
00671     if (conf->hasKey(CONF_CARDDIR)) {
00672         setCardDir(conf->readPathEntry(CONF_CARDDIR));
00673     }
00674 
00675     bool random = conf->readBoolEntry(CONF_RANDOMCARDDIR, false);
00676     d->randomCardDir->setChecked(random);
00677     slotRandomCardDirToggled(random);
00678 
00679     if (conf->hasKey(CONF_USEGLOBALCARDDIR) && conf->readBoolEntry(CONF_USEGLOBALCARDDIR)) {
00680         d->globalCardDir->setChecked(true);
00681     } else {
00682         d->globalCardDir->setChecked(false);
00683     }
00684  }
00685 
00686  d->cScale = conf->readDoubleNumEntry(CONF_SCALE, 1.0);
00687 
00688  conf->setGroup(origGroup);
00689 }
00690 
00691 void KCardDialog::slotCardResized(int s)
00692 {
00693  if (!d->cPreview) {
00694     return;
00695  }
00696  if (s < SLIDER_MIN || s > SLIDER_MAX) {
00697     kdError(11000) << "invalid scaling value!" << endl;
00698     return;
00699  }
00700 
00701  s *= -1;
00702  s += (SLIDER_MIN + SLIDER_MAX);
00703 
00704  QWMatrix m;
00705  double scale = (double)1000/s;
00706  m.scale(scale, scale);
00707  QPixmap pix = d->cPreviewPix.xForm(m);
00708  d->cPreview->setPixmap(pix);
00709  d->cScale = scale;
00710 }
00711 
00712 void KCardDialog::slotDefaultSize()
00713 {
00714  if (!d->scaleSlider) {
00715     return;
00716  }
00717  d->scaleSlider->setValue(-1000 + SLIDER_MIN + SLIDER_MAX);
00718 }
00719 
00720 void KCardDialog::saveConfig(KConfig* conf)
00721 {
00722  if (!conf) {
00723     return;
00724  }
00725  QString origGroup = conf->group();
00726 
00727  conf->setGroup(CONF_GROUP);
00728  if (! (flags() & NoDeck)) {
00729     conf->writeEntry(CONF_DECK, deck());
00730     conf->writeEntry(CONF_RANDOMDECK, isRandomDeck());
00731     conf->writeEntry(CONF_USEGLOBALDECK, d->globalDeck->isChecked());
00732  }
00733  if (! (flags() & NoCards)) {
00734     conf->writePathEntry(CONF_CARDDIR, cardDir());
00735     conf->writeEntry(CONF_RANDOMCARDDIR, isRandomCardDir());
00736     conf->writeEntry(CONF_USEGLOBALCARDDIR, d->globalCardDir->isChecked());
00737  }
00738  conf->writeEntry(CONF_SCALE, d->cScale);
00739 
00740  conf->setGroup(origGroup);
00741 }
00742 
00743 void KCardDialog::slotSetGlobalDeck()
00744 {
00745  KSimpleConfig* conf = new KSimpleConfig(QString::fromLatin1("kdeglobals"), false);
00746  conf->setGroup(CONF_GLOBAL_GROUP);
00747 
00748  conf->writeEntry(CONF_GLOBAL_DECK, deck());
00749  conf->writeEntry(CONF_GLOBAL_RANDOMDECK, isRandomDeck());
00750 
00751  delete conf;
00752 }
00753 
00754 void KCardDialog::slotSetGlobalCardDir()
00755 {
00756  KSimpleConfig* conf = new KSimpleConfig(QString::fromLatin1("kdeglobals"), false);
00757  conf->setGroup(CONF_GLOBAL_GROUP);
00758 
00759  conf->writePathEntry(CONF_GLOBAL_CARDDIR, cardDir());
00760  conf->writeEntry(CONF_GLOBAL_RANDOMCARDDIR, isRandomCardDir());
00761 
00762  delete conf;
00763 }
00764 
00765 void KCardDialog::getGlobalDeck(QString& deck, bool& random)
00766 {
00767  KSimpleConfig* conf = new KSimpleConfig(QString::fromLatin1("kdeglobals"), true);
00768  conf->setGroup(CONF_GLOBAL_GROUP);
00769 
00770  if (!conf->hasKey(CONF_GLOBAL_DECK) || conf->readBoolEntry(CONF_GLOBAL_RANDOMDECK, false)) {
00771     deck = getRandomDeck();
00772     random = true;
00773  } else {
00774     deck = conf->readEntry(CONF_GLOBAL_DECK);
00775     random = conf->readBoolEntry(CONF_GLOBAL_RANDOMDECK, false);
00776  }
00777 
00778  delete conf;
00779 }
00780 
00781 void KCardDialog::getGlobalCardDir(QString& dir, bool& random)
00782 {
00783  KSimpleConfig* conf = new KSimpleConfig(QString::fromLatin1("kdeglobals"), true);
00784  conf->setGroup(CONF_GLOBAL_GROUP);
00785 
00786  if (!conf->hasKey(CONF_GLOBAL_CARDDIR) || conf->readBoolEntry(CONF_GLOBAL_RANDOMCARDDIR, false)) {
00787     dir = getRandomCardDir();
00788     random = true;
00789  } else {
00790     dir = conf->readPathEntry(CONF_GLOBAL_CARDDIR);
00791     random = conf->readBoolEntry(CONF_GLOBAL_RANDOMCARDDIR, false);
00792  }
00793 
00794  delete conf;
00795 }
00796 
00797 void KCardDialog::init()
00798 {
00799     static bool _inited = false;
00800     if (_inited)
00801         return;
00802     KGlobal::dirs()->addResourceType("cards", KStandardDirs::kde_default("data") + QString::fromLatin1("carddecks/"));
00803 
00804     KGlobal::locale()->insertCatalogue("libkdegames");
00805     _inited = true;
00806 }
00807 
00808 #include "kcarddialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys