1
/***************************************************************************
2
* Copyright (C) 2008 by Alan Jones *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 3 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19
***************************************************************************/
20
#include <kinputdialog.h>
22
#include <QHeaderView>
24
#include "capturecheckerdetails.h"
33
CaptureCheckerDetails::CaptureCheckerDetails(QWidget * parent)
38
//Let's build the toolbars we need
40
capturesToolbar->setToolButtonStyle(Qt::ToolButtonIconOnly);
41
capturesToolbar->setOrientation(Qt::Vertical);
42
capturesToolbar->setIconDimensions(16);
43
captureAdd = new KAction(KIcon("list-add"),i18n("Add Capture"),this);
44
captureRemove = new KAction(KIcon("list-remove"),i18n("Remove Capture"),this);
45
captureRemove->setEnabled(false);
46
capturesToolbar->addAction(captureAdd);
47
capturesToolbar->addAction(captureRemove);
48
connect(captureAdd, SIGNAL(triggered( bool )), this, SLOT(addNewCapture()));
49
connect(captureRemove, SIGNAL(triggered( bool )), this, SLOT(removeCapture()));
52
variablesToolbar->setToolButtonStyle(Qt::ToolButtonIconOnly);
53
variablesToolbar->setOrientation(Qt::Vertical);
54
variablesToolbar->setIconDimensions(16);
55
variableAdd = new KAction(KIcon("list-add"),i18n("Add Variable"),this);
56
variableRemove = new KAction(KIcon("list-remove"),i18n("Remove Variable"),this);
57
variableRemove->setEnabled(false);
58
variableUp = new KAction(KIcon("arrow-up"), i18n("Move Variable Up"), this);
59
variableUp->setEnabled(false);
60
variableDown = new KAction(KIcon("arrow-down"), i18n("Move Variable Down"), this);
61
variableDown->setEnabled(false);
62
variablesToolbar->addAction(variableAdd);
63
variablesToolbar->addAction(variableRemove);
64
variablesToolbar->addAction(variableUp);
65
variablesToolbar->addAction(variableDown);
66
connect(variableAdd, SIGNAL(triggered( bool )), this, SLOT(addNewVariable()));
67
connect(variableRemove, SIGNAL(triggered( bool )), this, SLOT(removeVariable()));
68
connect(variableUp, SIGNAL(triggered( bool )), this, SLOT(moveVariableUp()));
69
connect(variableDown, SIGNAL(triggered( bool )), this, SLOT(moveVariableDown()));
72
captures->setColumnCount(2);
73
QStringList captureHeaders;
74
captureHeaders << "Name" << "Expression";
75
captures->setHorizontalHeaderLabels(captureHeaders);
76
captures->verticalHeader()->hide();
77
connect(captures, SIGNAL(itemSelectionChanged()), this, SLOT(captureSelectionChanged()));
80
variables->setColumnCount(3);
81
QStringList variableHeaders;
82
variableHeaders << "Name" << "Min" << "Max";
83
variables->setHorizontalHeaderLabels(variableHeaders);
84
variables->verticalHeader()->hide();
85
connect(variables, SIGNAL(itemSelectionChanged()), this, SLOT(variableSelectionChanged()));
88
mappings->setColumnCount(4);
89
QStringList mappingHeaders;
90
mappingHeaders << "Capture Name" << "Variable" << "Index" << "Test Value";
91
mappings->setHorizontalHeaderLabels(mappingHeaders);
92
mappings->verticalHeader()->hide();
96
void CaptureCheckerDetails::resizeColumns()
98
if (captures->columnWidth(1) != 65 && captures->width()!=100)
101
int captureWidth = captures->width();
102
captures->setColumnWidth(CAPTURE_NAME, int(captureWidth * 0.35 - 5));
103
captures->setColumnWidth(CAPTURE_VALUE, int(captureWidth * 0.65));
105
int variableWidth = variables->width();
106
variables->setColumnWidth(VARIABLE_NAME, int(variableWidth * 0.5 - 5));
107
variables->setColumnWidth(VARIABLE_MIN, int(variableWidth * 0.25));
108
variables->setColumnWidth(VARIABLE_MAX, int(variableWidth * 0.25));
110
int mappingWidth = mappings->width();
111
mappings->setColumnWidth(MAP_CAPTURE, int(mappingWidth * 0.2));
112
mappings->setColumnWidth(MAP_VARIABLE, int(mappingWidth * 0.2));
113
mappings->setColumnWidth(MAP_INDEX, int(mappingWidth * 0.1));
114
mappings->setColumnWidth(MAP_TEST, int(mappingWidth * 0.5 - 5));
117
void CaptureCheckerDetails::connectCaptureChecker(CaptureChecker* value)
122
setCaptures(value->getCaptures());
123
setVariables(value->getVariables());
124
setMappings(value->getMappings());
126
connect(value, SIGNAL(capturesChanged(QMap< QString, QString >)),
127
this, SLOT(setCaptures(QMap< QString, QString >)));
129
connect(this, SIGNAL(capturesChanged(QMap<QString, QString>)),
130
value, SLOT(setCaptures(QMap<QString, QString>)));
132
connect(value, SIGNAL(variablesChanged(QList< Variable >)),
133
this, SLOT(setVariables(QList< Variable >)));
135
connect(this, SIGNAL(variablesChanged(QList< Variable >)),
136
value, SLOT(setVariables(QList< Variable >)));
138
connect(value, SIGNAL(mappingsChanged(QMap< QPair < QString , QString >, int >)),
139
this, SLOT(setMappings(QMap< QPair < QString , QString >, int >)));
141
//not actually CaptureChecker signals, but should stop data from getting pushed out
142
connect(captures, SIGNAL(cellChanged(int, int)), this, SLOT(emitCaptures()));
143
connect(variables, SIGNAL(cellChanged(int, int)), this, SLOT(emitVariables()));
144
connect(mappings, SIGNAL(cellChanged(int, int)), this, SLOT(verifyMappingInput(int, int)));
148
void CaptureCheckerDetails::setCaptureChecker(CaptureChecker* value)
150
captureChecker = value;
152
connectCaptureChecker(value);
155
void CaptureCheckerDetails::addNewCapture()
158
QString name = KInputDialog::getText(i18n("Add New Capture"),
159
i18n("Please enter the new capture name."),QString(),&ok,this);
164
captureChecker->addNewCapture(name);
168
void CaptureCheckerDetails::removeCapture()
170
disconnect(captureChecker);
172
QList<QTableWidgetItem *> items = captures->selectedItems();
174
captureChecker->removeCapture(captures->item(items.at(0)->row(), CAPTURE_NAME)->text());
176
connectCaptureChecker(captureChecker);
177
setCaptures(captureChecker->getCaptures());
180
void CaptureCheckerDetails::addNewVariable()
183
QString name = KInputDialog::getText(i18n("Add New Variable"),
184
i18n("Please enter the new variable name."),QString(),&ok,this);
189
captureChecker->addNewVariable(name);
193
void CaptureCheckerDetails::removeVariable()
195
disconnect(captureChecker);
197
QList<QTableWidgetItem *> items = variables->selectedItems();
199
captureChecker->removeVariable(variables->item(items.at(0)->row(), VARIABLE_NAME)->text());
201
connectCaptureChecker(captureChecker);
202
setVariables(captureChecker->getVariables());
205
void CaptureCheckerDetails::moveVariableUp()
207
disconnect(captureChecker);
209
QList<QTableWidgetItem *> items = variables->selectedItems();
214
captureChecker->moveVariableUp(items.at(0)->row());
216
connectCaptureChecker(captureChecker);
217
setVariables(captureChecker->getVariables());
220
void CaptureCheckerDetails::moveVariableDown()
222
disconnect(captureChecker);
224
QList<QTableWidgetItem *> items = variables->selectedItems();
229
captureChecker->moveVariableDown(items.at(0)->row());
231
connectCaptureChecker(captureChecker);
232
setVariables(captureChecker->getVariables());
235
void CaptureCheckerDetails::captureSelectionChanged()
237
captureRemove->setEnabled(captures->selectedItems().count());
240
void CaptureCheckerDetails::variableSelectionChanged()
242
bool varSel = variables->selectedItems().count();
243
variableRemove->setEnabled(varSel);
247
variableUp->setEnabled(false);
248
variableDown->setEnabled(false);
252
//at the top it can't shift up at the bottom it can't shift down
253
variableUp->setEnabled(variables->selectedItems().at(0)->row() != 0);
254
variableDown->setEnabled(variables->selectedItems().at(0)->row() != variables->rowCount()-1);
258
void CaptureCheckerDetails::setTestString(const QString& value)
265
void CaptureCheckerDetails::verifyMappingInput(int row, int column)
267
if (column != MAP_INDEX)
270
QString captureName = mappings->item(row, MAP_CAPTURE)->text();
271
QString variableName = mappings->item(row, MAP_VARIABLE)->text();
272
int index = mappings->item(row, MAP_INDEX)->text().toInt();
274
captureChecker->setMappingValue(captureName, variableName, index);
277
void CaptureCheckerDetails::setCaptures(QMap<QString, QString> value)
279
disconnect(captures, SIGNAL(cellChanged(int, int)), this, SLOT(emitCaptures()));
280
//thar be bugs in these waters
281
captures->clearContents();
282
captures->setRowCount(value.count());
284
QTableWidgetItem *newItem;
286
QMap<QString, QString>::const_iterator i = value.constBegin();
289
while ( i != value.constEnd() )
291
newItem = new QTableWidgetItem(i.key());
292
captures->setItem(curRow, CAPTURE_NAME, newItem);
294
newItem = new QTableWidgetItem(i.value());
295
captures->setItem(curRow, CAPTURE_VALUE, newItem);
301
connect(captures, SIGNAL(cellChanged(int, int)), this, SLOT(emitCaptures()));
304
void CaptureCheckerDetails::setVariables(QList<Variable> value)
306
disconnect(variables, SIGNAL(cellChanged(int, int)), this, SLOT(emitVariables()));
307
QString selectedVariable;
310
if (variables->selectedItems().count())
312
selectedVariable = variables->item(variables->selectedItems().at(0)->row(), VARIABLE_NAME)->text();
315
variables->clearContents();
316
variables->setRowCount(value.count());
318
QTableWidgetItem *newItem;
320
for (int i=0; i<value.count(); i++)
322
if (value.at(i).name == selectedVariable)
327
newItem = new QTableWidgetItem(value.at(i).name);
328
variables->setItem(i, VARIABLE_NAME, newItem);
330
newItem = new QTableWidgetItem(value.at(i).min);
331
variables->setItem(i, VARIABLE_MIN, newItem);
333
newItem = new QTableWidgetItem(value.at(i).max);
334
variables->setItem(i, VARIABLE_MAX, newItem);
339
variables->selectRow(selectRow);
342
connect(variables, SIGNAL(cellChanged(int, int)), this, SLOT(emitVariables()));
345
void CaptureCheckerDetails::setMappings(QMap<QPair<QString, QString>, int> value)
347
disconnect(mappings, SIGNAL(cellChanged(int, int)), this, SLOT(verifyMappingInput(int, int)));
348
mappings->clearContents();
349
mappings->setRowCount(value.count());
351
QTableWidgetItem *newItem;
353
QMap<QPair<QString, QString>, int>::const_iterator i = value.constBegin();
356
while ( i != value.constEnd() )
358
newItem = new QTableWidgetItem(i.key().first);
359
newItem->setFlags(Qt::ItemIsEnabled);
360
mappings->setItem(curRow, MAP_CAPTURE, newItem);
362
newItem = new QTableWidgetItem(i.key().second);
363
newItem->setFlags(Qt::ItemIsEnabled);
364
mappings->setItem(curRow, MAP_VARIABLE, newItem);
366
newItem = new QTableWidgetItem(QString::number(i.value()));
367
mappings->setItem(curRow, MAP_INDEX, newItem);
369
newItem = new QTableWidgetItem(QString());
370
newItem->setFlags(Qt::ItemIsEnabled);
371
mappings->setItem(curRow, MAP_TEST, newItem);
377
connect(mappings, SIGNAL(cellChanged(int, int)), this, SLOT(verifyMappingInput(int, int)));
380
emit mappingsChanged();
383
void CaptureCheckerDetails::updateMappingTest()
385
//first clear the text on them all
386
for (int i=0; i<mappings->rowCount(); i++)
388
mappings->item(i, MAP_TEST)->setText(i18n("No Capture"));
389
//lazy way to get the default background hehe
390
mappings->item(i, MAP_TEST)->setBackground(mappings->item(i, MAP_CAPTURE)->background());
393
//if there's no test string we can skip doing anything
394
if (testString.isEmpty())
397
QStringList captureList = captureChecker->getCaptures().keys();
398
for (int i=0; i<captureList.count(); i++)
400
//first run through each of the captures getting their variable values
401
Capture curCap = captureChecker->findCapture(testString, captureList.at(i));
403
if (curCap.isEmpty())
406
for (int j=0; j<mappings->rowCount(); j++)
408
//go through each row on the mapping
409
//if we're not looking at the current capture - skip it and move on to the next row
410
if (mappings->item(j, MAP_CAPTURE)->text() != captureList.at(i))
413
if (curCap.getValue(mappings->item(j, MAP_VARIABLE)->text()).isEmpty())
416
mappings->item(j, MAP_TEST)->setText(curCap.getValue(mappings->item(j, MAP_VARIABLE)->text()));
418
if (curCap.isInRange(captureChecker->getMinCapture(), captureChecker->getMaxCapture()))
420
//this is an in range Match so colour the item green
421
mappings->item(j, MAP_TEST)->setBackground(QBrush(QColor(0,255,0,128)));
425
//this match is out of range so colour the item red
426
mappings->item(j, MAP_TEST)->setBackground(QBrush(QColor(255,0,0,128)));
433
void CaptureCheckerDetails::emitCaptures()
435
QMap<QString, QString> value;
437
for (int i=0; i<captures->rowCount(); i++)
439
value.insert(captures->item(i, CAPTURE_NAME)->text(), captures->item(i, CAPTURE_VALUE)->text());
442
emit capturesChanged(value);
445
void CaptureCheckerDetails::emitVariables()
447
QList<Variable> value;
450
for (int i=0; i<variables->rowCount(); i++)
452
curVar.name = variables->item(i, VARIABLE_NAME)->text();
453
curVar.min = variables->item(i, VARIABLE_MIN)->text();
454
curVar.max = variables->item(i, VARIABLE_MAX)->text();
456
value.append(curVar);
459
emit variablesChanged(value);