emsApplication/applications/emsConfigurer/maindialog.cpp

217 lines
6.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
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"));
InitializeUI();
connect(m_pDeviceListWidget, &QListWidget::currentItemChanged, this, &MainDialog::changePage);
// Load the window state
loadWindowState();
2024-09-10 16:55:41 +08:00
}
MainDialog::~MainDialog()
{
delete ui;
}
2024-09-11 17:09:26 +08:00
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);
}
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);
// Create and set up QTableView
QTableView *tableView = new QTableView(this);
// Create and set up a model for the table view
QStandardItemModel *model = new QStandardItemModel(4, 3, this); // 4 rows, 3 columns
model->setHorizontalHeaderLabels(QStringList() << "Column 1" << "Column 2" << "Column 3");
// Fill the model with data
for (int row = 0; row < 4; ++row)
{
for (int col = 0; col < 3; ++col)
{
QStandardItem *item = new QStandardItem(QString("Item %1-%2").arg(row).arg(col));
model->setItem(row, col, item);
}
}
tableView->setModel(model);
m_pDevicestackedWidget->addWidget(tableView); // Add QTableView as a page
m_pDevicestackedWidget->addWidget(new QPushButton("Page 2"));
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"));
}
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");
}