• Skip to content
  • Skip to link menu
KDE 4.0 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<KIO::Job*, int> m_phototasks;
00059         QMap<KIO::Job*, 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(KIO::Job *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 }
00095 
00096 KGGZSeatsDialogPrivate::KGGZSeatsDialogPrivate(KGGZSeatsDialog* qq)
00097 : q(qq)
00098 {
00099     m_root = NULL;
00100     m_mod = NULL;
00101     m_oldmode = displayseats;
00102 
00103     m_view = new QScrollArea();
00104     //m_view->setResizePolicy(QScrollArea::AutoOneFit);
00105 
00106     KSeparator *sep1 = new KSeparator();
00107     KSeparator *sep2 = new KSeparator();
00108 
00109     QLabel *label = new QLabel(i18n("Display:"));
00110 
00111     KComboBox *combo = new KComboBox();
00112     combo->addItem(i18n("Involved players and bots"), displayseats);
00113     combo->addItem(i18n("Spectators"), displayspectators);
00114 
00115     QPushButton *ok = new QPushButton(i18n("Close"));
00116 
00117     QVBoxLayout *box = new QVBoxLayout();
00118     box->addWidget(m_view);
00119     box->addWidget(sep1);
00120     QHBoxLayout *box2 = new QHBoxLayout();
00121     box2->addWidget(label);
00122     box2->addWidget(combo);
00123     box2->addStretch(1);
00124     box->addLayout(box2);
00125     box->addWidget(sep2);
00126     box->addWidget(ok);
00127     q->setLayout(box);
00128 
00129     QObject::connect(combo, SIGNAL(activated(int)), q, SLOT(slotDisplay(int)));
00130     QObject::connect(ok, SIGNAL(clicked()), q, SLOT(close()));
00131 
00132     q->setWindowTitle(i18n("Players, Bots and Spectators"));
00133     q->resize(300, 300);
00134     q->show();
00135 
00136     q->setMod(KGGZMod::Module::instance());
00137 }
00138 
00139 KGGZSeatsDialog::~KGGZSeatsDialog()
00140 {
00141     delete d;
00142 }
00143 
00144 void KGGZSeatsDialog::setMod(KGGZMod::Module *mod)
00145 {
00146     d->m_mod = mod;
00147 
00148     if(mod)
00149     {
00150         KGGZMod::InfoRequest ir;
00151         mod->sendRequest(ir);
00152 
00153         connect(mod, SIGNAL(signalEvent(const KGGZMod::Event&)), this, SLOT(slotInfo(const KGGZMod::Event&)));
00154 
00155         d->displaySeats();
00156     }
00157 }
00158 
00159 void KGGZSeatsDialogPrivate::displaySeats()
00160 {
00161     QPalette palette;
00162     int count = m_mod->players().count();
00163     int digits = (int)(log(count) / log(10) + 1);
00164 
00165     if(m_root)
00166     {
00167         // FIXME: m_root doesn't need to be member anymore? (Qt4)
00168         (void)m_view->takeWidget();
00169         delete m_root;
00170 
00171         m_hostnames.clear();
00172         m_realnames.clear();
00173         m_photos.clear();
00174         m_buttons.clear();
00175         m_buttondata.clear();
00176     }
00177     m_root = new QWidget(m_view->viewport());
00178     m_view->setWidget(m_root);
00179     QVBoxLayout *vboxmain = new QVBoxLayout(m_root);
00180 
00181     for(int i = 0; i < count; i++)
00182     {
00183         KGGZMod::Player *p = m_mod->players().at(i);
00184 
00185         QFrame *w = new QFrame(m_root);
00186         w->setFrameStyle(QFrame::Panel | QFrame::Raised);
00187         vboxmain->addWidget(w);
00188 
00189         QLCDNumber *numberframe = new QLCDNumber(w);
00190         numberframe->setNumDigits(digits);
00191         numberframe->display(i + 1);
00192 
00193         QFrame *photoframe = new QFrame(w);
00194         palette = photoframe->palette();
00195         palette.setColor(photoframe->backgroundRole(), QColor(120, 120, 120));
00196         photoframe->setPalette(palette);
00197         photoframe->setFrameStyle(QFrame::Panel | QFrame::Sunken);
00198         photoframe->setFixedSize(64, 64);
00199 
00200         QToolButton *actionbutton = new QToolButton(w);
00201         actionbutton->setArrowType(Qt::DownArrow);
00202         actionbutton->setText(i18n("Action..."));
00203 
00204         QString type = "unknown";
00205         switch(p->type())
00206         {
00207             case KGGZMod::Player::player:
00208                 type = i18n("Player");
00209                 break;
00210             case KGGZMod::Player::open:
00211                 type = i18n("Open");
00212                 break;
00213             case KGGZMod::Player::reserved:
00214                 type = i18n("Reserved");
00215                 break;
00216             case KGGZMod::Player::bot:
00217                 type = i18n("AI bot");
00218                 break;
00219             case KGGZMod::Player::abandoned:
00220                 type = i18n("Abandoned");
00221                 break;
00222             case KGGZMod::Player::unknown:
00223             default:
00224                 break;
00225         }
00226         QLabel *typelabel = new QLabel(i18n("Type: %1", type), w);
00227 
00228         QString name = p->name();
00229             if(name.isNull()) name = i18n("(unnamed)");
00230         QLabel *namelabel = new QLabel("<b><i>" + name + "</i></b>", w);
00231         palette = namelabel->palette();
00232         palette.setColor(namelabel->backgroundRole(), QColor(255, 255, 255));
00233         namelabel->setPalette(palette);
00234         namelabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
00235         namelabel->setMinimumWidth(150);
00236 
00237         QLabel *hostlabel = new QLabel("", w);
00238         hostlabel->hide();
00239 
00240         QLabel *realnamelabel = new QLabel("", w);
00241         realnamelabel->hide();
00242 
00243         m_hostnames[i] = hostlabel;
00244         m_realnames[i] = realnamelabel;
00245         m_photos[i] = photoframe;
00246 
00247         QVBoxLayout *box = new QVBoxLayout(w);
00248         QHBoxLayout *box2 = new QHBoxLayout();
00249         box2->addSpacing(5);
00250         QVBoxLayout *box5 = new QVBoxLayout();
00251         box5->addWidget(numberframe);
00252         box5->addWidget(actionbutton);
00253         box5->addStretch(1);
00254         box2->addLayout(box5);
00255         box2->addSpacing(5);
00256         QVBoxLayout *box4 = new QVBoxLayout();
00257         box4->addWidget(photoframe);
00258         box4->addStretch(1);
00259         box2->addLayout(box4);
00260         box2->addSpacing(5);
00261         QVBoxLayout *box3 = new QVBoxLayout();
00262         box3->addWidget(namelabel);
00263         box3->addSpacing(5);
00264         box3->addWidget(typelabel);
00265         box3->addSpacing(5);
00266         box3->addWidget(realnamelabel);
00267         box3->addSpacing(5);
00268         box3->addWidget(hostlabel);
00269         box3->addStretch(1);
00270         box2->addLayout(box3);
00271         box2->addStretch(1);
00272         box->addLayout(box2);
00273 
00274         m_buttons[actionbutton] = i;
00275         m_buttondata[actionbutton] = actionbutton;
00276 
00277         QObject::connect(actionbutton, SIGNAL(clicked()), q, SLOT(slotAction()));
00278     }
00279 
00280     vboxmain->addStretch(1);
00281 
00282     m_root->show();
00283 
00284     infos();
00285 }
00286 
00287 void KGGZSeatsDialogPrivate::displaySpectators()
00288 {
00289     int count = m_mod->spectators().count();
00290     int digits = (int)(log(count) / log(10) + 1);
00291 
00292     if(m_root)
00293     {
00294         (void)m_view->takeWidget();
00295         delete m_root;
00296     }
00297     m_root = new QWidget(m_view->viewport());
00298     m_view->setWidget(m_root);
00299     QVBoxLayout *vboxmain = new QVBoxLayout(m_root);
00300 
00301     for(int i = 0; i < count; i++)
00302     {
00303         KGGZMod::Player *p = m_mod->spectators().at(i);
00304 
00305         QFrame *w = new QFrame(m_root);
00306         w->setFrameStyle(QFrame::Panel | QFrame::Raised);
00307         vboxmain->addWidget(w);
00308 
00309         QLCDNumber *numberframe = new QLCDNumber(w);
00310         numberframe->setNumDigits(digits);
00311         numberframe->display(i + 1);
00312 
00313         QString type = i18n("Spectator");
00314         QLabel *typelabel = new QLabel(i18n("Type: %1", type), w);
00315 
00316         QString name = p->name();
00317             if(name.isNull()) name = i18n("(unnamed)");
00318         QLabel *namelabel = new QLabel("<b><i>" + name + "</i></b>", w);
00319         QPalette palette = namelabel->palette();
00320         palette.setColor(namelabel->backgroundRole(), QColor(255, 255, 255));
00321         namelabel->setPalette(palette);
00322         namelabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
00323         namelabel->setMinimumWidth(150);
00324 
00325         QVBoxLayout *box = new QVBoxLayout(w);
00326         QHBoxLayout *box2 = new QHBoxLayout();
00327         box2->addSpacing(5);
00328         QVBoxLayout *box5 = new QVBoxLayout();
00329         box5->addWidget(numberframe);
00330         box5->addStretch(1);
00331         box2->addLayout(box5);
00332         box2->addSpacing(5);
00333         QVBoxLayout *box3 = new QVBoxLayout();
00334         box3->addWidget(namelabel);
00335         box3->addSpacing(5);
00336         box3->addWidget(typelabel);
00337         box3->addStretch(1);
00338         box2->addLayout(box3);
00339         box2->addStretch(1);
00340         box->addLayout(box2);
00341     }
00342 
00343     vboxmain->addStretch(1);
00344 
00345     m_root->show();
00346 }
00347 
00348 void KGGZSeatsDialogPrivate::slotAction()
00349 {
00350     int seat;
00351        
00352     if(m_buttons.contains(q->sender()))
00353     {
00354         seat = m_buttons[q->sender()];
00355         kDebug(11004) << "seat" << seat << "oldmode" << m_oldmode;
00356 
00357         KGGZMod::Player *p = m_mod->players().at(seat);
00358         KGGZMod::Player *pself = m_mod->self();
00359         m_currentplayer = p;
00360 
00361         QMenu *pop = new QMenu(q);
00362         action_viewstats = pop->addAction(i18n("Statistics..."));
00363         pop->addSeparator();
00364         if(p->type() == KGGZMod::Player::open)
00365         {
00366             if(pself->type() == KGGZMod::Player::spectator)
00367             {
00368                 action_sitdown = pop->addAction(i18n("Sit down here"));
00369             }
00370             action_botadd = pop->addAction(i18n("Add a bot here"));
00371         }
00372         else if(p->type() == KGGZMod::Player::bot)
00373         {
00374             action_botremove = pop->addAction(i18n("Boot bot and open seat"));
00375         }
00376         else if(p->type() == KGGZMod::Player::player)
00377         {
00378             if(pself->type() == KGGZMod::Player::player)
00379             {
00380                 action_standup = pop->addAction(i18n("Stand up"));
00381             }
00382             action_bootplayer = pop->addAction(i18n("Boot player and open seat"));
00383         }
00384         else if(p->type() == KGGZMod::Player::reserved)
00385         {
00386         }
00387         else if(p->type() == KGGZMod::Player::abandoned)
00388         {
00389         }
00390 
00391         QObject::connect(pop, SIGNAL(triggered(QAction*)), q, SLOT(slotMenu(QAction*)));
00392 
00393         const QToolButton *buttonkey = qobject_cast<const QToolButton*>(q->sender());
00394         QToolButton *button = m_buttondata[buttonkey];
00395         //button->setPopup(pop);
00396         //button->openPopup();
00397         pop->popup(button->mapToGlobal(QPoint(0, 0)));
00398     }
00399     else
00400     {
00401         kDebug(11004) << "error";
00402         // error!
00403     }
00404 }
00405 
00406 void KGGZSeatsDialogPrivate::slotMenu(QAction *action)
00407 {
00408     kDebug(11004) << "slotMenu! action=" << action->text();
00409 
00410     if(action == action_standup)
00411     {
00412         KGGZMod::StandRequest req;
00413         m_mod->sendRequest(req);
00414     }
00415     else if(action == action_sitdown)
00416     {
00417         KGGZMod::SitRequest req(m_currentplayer->seat());
00418         m_mod->sendRequest(req);
00419     }
00420     else if(action == action_bootplayer)
00421     {
00422         KGGZMod::BootRequest req(m_currentplayer->name());
00423         m_mod->sendRequest(req);
00424     }
00425     else if(action == action_botadd)
00426     {
00427         KGGZMod::BotRequest req(m_currentplayer->seat());
00428         m_mod->sendRequest(req);
00429     }
00430     else if(action == action_botremove)
00431     {
00432         KGGZMod::OpenRequest req(m_currentplayer->seat());
00433         m_mod->sendRequest(req);
00434     }
00435     else if(action == action_viewstats)
00436     {
00437         // FIXME: how to display stats for all players globally???
00438         KGGZMod::Statistics *s = m_currentplayer->stats();
00439         if(s->hasRecord())
00440         {
00441             kDebug(11004) << "Wins:" << s->wins();
00442         }
00443     }
00444 }
00445 
00446 void KGGZSeatsDialogPrivate::slotInfo(const KGGZMod::Event& event)
00447 {
00448     if(event.type() == KGGZMod::Event::info)
00449     {
00450         infos();
00451     }
00452     else if(event.type() == KGGZMod::Event::seat)
00453     {
00454             //KGGZMod::SeatEvent se = (KGGZMod::SeatEvent)event;
00455     }
00456 }
00457 
00458 void KGGZSeatsDialogPrivate::infos()
00459 {
00460     int count = m_mod->players().count();
00461     for(int i = 0; i < count; i++)
00462     {
00463         KGGZMod::Player *p = m_mod->players().at(i);
00464         // FIXME: condition if info is really there? not really needed
00465         if(/*info*/1==1)
00466         {
00467             if(!p->hostname().isEmpty())
00468             {
00469                 QString hostname = i18n("Host: %1", p->hostname());
00470                 m_hostnames[i]->setText(hostname);
00471                 m_hostnames[i]->show();
00472             }
00473             if(!p->realname().isEmpty())
00474             {
00475                 QString realname = i18n("Realname: %1", p->realname());
00476                 m_realnames[i]->setText(realname);
00477                 m_realnames[i]->show();
00478             }
00479             if(!p->photo().isEmpty())
00480             {
00481                 KIO::TransferJob *job = KIO::get(p->photo(), KIO::NoReload, KIO::HideProgressInfo);
00482                 QObject::connect(job, SIGNAL(data(KIO::Job*, const QByteArray&)),
00483                     q, SLOT(slotTaskData(KIO::Job*, const QByteArray&)));
00484                 QObject::connect(job, SIGNAL(result(KIO::Job*)),
00485                     q, SLOT(slotTaskResult(KIO::Job*)));
00486                 m_phototasks[job] = i;
00487                 m_photodata[job] = QByteArray();
00488             }
00489         }
00490     }
00491 }
00492 
00493 void KGGZSeatsDialogPrivate::slotDisplay(int id)
00494 {
00495     if(id == m_oldmode) return;
00496     m_oldmode = id;
00497 
00498     if(id == displayseats) displaySeats();
00499     else if(id == displayspectators) displaySpectators();
00500 }
00501 
00502 void KGGZSeatsDialogPrivate::slotTaskData(KIO::Job *job, const QByteArray& data)
00503 {
00504     QByteArray data2 = m_photodata[job];
00505     int origsize = data2.size();
00506 
00507     // FIXME: use Qt4 append function?
00508     data2.resize(data2.size() + data.size());
00509     for(int i = 0; i < data.size(); i++)
00510     {
00511         data2[origsize + i] = data[i];
00512     }
00513     m_photodata[job] = data2;
00514 }
00515 
00516 void KGGZSeatsDialogPrivate::slotTaskResult(KIO::Job *job)
00517 {
00518     if(!job->error())
00519     {
00520         int i = m_phototasks[job];
00521         QByteArray data = m_photodata[job];
00522         QPixmap pix(data);
00523         /*QImage im = pix.convertToImage();
00524         QImage im2(64, 64, 32);
00525         im = im.smoothScale(64, 64, QImage::ScaleMin);
00526         im2.fill(QColor(0, 0, 0).rgb());
00527         int x = (64 - im.width()) / 2;
00528         int y = (64 - im.height()) / 2;
00529         bitBlt(&im2, x, y, &im, 0, 0, -1, -1, 0);
00530         pix.convertFromImage(im2);*/
00531         QPixmap pixscaled = pix.scaled(QSize(64, 64), Qt::KeepAspectRatio);
00532         QPalette palette = m_photos[i]->palette();
00533         palette.setBrush(m_photos[i]->backgroundRole(), QBrush(pixscaled));
00534         m_photos[i]->setPalette(palette);
00535     }
00536 
00537     m_photodata.remove(job);
00538     m_phototasks.remove(job);
00539 }
00540 
00541 #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