File: streamlistdelegate.cpp

package info (click to toggle)
ostinato 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,840 kB
  • sloc: cpp: 46,226; makefile: 8
file content (194 lines) | stat: -rw-r--r-- 5,508 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright (C) 2010 Srivats P.

This file is part of "Ostinato"

This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

#include <QComboBox>
#include <QCheckBox>
#include <QApplication>
#include <QMouseEvent>

#include "streammodel.h"
#include "streamlistdelegate.h"

StreamListDelegate::StreamListDelegate(QObject *parent)
: QItemDelegate(parent)
{
}


QWidget *StreamListDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &option,
    const QModelIndex &index) const
{
    QWidget    *editor = NULL;

    switch ((StreamModel::StreamFields) index.column())
    {
        case StreamModel::StreamStatus:
        {
            editor = new QCheckBox(parent);
            goto _handled;
        }
        case StreamModel::StreamNextWhat:
        {
            editor = new QComboBox(parent);
            static_cast<QComboBox*>(editor)->insertItems(0, 
                StreamModel::nextWhatOptionList());
            goto _handled;
        }

        case StreamModel::StreamIcon:
        case StreamModel::StreamName:
        default:
            break;
    }

    editor =  QItemDelegate::createEditor(parent, option, index);

_handled:
    return editor;

}


void StreamListDelegate::setEditorData(QWidget *editor,
    const QModelIndex &index) const
{
    switch ((StreamModel::StreamFields) index.column())
    {
        case StreamModel::StreamStatus:
        {
            QCheckBox *cb = static_cast<QCheckBox*>(editor);
            cb->setChecked(
                index.model()->data(index, Qt::EditRole).toBool());
            goto _handled;
        }
        case StreamModel::StreamNextWhat:
        {
            QComboBox *cb = static_cast<QComboBox*>(editor);
            cb->setCurrentIndex(
                index.model()->data(index, Qt::EditRole).toInt());
            goto _handled;
        }

        case StreamModel::StreamIcon:
        case StreamModel::StreamName:
        default:
            break;
    }

    QItemDelegate::setEditorData(editor, index);

_handled:
    return;
}


void StreamListDelegate::setModelData(QWidget *editor,
        QAbstractItemModel *model, const QModelIndex &index) const
{
    switch ((StreamModel::StreamFields) index.column())
    {
        case StreamModel::StreamStatus:
        {
            QCheckBox *cb = static_cast<QCheckBox*>(editor);
            model->setData(index, cb->isChecked(), Qt::EditRole);
            goto _handled;
        }

        case StreamModel::StreamNextWhat:
        {
            QComboBox *cb = static_cast<QComboBox*>(editor);
            model->setData(index, cb->currentIndex(), Qt::EditRole);
            goto _handled;
        }

        case StreamModel::StreamIcon:
        case StreamModel::StreamName:
        default:
            break;
    }

    QItemDelegate::setModelData(editor, model, index);

_handled:
    return;
}


void StreamListDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    switch ((StreamModel::StreamFields) index.column())
    {
        case StreamModel::StreamStatus:
        {
            /*
             * extra 'coz QItemDelegate does it - otherwise the editor
             * placement is incorrect
             */
            int extra = 2 * (qApp->style()->pixelMetric(
                QStyle::PM_FocusFrameHMargin, 0) + 1);

            editor->setGeometry(option.rect.translated(extra, 0));
            goto _handled;
        }
        case StreamModel::StreamIcon:
        case StreamModel::StreamName:
        case StreamModel::StreamNextWhat:
        default:
            break;
    }

    QItemDelegate::updateEditorGeometry(editor, option, index);

_handled:
    return;
} 


bool StreamListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
        const QStyleOptionViewItem &option, const QModelIndex &index) 
{
    /* 
     * Special Handling so that user can use the "Stream status" checkbox 
     * without double clicking first. Copied from QItemDelegate::editorEvent()
     * and modified suitably
     */
    if ((StreamModel::StreamFields)index.column() ==
            StreamModel::StreamStatus)
    {
        // make sure that we have the right event type
        if ((event->type() == QEvent::MouseButtonRelease)
            || (event->type() == QEvent::MouseButtonDblClick))
        {
            QRect checkRect = doCheck(option, option.rect, Qt::Checked);
            QRect emptyRect;
            doLayout(option, &checkRect, &emptyRect, &emptyRect, false);
            if (!checkRect.contains(static_cast<QMouseEvent*>(event)->pos()))
                return false;

            Qt::CheckState state = (static_cast<Qt::CheckState>(index.data(
                Qt::CheckStateRole).toInt()) == Qt::Checked ? Qt::Unchecked : Qt::Checked);
            return model->setData(index, state, Qt::CheckStateRole);
        }
    }

    return QItemDelegate::editorEvent(event, model, option, index);
}