~ubuntu-branches/ubuntu/lucid/ktorrent/lucid

« back to all changes in this revision

Viewing changes to plugins/bitfinder/filter/capturecheckerdetails.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-12-08 19:17:41 UTC
  • mfrom: (1.2.1 upstream) (0.7.12 sid)
  • Revision ID: james.westby@ubuntu.com-20091208191741-lqlq0xvnlv8ki19u
Tags: 3.3.1+dfsg.1-1ubuntu1
* Merge with Debian Testing remaining changes:
  - Build-depend directly on libboost-serialization1.40-dev since
    libboost-serialization-dev from boost-defaults is not in Main
  - Add in rules: include /usr/lib/kubuntu-desktop-i18n/debhelper/kubuntu.mk
  - Don't use dpkg-source 3.0 format
  - Add quilt to build-depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2008 by Alan Jones                                      *
3
 
 *   skyphyr@gmail.com                                                     *
4
 
 *                                                                         *
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.                                   *
9
 
 *                                                                         *
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.                          *
14
 
 *                                                                         *
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>
21
 
 
22
 
#include <QHeaderView>
23
 
 
24
 
#include "capturecheckerdetails.h"
25
 
 
26
 
#include <util/log.h>
27
 
 
28
 
using namespace bt;
29
 
 
30
 
namespace kt
31
 
        {
32
 
        
33
 
        CaptureCheckerDetails::CaptureCheckerDetails(QWidget * parent) 
34
 
                        : QWidget(parent)
35
 
                {
36
 
                setupUi(this);
37
 
                
38
 
                //Let's build the toolbars we need
39
 
                //capturesToolbar
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()));
50
 
 
51
 
                //variablesToolbar
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()));
70
 
                
71
 
                //captures
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()));
78
 
                
79
 
                //variables
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()));
86
 
                
87
 
                //mappings
88
 
                mappings->setColumnCount(4);
89
 
                QStringList mappingHeaders;
90
 
                mappingHeaders << "Capture Name" << "Variable" << "Index" << "Test Value";
91
 
                mappings->setHorizontalHeaderLabels(mappingHeaders);
92
 
                mappings->verticalHeader()->hide();
93
 
                
94
 
                }
95
 
        
96
 
        void CaptureCheckerDetails::resizeColumns()
97
 
                {
98
 
                if (captures->columnWidth(1) != 65 && captures->width()!=100)
99
 
                        return;
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));
104
 
                
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));
109
 
                
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));
115
 
                }
116
 
        
117
 
        void CaptureCheckerDetails::connectCaptureChecker(CaptureChecker* value)
118
 
                {
119
 
                if (!value)
120
 
                        return;
121
 
                
122
 
                setCaptures(value->getCaptures());
123
 
                setVariables(value->getVariables());
124
 
                setMappings(value->getMappings());
125
 
                
126
 
                connect(value, SIGNAL(capturesChanged(QMap< QString, QString >)), 
127
 
                                this, SLOT(setCaptures(QMap< QString, QString >)));
128
 
                
129
 
                connect(this, SIGNAL(capturesChanged(QMap<QString, QString>)), 
130
 
                                value, SLOT(setCaptures(QMap<QString, QString>)));
131
 
                
132
 
                connect(value, SIGNAL(variablesChanged(QList< Variable >)),
133
 
                                this, SLOT(setVariables(QList< Variable >)));
134
 
                                
135
 
                connect(this, SIGNAL(variablesChanged(QList< Variable >)),
136
 
                                value, SLOT(setVariables(QList< Variable >)));
137
 
                                
138
 
                connect(value, SIGNAL(mappingsChanged(QMap< QPair < QString , QString >, int >)),
139
 
                                this, SLOT(setMappings(QMap< QPair < QString , QString >, int >)));
140
 
                
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)));
145
 
 
146
 
                }
147
 
        
148
 
        void CaptureCheckerDetails::setCaptureChecker(CaptureChecker* value)
