File: comment.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 (380 lines) | stat: -rwxr-xr-x 12,123 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * 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 <QHBoxLayout>
#include <QDialogButtonBox>
#include <QLabel>
#include <QLayout>
#include <QMessageBox>
#include <QPushButton>
#include <QSqlError>
#include <QToolTip>
#include <QVBoxLayout>
#include <QVariant>
#include <QWhatsThis>
#include <QVector>
#include <QCheckBox>

#include <parameter.h>

#include "comment.h"
#include "shortcuts.h"
#include "xcombobox.h"
#include "xtextedit.h"
#include "xtsettings.h"

#define cNew  1
#define cEdit 2
#define cView 3

comment::comment( QWidget* parent, const char* name, bool modal, Qt::WindowFlags fl ) :
  QDialog( parent, fl )
{
  setupUi(this);

  if (name)
    setObjectName(name);
  if(modal)
    setWindowModality(Qt::WindowModal);

  _commentid = -1;
  _targetId = -1;
  _mode = cNew;

  if(!(_x_metrics && _x_metrics->boolean("CommentPublicPrivate")))
    _public->hide();
  _public->setChecked(_x_metrics && _x_metrics->boolean("CommentPublicDefault"));

  _comment->setSpellEnable(true);

  _close = buttonBox->button(QDialogButtonBox::Cancel);
  _close->setObjectName("_close");

  _save = buttonBox->button(QDialogButtonBox::Save);
  _save->setObjectName("_save");

  _prev = buttonBox->addButton(tr("&Previous"), QDialogButtonBox::ActionRole);
  _prev->setObjectName("_prev");

  _next = buttonBox->addButton(tr("&Next"), QDialogButtonBox::ActionRole);
  _next->setObjectName("_next");

  _more = buttonBox->addButton(tr("&More"), QDialogButtonBox::ActionRole);
  _more->setObjectName("_more");
  _more->setCheckable(true);

  _comments->setReadOnly(true);
  _comments->findChild<XCheckBox*>("_verbose")->setForgetful(true);
  _comments->findChild<XCheckBox*>("_verbose")->hide();
  _comments->findChild<XCheckBox*>("_verbose")->setChecked(false);
  _comments->_newComment->setVisible(false);
  _comments->setVerboseCommentList(true);
  _comments->setVisible(false);
  _comments->setEditable(false);

  connect(buttonBox, SIGNAL(accepted()), this, SLOT(sSave()));
  connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
  connect(_next, SIGNAL(clicked()), this, SLOT(sNextComment()));
  connect(_prev, SIGNAL(clicked()), this, SLOT(sPrevComment()));
  connect(_more, SIGNAL(toggled(bool)), _comments, SLOT(setVisible(bool)));

  _sourcetype = "";

  shortcuts::setStandardKeys(this);

  QSize savedSize = xtsettingsValue("comment-" + objectName() + "/geometry/size").toSize();
  if (savedSize.isValid())
    resize(savedSize);
  connect(this, SIGNAL(finished(int)), this, SLOT(saveSize()));
}

// ::closeEvent() would be better but it didn't get called
void comment::saveSize()
{
  xtsettingsSetValue("comment-" + objectName() + "/geometry/size", size());
}

