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

parley

  • sources
  • kde-4.12
  • kdeedu
  • parley
  • src
  • welcomescreen
buttondelegate.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2008 Daniel Laidig <d.laidig@gmx.de>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  ***************************************************************************/
13 
14 #include "buttondelegate.h"
15 #include "welcomescreen.h"
16 
17 #include <KMessageBox>
18 
19 #include <QStandardItemModel>
20 #include <QPainter>
21 #include <QAbstractItemDelegate>
22 #include <QApplication>
23 #include <QToolButton>
24 
25 const int margin = 5;
26 const int iconSize = 22;
27 
28 ButtonDelegate::ButtonDelegate(QAbstractItemView *itemView, WelcomeScreen *parent)
29  : QStyledItemDelegate(itemView), m_rightMargin(0), m_buttonHeight(0), m_welcomeScreen(parent), m_itemView(itemView)
30 {
31  m_editButton = new QToolButton(itemView->viewport());
32  m_editButton->setIcon(KIcon("document-edit"));
33  m_editButton->setToolTip(i18n("Open this vocabulary collection in the editor"));
34  m_editButton->resize(m_editButton->sizeHint());
35  m_editButton->setAutoRaise(true);
36  m_editButton->hide();
37  m_rightMargin = m_editButton->sizeHint().width() + margin;
38  m_buttonHeight = m_editButton->sizeHint().height();
39 
40  connect(m_editButton, SIGNAL(clicked()), this, SLOT(slotEdit()));
41 }
42 
43 ButtonDelegate::~ButtonDelegate()
44 {
45 }
46 
47 void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
48 {
49  QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
50  if(option.state & QStyle::State_MouseOver) {
51  m_editButton->show();
52  m_editButton->move(QPoint(option.rect.right()-margin-m_editButton->sizeHint().width(),
53  option.rect.top()+(option.rect.height()-m_editButton->height())/2+1));
54  } else if (!hoveredIndex().isValid()) {
55  m_editButton->hide();
56  }
57 
58  QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
59  int iconMargin = (option.rect.height() - iconSize)/2;
60  painter->drawPixmap(option.rect.x() + iconMargin, option.rect.y() + iconMargin, icon.pixmap(iconSize, iconSize));
61 
62  int rectX = option.rect.x() + iconMargin + iconSize + margin;
63  QRect textRect(rectX, option.rect.y(), option.rect.width() - rectX - m_rightMargin, option.rect.height());
64 
65  QFontMetrics fm(option.font);
66  QString elidedText = fm.elidedText(index.data().toString(), Qt::ElideRight, textRect.width());
67  painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText);
68 }
69 
70 QSize ButtonDelegate::sizeHint(const QStyleOptionViewItem &option,
71  const QModelIndex &index) const
72 {
73  Q_UNUSED(index);
74  Q_UNUSED(option);
75 
76  return QSize(qMax(iconSize, m_buttonHeight), 32);
77 }
78 
79 void ButtonDelegate::slotEdit()
80 {
81  const QModelIndex index = hoveredIndex();
82  KUrl url = index.data(Qt::UserRole).toUrl();
83  m_welcomeScreen->slotOpenUrl(url);
84 }
85 
86 void ButtonDelegate::slotPractice()
87 {
88  const QModelIndex index = hoveredIndex();
89  KUrl url = index.data(Qt::UserRole).toUrl();
90  m_welcomeScreen->slotPracticeUrl(url);
91 }
92 
93 QModelIndex ButtonDelegate::hoveredIndex() const
94 {
95  const QPoint pos = m_itemView->viewport()->mapFromGlobal(QCursor::pos());
96  return m_itemView->indexAt(pos);
97 }
98 
99 #include "buttondelegate.moc"
WelcomeScreen::slotPracticeUrl
void slotPracticeUrl(const KUrl &url)
Definition: welcomescreen.cpp:138
ButtonDelegate::~ButtonDelegate
virtual ~ButtonDelegate()
Definition: buttondelegate.cpp:43
ButtonDelegate::slotPractice
void slotPractice()
Definition: buttondelegate.cpp:86
buttondelegate.h
ButtonDelegate::ButtonDelegate
ButtonDelegate(QAbstractItemView *itemView, WelcomeScreen *parent)
Definition: buttondelegate.cpp:28
welcomescreen.h
QStyledItemDelegate
margin
const int margin
Definition: buttondelegate.cpp:25
ButtonDelegate::hoveredIndex
QModelIndex hoveredIndex() const
Definition: buttondelegate.cpp:93
WelcomeScreen::slotOpenUrl
void slotOpenUrl(const KUrl &url)
Definition: welcomescreen.cpp:118
ButtonDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: buttondelegate.cpp:47
iconSize
const int iconSize
Definition: buttondelegate.cpp:26
WelcomeScreen
Definition: welcomescreen.h:30
ButtonDelegate::sizeHint
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: buttondelegate.cpp:70
ButtonDelegate::slotEdit
void slotEdit()
Definition: buttondelegate.cpp:79
QToolButton
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:05 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

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