GPLv2 license
[scorecard] / src / cell-delegate.cpp
1 /*
2  * Copyright (C) 2009 Sakari Poussa
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 2.
7  */
8
9 #include "cell-delegate.h"
10 #include <QtGui>
11 #include <QDebug>
12
13 CellDelegate::CellDelegate(QObject *parent) : QItemDelegate(parent) 
14 {
15   m_parent = parent;
16 }
17
18 QWidget *CellDelegate::createEditor(QWidget *parent,
19                                     const QStyleOptionViewItem &,
20                                     const QModelIndex &index) const
21 {
22   CellFilter *filter = new CellFilter(m_parent); // HACK
23
24   //CellFilter filter(parent);
25
26   QLineEdit *le = new QLineEdit(parent);
27
28   le->installEventFilter(filter);
29
30   qDebug() << "parent=" << parent << " m_parent=" << m_parent;
31   // HACK: Connect CellFilter signal to CourseDialog nextCell slot
32   connect(filter, SIGNAL(nextCell(QObject *)), m_parent, SLOT(nextCell(QObject *)));
33
34   return le;
35 }
36
37 void CellDelegate::commitAndCloseEditor()
38 {
39     QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
40
41     // TODO: del editor
42     emit commitData(editor);
43     emit closeEditor(editor);
44 }
45
46 void CellDelegate::setEditorData(QWidget *editor,
47                                  const QModelIndex &index) const
48 {
49   QString value = index.model()->data(index, Qt::EditRole).toString();
50
51   QLineEdit *le = static_cast<QLineEdit *>(editor);
52
53   le->setText(value);
54 }
55
56 void CellDelegate::setModelData(QWidget *editor,
57     QAbstractItemModel *model, const QModelIndex &index) const
58 {
59     QLineEdit *le = static_cast<QLineEdit*>(editor);
60
61     QString value = le->text();
62
63     model->setData(index, value, Qt::EditRole);
64
65 }
66