void comment::set(const ParameterList &pParams)
{
  QVariant param;
  bool     valid;

  param = pParams.value("comment_id", &valid);
  if (valid)
  {
    _cmnttype->setType(XComboBox::AllCommentTypes);
    _commentid = param.toInt();
    populate();
  }

  param = pParams.value("commentIDList", &valid);
  if (valid)
  {
    _commentIDList = param.toList();
    _commentLocation = _commentIDList.indexOf(_commentid);

    if((_commentLocation-1) >= 0)
      _prev->setEnabled(true); 
    else
      _prev->setEnabled(false); 

    if((_commentLocation+1) < _commentIDList.size())
      _next->setEnabled(true); 
    else
      _next->setEnabled(false); 
  }

  param = pParams.value("mode", &valid);
  if (valid)
  {
    if (param.toString() == "new")
    {
      _mode = cNew;
      
      _cmnttype->setFocus();
      _next->setVisible(false);
      _prev->setVisible(false);
    }
    else if (param.toString() == "edit")
    {
      _mode = cEdit;
      _next->setVisible(false);
      _prev->setVisible(false);
      _cmnttype->setEnabled(false);
    }
    else if (param.toString() == "view")
    {
      _mode = cView;
      _next->setVisible(true);
      _prev->setVisible(true);
      _cmnttype->setEnabled(false);
      _comment->setReadOnly(true);
      _save->hide();
      _close->setText(tr("&Close"));
      _close->setShortcut(QKeySequence::Close);
      _more->hide();

      _close->setFocus();
    }
  }

  param = pParams.value("sourceType", &valid);
  if (valid)
  {
    _sourcetype = param.toString();
    if(!(_mode == cEdit || _mode == cView))
    {
      //      CommentMap *map = Comments::commentMap().value(_sourcetype);
      //      _cmnttype->setType(map ? (XComboBox::XComboBoxTypes)map->doctypeId : XComboBox::AllCommentTypes);
      if (_sourcetype == "ADDR")
        _cmnttype->setType(XComboBox::AddressCommentTypes);
      else if (_sourcetype == "BBH")
        _cmnttype->setType(XComboBox::BBOMHeadCommentTypes);
      else if (_sourcetype == "BBI")
        _cmnttype->setType(XComboBox::BBOMItemCommentTypes);
      else if (_sourcetype == "BMH")
        _cmnttype->setType(XComboBox::BOMHeadCommentTypes);
      else if (_sourcetype == "BMI")
        _cmnttype->setType(XComboBox::BOMItemCommentTypes);
      else if (_sourcetype == "BOH")
        _cmnttype->setType(XComboBox::BOOHeadCommentTypes);
      else if (_sourcetype == "BOI")
        _cmnttype->setType(XComboBox::BOOItemCommentTypes);
      else if (_sourcetype == "CRMA")
        _cmnttype->setType(XComboBox::CRMAccountCommentTypes);
      else if (_sourcetype == "T")
        _cmnttype->setType(XComboBox::ContactCommentTypes);
      else if (_sourcetype == "C")
        _cmnttype->setType(XComboBox::CustomerCommentTypes);
      else if (_sourcetype == "EMP")
        _cmnttype->setType(XComboBox::EmployeeCommentTypes);
      else if (_sourcetype == "FX")
        _cmnttype->setType(XComboBox::ExchangeRateCommentTypes);
      else if (_sourcetype == "INCDT")
        _cmnttype->setType(XComboBox::IncidentCommentTypes);
      else if (_sourcetype == "I")
        _cmnttype->setType(XComboBox::ItemCommentTypes);
      else if (_sourcetype == "IS")
        _cmnttype->setType(XComboBox::ItemSiteCommentTypes);
      else if (_sourcetype == "IR")
        _cmnttype->setType(XComboBox::ItemSourceCommentTypes);
      else if (_sourcetype == "L")
        _cmnttype->setType(XComboBox::LocationCommentTypes);
      else if (_sourcetype == "LS")
        _cmnttype->setType(XComboBox::LotSerialCommentTypes);
      else if (_sourcetype == "OPP")
        _cmnttype->setType(XComboBox::OpportunityCommentTypes);
      else if (_sourcetype == "J")
        _cmnttype->setType(XComboBox::ProjectCommentTypes);
      else if (_sourcetype == "P")
        _cmnttype->setType(XComboBox::PurchaseOrderCommentTypes);
      else if (_sourcetype == "PI")
        _cmnttype->setType(XComboBox::PurchaseOrderItemCommentTypes);
      else if (_sourcetype == "RA")
        _cmnttype->setType(XComboBox::ReturnAuthCommentTypes);
      else if (_sourcetype == "RI")
        _cmnttype->setType(XComboBox::ReturnAuthItemCommentTypes);
      else if (_sourcetype == "Q")
        _cmnttype->setType(XComboBox::QuoteCommentTypes);
      else if (_sourcetype == "QI")
        _cmnttype->setType(XComboBox::QuoteItemCommentTypes);
      else if (_sourcetype == "S")
        _cmnttype->setType(XComboBox::SalesOrderCommentTypes);
      else if (_sourcetype == "SI")
        _cmnttype->setType(XComboBox::SalesOrderItemCommentTypes);
      else if (_sourcetype == "TA")
        _cmnttype->setType(XComboBox::TaskCommentTypes);
      else if (_sourcetype == "TATC")
        _cmnttype->setType(XComboBox::TimeAttendanceCommentTypes);
      else if (_sourcetype == "TD")
        _cmnttype->setType(XComboBox::TodoItemCommentTypes);
      else if (_sourcetype == "TO")
        _cmnttype->setType(XComboBox::TransferOrderCommentTypes);
      else if (_sourcetype == "TI")
        _cmnttype->setType(XComboBox::TransferOrderItemCommentTypes);
      else if (_sourcetype == "V")
        _cmnttype->setType(XComboBox::VendorCommentTypes);
      else if (_sourcetype == "WH")
        _cmnttype->setType(XComboBox::WarehouseCommentTypes);
      else if (_sourcetype == "W")
        _cmnttype->setType(XComboBox::WorkOrderCommentTypes);
      else
        _cmnttype->setType(XComboBox::AllCommentTypes);
    }
  }

  param = pParams.value("source_id", &valid);
  if (valid)
  {
    _targetId = param.toInt();
  }

  _comments->setType(_sourcetype);
  _comments->setId(_targetId);
}

