-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
140 lines (117 loc) · 4.54 KB
/
mainwindow.cpp
File metadata and controls
140 lines (117 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <QDesktopServices>
#include <QDateTime>
#include "redpostparser.h"
#include "redposttrackercore.h"
#include "redpostviewmodel.h"
#include "redtrackersearcher.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
////////////////////////////////////////////////////////////////////////////////
const char* const STATUS_TEXT_TEMPLATE = "%1 (%2 posts found) pages processed.";
const char* const STATUS_TEXT_DONE = "Search complete. %1";
////////////////////////////////////////////////////////////////////////////////
/**
* Constructor.
*/
MainWindow::MainWindow( QWidget* parent /* = nullptr */ )
: QWidget( parent )
, m_pagesCompleted( 0 )
, m_maxPages( 0 )
, m_statusTextTemplate( QString( STATUS_TEXT_TEMPLATE ) )
, m_ui( new Ui::MainWindowClass )
{
m_ui->setupUi(this);
m_resultsModel = new RedPostViewModel( m_ui->m_tvResults );
m_ui->m_tvResults->setModel( m_resultsModel );
QHeaderView* header = m_ui->m_tvResults->horizontalHeader();
header->setSectionResizeMode( RedPostViewModel::ID_TITLE, QHeaderView::Stretch );
header->setSectionResizeMode( RedPostViewModel::ID_AUTHOR, QHeaderView::ResizeToContents );
header->setSectionResizeMode( RedPostViewModel::ID_DATE, QHeaderView::ResizeToContents );
m_ui->m_lePagesToLookup->setValidator( new QIntValidator( this ) );
m_ui->m_dteDateFilter->setDate( QDate::currentDate() );
connect( m_ui->m_tvResults, SIGNAL( doubleClicked( const QModelIndex& ) ), this, SLOT( onResultsItemDblClicked( const QModelIndex& ) ) );
connect( m_ui->m_btnStartSearch, SIGNAL( pressed() ), this, SLOT( onSearchRequested() ) );
connect( m_ui->m_leSearchField, SIGNAL( returnPressed() ), this, SLOT( onSearchRequested() ) );
connect( red_core.getRedSearcher(), SIGNAL( requestComplete() ), this, SLOT( onRequestCompleted() ) );
connect( red_core.getRedSearcher(), SIGNAL( failedToRetrivePage() ), this, SLOT( onFailedToRetrivePage() ) );
}
////////////////////////////////////////////////////////////////////////////////
/**
* Destructor.
*/
MainWindow::~MainWindow()
{
delete m_ui;
}
////////////////////////////////////////////////////////////////////////////////
/**
* This is slot that is called when a page has been successfully fetched from
* riot's APIs.
*/
void MainWindow::onRequestCompleted()
{
bool isSearchComplete = true;
if( m_maxPages == 0 || m_pagesCompleted < m_maxPages )
{
m_pagesCompleted++;
red_core.getRedSearcher()->requestNewRedTrackerPage();
isSearchComplete = false;
}
updateStatusLine( isSearchComplete );
}
////////////////////////////////////////////////////////////////////////////////
/**
* This is slot that is called when the user requests new search.
*/
void MainWindow::onSearchRequested()
{
RedTrackerSearcher* searcher = red_core.getRedSearcher();
QDate dateFilter = m_ui->m_dteDateFilter->date();
red_core.getRedPostParser()->clearData();
m_resultsModel->clearData();
QString pagesFiledText = m_ui->m_lePagesToLookup->text();
if( ! pagesFiledText.isEmpty() )
m_maxPages = pagesFiledText.toInt();
m_pagesCompleted = 0;
updateStatusLine();
searcher->setStartDate( std::move( dateFilter ) );
m_resultsModel->setFilterString( m_ui->m_leSearchField->text() );
searcher->requestNewRedTrackerPage();
}
////////////////////////////////////////////////////////////////////////////////
/**
* This is a slot that is called when the fetching of a page failed.
*/
void MainWindow::onFailedToRetrivePage()
{
qDebug() << "Failed to retrive a page";
}
////////////////////////////////////////////////////////////////////////////////
/**
* This is slot that is called when a resul from the table view are double
* clicked.
* The link associated with that row will be open in the user's browser.
*
* @param index The index that was double clicked.
*/
void MainWindow::onResultsItemDblClicked( const QModelIndex& index )
{
if( index.row() != RedPostViewModel::ID_TITLE )
return;
QDesktopServices::openUrl ( index.data( RedPostViewModel::ID_HREF ).toString() );
}
////////////////////////////////////////////////////////////////////////////////
/**
* This is a method that updates the status line.
*
* @param isSearchComplete Whether the search continues or not.
*/
void MainWindow::updateStatusLine( bool isSearchComplete /* = false */ )
{
QString statusLineText = m_statusTextTemplate.arg( m_pagesCompleted )
.arg( m_resultsModel->itemsSize() );
if( isSearchComplete )
statusLineText = QString( STATUS_TEXT_DONE ).arg( statusLineText );
m_ui->m_lblStatus->setText( statusLineText );
}
////////////////////////////////////////////////////////////////////////////////