149
 
                {
150
 
                captureChecker = value;
151
 
                
152
 
                connectCaptureChecker(value);
153
 
                }
154
 
        
155
 
        void CaptureCheckerDetails::addNewCapture()
156
 
                {
157
 
                bool ok = false;
158
 
                QString name = KInputDialog::getText(i18n("Add New Capture"), 
159
 
                                        i18n("Please enter the new capture name."),QString(),&ok,this);
160
 
                
161
 
                if (ok)
162
 
                        {
163
 
                        if (captureChecker)
164
 
                                captureChecker->addNewCapture(name);
165
 
                        }
166
 
                }
167
 
        
168
 
        void CaptureCheckerDetails::removeCapture()
169
 
                {
170
 
                disconnect(captureChecker);
171
 
                
172
 
                QList<QTableWidgetItem *> items = captures->selectedItems();
173
 
                
174
 
                captureChecker->removeCapture(captures->item(items.at(0)->row(), CAPTURE_NAME)->text());
175
 
                
176
 
                connectCaptureChecker(captureChecker);
177
 
                setCaptures(captureChecker->getCaptures());
178
 
                }
179
 
        
180
 
        void CaptureCheckerDetails::addNewVariable()
181
 
                {
182
 
                bool ok = false;
183
 
                QString name = KInputDialog::getText(i18n("Add New Variable"), 
184
 
                                        i18n("Please enter the new variable name."),QString(),&ok,this);
185
 
                
186
 
                if (ok)
187
 
                        {
188
 
                        if (captureChecker)
189
 
                                captureChecker->addNewVariable(name);
190
 
                        }
191
 
                }
192
 
        
193
 
        void CaptureCheckerDetails::removeVariable()
194
 
                {
195
 
                disconnect(captureChecker);
196
 
                
197
 
                QList<QTableWidgetItem *> items = variables->selectedItems();
198
 
                
199
 
                captureChecker->removeVariable(variables->item(items.at(0)->row(), VARIABLE_NAME)->text());
200
 
                
201
 
                connectCaptureChecker(captureChecker);
202
 
                setVariables(captureChecker->getVariables());
203
 
                }
204
 
        
205
 
        void CaptureCheckerDetails::moveVariableUp()
206
 
                {
207
 
                disconnect(captureChecker);
208
 
                
209
 
                QList<QTableWidgetItem *> items = variables->selectedItems();
210
 
                
211
 
                if (!items.count())
212
 
                        return;
213
 
                
214
 
                captureChecker->moveVariableUp(items.at(0)->row());
215
 
                
216
 
                connectCaptureChecker(captureChecker);
217
 
                setVariables(captureChecker->getVariables());
218
 
                }
219
 
                
220
 
        void CaptureCheckerDetails::moveVariableDown()
221
 
                {
222
 
                disconnect(captureChecker);
223
 
                
224
 
                QList<QTableWidgetItem *> items = variables->selectedItems();
225
 
                
226
 
                if (!items.count())
227
 
                        return;
228
 
                
229
 
                captureChecker->moveVariableDown(items.at(0)->row());
230
 
                
231
 
                connectCaptureChecker(captureChecker);
232
 
                setVariables(captureChecker->getVariables());
233
 
                }
234
 
        
235
 
        void CaptureCheckerDetails::captureSelectionChanged()
236
 
                {
237
 
                captureRemove->setEnabled(captures->selectedItems().count());
238
 
                }
239
 
                
240
 
        void CaptureCheckerDetails::variableSelectionChanged()
241
 
                {
242
 
                bool varSel = variables->selectedItems().count();
243
 
                variableRemove->setEnabled(varSel);
244
 
                
245
 
                if (!varSel)
246
 
                        {
247
 
                        variableUp->setEnabled(false);
248
 
                        variableDown->setEnabled(false);
249
 
                        return;
250
 
                        }
251
 
                
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);
255
 
                
256
 
                }
