#include "Group.h"
#include "ui_Group.h"
#include <QDebug>
#include <QMouseEvent>
#include <QSettings>

Group::Group(int Id, const QString& imagePath1, const QString& imagePath2, const QStringList& textList, QWidget* parent)
    : QWidget(parent), ui(new Ui::Group) {
    ui->setupUi(this);

    QPixmap pixmap1(imagePath1);
    QPixmap pixmap2(imagePath2);
    ui->Imagewidget_left->setPixmap(pixmap1);
    ui->Imagewidget_right->setPixmap(pixmap2);

    ui->DatacomboBox->addItems(textList);

    ui->Imagewidget_left->setProperty("groupId", Id);
    ui->Imagewidget_right->setProperty("groupId", Id);

    // 存储参数到 QSettings
    saveGroupSettings(Id, imagePath1, imagePath2, textList);

    initForm();
}

Group::~Group() {
    delete ui;
}

void Group::initForm() {
    ui->Imagewidget_left->installEventFilter(this);
    ui->Imagewidget_right->installEventFilter(this);
}

bool Group::eventFilter(QObject *obj, QEvent *event) {
    if (event->type() == QEvent::MouseButtonDblClick) {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if (mouseEvent->button() == Qt::LeftButton) {
            int groupId = obj->property("groupId").toInt();
            QSettings settings("YourCompany", "YourApplication_");
            settings.setValue("GroupId", groupId);

            if (obj == this->ui->Imagewidget_left) {
                settings.setValue("Index", 1);
            } else if (obj == this->ui->Imagewidget_right) {
                settings.setValue("Index", 2);
            }
            return true;
        }
    }
    return QWidget::eventFilter(obj, event);
}

void Group::saveGroupSettings(int Id, const QString& imagePath1, const QString& imagePath2, const QStringList& textList) {
    QSettings settings("YourOrganization", "YourApplication");
    settings.beginGroup(QString::number(Id));
    settings.setValue("ImagePath1", imagePath1);
    settings.setValue("ImagePath2", imagePath2);
    settings.setValue("TextList", textList);
    settings.endGroup();
}
