Qt: Keep Export Dissections save button disabled when changing files

We check the packet range and packet format group box
for validity and disable the Save button if they're invalid.
QFileDialog re-enables the save button whenever the text
is changed or a file is selected in the window.

Watch for the save button changing enabling state and
re-disable it if necessary.

This prevents saving an empty file with invalid selections.
This commit is contained in:
John Thacker 2024-07-11 11:36:02 -04:00
parent 3a63db2abb
commit 910ab43578
2 changed files with 41 additions and 7 deletions

View File

@ -100,6 +100,7 @@ ExportDissectionDialog::ExportDissectionDialog(QWidget *parent, capture_file *ca
this, &ExportDissectionDialog::checkValidity);
connect(&packet_format_group_box_, &PacketFormatGroupBox::formatChanged,
this, &ExportDissectionDialog::checkValidity);
save_bt_->installEventFilter(this);
}
connect(this, &ExportDissectionDialog::filterSelected, this, &ExportDissectionDialog::exportTypeChanged);
@ -219,27 +220,55 @@ void ExportDissectionDialog::exportTypeChanged(QString name_filter)
setDefaultSuffix(export_extensions[export_type_]);
}
void ExportDissectionDialog::checkValidity()
bool ExportDissectionDialog::isValid()
{
bool enable = true;
bool valid = true;
if (!save_bt_) return;
if (!packet_range_group_box_.isValid()) enable = false;
if (!packet_range_group_box_.isValid()) valid = false;
if (export_type_ == export_type_text) {
if (! packet_format_group_box_.summaryEnabled() &&
! packet_format_group_box_.detailsEnabled() &&
! packet_format_group_box_.bytesEnabled())
{
enable = false;
valid = false;
}
}
save_bt_->setEnabled(enable);
return valid;
}
void ExportDissectionDialog::checkValidity()
{
if (!save_bt_) return;
save_bt_->setEnabled(isValid());
}
void ExportDissectionDialog::on_buttonBox_helpRequested()
{
mainApp->helpTopicAction(HELP_EXPORT_FILE_DIALOG);
}
bool ExportDissectionDialog::eventFilter(QObject *obj, QEvent *event)
{
// The QFileDialogPrivate will enable the Ok (Open/Save) button when
// anything is typed or selected. We can't catch that beforehand, so
// watch for the enable status change and re-disable it if the
// group boxes are invalid.
// We could do extra work (here and elsewhere) not to disable the button
// if what's selected in the dialog is a directory, but even with save_bt_
// disabled clicking on the directory still opens it.
if (event->type() == QEvent::EnabledChange) {
QPushButton *button = qobject_cast<QPushButton *>(obj);
if (button && button == save_bt_) {
// The button is already changed by the time we get this event.
if (button->isEnabled() && !isValid()) {
button->setEnabled(false);
return true;
}
}
}
return QObject::eventFilter(obj, event);
}

View File

@ -34,6 +34,9 @@ public:
public slots:
void show();
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
private slots:
void dialogAccepted(const QStringList &selected);
void exportTypeChanged(QString name_filter);
@ -51,6 +54,8 @@ private:
PacketFormatGroupBox packet_format_group_box_;
QPushButton *save_bt_;
bool isValid();
};
#endif // EXPORT_DISSECTION_DIALOG_H