2011-10-28 22:40:43 +02:00
|
|
|
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
#include "hotkey_data_view_model.h"
|
|
|
|
|
|
|
|
#include "command/command.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "include/aegisub/hotkey.h"
|
|
|
|
#include "preferences.h"
|
|
|
|
|
2014-06-04 16:30:52 +02:00
|
|
|
#include <libaegisub/exception.h>
|
|
|
|
#include <libaegisub/hotkey.h>
|
|
|
|
#include <libaegisub/make_unique.h>
|
2011-10-28 22:40:43 +02:00
|
|
|
|
2012-02-02 20:18:10 +01:00
|
|
|
#include <algorithm>
|
2014-06-04 16:30:52 +02:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2014-06-04 16:38:12 +02:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2011-10-28 22:40:43 +02:00
|
|
|
|
|
|
|
using namespace agi::hotkey;
|
|
|
|
|
|
|
|
/// @class HotkeyModelItem
|
|
|
|
/// @brief A base class for things exposed by HotkeyDataViewModel
|
|
|
|
class HotkeyModelItem {
|
2014-04-25 19:01:07 +02:00
|
|
|
protected:
|
|
|
|
~HotkeyModelItem() = default;
|
2011-10-28 22:40:43 +02:00
|
|
|
public:
|
|
|
|
virtual unsigned int GetChildren(wxDataViewItemArray &children) const=0;
|
|
|
|
virtual wxDataViewItem GetParent() const=0;
|
|
|
|
virtual void GetValue(wxVariant &variant, unsigned int col) const=0;
|
|
|
|
virtual bool IsContainer() const=0;
|
|
|
|
virtual bool SetValue(wxVariant const& variant, unsigned int col)=0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class HotkeyModelRoot;
|
|
|
|
class HotkeyModelCategory;
|
|
|
|
|
|
|
|
/// @class HotkeyModelCombo
|
|
|
|
/// @brief A single hotkey exposed in the data view
|
|
|
|
///
|
|
|
|
/// All actual mutation of hotkeys happens through this class
|
2014-03-13 02:39:07 +01:00
|
|
|
class HotkeyModelCombo final : public HotkeyModelItem {
|
2011-10-28 22:40:43 +02:00
|
|
|
HotkeyModelCategory *parent; ///< The containing category
|
2014-06-04 16:30:52 +02:00
|
|
|
Combo combo; ///< The actual hotkey
|
|
|
|
std::string cmd_name;
|
|
|
|
std::string cmd_str;
|
2011-10-28 22:40:43 +02:00
|
|
|
public:
|
|
|
|
HotkeyModelCombo(HotkeyModelCategory *parent, Combo const& combo)
|
|
|
|
: parent(parent)
|
|
|
|
, combo(combo)
|
2014-06-04 16:30:52 +02:00
|
|
|
, cmd_name(combo.CmdName())
|
|
|
|
, cmd_str(combo.Str())
|
2011-10-28 22:40:43 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-04 16:30:52 +02:00
|
|
|
bool IsVisible(std::string const& filter) const {
|
|
|
|
return boost::to_lower_copy(cmd_name).find(filter) != std::string::npos
|
|
|
|
|| boost::to_lower_copy(cmd_str).find(filter) != std::string::npos;
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Apply(Hotkey::HotkeyMap *hk_map) {
|
2012-02-28 02:22:58 +01:00
|
|
|
if (combo.CmdName().size() || combo.Str().size())
|
|
|
|
hk_map->insert(make_pair(combo.CmdName(), combo));
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
unsigned int GetChildren(wxDataViewItemArray &) const override { return 0; }
|
|
|
|
wxDataViewItem GetParent() const override { return wxDataViewItem(parent); }
|
|
|
|
bool IsContainer() const override { return false; }
|
2011-10-28 22:40:43 +02:00
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
void GetValue(wxVariant &variant, unsigned int col) const override {
|
2011-10-28 22:40:43 +02:00
|
|
|
if (col == 0)
|
2013-01-04 16:01:50 +01:00
|
|
|
variant = to_wx(combo.Str());
|
2011-10-28 22:40:43 +02:00
|
|
|
else if (col == 1) {
|
|
|
|
wxIcon icon;
|
2014-01-02 04:20:50 +01:00
|
|
|
try {
|
|
|
|
auto icon_bmp = cmd::get(combo.CmdName())->Icon(16);
|
|
|
|
if (icon_bmp.IsOk())
|
|
|
|
icon.CopyFromBitmap(icon_bmp);
|
|
|
|
}
|
2014-01-04 01:56:18 +01:00
|
|
|
catch (agi::Exception const&) {
|
2014-01-02 04:20:50 +01:00
|
|
|
// Just use no icon; error is reported in the description column
|
|
|
|
}
|
2013-01-04 16:01:50 +01:00
|
|
|
variant << wxDataViewIconText(to_wx(combo.CmdName()), icon);
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
else if (col == 2) {
|
|
|
|
try {
|
|
|
|
variant = cmd::get(combo.CmdName())->StrHelp();
|
|
|
|
}
|
|
|
|
catch (agi::Exception const& e) {
|
2014-05-29 14:57:27 +02:00
|
|
|
variant = to_wx(e.GetMessage());
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-05-29 14:57:27 +02:00
|
|
|
throw agi::InternalError("HotkeyDataViewModel asked for an invalid column number");
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
bool SetValue(wxVariant const& variant, unsigned int col) override {
|
2011-10-28 22:40:43 +02:00
|
|
|
if (col == 0) {
|
2014-07-05 05:16:24 +02:00
|
|
|
combo = Combo(combo.Context(), combo.CmdName(), from_wx(variant.GetString()));
|
2014-06-04 16:30:52 +02:00
|
|
|
cmd_str = combo.Str();
|
2011-10-28 22:40:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (col == 1) {
|
|
|
|
wxDataViewIconText text;
|
|
|
|
text << variant;
|
2014-06-04 16:30:52 +02:00
|
|
|
cmd_name = from_wx(text.GetText());
|
2014-07-05 05:16:24 +02:00
|
|
|
combo = Combo(combo.Context(), cmd_name, combo.Str());
|
2011-10-28 22:40:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A hotkey context exposed in the data view
|
2014-03-13 02:39:07 +01:00
|
|
|
class HotkeyModelCategory final : public HotkeyModelItem {
|
2011-10-28 22:40:43 +02:00
|
|
|
std::list<HotkeyModelCombo> children;
|
|
|
|
wxDataViewModel *model;
|
2015-02-28 23:36:28 +01:00
|
|
|
std::string name;
|
|
|
|
wxString translated_name;
|
2011-10-28 22:40:43 +02:00
|
|
|
wxDataViewItemArray visible_items;
|
|
|
|
public:
|
2015-02-28 23:36:28 +01:00
|
|
|
HotkeyModelCategory(wxDataViewModel *model, std::string const& name)
|
2011-10-28 22:40:43 +02:00
|
|
|
: model(model)
|
2015-02-28 23:36:28 +01:00
|
|
|
, name(name)
|
|
|
|
, translated_name(wxGetTranslation(to_wx(name)))
|
2011-10-28 22:40:43 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-01 19:47:00 +01:00
|
|
|
wxDataViewItem AddChild(Combo const& combo) {
|
2012-11-28 16:35:26 +01:00
|
|
|
children.emplace_back(this, combo);
|
2012-12-01 03:47:24 +01:00
|
|
|
visible_items.push_back(wxDataViewItem(&children.back()));
|
2011-10-28 22:40:43 +02:00
|
|
|
model->ItemAdded(wxDataViewItem(this), wxDataViewItem(&children.back()));
|
2012-02-01 19:47:00 +01:00
|
|
|
return wxDataViewItem(&children.back());
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Delete(wxDataViewItem const& item) {
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto it = children.begin(); it != children.end(); ++it) {
|
2011-10-28 22:40:43 +02:00
|
|
|
if (&*it == item.GetID()) {
|
|
|
|
model->ItemDeleted(wxDataViewItem(this), wxDataViewItem((void*)&*it));
|
|
|
|
children.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Apply(Hotkey::HotkeyMap *hk_map) {
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto& combo : children)
|
|
|
|
combo.Apply(hk_map);
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
|
2014-06-04 16:30:52 +02:00
|
|
|
void SetFilter(std::string const& new_filter) {
|
2014-06-04 16:38:12 +02:00
|
|
|
std::vector<HotkeyModelCombo *> old_visible;
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto item : visible_items)
|
2014-06-04 16:38:12 +02:00
|
|
|
old_visible.push_back(static_cast<HotkeyModelCombo*>(item.GetID()));
|
|
|
|
sort(begin(old_visible), end(old_visible));
|
2011-10-28 22:40:43 +02:00
|
|
|
|
|
|
|
visible_items.clear();
|
|
|
|
|
|
|
|
wxDataViewItemArray added;
|
|
|
|
wxDataViewItemArray removed;
|
|
|
|
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto& combo : children) {
|
2014-06-04 16:38:12 +02:00
|
|
|
bool was_visible = binary_search(begin(old_visible), end(old_visible), &combo);
|
2012-11-04 04:53:03 +01:00
|
|
|
bool is_visible = combo.IsVisible(new_filter);
|
2011-10-28 22:40:43 +02:00
|
|
|
|
|
|
|
if (is_visible)
|
2012-12-01 03:47:24 +01:00
|
|
|
visible_items.push_back(wxDataViewItem(&combo));
|
2011-10-28 22:40:43 +02:00
|
|
|
if (was_visible && !is_visible)
|
2012-12-01 03:47:24 +01:00
|
|
|
removed.push_back(wxDataViewItem(&combo));
|
2011-10-28 22:40:43 +02:00
|
|
|
if (is_visible && !was_visible)
|
2012-12-01 03:47:24 +01:00
|
|
|
added.push_back(wxDataViewItem(&combo));
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!added.empty())
|
|
|
|
model->ItemsAdded(wxDataViewItem(this), added);
|
|
|
|
if (!removed.empty())
|
|
|
|
model->ItemsDeleted(wxDataViewItem(this), removed);
|
|
|
|
}
|
|
|
|
|
2015-02-28 23:36:28 +01:00
|
|
|
std::string const& GetName() const { return name; }
|
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
wxDataViewItem GetParent() const override { return wxDataViewItem(nullptr); }
|
|
|
|
bool IsContainer() const override { return true; }
|
|
|
|
bool SetValue(wxVariant const&, unsigned int) override { return false; }
|
|
|
|
void GetValue(wxVariant &variant, unsigned int col) const override {
|
2011-10-28 22:40:43 +02:00
|
|
|
if (col == 1)
|
2015-02-28 23:36:28 +01:00
|
|
|
variant << wxDataViewIconText(translated_name);
|
2011-10-28 22:40:43 +02:00
|
|
|
else
|
2015-02-28 23:36:28 +01:00
|
|
|
variant = translated_name;
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
unsigned int GetChildren(wxDataViewItemArray &out) const override {
|
2011-10-28 22:40:43 +02:00
|
|
|
out = visible_items;
|
|
|
|
return out.size();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The root containing the hotkey contexts
|
2014-03-13 02:39:07 +01:00
|
|
|
class HotkeyModelRoot final : public HotkeyModelItem {
|
2011-10-28 22:40:43 +02:00
|
|
|
std::list<HotkeyModelCategory> categories;
|
|
|
|
public:
|
|
|
|
HotkeyModelRoot(wxDataViewModel *model) {
|
|
|
|
Hotkey::HotkeyMap const& hk_map = hotkey::inst->GetHotkeyMap();
|
|
|
|
std::map<std::string, HotkeyModelCategory*> cat_map;
|
|
|
|
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto const& category : hk_map) {
|
2015-02-28 23:36:28 +01:00
|
|
|
std::string const& cat_name = category.second.Context();
|
2011-10-28 22:40:43 +02:00
|
|
|
HotkeyModelCategory *cat;
|
2012-11-04 04:53:03 +01:00
|
|
|
auto cat_it = cat_map.find(cat_name);
|
2011-10-28 22:40:43 +02:00
|
|
|
if (cat_it != cat_map.end())
|
|
|
|
cat = cat_it->second;
|
|
|
|
else {
|
2015-02-28 23:36:28 +01:00
|
|
|
categories.emplace_back(model, cat_name);
|
2011-10-28 22:40:43 +02:00
|
|
|
cat = cat_map[cat_name] = &categories.back();
|
|
|
|
}
|
|
|
|
|
2012-11-04 04:53:03 +01:00
|
|
|
cat->AddChild(category.second);
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Apply(Hotkey::HotkeyMap *hk_map) {
|
2013-04-13 17:21:28 +02:00
|
|
|
for (auto& category : categories)
|
|
|
|
category.Apply(hk_map);
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
|
2014-06-04 16:30:52 +02:00
|
|
|
void SetFilter(std::string const& filter) {
|
2013-04-13 17:21:28 +02:00
|
|
|
for (auto& category : categories)
|
|
|
|
category.SetFilter(filter);
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
wxDataViewItem GetParent() const override { return wxDataViewItem(nullptr); }
|
|
|
|
bool IsContainer() const override { return true; }
|
|
|
|
bool SetValue(wxVariant const&, unsigned int) override { return false; }
|
|
|
|
void GetValue(wxVariant &, unsigned int) const override { }
|
2011-10-28 22:40:43 +02:00
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
unsigned int GetChildren(wxDataViewItemArray &out) const override {
|
2011-10-28 22:40:43 +02:00
|
|
|
out.reserve(categories.size());
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto const& category : categories)
|
2012-12-01 03:47:24 +01:00
|
|
|
out.push_back(wxDataViewItem((void*)&category));
|
2011-10-28 22:40:43 +02:00
|
|
|
return out.size();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
HotkeyDataViewModel::HotkeyDataViewModel(Preferences *parent)
|
2014-04-23 22:53:24 +02:00
|
|
|
: root(agi::make_unique<HotkeyModelRoot>(this))
|
2011-10-28 22:40:43 +02:00
|
|
|
, parent(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const HotkeyModelItem * HotkeyDataViewModel::get(wxDataViewItem const& item) const {
|
|
|
|
if (item.IsOk())
|
|
|
|
return static_cast<HotkeyModelItem*>(item.GetID());
|
|
|
|
return root.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
HotkeyModelItem * HotkeyDataViewModel::get(wxDataViewItem const& item) {
|
|
|
|
if (item.IsOk())
|
|
|
|
return static_cast<HotkeyModelItem*>(item.GetID());
|
|
|
|
return root.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int HotkeyDataViewModel::GetChildren(wxDataViewItem const& item, wxDataViewItemArray &children) const {
|
|
|
|
return get(item)->GetChildren(children);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDataViewItem HotkeyDataViewModel::GetParent(wxDataViewItem const& item) const {
|
|
|
|
return get(item)->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HotkeyDataViewModel::GetValue(wxVariant &variant, wxDataViewItem const& item, unsigned int col) const {
|
|
|
|
get(item)->GetValue(variant, col);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HotkeyDataViewModel::IsContainer(wxDataViewItem const& item) const {
|
|
|
|
return get(item)->IsContainer();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HotkeyDataViewModel::SetValue(wxVariant const& variant, wxDataViewItem const& item, unsigned int col) {
|
|
|
|
if (!has_pending_changes) {
|
|
|
|
has_pending_changes = true;
|
2014-06-04 16:34:58 +02:00
|
|
|
parent->AddPendingChange([=] { Apply(); });
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
return get(item)->SetValue(variant, col);
|
|
|
|
}
|
|
|
|
|
2012-02-01 19:47:00 +01:00
|
|
|
wxDataViewItem HotkeyDataViewModel::New(wxDataViewItem item) {
|
|
|
|
if (!item.IsOk()) return wxDataViewItem();
|
2011-10-28 22:40:43 +02:00
|
|
|
|
|
|
|
if (!IsContainer(item))
|
|
|
|
item = GetParent(item);
|
|
|
|
|
|
|
|
HotkeyModelCategory *ctx = static_cast<HotkeyModelCategory*>(item.GetID());
|
2015-02-28 23:36:28 +01:00
|
|
|
return ctx->AddChild(Combo(ctx->GetName(), "", ""));
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void HotkeyDataViewModel::Delete(wxDataViewItem const& item) {
|
|
|
|
if (!item.IsOk() || IsContainer(item)) return;
|
|
|
|
|
|
|
|
static_cast<HotkeyModelCategory*>(GetParent(item).GetID())->Delete(item);
|
|
|
|
|
|
|
|
if (!has_pending_changes) {
|
|
|
|
has_pending_changes = true;
|
2014-06-04 16:34:58 +02:00
|
|
|
parent->AddPendingChange([=] { Apply(); });
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HotkeyDataViewModel::Apply() {
|
|
|
|
Hotkey::HotkeyMap hk_map;
|
|
|
|
root->Apply(&hk_map);
|
|
|
|
hotkey::inst->SetHotkeyMap(hk_map);
|
|
|
|
has_pending_changes = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HotkeyDataViewModel::SetFilter(wxString const& filter) {
|
2014-06-04 16:30:52 +02:00
|
|
|
auto str = from_wx(filter);
|
|
|
|
boost::to_lower(str);
|
|
|
|
root->SetFilter(str);
|
2011-10-28 22:40:43 +02:00
|
|
|
}
|