OpenCores
URL https://opencores.org/ocsvn/bw_tiff_compression/bw_tiff_compression/trunk

Subversion Repositories bw_tiff_compression

[/] [bw_tiff_compression/] [trunk/] [client_application/] [src/] [GUI/] [CCITT4Client.cpp] - Blame information for rev 16

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 amulder
/*
2
 * @file     CCITT4Client.cpp
3
 * @date     May 14, 2012
4
 * @author   Aart Mulder
5
 */
6
 
7
#include <QMessageBox>
8
#include <QImage>
9
#include <QGraphicsPixmapItem>
10
#include <QMessageBox>
11
#include <QDebug>
12
#include <QFileDialog>
13
#include <QDateTime>
14
#include <QListWidgetItem>
15
#include <QStringList>
16
#include <QIcon>
17
#include <QColor>
18
#include <QFile>
19
 
20
#include "CCITT4Client.h"
21
#include "ui_CCITT4Client.h"
22
#include "CPathLib.h"
23
 
24
CCCITT4Client::CCCITT4Client(QWidget *parent, QString sStoragePath) :
25
    QMainWindow(parent),
26
    ui(new Ui::CCCITT4Client)
27
{
28
    QString sTmp = sStoragePath;
29
 
30
    ui->setupUi(this);
31
 
32
    m_nCursorPos = 0;
33
 
34
    m_pSerialport = new CSerialport();
35
    m_pPortSelectionDialog = new CPortSelectionDialog();
36
 
37
    ui->BtSingleShot->setEnabled(false);
38
    ui->BtRepeat->setEnabled(false);
39
 
40
    if( (sStoragePath != "") && (QFile::exists(sStoragePath)) )
41
    {
42
        this->ui->lineEditPath->setText(sStoragePath);
43
    }
44
    else
45
    {
46
        this->ui->lineEditPath->setText(QDir::currentPath());
47
    }
48
 
49
    UpdateFileList();
50
 
51
    m_oScreenRefreshTimer.start(100);
52
 
53
    connect(ui->BtConnect, SIGNAL(clicked()), this, SLOT(OnBtConnectClicked()));
54
    connect(ui->BtSingleShot, SIGNAL(clicked()), this, SLOT(OnBtSingleShotClicked()));
55
    connect(ui->BtRepeat, SIGNAL(clicked()), this, SLOT(OnBtRepeatClicked()));
56
    connect(ui->BtPath, SIGNAL(clicked()), this, SLOT(OnBtPathClicked()));
57
    connect(ui->lineEditPath, SIGNAL(textChanged(QString)), this, SLOT(OnLineEditPathChanged(QString)));
58
    connect(ui->listWidgetFiles, SIGNAL(itemSelectionChanged()), this, SLOT(OnFilesListSelectionChanged()));
59
    connect(&m_oScreenRefreshTimer, SIGNAL(timeout()), this, SLOT(OnScreenRefreshTimer()));
60
    connect(m_pSerialport, SIGNAL(showErrorMessage(QString, bool, bool)), this, SLOT(OnShowErrorMessage(QString, bool, bool)));
61
    connect(m_pSerialport, SIGNAL(frameCompleted(QString)), this, SLOT(OnFrameCompleted(QString)));
62
 
63
}
64
 
65
CCCITT4Client::~CCCITT4Client()
66
{
67
    delete ui;
68
}
69
 
70
void CCCITT4Client::Show()
71
{
72
    QMainWindow::show();
73
 
74
    this->setHorizontalSpitter(1, -1, 140);
75
}
76
 
77
void CCCITT4Client::showEvent(QShowEvent *event)
78
{
79
    this->setHorizontalSpitter(1, -1, 240);
80
}
81
 
