275 lines
9.1 KiB
C++
275 lines
9.1 KiB
C++
#pragma execution_character_set("utf-8")
|
|
#include "QMainwindow.h"
|
|
#include <QSettings>
|
|
#include <QFile>
|
|
#include <QTextStream>
|
|
#include <QTimer>
|
|
#include <QDebug>
|
|
QMainwindow::QMainwindow(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
{
|
|
ui.setupUi(this);
|
|
QLogManager::Instance()->setLogLevel(QLogManager::SQ_INFO);;
|
|
m_nReplyCount = 0;
|
|
m_dataAcquisition = new QDataAcquisition;
|
|
m_netCheckTimer = new QTimer(this);
|
|
m_netCheckTimer->start(1000);
|
|
connect(m_netCheckTimer, &QTimer::timeout, this, [=]() {
|
|
if (m_dataAcquisition->m_InterfaceFutu->bConnected)
|
|
ui.lblNetStatu->setStyleSheet("background-color: green;");
|
|
else
|
|
ui.lblNetStatu->setStyleSheet("background-color: red;");
|
|
});
|
|
initWidget();
|
|
initReplyManage();
|
|
logViewer = new QLogViewer;
|
|
ui.verticalLayout_9->addWidget(logViewer);
|
|
LOG_INFO("Start Programm!");
|
|
QBigOrderViewer* viewer = new QBigOrderViewer();
|
|
ui.layoutBigorderViewer->addWidget(viewer);
|
|
QObject::connect(
|
|
viewer,
|
|
&QBigOrderViewer::sendStockCodeToMainForm,
|
|
this,
|
|
[this](const QString& code) {
|
|
int nIdex = 0;
|
|
for (const auto& item : m_replyCodes) {
|
|
if (item->stockCode == code)
|
|
break;
|
|
nIdex++;
|
|
}
|
|
if (nIdex < 0 || nIdex >= m_model->rowCount()) return;
|
|
ui.tableReplylist->clearSelection();
|
|
QModelIndex topLeft = m_model->index(nIdex, 0);
|
|
QModelIndex bottomRight = m_model->index(nIdex, m_model->columnCount() - 1);
|
|
QItemSelection selection(topLeft, bottomRight);
|
|
ui.tableReplylist->selectionModel()->select(selection, QItemSelectionModel::Select);
|
|
ui.tableReplylist->scrollTo(m_model->index(nIdex, 0), QAbstractItemView::PositionAtTop);
|
|
ui.tableReplylist->update();
|
|
}, Qt::QueuedConnection
|
|
);
|
|
auto manager = QBigOrderManager::instance();
|
|
QObject::connect(
|
|
manager,
|
|
&QBigOrderManager::markBigOrderSignal,
|
|
this,
|
|
[this]() {
|
|
m_lightWidget->triggerSignal();
|
|
}, Qt::QueuedConnection
|
|
);
|
|
if (!m_dataAcquisition || !m_dataAcquisition->m_InterfaceFutu) {
|
|
qCritical() << "Invalid data acquisition or interface";
|
|
return;
|
|
}
|
|
auto* processor = &m_dataAcquisition->m_InterfaceFutu->processor;
|
|
bool askConnected = QObject::connect(
|
|
processor,
|
|
&QOrderProcessor::maxOrderReady,
|
|
this,
|
|
[this](BigOrderInfo bigOrderInfo) {
|
|
if (auto manager = QBigOrderManager::instance()) {
|
|
if (!manager->addBigOrder(bigOrderInfo))
|
|
;
|
|
}
|
|
},Qt::QueuedConnection
|
|
);
|
|
QObject::connect(ui.btnDoReply, &QPushButton::clicked, [this]() {
|
|
m_nReplyCount = m_setReplayCode.size();
|
|
LOG_INFO("Start Reply!");
|
|
ui.btnDoReply->setText("ڼ ...");
|
|
m_dataAcquisition->m_InterfaceFutu->processor.setreplyCodeQuantity(m_replyCodeQuantity);
|
|
m_dataAcquisition->m_InterfaceFutu->subStock();
|
|
});
|
|
QObject::connect(ui.btnConnOpend, &QPushButton::clicked, [this]() {
|
|
if (!m_dataAcquisition->m_InterfaceFutu->bConnected)
|
|
m_dataAcquisition->m_InterfaceFutu->InitConn();
|
|
});
|
|
}
|
|
QMainwindow::~QMainwindow()
|
|
{
|
|
if (m_dataAcquisition)
|
|
{
|
|
delete m_dataAcquisition;
|
|
m_dataAcquisition = NULL;
|
|
}
|
|
}
|
|
void QMainwindow::initWidget()
|
|
{
|
|
m_model = new QStandardItemModel(0, 2, this);
|
|
m_proxyModel = new QSortFilterProxyModel(this);
|
|
m_proxyModel->setSourceModel(m_model);
|
|
ui.tableReplylist->setModel(m_proxyModel);
|
|
ui.tableReplylist->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
ui.tableReplylist->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
|
m_model->setHorizontalHeaderLabels(
|
|
QStringList() << "Ʊ" << "ֵ");
|
|
QObject::connect(m_model, &QStandardItemModel::dataChanged,
|
|
[&](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles) {
|
|
for (int row = topLeft.row(); row <= bottomRight.row(); ++row) {
|
|
for (int col = topLeft.column(); col <= bottomRight.column(); ++col) {
|
|
QModelIndex index = m_model->index(row, col);
|
|
QString newData = m_model->data(index, Qt::DisplayRole).toString();
|
|
m_replyCodes.at(row)->stockQuantityTh = newData.mid(0, newData.size() - 2).toFloat() * 1000;
|
|
}
|
|
}
|
|
});
|
|
QObject::connect(ui.tableReplylist->selectionModel(), &QItemSelectionModel::selectionChanged,
|
|
[&](const QItemSelection &selected, const QItemSelection &deselected) {
|
|
Q_UNUSED(deselected);
|
|
QModelIndexList selectedIndexes = ui.tableReplylist->selectionModel()->selectedIndexes();
|
|
if (selectedIndexes.isEmpty()) {
|
|
return;
|
|
}
|
|
int selectedRow = selectedIndexes.at(0).row();
|
|
ui.editInputCode->setText(m_replyCodes.at(selectedRow)->stockCode);
|
|
ui.editQuantityTh->setText(QString::number(m_replyCodes.at(selectedRow)->stockQuantityTh / 1000,'f',1));
|
|
});
|
|
m_lightWidget = new QBreathingLight();
|
|
ui.layoutBreathing->addWidget(m_lightWidget, 0, Qt::AlignCenter);
|
|
}
|
|
void QMainwindow::initReplyManage()
|
|
{
|
|
readReplyCodefile(m_replyCodes);
|
|
LOG_INFO("бسɹ!");
|
|
m_dataAcquisition->m_InterfaceFutu->setReplyList(m_replyCodes);
|
|
QIntValidator *intValidator = new QIntValidator(1, 99999, this);
|
|
ui.editInputCode->setValidator(intValidator);
|
|
QObject::connect(ui.btnAddCode, &QPushButton::clicked, [=]()
|
|
{
|
|
ReplyCodeItem order;
|
|
order.stockCode = toFixedDigitNumber(ui.editInputCode->text());
|
|
order.stockQuantityTh = ui.editQuantityTh->text().toFloat() * 1000;
|
|
bool bFinded = false;
|
|
for (const auto& item : m_replyCodes) {
|
|
if (item->stockCode == order.stockCode)
|
|
{
|
|
item->stockQuantityTh = order.stockQuantityTh;
|
|
m_replyCodeQuantity[item->stockCode] = order.stockQuantityTh;
|
|
bFinded = true;
|
|
LOG_INFO(",룺" + order.stockCode + " ,ֵ"+QString::number(order.stockQuantityTh));
|
|
break;
|
|
}
|
|
}
|
|
if (!bFinded)
|
|
{
|
|
if (m_replyCodes.size() + 1 > 50)
|
|
{
|
|
LOG_WARNING("бʧܣ!");
|
|
return;
|
|
}
|
|
auto newOrder = QSharedPointer<ReplyCodeItem>::create(order);
|
|
m_replyCodes.append(newOrder);
|
|
m_replyCodeQuantity.insert(order.stockCode, order.stockQuantityTh);
|
|
LOG_INFO(",룺" + order.stockCode + " ,ֵ" + QString::number(order.stockQuantityTh));
|
|
}
|
|
updateCodeTable();
|
|
});
|
|
QObject::connect(ui.btnDeleteCode, &QPushButton::clicked, [=]() {
|
|
QModelIndexList selected = ui.tableReplylist->selectionModel()->selectedRows();
|
|
for (int i = selected.size() - 1; i >= 0; --i) {
|
|
m_replyCodes.removeAt(selected[i].row());
|
|
m_replyCodeQuantity.remove(toFixedDigitNumber(ui.editInputCode->text()));
|
|
}
|
|
updateCodeTable();
|
|
});
|
|
QObject::connect(ui.btnSaveCodefile, &QPushButton::clicked, [=]() {
|
|
saveReplyCodefile();
|
|
m_dataAcquisition->m_InterfaceFutu->processor.setreplyCodeQuantity(m_replyCodeQuantity);
|
|
});
|
|
}
|
|
void QMainwindow::updateCodeTable()
|
|
{
|
|
m_model->removeRows(0, m_model->rowCount());
|
|
for (const auto& order : m_replyCodes) {
|
|
QList<QStandardItem*> rowItems;
|
|
QStandardItem* codeItem = new QStandardItem(order->stockCode);
|
|
codeItem->setTextAlignment(Qt::AlignCenter);
|
|
rowItems << codeItem;
|
|
QStandardItem* quanityItem = new QStandardItem(QString::number(order->stockQuantityTh / 1000, 'f', 1) + "K");
|
|
quanityItem->setTextAlignment(Qt::AlignCenter);
|
|
rowItems << quanityItem;
|
|
m_model->appendRow(rowItems);
|
|
}
|
|
}
|
|
QString QMainwindow::toFixedDigitNumber(const QString& input) {
|
|
QRegularExpression re("^\\d+$");
|
|
if (!re.match(input).hasMatch()) {
|
|
return QString();
|
|
}
|
|
if (input.length() > 5) {
|
|
return input.right(5);
|
|
}
|
|
else if (input.length() < 5) {
|
|
return QString("%1").arg(input, 5, '0');
|
|
}
|
|
return input;
|
|
}
|
|
void QMainwindow::readReplyCodefile(QList<QSharedPointer<ReplyCodeItem>>& replyList)
|
|
{
|
|
QString appDir = QCoreApplication::applicationDirPath();
|
|
QString fileName = QDir(appDir).absoluteFilePath("../../config/replyCodeList.csv");
|
|
QFile file(fileName);
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
LOG_FATAL("ļ");
|
|
}
|
|
QTextStream in(&file);
|
|
in.setCodec("UTF-8");
|
|
replyList.clear();
|
|
m_replyCodeQuantity.clear();
|
|
bool isFirstLine = true;
|
|
while (!in.atEnd()) {
|
|
QString line = in.readLine();
|
|
if (line.trimmed().isEmpty()) {
|
|
continue;
|
|
}
|
|
if (isFirstLine) {
|
|
isFirstLine = false;
|
|
continue;
|
|
}
|
|
QStringList fields = line.split(',');
|
|
if (1) {
|
|
QString code = fields[0].trimmed();
|
|
QString description = fields[1].trimmed();
|
|
ReplyCodeItem replyItem;
|
|
replyItem.stockCode = toFixedDigitNumber(code);
|
|
replyItem.stockQuantityTh = description.toFloat();
|
|
auto item = QSharedPointer<ReplyCodeItem>::create(replyItem);
|
|
m_replyCodes.append(item);
|
|
m_replyCodeQuantity.insert(replyItem.stockCode, replyItem.stockQuantityTh);
|
|
}
|
|
else {
|
|
}
|
|
}
|
|
file.close();
|
|
updateCodeTable();
|
|
}
|
|
void QMainwindow::saveReplyCodefile()
|
|
{
|
|
QString appDir = QCoreApplication::applicationDirPath();
|
|
QString fileName = QDir(appDir).absoluteFilePath("../../config/replyCodeList.csv");
|
|
QFile file(fileName);
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
|
LOG_FATAL("ļ" );
|
|
return;
|
|
}
|
|
QTextStream out(&file);
|
|
out.setCodec("GBK");
|
|
for (int col = 0; col < m_model->columnCount(); ++col) {
|
|
out << escapeCsv(m_model->headerData(col, Qt::Horizontal).toString());
|
|
if (col < m_model->columnCount() - 1) out << ",";
|
|
}
|
|
out << "\n";
|
|
for (const auto &value : m_replyCodes) {
|
|
out << value->stockCode<<","<< value->stockQuantityTh << "\n";
|
|
}
|
|
file.close();
|
|
LOG_INFO("бɹ!");
|
|
}
|
|
QString QMainwindow::escapeCsv(const QString &field) {
|
|
if (field.contains(',') || field.contains('"') || field.contains('\n')) {
|
|
QString escaped = field;
|
|
escaped.replace("\"", "\"\"");
|
|
return "\"" + escaped + "\"";
|
|
}
|
|
return field;
|
|
} |