diff --git a/toonz/sources/toonz/filebrowserpopup.cpp b/toonz/sources/toonz/filebrowserpopup.cpp index 4a1acef..737a02f 100644 --- a/toonz/sources/toonz/filebrowserpopup.cpp +++ b/toonz/sources/toonz/filebrowserpopup.cpp @@ -1527,6 +1527,55 @@ public: } saveLevelAsCommandHandler; //============================================================================= +// Used both for ReplaceLevelPopup and ReplaceParentDirectoryPopup +// which are needed to maintain the current selection on open. + +template +class OpenReplaceFilePopupHandler final : public MenuItemHandler { + T *m_popup; + +public: + OpenReplaceFilePopupHandler(CommandId cmdId) + : MenuItemHandler(cmdId), m_popup(0) {} + + // The current selection is cleared on the construction of FileBrowser + // ( by calling makeCurrent() in DvDirTreeView::currentChanged() ). + // Thus checking the selection must be done BEFORE making the browser + // or it fails to get the selection on the first call of this command. + bool initialize(TCellSelection::Range &range, std::set &columnRange, + bool &replaceCells) { + TSelection *sel = TApp::instance()->getCurrentSelection()->getSelection(); + if (!sel) return false; + TCellSelection *cellSel = dynamic_cast(sel); + TColumnSelection *columnSel = dynamic_cast(sel); + if ((!cellSel && !columnSel) || sel->isEmpty()) { + DVGui::error(tr("Nothing to replace: no cells or columns selected.")); + return false; + } + if (cellSel) { + range = cellSel->getSelectedCells(); + replaceCells = true; + } else if (columnSel) { + columnRange = columnSel->getIndices(); + replaceCells = false; + } + return true; + } + + void execute() override { + TCellSelection::Range range; + std::set columnRange; + bool replaceCells; + if (!initialize(range, columnRange, replaceCells)) return; + if (!m_popup) m_popup = new T(); + m_popup->setRange(range, columnRange, replaceCells); + m_popup->show(); + m_popup->raise(); + m_popup->activateWindow(); + } +}; + +//============================================================================= // ReplaceLevelPopup ReplaceLevelPopup::ReplaceLevelPopup() @@ -1537,24 +1586,12 @@ ReplaceLevelPopup::ReplaceLevelPopup() SLOT(onSelectionChanged(TSelection *))); } -void ReplaceLevelPopup::show() { - TSelection *sel = TApp::instance()->getCurrentSelection()->getSelection(); - if (!sel) return; - TCellSelection *cellSel = dynamic_cast(sel); - TColumnSelection *columnSel = dynamic_cast(sel); - if ((!cellSel && !columnSel) || sel->isEmpty()) { - DVGui::error(tr("Nothing to replace: no cells selected.")); - return; - } - - if (cellSel) { - m_range = cellSel->getSelectedCells(); - m_replaceCells = true; - } else if (columnSel) { - m_columnRange = columnSel->getIndices(); - m_replaceCells = false; - } - QDialog::show(); +void ReplaceLevelPopup::setRange(TCellSelection::Range &range, + std::set &columnRange, + bool &replaceCells) { + m_range = range; + m_columnRange = columnRange; + m_replaceCells = replaceCells; } bool ReplaceLevelPopup::execute() { @@ -1860,50 +1897,6 @@ void LoadColorModelPopup::showEvent(QShowEvent *e) { /*! replace the parent folder path of the levels in the selected cells */ -class OpenReplaceParentDirectoryPopupHandler final : public MenuItemHandler { - ReplaceParentDirectoryPopup *m_popup; - -public: - OpenReplaceParentDirectoryPopupHandler() - : MenuItemHandler(MI_ReplaceParentDirectory), m_popup(0) {} - - // The current selection is cleared on the construction of FileBrowser - // ( by calling makeCurrent() in DvDirTreeView::currentChanged() ). - // Thus checking the selection must be done BEFORE making the browser - // or it fails to get the selection on the first call of this command. - bool initialize(TCellSelection::Range &range, std::set &columnRange, - bool &replaceCells) { - TSelection *sel = TApp::instance()->getCurrentSelection()->getSelection(); - if (!sel) return false; - TCellSelection *cellSel = dynamic_cast(sel); - TColumnSelection *columnSel = dynamic_cast(sel); - if ((!cellSel && !columnSel) || sel->isEmpty()) { - DVGui::error(tr("Nothing to replace: no cells or columns selected.")); - return false; - } - if (cellSel) { - range = cellSel->getSelectedCells(); - replaceCells = true; - } else if (columnSel) { - columnRange = columnSel->getIndices(); - replaceCells = false; - } - return true; - } - - void execute() override { - TCellSelection::Range range; - std::set columnRange; - bool replaceCells; - if (!initialize(range, columnRange, replaceCells)) return; - if (!m_popup) m_popup = new ReplaceParentDirectoryPopup(); - m_popup->setRange(range, columnRange, replaceCells); - m_popup->show(); - m_popup->raise(); - m_popup->activateWindow(); - } -} openReplaceParentDirectoryPopupHandler; - ReplaceParentDirectoryPopup::ReplaceParentDirectoryPopup() : FileBrowserPopup(tr("Replace Parent Directory")) { setOkText(tr("Replace")); @@ -2162,8 +2155,6 @@ OpenPopupCommandHandler saveSubSceneAsPopupCommand( OpenPopupCommandHandler loadLevelPopupCommand(MI_LoadLevel); OpenPopupCommandHandler convertWithInputPopupCommand( MI_ConvertFileWithInput); -OpenPopupCommandHandler replaceLevelPopupCommand( - MI_ReplaceLevel); OpenPopupCommandHandler savePalettePopupCommand( MI_SavePaletteAs); OpenPopupCommandHandler loadColorModelPopupCommand( @@ -2171,5 +2162,10 @@ OpenPopupCommandHandler loadColorModelPopupCommand( OpenPopupCommandHandler importMagpieFilePopupCommand( MI_ImportMagpieFile); -// Note: MI_ReplaceParentDirectory uses the original hadler in order to obtain -// the selection information before making the browser. \ No newline at end of file +// Note: MI_ReplaceLevel and MI_ReplaceParentDirectory uses the original +// handler in order to obtain the selection information before making the +// browser. +OpenReplaceFilePopupHandler replaceLevelPopupCommand( + MI_ReplaceLevel); +OpenReplaceFilePopupHandler + replaceParentFolderPopupCommand(MI_ReplaceParentDirectory); \ No newline at end of file diff --git a/toonz/sources/toonz/filebrowserpopup.h b/toonz/sources/toonz/filebrowserpopup.h index 47e6b74..03af3ae 100644 --- a/toonz/sources/toonz/filebrowserpopup.h +++ b/toonz/sources/toonz/filebrowserpopup.h @@ -332,9 +332,10 @@ public: ReplaceLevelPopup(); bool execute() override; - void show(); void initFolder() override; + void setRange(TCellSelection::Range &range, std::set &columnRange, + bool &replaceCells); protected slots: void onSelectionChanged(TSelection *selection);