82
void CCCITT4Client::OnBtConnectClicked()
83
{
84
    QString sPortname = "";
85
    QList<QString> aPortNames;
86
 
87
    if(m_pSerialport == NULL)
88
        return;
89
 
90
    if(ui->BtConnect->isChecked())
91
    {
92
        ui->BtConnect->setChecked(false);
93
 
94
        /*
95
         * Show the port selection dialog to the user if there is more than
96
         * one port available. Show an error pop-up if no ports are available.
97
         */
98
        aPortNames = CSerialport::GetPortNames();
99
        if(aPortNames.size() <= 0)
100
        {
101
            QMessageBox::critical(this, "Error",
102
                    "No serial ports available");
103
            return;
104
        }
105
 
106
        m_pPortSelectionDialog->UpdateList(aPortNames);
107
        if(m_pPortSelectionDialog->exec() == QDialog::Accepted)
108
        {
109
            sPortname = m_pPortSelectionDialog->GetPortname();
110
        }
111
        else
112
        {
113
            ui->BtConnect->setChecked(false);
114
            return;
115
        }
116
 
117
#ifdef linux
118
        if(m_pSerialport->Connect((char*)sPortname.toStdString().c_str(), m_pPortSelectionDialog->GetBaudrate()))
119
#else
120
        if(m_pSerialport->Connect((char*)sPortname.toStdString().c_str(), m_pPortSelectionDialog->GetBaudrate()))
121
#endif
122
        {
123
            ui->BtSingleShot->setEnabled(true);
124
            ui->BtRepeat->setEnabled(true);
125
            ui->BtConnect->setChecked(true);
126
            ui->BtRepeat->setChecked(false);
127
        }
128
        else
129
        {
130
            ui->BtConnect->setChecked(false);
131
            QMessageBox::critical(this, "Error",
132
                    "Unable to connect to "+sPortname);
133
        }
134
    }
135
    else
136
    {
137
        m_pSerialport->Disconnect();
138
 
139
        ui->BtSingleShot->setEnabled(false);
140
        ui->BtRepeat->setEnabled(false);
141
        ui->BtRepeat->setChecked(false);
142
    }
143
}
144
 
145
void CCCITT4Client::OnBtSingleShotClicked()
146
{
147
    this->ui->statusBar->clearMessage();
148
    ui->textEditRxLog->insertPlainText("\n");
149
    m_nCursorPos = 0;
150
 
151
    ui->BtSingleShot->setEnabled(false);
152
 
153
    m_pSerialport->RequestNewFrame(QString(""), ui->lineEditPath->text());
154
}
155
 
156
void CCCITT4Client::OnBtRepeatClicked()
157
{
158
    if(ui->BtRepeat->isChecked())
159
    {
160
        if(m_pSerialport->IsStateStandby())
161
        {
162
            ui->BtSingleShot->setEnabled(false);
163
            OnBtSingleShotClicked();
164
        }
165
        else
166
        {
167
            ui->BtRepeat->setChecked(false);
168
        }
169
    }
170
    else
171
    {
172
        ui->BtSingleShot->setEnabled(true);
173
    }
174
}
175
 
176
void CCCITT4Client::OnBtPathClicked()
177
{
178
    QString sStorageDir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
179
                                                            ui->lineEditPath->text(),
180
                                                            QFileDialog::ShowDirsOnly
181
                                                            | QFileDialog::DontResolveSymlinks);
182
    this->ui->lineEditPath->setText(sStorageDir);
183
}
184
 
185
void CCCITT4Client::OnLineEditPathChanged(QString sPath)
186
{
187
    sPath.clear(); //To surpress the unused warning when building
188
 
189
    this->UpdateFileList();
190
}
191
 
