File: characteristicswidget.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 (235 lines) | stat: -rw-r--r-- 7,725 bytes parent folder | download | duplicates (2)
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
230
231
232
233
234
235
/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2015 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 "characteristicswidget.h"

#include <QVariant>
#include <QtScript>

#include "errorReporter.h"
#include "characteristicAssignment.h"

class CharacteristicsWidgetPrivate
{
  public:
    int                    id;
    QString                type;
    QString                paramName;
    CharacteristicsWidget *parent;
    bool                   readOnly;

    CharacteristicsWidgetPrivate(CharacteristicsWidget *p)
      : id(-1),
        parent(p),
        readOnly(false)
    {
    }
};

CharacteristicsWidget::CharacteristicsWidget(QWidget* parent, const char* name, QString type, int id)
    : QWidget(parent),
      _d(0)
{
  setupUi(this);
  if (name) setObjectName(name);
  _d = new CharacteristicsWidgetPrivate(this);

  connect(_newCharacteristic,    SIGNAL(clicked()), this, SLOT(sNew()));
  connect(_editCharacteristic,   SIGNAL(clicked()), this, SLOT(sEdit()));
  connect(_deleteCharacteristic, SIGNAL(clicked()), this, SLOT(sDelete()));

  connect(_charass, SIGNAL(valid(bool)), _editCharacteristic,   SLOT(setEnabled(bool)));
  connect(_charass, SIGNAL(valid(bool)), _deleteCharacteristic, SLOT(setEnabled(bool)));
  connect(_charass, SIGNAL(itemSelected(int)), _editCharacteristic, SLOT(animateClick()));

  _charass->addColumn(tr("Characteristic"), _itemColumn,   Qt::AlignLeft,   true, "char_name" );
  _charass->addColumn(tr("Value"),          -1,            Qt::AlignLeft,   true, "charass_value" );
  _charass->addColumn(tr("Default"),        _ynColumn*2,   Qt::AlignCenter, true, "charass_default" );

  if (! type.isEmpty()) setType(type);
  if (id >= 0)          setId(id);
}

CharacteristicsWidget::~CharacteristicsWidget()
{
  if (_d)
  {
    delete _d;
    _d = 0;
  }
}

void CharacteristicsWidget::languageChange()
{
  retranslateUi(this);
}

void CharacteristicsWidget::setReadOnly(bool newState)
{
  bool prevState = _d->readOnly;

  _d->readOnly = newState;
  _newCharacteristic->setEnabled(! newState);
  _editCharacteristic->setEnabled(! newState && _charass->id() >= 0);
  _deleteCharacteristic->setEnabled(! newState && _charass->id() >= 0);
  if (prevState != newState)
  {
    if (newState)
    {
      disconnect(_charass, SIGNAL(valid(bool)), _editCharacteristic,   SLOT(setEnabled(bool)));
      disconnect(_charass, SIGNAL(valid(bool)), _deleteCharacteristic, SLOT(setEnabled(bool)));
      disconnect(_charass, SIGNAL(itemSelected(int)), _editCharacteristic, SLOT(animateClick()));
    }
    else
    {
      connect(_charass, SIGNAL(valid(bool)), _editCharacteristic,   SLOT(setEnabled(bool)));
      connect(_charass, SIGNAL(valid(bool)), _deleteCharacteristic, SLOT(setEnabled(bool)));
      connect(_charass, SIGNAL(itemSelected(int)), _editCharacteristic, SLOT(animateClick()));
    }
  }
}

void CharacteristicsWidget::setType(QString doctype)
{
  QString prevtype = _d->type;
  XSqlQuery q;
  q.prepare("select * from source where source_charass = :charass;");
  q.bindValue(":charass", doctype);
  q.exec();
  if (q.first())
  {
    _d->paramName = q.value("source_key_param").toString();
    _d->type = doctype;
  }
  if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Retrieving Document Type"),
                           q, __FILE__, __LINE__))
  {
    _d->type = "";
  }
  emit valid(isValid());
  if (_d->type != prevtype) emit newType(_d->type);
}

