Files
GostGenerator/view/pagebreakconfirmationdialog.cpp
T

267 lines
11 KiB
C++

#include "pagebreakconfirmationdialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QTextEdit>
#include <QScrollArea>
#include <QGroupBox>
#include <QApplication>
#include <QScreen>
#include <QStyle>
PageBreakConfirmationDialog::PageBreakConfirmationDialog(const QList<PageBreakInfo> &pageBreakInfos, QWidget *parent)
: QDialog(parent)
, m_pageBreakInfos(pageBreakInfos)
, m_showDialogInFuture(true)
{
setWindowTitle("Подтверждение переноса заголовков");
setModal(true);
setMinimumSize(700, 500);
// Центрируем диалог на экране
if (parent) {
move(parent->window()->frameGeometry().center() - frameGeometry().center());
}
setupUI();
updateDetailsText();
}
void PageBreakConfirmationDialog::setupUI()
{
m_mainLayout = new QVBoxLayout(this);
// Заголовок
m_titleLabel = new QLabel("Обнаружены заголовки, требующие переноса на следующую страницу");
m_titleLabel->setStyleSheet("font-size: 14px; font-weight: bold; margin: 10px;");
m_mainLayout->addWidget(m_titleLabel);
// Описание
m_descriptionLabel = new QLabel(QString("Найдено %1 заголовков, которые могут быть разорваны между страницами. "
"Выберите, какие заголовки нужно защитить от разрыва.")
.arg(m_pageBreakInfos.size()));
m_descriptionLabel->setWordWrap(true);
m_descriptionLabel->setStyleSheet("margin: 10px;");
m_mainLayout->addWidget(m_descriptionLabel);
// Чекбокс "Выбрать все"
m_selectAllCheckBox = new QCheckBox("Выбрать все");
m_selectAllCheckBox->setChecked(true);
m_selectAllCheckBox->setTristate(false);
m_selectAllCheckBox->setStyleSheet("font-weight: bold; margin: 5px;");
m_mainLayout->addWidget(m_selectAllCheckBox);
// Область с чекбоксами для каждой строки
m_checkBoxesScrollArea = new QScrollArea();
m_checkBoxesScrollArea->setWidgetResizable(true);
m_checkBoxesScrollArea->setMaximumHeight(200);
m_checkBoxesScrollArea->setFrameStyle(QFrame::Box);
QWidget *checkBoxesWidget = new QWidget();
QVBoxLayout *checkBoxesLayout = new QVBoxLayout(checkBoxesWidget);
// Создаем чекбоксы для каждой строки
for (const PageBreakInfo &info : m_pageBreakInfos) {
QString checkBoxText = QString("Строка %1: \"%2\" (стр. %3 → %4, +%5 пустых)")
.arg(info.rowIndex + 1)
.arg(info.headerText)
.arg(info.currentPage)
.arg(info.nextPage)
.arg(info.emptyRowsToAdd);
QCheckBox *checkBox = new QCheckBox(checkBoxText);
checkBox->setChecked(true); // По умолчанию выбрано
checkBox->setStyleSheet("margin: 2px; padding: 2px;");
m_rowCheckBoxes.append(checkBox);
checkBoxesLayout->addWidget(checkBox);
// Подключаем сигнал для обновления состояния "Выбрать все"
connect(checkBox, &QCheckBox::toggled, this, &PageBreakConfirmationDialog::updateSelectAllCheckBox);
}
m_checkBoxesScrollArea->setWidget(checkBoxesWidget);
m_mainLayout->addWidget(m_checkBoxesScrollArea);
// Чекбокс для показа деталей
m_showDetailsCheckBox = new QCheckBox("Показать детали");
m_showDetailsCheckBox->setChecked(false);
m_mainLayout->addWidget(m_showDetailsCheckBox);
// Текстовое поле с деталями (скрыто по умолчанию)
m_detailsTextEdit = new QTextEdit();
m_detailsTextEdit->setReadOnly(true);
m_detailsTextEdit->setMaximumHeight(150);
m_detailsTextEdit->setVisible(false);
m_mainLayout->addWidget(m_detailsTextEdit);
// Чекбокс для запоминания выбора
m_rememberChoiceCheckBox = new QCheckBox("Запомнить мой выбор и не спрашивать в будущем");
m_mainLayout->addWidget(m_rememberChoiceCheckBox);
// Кнопки
QHBoxLayout *buttonLayout = new QHBoxLayout();
m_confirmAllButton = new QPushButton("Подтвердить все");
m_confirmAllButton->setStyleSheet("QPushButton { background-color: #4CAF50; color: white; padding: 8px 16px; border: none; border-radius: 4px; }"
"QPushButton:hover { background-color: #45a049; }");
m_confirmSelectedButton = new QPushButton("Подтвердить выбранные");
m_confirmSelectedButton->setStyleSheet("QPushButton { background-color: #2196F3; color: white; padding: 8px 16px; border: none; border-radius: 4px; }"
"QPushButton:hover { background-color: #1976D2; }");
m_skipAllButton = new QPushButton("Пропустить все");
m_skipAllButton->setStyleSheet("QPushButton { background-color: #FF9800; color: white; padding: 8px 16px; border: none; border-radius: 4px; }"
"QPushButton:hover { background-color: #F57C00; }");
m_cancelButton = new QPushButton("Отмена");
m_cancelButton->setStyleSheet("QPushButton { background-color: #f44336; color: white; padding: 8px 16px; border: none; border-radius: 4px; }"
"QPushButton:hover { background-color: #da190b; }");
buttonLayout->addWidget(m_confirmAllButton);
buttonLayout->addWidget(m_confirmSelectedButton);
buttonLayout->addWidget(m_skipAllButton);
buttonLayout->addStretch();
buttonLayout->addWidget(m_cancelButton);
m_mainLayout->addLayout(buttonLayout);
// Подключаем сигналы
connect(m_confirmAllButton, &QPushButton::clicked, this, &PageBreakConfirmationDialog::onConfirmAllClicked);
connect(m_confirmSelectedButton, &QPushButton::clicked, this, &PageBreakConfirmationDialog::onConfirmSelectedClicked);
connect(m_skipAllButton, &QPushButton::clicked, this, &PageBreakConfirmationDialog::onSkipAllClicked);
connect(m_cancelButton, &QPushButton::clicked, this, &QDialog::reject);
connect(m_showDetailsCheckBox, &QCheckBox::toggled, this, &PageBreakConfirmationDialog::onDetailsToggled);
connect(m_selectAllCheckBox, &QCheckBox::toggled, this, &PageBreakConfirmationDialog::onSelectAllToggled);
// По умолчанию подтверждаем все строки
for (const PageBreakInfo &info : m_pageBreakInfos) {
m_confirmedRows.append(info.rowIndex);
}
}
void PageBreakConfirmationDialog::updateDetailsText()
{
QString detailsText;
detailsText += "<h3>Детали переноса заголовков:</h3>\n";
for (const PageBreakInfo &info : m_pageBreakInfos) {
detailsText += QString("<p><b>Строка %1:</b> \"%2\"</p>\n")
.arg(info.rowIndex + 1)
.arg(info.headerText);
detailsText += QString("<p> • Текущая страница: %1</p>\n").arg(info.currentPage);
detailsText += QString("<p> • Следующая страница: %1</p>\n").arg(info.nextPage);
detailsText += QString("<p> • Добавится пустых строк: %1</p>\n").arg(info.emptyRowsToAdd);
detailsText += "<hr>\n";
}
m_detailsTextEdit->setHtml(detailsText);
}
void PageBreakConfirmationDialog::onConfirmAllClicked()
{
// Выбираем все строки
m_selectAllCheckBox->setChecked(true);
for (QCheckBox *checkBox : m_rowCheckBoxes) {
checkBox->setChecked(true);
}
// Подтверждаем все
m_confirmedRows.clear();
for (const PageBreakInfo &info : m_pageBreakInfos) {
m_confirmedRows.append(info.rowIndex);
}
m_showDialogInFuture = !m_rememberChoiceCheckBox->isChecked();
accept();
}
void PageBreakConfirmationDialog::onConfirmSelectedClicked()
{
// Подтверждаем только выбранные строки
m_confirmedRows.clear();
for (int i = 0; i < m_rowCheckBoxes.size(); ++i) {
if (m_rowCheckBoxes[i]->isChecked()) {
m_confirmedRows.append(m_pageBreakInfos[i].rowIndex);
}
}
m_showDialogInFuture = !m_rememberChoiceCheckBox->isChecked();
accept();
}
void PageBreakConfirmationDialog::onSkipAllClicked()
{
// Снимаем выбор со всех строк
m_selectAllCheckBox->setChecked(false);
for (QCheckBox *checkBox : m_rowCheckBoxes) {
checkBox->setChecked(false);
}
m_confirmedRows.clear();
m_showDialogInFuture = !m_rememberChoiceCheckBox->isChecked();
accept();
}
void PageBreakConfirmationDialog::onDetailsToggled(bool checked)
{
m_detailsTextEdit->setVisible(checked);
if (checked) {
resize(width(), height() + 150);
} else {
resize(width(), height() - 150);
}
}
void PageBreakConfirmationDialog::onSelectAllToggled(bool checked)
{
// Блокируем сигналы, чтобы избежать рекурсии
for (QCheckBox *checkBox : m_rowCheckBoxes) {
checkBox->blockSignals(true);
checkBox->setChecked(checked);
checkBox->blockSignals(false);
}
}
void PageBreakConfirmationDialog::updateSelectAllCheckBox()
{
// Проверяем, все ли чекбоксы выбраны
bool allChecked = true;
bool anyChecked = false;
for (QCheckBox *checkBox : m_rowCheckBoxes) {
if (checkBox->isChecked()) {
anyChecked = true;
} else {
allChecked = false;
}
}
// Блокируем сигналы, чтобы избежать рекурсии
m_selectAllCheckBox->blockSignals(true);
if (allChecked) {
m_selectAllCheckBox->setChecked(true);
m_selectAllCheckBox->setTristate(false);
} else if (anyChecked) {
m_selectAllCheckBox->setTristate(true);
m_selectAllCheckBox->setCheckState(Qt::PartiallyChecked);
} else {
m_selectAllCheckBox->setChecked(false);
m_selectAllCheckBox->setTristate(false);
}
m_selectAllCheckBox->blockSignals(false);
}
QList<int> PageBreakConfirmationDialog::getConfirmedRows() const
{
return m_confirmedRows;
}
bool PageBreakConfirmationDialog::shouldShowDialogInFuture() const
{
return m_showDialogInFuture;
}