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 #include <QScreen>
42 #include <QWindow>
43 
44 #include "scaled_image_widget.h"
45 #include "screenshot_dialog.h"
46 
47 namespace rviz
48 {
50  QWidget* render_window,
51  const QString& default_save_dir)
52  : QWidget(nullptr) // This should be a top-level window to act like a dialog.
53  , main_window_(main_window)
54  , render_window_(render_window)
55  , save_full_window_(false)
56  , delay_timer_(new QTimer(this))
57  , first_time_(true)
58  , default_save_dir_(default_save_dir)
59 {
61 
63 
64  QCheckBox* full_window_checkbox = new QCheckBox("Save entire rviz window");
65 
66  button_box_ =
67  new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Retry | QDialogButtonBox::Cancel);
68 
69  QVBoxLayout* main_layout = new QVBoxLayout;
70  main_layout->addWidget(image_widget_, 100);
71  main_layout->addWidget(new QLabel("Image will be saved at the original resolution."));
72  main_layout->addWidget(full_window_checkbox);
73  main_layout->addWidget(button_box_);
74 
75  setLayout(main_layout);
76 
77  connect(button_box_, &QDialogButtonBox::clicked, this, &ScreenshotDialog::onButtonClicked);
78  connect(full_window_checkbox, &QCheckBox::toggled, this, &ScreenshotDialog::setSaveFullWindow);
79  connect(delay_timer_, &QTimer::timeout, this, &ScreenshotDialog::onTimeout);
80 }
81 
82 void ScreenshotDialog::showEvent(QShowEvent* event)
83 {
84  if (first_time_)
85  {
86  QPoint center = main_window_->rect().center();
87  move(center.x() - width() / 2, center.y() - height() / 2);
88 
89  first_time_ = false;
90  }
91  QWidget::showEvent(event);
92 }
93 
94 void ScreenshotDialog::setSaveFullWindow(bool save_full_window)
95 {
96  save_full_window_ = save_full_window;
98 }
99 
101 {
102  main_window_->raise();
103  delay_timer_->start(100);
104 }
105 
107 {
108  delay_timer_->stop();
110  raise();
111  activateWindow();
112 }
113 
115 {
116  const QWidget* w = save_full_window_ ? main_window_ : render_window_;
117  screenshot_ = w->windowHandle()->screen()->grabWindow(w->winId());
119 }
120 
121 void ScreenshotDialog::onButtonClicked(QAbstractButton* clicked)
122 {
123  if (clicked == button_box_->button(QDialogButtonBox::Save))
124  {
125  save();
126  }
127  else if (clicked == button_box_->button(QDialogButtonBox::Retry))
128  {
129  takeScreenshot();
130  }
131  else if (clicked == button_box_->button(QDialogButtonBox::Cancel))
132  {
133  close();
134  }
135 }
136 
138 {
139  QString default_save_file = default_save_dir_ + "/rviz_screenshot_" +
140  QDateTime::currentDateTime().toString("yyyy_MM_dd-hh_mm_ss") + ".png";
141  QString filename = QFileDialog::getSaveFileName(this, "Save image", default_save_file);
142  if (filename != "")
143  {
144  QString with_slashes = QDir::fromNativeSeparators(filename);
145  QString file_part = with_slashes.section('/', -1);
146  default_save_dir_ = QDir::toNativeSeparators(with_slashes.section('/', 0, -2));
148 
149  // If filename has no dot, like "image" or has a dot in the zeroth
150  // position, like ".image", add ".png" to give a default file
151  // format.
152  if (file_part.lastIndexOf(".") <= 0)
153  {
154  filename += ".png";
155  }
156  QImageWriter writer(filename);
157  if (writer.write(screenshot_.toImage()))
158  {
159  close();
160  }
161  else
162  {
163  QString error_message;
164  if (writer.error() == QImageWriter::UnsupportedFormatError)
165  {
166  QString suffix = filename.section('.', -1);
167  QString formats_string;
168  QList<QByteArray> formats = QImageWriter::supportedImageFormats();
169  formats_string = formats[0];
170  for (int i = 1; i < formats.size(); i++)
171  {
172  formats_string += ", " + formats[i];
173  }
174 
175  error_message = "File type '" + suffix + "' is not supported.\n" +
176  "Supported image formats are: " + formats_string + "\n";
177  }
178  else
179  {
180  error_message = "Failed to write image to file " + filename;
181  }
182 
183  QMessageBox::critical(this, "Error", error_message);
184  }
185  }
186 }
187 
188 } // end namespace rviz
rviz::ScreenshotDialog::showEvent
void showEvent(QShowEvent *event) override
Definition: screenshot_dialog.cpp:82
rviz::ScreenshotDialog::save
void save()
Definition: screenshot_dialog.cpp:137
rviz::ScaledImageWidget::setImage
void setImage(QPixmap image)
Definition: scaled_image_widget.cpp:43
rviz::ScreenshotDialog::save_full_window_
bool save_full_window_
Definition: screenshot_dialog.h:84
rviz::ScreenshotDialog::main_window_
QWidget * main_window_
Definition: screenshot_dialog.h:80
screenshot_dialog.h
rviz::ScreenshotDialog::savedInDirectory
void savedInDirectory(const QString &directory)
Emitted when the user saves a file.
rviz::ScreenshotDialog::onTimeout
void onTimeout()
Definition: screenshot_dialog.cpp:106
rviz::ScreenshotDialog::setSaveFullWindow
void setSaveFullWindow(bool save_full_window)
Definition: screenshot_dialog.cpp:94
rviz::ScreenshotDialog::button_box_
QDialogButtonBox * button_box_
Definition: screenshot_dialog.h:83
rviz::ScreenshotDialog::takeScreenshot
void takeScreenshot()
Definition: screenshot_dialog.cpp:100
scaled_image_widget.h
rviz
Definition: add_display_dialog.cpp:54
rviz::ScreenshotDialog::onButtonClicked
void onButtonClicked(QAbstractButton *clicked)
Definition: screenshot_dialog.cpp:121
rviz::ScreenshotDialog::render_window_
QWidget * render_window_
Definition: screenshot_dialog.h:81
rviz::ScreenshotDialog::screenshot_
QPixmap screenshot_
Definition: screenshot_dialog.h:82
rviz::ScreenshotDialog::default_save_dir_
QString default_save_dir_
Definition: screenshot_dialog.h:88
rviz::ScreenshotDialog::first_time_
bool first_time_
Definition: screenshot_dialog.h:87
rviz::ScreenshotDialog::delay_timer_
QTimer * delay_timer_
Definition: screenshot_dialog.h:85
rviz::ScreenshotDialog::image_widget_
ScaledImageWidget * image_widget_
Definition: screenshot_dialog.h:79
rviz::ScreenshotDialog::ScreenshotDialog
ScreenshotDialog(QWidget *main_window, QWidget *render_window, const QString &default_save_dir=QString())
Definition: screenshot_dialog.cpp:49
rviz::ScaledImageWidget
A widget for showing a scaled version of an image (QPixmap).
Definition: scaled_image_widget.h:46
rviz::ScreenshotDialog::takeScreenshotNow
void takeScreenshotNow()
Definition: screenshot_dialog.cpp:114


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:10