Skip to content

Commit 3ab5e25

Browse files
committed
gui: default -key to most recent and add additional error checks for array out of bounds
Signed-off-by: Peter Gadfort <[email protected]>
1 parent b57cfc6 commit 3ab5e25

File tree

6 files changed

+31
-15
lines changed

6 files changed

+31
-15
lines changed

src/gui/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ save_animated_gif
193193
| `-resolution`| resolution in microns per pixel to use when saving the image, default will match what the GUI has selected.|
194194
| `-width`| width of the output image in pixels, default will be computed from the resolution. Cannot be used with ``-resolution``.|
195195
| `-delay`| delay between frames in the GIF.|
196-
| `-key`| used to distinguish multiple GIF files (returned by -add).|
196+
| `-key`| used to distinguish multiple GIF files (returned by -add). Defaults to the most recent GIF.|
197197

198198
### Select Objects
199199

src/gui/include/gui/gui.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,12 +827,12 @@ class Gui
827827
// Start returns the key for use by add and end. This allows multiple
828828
// gifs to be open at once.
829829
int gifStart(const std::string& filename);
830-
void gifAddFrame(int key,
830+
void gifAddFrame(std::optional<int> key,
831831
const odb::Rect& region = odb::Rect(),
832832
int width_px = 0,
833833
double dbu_per_pixel = 0,
834834
std::optional<int> delay = {});
835-
void gifEnd(int key);
835+
void gifEnd(std::optional<int> key);
836836

837837
void setHeatMapSetting(const std::string& name,
838838
const std::string& option,

src/gui/src/gui.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,13 +1461,17 @@ int Gui::gifStart(const std::string& filename)
14611461
return gifs_.size() - 1;
14621462
}
14631463

1464-
void Gui::gifAddFrame(const int key,
1464+
void Gui::gifAddFrame(std::optional<int> key,
14651465
const odb::Rect& region,
14661466
int width_px,
14671467
double dbu_per_pixel,
14681468
std::optional<int> delay)
14691469
{
1470-
if (key >= gifs_.size() || gifs_[key] == nullptr) {
1470+
if (!key.has_value()) {
1471+
key = gifs_.size() - 1;
1472+
}
1473+
if (gifs_.empty() || *key < 0 || *key >= gifs_.size()
1474+
|| gifs_[*key] == nullptr) {
14711475
logger_->warn(utl::GUI, 51, "GIF not active");
14721476
return;
14731477
}
@@ -1476,7 +1480,7 @@ void Gui::gifAddFrame(const int key,
14761480
logger_->error(utl::GUI, 50, "No design loaded.");
14771481
}
14781482

1479-
auto& gif = gifs_[key];
1483+
auto& gif = gifs_[*key];
14801484

14811485
odb::Rect save_region = region;
14821486
const bool use_die_area = region.dx() == 0 || region.dy() == 0;
@@ -1549,14 +1553,18 @@ void Gui::gifAddFrame(const int key,
15491553
delay.value_or(kDefaultGifDelay));
15501554
}
15511555

1552-
void Gui::gifEnd(const int key)
1556+
void Gui::gifEnd(std::optional<int> key)
15531557
{
1554-
if (key >= gifs_.size() || gifs_[key] == nullptr) {
1558+
if (!key.has_value()) {
1559+
key = gifs_.size() - 1;
1560+
}
1561+
if (gifs_.empty() || *key < 0 || *key >= gifs_.size()
1562+
|| gifs_[*key] == nullptr) {
15551563
logger_->warn(utl::GUI, 58, "GIF not active");
15561564
return;
15571565
}
15581566

1559-
auto& gif = gifs_[key];
1567+
auto& gif = gifs_[*key];
15601568
if (gif->writer == nullptr) {
15611569
logger_->warn(utl::GUI,
15621570
107,
@@ -1567,7 +1575,7 @@ void Gui::gifEnd(const int key)
15671575
}
15681576

15691577
GifEnd(gif->writer.get());
1570-
gifs_[key] = nullptr;
1578+
gifs_[*key] = nullptr;
15711579
}
15721580

15731581
class SafeApplication : public QApplication

src/gui/src/gui.i

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,16 +836,24 @@ void gif_add(int key, double xlo, double ylo, double xhi, double yhi, int width_
836836
if (delay > 0) {
837837
delay_pass = delay;
838838
}
839-
gui->gifAddFrame(key, make_rect(xlo, ylo, xhi, yhi), width_px, dbu_per_pixel, delay_pass);
839+
std::optional<int> key_pass;
840+
if (key >= 0) {
841+
key_pass = key;
842+
}
843+
gui->gifAddFrame(key_pass, make_rect(xlo, ylo, xhi, yhi), width_px, dbu_per_pixel, delay_pass);
840844
}
841845

842846
void gif_end(int key)
843847
{
844848
if (!check_gui("gif_end")) {
845849
return;
846850
}
851+
std::optional<int> key_pass;
852+
if (key >= 0) {
853+
key_pass = key;
854+
}
847855
auto gui = gui::Gui::get();
848-
gui->gifEnd(key);
856+
gui->gifEnd(key_pass);
849857
}
850858

851859
%} // inline

src/gui/src/gui.tcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ proc save_animated_gif { args } {
190190
set delay $keys(-delay)
191191
}
192192

193-
set key 0
193+
set key -1
194194
if { [info exists keys(-key)] } {
195195
set key $keys(-key)
196196
}

src/gui/src/stub.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ int Gui::gifStart(const std::string& filename)
209209
return 0;
210210
}
211211

212-
void Gui::gifEnd(const int key)
212+
void Gui::gifEnd(std::optional<int> key)
213213
{
214214
}
215215

216-
void Gui::gifAddFrame(const int key,
216+
void Gui::gifAddFrame(std::optional<int> key,
217217
const odb::Rect& region,
218218
int width_px,
219219
double dbu_per_pixel,

0 commit comments

Comments
 (0)