• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames/kggzgames

kggzseatsdialog.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the kggzgames library.
00003     Copyright (c) 2005 - 2007 Josef Spillner <josef@ggzgamingzone.org>
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 as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kggzseatsdialog.h"
00022 
00023 #include <kggzmod/module.h>
00024 #include <kggzmod/player.h>
00025 #include <kggzmod/statistics.h>
00026 
00027 #include <kdebug.h>
00028 #include <klocale.h>
00029 #include <kseparator.h>
00030 #include <kcombobox.h>
00031 #include <kio/job.h>
00032 
00033 #include <qlayout.h>
00034 #include <qframe.h>
00035 #include <qlabel.h>
00036 #include <qpushbutton.h>
00037 #include <qlcdnumber.h>
00038 #include <qscrollarea.h>
00039 #include <qimage.h>
00040 #include <qmenu.h>
00041 #include <qtoolbutton.h>
00042 
00043 #include <math.h>
00044 
00045 class KGGZSeatsDialogPrivate
00046 {
00047     public:
00048         KGGZSeatsDialogPrivate(KGGZSeatsDialog* qq);
00049 
00050         KGGZSeatsDialog* q;
00051 
00052         KGGZMod::Module *m_mod;
00053         QScrollArea *m_view;
00054         QWidget *m_root;
00055         QMap<int, QLabel*> m_hostnames;
00056         QMap<int, QLabel*> m_realnames;
00057         QMap<int, QFrame*> m_photos;
00058         QMap<KJob*, int> m_phototasks;
00059         QMap<KJob*, QByteArray> m_photodata;
00060         QMap<const QObject*, int> m_buttons;
00061         QMap<const QObject*, QToolButton*> m_buttondata;
00062         int m_oldmode;
00063         KGGZMod::Player *m_currentplayer;
00064 
00065         enum DisplayModes
00066         {
00067             displayseats,
00068             displayspectators
00069         };
00070 
00071         QAction *action_standup;
00072         QAction *action_sitdown;
00073         QAction *action_bootplayer;
00074         QAction *action_botadd;
00075         QAction *action_botremove;
00076         QAction *action_viewstats;
00077 
00078         void displaySeats();
00079         void displaySpectators();
00080         void infos();
00081 
00082         // slots
00083         void slotDisplay(int id);
00084         void slotTaskData(KIO::Job *job, const QByteArray&);
00085         void slotTaskResult(KJob *job);
00086         void slotInfo(const KGGZMod::Event& event);
00087         void slotAction();
00088         void slotMenu(QAction *action);
00089 };
00090 
00091 KGGZSeatsDialog::KGGZSeatsDialog(QWidget *parent)
00092 : QWidget(parent), d(new KGGZSeatsDialogPrivate(this))
00093 {
00094     setMod(KGGZMod::Module::instance());
00095 }
00096 
00097 KGGZSeatsDialogPrivate::KGGZSeatsDialogPrivate(KGGZSeatsDialog* qq)
00098 : q(qq)
00099 {
00100     m_root = NULL;
00101     m_mod = NULL;
00102     m_oldmode = displayseats;
00103 
00104     m_view = new QScrollArea();
00105     //m_view->setResizePolicy(QScrollArea::AutoOneFit);
00106 
00107     KSeparator *sep1 = new KSeparator();
00108     KSeparator *sep2 = new KSeparator();
00109 
00110     QLabel *label = new QLabel(i18n("Display:"));
00111 
00112     KComboBox *combo = new KComboBox();
00113     combo->addItem(i18n("Involved players and bots"), displayseats);
00114     combo->addItem(i18n("Spectators"), displayspectators);
00115 
00116     QPushButton *ok = new QPushButton(i18n("Close"));
00117 
00118     QVBoxLayout *box = new QVBoxLayout();
00119     box->addWidget(m_view);
00120     box->addWidget(sep1);
00121     QHBoxLayout *box2 = new QHBoxLayout();
00122     box2->addWidget(label);
00123     box2->addWidget(combo);
00124     box2->addStretch(1);
00125     box->addLayout(box2);
00126     box->addWidget(sep2);
00127     box->addWidget(ok);
00128     q->setLayout(box);
00129 
00130     QObject::connect(combo, SIGNAL(activated(int)), q, SLOT(slotDisplay(int)));
00131     QObject::connect(ok, SIGNAL(clicked()), q, SLOT(close()));
00132 
00133     q->setWindowTitle(i18n("Players, Bots and Spectators"));
00134     q->resize(300, 300);
00135     q->show();
00136 }
00137 
00138 KGGZSeatsDialog::~KGGZSeatsDialog()
00139 {
00140     delete d;
00141 }
00142 
00143 void KGGZSeatsDialog::setMod(KGGZMod::Module *mod)
00144 {
00145     kDebug(11004) << "Set module" << mod;
00146 
00147     d->m_mod = mod;
00148 
00149     if(mod)
00150     {
00151         KGGZMod::InfoRequest ir;
00152         mod->sendRequest(ir);
00153 
00154         connect(mod, SIGNAL(signalEvent(const KGGZMod::Event&)), this, SLOT(slotInfo(const KGGZMod::Event&)));
00155 
00156         d->displaySeats();
00157     }
00158 }
00159 
00160 void KGGZSeatsDialogPrivate::displaySeats()
00161 {
00162     kDebug(11004) << "Display seats";
00163 
00164     QPalette palette;
00165     int count = m_mod->players().count();
00166     int digits = (int)(log((double)count) / log((double)10.0) + 1);
00167 
00168     kDebug(11004) << "Number of players:" << count;
00169 
00170     if(m_root)
00171     {
00172         m_hostnames.clear();
00173         m_realnames.clear();
00174         m_photos.clear();
00175         m_buttons.clear();
00176         m_buttondata.clear();
00177     }
00178     m_root = new QWidget();
00179     m_view->setWidget(m_root);
00180     QVBoxLayout *vboxmain = new QVBoxLayout();
00181     m_root->setLayout(vboxmain);
00182 
00183     for(int i = 0; i < count; i++)
00184     {
00185         KGGZMod::Player *p = m_mod->players().at(i);
00186 
00187         QFrame *w = new QFrame();
00188         w->setFrameStyle(QFrame::Panel | QFrame::Raised);
00189         vboxmain->addWidget(w);
00190 
00191         QLCDNumber *numberframe = new QLCDNumber();
00192         numberframe->setNumDigits(digits);
00193         numberframe->display(i + 1);
00194 
00195         QFrame *photoframe = new QFrame();
00196         palette = photoframe->palette();
00197         palette.setColor(photoframe->backgroundRole(), QColor(120, 120, 120));
00198         photoframe->setPalette(palette);
00199         photoframe->setFrameStyle(QFrame::Panel | QFrame::Sunken);
00200         photoframe->setFixedSize(64, 64);
00201 
00202         QToolButton *actionbutton = new QToolButton();
00203         actionbutton->setArrowType(Qt::DownArrow);
00204         actionbutton->setText(i18n("Action..."));
00205 
00206         QString type = "unknown";
00207         switch(p->type())
00208         {
00209             case KGGZMod::Player::player:
00210                 type = i18n("Player");
00211                 break;
00212             case KGGZMod::Player::open:
00213                 type = i18n("Open");
00214                 break;
00215             case KGGZMod::Player::reserved:
00216                 type = i18n("Reserved");
00217                 break;
00218             case KGGZMod::Player::bot:
00219                 type = i18n("AI bot");
00220                 break;
00221             case KGGZMod::Player::abandoned:
00222                 type = i18n("Abandoned");
00223                 break;
00224             case KGGZMod::Player::unknown:
00225             default:
00226                 break;
00227         }
00228         QLabel *typelabel = new QLabel(i18n("Type: %1", type));
00229 
00230         QString name = p->name();
00231             if(name.isNull()) name = i18n("(unnamed)");
00232         QLabel *namelabel = new QLabel("<b><i>" + name + "</i></b>");
00233         palette = namelabel->palette();
00234         palette.setColor(namelabel->backgroundRole(), QColor(255, 255, 255));
00235         namelabel->setPalette(palette);
00236         namelabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
00237         namelabel->setMinimumWidth(150);
00238 
00239         QLabel *hostlabel = new QLabel(QString());
00240         hostlabel->hide();
00241 
00242         QLabel *realnamelabel = new QLabel(QString());
00243         realnamelabel->hide();
00244 
00245         m_hostnames[i] = hostlabel;
00246         m_realnames[i] = realnamelabel;
00247         m_photos[i] = photoframe;
00248 
00249         QVBoxLayout *box = new QVBoxLayout();
00250         w->setLayout(box);
00251         QHBoxLayout *box2 = new QHBoxLayout();
00252         box2->addSpacing(5);
00253         QVBoxLayout *box5 = new QVBoxLayout();
00254         box5->addWidget(numberframe);
00255         box5->addWidget(actionbutton);
00256         box5->addStretch(1);
00257         box2->addLayout(box5);
00258         box2->addSpacing(5);
00259         QVBoxLayout *box4 = new QVBoxLayout();
00260         box4->addWidget(photoframe);
00261         box4->addStretch(1);
00262         box2->addLayout(box4);
00263         box2->addSpacing(5);
00264         QVBoxLayout *box3 = new QVBoxLayout();
00265         box3->addWidget(namelabel);
00266         box3->addSpacing(5);
00267         box3->addWidget(typelabel);
00268         box3->addSpacing(5);
00269         box3->addWidget(realnamelabel);
00270         box3->addSpacing(5);
00271         box3->addWidget(hostlabel);
00272         box3->addStretch(1);
00273         box2->addLayout(box3);
00274         box2->addStretch(1);
00275         box->addLayout(box2);
00276 
00277         m_buttons[actionbutton] = i;
00278         m_buttondata[actionbutton] = actionbutton;
00279 
00280         QObject::connect(actionbutton, SIGNAL(clicked()), q, SLOT(slotAction()));
00281     }
00282 
00283     vboxmain->addStretch(1);
00284 
00285     m_root->show();
00286     m_root->adjustSize();
00287 
00288     infos();
00289 }
00290 
00291 void KGGZSeatsDialogPrivate::displaySpectators()
00292 {
00293     kDebug(11004) << "Display spectators";
00294 
00295     int count = m_mod->spectators().count();
00296     int digits = (int)(log((double)count) / log((double)10.0) + 1);
00297 
00298     m_root = new QWidget();
00299     m_view->setWidget(m_root);
00300     QVBoxLayout *vboxmain = new QVBoxLayout();
00301     m_root->setLayout(vboxmain);
00302 
00303     for(int i = 0; i < count; i++)
00304     {
00305         KGGZMod::Player *p = m_mod->spectators().at(i);
00306 
00307         QFrame *w = new QFrame();
00308         w->setFrameStyle(QFrame::Panel | QFrame::Raised);
00309         vboxmain->addWidget(w);
00310 
00311         QLCDNumber *numberframe = new QLCDNumber();
00312         numberframe->setNumDigits(digits);
00313         numberframe->display(i + 1);
00314 
00315         QString type = i18n("Spectator");
00316         QLabel *typelabel = new QLabel(i18n("Type: %1", type));
00317 
00318         QString name = p->name();
00319             if(name.isNull()) name = i18n("(unnamed)");
00320         QLabel *namelabel = new QLabel("<b><i>" + name + "</i></b>");
00321         QPalette palette = namelabel->palette();
00322         palette.setColor(namelabel->backgroundRole(), QColor(255, 255, 255));
00323         namelabel->setPalette(palette);
00324         namelabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
00325         namelabel->setMinimumWidth(150);
00326 
00327         QVBoxLayout *box = new QVBoxLayout();
00328         w->setLayout(box);
00329         QHBoxLayout *box2 = new QHBoxLayout();
00330         box2->addSpacing(5);
00331         QVBoxLayout *box5 = new QVBoxLayout();
00332         box5->addWidget(numberframe);
00333         box5->addStretch(1);
00334         box2->addLayout(box5);
00335         box2->addSpacing(5);
00336         QVBoxLayout *box3 = new QVBoxLayout();
00337         box3->addWidget(namelabel);
00338         box3->addSpacing(5);
00339         box3->addWidget(typelabel);
00340         box3->addStretch(1);
00341         box2->addLayout(box3);
00342         box2->addStretch(1);
00343         box->addLayout(box2);
00344     }
00345 
00346     vboxmain->addStretch(1);
00347 
00348     m_root->show();
00349     m_root->adjustSize();
00350 }
00351 
00352 void KGGZSeatsDialogPrivate::slotAction()
00353 {
00354     int seat;
00355        
00356     if(m_buttons.contains(q->sender()))
00357     {
00358         seat = m_buttons[q->sender()];
00359         kDebug(11004) << "seat" << seat << "oldmode" << m_oldmode;
00360 
00361         KGGZMod::Player *p = m_mod->players().at(seat);
00362         KGGZMod::Player *pself = m_mod->self();
00363         m_currentplayer = p;
00364 
00365         QMenu *pop = new QMenu(q);
00366         action_viewstats = pop->addAction(i18n("Statistics..."));
00367         pop->addSeparator();
00368         if(p->type() == KGGZMod::Player::open)
00369         {
00370             if(pself->type() == KGGZMod::Player::spectator)
00371             {
00372                 action_sitdown = pop->addAction(i18n("Sit down here"));
00373             }
00374             action_botadd = pop->addAction(i18n("Add a bot here"));
00375         }
00376         else if(p->type() == KGGZMod::Player::bot)
00377         {
00378             action_botremove = pop->addAction(i18n("Boot bot and open seat"));
00379         }
00380         else if(p->type() == KGGZMod::Player::player)
00381         {
00382             if(pself->type() == KGGZMod::Player::player)
00383             {
00384                 action_standup = pop->addAction(i18n("Stand up"));
00385             }
00386             action_bootplayer = pop->addAction(i18n("Boot player and open seat"));
00387         }
00388         else if(p->type() == KGGZMod::Player::reserved)
00389         {
00390         }
00391         else if(p->type() == KGGZMod::Player::abandoned)
00392         {
00393         }
00394 
00395         QObject::connect(pop, SIGNAL(triggered(QAction*)), q, SLOT(slotMenu(QAction*)));
00396 
00397         const QToolButton *buttonkey = qobject_cast<const QToolButton*>(q->sender());
00398         QToolButton *button = m_buttondata[buttonkey];
00399         //button->setPopup(pop);
00400         //button->openPopup();
00401         pop->popup(button->mapToGlobal(QPoint(0, 0)));
00402     }
00403     else
00404     {
00405         kDebug(11004) << "error";
00406         // error!
00407     }
00408 }
00409 
00410 void KGGZSeatsDialogPrivate::slotMenu(QAction *action)
00411 {
00412     kDebug(11004) << "slotMenu! action=" << action->text();
00413 
00414     if(action == action_standup)
00415     {
00416         KGGZMod::StandRequest req;
00417         m_mod->sendRequest(req);
00418     }
00419     else if(action == action_sitdown)
00420     {
00421         KGGZMod::SitRequest req(m_currentplayer->seat());
00422         m_mod->sendRequest(req);
00423     }
00424     else if(action == action_bootplayer)
00425     {
00426         KGGZMod::BootRequest req(m_currentplayer->name());
00427         m_mod->sendRequest(req);
00428     }
00429     else if(action == action_botadd)
00430     {
00431         KGGZMod::BotRequest req(m_currentplayer->seat());
00432         m_mod->sendRequest(req);
00433     }
00434     else if(action == action_botremove)
00435     {
00436         KGGZMod::OpenRequest req(m_currentplayer->seat());
00437         m_mod->sendRequest(req);
00438     }
00439     else if(action == action_viewstats)
00440     {
00441         // FIXME: how to display stats for all players globally???
00442         KGGZMod::Statistics *s = m_currentplayer->stats();
00443         if(s->hasRecord())
00444         {
00445             kDebug(11004) << "Wins:" << s->wins();
00446         }
00447     }
00448 }
00449 
00450 void KGGZSeatsDialogPrivate::slotInfo(const KGGZMod::Event& event)
00451 {
00452     kDebug(11004) << "Infos arrived";
00453 
00454     if(event.type() == KGGZMod::Event::info)
00455     {
00456         infos();
00457     }
00458     else if(event.type() == KGGZMod::Event::seat)
00459     {
00460         //KGGZMod::SeatEvent se = (KGGZMod::SeatEvent)event;
00461     }
00462 }
00463 
00464 void KGGZSeatsDialogPrivate::infos()
00465 {
00466     int count = m_mod->players().count();
00467     for(int i = 0; i < count; i++)
00468     {
00469         KGGZMod::Player *p = m_mod->players().at(i);
00470 
00471         if(!p->hostname().isEmpty())
00472         {
00473             QString hostname = i18n("Host: %1", p->hostname());
00474             m_hostnames[i]->setText(hostname);
00475             m_hostnames[i]->show();
00476         }
00477         if(!p->realname().isEmpty())
00478         {
00479             QString realname = i18n("Realname: %1", p->realname());
00480             m_realnames[i]->setText(realname);
00481             m_realnames[i]->show();
00482         }
00483         if(!p->photo().isEmpty())
00484         {
00485             KIO::TransferJob *job = KIO::get(p->photo(), KIO::NoReload, KIO::HideProgressInfo);
00486             QObject::connect(job, SIGNAL(data(KIO::Job*, const QByteArray&)),
00487                 q, SLOT(slotTaskData(KIO::Job*, const QByteArray&)));
00488             QObject::connect(job, SIGNAL(result(KJob*)),
00489                 q, SLOT(slotTaskResult(KJob*)));
00490             m_phototasks[job] = i;
00491             m_photodata[job] = QByteArray();
00492         }
00493     }
00494     m_root->adjustSize();
00495 }
00496 
00497 void KGGZSeatsDialogPrivate::slotDisplay(int id)
00498 {
00499     if(id == m_oldmode) return;
00500     m_oldmode = id;
00501 
00502     if(id == displayseats) displaySeats();
00503     else if(id == displayspectators) displaySpectators();
00504 }
00505 
00506 void KGGZSeatsDialogPrivate::slotTaskData(KIO::Job *job, const QByteArray& data)
00507 {
00508     QByteArray data2 = m_photodata[job];
00509     int origsize = data2.size();
00510 
00511     // FIXME: use Qt4 append function?
00512     data2.resize(data2.size() + data.size());
00513     for(int i = 0; i < data.size(); i++)
00514     {
00515         data2[origsize + i] = data[i];
00516     }
00517     m_photodata[job] = data2;
00518 }
00519 
00520 void KGGZSeatsDialogPrivate::slotTaskResult(KJob *job)
00521 {
00522     if(!job->error())
00523     {
00524         int i = m_phototasks[job];
00525         QByteArray data = m_photodata[job];
00526         QPixmap pix(data);
00527         /*QImage im = pix.convertToImage();
00528         QImage im2(64, 64, 32);
00529         im = im.smoothScale(64, 64, QImage::ScaleMin);
00530         im2.fill(QColor(0, 0, 0).rgb());
00531         int x = (64 - im.width()) / 2;
00532         int y = (64 - im.height()) / 2;
00533         bitBlt(&im2, x, y, &im, 0, 0, -1, -1, 0);
00534         pix.convertFromImage(im2);*/
00535         QPixmap pixscaled = pix.scaled(QSize(64, 64), Qt::KeepAspectRatio);
00536         QPalette palette = m_photos[i]->palette();
00537         palette.setBrush(m_photos[i]->backgroundRole(), QBrush(pixscaled));
00538         m_photos[i]->setPalette(palette);
00539     }
00540 
00541     m_photodata.remove(job);
00542     m_phototasks.remove(job);
00543 }
00544 
00545 #include "kggzseatsdialog.moc"

libkdegames/kggzgames

Skip menu "libkdegames/kggzgames"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal