File: miscdelegate.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 (166 lines) | stat: -rw-r--r-- 4,959 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
/***************************************************************************
  miscdelegate.cpp
  -------------------
  Misc delegate editor
  -------------------
  Copyright 2006-2008, David Johnson
  Please see the header file for copyright and license information
 ***************************************************************************/

#include <QAbstractItemModel>
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QLineEdit>

#include "data.h"
#include "misc.h"
#include "miscdelegate.h"
#include "miscmodel.h"

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

MiscDelegate::~MiscDelegate() {}

QWidget *MiscDelegate::createEditor(QWidget *parent,
                                     const QStyleOptionViewItem &/*option*/,
                                     const QModelIndex &index) const
{
    QComboBox *combo;
    QDoubleSpinBox *spin;
    QLineEdit *edit;
    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 MiscModel::NAME:
          combo = new QComboBox(parent);
          combo->setEditable(true);
          combo->addItem(QString());
          combo->addItems(Data::instance()->miscList());
          combo->installEventFilter(const_cast<MiscDelegate*>(this));
          return combo;

      case MiscModel::QUANTITY:
          if (blank) return 0;
          spin = new QDoubleSpinBox(parent);
          spin->setDecimals(3);
          spin->setRange(0.00, 1000.00);
          spin->setSingleStep(0.25);
          suffix = " " + Data::instance()->defaultMiscUnit().symbol();
          spin->setSuffix(suffix);
          spin->setAccelerated(true);
          spin->installEventFilter(const_cast<MiscDelegate*>(this));
          return spin;

      case MiscModel::TYPE:
          combo = new QComboBox(parent);
          combo->setEditable(false);
          combo->addItems(Misc::typeStringList());
          combo->installEventFilter(const_cast<MiscDelegate*>(this));
          return combo;

      case MiscModel::NOTES:
          if (blank) return 0;
          edit = new QLineEdit(parent);
          edit->installEventFilter(const_cast<MiscDelegate*>(this));
          return edit;

      default:
          return 0;;
    }
}

void MiscDelegate::setEditorData(QWidget *editor,
                                 const QModelIndex &index) const
{
    QComboBox *combo;
    QDoubleSpinBox *spin;
    QLineEdit *edit;
    int comboindex;

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

    // different kind of editor for each column
    switch (index.column()) {
      case MiscModel::NAME:
          combo = static_cast<QComboBox*>(editor);
          if (!combo) return;
          comboindex = combo->findText(value.toString());
          if (comboindex > 0) {
              combo->setCurrentIndex(comboindex);
          } else {
              combo->setEditText(value.toString());
          }
          break;

      case MiscModel::QUANTITY:
          spin = static_cast<QDoubleSpinBox*>(editor);
          if (!spin) return;
          spin->setValue(value.toDouble());
          break;

      case MiscModel::NOTES:
          edit = static_cast<QLineEdit*>(editor);
          if (!edit) return;
          edit->setText(value.toString());
          break;

      case MiscModel::TYPE:
          combo = static_cast<QComboBox*>(editor);
          if (!combo) return;
          combo->setCurrentIndex(combo->findText(value.toString()));
          break;

      default:
          QItemDelegate::setEditorData(editor, index);
          break;
    }
}

void MiscDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const
{
    QComboBox *combo;
    QDoubleSpinBox *spin;
    QLineEdit *edit;
    QVariant value;

    // different kind of editor for each column
    switch (index.column()) {
      case MiscModel::NAME:
      case MiscModel::TYPE:
          combo = static_cast<QComboBox*>(editor);
          if (!combo) return;
          value = combo->currentText();
          model->setData(index, value);
          break;

      case MiscModel::NOTES:
          edit = static_cast<QLineEdit*>(editor);
          if (!edit) return;
          value = edit->text();
          model->setData(index, value);
          break;

      case MiscModel::QUANTITY:
          spin = static_cast<QDoubleSpinBox*>(editor);
          if (!spin) return;
          value = spin->value();
          model->setData(index, value);
          break;
     
      default:
          QItemDelegate::setModelData(editor, model,index);
          break;
    }
}

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