emsApplication/applications/emsConfigurer/maindialog.cpp

255 lines
8.0 KiB
C++

#include "maindialog.h"
#include "ui_maindialog.h"
#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>
#include <hv/hlog.h>
#include "kutilities.h"
#include "devicepropertypage.h"
#include "formserialportsettingdialog.h"
MainDialog::MainDialog(QWidget *parent) :
QMainWindow (parent),
ui(new Ui::MainDialog)
{
//ui->setupUi(this);
this->setWindowIcon(QIcon(":/images/icon.png"));
InitializeUI();
// Load the window state
loadWindowState();
//设置返回消息回调
onMessageCallbackFunc f = std::bind(&MainDialog::onMessage, this, std::placeholders::_1, std::placeholders::_2);
AppTcpClient::getInstance()->resetOnMessageCallbackFunction(f);
}
MainDialog::~MainDialog()
{
delete ui;
}
void MainDialog::InitializeUI()
{
// Create central widget and layout
this->takeCentralWidget();
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);
// 将工具栏添加到主窗口的顶部
//addToolBar(Qt::TopToolBarArea, m_pMainToolBar);
// Set up layout
mainLayout->addWidget(m_pMainToolBar);
mainLayout->addWidget(splitter);
centralWidget->setLayout(mainLayout);
// Set central widget
setCentralWidget(centralWidget);
// Set initial size of the dialog
setWindowTitle(tr("EMS Configurer "));
//resize(800, 600);
setMinimumSize(1024, 768);
connect(m_pDeviceListWidget, &QListWidget::currentItemChanged, this, &MainDialog::changePage);
}
void MainDialog::CreateToolbar()
{
m_pMainToolBar = new QToolBar(this);
m_pMainToolBar->setIconSize(QSize(48, 48));
// 创建一个水平布局以容纳图标和占位符
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);
// Create actions for the toolbar
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"));
// Add actions to the toolbar
m_pMainToolBar->addAction(action1);
m_pMainToolBar->addAction(action2);
m_pMainToolBar->addAction(action4);
m_pMainToolBar->addSeparator();
m_pMainToolBar->addAction(action3);
// 将工具栏的 widget 设置为包含图标和占位符的 widget
m_pMainToolBar->addWidget(toolBarWidget);
// Connect actions to slots
connect(action1, &QAction::triggered, this, &MainDialog::onToolButton1Clicked);
connect(action2, &QAction::triggered, this, &MainDialog::onToolButton2Clicked);
connect(action4, &QAction::triggered, this, &MainDialog::onToolRefreshClicked);
connect(action3, &QAction::triggered, this, &MainDialog::onAboutButtonClicked);
}
void MainDialog::CreateTablePage()
{
m_pDevicestackedWidget = new QStackedWidget(this);
QVBoxLayout *stackedWidgetLayout = new QVBoxLayout(m_pDevicestackedWidget);
#if 1
//m_pDevicestackedWidget->setLayout(stackedWidgetLayout);
stackedWidgetLayout->addWidget(m_pDevicestackedWidget);
DevicePropertyPage *page1 = new DevicePropertyPage(m_pDevicestackedWidget);
DevicePropertyPage *page2 = new DevicePropertyPage(m_pDevicestackedWidget);
m_pDevicestackedWidget->addWidget(page1); // Add QTableView as a page
m_pDevicestackedWidget->addWidget(page2);
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"));
#endif
// setLayout(stackedWidgetLayout);
}
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());
}
//增加设备
void MainDialog::onToolButton1Clicked()
{
FormSerialPortSettingDialog dlg;
dlg.setModal(true);
if(dlg.exec() == QDialog::Accepted)
QMessageBox::information(this, "OK Clicked", "Button 1 was clicked!");
else
QMessageBox::information(this, "Cancel Clicked", "Cancel was clicked!");
}
//删除设备
void MainDialog::onToolButton2Clicked()
{
QMessageBox::information(this, "Button Clicked", "Button 2 was clicked!");
}
//关于
void MainDialog::onAboutButtonClicked()
{
QMessageBox::information(this, "Button Clicked", "About");
}
//刷新
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());
}