/
e-face
/
SuperPuperTerminal
Обзор
Документация
Войти
/
e-face
/
SuperPuperTerminal
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
src/baseconnection.cpp
360 строк
10 KB
e-face
First one
14 фев 2026, 19:38
14 фев 2026, 19:38
87f499b
Код
Авторство
О чём код?
#include "baseconnection.h" #include <QBoxLayout> #include <QHeaderView> #include <QScrollBar> #include <QTimer> #include <QFile> #include <QDateTime> #include <QInputDialog> #include <QLabel> #include <QSpinBox> #include <QVBoxLayout> #include <QDialogButtonBox> #include <QDialog> #include "icons.h" //---------------------------------------------- BaseConnection::BaseConnection(QWidget *parent) : QWidget(parent) { static int CTR = 0; _sessId = ++CTR; _counter = 0; _toolBar = new QToolBar(this); _table = new QTableWidget(this); _runBtn = new QToolButton(this); _inputFld = new QTextEdit(this); _splitter = new QSplitter(Qt::Vertical, this); QWidget* w = new QWidget(this); _splitter->addWidget(_table); _splitter->addWidget(w); QBoxLayout* l = new QBoxLayout(QBoxLayout::TopToBottom, this); l->addWidget(_toolBar); l->addWidget(_splitter); l->setSpacing(0); l->setMargin(2); QBoxLayout* l2 = new QBoxLayout(QBoxLayout::LeftToRight, w); l2->addWidget(_inputFld); l2->addWidget(_runBtn); l2->setSpacing(0); l2->setMargin(0); _runBtn->setText(">>"); _runBtn->setFixedWidth(48); _runBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Preferred); connect(_runBtn, SIGNAL(clicked(bool)), this, SLOT(sltSend())); _table->setColumnCount(2); _table->setColumnWidth(0, 32); _table->horizontalHeader()->hide(); _table->verticalHeader()->hide(); _table->setSelectionBehavior(QAbstractItemView::SelectRows); _table->setSelectionMode(QAbstractItemView::NoSelection); _table->setSortingEnabled(false); _table->horizontalHeader()->setStretchLastSection(true); _table->setAutoScroll(true); _table->setEditTriggers(QAbstractItemView::NoEditTriggers); _table->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); _table->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel); _inputFld->setFontPointSize(14); _inputFld->setFontFamily("Courier New"); _scriptEngine.installTarget(&_scriptTarget); _title = tr("Подключение"); _checkConnTimer = new QTimer(this); _checkConnTimer->setInterval(500); _checkConnTimer->setSingleShot(false); _checkConnTimer->start(); connect(_checkConnTimer, SIGNAL(timeout()), SLOT(sltCheckConnTimer())); setupToolBar(); } //---------------------------------------------- QString BaseConnection::title() const { return _title; } //---------------------------------------------- void BaseConnection::clog(QString msg) { QFile log("conn.log"); if (!log.open(QFile::ReadWrite | QFile::Append)) return; QString data = QDateTime::currentDateTime() .toString("[yyyy/MM/dd hh.mm.ss.zzz] ") + msg; log.write(data.toUtf8()); log.write("\n"); log.close(); qDebug() << data; } //---------------------------------------------- void BaseConnection::setData(const QString &data) { } //---------------------------------------------- QString BaseConnection::data() const { return "0"; } //---------------------------------------------- QString BaseConnection::basicData() const { return _inputFld->toPlainText(); } //---------------------------------------------- void BaseConnection::setBasicData(const QString &data) { _inputFld->setPlainText(data); } //---------------------------------------------- #include "tcp/tcpconnection.h" #include "udp/udpconnection.h" #include "serial/serialconnection.h" #include "QMetaObject" QWidget *BaseConnection::createSettingsPage(const QString& type, QWidget *parent) { if (type == "UDP_CLIENT") { QWidget* w = UDPConnection::createSettingsPage(); w->setProperty("IS_SETTINGS_PAGE", "true"); w->setProperty("CONN_SETTINGS", UDPConnection::defaultSettings()); QMetaObject::invokeMethod(w, "applySettings"); return w; } if (type == "Serial") { QWidget* w = SerialConnection::createSettingsPage(); w->setProperty("IS_SETTINGS_PAGE", "true"); w->setProperty("CONN_SETTINGS", SerialConnection::defaultSettings()); QMetaObject::invokeMethod(w, "applySettings"); return w; } QWidget* w = TCPConnection::createSettingsPage(); w->setProperty("IS_SETTINGS_PAGE", "true"); w->setProperty("CONN_SETTINGS", TCPConnection::defaultSettings()); QMetaObject::invokeMethod(w, "applySettings"); return w; } //---------------------------------------------- BaseConnection *BaseConnection::createConnection(QWidget *settingsPage) { if (settingsPage->property("IS_SETTINGS_PAGE").toString() != "true") { return 0; } return createConnection(settingsPage->property("CONN_SETTINGS").toString()); } //---------------------------------------------- BaseConnection *BaseConnection::createConnection(QString settings) { ConnSettings cs = unpackSettings(settings); if (!cs.contains("type")) return 0; if (cs["type"] == "TCPClient") { return new TCPConnection(settings); } if (cs["type"] == "UDPClient") { return new UDPConnection(settings); } if (cs["type"] == "Serial") { return new SerialConnection(settings); } return 0; } //---------------------------------------------- void BaseConnection::read(QByteArray data) { addItem(data, true); } //---------------------------------------------- void BaseConnection::write(QByteArray data) { addItem(data, false); } //---------------------------------------------- void BaseConnection::addItem(QByteArray data, bool isInput) { BaseConnection::clog(tr("%1 (%2): %3 = %4") .arg(title()) .arg(sessId()) .arg(isInput ? "I" : "O") .arg(QString(data.toHex()))); QTableWidgetItem* ci; _table->setRowCount(_table->rowCount() + 1); _table->setCellWidget(_table->rowCount() - 1, 1, new DataItem(data, isInput, this)); _table->setItem(_table->rowCount() - 1, 0, ci = new QTableWidgetItem( QString::number(_counter++))); _table->setRowHeight(_table->rowCount() - 1, 48); //_table->resizeRowToContents(_table->rowCount() - 1); ci->setTextAlignment(Qt::AlignCenter); ci->setBackgroundColor(isInput ? BgInputColor : BgOutputColor); if (_table->rowCount() > 10000) { _table->removeRow(0); } _table->scrollToBottom(); } //---------------------------------------------- QByteArray BaseConnection::prepareQuery() { _scriptTarget.reset(); _scriptEngine.execute(_inputFld->toPlainText()); return _scriptTarget.result(); } //---------------------------------------------- void BaseConnection::sltSend() { write(prepareQuery()); } //---------------------------------------------- void BaseConnection::sltCheckConnTimer() { shot(); int cs = isConnected(); if (_isConnected == cs) return; _isConnected = cs; QColor c; switch (_isConnected) { case 1: c = QColor(228, 233, 35); break; case 2: c = QColor(136, 224, 39); break; default: c = QColor(247, 108, 55); } QWidget* w = parentWidget() ? parentWidget() : this; QPalette p = w->palette(); p.setColor(QPalette::Window, c); w->setPalette(p); } //---------------------------------------------- void BaseConnection::setupToolBar() { _actionSettings = new QAction(ConnectionIcons::settingsIcon(), tr("Настройки"), this); _actionReceive = new QAction(ConnectionIcons::receiveIcon(), tr("Приём"), this); _actionReceive->setCheckable(true); _actionReceive->setChecked(true); _actionTransmit = new QAction(ConnectionIcons::transmitIcon(), tr("Передача"), this); _actionTransmit->setCheckable(true); _actionTransmit->setChecked(true); _actionClear = new QAction(ConnectionIcons::clearIcon(), tr("Очистить"), this); _toolBar->addAction(_actionSettings); _toolBar->addSeparator(); _toolBar->addAction(_actionReceive); _toolBar->addAction(_actionTransmit); _toolBar->addSeparator(); _toolBar->addAction(_actionClear); connect(_actionSettings, &QAction::triggered, this, &BaseConnection::sltShowSettings); connect(_actionReceive, &QAction::toggled, this, &BaseConnection::sltToggleReceive); connect(_actionTransmit, &QAction::toggled, this, &BaseConnection::sltToggleTransmit); connect(_actionClear, &QAction::triggered, this, &BaseConnection::sltClear); } //---------------------------------------------- void BaseConnection::sltShowSettings() { QDialog dlg(this); dlg.setWindowTitle(tr("Настройки отображения")); QVBoxLayout* layout = new QVBoxLayout(&dlg); QSpinBox* spinMaxItems = new QSpinBox(&dlg); spinMaxItems->setRange(100, 100000); spinMaxItems->setValue(_maxHistoryItems); spinMaxItems->setPrefix(tr("Макс. количество строк: ")); layout->addWidget(spinMaxItems); QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, &dlg); layout->addWidget(buttons); connect(buttons, &QDialogButtonBox::accepted, &dlg, &QDialog::accept); connect(buttons, &QDialogButtonBox::rejected, &dlg, &QDialog::reject); if (dlg.exec() == QDialog::Accepted) { _maxHistoryItems = spinMaxItems->value(); } } //---------------------------------------------- void BaseConnection::sltToggleReceive(bool checked) { _showInput = checked; } //---------------------------------------------- void BaseConnection::sltToggleTransmit(bool checked) { _showOutput = checked; } //---------------------------------------------- void BaseConnection::sltClear() { _table->setRowCount(0); _counter = 0; } //----------------------------------------------