void CharacteristicsWidget::setId(int id)
{
  int previd = _d->id;
  _d->id = (id < -1) ? -1 : id;
  sFillList();
  emit valid(isValid());
  if (_d->id != previd) emit newId(_d->id);
}

bool CharacteristicsWidget::isValid() const
{
  return (_d->id >= 0 && ! _d->type.isEmpty());
}

void CharacteristicsWidget::sNew()
{
  ParameterList params;
  params.append("mode", "new");
  params.append(_d->paramName, _d->id);

  characteristicAssignment newdlg(this, "", true);
  newdlg.set(params);

  if (newdlg.exec() != QDialog::Rejected)
    sFillList();
}

void CharacteristicsWidget::sEdit()
{
  ParameterList params;
  params.append("mode", "edit");
  params.append("charass_id", _charass->id());

  characteristicAssignment newdlg(this, "", true);
  newdlg.set(params);

  if (newdlg.exec() != QDialog::Rejected)
    sFillList();
}

void CharacteristicsWidget::sDelete()
{
  XSqlQuery q;
  q.prepare("DELETE FROM charass WHERE charass_id = :charass_id;");
  q.bindValue(":charass_id", _charass->id());
  q.exec();
  ErrorReporter::error(QtCriticalMsg, this, tr("Error Deleting Characteristic"),
                       q, __FILE__, __LINE__);

  sFillList();
}

void CharacteristicsWidget::sFillList()
{
  XSqlQuery q;
  q.prepare( "SELECT charass_id, char_name,"
             "       CASE WHEN char_type < 2 THEN charass_value"
             "            ELSE formatDate(charass_value::date)"
             "       END AS charass_value,"
             "       charass_default"
             "  FROM charass JOIN char ON charass_char_id = char_id"
             " WHERE charass_target_type = :type"
             "   AND charass_target_id   = :id"
             " ORDER BY char_order, char_name;" );
  q.bindValue(":type", _d->type);
  q.bindValue(":id",   _d->id);
  q.exec();
  _charass->populate(q);
  ErrorReporter::error(QtCriticalMsg, this, tr("Error Retrieving Characteristics"),
                       q, __FILE__, __LINE__);
}

// scripting exposure /////////////////////////////////////////////////////////

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

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

QScriptValue constructCharacteristicsWidget(QScriptContext *context,
                                            QScriptEngine  *engine)
{
  QWidget *parent = (qscriptvalue_cast<QWidget*>(context->argument(0)));
  if (context->argumentCount() == 0)
    return engine->toScriptValue(new CharacteristicsWidget());
  if (context->argumentCount() == 1)
    return engine->toScriptValue(new CharacteristicsWidget(parent, "_characteristicsWidget"));
  if (context->argumentCount() == 2)
    return engine->toScriptValue(new CharacteristicsWidget(parent,
                            context->argument(1).toString().toLatin1().data()));
  if (context->argumentCount() == 3)
    return engine->toScriptValue(new CharacteristicsWidget(parent,
                            context->argument(1).toString().toLatin1().data(),
                            context->argument(2).toString()));
  return engine->toScriptValue(new CharacteristicsWidget(parent,
                          context->argument(1).toString().toLatin1().data(),
                          context->argument(2).toString(),
                          context->argument(2).toInteger()));
}

void setupCharacteristicsWidget(QScriptEngine *engine)
{
  QScriptValue::PropertyFlags stdflags = QScriptValue::ReadOnly |
                                         QScriptValue::Undeletable;

  qScriptRegisterMetaType(engine, CharacteristicsWidgettoScriptValue,
                          CharacteristicsWidgetfromScriptValue);

  QScriptValue constructor = engine->newFunction(constructCharacteristicsWidget);
  engine->globalObject().setProperty("CharacteristicsWidget", constructor, stdflags);
}