Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / plugins / coreplugin / actionmanager / commandsfile.cpp
blob9554a817b6ac764b488fb11efe1b1abc76d32b8a
1 /**
2 ******************************************************************************
4 * @file commandsfile.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @addtogroup GCSPlugins GCS Plugins
8 * @{
9 * @addtogroup CorePlugin Core Plugin
10 * @{
11 * @brief The Core GCS plugin
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "commandsfile.h"
30 #include "shortcutsettings.h"
31 #include "command_p.h"
33 #include <coreplugin/uniqueidmanager.h>
35 #include <QtCore/QFile>
36 #include <QtXml/QDomDocument>
38 using namespace Core;
39 using namespace Core::Internal;
41 /*!
42 \class CommandsFile
43 \brief The CommandsFile class provides a collection of import and export commands.
44 \inheaderfile commandsfile.h
47 /*!
48 ...
50 CommandsFile::CommandsFile(const QString &filename)
51 : m_filename(filename)
54 /*!
55 ...
57 QMap<QString, QKeySequence> CommandsFile::importCommands() const
59 QMap<QString, QKeySequence> result;
61 QFile file(m_filename);
62 if (!file.open(QIODevice::ReadOnly)) {
63 return result;
66 QDomDocument doc("KeyboardMappingScheme");
67 if (!doc.setContent(&file)) {
68 return result;
71 QDomElement root = doc.documentElement();
72 if (root.nodeName() != QLatin1String("mapping")) {
73 return result;
76 QDomElement ks = root.firstChildElement();
77 for (; !ks.isNull(); ks = ks.nextSiblingElement()) {
78 if (ks.nodeName() == QLatin1String("shortcut")) {
79 QString id = ks.attribute(QLatin1String("id"));
80 QKeySequence shortcutkey;
81 QDomElement keyelem = ks.firstChildElement("key");
82 if (!keyelem.isNull()) {
83 shortcutkey = QKeySequence(keyelem.attribute("value"));
85 result.insert(id, shortcutkey);
89 file.close();
90 return result;
93 /*!
94 ...
96 bool CommandsFile::exportCommands(const QList<ShortcutItem *> &items)
98 UniqueIDManager *idmanager = UniqueIDManager::instance();
100 QFile file(m_filename);
102 if (!file.open(QIODevice::WriteOnly)) {
103 return false;
106 QDomDocument doc("KeyboardMappingScheme");
107 QDomElement root = doc.createElement("mapping");
108 doc.appendChild(root);
110 foreach(const ShortcutItem * item, items) {
111 QDomElement ctag = doc.createElement("shortcut");
113 ctag.setAttribute(QLatin1String("id"), idmanager->stringForUniqueIdentifier(item->m_cmd->id()));
114 root.appendChild(ctag);
116 QDomElement ktag = doc.createElement("key");
117 ktag.setAttribute(QLatin1String("value"), item->m_key.toString());
118 ctag.appendChild(ktag);
121 file.write(doc.toByteArray());
122 file.close();
123 return true;