Debian Codegraph @

HercPrinter/DecolationDialog.cpp

raw symbols SCIP
1 #include "DecolationDialog.h"
2 #include "ui_DecolationDialog.h"
3 #include "DecolationRules.h"
4 #include "Trigger.h"
5 #include "Preferences.h"
6 #include "FieldTip.h"
7 #include "HerculesStudio.h"
8
9 #include <QMessageBox>
10
11 DecolationDialog::DecolationDialog(QWidget *parent, const QString & item) :
12 QDialog(parent),
13 ui(new Ui::DecolationDialog),
14 mSepModel(NULL),
15 mAccModel(NULL)
16 {
17 ui->setupUi(this);
18
19 ui->decolationNameComboBox->insertItems(0, Preferences::getInstance().decolationList());
20 mCurrentName = ui->decolationNameComboBox->currentText();
21
22 connect(ui->decolationNameComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(nameChanged(int)));
23 connect(ui->decolationNameComboBox, SIGNAL(editTextChanged(QString)), this, SLOT(nameEdited(QString)));
24 connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(deleteClicked()));
25 connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(cancelClicked()));
26 connect(ui->addSeperatorButton, SIGNAL(clicked()), this, SLOT(insertSeperator()));
27 connect(ui->deleteSeperatorButton, SIGNAL(clicked()), this, SLOT(deleteSeperator()));
28 connect(ui->addAccountingButton, SIGNAL(clicked()), this, SLOT(insertAccounting()));
29 connect(ui->deleteAccountingButton, SIGNAL(clicked()), this, SLOT(deleteAccounting()));
30 addSeperator();
31 addAccounting();
32
33 if (item.length() > 0)
34 {
35 ui->decolationNameComboBox->setCurrentText(item);
36 }
37 else
38 {
39 ui->decolationNameComboBox->setCurrentIndex(0);
40 }
41 populate();
42 if (!Preferences::getInstance().balloonDecolation() && builtinDecolation(ui->decolationNameComboBox->currentText()))
43 {
44 Preferences::getInstance().setBalloonDecolation(true);
45 FieldTip *baloon = new FieldTip(this, true);
46 baloon->setOwner(ui->decolationNameComboBox);
47 baloon->showMessage("Change this name to define you own decolation", 7000);
48 }
49 }
50
51 DecolationDialog::~DecolationDialog()
52 {
53 delete ui;
54 }
55
56 void DecolationDialog::cancelClicked()
57 {
58 if (!builtinDecolation(ui->decolationNameComboBox->currentText()))
59 save(ui->decolationNameComboBox->currentText());
60 deleteLater();
61 }
62
63 bool DecolationDialog::builtinDecolation(QString decName)
64 {
65 if ((decName == "mvs-legacy") ||
66 (decName == "mvs38"))
67 return true;
68 else
69 return false;
70 }
71
72 void DecolationDialog::deleteClicked()
73 {
74 QString decToDelete = ui->decolationNameComboBox->currentText();
75 Preferences::getInstance().deleteDecolation(ui->decolationNameComboBox->currentText());
76 mCurrentName = "";
77 ui->decolationNameComboBox->clear();
78 ui->decolationNameComboBox->insertItems(0,Preferences::getInstance().decolationList());
79 ui->decolationNameComboBox->setCurrentIndex(0);
80
81 }
82
83 void DecolationDialog::nameChanged(int)
84 {
85 save(mCurrentName);
86 mCurrentName = ui->decolationNameComboBox->currentText();
87 addSeperator();
88 addAccounting();
89 populate();
90 }
91
92 void DecolationDialog::nameEdited(QString name)
93 {
94 setProtected(builtinDecolation(name));
95 }
96
97 void DecolationDialog::populate()
98 {
99 QString name = ui->decolationNameComboBox->currentText();
100 DecolationRules * dec = Preferences::getInstance().decolation(name);
101 ui->decolationNameComboBox->setCurrentText(dec->name());
102 ui->endJobPagesSpinBox->setValue(dec->mSeperator.mPages);
103 ui->fileNameTemplate->setText(dec->filenameTemplate());
104 mSepModel->clear();
105 for (int i=0; i<dec->mSeperator.mTriggers.count(); i++)
106 {
107 mSepModel->insertRow(mSepModel->rowCount()-1, QModelIndex(),
108 dec->mSeperator.mTriggers[i].line(),
109 dec->mSeperator.mTriggers[i].column(),
110 dec->mSeperator.mTriggers[i].text(),
111 dec->mSeperator.mTriggers[i].triggerType());
112 }
113
114 mAccModel->clear();
115 for (int i=0; i<dec->mAccounting.mFields.count(); i++)
116 {
117 mAccModel->insertRow(mAccModel->rowCount()-1, QModelIndex(),
118 dec->mAccounting.mFields[i].mLine,
119 dec->mAccounting.mFields[i].mColumn,
120 dec->mAccounting.mFields[i].mLength,
121 dec->mAccounting.mFields[i].mName);
122 }
123 setProtected(builtinDecolation(name));
124 }
125
126 void DecolationDialog::setProtected(bool protect)
127 {
128 ui->endJobPagesSpinBox->setEnabled(!protect);
129 ui->fileNameTemplate->setEnabled(!protect);
130 ui->accountingTableView->setEnabled(!protect);
131 ui->seperatorTableView->setEnabled(!protect);
132 ui->addAccountingButton->setEnabled(!protect);
133 ui->deleteAccountingButton->setEnabled(!protect);
134 ui->addSeperatorButton->setEnabled(!protect);
135 ui->deleteSeperatorButton->setEnabled(!protect);
136 ui->deleteButton->setEnabled(!protect);
137 }
138
139 void DecolationDialog::save(QString name)
140 {
141 if (name.length() == 0) return ;
142 DecolationRules dec;
143
144 mSepModel->addToDecolation(dec);
145 mAccModel->addToDecolation(dec);
146 dec.mSeperator.mPages = ui->endJobPagesSpinBox->value();
147 dec.mFileNameTemplate = ui->fileNameTemplate->text();
148 dec.setName(name);
149 Preferences::getInstance().setDecolation(dec);
150 }
151
152
153 void DecolationDialog::addSeperator()
154 {
155 QItemSelectionModel *oldModel = ui->seperatorTableView->selectionModel();
156 mSepModel = new SepTableModel(this);
157 ui->seperatorTableView->setModel(mSepModel);
158 delete oldModel;
159 ui->seperatorTableView->resizeColumnToContents(0);
160 }
161
162 void DecolationDialog::insertSeperator()
163 {
164 mSepModel->insertRow(mSepModel->rowCount()-1, QModelIndex(),
165 1, 1,"(value)", Trigger::TriggerString);
166 }
167
168 void DecolationDialog::addAccounting()
169 {
170 QItemSelectionModel *oldModel = ui->accountingTableView->selectionModel();
171 mAccModel = new AccountingTableModel(this);
172 ui->accountingTableView->setModel(mAccModel);
173 delete oldModel;
174 ui->accountingTableView->resizeColumnToContents(0);
175 }
176
177 void DecolationDialog::insertAccounting()
178 {
179 mAccModel->insertRow(mAccModel->rowCount()-1, QModelIndex(),
180 1, 1, 8, "Job-name");
181 }
182
183 void DecolationDialog::deleteSeperator()
184 {
185 int i = ui->seperatorTableView->currentIndex().row();
186 mSepModel->removeRow(i, QModelIndex());
187 }
188
189 void DecolationDialog::deleteAccounting()
190 {
191 int i = ui->accountingTableView->currentIndex().row();
192 mAccModel->removeRow(i, QModelIndex());
193 }
194
195 //////////////////////////////////////
196
197 DecolationModelBase::DecolationModelBase(QObject * o) :
198 QAbstractTableModel(o)
199 {
200
201 }
202
203 int DecolationModelBase::rowCount(const QModelIndex &parent) const
204 {
205 hOutDebug(1,"row count:" << zmTable.size());
206 if (parent.isValid()) return 0;
207 else return zmTable.size();
208 }
209
210
211 int DecolationModelBase::columnCount(const QModelIndex &parent) const
212 {
213 if (parent.isValid())
214 return 0;
215 else return 4;
216 }
217
218 bool DecolationModelBase::removeRow ( int row, const QModelIndex & parent)
219 {
220 if (parent.isValid()) return false;
221 if (row >= zmTable.size()) return false;
222 DecTableType::iterator it = zmTable.begin()+row;
223
224 if (row < 0) return false;
225 beginRemoveRows(parent,row, row);
226 zmTable.erase(it);
227 endRemoveRows();
228 return true;
229 }
230
231 QVariant DecolationModelBase::data(const QModelIndex &index, int role) const
232 {
233 if (index.row() >= zmTable.size()) return QVariant(QVariant::Invalid);
234 DecTableType::const_iterator it = zmTable.begin() + index.row();
235 QStringList list = *it;
236 if (role != Qt::DisplayRole) return QVariant(QVariant::Invalid);
237 Q_ASSERT(list.size() > 3);
238 return QVariant(list[index.column()]);
239 }
240
241 QVariant DecolationModelBase::rowData(const QModelIndex &index) const
242 {
243 if (index.row() >= zmTable.size()) return QVariant(QVariant::Invalid);
244 DecTableType::const_iterator it = zmTable.begin() + index.row();
245 QStringList list = *it;
246 return QVariant(list);
247 }
248
249 Qt::ItemFlags DecolationModelBase::flags(const QModelIndex &index) const
250 {
251 Qt::ItemFlags ret = QAbstractTableModel::flags(index);
252 return ret | Qt::ItemIsEditable;
253 }
254
255 bool DecolationModelBase::setData(const QModelIndex &index, const QVariant &value, int)
256 {
257 hOutDebug(4,"set data " << index.row())
258 if (zmTable.size() <= index.row()) return false;
259 if (!value.isValid() || !value.canConvert(QVariant::StringList)) return false;
260 if (value.toString().length() == 0) return false;
261 DecTableType::const_iterator it = zmTable.begin() + index.row();
262
263 QStringList list = *it;
264 list[index.column()] = value.toString();
265 zmTable[index.row()] = list;
266 return true;
267 }
268
269 void DecolationModelBase::clear()
270 {
271 zmTable.clear();
272 }
273
274 ////////////////////////////////////////
275 SepTableModel::SepTableModel(QObject *o) : DecolationModelBase(o)
276 {
277
278 }
279
280 bool SepTableModel::insertRow(int row, const QModelIndex & index,
281 int line, int column, const QString& value, Trigger::TriggerType type)
282 {
283 hOutDebug(1,"insert " << line << " , "<< column << " " << value.toStdString() <<Trigger::decodeTriggerType(type).toStdString());
284
285 if (row > zmTable.size()) return false;
286 QStringList list;
287 list << QString::number(line) << QString::number(column) << value << Trigger::decodeTriggerType(type);
288
289 int key = zmTable.size();
290 beginInsertRows(index, key+1, key+1);
291 zmTable.append(list);
292 endInsertRows();
293 hOutDebug(1, "new row count " << zmTable.count());
294
295 return true;
296 }
297
298
299 QVariant SepTableModel::headerData ( int col, Qt::Orientation orientation, int role ) const
300 {
301 static const char * names[] =
302 {
303 "line",
304 "Column",
305 "Value",
306 "Type"
307 };
308 hOutDebug(3, "header " << col);
309 if (orientation == Qt::Horizontal && role == 0)
310 {
311 if (col < 4) return QVariant(QString(names[col]));
312 else return QVariant(QString("?"));
313 }
314 else
315 {
316 return QVariant();
317 }
318 }
319
320 void SepTableModel::addToDecolation(DecolationRules &decolationRules)
321 {
322 Seperator& sep = decolationRules.mSeperator;
323 sep.mPages=1;
324 DecTableType:: iterator it = zmTable.begin();
325 for (; it != zmTable.end(); it++)
326 {
327 Trigger trigger;
328 QStringList& l = *it;
329 trigger.set(l[0].toInt(), l[1].toInt(), l[3].toInt(), l[2]);
330 sep.mTriggers.push_back(trigger);
331 }
332 }
333
334
335 //////////////////////////////////////
336
337 AccountingTableModel::AccountingTableModel(QObject *o) : DecolationModelBase(o)
338 {
339
340 }
341
342 bool AccountingTableModel::insertRow(int row, const QModelIndex & index,
343 int line, int column, int length, QString name)
344 {
345 hOutDebug(4,"insert " << line << " , "<< column << " " << length <<" " << name.toStdString());
346
347 if (row > zmTable.size()) return false;
348 QStringList list;
349 list << QString::number(line) << QString::number(column) << QString::number(length) << name;
350
351 int key = zmTable.size();
352 beginInsertRows(index, key+1, key+1);
353 zmTable.append(list);
354 endInsertRows();
355
356 return true;
357 }
358
359 QVariant AccountingTableModel::headerData ( int col, Qt::Orientation orientation, int role ) const
360 {
361 static const char * names[] =
362 {
363 "line",
364 "Column",
365 "Length",
366 "Field Name"
367 };
368 hOutDebug(3, "header " << col);
369 if (orientation == Qt::Horizontal && role == 0)
370 {
371 if (col < 4) return QVariant(QString(names[col]));
372 else return QVariant(QString("?"));
373 }
374 else
375 {
376 return QVariant();
377 }
378 }
379
380 void AccountingTableModel::addToDecolation(DecolationRules &decolationRules)
381 {
382 AccountingFields& acc = decolationRules.mAccounting;
383 for (int i=0; i<zmTable.size(); i++)
384 {
385 AccountingField oneField;
386 oneField.mName = zmTable[i][3];
387 oneField.mLine = zmTable[i][0].toInt();
388 oneField.mColumn = zmTable[i][1].toInt();
389 oneField.mLength = zmTable[i][2].toInt();
390 acc.add(oneField);
391 }
392 }