emsApplication/applications/emsConfigurer/maindialog.cpp

204 lines
6.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 "devicepropertypage.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();
}
MainDialog::~MainDialog()
{
delete ui;
}
void MainDialog::InitializeUI()
{
// Create central widget and layout
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);
// 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));
// Create actions for the toolbar
QAction *action1 = new QAction(QIcon(":/images/icon.png"), "Button 1", this);
QAction *action2 = new QAction(QIcon(":/images/icon.png"), "Button 2", this);
QAction *action3 = new QAction(QIcon(":/images/icon.png"), "About...", this);
// Add actions to the toolbar
m_pMainToolBar->addAction(action1);
m_pMainToolBar->addAction(action2);
m_pMainToolBar->addAction(action3);
// Connect actions to slots
connect(action1, &QAction::triggered, this, &MainDialog::onToolButton1Clicked);
connect(action2, &QAction::triggered, this, &MainDialog::onToolButton1Clicked);
connect(action3, &QAction::triggered, this, &MainDialog::onAboutButtonClicked);
}
void MainDialog::CreateTablePage()
{
m_pDevicestackedWidget = new QStackedWidget(this);
QVBoxLayout *stackedWidgetLayout = new QVBoxLayout();
m_pDevicestackedWidget->setLayout(stackedWidgetLayout);
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"));
page1->InitializeTable();
page2->InitializeTable();
}
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()
{
QMessageBox::information(this, "Button Clicked", "Button 1 was clicked!");
}
void MainDialog::onToolButton2Clicked()
{
QMessageBox::information(this, "Button Clicked", "Button 2 was clicked!");
}
void MainDialog::onAboutButtonClicked()
{
QMessageBox::information(this, "Button Clicked", "About");
}