1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/***************************************************************************
* Copyright 2009-2012 Stefan Majewsky <majewsky@gmx.net> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License *
* version 2 as published by the Free Software Foundation *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "kgthemeselector.h"
#include "kgthemeselector_p.h"
#include <QCloseEvent>
#include <QFont>
#include <QFontMetrics>
#include <QPainter>
#include <QAbstractItemView>
#include <QApplication>
#include <QListWidget>
#include <QPushButton>
#include <QScrollBar>
#include <QVBoxLayout>
#include <QDialogButtonBox>
#include <QDialog>
#include <QIcon>
#include <KLocalizedString>
#include <KNS3/DownloadDialog>
namespace Metrics
{
const int Padding = 6;
const QSize ThumbnailBaseSize(64, 64);
}
//BEGIN KgThemeSelector
class KgThemeSelector::Private
{
public:
KgThemeSelector* q;
KgThemeProvider* m_provider;
Options m_options;
QListWidget* m_list;
QPushButton* m_knsButton;
void fillList();
Private(KgThemeProvider* provider, Options options, KgThemeSelector* q) : q(q), m_provider(provider), m_options(options), m_knsButton(nullptr) {}
void _k_updateListSelection(const KgTheme* theme);
void _k_updateProviderSelection();
void _k_showNewStuffDialog();
};
KgThemeSelector::KgThemeSelector(KgThemeProvider* provider, Options options, QWidget* parent)
: QWidget(parent)
, d(new Private(provider, options, this))
{
d->m_list = new QListWidget(this);
d->m_list->setSelectionMode(QAbstractItemView::SingleSelection);
d->m_list->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
//load themes from provider
d->fillList();
//setup appearance of the theme list (min. size = 4 items)
KgThemeDelegate* delegate = new KgThemeDelegate(d->m_list);
const QSize itemSizeHint = delegate->sizeHint(QStyleOptionViewItem(), QModelIndex());
const QSize scrollBarSizeHint = d->m_list->verticalScrollBar()->sizeHint();
d->m_list->setMinimumSize(itemSizeHint.width() + 2 * scrollBarSizeHint.width(), 4.1 * itemSizeHint.height());
//monitor change selection in both directions
connect(d->m_provider, SIGNAL(currentThemeChanged(const KgTheme*)),
SLOT(_k_updateListSelection(const KgTheme*)));
connect(d->m_list, SIGNAL(itemSelectionChanged()),
SLOT(_k_updateProviderSelection()));
//setup main layout
QVBoxLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(d->m_list);
//setup KNS button
if (options & EnableNewStuffDownload)
{
d->m_knsButton = new QPushButton(QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")),
i18n("Get New Themes..."), this);
layout->addWidget(d->m_knsButton);
connect(d->m_knsButton, SIGNAL(clicked()), SLOT(_k_showNewStuffDialog()));
}
}
KgThemeSelector::~KgThemeSelector()
{
delete d;
}
void KgThemeSelector::Private::fillList()
{
m_list->clear();
const auto themes = m_provider->themes();
for (const KgTheme* theme : themes) {
QListWidgetItem* item = new QListWidgetItem(theme->name(), m_list);
item->setData(Qt::DecorationRole,
m_provider->generatePreview(theme, Metrics::ThumbnailBaseSize));
item->setData(KgThemeDelegate::DescriptionRole, theme->description());
item->setData(KgThemeDelegate::AuthorRole, theme->author());
item->setData(KgThemeDelegate::AuthorEmailRole, theme->authorEmail());
item->setData(KgThemeDelegate::IdRole, theme->identifier());
}
_k_updateListSelection(m_provider->currentTheme());
}
void KgThemeSelector::Private::_k_updateListSelection(const KgTheme* theme)
{
for (int idx = 0; idx < m_list->count(); ++idx)
{
QListWidgetItem* item = m_list->item(idx);
const QByteArray thisId = item->data(KgThemeDelegate::IdRole).toByteArray();
if (thisId == theme->identifier())
{
m_list->setCurrentItem(item, QItemSelectionModel::ClearAndSelect);
return;
}
}
//make sure that something is selected
if (m_list->count() > 0)
{
m_list->setCurrentRow(0, QItemSelectionModel::ClearAndSelect);
}
}
void KgThemeSelector::Private::_k_updateProviderSelection()
{
const QListWidgetItem* selItem = m_list->selectedItems().value(0);
if (!selItem)
{
return;
}
const QByteArray selId = selItem->data(KgThemeDelegate::IdRole).toByteArray();
//select the theme with this identifier
const auto themes = m_provider->themes();
for (const KgTheme* theme : themes) {
if (theme->identifier() == selId)
{
m_provider->setCurrentTheme(theme);
}
}
}
void KgThemeSelector::Private::_k_showNewStuffDialog()
{
QPointer<KNS3::DownloadDialog> dialog(new KNS3::DownloadDialog(q));
dialog->exec();
if (dialog && !dialog->changedEntries().isEmpty())
{
m_provider->rediscoverThemes();
fillList();
}
//restore previous selection
_k_updateListSelection(m_provider->currentTheme());
delete dialog;
}
class KgThemeSelector::Dialog : public QDialog
{
public:
Dialog(KgThemeSelector* sel, const QString& caption)
: mSelector(sel)
{
QVBoxLayout *mainLayout = new QVBoxLayout;
setLayout(mainLayout);
mainLayout->addWidget(sel);
//replace
QPushButton* btn = sel->d->m_knsButton;
QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
if (btn)
{
btn->hide();
QPushButton *stuff = new QPushButton(QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")), btn->text());
buttonBox->addButton(stuff, QDialogButtonBox::ActionRole);
buttonBox->addButton(QDialogButtonBox::Close);
connect(stuff, &QAbstractButton::clicked, btn, &QAbstractButton::clicked);
connect(buttonBox, &QDialogButtonBox::rejected, this, &KgThemeSelector::Dialog::reject);
}
else
{
buttonBox->setStandardButtons(QDialogButtonBox::Close);
connect(buttonBox, &QDialogButtonBox::rejected, this, &KgThemeSelector::Dialog::reject);
}
//window caption
if (caption.isEmpty())
{
setWindowTitle(i18nc("@title:window config dialog", "Select theme"));
}
else
{
setWindowTitle(caption);
}
mainLayout->addWidget(buttonBox);
show();
}
protected:
void closeEvent(QCloseEvent* event) override
{
event->accept();
//delete myself, but *not* the KgThemeSelector
mSelector->setParent(nullptr);
deleteLater();
//restore the KNS button
if (mSelector->d->m_knsButton)
{
mSelector->d->m_knsButton->show();
}
}
private:
KgThemeSelector* mSelector;
};
void KgThemeSelector::showAsDialog(const QString& caption)
{
if (!isVisible())
{
new KgThemeSelector::Dialog(this, caption);
}
}
//END KgThemeSelector
//BEGIN KgThemeDelegate
KgThemeDelegate::KgThemeDelegate(QObject* parent)
: QStyledItemDelegate(parent)
{
QAbstractItemView* view = qobject_cast<QAbstractItemView*>(parent);
if (view)
view->setItemDelegate(this);
}
QRect KgThemeDelegate::thumbnailRect(const QRect& baseRect) const
{
QRect thumbnailBaseRect(QPoint(Metrics::Padding + baseRect.left(), 0), Metrics::ThumbnailBaseSize);
thumbnailBaseRect.moveCenter(QPoint(thumbnailBaseRect.center().x(), baseRect.center().y()));
if (QApplication::isRightToLeft())
thumbnailBaseRect.moveRight(baseRect.right() - Metrics::Padding);
return thumbnailBaseRect;
}
void KgThemeDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
const bool rtl = option.direction == Qt::RightToLeft;
QRect baseRect = option.rect;
//draw background
QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, nullptr);
//draw thumbnail
QRect thumbnailBaseRect = this->thumbnailRect(baseRect);
const QPixmap thumbnail = index.data(Qt::DecorationRole).value<QPixmap>();
QApplication::style()->drawItemPixmap(painter, thumbnailBaseRect, Qt::AlignCenter, thumbnail);
//find metrics: text
QStringList texts; QList<QFont> fonts;
{
QString name = index.data(Qt::DisplayRole).toString();
if (name.isEmpty())
name = i18n("[No name]");
texts << name;
QFont theFont(painter->font()); theFont.setBold(true); fonts << theFont;
}{
QString comment = index.data(DescriptionRole).toString();
if (!comment.isEmpty())
{
texts << comment;
fonts << painter->font();
}
}{
QString author = index.data(AuthorRole).toString();
if (!author.isEmpty())
{
const QString authorString = ki18nc("Author attribution, e.g. \"by Jack\"", "by %1").subs(author).toString();
texts << authorString;
QFont theFont(painter->font()); theFont.setItalic(true); fonts << theFont;
}
}
//TODO: display AuthorEmailRole
QList<QRect> textRects; int totalTextHeight = 0;
for (int i = 0; i < texts.count(); ++i)
{
QFontMetrics fm(fonts[i]);
textRects << fm.boundingRect(texts[i]);
textRects[i].setHeight(qMax(textRects[i].height(), fm.lineSpacing()));
totalTextHeight += textRects[i].height();
}
QRect textBaseRect(baseRect);
if (rtl)
{
textBaseRect.setRight(thumbnailBaseRect.left() - Metrics::Padding);
textBaseRect.adjust(Metrics::Padding, Metrics::Padding, 0, -Metrics::Padding);
}
else
{
textBaseRect.setLeft(thumbnailBaseRect.right() + Metrics::Padding);
textBaseRect.adjust(0, Metrics::Padding, -Metrics::Padding, -Metrics::Padding);
}
textBaseRect.setHeight(totalTextHeight);
textBaseRect.moveTop(baseRect.top() + (baseRect.height() - textBaseRect.height()) / 2);
//draw texts
QRect currentTextRect(textBaseRect);
painter->save();
for (int i = 0; i < texts.count(); ++i)
{
painter->setFont(fonts[i]);
const QRect& textRect = textRects[i];
currentTextRect.setHeight(textRect.height());
const QFontMetrics fm(fonts[i]);
const QString text = fm.elidedText(texts[i], Qt::ElideRight, currentTextRect.width());
painter->drawText(currentTextRect, Qt::AlignLeft | Qt::AlignVCenter, text);
currentTextRect.moveTop(currentTextRect.bottom());
}
painter->restore();
}
QSize KgThemeDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
Q_UNUSED(option) Q_UNUSED(index)
//TODO: take text size into account
return QSize(400, Metrics::ThumbnailBaseSize.height() + 2 * Metrics::Padding);
}
//END KgThemeDelegate
#include "moc_kgthemeselector.cpp"
|