src/job_editor_widget/jobpropertydelegate.cpp

Go to the documentation of this file.
00001 
00008 #include "jobpropertydelegate.h"
00009 #include "jobpropertyitem.h"
00010 
00011 
00012 #include <QKeyEvent>
00013 #include <QApplication>
00014 #include <QPen>
00015 #include <QPainter>
00016 #include <QFont>
00017 #include <QLineEdit>
00018 #include <QAbstractItemView>
00019 
00025 JobPropertyDelegate::JobPropertyDelegate(QObject *parent)
00026     : QItemDelegate(parent),
00027       m_syncing(false),
00028       m_lastEdited(0)
00029 {
00030 }
00031 
00035 JobPropertyDelegate::~JobPropertyDelegate()
00036 {
00037 }
00038 
00047 bool JobPropertyDelegate::eventFilter(QObject *object, QEvent *event)
00048 {
00049     QWidget *editor = qobject_cast<QWidget*>(object);
00050     if (editor && qobject_cast<QLineEdit*>(editor->parent()))
00051         editor = editor->parentWidget();
00052 
00053     switch (event->type()) {
00054         case QEvent::KeyPress:
00055         case QEvent::KeyRelease:
00056         {
00057             QKeyEvent *ke = static_cast<QKeyEvent*>(event);
00058             if (!(ke->modifiers() & Qt::ControlModifier)
00059                 && (ke->key() == Qt::Key_Up || ke->key() == Qt::Key_Down)) {
00060                 event->ignore();
00061                 return true;
00062             }
00063             if (object->metaObject()->className() == QLatin1String("QtKeySequenceEdit"))
00064             {
00065                 event->ignore();
00066                 return false;
00067             }
00068         } break;
00069 
00070         case QEvent::FocusOut:
00071             if (!editor->isActiveWindow() || (QApplication::focusWidget() != editor))
00072             {
00073                 emit commitData(editor);
00074                 emit closeEditor(editor);
00075             }
00076             return false;
00077 
00078         default:
00079             break;
00080     }
00081 
00082     return QItemDelegate::eventFilter(editor ? editor : object, event);
00083 }
00084 
00092 void JobPropertyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const
00093 {
00094     QStyleOptionViewItem option = opt;
00095 
00096     if (index.column() == 1)
00097     {
00098         option.state &= ~QStyle::State_Selected;
00099     }
00100 
00101     option.state &= ~QStyle::State_HasFocus;
00102 
00103     const QPen savedPen = painter->pen();
00104 
00105     QItemDelegate::paint(painter, option, index);
00106 
00107     const QColor color = static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
00108     painter->setPen(QPen(color));
00109     if (index.column() == 1 )
00110     {
00111         const int right = (option.direction == Qt::LeftToRight) ? option.rect.right() : option.rect.left();
00112         painter->drawLine(right, option.rect.y(), right, option.rect.bottom());
00113     }
00114     painter->drawLine(option.rect.x(), option.rect.bottom(),
00115             option.rect.right(), option.rect.bottom());
00116     painter->setPen(savedPen);
00117 }
00118 
00125 QSize JobPropertyDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const
00126 {
00127     return QItemDelegate::sizeHint(opt, index) + QSize(4, 4);
00128 }
00129 
00130 
00136 void JobPropertyDelegate::slotDestroyed(QObject *object)
00137 {
00138     if (m_lastEdited == object)
00139     {
00140         m_lastEdited = 0;
00141         emit editorClosed();
00142     }
00143 }
00144 
00152 QWidget *JobPropertyDelegate::createEditor(QWidget *parent,
00153                                 const QStyleOptionViewItem &option,
00154                                 const QModelIndex &index) const
00155 {
00156     Q_UNUSED(option);
00157 
00158     JobPropertyModel *model = const_cast<JobPropertyModel *>(static_cast<const JobPropertyModel *>(index.model()));
00159     const JobPropertyItem *property = model->getItem(index);
00160     if (property == 0)
00161         return 0;
00162 
00163     QWidget *editor = 0;
00164 
00165     if (!property->isReadOnly() && index.column() != 0)
00166     {
00167         editor = property->createEditor(parent, this, SLOT(sync()));
00168         editor->installEventFilter(const_cast<JobPropertyDelegate *>(this));
00169 
00170         connect(editor, SIGNAL(destroyed(QObject *)), this, SLOT(slotDestroyed(QObject *)));
00171 
00172         JobPropertyDelegate *that = const_cast<JobPropertyDelegate *>(this);
00173         if (!m_lastEdited)
00174             emit that->editorOpened();
00175         m_lastEdited = editor;
00176     }
00177     return editor;
00178 }
00179 
00186 void JobPropertyDelegate::setEditorData(QWidget *editor,
00187                              const QModelIndex &index) const
00188 {
00189     const QAbstractItemModel *model = index.model();
00190     JobPropertyItem *property = static_cast<const JobPropertyModel*>(model)->getItem(index);
00191     if (property)
00192     {
00193         property->updateEditorContents(editor);
00194     }
00195 }
00196 
00204 void JobPropertyDelegate::setModelData(QWidget *editor,
00205                             QAbstractItemModel *model,
00206                             const QModelIndex &index) const
00207 {
00208     if (JobPropertyItem *property = static_cast<const JobPropertyModel*>(model)->getItem(index))
00209     {
00210         property->updateValue(editor);
00211         model->setData(index, property->data());
00212     }
00213 }
00214 
00215 
00219 void JobPropertyDelegate::sync()
00220 {
00221     m_syncing = true;
00222     QWidget *w = qobject_cast<QWidget*>(sender());
00223     if (w == 0)
00224         return;
00225     emit commitData(w);
00226     m_syncing = false;
00227 }
00228 
00232 void JobPropertyDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
00233 {
00234     QItemDelegate::updateEditorGeometry(editor, option, index);
00235     editor->setGeometry(editor->geometry().adjusted(0, 0, -1, -1));
00236 }
00237 
00243 void JobPropertyDelegate::onCustomContextMenuRequested(const QPoint & pos)
00244 {
00245     QModelIndex idx = dynamic_cast<QAbstractItemView *>(parent())->indexAt(pos);
00246     JobPropertyModel *model = const_cast<JobPropertyModel *>(static_cast<const JobPropertyModel *>(idx.model()));
00247     if (model)
00248     {
00249         JobPropertyItem *jpi = model->getItem(idx);
00250 
00251         if (jpi && jpi->hasContextMenu())
00252             jpi->createCustomContextMenu(dynamic_cast<QWidget *>(parent()), pos);
00253     }
00254 }

Generated on Mon Mar 16 18:46:05 2009 for QCJM by  doxygen 1.5.4