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

parley

buttondelegate.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002     Copyright 2008 Daniel Laidig <d.laidig@gmx.de>
00003  ***************************************************************************/
00004 
00005 /***************************************************************************
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  ***************************************************************************/
00013 
00014 #include "buttondelegate.h"
00015 #include "welcomescreen.h"
00016 
00017 #include <kwidgetitemdelegate.h>
00018 #include <KPushButton>
00019 #include <KMessageBox>
00020 
00021 #include <QStandardItemModel>
00022 #include <QPainter>
00023 #include <QAbstractItemDelegate>
00024 #include <QApplication>
00025 
00026 const int margin = 5;
00027 const int iconSize = 32;
00028 
00029 ButtonDelegate::ButtonDelegate(QAbstractItemView *itemView, WelcomeScreen *parent )
00030         : KWidgetItemDelegate(itemView, parent), m_rightMargin(0), m_buttonHeight(0), m_welcomeScreen(parent)
00031 {
00032 }
00033 
00034 ButtonDelegate::~ButtonDelegate()
00035 {
00036 }
00037     
00038 QList<QWidget*> ButtonDelegate::createItemWidgets() const
00039 {
00040     QList<QWidget*> widgetList;
00041     KPushButton *editButton = new KPushButton();
00042     editButton->setText(i18n("&Edit"));
00043     editButton->setIcon(KIcon("document-edit"));
00044     KPushButton *practiceButton = new KPushButton();
00045     practiceButton->setText(i18n("&Practice"));
00046     practiceButton->setIcon(KIcon("practice-start"));
00047     m_rightMargin = editButton->sizeHint().width() + practiceButton->sizeHint().width() + 3*margin;
00048     m_buttonHeight = editButton->sizeHint().height();
00049 
00050     connect(editButton, SIGNAL(clicked()), this, SLOT(slotEdit()));
00051     connect(practiceButton, SIGNAL(clicked()), this, SLOT(slotPractice()));
00052 
00053     widgetList << editButton << practiceButton;
00054     return widgetList;
00055 }
00056 
00057 void ButtonDelegate::updateItemWidgets(const QList<QWidget*> widgets, const QStyleOptionViewItem &option, const QPersistentModelIndex &index) const
00058 {
00059     KPushButton *editButton = static_cast<KPushButton*>(widgets[0]);
00060     QSize editButtonSizeHint = editButton->sizeHint();
00061     editButton->resize(editButtonSizeHint);
00062     
00063     KPushButton *practiceButton = static_cast<KPushButton*>(widgets[1]);
00064     QSize practiceButtonSizeHint = practiceButton->sizeHint();
00065     practiceButton->resize(practiceButtonSizeHint);
00066     
00067     editButton->move(option.rect.width() - editButtonSizeHint.width() - practiceButtonSizeHint.width() - 2*margin, (option.rect.height() - editButtonSizeHint.height())/2);
00068     practiceButton->move(option.rect.width() - practiceButtonSizeHint.width() - margin, (option.rect.height() - practiceButtonSizeHint.height())/2);  
00069 }
00070 
00071 void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
00072 {
00073     QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
00074     
00075     QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
00076     int iconMargin = (option.rect.height() - iconSize)/2;
00077     painter->drawPixmap(option.rect.x() + iconMargin, option.rect.y() + iconMargin, icon.pixmap(iconSize, iconSize));
00078     
00079     int rectX = option.rect.x() + iconMargin + iconSize + margin;
00080     QRect textRect(rectX, option.rect.y(), option.rect.width() - rectX - m_rightMargin, option.rect.height());
00081     
00082     QFontMetrics fm(option.font);
00083     QString elidedText = fm.elidedText(index.data().toString(), Qt::ElideRight, textRect.width());
00084     painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText);
00085 
00086     KWidgetItemDelegate::paintWidgets(painter, option, index);
00087 }
00088 
00089 QSize ButtonDelegate::sizeHint(const QStyleOptionViewItem &option,
00090                 const QModelIndex &index) const
00091 {
00092     Q_UNUSED(index);
00093 
00094     return QSize(qMax(iconSize, m_buttonHeight), 32);
00095 }
00096 
00097 void ButtonDelegate::slotEdit()
00098 {
00099     const QModelIndex index = focusedIndex();
00100     KUrl url = index.data(Qt::UserRole).toUrl();
00101     m_welcomeScreen->slotOpenUrl(url);
00102 }
00103 
00104 void ButtonDelegate::slotPractice()
00105 {
00106     const QModelIndex index = focusedIndex();
00107     KUrl url = index.data(Qt::UserRole).toUrl();
00108     m_welcomeScreen->slotPracticeUrl(url);
00109 }
00110 
00111 #include "buttondelegate.moc"

parley

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

kdeedu

Skip menu "kdeedu"
  •     lib
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal