• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

granatier

  • sources
  • kde-4.14
  • kdegames
  • granatier
  • src
  • config
playerselector.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright 2012 Mathias Kraus <k.hias@gmx.de> *
3  * Copyright 2009-2012 Stefan Majewsky <majewsky@gmx.net> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU Library General Public License *
7  * version 2 as published by the Free Software Foundation *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU Library General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Library General Public *
15  * License along with this program; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18  ***************************************************************************/
19 
20 #include "playerselector.h"
21 #include "playerselectoritem.h"
22 #include "playersettings.h"
23 
24 #include <QtCore/QPointer>
25 #include <QtGui/QAbstractItemView>
26 #include <QtGui/QApplication>
27 #include <QtGui/QCloseEvent>
28 #include <QtGui/QFont>
29 #include <QtGui/QFontMetrics>
30 #include <QtGui/QListWidget>
31 #include <QtGui/QLineEdit>
32 #include <QtGui/QPainter>
33 #include <QtGui/QPushButton>
34 #include <QtGui/QScrollBar>
35 #include <QtGui/QVBoxLayout>
36 #include <QtGui/QSpacerItem>
37 #include <QtSvg/QSvgRenderer>
38 #include <KDE/KStandardDirs>
39 #include <KDE/KConfig>
40 #include <KDE/KLocalizedString>
41 #include <KNS3/DownloadDialog>
42 
43 //PlayerSelectorDelegate declaration
44 #include <QtGui/QStyledItemDelegate>
45 class PlayerSelectorDelegate : public QStyledItemDelegate
46 {
47 public:
48  PlayerSelectorDelegate(QObject* parent = 0);
49  virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
51  virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
52 };
53 
54 //BEGIN PlayerSelector
55 
56 struct PlayerSelector::Private
57 {
58  PlayerSelector* q;
59  PlayerSettings* m_playerSettings;
60  Options m_options;
61  QListWidget* m_list;
62  QPushButton* m_knsButton;
63 
64  void fillList();
65 
66  Private(PlayerSettings* playerSettings, Options options, PlayerSelector* q) : q(q), m_playerSettings(playerSettings), m_options(options), m_knsButton(0) {}
67 
68  void _k_showNewStuffDialog();
69 };
70 
71 PlayerSelector::PlayerSelector(PlayerSettings* playerSettings, Options options, QWidget* parent)
72  : QWidget(parent)
73  , d(new Private(playerSettings, options, this))
74 {
75  d->m_list = new QListWidget(this);
76  d->m_list->setSelectionMode(QAbstractItemView::NoSelection);
77  d->m_list->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
78  //load themes from provider
79  d->fillList();
80  //setup appearance of the theme list (min. size = 4 items)
81  PlayerSelectorDelegate* delegate = new PlayerSelectorDelegate(d->m_list);
82  const QSize itemSizeHint = delegate->sizeHint(QStyleOptionViewItem(), QModelIndex());
83  const QSize scrollBarSizeHint = d->m_list->verticalScrollBar()->sizeHint();
84  d->m_list->setMinimumSize(itemSizeHint.width() + 2 * scrollBarSizeHint.width(), 3.3 * itemSizeHint.height());
85  //setup main layout
86  QVBoxLayout* layout = new QVBoxLayout(this);
87  layout->setMargin(0);
88  layout->addWidget(d->m_list);
89  //setup KNS button
90  if (options & EnableNewStuffDownload)
91  {
92  d->m_knsButton = new QPushButton(KIcon("get-hot-new-stuff"),
93  i18n("Get New Players..."), this);
94  layout->addWidget(d->m_knsButton);
95  connect(d->m_knsButton, SIGNAL(clicked()), SLOT(_k_showNewStuffDialog()));
96  }
97 }
98 
99 PlayerSelector::~PlayerSelector()
100 {
101  delete d;
102 }
103 
104 void PlayerSelector::Private::fillList()
105 {
106  m_list->clear();
107 
108  QModelIndex modelIndex;
109  PlayerSelectorItem* playerSelectorItem;
110 
111  QSvgRenderer renderer;
112  QPixmap pixmap(QSize(64, 64));
113  pixmap.fill(QColor(0, 0, 0, 0));
114  QPainter pixPainter(&pixmap);
115 
116  QStringList playerIDs = m_playerSettings->playerIDs();
117 
118  for(int i = 0; i < playerIDs.count(); i++)
119  {
120  QListWidgetItem* item = new QListWidgetItem(playerIDs[i], m_list);
121 
122  item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
123 
124  playerSelectorItem = new PlayerSelectorItem(playerIDs[i], m_playerSettings, m_list);
125 
126  renderer.load(KStandardDirs::locate("appdata", QString("players/%1").arg(m_playerSettings->playerGraphicsFile(playerIDs[i]))));
127  renderer.render(&pixPainter, "player_0");
128  playerSelectorItem->setPlayerPreviewPixmap(pixmap);
129 
130  KConfig desktopFile(KStandardDirs::locate("appdata", "players/" + playerIDs[i]), KConfig::SimpleConfig);
131  QString author = desktopFile.group("KGameTheme").readEntry<QString>("Author", "");
132  QString authorEmail = QString("<a href=\"mailto:%1\">%1</a>").arg(desktopFile.group("KGameTheme").readEntry<QString>("AuthorEmail", ""));
133  //TODO: QString description = desktopFile.group("KGameTheme").readEntry<QString>("Description", "");
134  playerSelectorItem->setPlayerAuthor(author, authorEmail);
135 
136  modelIndex = m_list->model()->index(i, 0, m_list->rootIndex());
137  m_list->setIndexWidget(modelIndex, playerSelectorItem);
138  }
139 }
140 
141 void PlayerSelector::Private::_k_showNewStuffDialog()
142 {
143  QPointer<KNS3::DownloadDialog> dialog = new KNS3::DownloadDialog (q);
144  if(dialog->exec() == QDialog::Accepted)
145  {
146  if(!(dialog->changedEntries().isEmpty()))
147  {
148  //TODO: discover new arenas and add them to the list
149  fillList();
150  }
151  }
152  delete dialog;
153 }
154 
155 class PlayerSelector::Dialog : public KDialog
156 {
157  public:
158  Dialog(PlayerSelector* sel, const QString& caption)
159  {
160  setMainWidget(sel);
161  //replace
162  QPushButton* btn = sel->d->m_knsButton;
163  if (btn)
164  {
165  btn->hide();
166  setButtons(Close | User1);
167  setButtonText(User1, btn->text());
168  //cannot use btn->icon() because setButtonIcon() wants KIcon
169  setButtonIcon(User1, KIcon("get-hot-new-stuff"));
170  connect(this, SIGNAL(user1Clicked()), btn, SIGNAL(clicked()));
171  }
172  else
173  {
174  setButtons(Close);
175  }
176  //window caption
177  if (caption.isEmpty())
178  {
179  setCaption(i18nc("@title:window config dialog", "Select player"));
180  }
181  else
182  {
183  setCaption(caption);
184  }
185  show();
186  }
187  protected:
188  virtual void closeEvent(QCloseEvent* event)
189  {
190  event->accept();
191  PlayerSelector* sel = qobject_cast<PlayerSelector*>(mainWidget());
192  //delete myself, but *not* the PlayerSelector
193  sel->setParent(0);
194  deleteLater();
195  //restore the KNS button
196  if (sel->d->m_knsButton)
197  {
198  sel->d->m_knsButton->show();
199  }
200  }
201 };
202 
203 void PlayerSelector::showAsDialog(const QString& caption)
204 {
205  if (!isVisible())
206  {
207  new PlayerSelector::Dialog(this, caption);
208  }
209 }
210 
211 //END PlayerSelector
212 //BEGIN PlayerSelectorDelegate
213 
214 PlayerSelectorDelegate::PlayerSelectorDelegate(QObject* parent)
215  : QStyledItemDelegate(parent)
216 {
217  QAbstractItemView* view = qobject_cast<QAbstractItemView*>(parent);
218  if (view)
219  view->setItemDelegate(this);
220 }
221 
222 void PlayerSelectorDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& /*index*/) const
223 {
224  //draw background
225  QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
226 }
227 
228 QSize PlayerSelectorDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
229 {
230  Q_UNUSED(option) Q_UNUSED(index)
231  //TODO: take text size into account
232  return QSize(600, 64 /*player preview height*/ + 2 * 6 /*padding*/ + 40 /* some space for the player name */);
233 }
234 
235 //END PlayerSelectorDelegate
236 
237 #include "playerselector.moc"
QWidget::layout
QLayout * layout() const
QModelIndex
QWidget
QAbstractItemModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
QSize::width
int width() const
QAbstractItemView
PlayerSelector::EnableNewStuffDownload
Enable downloading of additional themes with KNewStuff3.
Definition: playerselector.h:38
QSvgRenderer::render
void render(QPainter *painter)
PlayerSelector::PlayerSelector
PlayerSelector(PlayerSettings *playerSettings, Options options=DefaultBehavior, QWidget *parent=0)
Definition: playerselector.cpp:71
PlayerSelectorItem::setPlayerAuthor
void setPlayerAuthor(const QString &name, const QString &mail)
Definition: playerselectoritem.cpp:159
QSvgRenderer
QPointer
QListWidgetItem
QWidget::isVisible
bool isVisible() const
PlayerSelectorItem::setPlayerPreviewPixmap
void setPlayerPreviewPixmap(const QPixmap &pixmap)
Definition: playerselectoritem.cpp:149
QListWidget
QListWidgetItem::flags
Qt::ItemFlags flags() const
QWidget::setParent
void setParent(QWidget *parent)
PlayerSelectorItem
Definition: playerselectoritem.h:29
QCloseEvent
PlayerSelector
Definition: playerselector.h:28
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QList::count
int count(const T &value) const
QSvgRenderer::load
bool load(const QString &filename)
QStyleOptionViewItem
QObject
QStyledItemDelegate::paint
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
QPainter
QListWidgetItem::setFlags
void setFlags(QFlags< Qt::ItemFlag > flags)
QString::isEmpty
bool isEmpty() const
QAbstractItemView::setItemDelegate
void setItemDelegate(QAbstractItemDelegate *delegate)
QStyledItemDelegate::sizeHint
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
QVBoxLayout
PlayerSettings
Definition: playersettings.h:27
QObject::deleteLater
void deleteLater()
PlayerSelector::~PlayerSelector
virtual ~PlayerSelector()
Definition: playerselector.cpp:99
QString
QWidget::hide
void hide()
QColor
QLayout::setMargin
void setMargin(int margin)
QStringList
QPixmap
playersettings.h
QSize
QModelIndex::model
const QAbstractItemModel * model() const
QApplication::style
QStyle * style()
QWidget::setCaption
void setCaption(const QString &c)
QSize::height
int height() const
QStyle::drawPrimitive
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const =0
QAbstractButton::text
text
QPushButton
QWidget::show
void show()
QWidget::closeEvent
virtual void closeEvent(QCloseEvent *event)
PlayerSelector::showAsDialog
void showAsDialog(const QString &caption=QString())
Create and show a non-modal dialog which displays this selector.
Definition: playerselector.cpp:203
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
playerselector.h
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QWidget::event
virtual bool event(QEvent *event)
playerselectoritem.h
QWidget::caption
QString caption() const
QStyledItemDelegate
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

granatier

Skip menu "granatier"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal