screenshot_dialog.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <QDateTime>
31 #include <QMessageBox>
32 #include <QImageWriter>
33 #include <QVBoxLayout>
34 #include <QHBoxLayout>
35 #include <QDialogButtonBox>
36 #include <QLabel>
37 #include <QPushButton> // Included so we know that QPushButton inherits QAbstractButton
38 #include <QFileDialog>
39 #include <QCheckBox>
40 #include <QTimer>
41 
42 #include "scaled_image_widget.h"
43 #include "screenshot_dialog.h"
44 
45 namespace rviz
46 {
48  QWidget* render_window,
49  const QString& default_save_dir)
50  : QWidget(nullptr) // This should be a top-level window to act like a dialog.
51  , main_window_(main_window)
52  , render_window_(render_window)
53  , save_full_window_(false)
54  , delay_timer_(new QTimer(this))
55  , first_time_(true)
56  , default_save_dir_(default_save_dir)
57 {
59 
61 
62  QCheckBox* full_window_checkbox = new QCheckBox("Save entire rviz window");
63 
64  button_box_ =
65  new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Retry | QDialogButtonBox::Cancel);
66 
67  QVBoxLayout* main_layout = new QVBoxLayout;
68  main_layout->addWidget(image_widget_, 100);
69  main_layout->addWidget(new QLabel("Image will be saved at the original resolution."));
70  main_layout->addWidget(full_window_checkbox);
71  main_layout->addWidget(button_box_);
72 
73  setLayout(main_layout);
74 
75  connect(button_box_, SIGNAL(clicked(QAbstractButton*)), this, SLOT(onButtonClicked(QAbstractButton*)));
76  connect(full_window_checkbox, SIGNAL(toggled(bool)), this, SLOT(setSaveFullWindow(bool)));
77  connect(delay_timer_, SIGNAL(timeout()), this, SLOT(onTimeout()));
78 }
79 
80 void ScreenshotDialog::showEvent(QShowEvent* event)
81 {
82  if (first_time_)
83  {
84  QPoint center = main_window_->rect().center();
85  move(center.x() - width() / 2, center.y() - height() / 2);
86 
87  first_time_ = false;
88  }
89  QWidget::showEvent(event);
90 }
91 
92 void ScreenshotDialog::setSaveFullWindow(bool save_full_window)
93 {
94  save_full_window_ = save_full_window;
96 }
97 
99 {
100  main_window_->raise();
101  delay_timer_->start(100);
102 }
103 
105 {
106  delay_timer_->stop();
108  raise();
109  activateWindow();
110 }
111 
113 {
114  if (save_full_window_)
115  {
116  screenshot_ = QPixmap::grabWindow(main_window_->winId());
117  }
118  else
119  {
120  screenshot_ = QPixmap::grabWindow(render_window_->winId());
121  }
123 }
124 
125 void ScreenshotDialog::onButtonClicked(QAbstractButton* clicked)
126 {
127  if (clicked == button_box_->button(QDialogButtonBox::Save))
128  {
129  save();
130  }
131  else if (clicked == button_box_->button(QDialogButtonBox::Retry))
132  {
133  takeScreenshot();
134  }
135  else if (clicked == button_box_->button(QDialogButtonBox::Cancel))
136  {
137  close();
138  }
139 }
140 
142 {
143  QString default_save_file = default_save_dir_ + "/rviz_screenshot_" +
144  QDateTime::currentDateTime().toString("yyyy_MM_dd-hh_mm_ss") + ".png";
145  QString filename = QFileDialog::getSaveFileName(this, "Save image", default_save_file);
146  if (filename != "")
147  {
148  QString with_slashes = QDir::fromNativeSeparators(filename);
149  QString file_part = with_slashes.section('/', -1);
150  default_save_dir_ = QDir::toNativeSeparators(with_slashes.section('/', 0, -2));
152 
153  // If filename has no dot, like "image" or has a dot in the zeroth
154  // position, like ".image", add ".png" to give a default file
155  // format.
156  if (file_part.lastIndexOf(".") <= 0)
157  {
158  filename += ".png";
159  }
160  QImageWriter writer(filename);
161  if (writer.write(screenshot_.toImage()))
162  {
163  close();
164  }
165  else
166  {
167  QString error_message;
168  if (writer.error() == QImageWriter::UnsupportedFormatError)
169  {
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++)
175  {
176  formats_string += ", " + formats[i];
177  }
178 
179  error_message = "File type '" + suffix + "' is not supported.\n" +
180  "Supported image formats are: " + formats_string + "\n";
181  }
182  else
183  {
184  error_message = "Failed to write image to file " + filename;
185  }
186 
187  QMessageBox::critical(this, "Error", error_message);
188  }
189  }
190 }
191 
192 } // end namespace rviz
A widget for showing a scaled version of an image (QPixmap).
filename
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_
void setImage(QPixmap image)
void onButtonClicked(QAbstractButton *clicked)
QDialogButtonBox * button_box_
void showEvent(QShowEvent *event) override
void setSaveFullWindow(bool save_full_window)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:25