22 #include <QtGui/QAbstractItemView>
23 #include <QtGui/QApplication>
24 #include <QtGui/QCloseEvent>
25 #include <QtGui/QFont>
26 #include <QtGui/QFontMetrics>
27 #include <QtGui/QListWidget>
28 #include <QtGui/QPainter>
29 #include <QtGui/QPushButton>
30 #include <QtGui/QScrollBar>
31 #include <QtGui/QVBoxLayout>
33 #include <KDE/KLocalizedString>
34 #include <KNS3/DownloadDialog>
44 class KgThemeSelector::Private
51 QPushButton* m_knsButton;
57 void _k_updateListSelection(
const KgTheme* theme);
58 void _k_updateProviderSelection();
59 void _k_showNewStuffDialog();
64 , d(new Private(provider, options, this))
66 d->m_list =
new QListWidget(
this);
67 d->m_list->setSelectionMode(QAbstractItemView::SingleSelection);
68 d->m_list->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
73 const QSize itemSizeHint = delegate->
sizeHint(QStyleOptionViewItem(), QModelIndex());
74 const QSize scrollBarSizeHint = d->m_list->verticalScrollBar()->sizeHint();
75 d->m_list->setMinimumSize(itemSizeHint.width() + 2 * scrollBarSizeHint.width(), 4.1 * itemSizeHint.height());
77 connect(d->m_provider, SIGNAL(currentThemeChanged(
const KgTheme*)),
78 SLOT(_k_updateListSelection(
const KgTheme*)));
79 connect(d->m_list, SIGNAL(itemSelectionChanged()),
80 SLOT(_k_updateProviderSelection()));
82 QVBoxLayout* layout =
new QVBoxLayout(
this);
84 layout->addWidget(d->m_list);
88 d->m_knsButton =
new QPushButton(KIcon(
"get-hot-new-stuff"),
89 i18n(
"Get New Themes..."),
this);
90 layout->addWidget(d->m_knsButton);
91 connect(d->m_knsButton, SIGNAL(clicked()), SLOT(_k_showNewStuffDialog()));
100 void KgThemeSelector::Private::fillList()
103 foreach (
const KgTheme* theme, m_provider->themes())
105 QListWidgetItem* item =
new QListWidgetItem(theme->
name(), m_list);
106 item->setData(Qt::DecorationRole,
113 _k_updateListSelection(m_provider->currentTheme());
116 void KgThemeSelector::Private::_k_updateListSelection(
const KgTheme* theme)
118 for (
int idx = 0; idx < m_list->count(); ++idx)
120 QListWidgetItem* item = m_list->item(idx);
124 m_list->setCurrentItem(item, QItemSelectionModel::ClearAndSelect);
129 if (m_list->count() > 0)
131 m_list->setCurrentRow(0, QItemSelectionModel::ClearAndSelect);
135 void KgThemeSelector::Private::_k_updateProviderSelection()
137 const QListWidgetItem* selItem = m_list->selectedItems().value(0);
144 foreach (
const KgTheme* theme, m_provider->themes())
148 m_provider->setCurrentTheme(theme);
153 void KgThemeSelector::Private::_k_showNewStuffDialog()
155 KNS3::DownloadDialog dialog(q);
157 if (!dialog.changedEntries().isEmpty())
159 m_provider->rediscoverThemes();
163 _k_updateListSelection(m_provider->currentTheme());
166 class KgThemeSelector::Dialog :
public KDialog
173 QPushButton* btn = sel->d->m_knsButton;
177 setButtons(Close | User1);
178 setButtonText(User1, btn->text());
180 setButtonIcon(User1, KIcon(
"get-hot-new-stuff"));
181 connect(
this, SIGNAL(user1Clicked()), btn, SIGNAL(clicked()));
188 if (caption.isEmpty())
190 setCaption(i18nc(
"@title:window config dialog",
"Select theme"));
199 virtual void closeEvent(QCloseEvent* event)
207 if (sel->d->m_knsButton)
209 sel->d->m_knsButton->show();
218 new KgThemeSelector::Dialog(
this, caption);
228 QAbstractItemView* view = qobject_cast<QAbstractItemView*>(parent);
230 view->setItemDelegate(
this);
236 thumbnailBaseRect.moveCenter(QPoint(thumbnailBaseRect.center().x(), baseRect.center().y()));
237 if (QApplication::isRightToLeft())
239 return thumbnailBaseRect;
244 const bool rtl = option.direction == Qt::RightToLeft;
245 QRect baseRect = option.rect;
247 QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
250 const QPixmap thumbnail = index.data(Qt::DecorationRole).value<QPixmap>().scaled(
Metrics::ThumbnailBaseSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
251 QRect
thumbnailRect(thumbnailBaseRect.topLeft(), thumbnail.size());
258 QStringList texts; QList<QFont> fonts;
260 QString
name = index.data(Qt::DisplayRole).toString();
262 name = i18n(
"[No name]");
264 QFont theFont(painter->font()); theFont.setBold(
true); fonts << theFont;
267 if (!comment.isEmpty())
270 fonts << painter->font();
273 QString author = index.data(
AuthorRole).toString();
274 if (!author.isEmpty())
276 const QString authorString = ki18nc(
"Author attribution, e.g. \"by Jack\"",
"by %1").subs(author).toString();
277 texts << authorString;
278 QFont theFont(painter->font()); theFont.setItalic(
true); fonts << theFont;
282 QList<QRect> textRects;
int totalTextHeight = 0;
283 for (
int i = 0; i < texts.count(); ++i)
285 QFontMetrics fm(fonts[i]);
286 textRects << fm.boundingRect(texts[i]);
287 textRects[i].setHeight(qMax(textRects[i].height(), fm.lineSpacing()));
288 totalTextHeight += textRects[i].height();
290 QRect textBaseRect(baseRect);
301 textBaseRect.setHeight(totalTextHeight);
302 textBaseRect.moveTop(baseRect.top() + (baseRect.height() - textBaseRect.height()) / 2);
304 QRect currentTextRect(textBaseRect);
306 for (
int i = 0; i < texts.count(); ++i)
308 painter->setFont(fonts[i]);
309 const QRect& textRect = textRects[i];
310 currentTextRect.setHeight(textRect.height());
311 const QFontMetrics fm(fonts[i]);
312 const QString text = fm.elidedText(texts[i], Qt::ElideRight, currentTextRect.width());
313 painter->drawText(currentTextRect, Qt::AlignLeft | Qt::AlignVCenter, text);
314 currentTextRect.moveTop(currentTextRect.bottom());
321 Q_UNUSED(option) Q_UNUSED(index)
328 #include "kgthemeselector.moc"
Enable downloading of additional themes with KNewStuff3.
KgThemeDelegate(QObject *parent=0)
QRect thumbnailRect(const QRect &baseRect) const
const QSize ThumbnailBaseSize(64, 64)
A theme provider manages KgTheme instances, and maintains a selection of the currentTheme().
KDEGAMES_EXPORT const char * name(StandardGameAction id)
This will return the internal name of a given standard action.
KgThemeSelector(KgThemeProvider *provider, Options options=DefaultBehavior, QWidget *parent=0)
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual ~KgThemeSelector()
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
A theme describes the visual appearance of a game.
void showAsDialog(const QString &caption=QString())
Create and show a non-modal dialog which displays this selector.