LibreOffice Module framework (master) 1
acceleratorcache.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
21
22#include <com/sun/star/container/NoSuchElementException.hpp>
23
24#include <vcl/svapp.hxx>
25
26namespace framework
27{
28bool AcceleratorCache::hasKey(const css::awt::KeyEvent& aKey) const
29{
30 return (m_lKey2Commands.find(aKey) != m_lKey2Commands.end());
31}
32
33bool AcceleratorCache::hasCommand(const OUString& sCommand) const
34{
35 return (m_lCommand2Keys.find(sCommand) != m_lCommand2Keys.end());
36}
37
39{
40 TKeyList lKeys;
41 lKeys.reserve(m_lKey2Commands.size());
42
43 for (auto const& key2Command : m_lKey2Commands)
44 {
45 lKeys.push_back(key2Command.first);
46 }
47
48 return lKeys;
49}
50
51void AcceleratorCache::setKeyCommandPair(const css::awt::KeyEvent& aKey, const OUString& sCommand)
52{
53 // register command for the specified key
54 m_lKey2Commands[aKey] = sCommand;
55
56 // update optimized structure to bind multiple keys to one command
57 TKeyList& rKeyList = m_lCommand2Keys[sCommand];
58 rKeyList.push_back(aKey);
59}
60
62{
63 TCommand2Keys::const_iterator pCommand = m_lCommand2Keys.find(sCommand);
64 if (pCommand == m_lCommand2Keys.end())
65 throw css::container::NoSuchElementException();
66 return pCommand->second;
67}
68
69OUString AcceleratorCache::getCommandByKey(const css::awt::KeyEvent& aKey) const
70{
71 TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey);
72 if (pKey == m_lKey2Commands.end())
73 throw css::container::NoSuchElementException();
74 return pKey->second;
75}
76
77void AcceleratorCache::removeKey(const css::awt::KeyEvent& aKey)
78{
79 // check if key exists
80 TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey);
81 if (pKey == m_lKey2Commands.end())
82 return;
83
84 // get its registered command
85 // Because we must know its place inside the optimized
86 // structure, which bind keys to commands, too!
87 OUString sCommand = pKey->second;
88 pKey = m_lKey2Commands.end(); // nobody should use an undefined value .-)
89
90 // remove key from primary list
91 m_lKey2Commands.erase(aKey);
92
93 // get keylist for that command
94 TCommand2Keys::iterator pCommand = m_lCommand2Keys.find(sCommand);
95 if (pCommand == m_lCommand2Keys.end())
96 return;
97 TKeyList& lKeys = pCommand->second;
98
99 // one or more keys assign
100 if (lKeys.size() == 1)
101 // remove key from optimized command list
102 m_lCommand2Keys.erase(sCommand);
103 else // only remove this key from the keylist
104 {
105 auto pKeys = ::std::find(lKeys.begin(), lKeys.end(), aKey);
106
107 if (pKeys != lKeys.end())
108 lKeys.erase(pKeys);
109 }
110}
111
112void AcceleratorCache::removeCommand(const OUString& sCommand)
113{
114 const TKeyList& lKeys = getKeysByCommand(sCommand);
115 for (auto const& lKey : lKeys)
116 {
117 removeKey(lKey);
118 }
119}
120
121} // namespace framework
122
123/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
TKey2Commands m_lKey2Commands
map keys to commands in relation 1:1.
TKeyList getKeysByCommand(const OUString &sCommand) const
returns the list of keys, which are registered for this command.
void setKeyCommandPair(const css::awt::KeyEvent &aKey, const OUString &sCommand)
add a new or change an existing key-command pair of this container.
::std::vector< css::awt::KeyEvent > TKeyList
commands -> keys
void removeCommand(const OUString &sCommand)
bool hasCommand(const OUString &sCommand) const
OUString getCommandByKey(const css::awt::KeyEvent &aKey) const
void removeKey(const css::awt::KeyEvent &aKey)
bool hasKey(const css::awt::KeyEvent &aKey) const
checks if the specified key exists.
TCommand2Keys m_lCommand2Keys
map commands to keys in relation 1:n.