123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- #include "MainAndSecondaryCamerasWnd.h"
- #include "ui_MainAndSecondaryCamerasWnd.h"
- #include <QSettings>
- #include <QCloseEvent>
- #include "Group.h"
- #include <QVBoxLayout>
- #include <QTimer>
- #include "CameraImage/CameraImageHandler.h"
- MainAndSecondaryCamerasWnd::MainAndSecondaryCamerasWnd(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainAndSecondaryCamerasWnd)
- {
- ui->setupUi(this);
- QSettings settings("YourOrganization", "YourApplication");
- settings.beginGroup("LastSettings");
- lastRightPixmap = settings.value("LastRightPixmap").value<QPixmap>();
- lasttextList = settings.value("LasttextList").toStringList();
- lastLastRightPixmap = settings.value("LastLastRightPixmap").value<QPixmap>();
- lastLasttextList = settings.value("LastLasttextList").toStringList();
- settings.endGroup();
- initFrom();
- }
- MainAndSecondaryCamerasWnd::~MainAndSecondaryCamerasWnd()
- {
- delete ui;
- }
- void MainAndSecondaryCamerasWnd::initFrom()
- {
- QTimer *timer = new QTimer(this);
- connect(timer, &QTimer::timeout, this, &MainAndSecondaryCamerasWnd::checkSettings);
- timer->start(100);
- ui->Leftlabel_Percentage->setAlignment(Qt::AlignCenter);
- ui->Rightlabel_Percentage->setAlignment(Qt::AlignCenter);
- 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_7, ui->lineEdit_7);
- connectSliderAndLineEdit(ui->verticalSlider_8, ui->lineEdit_8);
- QWidget *viewport = ui->scrollArea->viewport();
- QWidget *container = new QWidget(viewport);
- QHBoxLayout *mainLayout = new QHBoxLayout(container);
- mainLayout->setSpacing(28);
- mainLayout->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) {
- mainLayout->addWidget(widget);
- widgets.append(widget);
- }
- delete manager;
- }
- // 设置控件的最小高度和最大宽度
- int minHeight = 162;
- int maxWidth = 244;
- int num = -28;
- for (Group* w : widgets) {
- w->setMinimumHeight(minHeight);
- w->setMaximumWidth(maxWidth);
- num = num + 272;
- }
- container->setMinimumSize(num, 162);
- container->setLayout(mainLayout);
- ui->scrollArea->setWidget(container);
- ui->scrollArea->resize(1062, 177);
- ui->LeftOperatewidget->setMouseTracking(true);
- ui->RightOperatewidget->setMouseTracking(true);
- }
- void MainAndSecondaryCamerasWnd::initSliders() {
- QList<QSlider*> sliders = {
- ui->verticalSlider, ui->verticalSlider_2, ui->verticalSlider_3,
- ui->verticalSlider_4, ui->verticalSlider_7, ui->verticalSlider_8
- };
- 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 MainAndSecondaryCamerasWnd::initLineEdits() {
- QList<QLineEdit*> lineEdits = {
- ui->lineEdit, ui->lineEdit_2, ui->lineEdit_3,
- ui->lineEdit_4, ui->lineEdit_7, ui->lineEdit_8
- };
- for (QLineEdit* lineEdit : lineEdits) {
- lineEdit->setAlignment(Qt::AlignCenter);
- }
- }
- void MainAndSecondaryCamerasWnd::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 MainAndSecondaryCamerasWnd::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 MainAndSecondaryCamerasWnd::loadSettings()
- {
- QSettings settings("YourCompany", "YourApplication_");
- int groupId = settings.value("GroupId", 0).toInt();
- int index = settings.value("Index", 0).toInt();
- loadGroupSettings(groupId, index);
- }
- void MainAndSecondaryCamerasWnd::loadGroupSettings(int Id, int Index) {
- // isUpdatingSettings = true;
- 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();
- QString lastRightImage = settings.value("LastRightImage").toString();
- QSize size_left = ui->LeftOperatewidget->size();
- QSize size_right = ui->RightOperatewidget->size();
- QPixmap newPixmap;
- if (Index == 1) {
- newPixmap = QPixmap(imagePath1);
- } else if (Index == 2) {
- newPixmap = QPixmap(imagePath2);
- }
- QPixmap scaledPixmap_right = newPixmap.scaled(size_right, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->RightOperatewidget->setPixmap(scaledPixmap_right);
- QPixmap scaledPixmap_left = lastRightPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->LeftOperatewidget->setPixmap(scaledPixmap_left);
- // 更新当前图片的成员变量
- Left_currentPixmap = scaledPixmap_left;
- Left_scaleFactor = 1.0;
- Right_currentPixmap = scaledPixmap_right;
- Right_scaleFactor = 1.0;
- ui->Leftlabel_Percentage->setText("100%");
- ui->Rightlabel_Percentage->setText("100%");
- ui->LeftDataSources->clear();
- ui->LeftDataSources->addItems(lasttextList);
- ui->RightDataSources->clear();
- ui->RightDataSources->addItems(textList);
- // 保存到 QSettings
- settings.beginGroup("LastSettings");
- settings.setValue("LastRightPixmap", lastRightPixmap);
- settings.setValue("LasttextList", lasttextList);
- settings.setValue("LastLastRightPixmap", lastLastRightPixmap);
- settings.setValue("LastLasttextList", lastLasttextList);
- settings.endGroup();
- if (isUpdatingSettings) {
- lastLastRightPixmap = lastRightPixmap;
- lastLasttextList = lasttextList;
- lastRightPixmap = scaledPixmap_right;
- lasttextList = textList;
- }
- }
- void MainAndSecondaryCamerasWnd::on_LeftZoomUpButton_clicked()
- {
- Left_scaleFactor *= 1.1;
- int newWidth = Left_currentPixmap.width() * Left_scaleFactor;
- int newHeight = Left_currentPixmap.height() * Left_scaleFactor;
- QPixmap scaledImage = Left_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->LeftOperatewidget->setPixmap(scaledImage);
- double percentage = (Left_scaleFactor * 100);
- QString percentageStr = QString::number((int)percentage);
- ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
- }
- void MainAndSecondaryCamerasWnd::on_RightZoomUpButton_clicked()
- {
- Right_scaleFactor *= 1.1;
- int newWidth = Right_currentPixmap.width() * Right_scaleFactor;
- int newHeight = Right_currentPixmap.height() * Right_scaleFactor;
- QPixmap scaledImage = Right_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->RightOperatewidget->setPixmap(scaledImage);
- double percentage = (Right_scaleFactor * 100);
- QString percentageStr = QString::number((int)percentage);
- ui->Rightlabel_Percentage->setText(QString("%1%").arg(percentageStr));
- }
- void MainAndSecondaryCamerasWnd::on_LeftZoomOutButton_clicked()
- {
- double Left_newScaleFactor = Left_scaleFactor * 0.9;
- if (Left_newScaleFactor >= 1.0) {
- Left_scaleFactor = Left_newScaleFactor;
- } else {
- Left_scaleFactor = 1.0;
- }
- int newWidth = Left_currentPixmap.width() * Left_scaleFactor;
- int newHeight = Left_currentPixmap.height() * Left_scaleFactor;
- QPixmap scaledImage = Left_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->LeftOperatewidget->setPixmap(scaledImage);
- double percentage = (Left_scaleFactor * 100);
- QString percentageStr = QString::number((int)percentage);
- ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
- }
- void MainAndSecondaryCamerasWnd::on_RightZoomOutButton_clicked()
- {
- double Right_newScaleFactor = Right_scaleFactor * 0.9;
- if (Right_newScaleFactor >= 1.0) {
- Right_scaleFactor = Right_newScaleFactor;
- } else {
- Right_scaleFactor = 1.0;
- }
- int newWidth = Right_currentPixmap.width() * Right_scaleFactor;
- int newHeight = Right_currentPixmap.height() * Right_scaleFactor;
- QPixmap scaledImage = Right_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->RightOperatewidget->setPixmap(scaledImage);
- double percentage = (Right_scaleFactor * 100);
- QString percentageStr = QString::number((int)percentage);
- ui->Rightlabel_Percentage->setText(QString("%1%").arg(percentageStr));
- }
- void MainAndSecondaryCamerasWnd::wheelEvent(QWheelEvent *event)
- {
- if (ui->LeftOperatewidget->rect().contains(ui->LeftOperatewidget->mapFromGlobal(event->globalPosition().toPoint()))) {
- if (event->angleDelta().y() > 0) {
- Left_scaleFactor *= 1.1;
- } else {
- double Left_newScaleFactor = Left_scaleFactor * 0.9;
- if (Left_newScaleFactor >= 1.0) {
- Left_scaleFactor = Left_newScaleFactor;
- } else {
- Left_scaleFactor = 1.0;
- }
- }
- int newWidth_left = Left_currentPixmap.width() * Left_scaleFactor;
- int newHeight_left = Left_currentPixmap.height() * Left_scaleFactor;
- QPixmap scaledImage_left = Left_currentPixmap.scaled(newWidth_left, newHeight_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->LeftOperatewidget->setPixmap(scaledImage_left);
- double percentage_left = Left_scaleFactor * 100;
- QString percentageStr_left = QString::number((int)percentage_left);
- ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr_left));
- }
- if (ui->RightOperatewidget->rect().contains(ui->RightOperatewidget->mapFromGlobal(event->globalPosition().toPoint()))) {
- if (event->angleDelta().y() > 0) {
- Right_scaleFactor *= 1.1;
- } else {
- double Right_newScaleFactor = Right_scaleFactor * 0.9;
- if (Right_newScaleFactor >= 1.0) {
- Right_scaleFactor = Right_newScaleFactor;
- } else {
- Right_scaleFactor = 1.0;
- }
- }
- int newWidth = Right_currentPixmap.width() * Right_scaleFactor;
- int newHeight = Right_currentPixmap.height() * Right_scaleFactor;
- QPixmap scaledImage = Right_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- ui->RightOperatewidget->setPixmap(scaledImage);
- double percentage = Right_scaleFactor * 100;
- QString percentageStr = QString::number((int)percentage);
- ui->Rightlabel_Percentage->setText(QString("%1%").arg(percentageStr));
- }
- QMainWindow::wheelEvent(event);
- }
- void MainAndSecondaryCamerasWnd::showEvent(QShowEvent *event) {
- isUpdatingSettings = false;
- QMainWindow::showEvent(event);
- loadSettings();
- isUpdatingSettings = true;
- }
- void MainAndSecondaryCamerasWnd::hideEvent(QHideEvent *event) {
- isUpdatingSettings = false;
- QMainWindow::hideEvent(event);
- }
|