-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
executable file
·107 lines (88 loc) · 2.98 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebFrame>
#include <QWebElement>
#include <QTimer>
#include "CommandAbstract.h"
#include "OpenGoogle.h"
#include "SearchSiteCmd.h"
#include "DatabaseConnector.h"
#include <QFileInfo>
#include <QSqlQueryModel>
#include <QWebView>
#include <QSqlQuery>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->searchButton, SIGNAL(clicked(bool)), this, SLOT(startSearch()));
m_view = new QWebView;
DatabaseConnector databaseConnector("localhost", dbFile().absoluteFilePath(), "admin", "admin");
databaseConnector.connect();
m_domainsModel = std::shared_ptr<QSqlQueryModel>(new QSqlQueryModel);
m_domainsModel->setQuery("SELECT * FROM Domains");
m_domainsModel->setHeaderData(0, Qt::Horizontal, tr("ID"));
m_domainsModel->setHeaderData(1, Qt::Horizontal, tr("Domain"));
ui->domainsView->setModel(m_domainsModel.get());
ui->domainsView->resizeColumnsToContents();
m_keywordsModel = std::shared_ptr<QSqlQueryModel>(new QSqlQueryModel);
ui->keywordsView->setModel(m_keywordsModel.get());
connect(ui->domainsView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(onDomainIndexChanged(QModelIndex,QModelIndex)));
std::shared_ptr<OpenGoogle>command(new OpenGoogle(m_view));
connect(command.get(), SIGNAL(loadFinished()), this, SLOT(onPageLoaded()));
runCommand(command);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::runCommand(std::shared_ptr<CommandAbstract> command)
{
m_command = command;
m_command->run();
}
void MainWindow::onPageLoaded()
{
}
void MainWindow::startSearch()
{
if(m_currDomainID <= 0)
return;
QSqlQuery query(QString("SELECT * FROM Domains WHERE ID= '%1'").arg(m_currDomainID));
if(query.next())
{
std::shared_ptr<SearchSiteCmd>command(new SearchSiteCmd(m_view, m_currDomainID));
connect(command.get(), SIGNAL(finished()), this, SLOT(onSearchFinished()));
runCommand(command);
}
}
void MainWindow::onDomainIndexChanged(QModelIndex curr, QModelIndex prev)
{
QModelIndex index = m_domainsModel->index(curr.row(), 0);
m_currDomainID = index.data().toInt();
m_keywordsModel->setQuery(QString("SELECT * FROM Keywords WHERE DomainID = '%1' ORDER BY Position DESC").arg(m_currDomainID));
ui->keywordsView->resizeColumnsToContents();
}
void MainWindow::onSearchFinished()
{
m_domainsModel->setQuery("SELECT * FROM Domains");
}
QFileInfo MainWindow::dbFile()
{
QDir dbDir(qApp->applicationDirPath());
#if defined(Q_OS_WIN)
#elif defined(Q_OS_MAC)
if (dbDir.dirName() == "MacOS") {
dbDir.cdUp();
}
else{
dbDir.cd(qApp->applicationName()+".app");
dbDir.cd("Contents");
}
#endif
if(!dbDir.cd("tools"))
dbDir.mkdir("tools");
dbDir.cd("tools");
return QFileInfo (dbDir, "Database.db");
}