257
 
        
258
 
        void CaptureCheckerDetails::setTestString(const QString& value)
259
 
                {
260
 
                testString = value;
261
 
                
262
 
                updateMappingTest();
263
 
                }
264
 
        
265
 
        void CaptureCheckerDetails::verifyMappingInput(int row, int column)
266
 
                {
267
 
                if (column != MAP_INDEX)
268
 
                        return;
269
 
                
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();
273
 
                
274
 
                captureChecker->setMappingValue(captureName, variableName, index);
275
 
                }
276
 
 
277
 
        void CaptureCheckerDetails::setCaptures(QMap<QString, QString> value)
278
 
                {
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());
283
 
                
284
 
                QTableWidgetItem *newItem;
285
 
                
286
 
                QMap<QString, QString>::const_iterator i = value.constBegin();
287
 
                int curRow = 0;
288
 
                
289
 
                while ( i != value.constEnd() )
290
 
                        {
291
 
                        newItem = new QTableWidgetItem(i.key());
292
 
                        captures->setItem(curRow, CAPTURE_NAME, newItem);
293
 
                        
294
 
                        newItem = new QTableWidgetItem(i.value());
295
 
                        captures->setItem(curRow, CAPTURE_VALUE, newItem);
296
 
                        
297
 
                        curRow++;
298
 
                        i++;
299
 
                        }
300
 
                        
301
 
                connect(captures, SIGNAL(cellChanged(int, int)), this, SLOT(emitCaptures()));
302
 
                }
303
 
                
304
 
        void CaptureCheckerDetails::setVariables(QList<Variable> value)
305
 
                {
306
 
                disconnect(variables, SIGNAL(cellChanged(int, int)), this, SLOT(emitVariables()));
307
 
                QString selectedVariable;
308
 
                int selectRow = -1;
309
 
                
310
 
                if (variables->selectedItems().count())
311
 
                        {
312
 
                        selectedVariable = variables->item(variables->selectedItems().at(0)->row(), VARIABLE_NAME)->text();
313
 
                        }
314
 
                
315
 
                variables->clearContents();
316
 
                variables->setRowCount(value.count());
317
 
                
318
 
                QTableWidgetItem *newItem;
319
 
                
320
 
                for (int i=0; i<value.count(); i++)
321
 
                        {
322
 
                        if (value.at(i).name == selectedVariable)
323
 
                                {
324
 
                                selectRow = i;
325
 
                                }
326
 
                        
327
 
                        newItem = new QTableWidgetItem(value.at(i).name);
328
 
                        variables->setItem(i, VARIABLE_NAME, newItem);
329
 
                        
330
 
                        newItem = new QTableWidgetItem(value.at(i).min);
331
 
                        variables->setItem(i, VARIABLE_MIN, newItem);
332
 
                        
333
 
                        newItem = new QTableWidgetItem(value.at(i).max);
334
 
                        variables->setItem(i, VARIABLE_MAX, newItem);
335
 
                        }
336
 
                
337
 
                if (selectRow >=0)
338
 
                        {
339
 
                        variables->selectRow(selectRow);
340
 
                        }
341
 
                
342
 
                connect(variables, SIGNAL(cellChanged(int, int)), this, SLOT(emitVariables()));
343
 
                }
344
 
                
345
 
        void CaptureCheckerDetails::setMappings(QMap<QPair<QString, QString>, int> value)