192
void CCCITT4Client::OnFilesListItemClicked(QListWidgetItem *pItem)
193
{
194
    QImage *pImage;
195
    QString sFilename;
196
    QDir oDir;
197
    QMessageBox msgBox;
198
 
199
    oDir.setCurrent(ui->lineEditPath->text());
200
 
201
    sFilename = oDir.absoluteFilePath(pItem->text());
202
 
203
    this->ui->graphicsView->Scene->clear();
204
 
205
    pImage = new QImage(sFilename);
206
    if(pImage->isNull())
207
    {
208
        msgBox.setText(QString("Can't' open the file: %1").arg(sFilename));
209
        msgBox.exec();
210
 
211
        /* Update the file list because it seems to be corrupt */
212
        UpdateFileList();
213
 
214
        return;
215
    }
216
 
217
    this->ui->graphicsView->Scene->addItem(new QGraphicsPixmapItem(QPixmap::fromImage(*pImage)));
218
    this->show();
219
 
220
    /* Move the splitter to enhance the image viewer. */
221
    setVerticalSpitter(0, 80);
222
}
223
 
224
void CCCITT4Client::OnFilesListSelectionChanged()
225
{
226
    QListWidgetItem *pListItem = ui->listWidgetFiles->currentItem();
227
 
228
    OnFilesListItemClicked(pListItem);
229
}
230
 
231
void CCCITT4Client::UpdateFileList()
232
{
233
    QListWidgetItem *pListItem;
234
    QDir *pDir;
235
    QFileInfoList list;
236
    int i, j;
237
    bool bFileExists, bItemExists;
238
 
239
    /* Disconnect the list event while changing the contents */
240
    disconnect(ui->listWidgetFiles, SIGNAL(itemSelectionChanged()), this, SLOT(OnFilesListSelectionChanged()));
241
 
242
    /* Obtain a list of all the tif files in the selected directory */
243
    pDir = new QDir(ui->lineEditPath->text());
244
    list = pDir->entryInfoList(QStringList("*.tif"), QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks, QDir::Time);
245
 
246
    /* Remove list elements of which the corresponding file does not exist anymore */
247
    for(i = 0; i < ui->listWidgetFiles->count(); i++)
248
    {
249
        pListItem = ui->listWidgetFiles->item(i);
250
 
251
        /* Verify if the file exists */
252
        bFileExists = false;
253
        if(pListItem != NULL)
254
        {
255
            for(j = 0; (j < list.size()) && (bFileExists == false); j++)
256
            {
257
                if(list.at(j).fileName().compare(pListItem->text()) == 0)
258
                {
259
                    bFileExists = true;
260
                }
261
            }
262
        }
263
 
264
        /* Delete the list element if the file doesn't exists */
265
        if(bFileExists == false)
266
        {
267
            ui->listWidgetFiles->removeItemWidget(pListItem);
268
            delete pListItem;
269
            pListItem = NULL;
270
            i = 0;
271
        }
272
    }
273
 
274
    /* Iterate over all the files and add them to the list if they are not contained yet */
275
    for(i = 0; i < list.size(); ++i)
276
    {
277
        bItemExists = false;
278
        for(j = 0; j < ui->listWidgetFiles->count(); j++)
279
        {
280
            if(list.at(i).fileName().compare(ui->listWidgetFiles->item(j)->text()) == 0)
281
            {
282
                bItemExists = true;
283
            }
284
        }
285
 
286
        if(bItemExists == false)
287
        {
288
            pListItem = new QListWidgetItem(QIcon(list.at(i).absoluteFilePath()), list.at(i).fileName());
289
 
290
            ui->listWidgetFiles->addItem(pListItem);
291
        }
292
    }
293
 
294
    /* Alternate the backgroundcolor of the list elements */
295
    for(i = 0; i < ui->listWidgetFiles->count(); i++)
296
    {
297
        if(i & 0x1)
298
        {
299
            ui->listWidgetFiles->item(i)->setBackgroundColor(QColor::fromHsv(0,0,240));
300
        }
301
        else
302
        {
303
            ui->listWidgetFiles->item(i)->setBackgroundColor(QColor::fromHsv(0,0,255));
304
        }
305
    }
306
 
307
    delete pDir;
308
 
309
    /* reconnnect the list event */
310
    connect(ui->listWidgetFiles, SIGNAL(itemSelectionChanged()), this, SLOT(OnFilesListSelectionChanged()));
311
}
312
 
