123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- #include "SingleCameraOperationWnd.h"
- #include "ui_SingleCameraOperationWnd.h"
- #include <QSettings>
- #include <QCloseEvent>
- #include "Group.h"
- #include <QVBoxLayout>
- #include <QDebug>
- #include <QTimer>
- #include "CameraImage/CameraImageHandler.h"
- SingleCameraOperationWnd::SingleCameraOperationWnd(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::SingleCameraOperationWnd), scaleFactor(1.0)
- {
- ui->setupUi(this);
- initFrom();
- }
- SingleCameraOperationWnd::~SingleCameraOperationWnd()
- {
- delete ui;
- }
- void SingleCameraOperationWnd::initFrom() {
- QTimer *timer = new QTimer(this);
- connect(timer, &QTimer::timeout, this, &SingleCameraOperationWnd::checkSettings);
- timer->start(100);
- ui->label_Percentage->setAlignment(Qt::AlignCenter);
- // 设置右下部分
- loadSliderStates();
- initSliders();
- initLineEdits();
- connectSliderAndLineEdit(ui->verticalSlider, ui->lineEdit);
- connectSliderAndLineEdit(ui->verticalSlider_2, ui->lineEdit_2);
- connectSliderAndLineEdit(ui->verticalSlider_3, ui->lineEdit_3);
- connectSliderAndLineEdit(ui->verticalSlider_4, ui->lineEdit_4);
- connectSliderAndLineEdit(ui->verticalSlider_5, ui->lineEdit_5);
- connectSliderAndLineEdit(ui->verticalSlider_6, ui->lineEdit_6);
- // 设置右上部分
- QWidget *viewport = ui->scrollArea->viewport();
- QWidget *container = new QWidget(viewport);
- QVBoxLayout *layout = new QVBoxLayout(container);
- layout->setSpacing(16); // 设置Group之间的间隔距离
- layout->setMargin(0);
- QList<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8};
- QList<Group*> widgets;
- for (int i = 0; i < numbers.size(); ++i) {
- int num = numbers[i];
- CameraImageHandler* manager = new CameraImageHandler(num);
- Group* widget = manager->getGroup();
- if (widget != nullptr) {
- layout->addWidget(widget);
- widgets.append(widget);
- }
- delete manager;
- }
- // 设置控件的最小高度和最大宽度
- int minHeight = 162;
- int maxWidth = 244;
- for (Group* w : widgets) {
- w->setMinimumHeight(minHeight);
- w->setMaximumWidth(maxWidth);
- }
- container->setLayout(layout);
- ui->scrollArea->setWidget(container);
- ui->scrollArea->resize(261, 700);
- ui->Operatewidget->setMouseTracking(true);
- }
- void SingleCameraOperationWnd::closeEvent(QCloseEvent *event) {
- // 保存滑块状态
- saveSliderStates();
- // 确保调用基类的 closeEvent 处理其他关闭逻辑
- QMainWindow::closeEvent(event);
- }
- void SingleCameraOperationWnd::initSliders() {
- QList<QSlider*> sliders = {
- ui->verticalSlider, ui->verticalSlider_2, ui->verticalSlider_3,
- ui->verticalSlider_4, ui->verticalSlider_5, ui->verticalSlider_6
- };
- QList<int> initialValues = {11, 22, 9, 11, 0, 0};
- for (int i = 0; i < sliders.size(); ++i) {
- sliders[i]->setMinimum(0);
- sliders[i]->setMaximum(100);
- sliders[i]->setValue(initialValues[i]);
- }
- }
- void SingleCameraOperationWnd::initLineEdits() {
- QList<QLineEdit*> lineEdits = {
- ui->lineEdit, ui->lineEdit_2, ui->lineEdit_3,
- ui->lineEdit_4, ui->lineEdit_5, ui->lineEdit_6
- };
- for (QLineEdit* lineEdit : lineEdits) {
- lineEdit->setAlignment(Qt::AlignCenter);
- }
- }
- void SingleCameraOperationWnd::saveSliderStates() {
- QSettings settings("YourCompany", "YourApp");
- settings.beginGroup("SliderStates");
- settings.setValue("verticalSlider", ui->verticalSlider->value());
- settings.setValue("verticalSlider_2", ui->verticalSlider_2->value());
- settings.setValue("verticalSlider_3", ui->verticalSlider_3->value());
- settings.setValue("verticalSlider_4", ui->verticalSlider_4->value());
- settings.setValue("verticalSlider_5", ui->verticalSlider_5->value());
- settings.setValue("verticalSlider_6", ui->verticalSlider_6->value());
- settings.endGroup();
- }
- void SingleCameraOperationWnd::loadSliderStates() {
- QSettings settings("YourCompany", "YourApp");
- settings.beginGroup("SliderStates");
- ui->verticalSlider->setValue(settings.value("verticalSlider", 11).toInt());
- ui->verticalSlider_2->setValue(settings.value("verticalSlider_2", 22).toInt());
- ui->verticalSlider_3->setValue(settings.value("verticalSlider_3", 9).toInt());
- ui->verticalSlider_4->setValue(settings.value("verticalSlider_4", 11).toInt());
- ui->verticalSlider_5->setValue(settings.value("verticalSlider_5", 0).toInt());
- ui->verticalSlider_6->setValue(settings.value("verticalSlider_6", 0).toInt());
- settings.endGroup();
- }
- // QSlider 和 QLineEdit 的联动
- void SingleCameraOperationWnd::connectSliderAndLineEdit(QSlider* slider, QLineEdit* lineEdit)
- {
- QIntValidator* validator = new QIntValidator(slider->minimum(), slider->maximum(), lineEdit);
- lineEdit->setValidator(validator);
- connect(slider, &QSlider::valueChanged, [lineEdit](int value) {
- lineEdit->setText(QString::number(value));
- });
- connect(lineEdit, &QLineEdit::textChanged, [slider](const QString &text) {
- bool ok;
- int value = text.toInt(&ok);
- if (ok && value >= slider->minimum() && value <= slider->maximum()) {
- slider->setValue(value);
- } else if (!text.isEmpty()) {
- int closestValue = qMin(qMax(text.toInt(&ok), slider->minimum()), slider->maximum());
- slider->setValue(closestValue);
- }
- });
- }
- void SingleCameraOperationWnd::checkSettings() {
- QSettings settings("YourCompany", "YourApplication_");
- int groupId = settings.value("GroupId", 0).toInt();
- int index = settings.value("Index", 0).toInt();
- if (groupId != lastGroupId || index != lastIndex) {
- lastGroupId = groupId;
- lastIndex = index;
- loadSettings();
- }
- }
- void SingleCameraOperationWnd::loadSettings()
- {
- QSettings settings("YourCompany", "YourApplication_");
- int groupId = settings.value("GroupId", 0).toInt();
- int index = settings.value("Index", 0).toInt();
- loadGroupSettings(groupId, index);
- }
- void SingleCameraOperationWnd::loadGroupSettings(int Id, int Index) {
- QSettings settings("YourOrganization", "YourApplication");
- settings.beginGroup(QString::number(Id));
- QString imagePath1 = settings.value("ImagePath1").toString();
- QString imagePath2 = settings.value("ImagePath2").toString();
- QStringList textList = settings.value("TextList").toStringList();
- settings.endGroup();
- QSize size = ui->Operatewidget->size();
- QPixmap newPixmap;
- if (Index == 1) {
- newPixmap = QPixmap(imagePath1);
- } else if (Index == 2) {
- newPixmap = QPixmap(imagePath2);
- } else {
- newPixmap = QPixmap(":/images/test_image/image.png");
- }
- QPixmap scaledPixmap = newPixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->Operatewidget->setPixmap(scaledPixmap);
- // 更新当前图片的成员变量
- currentPixmap = scaledPixmap;
- scaleFactor = 1.0;
- ui->label_Percentage->setText("100%");
- ui->DatacomboBox->clear();
- ui->DatacomboBox->addItems(textList);
- // qDebug() << "ComboBox items added:" << ui->DatacomboBox->count();
- // connect(ui->DatacomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(SingleCameraOperationWnd::onComboBoxIndexChanged(int)));
- }
- void SingleCameraOperationWnd::on_ZoomUpButton_clicked() {
- scaleFactor *= 1.1;
- int newWidth = currentPixmap.width() * scaleFactor;
- int newHeight = currentPixmap.height() * scaleFactor;
- QPixmap scaledImage = currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->Operatewidget->setPixmap(scaledImage); // 这里传递缩放后的图片
- double percentage = (scaleFactor * 100);
- QString percentageStr = QString::number((int)percentage);
- ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
- }
- void SingleCameraOperationWnd::on_ZoomOutButton_clicked() {
- double newScaleFactor = scaleFactor * 0.9;
- if (newScaleFactor >= 1.0) {
- scaleFactor = newScaleFactor;
- } else {
- scaleFactor = 1.0;
- }
- int newWidth = currentPixmap.width() * scaleFactor;
- int newHeight = currentPixmap.height() * scaleFactor;
- QPixmap scaledImage = currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->Operatewidget->setPixmap(scaledImage); // 这里传递缩放后的图片
- double percentage = scaleFactor * 100;
- QString percentageStr = QString::number((int)percentage);
- ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
- }
- void SingleCameraOperationWnd::wheelEvent(QWheelEvent *event) {
- // 检查鼠标事件是否发生在 Operatewidget 上
- if (ui->Operatewidget->rect().contains(ui->Operatewidget->mapFromGlobal(event->globalPos()))) {
- if (event->angleDelta().y() > 0) {
- scaleFactor *= 1.1;
- } else {
- double newScaleFactor = scaleFactor * 0.9;
- // 确保新的缩放因子不小于1.0(即图片的原始大小)
- if (newScaleFactor >= 1.0) {
- scaleFactor = newScaleFactor;
- } else {
- scaleFactor = 1.0; // 如果新的缩放因子小于1.0,则直接设置为1.0
- }
- }
- int newWidth = currentPixmap.width() * scaleFactor;
- int newHeight = currentPixmap.height() * scaleFactor;
- QPixmap scaledImage = currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->Operatewidget->setPixmap(scaledImage); // 这里传递缩放后的图片
- double percentage = scaleFactor * 100;
- QString percentageStr = QString::number((int)percentage);
- ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
- }
- QMainWindow::wheelEvent(event);
- }
- void SingleCameraOperationWnd::onComboBoxIndexChanged(int index) {
- // 根据index获取图片路径
- QString imagePath = getImagePathFromIndex(index);
- if (!imagePath.isEmpty()) {
- QPixmap newPixmap(imagePath);
- QPixmap scaledPixmap = newPixmap.scaled(ui->Operatewidget->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->Operatewidget->setPixmap(scaledPixmap);
- // 更新当前图片的成员变量
- currentPixmap = scaledPixmap;
- scaleFactor = 1.0;
- ui->label_Percentage->setText("100%");
- }
- }
- QString SingleCameraOperationWnd::getImagePathFromIndex(int index) {
- QStringList imagePaths = {
- ":/images/test_image/image.png",
- ":/images/test_image/image_2.png",
- };
- if (index >= 0 && index < imagePaths.size()) {
- return imagePaths[index];
- }
- return ":/images/test_image/image.png";
- }
- void SingleCameraOperationWnd::showEvent(QShowEvent *event) {
- QMainWindow::showEvent(event);
- loadSettings();
- }
- void SingleCameraOperationWnd::hideEvent(QHideEvent *event) {
- QMainWindow::hideEvent(event);
- }
|