File: imageAssignment.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 (234 lines) | stat: -rw-r--r-- 6,235 bytes parent folder | download | duplicates (4)
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
/*
 * 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 <QVariant>

#include "imageAssignment.h"
#include "imageview.h"

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

  setObjectName(name ? name : "imageAssignment");
  setModal(modal);

  connect(_image, SIGNAL(valid(bool)), _save, SLOT(setEnabled(bool)));
  connect(_close, SIGNAL(clicked()), this, SLOT(reject()));
  connect(_save, SIGNAL(clicked()), this, SLOT(sSave()));
  connect(_new, SIGNAL(clicked()), this, SLOT(sNew()));
  connect(_view, SIGNAL(clicked()), this, SLOT(sView()));

  _image->addColumn(tr("Name"),        _itemColumn, Qt::AlignLeft, true, "image_name" );
  _image->addColumn(tr("Description"), -1,          Qt::AlignLeft, true, "image_descrip" );

  _imageassid = -1;
  _mode = cNew;
  _sourceid = -1;

  sFillList();
}

imageAssignment::~imageAssignment()
{
  // no need to delete child widgets, Qt does it all for us
}

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

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

  param = pParams.value("sourceType", &valid);
  if (valid)
  {
    _sourcetype = param.toString();
    if (_sourcetype != "I") // Item
    {
      _purpose->setCurrentIndex(3);
      _purpose->hide();
      _purposeLit->hide();
    }
  }

  param = pParams.value("source_id", &valid);
  if (valid)
    _sourceid = param.toInt();

  param = pParams.value("imageass_id", &valid);
  if (valid)
  {
    _imageassid = param.toInt();
    populate();
  }

  param = pParams.value("mode", &valid);
  if (valid)
  {
    if (param.toString() == "new")
    {
      _mode = cNew;
      _purpose->setFocus();
    }
    else if (param.toString() == "edit")
    {
      _mode = cEdit;
      _save->setFocus();
      _purpose->setEnabled(false);
    }
    else if (param.toString() == "view")
    {
      _mode = cView;
      _save->hide();
      _new->hide();
      _purpose->setEnabled(false);
      _image->setEnabled(false);
    }
  }
}

void imageAssignment::sSave()
{
  QString purpose;
  if (_purpose->currentIndex() == 0)  
    purpose = "I";
  if (_purpose->currentIndex() == 1)  
    purpose = "P";
  if (_purpose->currentIndex() == 2)  
    purpose = "E";
  if (_purpose->currentIndex() == 3)  
    purpose = "M";

  XSqlQuery newImage;

  if (_mode == cNew)
  {
    _imageassid = -1;

    if (purpose != "M")
    {
      XSqlQuery imageassid;
      imageassid.prepare( "SELECT imageass_id "
                           "FROM imageass "
                           "WHERE ( (imageass_source_id=:source_id)"
                           " AND ( imageass_source=:source) "
                           " AND (imageass_purpose=:imageass_purpose) );" );
      imageassid.bindValue(":source",    _sourcetype);
      imageassid.bindValue(":source_id", _sourceid);
      imageassid.bindValue(":imageass_purpose", purpose);
      imageassid.exec();
      if (imageassid.first())
      {
        _imageassid = imageassid.value("imageass_id").toInt();
        _mode = cEdit;
      }
    }
      
    if (_imageassid == -1)
    {
      XSqlQuery imageassid;
      imageassid.prepare("SELECT NEXTVAL('docass_docass_id_seq') AS _imageass_id;");
      imageassid.exec();
      if (imageassid.first())
        _imageassid = imageassid.value("_imageass_id").toInt();
    }

    newImage.prepare( "INSERT INTO imageass "
                      "( imageass_id, imageass_source, imageass_source_id, imageass_purpose, imageass_image_id ) "
                      "VALUES "
                      "( :imageass_id, :imageass_source, :imageass_source_id, :imageass_purpose, :imageass_image_id );" );
  }

  if (_mode == cEdit)
    newImage.prepare( "UPDATE imageass "
                      "SET imageass_purpose=:imageass_purpose, imageass_image_id=:imageass_image_id "
                      "WHERE (imageass_id=:imageass_id);" );

  newImage.bindValue(":imageass_id", _imageassid);
  newImage.bindValue(":imageass_source",    _sourcetype);
  newImage.bindValue(":imageass_source_id", _sourceid);
  newImage.bindValue(":imageass_image_id", _image->id());
  newImage.bindValue(":imageass_purpose", purpose);

  newImage.exec();

  done(_imageassid);
}

void imageAssignment::sNew()
{
  ParameterList params;
  params.append("mode", "new");

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

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

void imageAssignment::sView()
{
  ParameterList params;
  params.append("mode", "view");
  params.append("image_id", _image->id());

  imageview newdlg(this, "", true);
  newdlg.set(params);
  newdlg.exec();
}

void imageAssignment::sFillList()
{
  _image->populate( "SELECT image_id, image_name, firstLine(image_descrip) AS image_descrip "
                    "FROM image "
                    "ORDER BY image_name;" );
}

void imageAssignment::populate()
{
  XSqlQuery imageass;
  imageass.prepare( "SELECT imageass_purpose, imageass_image_id, imageass_source "
                     "FROM imageass "
                     "WHERE (imageass_id=:imageass_id);" );
  imageass.bindValue(":imageass_id", _imageassid);
  imageass.exec();
  if (imageass.first())
  {
    if (imageass.value("imageass_source").toString() == "I")
    {
      QString purpose = imageass.value("imageass_purpose").toString();

      if (purpose == "I")
        _purpose->setCurrentIndex(0);
      else if (purpose == "P")
        _purpose->setCurrentIndex(1);
      else if (purpose == "E")
        _purpose->setCurrentIndex(2);
      else if (purpose == "M")
        _purpose->setCurrentIndex(3);
    }
    else
    {
      _purpose->setCurrentIndex(3);
      _purpose->hide();
      _purposeLit->hide();
    }

    _image->setId(imageass.value("imageass_image_id").toInt());
  }
}