313
void CCCITT4Client::setHorizontalSpitter(int nIndex, int nSizePercent, int nSizePixels)
314
{
315
    QList<int> aSize;
316
    int nSizeTotal, nSizeTotalOthers, i;
317
 
318
    if(nSizePercent > 100)
319
        return;
320
 
321
    /* Obtain current sizes */
322
    aSize = ui->splitter->sizes();
323
 
324
    /* Verify that the splitter contains enough items */
325
    if( (nIndex >= aSize.count()) || (nIndex < 0) )
326
        return;
327
 
328
    /* Calculate the total size */
329
    nSizeTotal = this->ui->splitter->width()
330
            - (this->ui->splitter->handleWidth() * (this->ui->splitter->count()-1))
331
            - (this->ui->splitter->lineWidth() * (this->ui->splitter->count()-1) );
332
 
333
    /* Set the size for the other items */
334
    for(i = 0; i < aSize.count(); i++)
335
    {
336
        if(i != nIndex)
337
        {
338
            /* Special option: when nSizePercent < 0 then nSizePixels is used instead */
339
            if(nSizePercent >= 0)
340
            {
341
                aSize[i] = ((nSizeTotal * ((100 - nSizePercent) / aSize.count()-1)) / 100);
342
            }
343
            else
344
            {
345
                aSize[i] = (nSizeTotal - nSizePixels) / (aSize.count()-1);
346
            }
347
        }
348
    }
349
 
350
    /* Calcualte ocupied size of the other items */
351
    for(i = 0, nSizeTotalOthers = 0; i < aSize.count(); i++)
352
    {
353
        if(i != nIndex)
354
            nSizeTotalOthers += aSize.at(i);
355
    }
356
 
357
    /* Calculate and set the size of the requested item */
358
    aSize[nIndex] = nSizeTotal - nSizeTotalOthers;
359
 
360
    this->ui->splitter->setSizes(aSize);
361
}
362
 
363
void CCCITT4Client::setVerticalSpitter(int nIndex, int nSizePercent)
364
{
365
    QList<int> aSize;
366
    int nSizeTotal, nSizeTotalOthers, i;
367
 
368
    if( (nSizePercent < 0) || (nSizePercent > 100) )
369
        return;
370
 
371
    /* Obtain current sizes */
372
    aSize = ui->splitterWidgetViewer->sizes();
373
 
374
    /* Verify that the splitter contains 3 items(log, image and graph) */
375
    if( (nIndex >= aSize.count()) || (nIndex < 0) )
376
        return;
377
 
378
    /* Calculate the total size */
379
    nSizeTotal = this->ui->splitterWidgetViewer->height()
380
            - (this->ui->splitterWidgetViewer->handleWidth() * (this->ui->splitterWidgetViewer->count()-1))
381
            - (this->ui->splitterWidgetViewer->lineWidth() * (this->ui->splitterWidgetViewer->count()-1) );
382
 
383
    /* Set the size for the other items */
384
    for(i = 0; i < aSize.count(); i++)
385
    {
386
        if(i != nIndex)
387
            aSize[i] = ((nSizeTotal * ((100 - nSizePercent) / aSize.count()-1)) / 100);
388
    }
389
 
390
    /* Calcualte ocupied size of the other items */
391
    for(i = 0, nSizeTotalOthers = 0; i < aSize.count(); i++)
392
    {
393
        if(i != nIndex)
394
            nSizeTotalOthers += aSize.at(i);
395
    }
396
 
397
    /* Calculate and set the size of the requested item */
398
    aSize[nIndex] = nSizeTotal - nSizeTotalOthers;
399
 
400
    this->ui->splitterWidgetViewer->setSizes(aSize);
401
}
402
 
