Debian Codegraph @

HercConfiguration/Configuration.cpp

raw symbols SCIP
1 /*
2 * File: Configuration.cpp
3 *
4 * Author: Jacob Dekel
5 * Created on: Aug 7, 2009
6 *
7 * Copyright (c) 2009-2013 Jacob Dekel
8 * $Id$
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25 #include "Configuration.h"
26 #include "SystemConfigLine.h"
27 #include "DeviceConfigLine.h"
28 #include "ConfigurationFreeEditor.h"
29 #include "Preferences.h"
30
31 #include <QFileDialog>
32 #include <QTimer>
33
34 #include <iostream>
35 #include <sstream>
36
37 #define genEntry(keyword) {#keyword, ConfigurationEditor::keyword},
38 #define synonymEntry(keyword1, keyword2) {#keyword1, #keyword2}, {#keyword2, #keyword1},
39
40 struct ConfigTableEntry Configuration::mConfigTable[] = {
41 genEntry(ARCHMODE)
42 genEntry(ASN_AND_LX_REUSE)
43 genEntry(ALRF)
44 genEntry(AUTO_MOUNT)
45 genEntry(AUTO_SCSI_MOUNT)
46 //CODEPAGE
47 genEntry(CNSLPORT)
48 genEntry(CONKPALV)
49 genEntry(CPUMODEL)
50 genEntry(CPUPRIO)
51 genEntry(CPUSERIAL)
52 genEntry(CPUVERID)
53 //DEFSYM
54 genEntry(DEVPRIO)
55 genEntry(DEVTMAX)
56 genEntry(DIAG8CMD)
57 genEntry(ECPSVM)
58 genEntry(ENGINES)
59 genEntry(HERCPRIO)
60 genEntry(HTTPPORT)
61 genEntry(HTTPROOT)
62 //genEntry(IGNORE)
63 //INCLUDE
64 genEntry(IODELAY)
65 genEntry(LDMOD)
66 genEntry(LEGACYSENSEID)
67 genEntry(LOADPARM)
68 genEntry(LOGOPT)
69 genEntry(LPARNAME)
70 genEntry(MAINSIZE)
71 genEntry(MANUFACTURER)
72 genEntry(MODEL)
73 genEntry(MODPATH)
74 genEntry(MOUNTED_TAPE_REINIT)
75 genEntry(NUMCPU)
76 genEntry(NUMVEC)
77 genEntry(OSTAILOR)
78 genEntry(PANRATE)
79 //PANTITLE --
80 genEntry(PGMPRDOS)
81 genEntry(PLANT)
82 genEntry(SHCMDOPT)
83 genEntry(SHRDPORT)
84 genEntry(SYSEPOCH)
85 genEntry(TIMERINT)
86 genEntry(TODDRAG)
87 genEntry(TODPRIO)
88 //TRACEOPT --
89 genEntry(TZOFFSET)
90 genEntry(XPNDSIZE)
91 genEntry(YROFFSET)
92
93 {"",NULL}
94 };
95
96 Configuration::Configuration(ConfigFile* configFile, bool newConfig, QWidget *parent)
97 : QDialog(parent), mParent(parent), mConfigFile(configFile), mNewConfig(newConfig)
98 {
99 ui.setupUi(this);
100 mDevWgt = new DevicesWidget(configFile, ui.Configuration->widget(5));
101 mDevWgt->move(0,50);
102 mDevWgt->show();
103
104 mFreeEdit = new ConfigurationFreeEditor(ui.Configuration->widget(6));
105 mSyntaxHighlighter = new ConfigHighlight(mFreeEdit);
106 mSyntaxHighlighter->setDocument(mFreeEdit->document());
107 mFreeEdit->move(10,50);
108 mFreeEdit->resize(411, 671);
109
110 initilize();
111 populate(ConfigurationEditor::toScreen);
112 }
113
114 Configuration::~Configuration()
115 {
116 }
117
118 void Configuration::initilize()
119 {
120
121 DoubleDigitSpinBox * hh = new DoubleDigitSpinBox(ui.groupBox_4, true);
122 hh->setObjectName(QString::fromUtf8("tzOffsetHH"));
123 hh->setGeometry(QRect(ui.tzOffsetHH->geometry()));
124 hh->setMinimum(ui.tzOffsetHH->minimum());
125 hh->setMaximum(ui.tzOffsetHH->maximum());
126 if (ui.tzOffsetHH != NULL) delete ui.tzOffsetHH;
127 ui.tzOffsetHH = hh;
128
129 DoubleDigitSpinBox * mm = new DoubleDigitSpinBox(ui.groupBox_4, false);
130 mm->setObjectName(QString::fromUtf8("tzOffsetMM"));
131 mm->setGeometry(QRect(ui.tzOffsetMM->geometry()));
132 mm->setMaximum(ui.tzOffsetMM->maximum());
133 if (ui.tzOffsetMM != NULL) delete ui.tzOffsetMM;
134 ui.tzOffsetMM = mm;
135
136 mCurrTab = ui.Configuration->currentIndex();
137 ui.logopt->setVisible(false); // irrelevant for gui, but needed for syntax checking
138
139 connect(ui.pushButton_OK, SIGNAL(clicked()), this , SLOT(okPressed()));
140 connect(ui.pushButton_Cancel, SIGNAL(clicked()), this , SLOT(cancelPressed()));
141 connect(ui.autoMountBrowseButton, SIGNAL(clicked()), this, SLOT(autoMountBrowsePressed()));
142 connect(ui.autoScsiMountCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScsiMountChanged()));
143 connect(ui.cpuPrioSlider, SIGNAL(valueChanged(int)), this, SLOT(cpuPrioChanged(int)) );
144 connect(ui.devPrioSlider, SIGNAL(valueChanged(int)), this, SLOT(devPrioChanged(int)) );
145 connect(ui.hercPrioSlider, SIGNAL(valueChanged(int)), this, SLOT(hercPrioChanged(int)) );
146 connect(ui.todPrioSlider, SIGNAL(valueChanged(int)), this, SLOT(todPrioChanged(int)) );
147 connect(ui.ecpSvm, SIGNAL(stateChanged(int)), this, SLOT(ecpSvmChanged(int)) );
148 connect(ui.httpPortCheckBox, SIGNAL(stateChanged(int)), this, SLOT(httpPortChanged(int)));
149 connect(ui.httpRootBrowseButton, SIGNAL(clicked()), this, SLOT(httpRootBrowsePressed()));
150 connect(ui.modPathButton, SIGNAL(clicked()), this, SLOT(modPathBrowsePressed()));
151 connect(ui.Configuration, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
152 connect(mFreeEdit, SIGNAL(blockCountChanged(int)), this, SLOT(blockCountChangedSlot()));
153 }
154
155 void Configuration::resizeEvent(QResizeEvent *event)
156 {
157 QDialog::resizeEvent(event);
158
159 // move buttons
160 int newY = qMax (530, size().height() - 47);
161 int newX = qMax ( 430, size().width() - 102);
162 ui.pushButton_OK->move(newX,newY);
163 ui.pushButton_Cancel->move(newX-100,newY);
164
165 // resize QTabWidget
166 int newHeight = qMax ( size().height() - 86 , 491 );
167 int newWidth = qMax ( size().width() - 41, 691 );
168 ui.Configuration->resize( newWidth, newHeight );
169
170 // resize plainTextEdit
171 newHeight = qMax ( size().height() - 164, 411);
172 newWidth = qMax ( size().width() - 61, 671);
173 mFreeEdit->resize( newWidth, newHeight);
174
175 }
176
177 void Configuration::populate(ConfigurationEditor::Direction dir)
178 {
179 if (mConfigFile == NULL || mNewConfig)
180 {
181 ui.fileNameLabel->setText("(new file)");
182 ui.fileNameLabel_2->setText("(new file)");
183 ui.fileNameLabel_3->setText("(new file)");
184 ui.fileNameLabel_4->setText("(new file)");
185 ui.fileNameLabel_5->setText("(new file)");
186 ui.fileNameLabel_6->setText("(new file)");
187 ui.fileNameLabel_7->setText("(new file)");
188 }
189 else
190 {
191 ui.fileNameLabel->setText(mConfigFile->getFileName().c_str());
192 ui.fileNameLabel_2->setText(mConfigFile->getFileName().c_str());
193 ui.fileNameLabel_3->setText(mConfigFile->getFileName().c_str());
194 ui.fileNameLabel_4->setText(mConfigFile->getFileName().c_str());
195 ui.fileNameLabel_5->setText(mConfigFile->getFileName().c_str());
196 ui.fileNameLabel_6->setText(mConfigFile->getFileName().c_str());
197 ui.fileNameLabel_7->setText(mConfigFile->getFileName().c_str());
198 }
199 for (int i=0; mConfigTable[i].keyword.compare("") != 0 ; i++)
200 {
201 hOutDebug(5,"populate:" << mConfigTable[i].keyword)
202 const ConfigLine * configLine = mConfigFile->locateLine(mConfigTable[i].keyword, true, true);
203 mConfigTable[i].populator(&ui, configLine, dir);
204 }
205 }
206
207 void Configuration::handleHighlight(const ConfigLine& configLine, QTextCursor& cursor, QTextCharFormat& format)
208 {
209 if (configLine.inError())
210 format.setForeground(Qt::red);
211 else
212 format.setForeground(Qt::black);
213 cursor.setCharFormat(format);
214 mFreeEdit->setTextCursor(cursor);
215 }
216
217
218 void Configuration::okPressed()
219 {
220 if (ui.Configuration->currentIndex() == 6)
221 ui.Configuration->setCurrentIndex(0);
222 populate(ConfigurationEditor::fromScreen);
223 if (mNewConfig)
224 {
225 mParent->connect(this,SIGNAL(OKSignal()), mParent, SLOT(saveConfigAs()));
226 emit OKSignal();
227 disconnect(this,0,0,0);
228 }
229
230 deleteLater();
231 }
232
233 void Configuration::cancelPressed()
234 {
235 disconnect(this,0,0,0);
236 deleteLater();
237 }
238
239 void Configuration::autoScsiMountChanged()
240 {
241 ui.autoScisiSecSpin->setEnabled(ui.autoScsiMountCheck->isChecked());
242 }
243
244 void Configuration::cpuPrioChanged(int newValue)
245 {
246 ui.cpuPrioNumber->display(newValue);
247 }
248
249 void Configuration::devPrioChanged(int newValue)
250 {
251 ui.devPrioNumber->display(newValue);
252 }
253
254 void Configuration::ecpSvmChanged(int newValue)
255 {
256 if (newValue == Qt::Checked)
257 {
258 ui.ecpSvmSpin->setEnabled(true);
259 }
260 else
261 {
262 ui.ecpSvmSpin->setEnabled(false);
263 }
264 }
265
266 void Configuration::httpPortChanged(int newValue)
267 {
268 if (newValue == Qt::Checked)
269 {
270 ui.httpPortSpin->setEnabled(true);
271 }
272 else
273 {
274 ui.httpPortSpin->setValue(0);
275 ui.httpPortSpin->setEnabled(false);
276 }
277 }
278
279 void Configuration::hercPrioChanged(int newValue)
280 {
281 ui.hercPrioNumber->display(newValue);
282 }
283
284 void Configuration::todPrioChanged(int newValue)
285 {
286 ui.clockPrioNumber->display(newValue);
287 }
288
289 void Configuration::autoMountBrowsePressed()
290 {
291 std::string s = QFileDialog::getExistingDirectory(this,"Browse Auto Mount Directory","").toStdString();
292 ui.autoMountDir->setText(s.c_str());
293
294 }
295
296 void Configuration::httpRootBrowsePressed()
297 {
298 std::string s = QFileDialog::getExistingDirectory(this,"Browse HTTP Root Directory",ui.httpRootDir->text()).toStdString();
299 ui.httpRootDir->setText(s.c_str());
300 }
301
302 void Configuration::modPathBrowsePressed()
303 {
304 std::string s = QFileDialog::getExistingDirectory(this,"Browse Modules Path Directory",ui.modPathButton->text()).toStdString();
305 ui.modPath->setText(s.c_str());
306
307 }
308
309 void Configuration::tabChanged(int newTab)
310 {
311 if (newTab == 6)
312 {
313 //populate configtab with data from first 5 tabs
314 mConfigFile->appendNewLines();
315 populate(ConfigurationEditor::fromScreen);
316
317 //build free editor data
318 mFreeEdit->document()->clear();
319 QTextCursor cursor = mFreeEdit->textCursor();
320 QTextCharFormat format = cursor.charFormat();
321 format.setFont(QFont(Preferences::getInstance().fontName(Preferences::ConfigurationFontObject).c_str(),
322 Preferences::getInstance().fontSize(Preferences::ConfigurationFontObject)));
323 cursor.setCharFormat(format);
324 mFreeEdit->setTextCursor(cursor);
325
326 for (int i=0; i<=mConfigFile->getLastSys(); i++)
327 {
328 SystemConfigLine * configLine = (*mConfigFile)[i];
329 if (!configLine->isNew())
330 mFreeEdit->textCursor().insertText(configLine->getLine().c_str());
331 configLine->setUpdated(false);
332 hOutDebug(5,"sys:" << i << configLine->getLine().c_str());
333 }
334 for (int i=mConfigFile->getLastSys()+1; i<mConfigFile->size(); i++)
335 {
336 const DeviceConfigLine * configLine = mConfigFile->getDevice(i);
337 mFreeEdit->textCursor().insertText(configLine->getLine().c_str());
338 hOutDebug(5,"dev:" << i << configLine->getLine().c_str());
339 }
340
341 cursor = mFreeEdit->textCursor();
342 cursor.setPosition(0);
343 mFreeEdit->setTextCursor(cursor);
344 }
345 else if (mCurrTab == 6)
346 {
347 //reconstruct configuration table from editor data
348 mConfigFile->updateEditor(*mFreeEdit);
349 mDevWgt->update(mConfigFile);
350
351 //populate first 5 tabs with data from table
352 populate(ConfigurationEditor::toScreen);
353 }
354 mCurrTab = newTab;
355 }
356
357 Configuration::DoubleDigitSpinBox::DoubleDigitSpinBox(QWidget * parent, bool appendPlus):
358 QSpinBox(parent)
359 {
360 mAppendPlus = appendPlus;
361 }
362
363 int Configuration::DoubleDigitSpinBox::valueFromText(const QString& text) const
364 {
365 return QSpinBox::valueFromText(text);
366 }
367
368 QString Configuration::DoubleDigitSpinBox::textFromValue(int value) const
369 {
370 std::stringstream ss;
371 int sig=1;
372 int pValue=value;
373 if (value < 0)
374 {
375 sig = -1;
376 pValue = -value;
377 }
378 ss << pValue;
379
380 QString ret = ss.str().c_str();
381 if (pValue < 10) ret = "0" + ret;
382 if (sig == -1) ret = "-" + ret;
383 if (mAppendPlus && sig != -1) ret = "+" + ret;
384 hOutDebug(5, "textFromValue:" << value << "='" << ret.toStdString())
385 return ret;
386 }
387
388 Configuration::HexSpinBox::HexSpinBox(QWidget * parent):
389 QSpinBox(parent)
390 {
391 this->setMaximum(0xffff);
392 }
393
394 int Configuration::HexSpinBox::valueFromText(const QString& text) const
395 {
396 hOutDebug(5,"ValueFromText:" << text.toStdString() << ConfigurationEditor::parseNum(text.toStdString(),16) );
397 return ConfigurationEditor::parseNum(text.toStdString(),16);
398 }
399
400 QString Configuration::HexSpinBox::textFromValue(int value) const
401 {
402 std::stringstream ss;
403 ss << std::hex << value;
404 char formatted[5];
405 if (value <= 0xffff)
406 sprintf(formatted,"%4.4X",(value));
407 else
408 strcpy(formatted,"0000");
409 QString ret(formatted);
410 hOutDebug(5, "textFromValue:" << value << "='" << ret.toStdString())
411 return ret;
412 }
413
414 void Configuration::blockCountChangedSlot()
415 {
416 int mLastSys=-1;
417 for (QTextBlock block = mFreeEdit->document()->begin();
418 block != mFreeEdit->document()->end();
419 block = block.next())
420 {
421 mLastSys++;
422 std::string str = block.text().toUtf8().data();
423 int skipSpace = str.find_first_not_of(" ");
424 if (skipSpace < 0 || str[skipSpace] == '#') continue;
425 if (str[skipSpace] >= '0' && str[skipSpace] <= '9')
426 break;
427 }
428 hOutDebug(5,"lastSys=" << mLastSys);
429 }
430
431
432
433 QValidator::State Configuration::HexSpinBox::validate (QString & input, int &) const
434 {
435 if (input.length() > 4 ||
436 input.toStdString().find_first_not_of("0123456789ABCDEFabcdef") != std::string::npos)
437 return QValidator::Invalid;
438 else
439 return QValidator::Acceptable;
440 }
441
442
443 ConfigHighlight::ConfigHighlight(QWidget * parent)
444 : QSyntaxHighlighter(parent)
445 {
446 mDummyUi.setupUi(&mDummyConfig);
447 mDummyConfig.setVisible(false);
448 mRed = QColor(255,0,0,255);
449 mGreen = QColor(0,96,0,255);
450 mBlue = QColor(0,0,255,255);
451 mBlack = QColor(64,64,64,255);
452 mGray = QColor(128,32,32,255);
453 }
454
455 void ConfigHighlight::highlightBlock(const QString &text)
456 {
457 hOutDebug(3,"Highlight:" << text.toStdString());
458
459 if (text.length() == 0) return;
460
461 if (previousBlockState() == 2)
462 {
463 setCurrentBlockState(2);
464 }
465 else
466 {
467 std::string str = text.toUtf8().data();
468 int skipSpace = str.find_first_not_of(" ");
469 if (skipSpace >= 0 && str[skipSpace] != '#')
470 {
471 if (str[skipSpace] >= '0' && str[skipSpace] <= '9')
472 setCurrentBlockState(2);
473 else
474 setCurrentBlockState(1);
475 }
476 }
477 if (currentBlockState() == 2)
478 {
479 setFormat(0,text.length(),mBlue);
480 return;
481 }
482
483
484 int remarkCol = text.indexOf("#");
485
486 ConfigLine configLine(text.toUtf8().data());
487 int i=0;
488 std::string tokenToSearch = configLine.getUppercaseToken(0);
489 while (Configuration::mConfigTable[i].keyword.length() != 0 && Configuration::mConfigTable[i].keyword.compare(tokenToSearch) != 0)
490 {
491 i++;
492 }
493 if (Configuration::mConfigTable[i].keyword.length() != 0 && Configuration::mConfigTable[i].keyword.compare(tokenToSearch) == 0)
494 {
495 Configuration::mConfigTable[i].populator(&mDummyUi, &configLine, ConfigurationEditor::toScreen);
496 int startCol = configLine.getAbsoluteTokenColumn(0);
497 int endCol = configLine.getAbsoluteTokenColumn(1);
498 setFormat(startCol,endCol-startCol+1,mBlue);
499 if (configLine.size() > 0)
500 {
501 int lastToken = configLine.size();
502 startCol = configLine.getAbsoluteTokenColumn(1);
503 endCol = configLine.getAbsoluteTokenColumn(lastToken-1) + configLine.getAbsoluteTokenEnd(lastToken-1);
504 setFormat(startCol,endCol-startCol+1,mGray);
505 }
506 if (configLine.inError())
507 {
508 setFormat(configLine.errorColumn(),configLine.getLine().size()-configLine.errorColumn(),mRed);
509 }
510 if (remarkCol != -1)
511 {
512 setFormat(remarkCol,configLine.getLine().size()-remarkCol,mGreen);
513 }
514 }
515 else
516 {
517 if (configLine.isRemark())
518 {
519 setFormat(0,configLine.getLine().size(),mGreen);
520 }
521 else
522 {
523 setFormat(0,configLine.getLine().size(),mRed);
524 if (remarkCol != -1)
525 {
526 setFormat(remarkCol,configLine.getLine().size()-remarkCol,mGreen);
527 }
528 }
529 }
530 }