File: xdoccopysetter.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 (213 lines) | stat: -rw-r--r-- 6,849 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
/*
 * 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 "xdoccopysetter.h"

#include <QtScript>

#include "editwatermark.h"

static QString yesStr = QT_TRANSLATE_NOOP("XDocCopySetter", "Yes");
static QString noStr  = QT_TRANSLATE_NOOP("XDocCopySetter", "No");

/**
  @class XDocCopySetter

  @brief The XDocCopySetter gives the %user a unified interface for telling
         the system how many copies of a particular document should be printed
         when it gets sent to a printer, what the watermark should be on the
         report, and whether the report should show prices or not.

  @see printInvoice

 */

XDocCopySetter::XDocCopySetter(QWidget *parent, const char *pName) :
  QWidget(parent)
{
  setupUi(this);

  if(pName)
    setObjectName(pName);

  connect(_watermarks,  SIGNAL(itemSelected(int)), this, SLOT(sEditWatermark()));
  connect(_numOfCopies, SIGNAL(valueChanged(int)), this, SLOT(setNumCopies(int)));

  _watermarks->addColumn(tr("Copy #"),      80, Qt::AlignRight);
  _watermarks->addColumn(tr("Watermark"),   -1, Qt::AlignLeft);
  _watermarks->addColumn(tr("Show Prices"), 80, Qt::AlignCenter);
}

XDocCopySetter::~XDocCopySetter()
{
}

void XDocCopySetter::languageChange()
{
}

QString XDocCopySetter::labelText()       const { return _copiesGroup->title();}
int     XDocCopySetter::numCopies()       const { return _numOfCopies->value();}
QString XDocCopySetter::numCopiesMetric() const { return _numCopiesMetric;     }
QString XDocCopySetter::showPriceMetric() const { return _showPriceMetric;     }
QString XDocCopySetter::watermarkMetric() const { return _watermarkMetric;     }

bool XDocCopySetter::showCosts(const int row) const
{
  return (_watermarks->topLevelItem(row)->text(2) == yesStr);
}

QString XDocCopySetter::watermark(const int row) const
{
  return _watermarks->topLevelItem(row)->text(1);
}

bool XDocCopySetter::save()
{
  if (_x_metrics)
  {
    if (! _numCopiesMetric.isEmpty())
      _x_metrics->set(_numCopiesMetric, _numOfCopies->value());

    QString fullmetric("%1%2");
    for (int i = 0; i < _watermarks->topLevelItemCount(); i++)
    {
      XTreeWidgetItem *item = _watermarks->topLevelItem(i);
      if (! _watermarkMetric.isEmpty())
        _x_metrics->set(fullmetric.arg(_watermarkMetric).arg(i), item->text(1));
      if (! _showPriceMetric.isEmpty())
        _x_metrics->set(fullmetric.arg(_showPriceMetric).arg(i), (item->text(2) == yesStr));
    }
  }
  return true;
}

void XDocCopySetter::setLabelText(const QString text)
{
  _copiesGroup->setTitle(text);
}

void XDocCopySetter::setNumCopies(const int numCopies)
{
  _numOfCopies->setValue(numCopies);

  while (_watermarks->topLevelItemCount() > numCopies)
    _watermarks->takeTopLevelItem(_watermarks->topLevelItemCount() - 1);

  for (int i = _watermarks->topLevelItemCount(); i < numCopies; i++)
  {
    QString watermark = _watermarkMetric.isEmpty() ? ""
             : _x_metrics->value(QString("%1%2").arg(_watermarkMetric).arg(i));
    QString showprice;
    if (_showPriceMetric.isEmpty() ||
        _x_metrics->boolean(QString("%1%2").arg(_showPriceMetric).arg(i)) ||
        _x_metrics->value(QString("%1%2").arg(_showPriceMetric).arg(i)).isEmpty())
      showprice = yesStr;
    else
      showprice = noStr;
    new XTreeWidgetItem(_watermarks,
                        _watermarks->topLevelItem(_watermarks->topLevelItemCount() - 1),
                        i, i, tr("Copy #%1").arg(i + 1), watermark, showprice);
  }
}

void XDocCopySetter::setNumCopiesMetric(const QString metric)
{
  _numCopiesMetric = metric;

  // _numCopies->setValue cascades to setNumCopies
  if (! _numCopiesMetric.isEmpty() && _x_metrics)
    _numOfCopies->setValue(_x_metrics->value(metric).toInt());
}

void XDocCopySetter::setShowPriceMetric(const QString metric)
{
  _showPriceMetric = metric;

  if (! _showPriceMetric.isEmpty() && _x_metrics)
  {
    for (int i = 0; i < _watermarks->topLevelItemCount(); i++)
    {
      QString fullmetric = QString("%1%2").arg(metric).arg(i);
      if (_x_metrics->value(fullmetric).isEmpty() ||
          _x_metrics->boolean(fullmetric))
        _watermarks->topLevelItem(i)->setText(2, yesStr);
      else
        _watermarks->topLevelItem(i)->setText(2, noStr);
    }
  }
}

void XDocCopySetter::setWatermarkMetric(const QString metric)
{
  _watermarkMetric = metric;

  if (! _watermarkMetric.isEmpty() && _x_metrics)
  {
    for (int i = 0; i < _watermarks->topLevelItemCount(); i++)
    {
      QString fullmetric = QString("%1%2").arg(metric).arg(i);
      if (! _x_metrics->value(fullmetric).isEmpty())
        _watermarks->topLevelItem(i)->setText(1, _x_metrics->value(fullmetric));
    }
  }
}

void XDocCopySetter::sEditWatermark()
{
  QTreeWidgetItem *cursor = _watermarks->currentItem();
  ParameterList params;
  params.append("watermark",  cursor->text(1));
  params.append("showPrices", (cursor->text(2) == yesStr));

  EditWatermark newdlg(this);
  newdlg.set(params);
  if (newdlg.exec() == QDialog::Accepted)
  {
    cursor->setText(1, newdlg.watermark());
    cursor->setText(2, ((newdlg.showPrices()) ? yesStr : noStr));
  }
}

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

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

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

QScriptValue constructXDocCopySetter(QScriptContext *context,
                                       QScriptEngine  *engine)
{
  QWidget *parent = (qscriptvalue_cast<QWidget*>(context->argument(0)));
  const char *objname = "_xDocCopySetter";
  if (context->argumentCount() > 1)
    objname = context->argument(1).toString().toLatin1().data();
  return engine->toScriptValue(new XDocCopySetter(parent, objname));
}

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

  qScriptRegisterMetaType(engine, XDocCopySettertoScriptValue,
                          XDocCopySetterfromScriptValue);

  QScriptValue constructor = engine->newFunction(constructXDocCopySetter);
  engine->globalObject().setProperty("XDocCopySetter", constructor, stdflags);

  //constructor.setProperty("ChangeFuture", QScriptValue(engine, XDocCopySetter::ChangeFuture), stdflags);
}