403
void CCCITT4Client::keyPressEvent(QKeyEvent *event)
404
{
405
    if(event->key() == Qt::Key_Escape)
406
    {
407
 
408
    }
409
}
410
 
411
void CCCITT4Client::keyReleaseEvent(QKeyEvent *event)
412
{
413
    /*
414
     * On the first escape event cancel any pending request.
415
     * On the second escape event clear the log window.
416
     */
417
    if(event->key() == Qt::Key_Escape)
418
    {
419
        if(this->m_pSerialport->IsStateWaitForData() || ui->BtRepeat->isChecked())
420
        {
421
            this->m_pSerialport->CancelRequest();
422
 
423
            ui->BtSingleShot->setEnabled(true);
424
            ui->BtRepeat->setChecked(false);
425
        }
426
        else
427
        {
428
            this->ui->textEditRxLog->clear();
429
        }
430
    }
431
}
432
 
433
QString CCCITT4Client::byteToHexString(quint8 nValue)
434
{
435
    QString sResult;
436
 
437
    sResult = QString("%1").arg((int)nValue, 2, 16, QChar('0'));
438
 
439
    return sResult;
440
}
441
 
442
void CCCITT4Client::OnScreenRefreshTimer()
443
{
444
    QByteArray aRxData;
445
    int i;
446
    char cData;
447
 
448
    aRxData.clear();
449
    m_pSerialport->GetNewBytes(&aRxData);
450
 
451
    for(i = 0; i < aRxData.size(); i++)
452
    {
453
        cData = aRxData.at(i);
454
        /*
455
         * Print the data on the receive log
456
         */
457
        if(m_nCursorPos == 8)
458
        {
459
            ui->textEditRxLog->insertPlainText("\t");
460
        }
461
        if(m_nCursorPos >= 16)
462
        {
463
            ui->textEditRxLog->insertPlainText("\n");
464
            m_nCursorPos = 0;
465
        }
466
 
467
        ui->textEditRxLog->insertPlainText(byteToHexString(cData) + " ");
468
 
469
        m_nCursorPos++;
470
    }
471
 
472
    this->ui->statusBar->showMessage(QString("Expected:%1, received:%2").arg(m_pSerialport->GetBytesExpected()).arg(m_pSerialport->GetBytesReceived()));
473
}
474
 
475
void CCCITT4Client::OnShowErrorMessage(QString sMessage, bool bEnableBtSingleShot, bool bCheckedBtRepeat)
476
{
477
    QMessageBox msgBox;
478
 
479
    msgBox.setText(sMessage);
480
    msgBox.exec();
481
 
482
    ui->BtSingleShot->setEnabled(bEnableBtSingleShot);
483
    ui->BtRepeat->setChecked(bCheckedBtRepeat);
484
 
485
    /* Update the file list */
486
    this->UpdateFileList();
487
}
488
 
489
void CCCITT4Client::OnFrameCompleted(QString sFilename)
490
{
491
    QImage *pImage;
492
 
493
    /*
494
     * Call this function to be sure that all the data is printed before
495
     * starting a new request.
496
     */
497
    OnScreenRefreshTimer();
498
 
499
    /* Update the file list */
500
    this->UpdateFileList();
501
 
502
    this->ui->graphicsView->Scene->clear();
503
 
504
    pImage = new QImage(sFilename);
505
    if(pImage->isNull())
506
    {
507
        return;
508
    }
509
 
510
    this->ui->graphicsView->Scene->addItem(new QGraphicsPixmapItem(QPixmap::fromImage(*pImage)));
511
    this->show();
512
 
513
    /* Move the splitter to enhance the image viewer. */
514
    setVerticalSpitter(0, 80);
515
 
516
    ui->BtSingleShot->setEnabled(true);
517
 
518
    /* Request for a new frame if the repeat mode is active */
519
    if(ui->BtRepeat->isChecked())
520
    {
521
        OnBtSingleShotClicked();
522
    }
523
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.