31 #include <QMessageBox> 32 #include <QImageWriter> 33 #include <QVBoxLayout> 34 #include <QHBoxLayout> 35 #include <QDialogButtonBox> 37 #include <QPushButton> 38 #include <QFileDialog> 48 QWidget* render_window,
49 const QString& default_save_dir)
51 , main_window_(main_window)
52 , render_window_(render_window)
53 , save_full_window_(false)
54 , delay_timer_(new QTimer(this))
56 , default_save_dir_(default_save_dir)
62 QCheckBox* full_window_checkbox =
new QCheckBox(
"Save entire rviz window");
65 new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Retry | QDialogButtonBox::Cancel);
67 QVBoxLayout* main_layout =
new QVBoxLayout;
69 main_layout->addWidget(
new QLabel(
"Image will be saved at the original resolution."));
70 main_layout->addWidget(full_window_checkbox);
73 setLayout(main_layout);
76 connect(full_window_checkbox, SIGNAL(toggled(
bool)),
this, SLOT(
setSaveFullWindow(
bool)));
85 move(center.x() - width() / 2, center.y() - height() / 2);
89 QWidget::showEvent(event);
127 if (clicked ==
button_box_->button(QDialogButtonBox::Save))
131 else if (clicked ==
button_box_->button(QDialogButtonBox::Retry))
135 else if (clicked ==
button_box_->button(QDialogButtonBox::Cancel))
144 QDateTime::currentDateTime().toString(
"yyyy_MM_dd-hh_mm_ss") +
".png";
145 QString
filename = QFileDialog::getSaveFileName(
this,
"Save image", default_save_file);
148 QString with_slashes = QDir::fromNativeSeparators(filename);
149 QString file_part = with_slashes.section(
'/', -1);
156 if (file_part.lastIndexOf(
".") <= 0)
160 QImageWriter writer(filename);
167 QString error_message;
168 if (writer.error() == QImageWriter::UnsupportedFormatError)
170 QString suffix = filename.section(
'.', -1);
171 QString formats_string;
172 QList<QByteArray> formats = QImageWriter::supportedImageFormats();
173 formats_string = formats[0];
174 for (
int i = 1; i < formats.size(); i++)
176 formats_string +=
", " + formats[i];
179 error_message =
"File type '" + suffix +
"' is not supported.\n" +
180 "Supported image formats are: " + formats_string +
"\n";
184 error_message =
"Failed to write image to file " + filename;
187 QMessageBox::critical(
this,
"Error", error_message);
ScreenshotDialog(QWidget *main_window, QWidget *render_window, const QString &default_save_dir=QString())
void savedInDirectory(const QString &directory)
Emitted when the user saves a file.
ScaledImageWidget * image_widget_
QString default_save_dir_
void onButtonClicked(QAbstractButton *clicked)
QDialogButtonBox * button_box_
void showEvent(QShowEvent *event) override
void setSaveFullWindow(bool save_full_window)