defuze.me
Client
|
00001 /************************************************************************** 00002 ** defuze.me Epitech Innovative Project 00003 ** 00004 ** Copyright 2010-2012 00005 ** Athena Calmettes - Jocelyn De La Rosa - Francois Gaillard 00006 ** Adrien Jarthon - Alexandre Moore - Luc Peres - Arnaud Sellier 00007 ** 00008 ** All rights reserved. 00009 **************************************************************************/ 00010 00011 #include "listsdelegate.hpp" 00012 #include "listsitem.hpp" 00013 #include "editabletreeitem.hpp" 00014 #include <QPainter> 00015 #include <QApplication> 00016 #include <QLineEdit> 00017 00018 using namespace Lists; 00019 00020 ListsDelegate::ListsDelegate(QObject *parent) : QStyledItemDelegate(parent) 00021 { 00022 editorRegEpx = new QRegExp("\\S.{1,99}"); 00023 } 00024 00025 QSize ListsDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 00026 { 00027 Q_UNUSED(index); 00028 int height = 2; 00029 00030 return QSize(200, QFontMetrics(option.font).height() * height); 00031 } 00032 00033 void ListsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 00034 { 00035 painter->save(); 00036 00037 ListsItem *item = static_cast<ListsItem*>(index.internalPointer()); 00038 //EditableTreeItem *item = static_cast<EditableTreeItem*>(index.internalPointer()); 00039 00040 // Draw background when selected 00041 if (option.state & QStyle::State_Selected) 00042 painter->fillRect(option.rect, option.palette.highlight()); 00043 00044 // Draw shadows 00045 painter->setPen(QColor(255, 255, 255, 20)); 00046 painter->drawLine(QPoint(0, option.rect.top()), option.rect.topRight()); 00047 painter->setPen(QColor(0, 0, 0, 60)); 00048 painter->drawLine(QPoint(0, option.rect.bottom()), option.rect.bottomRight()); 00049 // Set text mode 00050 if (option.state & QStyle::State_Selected) 00051 painter->setPen(option.palette.highlightedText().color()); 00052 else 00053 painter->setPen(option.palette.foreground().color()); 00054 00055 00056 // Set the title font 00057 QFont font(option.font.family()); 00058 00059 QRect titleRect = option.rect.adjusted(31, 0, -31, 0); 00060 QRect pixmapRect = QRect(option.rect.left(), option.rect.top() + (option.rect.height() - 22) / 2, 22, 22); 00061 00062 font.setPointSize(11); 00063 painter->setFont(font); 00064 00065 painter->fillRect(pixmapRect, QColor(255,255,255,51)); 00066 00067 font.setPointSize(13); 00068 painter->setFont(font); 00069 // Draw Title 00070 painter->drawText(titleRect, Qt::AlignLeft | Qt::AlignVCenter, item->data(0).toString()); 00071 00072 painter->restore(); 00073 } 00074 00075 QWidget *ListsDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const 00076 { 00077 QLineEdit *editor = new QLineEdit(parent); 00078 editor->setValidator(new QRegExpValidator(*editorRegEpx, 0)); 00079 return editor; 00080 } 00081 00082 void ListsDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const 00083 { 00084 QString value = index.model()->data(index, Qt::EditRole).toString(); 00085 00086 QLineEdit *lineEdit = static_cast<QLineEdit*>(editor); 00087 lineEdit->setText(value); 00088 } 00089 00090 void ListsDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 00091 { 00092 QLineEdit *lineEdit = static_cast<QLineEdit*>(editor); 00093 QString value = lineEdit->text(); 00094 00095 model->setData(index, value, Qt::EditRole); 00096 } 00097 00098 void ListsDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const 00099 { 00100 editor->setGeometry(option.rect); 00101 }