26 #include <tqgroupbox.h>
28 #include <tqcheckbox.h>
30 #include <tqtooltip.h>
32 #include <tqwmatrix.h>
34 #include <tdeapplication.h>
35 #include <tdelocale.h>
36 #include <kstandarddirs.h>
37 #include <kiconview.h>
38 #include <ksimpleconfig.h>
40 #include "kcarddialog.h"
41 #include <tqpushbutton.h>
44 #define KCARD_DEFAULTDECK TQString::fromLatin1("deck0.png")
45 #define KCARD_DEFAULTCARD TQString::fromLatin1("11.png")
46 #define KCARD_DEFAULTCARDDIR TQString::fromLatin1("cards-default/")
49 #define SLIDER_MIN 400
50 #define SLIDER_MAX 3000
53 #define CONF_GROUP "KCardDialog"
54 #define CONF_RANDOMDECK TQString::fromLatin1("RandomDeck")
55 #define CONF_DECK TQString::fromLatin1("Deck")
56 #define CONF_CARDDIR TQString::fromLatin1("CardDir")
57 #define CONF_RANDOMCARDDIR TQString::fromLatin1("RandomCardDir")
58 #define CONF_USEGLOBALDECK TQString::fromLatin1("GlobalDeck")
59 #define CONF_USEGLOBALCARDDIR TQString::fromLatin1("GlobalCardDir")
60 #define CONF_SCALE TQString::fromLatin1("Scale")
62 #define CONF_GLOBAL_GROUP TQString::fromLatin1("KCardDialog Settings")
63 #define CONF_GLOBAL_DECK TQString::fromLatin1("GlobalDeck")
64 #define CONF_GLOBAL_CARDDIR TQString::fromLatin1("GlobalCardDir")
65 #define CONF_GLOBAL_RANDOMDECK TQString::fromLatin1("GlobalRandomDeck")
66 #define CONF_GLOBAL_RANDOMCARDDIR TQString::fromLatin1("GlobalRandomCardDir")
69 class KCardDialogPrivate
90 TDEIconView* deckIconView;
91 TDEIconView* cardIconView;
92 TQCheckBox* randomDeck;
93 TQCheckBox* randomCardDir;
94 TQCheckBox* globalDeck;
95 TQCheckBox* globalCardDir;
97 TQSlider* scaleSlider;
101 TQMap<TQIconViewItem*, TQString> deckMap;
102 TQMap<TQIconViewItem*, TQString> cardMap;
103 TQMap<TQString, TQString> helpMap;
113 CardFlags pFlags,
bool* pRandomDeck,
bool* pRandomCardDir,
114 double* pScale, TDEConfig* pConf)
125 int result=dlg.exec();
126 if (result==TQDialog::Accepted)
131 if (!pCardDir.isNull() && pCardDir.right(1)!=TQString::fromLatin1(
"/"))
133 pCardDir+=TQString::fromLatin1(
"/");
150 kdDebug(11000) <<
"use global deck" << endl;
152 getGlobalDeck(pDeck, random);
153 kdDebug(11000) <<
"use: " << pDeck<< endl;
158 kdDebug(11000) <<
"use random deck" << endl;
163 kdDebug(11000) <<
"use global carddir" << endl;
165 getGlobalCardDir(pCardDir, random);
166 kdDebug(11000) <<
"use: " << pCardDir << endl;
169 *pRandomCardDir=random;
171 kdDebug(11000) <<
"use random carddir" << endl;
185 TQString origGroup = conf->group();
187 conf->setGroup(CONF_GROUP);
188 if (conf->readBoolEntry(CONF_RANDOMDECK) || !conf->hasKey(CONF_DECK)) {
191 pDeck = conf->readEntry(CONF_DECK);
193 if (conf->readBoolEntry(CONF_RANDOMCARDDIR) || !conf->hasKey(CONF_CARDDIR)) {
196 pCardDir = conf->readPathEntry(CONF_CARDDIR);
198 pScale = conf->readDoubleNumEntry(CONF_SCALE, 1.0);
200 if (conf->readBoolEntry(CONF_USEGLOBALDECK,
false)) {
202 getGlobalDeck(pCardDir, random);
203 if (random || pDeck.isNull() ) {
207 if (conf->readBoolEntry(CONF_USEGLOBALCARDDIR,
false)) {
209 getGlobalCardDir(pCardDir, random);
210 if (random || pCardDir.isNull() ) {
215 conf->setGroup(origGroup);
221 return locate(
"cards", TQString::fromLatin1(
"decks/") + KCARD_DEFAULTDECK);
228 TQString file = KCARD_DEFAULTCARDDIR + KCARD_DEFAULTCARD;
229 return TDEGlobal::dirs()->findResourceDir(
"cards",file) + KCARD_DEFAULTCARDDIR;
236 TQString entry = carddir + TQString::number(index);
237 if (TDEStandardDirs::exists(entry + TQString::fromLatin1(
".png")))
238 return entry + TQString::fromLatin1(
".png");
241 if (TDEStandardDirs::exists(entry + TQString::fromLatin1(
".xpm")))
242 return entry + TQString::fromLatin1(
".xpm");
254 {
return (d->randomDeck ? d->randomDeck->isChecked() :
false); }
256 {
return (d->randomCardDir ? d->randomCardDir->isChecked() :
false); }
258 {
return (d->globalDeck ? d->globalDeck->isChecked() :
false); }
260 {
return (d->globalCardDir ? d->globalCardDir->isChecked() :
false); }
264 TQHBoxLayout* topLayout =
new TQHBoxLayout(plainPage(), spacingHint());
265 TQVBoxLayout* cardLayout =
new TQVBoxLayout(topLayout);
270 setInitialSize(TQSize(600,400));
272 if (! (
flags() & NoDeck))
274 TQHBoxLayout* layout =
new TQHBoxLayout(cardLayout);
277 TQGroupBox* grp1 =
new TQGroupBox(1,TQt::Horizontal, i18n(
"Choose Backside"), plainPage());
278 layout->addWidget(grp1);
280 d->deckIconView =
new TDEIconView(grp1,
"decks");
281 d->deckIconView->setSpacing(8);
286 d->deckIconView->setGridX(82);
287 d->deckIconView->setGridY(106);
288 d->deckIconView->setSelectionMode(TQIconView::Single);
289 d->deckIconView->setResizeMode(TQIconView::Adjust);
290 d->deckIconView->setMinimumWidth(360);
291 d->deckIconView->setMinimumHeight(170);
292 d->deckIconView->setWordWrapIconText(
false);
293 d->deckIconView->showToolTips();
296 TQVBoxLayout* l =
new TQVBoxLayout(layout);
297 TQGroupBox* grp3 =
new TQGroupBox(i18n(
"Backside"), plainPage());
298 grp3->setFixedSize(100, 130);
299 l->addWidget(grp3, 0, AlignTop|AlignHCenter);
300 d->deckLabel =
new TQLabel(grp3);
301 d->deckLabel->setText(i18n(
"empty"));
302 d->deckLabel->setAlignment(AlignHCenter|AlignVCenter);
303 d->deckLabel->setGeometry(10, 20, 80, 90);
305 d->randomDeck =
new TQCheckBox(plainPage());
306 d->randomDeck->setChecked(
false);
307 connect(d->randomDeck, TQ_SIGNAL(toggled(
bool)),
this,
308 TQ_SLOT(slotRandomDeckToggled(
bool)));
309 d->randomDeck->setText(i18n(
"Random backside"));
310 l->addWidget(d->randomDeck, 0, AlignTop|AlignHCenter);
312 d->globalDeck =
new TQCheckBox(plainPage());
313 d->globalDeck->setChecked(
false);
314 d->globalDeck->setText(i18n(
"Use global backside"));
315 l->addWidget(d->globalDeck, 0, AlignTop|AlignHCenter);
317 TQPushButton* b =
new TQPushButton(i18n(
"Make Backside Global"), plainPage());
318 connect(b, TQ_SIGNAL(pressed()),
this, TQ_SLOT(slotSetGlobalDeck()));
319 l->addWidget(b, 0, AlignTop|AlignHCenter);
321 connect(d->deckIconView,TQ_SIGNAL(clicked(TQIconViewItem *)),
322 this,TQ_SLOT(slotDeckClicked(TQIconViewItem *)));
325 if (! (
flags() & NoCards))
328 TQHBoxLayout* layout =
new TQHBoxLayout(cardLayout);
329 TQGroupBox* grp2 =
new TQGroupBox(1,TQt::Horizontal, i18n(
"Choose Frontside"), plainPage());
330 layout->addWidget(grp2);
332 d->cardIconView =
new TDEIconView(grp2,
"cards");
337 d->cardIconView->setGridX(82);
338 d->cardIconView->setGridY(106);
339 d->cardIconView->setResizeMode(TQIconView::Adjust);
340 d->cardIconView->setMinimumWidth(360);
341 d->cardIconView->setMinimumHeight(170);
342 d->cardIconView->setWordWrapIconText(
false);
343 d->cardIconView->showToolTips();
346 TQVBoxLayout* l =
new TQVBoxLayout(layout);
347 TQGroupBox* grp4 =
new TQGroupBox(i18n(
"Frontside"), plainPage());
348 grp4->setFixedSize(100, 130);
349 l->addWidget(grp4, 0, AlignTop|AlignHCenter);
350 d->cardLabel =
new TQLabel(grp4);
351 d->cardLabel->setText(i18n(
"empty"));
352 d->cardLabel->setAlignment(AlignHCenter|AlignVCenter);
353 d->cardLabel->setGeometry(10, 20, 80, 90 );
355 d->randomCardDir =
new TQCheckBox(plainPage());
356 d->randomCardDir->setChecked(
false);
357 connect(d->randomCardDir, TQ_SIGNAL(toggled(
bool)),
this,
358 TQ_SLOT(slotRandomCardDirToggled(
bool)));
359 d->randomCardDir->setText(i18n(
"Random frontside"));
360 l->addWidget(d->randomCardDir, 0, AlignTop|AlignHCenter);
362 d->globalCardDir =
new TQCheckBox(plainPage());
363 d->globalCardDir->setChecked(
false);
364 d->globalCardDir->setText(i18n(
"Use global frontside"));
365 l->addWidget(d->globalCardDir, 0, AlignTop|AlignHCenter);
367 TQPushButton* b =
new TQPushButton(i18n(
"Make Frontside Global"), plainPage());
368 connect(b, TQ_SIGNAL(pressed()),
this, TQ_SLOT(slotSetGlobalCardDir()));
369 l->addWidget(b, 0, AlignTop|AlignHCenter);
371 connect(d->cardIconView,TQ_SIGNAL(clicked(TQIconViewItem *)),
372 this,TQ_SLOT(slotCardClicked(TQIconViewItem *)));
377 if (! (
flags() & NoDeck))
380 d->deckIconView->arrangeItemsInGrid();
383 if (!
deck().isNull())
386 TQPixmap pixmap(file);
387 pixmap=pixmap.xForm(m);
388 d->deckLabel->setPixmap(pixmap);
389 TQToolTip::add(d->deckLabel,d->helpMap[file]);
394 if (! (
flags() & NoCards))
397 d->cardIconView->arrangeItemsInGrid();
402 file =
cardDir() + KCARD_DEFAULTCARD;
403 TQPixmap pixmap(file);
404 pixmap = pixmap.xForm(m);
405 d->cardLabel->setPixmap(pixmap);
406 TQToolTip::add(d->cardLabel,d->helpMap[
cardDir()]);
425 TQVBoxLayout* layout =
new TQVBoxLayout(topLayout);
426 TQGroupBox* grp =
new TQGroupBox(1,TQt::Horizontal, i18n(
"Resize Cards"), plainPage());
427 layout->setResizeMode(TQLayout::Fixed);
428 layout->addWidget(grp);
429 TQWidget* box =
new TQWidget(grp);
430 TQHBoxLayout* hbox =
new TQHBoxLayout(box, 0, spacingHint());
431 TQVBoxLayout* boxLayout =
new TQVBoxLayout(hbox);
434 d->scaleSlider =
new TQSlider(1, SLIDER_MAX, 1, (-1000+SLIDER_MIN+SLIDER_MAX),TQt::Horizontal, box);
435 d->scaleSlider->setMinValue(SLIDER_MIN);
436 connect(d->scaleSlider, TQ_SIGNAL(valueChanged(
int)),
this, TQ_SLOT(slotCardResized(
int)));
437 boxLayout->addWidget(d->scaleSlider, 0, AlignLeft);
439 TQPushButton* b =
new TQPushButton(i18n(
"Default Size"), box);
440 connect(b, TQ_SIGNAL(pressed()),
this, TQ_SLOT(slotDefaultSize()));
441 boxLayout->addWidget(b, 0, AlignLeft);
443 TQLabel* l =
new TQLabel(i18n(
"Preview:"), box);
444 boxLayout->addWidget(l);
446 d->cPreview =
new TQLabel(box);
447 boxLayout->addWidget(d->cPreview, 0, AlignCenter|AlignVCenter);
449 slotCardResized(d->scaleSlider->value());
453 void KCardDialog::insertCardIcons()
455 TQStringList list = TDEGlobal::dirs()->findAllResources(
"cards",
"card*/index.desktop",
false,
true);
465 for (TQStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
467 KSimpleConfig cfg(*it);
468 cfg.setGroup(TQString::fromLatin1(
"KDE Backdeck"));
469 TQString path = (*it).left((*it).findRev(
'/') + 1);
470 assert(path[path.length() - 1] ==
'/');
471 TQPixmap pixmap(path + cfg.readEntry(
"Preview",
"12c.png"));
476 TQString name=cfg.readEntry(
"Name", i18n(
"unnamed"));
477 TQIconViewItem *item=
new TQIconViewItem(d->cardIconView, name, pixmap);
479 item->setDragEnabled(
false);
480 item->setDropEnabled(
false);
481 item->setRenameEnabled(
false);
482 item->setSelectable(
true);
484 d->cardMap[item] = path;
485 d->helpMap[path] = cfg.readEntry(
"Comment",name);
489 void KCardDialog::insertDeckIcons()
491 TQStringList list = TDEGlobal::dirs()->findAllResources(
"cards",
"decks/*.desktop",
false,
true);
501 for (TQStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
503 KSimpleConfig cfg(*it);
504 TQPixmap pixmap(getDeckName(*it));
510 cfg.setGroup(TQString::fromLatin1(
"KDE Cards"));
511 TQString name=cfg.readEntry(
"Name", i18n(
"unnamed"));
512 TQIconViewItem *item=
new TQIconViewItem(d->deckIconView,name, pixmap);
514 item->setDragEnabled(
false);
515 item->setDropEnabled(
false);
516 item->setRenameEnabled(
false);
518 d->deckMap[item] = getDeckName(*it);
519 d->helpMap[d->deckMap[item]] = cfg.readEntry(
"Comment",name);
532 : KDialogBase( Plain, i18n(
"Carddeck Selection"), Ok|Cancel, Ok, parent, name, true, true)
536 d =
new KCardDialogPrivate;
540 void KCardDialog::slotDeckClicked(TQIconViewItem *item)
542 if (item && item->pixmap())
544 d->deckLabel->setPixmap(* (item->pixmap()));
545 TQToolTip::remove( d->deckLabel );
546 TQToolTip::add(d->deckLabel,d->helpMap[d->deckMap[item]]);
550 void KCardDialog::slotCardClicked(TQIconViewItem *item)
552 if (item && item->pixmap())
554 d->cardLabel->setPixmap(* (item->pixmap()));
555 TQString path = d->cardMap[item];
556 TQToolTip::remove( d->deckLabel );
557 TQToolTip::add(d->cardLabel,d->helpMap[path]);
562 TQString KCardDialog::getDeckName(
const TQString &desktop)
564 TQString entry = desktop.left(desktop.length() - strlen(
".desktop"));
565 if (TDEStandardDirs::exists(entry + TQString::fromLatin1(
".png")))
566 return entry + TQString::fromLatin1(
".png");
569 if (TDEStandardDirs::exists(entry + TQString::fromLatin1(
".xpm")))
570 return entry + TQString::fromLatin1(
".xpm");
578 TQStringList list = TDEGlobal::dirs()->findAllResources(
"cards",
"decks/*.desktop");
582 int d = TDEApplication::random() % list.count();
583 return getDeckName(*list.at(d));
590 TQStringList list = TDEGlobal::dirs()->findAllResources(
"cards",
"card*/index.desktop");
594 int d = TDEApplication::random() % list.count();
595 TQString entry = *list.at(d);
596 return entry.left(entry.length() - strlen(
"index.desktop"));
605 d->randomDeck->show();
607 d->randomDeck->hide();
612 if (!d->randomCardDir)
616 d->randomCardDir->show();
618 d->randomCardDir->hide();
621 void KCardDialog::slotRandomDeckToggled(
bool on)
624 d->deckLabel->setText(
"random");
627 d->deckLabel->setText(
"empty");
632 void KCardDialog::slotRandomCardDirToggled(
bool on)
635 d->cardLabel->setText(
"random");
637 if (
cardDir().length()>0 &&
cardDir().right(1)!=TQString::fromLatin1(
"/")) {
641 d->cardLabel->setText(
"empty");
652 TQString origGroup = conf->group();
654 conf->setGroup(CONF_GROUP);
655 if (! (
flags() & NoDeck)) {
656 if (conf->hasKey(CONF_DECK)) {
657 setDeck(conf->readEntry(CONF_DECK));
660 bool random = conf->readBoolEntry(CONF_RANDOMDECK,
false);
661 d->randomDeck->setChecked(random);
662 slotRandomDeckToggled(random);
664 if (conf->hasKey(CONF_USEGLOBALDECK) && conf->readBoolEntry(CONF_USEGLOBALDECK)) {
665 d->globalDeck->setChecked(
true);
667 d->globalDeck->setChecked(
false);
670 if (! (
flags() & NoCards)) {
671 if (conf->hasKey(CONF_CARDDIR)) {
672 setCardDir(conf->readPathEntry(CONF_CARDDIR));
675 bool random = conf->readBoolEntry(CONF_RANDOMCARDDIR,
false);
676 d->randomCardDir->setChecked(random);
677 slotRandomCardDirToggled(random);
679 if (conf->hasKey(CONF_USEGLOBALCARDDIR) && conf->readBoolEntry(CONF_USEGLOBALCARDDIR)) {
680 d->globalCardDir->setChecked(
true);
682 d->globalCardDir->setChecked(
false);
686 d->cScale = conf->readDoubleNumEntry(CONF_SCALE, 1.0);
688 conf->setGroup(origGroup);
691 void KCardDialog::slotCardResized(
int s)
696 if (s < SLIDER_MIN || s > SLIDER_MAX) {
697 kdError(11000) <<
"invalid scaling value!" << endl;
702 s += (SLIDER_MIN + SLIDER_MAX);
705 double scale = (double)1000/s;
706 m.scale(scale, scale);
707 TQPixmap pix = d->cPreviewPix.xForm(m);
708 d->cPreview->setPixmap(pix);
712 void KCardDialog::slotDefaultSize()
714 if (!d->scaleSlider) {
717 d->scaleSlider->setValue(-1000 + SLIDER_MIN + SLIDER_MAX);
725 TQString origGroup = conf->group();
727 conf->setGroup(CONF_GROUP);
728 if (! (
flags() & NoDeck)) {
729 conf->writeEntry(CONF_DECK,
deck());
731 conf->writeEntry(CONF_USEGLOBALDECK, d->globalDeck->isChecked());
733 if (! (
flags() & NoCards)) {
734 conf->writePathEntry(CONF_CARDDIR,
cardDir());
736 conf->writeEntry(CONF_USEGLOBALCARDDIR, d->globalCardDir->isChecked());
738 conf->writeEntry(CONF_SCALE, d->cScale);
740 conf->setGroup(origGroup);
743 void KCardDialog::slotSetGlobalDeck()
745 KSimpleConfig* conf =
new KSimpleConfig(TQString::fromLatin1(
"kdeglobals"),
false);
746 conf->setGroup(CONF_GLOBAL_GROUP);
748 conf->writeEntry(CONF_GLOBAL_DECK,
deck());
749 conf->writeEntry(CONF_GLOBAL_RANDOMDECK,
isRandomDeck());
754 void KCardDialog::slotSetGlobalCardDir()
756 KSimpleConfig* conf =
new KSimpleConfig(TQString::fromLatin1(
"kdeglobals"),
false);
757 conf->setGroup(CONF_GLOBAL_GROUP);
759 conf->writePathEntry(CONF_GLOBAL_CARDDIR,
cardDir());
765 void KCardDialog::getGlobalDeck(TQString& deck,
bool& random)
767 KSimpleConfig* conf =
new KSimpleConfig(TQString::fromLatin1(
"kdeglobals"),
true);
768 conf->setGroup(CONF_GLOBAL_GROUP);
770 if (!conf->hasKey(CONF_GLOBAL_DECK) || conf->readBoolEntry(CONF_GLOBAL_RANDOMDECK,
false)) {
774 deck = conf->readEntry(CONF_GLOBAL_DECK);
775 random = conf->readBoolEntry(CONF_GLOBAL_RANDOMDECK,
false);
781 void KCardDialog::getGlobalCardDir(TQString& dir,
bool& random)
783 KSimpleConfig* conf =
new KSimpleConfig(TQString::fromLatin1(
"kdeglobals"),
true);
784 conf->setGroup(CONF_GLOBAL_GROUP);
786 if (!conf->hasKey(CONF_GLOBAL_CARDDIR) || conf->readBoolEntry(CONF_GLOBAL_RANDOMCARDDIR,
false)) {
790 dir = conf->readPathEntry(CONF_GLOBAL_CARDDIR);
791 random = conf->readBoolEntry(CONF_GLOBAL_RANDOMCARDDIR,
false);
797 void KCardDialog::init()
799 static bool _inited =
false;
802 TDEGlobal::dirs()->addResourceType(
"cards", TDEStandardDirs::kde_default(
"data") + TQString::fromLatin1(
"carddecks/"));
804 TDEGlobal::locale()->insertCatalogue(
"libtdegames");
808 #include "kcarddialog.moc"
A carddeck selection dialog for card games.
KCardDialog(TQWidget *parent=NULL, const char *name=NULL, CardFlags flags=Both)
Constructs a card deck selection dialog.
static void getConfigCardDeck(TDEConfig *conf, TQString &deck, TQString &cardDir, double &scale)
Read the configuration from the applications rc file and put the previously chosen deck/frontside in ...
void setCardDir(const TQString &dir)
Sets the default card directory.
bool isRandomCardDir() const
static TQString getDefaultCardDir()
Returns the default path to the card frontsides.
const TQString & deck() const
Returns the chosen deck, which is a valid path to a imagefile.
void saveConfig(TDEConfig *conf)
Saves the KCardDialog config into a config file.
void showRandomDeckBox(bool s)
Show or hides the "random backside" checkbox.
void setupDialog(bool showResizeBox=false)
Creates the default widgets in the dialog.
static TQString getRandomCardDir()
Returns a random directory of cards.
void showRandomCardDirBox(bool s)
Show or hides the "random foreside" checkbox.
const TQString & cardDir() const
void loadConfig(TDEConfig *conf)
Load the default settings into the dialog (e.g.
static TQString getDefaultDeck()
Returns the default path to the card deck backsides.
static int getCardDeck(TQString &deck, TQString &carddir, TQWidget *parent=0, CardFlags flags=Both, bool *randomDeck=0, bool *randomCardDir=0, double *scale=0, TDEConfig *conf=0)
Creates a modal carddeck dialog, lets the user choose a deck, and returns when the dialog is closed.
static TQString getRandomDeck()
Returns a random deck in deckPath()
bool isRandomDeck() const
bool isGlobalDeck() const
void setDeck(const TQString &file)
Sets the default deck.
bool isGlobalCardDir() const
static TQString getCardPath(const TQString &carddir, int index)
Returns the path to the card frontside specified in dir carddir.
~KCardDialog()
Destructs a card deck selection dialog.