-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainWnd.cpp.cc
More file actions
352 lines (277 loc) · 10.2 KB
/
mainWnd.cpp.cc
File metadata and controls
352 lines (277 loc) · 10.2 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: mainWnd.cpp
* Author: andy
*
* Created on March 21, 2017, 11:47 AM
*/
#include <QSqlDatabase>
#include <QtSql>
#include <QApplication>
#include <QHBoxLayout>
#include <QObjectList>
#include <QDateTime>
#include <QDate>
#include <QTreeWidgetItem>
#include <QStandardItemModel>
#include <QVariant>
#include <QAction>
#include "financedemo.h"
#include "FinanceChart.h"
#include "mainWnd.h"
#include "ui_mainWnd.h"
#include "aboutForm.h"
#include "Open_Positions.h"
#include "Num_Bids.h"
#include "Num_Offers.h"
mainWnd::mainWnd() {
widget.setupUi(this);
// Tree
QStandardItem *itemHeader = new QStandardItem(QString("Charts"));
model->setHorizontalHeaderItem(0, itemHeader);
QStandardItem *parentItem = model->invisibleRootItem();
QStandardItem *item0 = new QStandardItem(QString("Opened interest"));
parentItem->appendRow(item0);
chart1 = new Open_Positions();
QStandardItem *item1 = new QStandardItem(chart1->getName());
QVariant var1 = QVariant::fromValue(chart1->getNoOfCharts());
item1->setData(var1);
item0->appendRow(item1);
chart2 = new Num_Bids();
QStandardItem *item2 = new QStandardItem(chart2->getName());
QVariant var2 = QVariant::fromValue(chart2->getNoOfCharts());
item2->setData(var2);
item0->appendRow(item2);
chart3 = new Num_Offers();
QStandardItem *item3 = new QStandardItem(chart3->getName());
QVariant var3 = QVariant::fromValue(chart3->getNoOfCharts());
item3->setData(var3);
item0->appendRow(item3);
widget.treeView->setModel(model);
widget.treeView->expandAll();
connect(widget.treeView->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(treeViewClickProgress(const QModelIndex &, const QModelIndex &)));
demo.create(widget.widgetChartView);
connect(widget.actionAbout, SIGNAL(triggered()), this, SLOT(menuAbout()));
}
mainWnd::~mainWnd() {
}
void mainWnd::treeViewClickProgress(const QModelIndex &index0, const QModelIndex &index1) {
int rowNum = widget.treeView->selectionModel()->currentIndex().row();
int colNum = widget.treeView->selectionModel()->currentIndex().column();
QVariant data = widget.treeView->selectionModel()->model()->data(widget.treeView->selectionModel()->currentIndex());
const QString strData = data.toString();
//char* text = strData.toLatin1().data();
const QString text1 = chart1->getName();
const QString text2 = chart2->getName();
const QString text3 = chart3->getName();
if (strData == text1)
demo.drawChart(chart1);
else if (strData == text2)
demo.drawChart(chart2);
else if (strData == text3)
demo.drawChart(chart3);
}
void mainWnd::menuAbout() {
about = new aboutForm();
if (about->exec() == QDialog::Accepted) {
// show dialog
}
delete about;
}
//
// Because QT uses QDateTime, while ChartDirector uses Chart::chartTime, we need
// utilities to convert from one to another
//
// Convert from QDateTime to chartTime
static double QDateTimeToChartTime(QDateTime q) {
QDate d = q.date();
QTime t = q.time();
return Chart::chartTime(d.year(), d.month(), d.day(), t.hour(), t.minute(),
t.second()) + t.msec() / 1000.0;
}
FinanceDemo::FinanceDemo()
: QWidget() {
}
//
// Destructor
//
FinanceDemo::~FinanceDemo() {
if (db.isOpen())
db.close();
}
void FinanceDemo::create(QWidget *parent) {
//
// Set up the GUI
//
resize(954, 720);
QFont defaultFont("Arial");
defaultFont.setPixelSize(11);
setFont(defaultFont);
setPalette(QPalette(QColor(0xff, 0xff, 0xff)));
// Use horizontal layout to put the tree view on the left and the charts on the right
QVBoxLayout *vertLayout = new QVBoxLayout(this);
vertLayout->setMargin(0);
vertLayout->setSpacing(0);
parent->setLayout(vertLayout);
// The frame on the left side
QWidget *leftPanel = new QWidget(this);
leftPanel->setAutoFillBackground(true);
leftPanel->setPalette(QPalette(QColor(186, 221, 255)));
leftPanel->setFixedWidth(1035);
leftPanel->setFixedHeight(75);
vertLayout->addWidget(leftPanel);
QFrame *separator = new QFrame(this);
separator->setFrameStyle(QFrame::VLine | QFrame::Plain);
vertLayout->addWidget(separator);
QWidget *rightPanel = new QWidget(this);
vertLayout->addWidget(rightPanel);
rightPanel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// Ticker Symbol
(new QLabel("Ticker Symbol", leftPanel))->setGeometry(8, 10, 140, 18);
m_TickerSymbol = new QComboBox(leftPanel);
m_TickerSymbol->setGeometry(8, 26, 140, 20);
// Time Period
(new QLabel("Time Period", leftPanel))->setGeometry(155, 10, 140, 18);
m_TimeRange = new QComboBox(leftPanel);
m_TimeRange->setGeometry(155, 26, 140, 20);
// Chart Type
(new QLabel("Chart Type", leftPanel))->setGeometry(302, 10, 140, 18);
m_ChartType = new QComboBox(leftPanel);
m_ChartType->setGeometry(302, 26, 140, 20);
// Price Type
(new QLabel("Price Type", leftPanel))->setGeometry(449, 10, 140, 18);
m_PriceType = new QComboBox(leftPanel);
m_PriceType->setGeometry(449, 26, 140, 20);
// Date From
(new QLabel("From", leftPanel))->setGeometry(596, 10, 140, 18);
m_DateFrom = new QDateEdit(leftPanel);
m_DateFrom->setGeometry(596, 26, 140, 20);
m_DateFrom->setDate(QDate(2016, 02, 01));
m_DateFrom->setCalendarWidget(m_CalFrom);
m_DateFrom->setCalendarPopup(true);
// Date To
(new QLabel("To", leftPanel))->setGeometry(743, 10, 140, 18);
m_DateTo = new QDateEdit(leftPanel);
m_DateTo->setGeometry(743, 26, 140, 20);
m_DateTo->setDate(QDate(2016, 02, 10));
m_DateTo->setCalendarWidget(m_CalTo);
m_DateTo->setCalendarPopup(true);
// Chart Size
(new QLabel("Chart Size", leftPanel))->setGeometry(890, 10, 140, 18);
m_ChartSize = new QSlider(Qt::Horizontal, leftPanel);
m_ChartSize->setGeometry(890, 26, 140, 20);
m_ChartSize->setPageStep(1);
m_ChartSize->setSingleStep(0);
m_ChartSize->setMinimum(5);
m_ChartSize->setMaximum(15);
m_ChartSize->setSliderPosition(9);
m_ChartSize->setTracking(true);
ballSize = 9;
QFont labelFont(defaultFont);
labelFont.setBold(true);
const QObjectList& children = leftPanel->children();
for (int i = 0; i < children.size(); ++i) {
QLabel *label = qobject_cast<QLabel *>(children[i]);
if (0 != label)
label->setFont(labelFont);
}
// Chart Viewer
m_ChartViewer = new QChartViewer(rightPanel);
m_ChartViewer->move(8, 12);
//
// Fill the contents of the combo boxes
//
if (!db.isOpen()) {
db = QSqlDatabase::addDatabase("QPSQL", "stocks_conn");
db.setHostName("localhost");
db.setDatabaseName("consoledb2");
db.setUserName("");
db.setPassword("");
bool ok = db.open();
}
QSqlTableModel *model0 = new QSqlTableModel(this, db);
model0->setTable("\"public\".\"stocks\"");
model0->setFilter(QString("Active = true"));
model0->select();
for (int i = 0; i < model0->rowCount(); i++) {
QSqlRecord rec = model0->record(i);
int id = rec.value("id").toInt();
QString ticker = rec.value("ticker").toString();
m_TickerSymbol->addItem(ticker, id);
}
m_TickerSymbol->setCurrentIndex(282);
QSqlTableModel *model1 = new QSqlTableModel(this, db);
model1->setTable("\"public\".\"timeframes\"");
model1->setFilter(QString("Active = true"));
model1->select();
for (int i = 0; i < model1->rowCount(); i++) {
QSqlRecord rec = model1->record(i);
int tfsec = rec.value("id").toInt();
QString tfname = rec.value("timeframename").toString();
m_TimeRange->addItem(tfname, tfsec);
}
m_TimeRange->setCurrentIndex(2);
m_PriceType->addItem("By close", "ByClose");
m_PriceType->addItem("By average price", "ByAvg");
m_ChartType->addItem("None", "None");
m_ChartType->addItem("CandleStick", "CandleStick");
m_ChartType->addItem("Closing Price", "Close");
m_ChartType->addItem("Median Price", "Median");
m_ChartType->addItem("OHLC", "OHLC");
m_ChartType->addItem("Typical Price", "TP");
m_ChartType->addItem("Weighted Close", "WC");
m_ChartType->setCurrentIndex(1);
// Use the same action hanlder for all controls
const QObjectList &allControls = leftPanel->children();
for (int i = 0; i < allControls.count(); ++i) {
QObject *obj;
if ((obj = qobject_cast<QComboBox *>(allControls[i])) != 0)
connect(obj, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboBoxChanged(int)));
else if ((obj = qobject_cast<QDateEdit *>(allControls[i])) != 0) {
connect(obj, SIGNAL(dateChanged(QDate)), this, SLOT(onDateChanged()));
} else if ((obj = qobject_cast<QSlider *>(allControls[i])) != 0)
connect(obj, SIGNAL(valueChanged(int)), SLOT(onSliderValueChanged(int)));
}
}
void FinanceDemo::onComboBoxChanged(int) {
drawChart();
}
void FinanceDemo::onDateChanged() {
drawChart();
}
void FinanceDemo::onSliderValueChanged(int) {
drawChart();
}
/////////////////////////////////////////////////////////////////////////////
// Chart Creation
/// <summary>
/// Draw the chart according to user selection and display it in the ChartViewer.
/// </summary>
/// <param name="viewer">The ChartViewer object to display the chart.</param>
void FinanceDemo::drawChart(DemoModule *chart) {
//
// Initialize member variables
//
m_Chart = chart;
drawChart();
}
void FinanceDemo::drawChart() {
//
// Initialize member variables
//
int ticker = m_TickerSymbol->itemData(m_TickerSymbol->currentIndex()).toInt();
int timeRange = m_TimeRange->itemData(m_TimeRange->currentIndex()).toInt();
QString chartType = m_ChartType->itemData(m_ChartType->currentIndex()).toString();
ballSize = m_ChartSize->value();
m_Chart->createChart(m_ChartViewer,
ticker,
m_DateFrom->dateTime(),
m_DateTo->dateTime(),
timeRange,
chartType,
ballSize);
}