1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#include <BALL/VIEW/KERNEL/shortcutRegistry.h>
#include <BALL/SYSTEM/file.h>
#include <BALL/FORMAT/lineBasedFile.h>
#include <BALL/VIEW/KERNEL/common.h>
#include <QtWidgets/QAction>
namespace BALL
{
namespace VIEW
{
const char* ShortcutRegistry::BETWEEN_SC_SEPERATOR = "?";
const char* ShortcutRegistry::IN_SC_SEPERATOR = "$";
ShortcutRegistry::ShortcutRegistry()
{
setObjectName("ShortcutRegistry");
registerThis();
}
ShortcutRegistry::~ShortcutRegistry()
{
unregisterThis();
}
void ShortcutRegistry::registerShortcut(String description, QAction* shortcut)
{
// eat white spaces and tabs in the description
while (description.substitute(" ", "") != String::EndPos) {};
while (description.substitute("\t", "") != String::EndPos) {};
while (description.substitute("\r", "") != String::EndPos) {};
while (description.substitute("\n", "") != String::EndPos) {};
String key_seq;
std::map<String, String>::iterator res = unknown_shortcuts_.find(description);
if (res != unknown_shortcuts_.end())
{
shortcut->setShortcut(QKeySequence(QString(res->second.c_str())));
key_seq = res->second;
}
else
{
key_seq = ascii(shortcut->shortcut().toString());
}
if (hasDescription(description))
{
Log.warn() << "Double shortcut entry " << description << std::endl;
return;
}
shortcuts_[description] = shortcut;
if (shortcut_keys_.has(key_seq))
{
Log.warn() << "Double shortcut entry " << key_seq << std::endl;
}
else if (key_seq != "")
{
shortcut_keys_.insert(key_seq);
}
Q_EMIT shortcutChanged();
}
void ShortcutRegistry::clear()
{
shortcuts_.clear();
shortcut_keys_.clear();
Q_EMIT shortcutChanged();
}
void ShortcutRegistry::clearKeySequences()
{
std::map<String, QAction*>::iterator it;
for (it = shortcuts_.begin(); it != shortcuts_.end(); ++it)
{
it->second->setShortcut(QKeySequence());
}
shortcut_keys_.clear();
Q_EMIT shortcutChanged();
}
bool ShortcutRegistry::readShortcutsFromFile(const String& filename)
{
LineBasedFile infile;
try
{
infile.open(filename, std::ios::in);
}
catch(Exception::FileNotFound&)
{
Log.error() << "ShortcutRegistry: Could not open file " << filename << "." << std::endl;
return false;
}
infile.enableTrimWhitespaces(true);
while (infile.readLine())
{
std::vector<String> fields;
infile.getLine().split(fields);
if (fields.size() != 2)
continue; // no shortcut given
if (!hasDescription(fields[0]))
{
Log.warn() << "ShortcutRegistry: no action associated with description " << fields[0] << std::endl;
}
else // we have this action
{
QAction* action = shortcuts_[fields[0]];
// in kiosk mode, the action might be 0
if (!action)
continue;
if (!action->parent())
{
Log.error() << "ShortcutRegistry: action associated with description " << fields[0] << " has no parent! Ignoring!" << std::endl;
}
else
{
if (!shortcut_keys_.has(fields[1]))
{
action->setShortcut(QKeySequence(fields[1].c_str()));
shortcut_keys_.insert(fields[1]);
}
else
{
Log.warn() << "ShortcutRegistry: duplicate shortcut " << fields[1] << ". Ignoring!" << std::endl;
}
}
}
}
infile.close();
Q_EMIT shortcutChanged();
return true;
}
bool ShortcutRegistry::writeShortcutsToFile(const String& filename)
{
File outfile;
try
{
outfile.open(filename, std::ios::out);
}
catch (Exception::FileNotFound& e) // todo: explain what went wrong
{
Log.error() << "ShortcutRegistry: " << e.what() << std::endl;
return false;
}
std::map<String,QAction*>::const_iterator it;
for (it=shortcuts_.begin(); it!=shortcuts_.end(); ++it)
{
outfile << it->first << "\t" << ascii(it->second->shortcut().toString()) << std::endl;
}
outfile.close();
return true;
}
bool ShortcutRegistry::changeShortcut(QAction* shortcut, const String& new_sequence)
{
if(!shortcut) {
throw Exception::NullPointer(__FILE__, __LINE__);
}
if(hasKey(new_sequence)) {
return false;
}
if(shortcut->shortcut() != QKeySequence()) {
shortcut_keys_.erase(ascii(shortcut->shortcut().toString()));
}
QKeySequence seq(new_sequence.c_str());
//If new_sequence contained garbage we failed setting a shortcut
if((new_sequence != "") && (seq == QKeySequence())) {
return false;
}
shortcut_keys_.insert(ascii(seq.toString()));
shortcut->setShortcut(seq);
return true;
}
bool ShortcutRegistry::changeShortcut(int index, const String& new_sequence)
{
return changeShortcut(getEntry_(index).second, new_sequence);
}
size_t ShortcutRegistry::size()
{
return shortcuts_.size();
}
bool ShortcutRegistry::hasDescription(const String& description)
{
return (shortcuts_.find(description) != shortcuts_.end());
}
bool ShortcutRegistry::hasKey(const QString& key_seq)
{
return shortcut_keys_.has(ascii(key_seq));
}
bool ShortcutRegistry::hasKey(const QKeySequence& key_seq)
{
return hasKey(key_seq.toString());
}
bool ShortcutRegistry::hasKey(const String& key_seq)
{
return shortcut_keys_.has(key_seq);
}
std::pair<String, QAction*> ShortcutRegistry::operator[](Index i)
{
return getEntry_(i);
}
bool ShortcutRegistry::getValue(String& value) const
{
std::map<String,QAction*>::const_iterator it = shortcuts_.begin();
for (; it!=shortcuts_.end() && (!it->second || it->second->shortcut().isEmpty()); ++it) ;
if (it == shortcuts_.end())
return true;
value += ascii(QByteArray(it->first.c_str()).toPercentEncoding());
value += IN_SC_SEPERATOR;
value += ascii(it->second->shortcut().toString().toUtf8().toPercentEncoding());
for (++it; it != shortcuts_.end(); ++it)
{
if (!it->second || it->second->shortcut().isEmpty())
{
continue;
}
value += BETWEEN_SC_SEPERATOR;
value += ascii(QByteArray(it->first.c_str()).toPercentEncoding());
value += IN_SC_SEPERATOR;
value += ascii(it->second->shortcut().toString().toLatin1().toPercentEncoding());
}
return true;
}
bool ShortcutRegistry::setValue(const String& value)
{
std::vector<String> shortcuts;
String tmp(QByteArray::fromPercentEncoding(QByteArray(value.c_str())).data());
tmp.split(shortcuts, BETWEEN_SC_SEPERATOR, 0);
for (size_t i = 0; i < shortcuts.size(); ++i) {
String sc[2];
shortcuts[i].split(sc, 2, IN_SC_SEPERATOR);
//If no shortcut has been set, we do not need to take it into consideration
if (sc[1] == "")
{
continue;
}
//Update the currently known shortcuts
std::map<String, QAction*>::iterator res = shortcuts_.find(sc[0]);
if (res != shortcuts_.end())
{
changeShortcut(res->second, sc[1]);
}
else
{
//Should unknown shortcuts block existing shortcuts?
//I don't think so, let's see how it works out...
unknown_shortcuts_.insert(std::make_pair(sc[0], sc[1]));
}
}
return true;
}
std::pair<String, QAction*> ShortcutRegistry::getEntry_(Index pos)
{
if ((size_t)pos >= shortcuts_.size()) {
throw Exception::OutOfRange(__FILE__, __LINE__);
}
std::map<String, QAction*>::iterator it = shortcuts_.begin();
for(int i = 0; i < pos; ++i, ++it) ;
return *it;
}
} // namespace VIEW
} // namespace BALL
|