1 
2 
3 #include "savepresetpopup.h"
4 
5 // Tnz6 includes
6 #include "menubarcommandids.h"
7 #include "filebrowsermodel.h"
8 #include "tapp.h"
9 
10 // TnzQt includes
11 #include "toonzqt/menubarcommand.h"
12 
13 // TnzLib includes
14 #include "toonz/tfxhandle.h"
15 #include "toonz/tcolumnfx.h"
16 #include "toonz/toonzfolders.h"
17 
18 // TnzBase includes
19 #include "tfx.h"
20 #include "tmacrofx.h"
21 
22 // TnzCore includes
23 #include "tsystem.h"
24 #include "tstream.h"
25 #include "tfxattributes.h"
26 
27 // Qt includes
28 #include <QPushButton>
29 #include <QMainWindow>
30 
31 //=============================================================================
32 // SavePresetPopup
33 //-----------------------------------------------------------------------------
34 
SavePresetPopup()35 SavePresetPopup::SavePresetPopup()
36     : Dialog(TApp::instance()->getMainWindow(), true, true, "SavePreset") {
37   setWindowTitle(tr("Save Preset"));
38   addWidget(tr("Preset Name:"), m_nameFld = new DVGui::LineEdit(this));
39   m_nameFld->setMinimumWidth(170);
40 
41   QPushButton *okBtn = new QPushButton(tr("Save"), this);
42   okBtn->setDefault(true);
43   QPushButton *cancelBtn = new QPushButton(tr("Cancel"), this);
44   connect(okBtn, SIGNAL(clicked()), this, SLOT(onOkBtn()));
45   connect(cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
46 
47   addButtonBarWidget(okBtn, cancelBtn);
48 }
49 
50 //-----------------------------------------------------------------------------
51 
~SavePresetPopup()52 SavePresetPopup::~SavePresetPopup() {}
53 
54 //-----------------------------------------------------------------------------
55 
apply()56 bool SavePresetPopup::apply() {
57   TFxHandle *fxHandle = TApp::instance()->getCurrentFx();
58   TFx *fx             = fxHandle->getFx();
59   if (TZeraryColumnFx *zfx = dynamic_cast<TZeraryColumnFx *>(fx))
60     fx = zfx->getZeraryFx();
61   if (!fx) return 0;
62 
63   TMacroFx *macroFx = dynamic_cast<TMacroFx *>(fx);
64   bool isMacro      = macroFx != 0;
65   std::wstring name = m_nameFld->text().toStdWString();
66   if (name.empty()) return 0;
67   TFilePath fp = ToonzFolder::getFxPresetFolder() + "presets" +
68                  fx->getFxType() + (name + (isMacro ? L".macrofx" : L".fx"));
69   if (!TFileStatus(fp.getParentDir()).doesExist()) {
70     try {
71       TFilePath parent = fp.getParentDir();
72       TSystem::mkDir(parent);
73       DvDirModel::instance()->refreshFolder(parent.getParentDir());
74     } catch (...) {
75       DVGui::error(
76           tr("It is not possible to create the preset folder %1.")
77               .arg(QString::fromStdString(fp.getParentDir().getName())));
78       return 0;
79     }
80   }
81   if (TFileStatus(fp).doesExist()) {
82     if (DVGui::MsgBox(tr("Do you want to overwrite?"), tr("Yes"), tr("No")) ==
83         2)
84       return 0;
85   }
86   if (isMacro) {
87     TOStream os(fp);
88     TFx *fx2                                    = fx->clone(false);
89     fx2->getAttributes()->passiveCacheDataIdx() = -1;
90     os << fx2;
91     delete fx2;
92   } else {
93     TOStream os(fp);
94     fx->savePreset(os);
95   }
96   fxHandle->notifyFxPresetSaved();
97   return true;
98 }
99 
100 //-----------------------------------------------------------------------------
101 
onOkBtn()102 void SavePresetPopup::onOkBtn() {
103   if (apply()) accept();
104 }
105 
106 //-----------------------------------------------------------------------------
107 
108 OpenPopupCommandHandler<SavePresetPopup> openSavePresetPopup(MI_SavePreset);
109