File: numbergencombobox.cpp

package info (click to toggle)
postbooks 4.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 112,660 kB
  • ctags: 22,890
  • sloc: cpp: 310,358; sh: 607; xml: 214; python: 140; awk: 104; makefile: 50
file content (229 lines) | stat: -rw-r--r-- 6,239 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
 * It is licensed to you under the Common Public Attribution License
 * version 1.0, the full text of which (including xTuple-specific Exhibits)
 * is available at www.xtuple.com/CPAL.  By using this software, you agree
 * to be bound by its terms.
 */

#include <QComboBox>
#include <QStandardItemModel>
#include <QtScript>

#include "numbergencombobox.h"
#include "xt.h"

#define DEBUG false

// prototype for reimplementing XComboBox using Qt's model/view.

NumberGenComboBox::NumberGenComboBox(QWidget *pParent) :
  QComboBox(pParent),
  _allowedMethods(Manual | Automatic | Override | Shared)
{
  setAllowedMethods(_allowedMethods);
  connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(handleIndexChanged(int)));
}

void NumberGenComboBox::append(NumberGenComboBox::GenMethod id,
                               QString text,
                               QString code)
{
  int index = count();
  insertItem(index, text);
  setItemData(index, id, Xt::IdRole);
  setItemData(index, code.isEmpty() ? text : code, Xt::RawRole);
}

NumberGenComboBox::~NumberGenComboBox()
{
}

NumberGenComboBox::GenMethods NumberGenComboBox::allowedMethods() const
{
  return _allowedMethods;
}

QString NumberGenComboBox::automaticText() const
{
  int i = indexOfMethod(Automatic);
  return (i >= 0) ? itemText(i) : QString();
}

QString NumberGenComboBox::manualText() const
{
  int i = indexOfMethod(Manual);
  return (i >= 0) ? itemText(i) : QString();
}

// equivalent to XComboBox::id()
NumberGenComboBox::GenMethod NumberGenComboBox::method() const
{
  return (GenMethod)itemData(currentIndex(), Xt::IdRole).toInt();
}

// equivalent to XComboBox::code()
QString NumberGenComboBox::methodCode() const
{
  return itemData(currentIndex(), Xt::RawRole).toString();
}

QString NumberGenComboBox::overrideText() const
{
  int i = indexOfMethod(Override);
  return (i >= 0) ? itemText(i) : QString();
}

QString NumberGenComboBox::sharedText() const
{
  int i = indexOfMethod(Shared);
  return (i >= 0) ? itemText(i) : QString();
}

QString NumberGenComboBox::methodToCode(const GenMethod method)
{
  QString result;
  switch (method)
  {
    case Manual:    result = "M";    break;
    case Automatic: result = "A";    break;
    case Override:  result = "O";    break;
    case Shared:    result = "S";    break;
  }
  return result;
}

void NumberGenComboBox::handleIndexChanged(const int /*index*/)
{
  emit newMethod(method());
  emit newMethod((int)method());
  emit newMethodCode(methodCode());
}

void NumberGenComboBox::setAllowedMethods(const GenMethods methods)
{
  if (DEBUG)
    qDebug("%s::setAllowedMethods(0x%x) entered",
           qPrintable(objectName()), (int)methods);

  clear();

  if (methods & (int)Manual)
    append(Manual, tr("Manual"), methodToCode(Manual));
  if (methods & (int)Automatic)
    append(Automatic, tr("Automatic"), methodToCode(Automatic));
  if (methods & (int)Override)
    append(Override, tr("Automatic With Override"), methodToCode(Override));
  if (methods & (int)Shared)
    append(Shared, tr("Shared"), methodToCode(Shared));

  _allowedMethods = methods;
  
  if (DEBUG)
    qDebug("%s::setAllowedMethods(%x) returning",
           qPrintable(objectName()), (int)methods);
}

void NumberGenComboBox::setAutomaticText(const QString text)
{
  int i = indexOfMethod(Automatic);
  if (i >= 0)
    setItemText(i, text);
}

void NumberGenComboBox::setManualText(const QString text)
{
  int i = indexOfMethod(Manual);
  if (i >= 0)
    setItemText(i, text);
}

// this is equivalent to XComboBox::setId()
void NumberGenComboBox::setMethod(const NumberGenComboBox::GenMethod method)
{
  int i = indexOfMethod(method);
  setCurrentIndex(i);
}

void NumberGenComboBox::setMethod(const QString method)
{
  int index = -1;

  for (int i = 0; i < count(); i++)
    if (itemData(i, Xt::RawRole).toString() == method)
    {
      index = i;
      break;
    }

  setCurrentIndex(index);
}

void NumberGenComboBox::setOverrideText(const QString text)
{
  int i = indexOfMethod(Override);
  if (i >= 0)
    setItemText(i, text);
}

void NumberGenComboBox::setSharedText(const QString text)
{
  int i = indexOfMethod(Shared);
  if (i >= 0)
    setItemText(i, text);
}

int NumberGenComboBox::indexOfMethod(const NumberGenComboBox::GenMethod method) const
{
  for (int i = 0; i < count(); i++)
    if ((GenMethod)itemData(i, Xt::IdRole).toInt() == method)
      return i;
  return -1;
}

// script exposure ////////////////////////////////////////////////////////////

void NumberGenComboBoxFromScriptValue(const QScriptValue &obj,
                                      NumberGenComboBox * &item)
{
  item = qobject_cast<NumberGenComboBox*>(obj.toQObject());
}

QScriptValue NumberGenComboBoxToScriptValue(QScriptEngine *engine,
                                            NumberGenComboBox *const &item)
{
  return engine->newQObject(item);
}

QScriptValue constructNumberGenComboBox(QScriptContext *context,
                                        QScriptEngine  *engine)
{
  NumberGenComboBox *obj = 0;

  if (context->argumentCount() >= 1 &&
      qscriptvalue_cast<QWidget*>(context->argument(0)))
    obj = new NumberGenComboBox(qscriptvalue_cast<QWidget*>(context->argument(0)));
  else
    obj = new NumberGenComboBox();

  return engine->toScriptValue(obj);
}

#define CONST(a)    QScriptValue(engine, a)
#define CONSTFLAGS  QScriptValue::ReadOnly | QScriptValue::Undeletable

void setupNumberGenComboBox(QScriptEngine *engine)
{
  QScriptValue glob = engine->newFunction(constructNumberGenComboBox);

  qScriptRegisterMetaType(engine, NumberGenComboBoxToScriptValue, NumberGenComboBoxFromScriptValue);

  glob.setProperty("Manual",    CONST(NumberGenComboBox::Manual),    CONSTFLAGS);
  glob.setProperty("Automatic", CONST(NumberGenComboBox::Automatic), CONSTFLAGS);
  glob.setProperty("Override",  CONST(NumberGenComboBox::Override),  CONSTFLAGS);
  glob.setProperty("Shared",    CONST(NumberGenComboBox::Shared),    CONSTFLAGS);

  engine->globalObject().setProperty("NumberGenComboBox", glob, CONSTFLAGS);
}