Move the ItemType enum out of GuiPref and into KeyMap, where it belongs.

This is preparation for multi-LFUN bindings.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26503 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Richard Heck 2008-09-23 13:53:38 +00:00
parent 31e5a15695
commit ad0ecf86a7
4 changed files with 47 additions and 46 deletions

View File

@ -449,7 +449,7 @@ KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
} }
KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const KeyMap::BindingList KeyMap::listBindings(bool unbound, KeyMap::ItemType tag) const
{ {
BindingList list; BindingList list;
listBindings(list, KeySequence(0, 0), tag); listBindings(list, KeySequence(0, 0), tag);
@ -475,7 +475,7 @@ KeyMap::BindingList KeyMap::listBindings(bool unbound, int tag) const
void KeyMap::listBindings(BindingList & list, void KeyMap::listBindings(BindingList & list,
KeySequence const & prefix, int tag) const KeySequence const & prefix, KeyMap::ItemType tag) const
{ {
Table::const_iterator it = table.begin(); Table::const_iterator it = table.begin();
Table::const_iterator it_end = table.end(); Table::const_iterator it_end = table.end();

View File

@ -29,6 +29,14 @@ namespace lyx {
/// Defines key maps and actions for key sequences /// Defines key maps and actions for key sequences
class KeyMap { class KeyMap {
public: public:
enum ItemType {
System, //< loaded from a bind file
UserBind, //< \bind loaded from user.bind
UserUnbind, //< \unbind loaded from user.bind, with corresponding
//< entry in system bind file
UserExtraUnbind //< \unbind loaded from user.bind, without
//< corresponding entry in system bind file.
};
/** /**
* Bind/Unbind a key sequence to an action. * Bind/Unbind a key sequence to an action.
* @return 0 on success, or position in string seq where error * @return 0 on success, or position in string seq where error
@ -70,7 +78,7 @@ public:
* @param unbind use \unbind instead of \bind, indicating this KeyMap * @param unbind use \unbind instead of \bind, indicating this KeyMap
* actually record unbind maps. * actually record unbind maps.
*/ */
void write(std::string const & bind_file, bool append, bool unbind=false) const; void write(std::string const & bind_file, bool append, bool unbind = false) const;
/** /**
* print all available keysyms * print all available keysyms
@ -87,7 +95,7 @@ public:
* @return the action / LFUN_COMMAND_PREFIX / LFUN_UNKNOWN_ACTION * @return the action / LFUN_COMMAND_PREFIX / LFUN_UNKNOWN_ACTION
*/ */
FuncRequest const & FuncRequest const &
lookup(KeySymbol const & key, KeyModifier mod, KeySequence * seq) const; lookup(KeySymbol const & key, KeyModifier mod, KeySequence * seq) const;
/// ///
typedef std::vector<KeySequence> Bindings; typedef std::vector<KeySequence> Bindings;
@ -99,11 +107,11 @@ public:
docstring printBindings(FuncRequest const & func) const; docstring printBindings(FuncRequest const & func) const;
struct Binding { struct Binding {
Binding(FuncRequest const & r, KeySequence const & s, int t) Binding(FuncRequest const & r, KeySequence const & s, ItemType t)
: request(r), sequence(s), tag(t) {} : request(r), sequence(s), tag(t) {}
FuncRequest request; FuncRequest request;
KeySequence sequence; KeySequence sequence;
int tag; KeyMap::ItemType tag;
}; };
typedef std::vector<Binding> BindingList; typedef std::vector<Binding> BindingList;
/** /**
@ -111,7 +119,7 @@ public:
* @param unbound list unbound (func without any keybinding) as well * @param unbound list unbound (func without any keybinding) as well
* @param tag an optional tag to indicate the source of the bindinglist * @param tag an optional tag to indicate the source of the bindinglist
*/ */
BindingList listBindings(bool unbound, int tag = 0) const; BindingList listBindings(bool unbound, ItemType tag = System) const;
/** /**
* Given an action, find the first 1-key binding (if it exists). * Given an action, find the first 1-key binding (if it exists).
@ -119,7 +127,7 @@ public:
* [only used by the Qt/Mac frontend] * [only used by the Qt/Mac frontend]
*/ */
std::pair<KeySymbol, KeyModifier> std::pair<KeySymbol, KeyModifier>
find1keybinding(FuncRequest const & func) const; find1keybinding(FuncRequest const & func) const;
/** /**
* Returns a string of the given keysym, with modifiers. * Returns a string of the given keysym, with modifiers.
@ -151,10 +159,10 @@ private:
* @param prefix a sequence to prepend the results * @param prefix a sequence to prepend the results
*/ */
Bindings findBindings(FuncRequest const & func, Bindings findBindings(FuncRequest const & func,
KeySequence const & prefix) const; KeySequence const & prefix) const;
void listBindings(BindingList & list, KeySequence const & prefix, void listBindings(BindingList & list, KeySequence const & prefix,
int tag) const; ItemType tag) const;
/// is the table empty ? /// is the table empty ?
bool empty() const { return table.empty(); } bool empty() const { return table.empty(); }

View File

@ -2141,11 +2141,11 @@ void PrefShortcuts::updateShortcutsTW()
// listBindings(unbound=true) lists all bound and unbound lfuns // listBindings(unbound=true) lists all bound and unbound lfuns
// Items in this list is tagged by its source. // Items in this list is tagged by its source.
KeyMap::BindingList bindinglist = system_bind_.listBindings(true, KeyMap::BindingList bindinglist = system_bind_.listBindings(true,
static_cast<int>(System)); KeyMap::System);
KeyMap::BindingList user_bindinglist = user_bind_.listBindings(false, KeyMap::BindingList user_bindinglist = user_bind_.listBindings(false,
static_cast<int>(UserBind)); KeyMap::UserBind);
KeyMap::BindingList user_unbindinglist = user_unbind_.listBindings(false, KeyMap::BindingList user_unbindinglist = user_unbind_.listBindings(false,
static_cast<int>(UserUnbind)); KeyMap::UserUnbind);
bindinglist.insert(bindinglist.end(), user_bindinglist.begin(), bindinglist.insert(bindinglist.end(), user_bindinglist.begin(),
user_bindinglist.end()); user_bindinglist.end());
bindinglist.insert(bindinglist.end(), user_unbindinglist.begin(), bindinglist.insert(bindinglist.end(), user_unbindinglist.begin(),
@ -2154,7 +2154,7 @@ void PrefShortcuts::updateShortcutsTW()
KeyMap::BindingList::const_iterator it = bindinglist.begin(); KeyMap::BindingList::const_iterator it = bindinglist.begin();
KeyMap::BindingList::const_iterator it_end = bindinglist.end(); KeyMap::BindingList::const_iterator it_end = bindinglist.end();
for (; it != it_end; ++it) for (; it != it_end; ++it)
insertShortcutItem(it->request, it->sequence, ItemType(it->tag)); insertShortcutItem(it->request, it->sequence, KeyMap::ItemType(it->tag));
shortcutsTW->sortItems(0, Qt::AscendingOrder); shortcutsTW->sortItems(0, Qt::AscendingOrder);
QList<QTreeWidgetItem*> items = shortcutsTW->selectedItems(); QList<QTreeWidgetItem*> items = shortcutsTW->selectedItems();
@ -2165,22 +2165,22 @@ void PrefShortcuts::updateShortcutsTW()
} }
void PrefShortcuts::setItemType(QTreeWidgetItem * item, ItemType tag) void PrefShortcuts::setItemType(QTreeWidgetItem * item, KeyMap::ItemType tag)
{ {
item->setData(0, Qt::UserRole, QVariant(tag)); item->setData(0, Qt::UserRole, QVariant(tag));
QFont font; QFont font;
switch (tag) { switch (tag) {
case System: case KeyMap::System:
break; break;
case UserBind: case KeyMap::UserBind:
font.setBold(true); font.setBold(true);
break; break;
case UserUnbind: case KeyMap::UserUnbind:
font.setStrikeOut(true); font.setStrikeOut(true);
break; break;
// this item is not displayed now. // this item is not displayed now.
case UserExtraUnbind: case KeyMap::UserExtraUnbind:
font.setStrikeOut(true); font.setStrikeOut(true);
break; break;
} }
@ -2190,18 +2190,18 @@ void PrefShortcuts::setItemType(QTreeWidgetItem * item, ItemType tag)
QTreeWidgetItem * PrefShortcuts::insertShortcutItem(FuncRequest const & lfun, QTreeWidgetItem * PrefShortcuts::insertShortcutItem(FuncRequest const & lfun,
KeySequence const & seq, ItemType tag) KeySequence const & seq, KeyMap::ItemType tag)
{ {
FuncCode action = lfun.action; FuncCode action = lfun.action;
string const action_name = lyxaction.getActionName(action); string const action_name = lyxaction.getActionName(action);
QString const lfun_name = toqstr(from_utf8(action_name) QString const lfun_name = toqstr(from_utf8(action_name)
+ ' ' + lfun.argument()); + ' ' + lfun.argument());
QString const shortcut = toqstr(seq.print(KeySequence::ForGui)); QString const shortcut = toqstr(seq.print(KeySequence::ForGui));
ItemType item_tag = tag; KeyMap::ItemType item_tag = tag;
QTreeWidgetItem * newItem = 0; QTreeWidgetItem * newItem = 0;
// for unbind items, try to find an existing item in the system bind list // for unbind items, try to find an existing item in the system bind list
if (tag == UserUnbind) { if (tag == KeyMap::UserUnbind) {
QList<QTreeWidgetItem*> const items = shortcutsTW->findItems(lfun_name, QList<QTreeWidgetItem*> const items = shortcutsTW->findItems(lfun_name,
Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive), 0); Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive), 0);
for (int i = 0; i < items.size(); ++i) { for (int i = 0; i < items.size(); ++i) {
@ -2209,11 +2209,11 @@ QTreeWidgetItem * PrefShortcuts::insertShortcutItem(FuncRequest const & lfun,
newItem = items[i]; newItem = items[i];
break; break;
} }
// if not found, this unbind item is UserExtraUnbind // if not found, this unbind item is KeyMap::UserExtraUnbind
// Such an item is not displayed to avoid confusion (what is // Such an item is not displayed to avoid confusion (what is
// unmatched removed?). // unmatched removed?).
if (!newItem) { if (!newItem) {
item_tag = UserExtraUnbind; item_tag = KeyMap::UserExtraUnbind;
return 0; return 0;
} }
} }
@ -2259,8 +2259,9 @@ void PrefShortcuts::on_shortcutsTW_itemSelectionChanged()
if (items.isEmpty()) if (items.isEmpty())
return; return;
ItemType tag = static_cast<ItemType>(items[0]->data(0, Qt::UserRole).toInt()); KeyMap::ItemType tag =
if (tag == UserUnbind) static_cast<KeyMap::ItemType>(items[0]->data(0, Qt::UserRole).toInt());
if (tag == KeyMap::UserUnbind)
removePB->setText(qt_("Res&tore")); removePB->setText(qt_("Res&tore"));
else else
removePB->setText(qt_("Remo&ve")); removePB->setText(qt_("Remo&ve"));
@ -2298,18 +2299,19 @@ void PrefShortcuts::removeShortcut()
string shortcut = fromqstr(items[i]->data(1, Qt::UserRole).toString()); string shortcut = fromqstr(items[i]->data(1, Qt::UserRole).toString());
string lfun = fromqstr(items[i]->text(0)); string lfun = fromqstr(items[i]->text(0));
FuncRequest func = lyxaction.lookupFunc(lfun); FuncRequest func = lyxaction.lookupFunc(lfun);
ItemType tag = static_cast<ItemType>(items[i]->data(0, Qt::UserRole).toInt()); KeyMap::ItemType tag =
static_cast<KeyMap::ItemType>(items[i]->data(0, Qt::UserRole).toInt());
switch (tag) { switch (tag) {
case System: { case KeyMap::System: {
// for system bind, we do not touch the item // for system bind, we do not touch the item
// but add an user unbind item // but add an user unbind item
user_unbind_.bind(shortcut, func); user_unbind_.bind(shortcut, func);
setItemType(items[i], UserUnbind); setItemType(items[i], KeyMap::UserUnbind);
removePB->setText(qt_("Res&tore")); removePB->setText(qt_("Res&tore"));
break; break;
} }
case UserBind: { case KeyMap::UserBind: {
// for user_bind, we remove this bind // for user_bind, we remove this bind
QTreeWidgetItem * parent = items[i]->parent(); QTreeWidgetItem * parent = items[i]->parent();
int itemIdx = parent->indexOfChild(items[i]); int itemIdx = parent->indexOfChild(items[i]);
@ -2321,15 +2323,15 @@ void PrefShortcuts::removeShortcut()
user_bind_.unbind(shortcut, func); user_bind_.unbind(shortcut, func);
break; break;
} }
case UserUnbind: { case KeyMap::UserUnbind: {
// for user_unbind, we remove the unbind, and the item // for user_unbind, we remove the unbind, and the item
// become System again. // become KeyMap::System again.
user_unbind_.unbind(shortcut, func); user_unbind_.unbind(shortcut, func);
setItemType(items[i], System); setItemType(items[i], KeyMap::System);
removePB->setText(qt_("Remo&ve")); removePB->setText(qt_("Remo&ve"));
break; break;
} }
case UserExtraUnbind: { case KeyMap::UserExtraUnbind: {
// for user unbind that is not in system bind file, // for user unbind that is not in system bind file,
// remove this unbind file // remove this unbind file
QTreeWidgetItem * parent = items[i]->parent(); QTreeWidgetItem * parent = items[i]->parent();
@ -2431,7 +2433,7 @@ void PrefShortcuts::shortcut_okPB_pressed()
// so remove the previous one // so remove the previous one
removeShortcut(); removeShortcut();
QTreeWidgetItem * item = insertShortcutItem(func, k, UserBind); QTreeWidgetItem * item = insertShortcutItem(func, k, KeyMap::UserBind);
if (item) { if (item) {
user_bind_.bind(&k, func); user_bind_.bind(&k, func);
shortcutsTW->sortItems(0, Qt::AscendingOrder); shortcutsTW->sortItems(0, Qt::AscendingOrder);

View File

@ -421,15 +421,6 @@ public:
class PrefShortcuts : public PrefModule, public Ui::PrefShortcuts class PrefShortcuts : public PrefModule, public Ui::PrefShortcuts
{ {
Q_OBJECT Q_OBJECT
private:
enum ItemType {
System, //< loaded from a bind file
UserBind, //< \bind loaded from user.bind
UserUnbind, //< \unbind loaded from user.bind, with corresponding
//< entry in system bind file
UserExtraUnbind //< \unbind loaded from user.bind, without
//< corresponding entry in system bind file.
};
public: public:
PrefShortcuts(GuiPreferences * form); PrefShortcuts(GuiPreferences * form);
@ -439,9 +430,9 @@ public:
void modifyShortcut(); void modifyShortcut();
void removeShortcut(); void removeShortcut();
/// ///
void setItemType(QTreeWidgetItem * item, ItemType tag); void setItemType(QTreeWidgetItem * item, KeyMap::ItemType tag);
QTreeWidgetItem * insertShortcutItem(FuncRequest const & lfun, QTreeWidgetItem * insertShortcutItem(FuncRequest const & lfun,
KeySequence const & shortcut, ItemType tag); KeySequence const & shortcut, KeyMap::ItemType tag);
public Q_SLOTS: public Q_SLOTS:
void select_bind(); void select_bind();