1 // Aseprite
2 // Copyright (C) 2001-2017  David Capello
3 //
4 // This program is distributed under the terms of
5 // the End-User License Agreement for Aseprite.
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include "app/app.h"
12 #include "app/commands/command.h"
13 #include "app/context_access.h"
14 #include "app/doc_api.h"
15 #include "app/i18n/strings.h"
16 #include "app/modules/gui.h"
17 #include "app/transaction.h"
18 #include "app/ui/status_bar.h"
19 #include "doc/layer.h"
20 #include "doc/sprite.h"
21 #include "ui/alert.h"
22 #include "ui/widget.h"
23 
24 namespace app {
25 
26 class RemoveLayerCommand : public Command {
27 public:
28   RemoveLayerCommand();
clone() const29   Command* clone() const override { return new RemoveLayerCommand(*this); }
30 
31 protected:
32   bool onEnabled(Context* context) override;
33   void onExecute(Context* context) override;
34 };
35 
RemoveLayerCommand()36 RemoveLayerCommand::RemoveLayerCommand()
37   : Command(CommandId::RemoveLayer(), CmdRecordableFlag)
38 {
39 }
40 
onEnabled(Context * context)41 bool RemoveLayerCommand::onEnabled(Context* context)
42 {
43   return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
44                              ContextFlags::HasActiveSprite |
45                              ContextFlags::HasActiveLayer);
46 }
47 
onExecute(Context * context)48 void RemoveLayerCommand::onExecute(Context* context)
49 {
50   std::string layerName;
51   ContextWriter writer(context);
52   Doc* document(writer.document());
53   Sprite* sprite(writer.sprite());
54   {
55     Transaction transaction(writer.context(), "Remove Layer");
56     DocApi api = document->getApi(transaction);
57 
58     const Site* site = writer.site();
59     if (site->inTimeline() &&
60         !site->selectedLayers().empty()) {
61       SelectedLayers selLayers = site->selectedLayers();
62       selLayers.removeChildrenIfParentIsSelected();
63 
64       layer_t deletedTopLevelLayers = 0;
65       for (Layer* layer : selLayers) {
66         if (layer->parent() == sprite->root())
67           ++deletedTopLevelLayers;
68       }
69 
70       if (deletedTopLevelLayers == sprite->root()->layersCount()) {
71         ui::Alert::show(Strings::alerts_cannot_delete_all_layers());
72         return;
73       }
74 
75       for (Layer* layer : selLayers) {
76         api.removeLayer(layer);
77       }
78     }
79     else {
80       if (sprite->allLayersCount() == 1) {
81         ui::Alert::show(Strings::alerts_cannot_delete_all_layers());
82         return;
83       }
84 
85       Layer* layer = writer.layer();
86       layerName = layer->name();
87       api.removeLayer(layer);
88     }
89 
90     transaction.commit();
91   }
92   update_screen_for_document(document);
93 
94   StatusBar::instance()->invalidate();
95   if (!layerName.empty())
96     StatusBar::instance()->showTip(1000, "Layer '%s' removed", layerName.c_str());
97   else
98     StatusBar::instance()->showTip(1000, "Layers removed");
99 }
100 
createRemoveLayerCommand()101 Command* CommandFactory::createRemoveLayerCommand()
102 {
103   return new RemoveLayerCommand;
104 }
105 
106 } // namespace app
107