File: styledelegate.cpp

package info (click to toggle)
qbrew 0.4.1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,724 kB
  • ctags: 1,355
  • sloc: cpp: 7,503; sh: 414; makefile: 18
file content (169 lines) | stat: -rw-r--r-- 4,963 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
  styledelegate.cpp
  -------------------
  Style delegate editor
  -------------------
  Copyright 2006-2008, David Johnson
  Please see the header file for copyright and license information
 ***************************************************************************/

#include <QAbstractItemModel>
#include <QDoubleSpinBox>
#include <QLineEdit>
#include <QSpinBox>

#include "data.h"
#include "style.h"
#include "styledelegate.h"
#include "stylemodel.h"

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

StyleDelegate::~StyleDelegate() {}

QWidget *StyleDelegate::createEditor(QWidget *parent,
                                     const QStyleOptionViewItem &/*option*/,
                                     const QModelIndex &index) const
{
    QLineEdit *edit;
    QDoubleSpinBox *dspin;
    QSpinBox *spin;
    QString suffix;

    // can only edit name on blank row
    bool blank = index.row() >= index.model()->rowCount();

    // different kind of editor for each column
    switch (index.column()) {
      case StyleModel::NAME:
          edit = new QLineEdit(parent);
          edit->installEventFilter(const_cast<StyleDelegate*>(this));
          return edit;

      case StyleModel::OGLOW:
      case StyleModel::OGHI:
      case StyleModel::FGLOW:
      case StyleModel::FGHI:
          if (blank) return 0;
          dspin = new QDoubleSpinBox(parent);
          dspin->setDecimals(3);
          dspin->setRange(0.990, 1.150);
          dspin->setSingleStep(0.001);
          dspin->setAccelerated(true);
          dspin->installEventFilter(const_cast<StyleDelegate*>(this));
          return dspin;

      case StyleModel::IBULOW:
      case StyleModel::IBUHI:
          if (blank) return 0;
          spin = new QSpinBox(parent);
          spin->setRange(0, 120);
          spin->setSingleStep(1);
          spin->setAccelerated(true);
          spin->installEventFilter(const_cast<StyleDelegate*>(this));
          return spin;

      case StyleModel::SRMLOW:
      case StyleModel::SRMHI:
          if (blank) return 0;
          spin = new QSpinBox(parent);
          spin->setRange(0, 50);
          spin->setSingleStep(1);
          spin->setAccelerated(true);
          spin->installEventFilter(const_cast<StyleDelegate*>(this));
          return spin;

      default:
          return 0;
    }
}

void StyleDelegate::setEditorData(QWidget *editor,
                                  const QModelIndex &index) const
{
    QLineEdit *edit;
    QDoubleSpinBox *dspin;
    QSpinBox *spin;

    QVariant value = index.model()->data(index, Qt::EditRole);

    // different kind of editor for each column
    switch (index.column()) {
      case StyleModel::NAME:
          edit = static_cast<QLineEdit*>(editor);
          if (!edit) return;
          edit->setText(value.toString());
          break;

      case StyleModel::OGLOW:
      case StyleModel::OGHI:
      case StyleModel::FGLOW:
      case StyleModel::FGHI:
          dspin = static_cast<QDoubleSpinBox*>(editor);
          if (!dspin) return;
          dspin->setValue(value.toDouble());
          break;

      case StyleModel::IBULOW:
      case StyleModel::IBUHI:
      case StyleModel::SRMLOW:
      case StyleModel::SRMHI:
          spin = static_cast<QSpinBox*>(editor);
          if (!spin) return;
          spin->setValue(value.toUInt());
          break;
      default:
          QItemDelegate::setEditorData(editor, index);
          break;
    }
}

void StyleDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const
{
    QLineEdit *edit;
    QDoubleSpinBox *dspin;
    QSpinBox *spin;
    QVariant value;

    // different kind of editor for each column
    switch (index.column()) {
      case StyleModel::NAME:
          edit = static_cast<QLineEdit*>(editor);
          if (!edit) return;
          value = edit->text();
          model->setData(index, value);
          break;

      case StyleModel::OGLOW:
      case StyleModel::OGHI:
      case StyleModel::FGLOW:
      case StyleModel::FGHI:
          dspin = static_cast<QDoubleSpinBox*>(editor);
          if (!dspin) return;
          value = dspin->value();
          model->setData(index, value);
          break;

      case StyleModel::IBULOW:
      case StyleModel::IBUHI:
      case StyleModel::SRMLOW:
      case StyleModel::SRMHI:
          spin = static_cast<QSpinBox*>(editor);
          if (!spin) return;
          value = spin->value();
          model->setData(index, value);
          break;

      default:
          QItemDelegate::setModelData(editor, model,index);
          break;
    }
}

void StyleDelegate::updateEditorGeometry(QWidget *editor,
                              const QStyleOptionViewItem &option,
                              const QModelIndex &/*index*/) const
{
    editor->setGeometry(option.rect);
}