346
 
                {
347
 
                disconnect(mappings, SIGNAL(cellChanged(int, int)), this, SLOT(verifyMappingInput(int, int)));
348
 
                mappings->clearContents();
349
 
                mappings->setRowCount(value.count());
350
 
                
351
 
                QTableWidgetItem *newItem;
352
 
                
353
 
                QMap<QPair<QString, QString>, int>::const_iterator i = value.constBegin();
354
 
                int curRow = 0;
355
 
                
356
 
                while ( i != value.constEnd() )
357
 
                        {
358
 
                        newItem = new QTableWidgetItem(i.key().first);
359
 
                        newItem->setFlags(Qt::ItemIsEnabled);
360
 
                        mappings->setItem(curRow, MAP_CAPTURE, newItem);
361
 
                        
362
 
                        newItem = new QTableWidgetItem(i.key().second);
363
 
                        newItem->setFlags(Qt::ItemIsEnabled);
364
 
                        mappings->setItem(curRow, MAP_VARIABLE, newItem);
365
 
                        
366
 
                        newItem = new QTableWidgetItem(QString::number(i.value()));
367
 
                        mappings->setItem(curRow, MAP_INDEX, newItem);
368
 
                        
369
 
                        newItem = new QTableWidgetItem(QString());
370
 
                        newItem->setFlags(Qt::ItemIsEnabled);
371
 
                        mappings->setItem(curRow, MAP_TEST, newItem);
372
 
                        
373
 
                        curRow++;
374
 
                        i++;
375
 
                        }
376
 
                
377
 
                connect(mappings, SIGNAL(cellChanged(int, int)), this, SLOT(verifyMappingInput(int, int)));
378
 
                
379
 
                updateMappingTest();
380
 
                emit mappingsChanged();
381
 
                }
382
 
        
383
 
        void CaptureCheckerDetails::updateMappingTest()
384
 
                {
385
 
                //first clear the text on them all
386
 
                for (int i=0; i<mappings->rowCount(); i++)
387
 
                        {
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());
391
 
                        }
392
 
                
393
 
                //if there's no test string we can skip doing anything
394
 
                if (testString.isEmpty())
395
 
                        return;
396
 
                
397
 
                QStringList captureList = captureChecker->getCaptures().keys();
398
 
                for (int i=0; i<captureList.count(); i++)
399
 
                        {
400
 
                        //first run through each of the captures getting their variable values
401
 
                        Capture curCap = captureChecker->findCapture(testString, captureList.at(i));
402
 
                        
403
 
                        if (curCap.isEmpty())
404
 
                                continue;
405
 
                                
406
 
                        for (int j=0; j<mappings->rowCount(); j++)
407
 
                                {
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))
411
 
                                        continue;
412
 
                                
413
 
                                if (curCap.getValue(mappings->item(j, MAP_VARIABLE)->text()).isEmpty())
414
 
                                        return;
415
 
                                
416
 
                                mappings->item(j, MAP_TEST)->setText(curCap.getValue(mappings->item(j, MAP_VARIABLE)->text()));
417
 
                                
418
 
                                if (curCap.isInRange(captureChecker->getMinCapture(), captureChecker->getMaxCapture()))
419
 
                                        {
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)));
422
 
                                        }
423
 
                                else
424
 
                                        {
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)));
427
 
                                        }
428
 
                                }
429
 
                        
430
 
                        }
431
 
                }
432
 
        
433
 
        void CaptureCheckerDetails::emitCaptures()
434
 
                {
435
 
                QMap<QString, QString> value;
436
 
                
437
 
                for (int i=0; i<captures->rowCount(); i++)
438
 
                        {
439
 
                        value.insert(captures->item(i, CAPTURE_NAME)->text(), captures->item(i, CAPTURE_VALUE)->text());
440
 
                        }
441
 
                
442
 
                emit capturesChanged(value);
443
 
                }
444
 
                
445
 
        void CaptureCheckerDetails::emitVariables()
446
 
                {
447
 
                QList<Variable> value;
448
 
                
449
 
                Variable curVar;
450
 
                for (int i=0; i<variables->rowCount(); i++)
451
 
                        {
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();
455
 
                        
456
 
                        value.append(curVar);
457
 
                        }
458
 
                
459
 
                emit variablesChanged(value);
460
 
                }
461
 
                
462
 
        }