emsApplication/applications/emsConfigurer/maindialog.cpp

260 lines
8.4 KiB
C++
Raw Normal View History

2024-09-11 17:09:26 +08:00
#include "maindialog.h"
2024-09-10 16:55:41 +08:00
#include "ui_maindialog.h"
2024-09-11 17:09:26 +08:00
#include <QToolBar>
#include <QListWidget>
#include <QStackedWidget>
#include <QVBoxLayout>
#include <QIcon>
#include <QSize>
#include <QPushButton>
#include <QSplitter>
#include <QSettings>
#include <QCloseEvent>
#include <QMessageBox>
#include <QTableView>
#include <QStandardItemModel>
2024-09-10 16:55:41 +08:00
#include <hv/hlog.h>
#include "kutilities.h"
2024-09-12 16:23:44 +08:00
#include "devicepropertypage.h"
2024-10-17 20:25:43 +08:00
#include "formserialportsettingdialog.h"
#include "aboutdialog.h"
2024-09-12 16:23:44 +08:00
2024-09-10 16:55:41 +08:00
MainDialog::MainDialog(QWidget *parent) :
2024-09-11 17:09:26 +08:00
QMainWindow (parent),
2024-09-10 16:55:41 +08:00
ui(new Ui::MainDialog)
{
2024-09-11 17:09:26 +08:00
//ui->setupUi(this);
this->setWindowIcon(QIcon(":/images/icon.png"));
this->setWindowTitle(tr("EMU Configurer Kit"));
2024-09-11 17:09:26 +08:00
2024-09-14 13:01:48 +08:00
InitializeUI();
2024-09-11 17:09:26 +08:00
// Load the window state
2024-09-14 13:01:48 +08:00
loadWindowState();
//设置返回消息回调
onMessageCallbackFunc f = std::bind(&MainDialog::onMessage, this, std::placeholders::_1, std::placeholders::_2);
AppTcpClient::getInstance()->resetOnMessageCallbackFunction(f);
2024-09-10 16:55:41 +08:00
}
MainDialog::~MainDialog()
{
delete m_pStackedWidgetLayout;
2024-09-10 16:55:41 +08:00
delete ui;
}
2024-09-11 17:09:26 +08:00
void MainDialog::InitializeUI()
{
// Create central widget and layout
2024-09-14 13:01:48 +08:00
this->takeCentralWidget();
2024-09-11 17:09:26 +08:00
QWidget *centralWidget = new QWidget(this);
QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
//Create toolbar
CreateToolbar();
// Set up the QListWidget
m_pDeviceListWidget = new QListWidget(this);
m_pDeviceListWidget->setStyleSheet("background-color:transparent");
m_pDeviceListWidget->setViewMode(QListView::IconMode);
m_pDeviceListWidget->setIconSize(QSize(70, 70));
m_pDeviceListWidget->setGridSize(QSize(145, 100)); // item 的大小
m_pDeviceListWidget->setMovement(QListView::Static);
m_pDeviceListWidget->setMaximumWidth(170);
m_pDeviceListWidget->setMinimumWidth(170); // Set minimum width
m_pDeviceListWidget->setResizeMode(QListView::Fixed);
m_pDeviceListWidget->setSpacing(25);
//m_pDeviceListWidget->setUniformItemSizes(true); // 所有的 item 一样大
m_pDeviceListWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
CreateIcon(QIcon(":/images/PagePower.png"),tr("Power"));
CreateIcon(QIcon(":/images/PageBattery.png"),tr("Battery"));
CreateIcon(QIcon(":/images/PageSwitch.png"),tr("Switch"));
CreateIcon(QIcon(":/images/PageAir.png"),tr("Air"));
CreateIcon(QIcon(":/images/PageFan.png"),tr("Fan"));
CreateIcon(QIcon(":/images/PageSensor.png"),tr("Sensor"));
// Set up the QStackedWidget
CreateTablePage();
// Set up the QSplitter to manage the resizing of QListWidget and QStackedWidget
QSplitter *splitter = new QSplitter(Qt::Horizontal, this);
splitter->addWidget(m_pDeviceListWidget);
splitter->addWidget(m_pDevicestackedWidget);
splitter->setSizes(QList<int>({200, 600})); // Set initial sizes (200 for QListWidget, 600 for QStackedWidget)
// Disable splitter dragging
splitter->setChildrenCollapsible(false);
2024-09-14 13:01:48 +08:00
// 将工具栏添加到主窗口的顶部
//addToolBar(Qt::TopToolBarArea, m_pMainToolBar);
2024-09-11 17:09:26 +08:00
// Set up layout
mainLayout->addWidget(m_pMainToolBar);
mainLayout->addWidget(splitter);
centralWidget->setLayout(mainLayout);
// Set central widget
setCentralWidget(centralWidget);
//resize(800, 600);
setMinimumSize(1024, 768);
2024-09-12 16:23:44 +08:00
connect(m_pDeviceListWidget, &QListWidget::currentItemChanged, this, &MainDialog::changePage);
2024-09-11 17:09:26 +08:00
}
void MainDialog::CreateToolbar()
{
m_pMainToolBar = new QToolBar(this);
m_pMainToolBar->setIconSize(QSize(48, 48));
2024-09-14 13:01:48 +08:00
// 创建一个水平布局以容纳图标和占位符
QWidget *toolBarWidget = new QWidget(this);
QHBoxLayout *layout = new QHBoxLayout(toolBarWidget);
layout->setContentsMargins(0, 0, 0, 0); // 去除内边距
// 添加一个弹性空间以推送图标到右侧
QSpacerItem *spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
layout->addItem(spacer);
2024-09-11 17:09:26 +08:00
// Create actions for the toolbar
2024-10-12 14:00:37 +08:00
QAction *action1 = new QAction(QIcon(":/images/icons8-add-64.png"), tr("Add Device"), this);
action1->setToolTip(tr("Add a new device"));
QAction *action2 = new QAction(QIcon(":/images/icons8-close-64.png"), tr("Remove Device"), this);
action2->setToolTip(tr("Remove a device"));
QAction *action4 = new QAction(QIcon(":/images/icons8-refresh-64.png"), tr("Refresh"), this);
action4->setToolTip(tr("Refresh"));
QAction *action3 = new QAction(QIcon(":/images/icon.png"), tr("About..."), this);
action3->setToolTip(tr("Show information about EMU Configurer toolkit"));
2024-09-11 17:09:26 +08:00
// Add actions to the toolbar
m_pMainToolBar->addAction(action1);
m_pMainToolBar->addAction(action2);
2024-10-12 14:00:37 +08:00
m_pMainToolBar->addAction(action4);
m_pMainToolBar->addSeparator();
2024-09-11 17:09:26 +08:00
m_pMainToolBar->addAction(action3);
2024-09-14 13:01:48 +08:00
// 将工具栏的 widget 设置为包含图标和占位符的 widget
m_pMainToolBar->addWidget(toolBarWidget);
2024-09-11 17:09:26 +08:00
// Connect actions to slots
connect(action1, &QAction::triggered, this, &MainDialog::onToolButton1Clicked);
2024-10-12 14:00:37 +08:00
connect(action2, &QAction::triggered, this, &MainDialog::onToolButton2Clicked);
connect(action4, &QAction::triggered, this, &MainDialog::onToolRefreshClicked);
2024-09-11 17:09:26 +08:00
connect(action3, &QAction::triggered, this, &MainDialog::onAboutButtonClicked);
}
void MainDialog::CreateTablePage()
{
m_pDevicestackedWidget = new QStackedWidget(this);
m_pStackedWidgetLayout = new QVBoxLayout(m_pDevicestackedWidget);
2024-09-14 13:01:48 +08:00
#if 1
m_pStackedWidgetLayout->addWidget(m_pDevicestackedWidget);
2024-09-11 17:09:26 +08:00
2024-09-12 16:23:44 +08:00
DevicePropertyPage *page1 = new DevicePropertyPage(m_pDevicestackedWidget);
2024-09-14 13:01:48 +08:00
DevicePropertyPage *page2 = new DevicePropertyPage(m_pDevicestackedWidget);
2024-09-11 17:09:26 +08:00
2024-09-12 16:23:44 +08:00
m_pDevicestackedWidget->addWidget(page1); // Add QTableView as a page
m_pDevicestackedWidget->addWidget(page2);
2024-09-11 17:09:26 +08:00
m_pDevicestackedWidget->addWidget(new QPushButton("Page 3"));
m_pDevicestackedWidget->addWidget(new QPushButton("Page 4"));
m_pDevicestackedWidget->addWidget(new QPushButton("Page 5"));
m_pDevicestackedWidget->addWidget(new QPushButton("Page 6"));
2024-09-14 13:01:48 +08:00
#endif
2024-09-11 17:09:26 +08:00
}
2024-09-14 13:01:48 +08:00
2024-09-11 17:09:26 +08:00
void MainDialog::CreateIcon(const QIcon& icon,QString text)
{
QListWidgetItem *itemButton = new QListWidgetItem(m_pDeviceListWidget);
itemButton->setIcon(icon);
itemButton->setText(text);
itemButton->setTextAlignment(Qt::AlignHCenter);
itemButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}
void MainDialog::changePage(QListWidgetItem *current, QListWidgetItem *previous)
{
if (!current)
current = previous;
m_pDevicestackedWidget->setCurrentIndex(m_pDeviceListWidget->row(current));
}
void MainDialog::closeEvent(QCloseEvent *event)
{
// Save the window state
saveWindowState();
QMainWindow::closeEvent(event);
}
void MainDialog::saveWindowState()
{
QSettings settings("HJ-NET", "EMSCFG");
settings.setValue("geometry", saveGeometry());
settings.setValue("windowState", saveState());
}
void MainDialog::loadWindowState()
{
QSettings settings("HJ-NET", "EMSCFG");
restoreGeometry(settings.value("geometry").toByteArray());
restoreState(settings.value("windowState").toByteArray());
}
2024-10-12 14:00:37 +08:00
//增加设备
2024-09-11 17:09:26 +08:00
void MainDialog::onToolButton1Clicked()
{
FormSerialPortSettingDialog* dlg = new FormSerialPortSettingDialog(this);
dlg->setWindowFlags(dlg->windowFlags()&~(Qt::WindowMinMaxButtonsHint|Qt::WindowContextHelpButtonHint));
//dlg.setModal(true);
dlg->show();
if(dlg->exec() == QDialog::Accepted)
2024-10-17 20:25:43 +08:00
QMessageBox::information(this, "OK Clicked", "Button 1 was clicked!");
else
QMessageBox::information(this, "Cancel Clicked", "Cancel was clicked!");
2024-09-11 17:09:26 +08:00
}
2024-10-12 14:00:37 +08:00
//删除设备
2024-09-11 17:09:26 +08:00
void MainDialog::onToolButton2Clicked()
{
QMessageBox::information(this, "Button Clicked", "Button 2 was clicked!");
}
2024-10-12 14:00:37 +08:00
//关于
2024-09-11 17:09:26 +08:00
void MainDialog::onAboutButtonClicked()
{
AboutDialog *aboutDlg = new AboutDialog(this);
aboutDlg->setWindowFlags(aboutDlg->windowFlags()&~(Qt::WindowMinMaxButtonsHint|Qt::WindowContextHelpButtonHint));
//aboutDlg->setWindowFlags(aboutDlg->windowFlags() | Qt::WindowStaysOnTopHint);
aboutDlg->setStyleSheet("border-radius: 4px;"); // 定制圆角
aboutDlg->show();
2024-09-11 17:09:26 +08:00
}
2024-10-12 14:00:37 +08:00
//刷新
void MainDialog::onToolRefreshClicked()
{
QMessageBox::information(this, "Refresh Button Clicked", "Refresh");
}
void MainDialog::onMessage(const hv::SocketChannelPtr& channel, hv::Buffer* buf)
{
hlogd("MainDialog <==\n%s", Kutilities::printHex((void*)buf->data(),(int)buf->size()).c_str());
}