void comment::sSave()
{
  if (_cmnttype->id() == -1)
  {
    QMessageBox::critical( this, tr("Cannot Post Comment"),
                           tr("<p>You must select a Comment Type for this "
                              "Comment before you may post it.") );
    _cmnttype->setFocus();
    return;
  }

  int result = -1;
  if(_mode == cNew)
  {
    _query.prepare("SELECT postComment(:cmnttype_id, :source_type, :source_id, :text, :public) AS result;");
    _query.bindValue(":cmnttype_id", _cmnttype->id());
    _query.bindValue(":source_type", _sourcetype);
    _query.bindValue(":source_id", _targetId);
    _query.bindValue(":text", _comment->toPlainText().trimmed());
    _query.bindValue(":public", _public->isChecked());
    _query.exec();
    if (_query.first())
    {
      int result = _query.value("result").toInt();
      if (result < 0)
      {
        QMessageBox::critical(this, tr("Cannot Post Comment"),
                              tr("<p>A Stored Procedure failed to run "
                                 "properly.<br>(%1, %2)<br>")
                                .arg("postComment").arg(result));
      }
    }
    else if (_query.lastError().type() != QSqlError::NoError)
    {
      QMessageBox::critical(this, tr("Cannot Post Comment"),
                            _query.lastError().databaseText());
    }
  }
  else if(_mode == cEdit)
  {
    result = _commentid;
    _query.prepare("UPDATE comment SET comment_text=:text, comment_public=:public WHERE comment_id=:comment_id");
    _query.bindValue(":text", _comment->toPlainText().trimmed());
    _query.bindValue(":comment_id", _commentid);
    _query.bindValue(":public", _public->isChecked());
    _query.exec();
    if(_query.lastError().type() != QSqlError::NoError)
    {
      QMessageBox::critical(this, tr("Cannot Post Comment"),
                            _query.lastError().databaseText());
      return;
    }
  }

  if(result < 0)
    done(result);
  else
    reject();
}

void comment::populate()
{
  _query.prepare( "SELECT comment_cmnttype_id, comment_text, comment_public "
                  "FROM comment "
                  "WHERE (comment_id=:comment_id);" );
  _query.bindValue(":comment_id", _commentid);
  _query.exec();
  if (_query.first())
  {
    _cmnttype->setId(_query.value("comment_cmnttype_id").toInt());
    _comment->setText(_query.value("comment_text").toString());
    _public->setChecked(_query.value("comment_public").toBool());
  }
  else if (_query.lastError().type() != QSqlError::NoError)
  {
    QMessageBox::critical(this, tr("Error Selecting Comment"),
                          _query.lastError().databaseText());
    return;
  }
}

void comment::sNextComment()
{
  if((_commentLocation+1) < _commentIDList.size())
  {
    _commentLocation++;
    _commentid = _commentIDList[_commentLocation].toInt();
    populate();
    if((_commentLocation+1) < _commentIDList.size())
      _next->setEnabled(true); 
    else
      _next->setEnabled(false); 

    if((_commentLocation-1) >= 0)
      _prev->setEnabled(true); 
    else
      _prev->setEnabled(false); 
  }
}

void comment::sPrevComment()
{
  if((_commentLocation-1) >= 0)
  {
    _commentLocation--;
    _commentid = _commentIDList[_commentLocation].toInt();
    populate();
    if((_commentLocation-1) >= 0)
      _prev->setEnabled(true); 
    else
      _prev->setEnabled(false); 

    if((_commentLocation+1) < _commentIDList.size())
      _next->setEnabled(true); 
    else
      _next->setEnabled(false); 
  }
}