Clean up favorites preferences code

This commit is contained in:
Lawrence Lee 2021-12-11 15:35:52 -08:00
parent 80944d6c9b
commit 546c99d276
3 changed files with 85 additions and 40 deletions

View File

@ -199,7 +199,7 @@ private:
Gtk::Button buttonDown;
Gtk::Button buttonRemove;
sigc::signal<void, const std::vector<Gtk::TreeModel::Path>> signalRowsPreErase;
sigc::signal<void, const std::vector<Gtk::TreeModel::Path> &> signalRowsPreErase;
void onButtonDownPressed();
void onButtonRemovePressed();
@ -224,7 +224,7 @@ public:
* The signal contains a vector of tree model paths of the rows that will be
* erased.
*/
sigc::signal<void, const std::vector<Gtk::TreeModel::Path>> getSignalRowsPreErase();
sigc::signal<void, const std::vector<Gtk::TreeModel::Path> &> getSignalRowsPreErase() const;
};
/**
@ -319,7 +319,7 @@ void ListEditButtons::onButtonDownPressed()
}
// Get the selected row and next row.
auto selected_row_iter = listStore->get_iter(selected[0]);
const auto selected_row_iter = listStore->get_iter(selected[0]);
auto next_row_iter = selected_row_iter;
next_row_iter++;
@ -336,17 +336,21 @@ void ListEditButtons::onButtonRemovePressed()
{
const std::vector<Gtk::TreeModel::Path> selected_paths =
list.get_selection()->get_selected_rows();
std::vector<Gtk::TreeModel::RowReference> selected;
std::vector<Gtk::TreeModel::RowReference> selected(selected_paths.size());
// Get row references, which are valid until the row is removed.
for (const auto & row_path : selected_paths) {
selected.push_back(Gtk::TreeModel::RowReference(listStore, row_path));
}
std::transform(
selected_paths.begin(),
selected_paths.end(),
selected.begin(),
[this](const Gtk::TreeModel::Path &row_path) {
return Gtk::TreeModel::RowReference(listStore, row_path);
});
signalRowsPreErase.emit(selected_paths);
// Remove the selected rows.
for (const auto & row_ref : selected) {
for (const auto &row_ref : selected) {
const auto row_path = row_ref.get_path();
if (row_path) {
listStore->erase(listStore->get_iter(row_path));
@ -368,7 +372,7 @@ void ListEditButtons::onButtonUpPressed()
return;
}
auto selected_row_iter = listStore->get_iter(selected[0]);
const auto selected_row_iter = listStore->get_iter(selected[0]);
if (selected_row_iter == list_children.begin()) { // Can't be first row.
return;
@ -411,8 +415,8 @@ void ListEditButtons::updateButtonSensitivity()
buttonRemove.set_sensitive(selected.size() > 0);
}
sigc::signal<void, const std::vector<Gtk::TreeModel::Path>>
ListEditButtons::getSignalRowsPreErase()
sigc::signal<void, const std::vector<Gtk::TreeModel::Path> &>
ListEditButtons::getSignalRowsPreErase() const
{
return signalRowsPreErase;
}
@ -420,8 +424,6 @@ ListEditButtons::getSignalRowsPreErase()
}
struct ToolLocationPreference::Impl {
static std::unordered_map<std::string, Tool> toolNamesReverseMap;
Options &options;
// General options.
@ -488,7 +490,7 @@ struct ToolLocationPreference::Impl {
* @param paths Paths in the favorites list pointing to the rows that are
* about to be removed.
*/
void onFavoritesRowsPreRemove(const std::vector<Gtk::TreeModel::Path> paths);
void onFavoritesRowsPreRemove(const std::vector<Gtk::TreeModel::Path> &paths);
/**
* Converts tool names to their corresponding tools.
*
@ -504,9 +506,6 @@ struct ToolLocationPreference::Impl {
void updateOptions();
};
std::unordered_map<std::string, Tool>
ToolLocationPreference::Impl::toolNamesReverseMap;
ToolLocationPreference::Impl::Impl(Options &options) :
options(options),
@ -577,7 +576,7 @@ void ToolLocationPreference::Impl::favoriteToggled(const Glib::ustring &row_path
// Update the favorites list.
if (is_favorite) {
// Add to favorites list.
auto new_favorite_row_iter = favoritesModelPtr->append();
const auto new_favorite_row_iter = favoritesModelPtr->append();
new_favorite_row_iter->set_value(
favoritesColumns.toolName,
M(getToolTitleKey(tool)));
@ -602,7 +601,7 @@ void ToolLocationPreference::Impl::initFavoritesRows(
{
// Add the favorites to the favorites list store.
for (const auto tool : favorites) {
auto favorite_row_iter = favoritesModelPtr->append();
const auto favorite_row_iter = favoritesModelPtr->append();
favorite_row_iter->set_value(
favoritesColumns.toolName,
M(getToolTitleKey(tool)));
@ -617,7 +616,8 @@ void ToolLocationPreference::Impl::addToolListRowGroup(
{
// Recursively add the tool and its children to the tool list tree store.
for (const ToolPanelCoordinator::ToolTree &tool : tools) {
auto tool_row_iter = toolListModelPtr->append(parentRowIter->children());
const auto tool_row_iter =
toolListModelPtr->append(parentRowIter->children());
tool_row_iter->set_value(
toolListColumns.toolName,
M(getToolTitleKey(tool.id)));
@ -653,7 +653,7 @@ void ToolLocationPreference::Impl::initToolListRows(const std::vector<Tool> &fav
ToolPanelCoordinator::Panel::TRANSFORM_PANEL,
ToolPanelCoordinator::Panel::RAW,
}) {
auto tool_group_iter = toolListModelPtr->append();
const auto tool_group_iter = toolListModelPtr->append();
tool_group_iter->set_value(
toolListColumns.toolName,
M(getToolPanelTitleKey(panel)));
@ -662,7 +662,7 @@ void ToolLocationPreference::Impl::initToolListRows(const std::vector<Tool> &fav
}
void ToolLocationPreference::Impl::onFavoritesRowsPreRemove(
const std::vector<Gtk::TreeModel::Path> paths)
const std::vector<Gtk::TreeModel::Path> &paths)
{
// Unset the favorite column in the tools list for tools about to be removed
// from the favorites list.
@ -678,13 +678,15 @@ std::vector<Tool> ToolLocationPreference::Impl::toolNamesToTools(
{
std::vector<Tool> tool_set;
for (auto &&tool_name : tool_names) {
for (const auto &tool_name : tool_names) {
Tool tool;
try {
tool = ToolPanelCoordinator::getToolFromName(tool_name);
} catch (const std::exception &e) {
} catch (const std::out_of_range &e) {
if (rtengine::settings->verbose) {
std::cerr << "Unrecognized tool name \"" << tool_name << "\"." << std::endl;
std::cerr
<< "Unrecognized tool name \"" << tool_name << "\"."
<< std::endl;
}
assert(false);
continue;
@ -701,7 +703,7 @@ void ToolLocationPreference::Impl::updateOptions()
const auto favorites_rows = favoritesModelPtr->children();
options.favorites.resize(favorites_rows.size());
for (unsigned i = 0; i < favorites_rows.size(); i++) {
for (Gtk::TreeNodeChildren::size_type i = 0; i < favorites_rows.size(); i++) {
const Tool tool = favorites_rows[i].get_value(favoritesColumns.tool);
options.favorites[i] = ToolPanelCoordinator::getToolName(tool);
}

View File

@ -16,6 +16,8 @@
* You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see <https://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "multilangmgr.h"
#include "toolpanelcoord.h"
#include "metadatapanel.h"
@ -350,12 +352,14 @@ ToolPanelCoordinator::ToolPanelCoordinator (bool batch) : ipc (nullptr), favorit
for (const auto &panel_tool_layout : getDefaultToolLayout()) {
const auto &panel_tools = panel_tool_layout.second;
std::vector<const ToolTree *> unprocessed_tools;
std::vector<const ToolTree *> unprocessed_tools(panel_tools.size());
// Start with the root tools for every panel.
for (const auto &tool_tree : panel_tools) {
unprocessed_tools.push_back(&tool_tree);
}
std::transform(
panel_tools.begin(),
panel_tools.end(),
unprocessed_tools.begin(),
[](const ToolTree &tool_tree) { return &tool_tree; });
// Process each tool.
while (!unprocessed_tools.empty()) {
@ -363,7 +367,7 @@ ToolPanelCoordinator::ToolPanelCoordinator (bool batch) : ipc (nullptr), favorit
const ToolTree *cur_tool = unprocessed_tools.back();
unprocessed_tools.pop_back();
// Add tool to list of expanders and tool panels.
FoldableToolPanel *tool_panel = getFoldableToolPanel(*cur_tool);
FoldableToolPanel *const tool_panel = getFoldableToolPanel(*cur_tool);
expList.push_back(tool_panel->getExpander());
toolPanels.push_back(tool_panel);
expanderToToolPanelMap[tool_panel->getExpander()] = tool_panel;
@ -514,7 +518,7 @@ ToolPanelCoordinator::ToolPanelCoordinator (bool batch) : ipc (nullptr), favorit
prevPage = toolPanelNotebook->get_nth_page(0);
}
const ToolPanelCoordinator::ToolLayout& ToolPanelCoordinator::getDefaultToolLayout()
const ToolPanelCoordinator::ToolLayout &ToolPanelCoordinator::getDefaultToolLayout()
{
return PANEL_TOOLS;
}
@ -708,10 +712,22 @@ void ToolPanelCoordinator::updateFavoritesPanel(
std::vector<std::reference_wrapper<const ToolTree>> favorites_tool_tree;
for (const auto &tool_name : favoritesNames) {
Tool tool = getToolFromName(tool_name.raw());
Tool tool;
try {
tool = getToolFromName(tool_name.raw());
} catch (const std::out_of_range &e) {
if (rtengine::settings->verbose) {
std::cerr
<< "Unrecognized favorite tool \"" << tool_name << "\""
<< std::endl;
}
continue;
}
if (isFavoritable(tool)) {
favorites_set.insert(tool);
favorites_tool_tree.push_back(
std::ref(*(toolToDefaultToolTreeMap.at(tool))));
}
}
updateToolPanel(
@ -758,7 +774,20 @@ void ToolPanelCoordinator::updatePanelTools(
std::unordered_set<Tool, ScopedEnumHash> favoriteTools;
for (const auto &tool_name : favorites) {
favoriteTools.insert(getToolFromName(tool_name.raw()));
Tool tool;
try {
tool = getToolFromName(tool_name.raw());
} catch (const std::out_of_range &e) {
if (rtengine::settings->verbose) {
std::cerr
<< "Unrecognized favorite tool \"" << tool_name << "\""
<< std::endl;
}
continue;
}
if (isFavoritable(tool)) {
favoriteTools.insert(tool);
}
}
updateToolPanel(panel, *default_panel_tools, 1, favoriteTools, cloneFavoriteTools);
@ -770,7 +799,7 @@ ToolPanelCoordinator::updateToolPanel(
Gtk::Box *panelBox,
const std::vector<T> &children,
int level,
std::unordered_set<Tool, ScopedEnumHash> favorites,
const std::unordered_set<Tool, ScopedEnumHash> &favorites,
bool cloneFavoriteTools)
{
const bool is_favorite_panel = panelBox == favoritePanel;
@ -1646,8 +1675,20 @@ void ToolPanelCoordinator::updateToolLocations(
// Update favorite tool panels list.
favoritesToolPanels.clear();
for (const auto &favorite_name : favorites) {
favoritesToolPanels.push_back(
getFoldableToolPanel(getToolFromName(favorite_name)));
Tool tool;
try {
tool = getToolFromName(favorite_name.raw());
} catch (const std::out_of_range &e) {
if (rtengine::settings->verbose) {
std::cerr
<< "Unrecognized favorite tool \"" << favorite_name << "\""
<< std::endl;
}
continue;
}
if (isFavoritable(tool)) {
favoritesToolPanels.push_back(getFoldableToolPanel(tool));
}
}
int cur_page_num = toolPanelNotebook->get_current_page();
@ -1948,6 +1989,7 @@ FoldableToolPanel *ToolPanelCoordinator::getFoldableToolPanel(Tool tool) const
case Tool::PD_SHARPENING:
return pdSharpening;
};
assert(false);
return nullptr;
}

View File

@ -305,7 +305,7 @@ public:
std::vector<ToolTree> children;
};
using ToolLayout = std::unordered_map<Panel, const std::vector<ToolTree>, ScopedEnumHash>;
using ToolLayout = std::unordered_map<Panel, const std::vector<ToolTree> &, ScopedEnumHash>;
CoarsePanel* coarse;
Gtk::Notebook* toolPanelNotebook;
@ -313,12 +313,13 @@ public:
ToolPanelCoordinator(bool batch = false);
~ToolPanelCoordinator () override;
static const ToolLayout& getDefaultToolLayout();
static const ToolLayout &getDefaultToolLayout();
/**
* Gets the tool with the provided tool name.
*
* @param name The tool name as a raw string.
* @return The tool.
* @throws std::out_of_range If the name is not recognized.
*/
static Tool getToolFromName(const std::string &name);
/**
@ -472,7 +473,7 @@ protected:
Gtk::Box *panelBox,
const std::vector<T> &children,
int level,
std::unordered_set<Tool, ScopedEnumHash> favorites,
const std::unordered_set<Tool, ScopedEnumHash> &favorites,
bool cloneFavoriteTools);
private: