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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
/*
* Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
* Copyright (C) 2008-2011 Daniel Nicoletti <dantti12@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library/Lesser General Public License
* version 2, or (at your option) any later version, 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 General Public License for more details
*
* You should have received a copy of the GNU Library/Lesser 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 "ChangesDelegate.h"
#include <KIconLoader>
#include <KLocalizedString>
#include <QApplication>
#include <QLoggingCategory>
#include <QPushButton>
#include <QTreeView>
#include <QHeaderView>
#include <QPainter>
#include "PackageModel.h"
#include "PkIcons.h"
#include <Transaction>
#define FAV_ICON_SIZE 24
#define EMBLEM_ICON_SIZE 8
#define UNIVERSAL_PADDING 4
#define FADE_LENGTH 16
#define MAIN_ICON_SIZE 32
Q_DECLARE_LOGGING_CATEGORY(APPER_LIB)
using namespace PackageKit;
ChangesDelegate::ChangesDelegate(QAbstractItemView *parent) :
KExtendableItemDelegate(parent),
m_viewport(parent->viewport()),
// loads it here to be faster when displaying items
m_packageIcon(QIcon::fromTheme(QLatin1String("package"))),
m_collectionIcon(QIcon::fromTheme(QLatin1String("package-orign"))),
m_installIcon(QIcon::fromTheme(QLatin1String("dialog-cancel"))),
m_installString(i18n("Do not Install")),
m_removeIcon(QIcon::fromTheme(QLatin1String("dialog-cancel"))),
m_removeString(i18n("Do not Remove")),
m_undoIcon(QIcon::fromTheme(QLatin1String("edit-undo"))),
m_undoString(i18n("Deselect")),
m_checkedIcon(QIcon::fromTheme(QLatin1String("dialog-ok-apply")))
{
// maybe rename or copy it to package-available
if (QApplication::isRightToLeft()) {
setExtendPixmap(SmallIcon(QLatin1String("arrow-left")));
} else {
setExtendPixmap(SmallIcon(QLatin1String("arrow-right")));
}
setContractPixmap(SmallIcon(QLatin1String("arrow-down")));
// store the size of the extend pixmap to know how much we should move
m_extendPixmapWidth = SmallIcon(QLatin1String("arrow-right")).size().width();
QPushButton button, button2;
button.setText(m_installString);
button.setIcon(m_installIcon);
button2.setText(m_removeString);
button2.setIcon(m_removeIcon);
m_buttonSize = button.sizeHint();
int width = qMax(button.sizeHint().width(), button2.sizeHint().width());
button.setText(m_undoString);
width = qMax(width, button2.sizeHint().width());
m_buttonSize.setWidth(width);
m_buttonIconSize = button.iconSize();
}
void ChangesDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (!index.isValid()) {
return;
}
bool leftToRight = (painter->layoutDirection() == Qt::LeftToRight);
QStyleOptionViewItem opt(option);
QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
painter->save();
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
painter->restore();
//grab the package from the index pointer
QString pkgName = index.data(PackageModel::NameRole).toString();
QString pkgSummary = index.data(PackageModel::SummaryRole).toString();
QString pkgVersion = index.data(PackageModel::VersionRole).toString();
QString pkgArch = index.data(PackageModel::ArchRole).toString();
// QString pkgIconPath = index.data(PackageModel::IconPathRole).toString();
bool pkgChecked = index.data(PackageModel::CheckStateRole).toBool();
bool pkgCheckable = !index.data(Qt::CheckStateRole).isNull();
Transaction::Info info;
info = index.data(PackageModel::InfoRole).value<Transaction::Info>();
bool pkgInstalled = (info == Transaction::InfoInstalled ||
info == Transaction::InfoCollectionInstalled);
bool pkgCollection = (info == Transaction::InfoCollectionInstalled ||
info == Transaction::InfoCollectionAvailable);
QIcon emblemIcon;
if (pkgCheckable) {
// update kind icon
emblemIcon = index.data(PackageModel::IconRole).value<QIcon>();
} else {
emblemIcon = m_checkedIcon;
}
// pain the background (checkbox and the extender)
if (m_extendPixmapWidth) {
KExtendableItemDelegate::paint(painter, opt, index);
}
int leftCount;
if (leftToRight) {
opt.rect.setLeft(option.rect.left() + m_extendPixmapWidth + UNIVERSAL_PADDING);
leftCount = opt.rect.left() + UNIVERSAL_PADDING;
} else {
opt.rect.setRight(option.rect.right() - m_extendPixmapWidth - UNIVERSAL_PADDING);
leftCount = opt.rect.width() - (UNIVERSAL_PADDING + MAIN_ICON_SIZE);
}
int left = opt.rect.left();
int top = opt.rect.top();
int width = opt.rect.width();
QStyleOptionButton optBt;
optBt.rect = opt.rect;
if (pkgCheckable) {
optBt.rect = style->subElementRect(QStyle::SE_CheckBoxIndicator, &optBt);
// Count the checkbox size
if (leftToRight) {
leftCount += optBt.rect.width();
} else {
leftCount -= optBt.rect.width();
}
} else if ((option.state & QStyle::State_MouseOver) ||
(option.state & QStyle::State_Selected) ||
!pkgChecked) {
if (leftToRight) {
optBt.rect.setLeft(left + width - (m_buttonSize.width() + UNIVERSAL_PADDING));
width -= m_buttonSize.width() + UNIVERSAL_PADDING;
} else {
optBt.rect.setLeft(left + UNIVERSAL_PADDING);
left += m_buttonSize.width() + UNIVERSAL_PADDING;
}
// Calculate the top of the button which is the item height - the button height size divided by 2
// this give us a little value which is the top and bottom margin
optBt.rect.setTop(optBt.rect.top() + ((calcItemHeight(option) - m_buttonSize.height()) / 2));
optBt.rect.setSize(m_buttonSize); // the width and height sizes of the button
optBt.features = QStyleOptionButton::Flat;
optBt.iconSize = m_buttonIconSize;
optBt.icon = pkgInstalled ? m_removeIcon : m_installIcon;
optBt.text = pkgInstalled ? m_removeString : m_installString;
if (pkgChecked) {
optBt.state |= QStyle::State_Raised | QStyle::State_Active | QStyle::State_Enabled;;
} else {
if ((option.state & QStyle::State_MouseOver) &&
!(option.state & QStyle::State_Selected)) {
optBt.state |= QStyle::State_MouseOver;
}
optBt.state |= QStyle::State_Sunken | QStyle::State_Active | QStyle::State_Enabled;
}
style->drawControl(QStyle::CE_PushButton, &optBt, painter);
}
// QAbstractItemView *view = qobject_cast<QAbstractItemView*>(parent());
// QPoint pos = view->viewport()->mapFromGlobal(QCursor::pos());
// kDebug() << pos;
// selects the mode to paint the icon based on the info field
QIcon::Mode iconMode = QIcon::Normal;
if (option.state & QStyle::State_MouseOver) {
iconMode = QIcon::Active;
}
QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected))?
option.palette.color(QPalette::HighlightedText):option.palette.color(QPalette::Text);
// Painting main column
QStyleOptionViewItem local_option_title(option);
QStyleOptionViewItem local_option_normal(option);
local_option_normal.font.setPointSize(local_option_normal.font.pointSize() - 1);
QPixmap pixmap(option.rect.size());
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
p.translate(-option.rect.topLeft());
// Main icon
QIcon icon;
if (pkgCollection) {
icon = m_collectionIcon;
} else {
icon = PkIcons::getIcon(index.data(PackageModel::IconRole).toString(), QString());
if (icon.isNull()) {
icon = m_packageIcon;
}
}
// if (pkgIconPath.isEmpty()) {
// icon = pkgCollection ? m_collectionIcon : m_packageIcon;
// } else {
// icon = PkIcons::getIcon(pkgIconPath, "package");
// }
int iconSize = calcItemHeight(option) - 2 * UNIVERSAL_PADDING;
icon.paint(&p,
leftCount,
top + UNIVERSAL_PADDING,
iconSize,
iconSize,
Qt::AlignCenter,
iconMode);
int textWidth;
if (leftToRight) {
// add the main icon
leftCount += iconSize + UNIVERSAL_PADDING;
textWidth = width - (leftCount - left);
} else {
leftCount -= UNIVERSAL_PADDING;
textWidth = leftCount - left;
leftCount = left;
}
// Painting
// Text
const int itemHeight = calcItemHeight(option);
p.setPen(foregroundColor);
// compose the top line
// Collections does not have version and arch
if (option.state & QStyle::State_MouseOver && !pkgCollection) {
//! pkgName = pkgName + " - " + pkgVersion + (pkgArch.isNull() ? NULL : " (" + pkgArch + ')');
}
// draw the top line
int topTextHeight = QFontInfo(local_option_title.font).pixelSize();
p.setFont(local_option_title.font);
p.drawText(leftCount,
top,
textWidth,
topTextHeight + UNIVERSAL_PADDING,
Qt::AlignVCenter | Qt::AlignLeft,
pkgName);
// draw the bottom line
iconSize = topTextHeight + UNIVERSAL_PADDING;
if (pkgCheckable || pkgInstalled) {
emblemIcon.paint(&p,
leftToRight ? leftCount : (textWidth + left) - iconSize,
top + topTextHeight + UNIVERSAL_PADDING,
iconSize,
iconSize,
Qt::AlignVCenter | Qt::AlignHCenter,
iconMode);
}
// store the original opacity
qreal opa = p.opacity();
if (!(option.state & QStyle::State_MouseOver) && !(option.state & QStyle::State_Selected)) {
p.setOpacity(opa / 2.5);
}
p.setFont(local_option_normal.font);
p.drawText(leftToRight ? leftCount + iconSize + UNIVERSAL_PADDING : left - UNIVERSAL_PADDING,
top + itemHeight / 2,
textWidth - iconSize,
QFontInfo(local_option_normal.font).pixelSize() + UNIVERSAL_PADDING,
Qt::AlignTop | Qt::AlignLeft,
pkgSummary);
p.setOpacity(opa);
QLinearGradient gradient;
// Gradient part of the background - fading of the text at the end
if (leftToRight) {
gradient = QLinearGradient(left + width - UNIVERSAL_PADDING - FADE_LENGTH,
0,
left + width - UNIVERSAL_PADDING,
0);
gradient.setColorAt(0, Qt::white);
gradient.setColorAt(1, Qt::transparent);
} else {
gradient = QLinearGradient(left + UNIVERSAL_PADDING,
0,
left + UNIVERSAL_PADDING + FADE_LENGTH,
0);
gradient.setColorAt(0, Qt::transparent);
gradient.setColorAt(1, Qt::white);
}
QRect paintRect = option.rect;
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
p.fillRect(paintRect, gradient);
if (leftToRight) {
gradient.setStart(left + width
- (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE) - FADE_LENGTH, 0);
gradient.setFinalStop(left + width
- (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE), 0);
} else {
gradient.setStart(left + UNIVERSAL_PADDING
+ (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE), 0);
gradient.setFinalStop(left + UNIVERSAL_PADDING
+ (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE) + FADE_LENGTH, 0);
}
paintRect.setHeight(UNIVERSAL_PADDING + MAIN_ICON_SIZE / 2);
p.fillRect(paintRect, gradient);
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
p.end();
painter->drawPixmap(option.rect.topLeft(), pixmap);
}
int ChangesDelegate::calcItemHeight(const QStyleOptionViewItem &option) const
{
// Painting main column
QStyleOptionViewItem local_option_title(option);
QStyleOptionViewItem local_option_normal(option);
local_option_normal.font.setPointSize(local_option_normal.font.pointSize() - 1);
int textHeight = QFontInfo(local_option_title.font).pixelSize() + QFontInfo(local_option_normal.font).pixelSize();
return textHeight + 3 * UNIVERSAL_PADDING;
}
bool ChangesDelegate::insideButton(const QRect &rect, const QPoint &pos) const
{
// kDebug() << rect << pos;
if ((pos.x() >= rect.x() && (pos.x() <= rect.x() + rect.width())) &&
(pos.y() >= rect.y() && (pos.y() <= rect.y() + rect.height()))) {
return true;
}
return false;
}
bool ChangesDelegate::editorEvent(QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
Q_UNUSED(option)
if (event->type() == QEvent::MouseButtonRelease) {
QAbstractItemView *view = qobject_cast<QAbstractItemView*>(parent());
QPoint point = m_viewport->mapFromGlobal(QCursor::pos());
QTreeView *tree = qobject_cast<QTreeView*>(parent());
if (tree) {
point.ry() -= tree->header()->size().height();
}
bool leftToRight = QApplication::isLeftToRight();
QStyleOptionButton optBt;
optBt.rect = option.rect;
if (leftToRight) {
optBt.rect.setLeft(option.rect.left() + option.rect.width() - (m_buttonSize.width() + UNIVERSAL_PADDING));
} else {
optBt.rect.setLeft(option.rect.left() + UNIVERSAL_PADDING);
}
// Calculate the top of the button which is the item height - the button height size divided by 2
// this give us a little value which is the top and bottom margin
optBt.rect.setTop(optBt.rect.top() + ((calcItemHeight(option) - m_buttonSize.height()) / 2));
optBt.rect.setSize(m_buttonSize);
qCDebug(APPER_LIB) << point << option.rect.left() << option << insideButton(optBt.rect, point);
// kDebug() << view->visualRect(index);
if (insideButton(optBt.rect, point)) {
return model->setData(index,
!index.data(PackageModel::CheckStateRole).toBool(),
Qt::CheckStateRole);
}
QRect rect = view->visualRect(index);
if (QApplication::isRightToLeft()) {
if ((rect.width() - point.x()) <= m_extendPixmapWidth) {
emit showExtendItem(index);
}
} else if (point.x() <= m_extendPixmapWidth) {
emit showExtendItem(index);
}
}
// We need move the option rect left because KExtendableItemDelegate
// drew the extendPixmap
QStyleOptionViewItem opt(option);
if (QApplication::isRightToLeft()) {
opt.rect.setRight(option.rect.right() - m_extendPixmapWidth);
} else {
opt.rect.setLeft(option.rect.left() + m_extendPixmapWidth);
}
// When the exterder is shown the height get compromised,
// this makes sure the check box is always known
opt.rect.setHeight(calcItemHeight(option));
return KExtendableItemDelegate::editorEvent(event, model, opt, index);
}
void ChangesDelegate::setExtendPixmapWidth(int width)
{
m_extendPixmapWidth = width;
}
void ChangesDelegate::setViewport(QWidget *viewport)
{
m_viewport = viewport;
}
QSize ChangesDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
int width = (index.column() == 0) ? index.data(Qt::SizeHintRole).toSize().width() : FAV_ICON_SIZE + 2 * UNIVERSAL_PADDING;
QSize ret(KExtendableItemDelegate::sizeHint(option, index));
// remove the default size of the index
ret -= QStyledItemDelegate::sizeHint(option, index);
ret.rheight() += calcItemHeight(option);
ret.rwidth() += width;
return ret;
}
#include "moc_ChangesDelegate.cpp"
|