leocad/common/lc_shortcuts.cpp

103 lines
2.1 KiB
C++
Raw Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
#include "lc_shortcuts.h"
#include "lc_profile.h"
lcKeyboardShortcuts gKeyboardShortcuts;
void lcLoadDefaultKeyboardShortcuts()
{
2015-09-27 09:02:57 +02:00
QByteArray Buffer = lcGetProfileBuffer(LC_PROFILE_SHORTCUTS);
2015-09-27 20:25:35 +02:00
QTextStream Stream(Buffer, QIODevice::ReadOnly);
2013-08-09 06:57:18 +02:00
2015-09-27 20:25:35 +02:00
if (Buffer.isEmpty() || !gKeyboardShortcuts.Load(Stream))
2015-09-27 09:02:57 +02:00
gKeyboardShortcuts.Reset();
2013-08-09 06:57:18 +02:00
}
void lcSaveDefaultKeyboardShortcuts()
{
2015-09-27 09:02:57 +02:00
QByteArray Buffer;
2015-09-27 20:25:35 +02:00
QTextStream Stream(&Buffer, QIODevice::WriteOnly);
2013-08-09 06:57:18 +02:00
2015-09-27 20:25:35 +02:00
gKeyboardShortcuts.Save(Stream);
2013-08-09 06:57:18 +02:00
2015-09-27 09:02:57 +02:00
lcSetProfileBuffer(LC_PROFILE_SHORTCUTS, Buffer);
2013-08-09 06:57:18 +02:00
}
void lcResetDefaultKeyboardShortcuts()
{
2015-09-27 09:02:57 +02:00
gKeyboardShortcuts.Reset();
2013-08-09 06:57:18 +02:00
lcRemoveProfileKey(LC_PROFILE_SHORTCUTS);
}
2015-09-27 09:02:57 +02:00
void lcKeyboardShortcuts::Reset()
2013-08-09 06:57:18 +02:00
{
for (int CommandIdx = 0; CommandIdx < LC_NUM_COMMANDS; CommandIdx++)
2015-09-27 09:02:57 +02:00
mShortcuts[CommandIdx] = qApp->translate("Shortcut", gCommands[CommandIdx].DefaultShortcut);
2013-08-09 06:57:18 +02:00
}
2015-09-27 09:02:57 +02:00
bool lcKeyboardShortcuts::Save(const QString& FileName)
2013-08-09 06:57:18 +02:00
{
2015-09-27 09:02:57 +02:00
QFile File(FileName);
2013-08-09 06:57:18 +02:00
2015-09-27 09:02:57 +02:00
if (!File.open(QIODevice::WriteOnly))
2013-08-09 06:57:18 +02:00
return false;
2015-09-27 20:25:35 +02:00
QTextStream Stream(&File);
return Save(Stream);
2013-08-09 06:57:18 +02:00
}
2015-09-27 09:02:57 +02:00
bool lcKeyboardShortcuts::Save(QTextStream& Stream)
2013-08-09 06:57:18 +02:00
{
for (int CommandIdx = 0; CommandIdx < LC_NUM_COMMANDS; CommandIdx++)
{
2015-09-27 09:02:57 +02:00
if (mShortcuts[CommandIdx].isEmpty())
2013-08-09 06:57:18 +02:00
continue;
2015-09-27 09:02:57 +02:00
Stream << gCommands[CommandIdx].ID << QLatin1String("=") << mShortcuts[CommandIdx] << QLatin1String("\n");
2013-08-09 06:57:18 +02:00
}
return true;
}
2015-09-27 09:02:57 +02:00
bool lcKeyboardShortcuts::Load(const QString& FileName)
2013-08-09 06:57:18 +02:00
{
2015-09-27 09:02:57 +02:00
QFile File(FileName);
2013-08-09 06:57:18 +02:00
2015-09-27 09:02:57 +02:00
if (!File.open(QIODevice::ReadOnly))
2013-08-09 06:57:18 +02:00
return false;
2015-09-27 20:25:35 +02:00
QTextStream Stream(&File);
return Load(Stream);
2013-08-09 06:57:18 +02:00
}
2015-09-27 09:02:57 +02:00
bool lcKeyboardShortcuts::Load(QTextStream& Stream)
2013-08-09 06:57:18 +02:00
{
for (int CommandIdx = 0; CommandIdx < LC_NUM_COMMANDS; CommandIdx++)
2015-09-27 09:02:57 +02:00
mShortcuts[CommandIdx].clear();
2013-08-09 06:57:18 +02:00
2015-09-27 09:02:57 +02:00
for (QString Line = Stream.readLine(); !Line.isNull(); Line = Stream.readLine())
2013-08-09 06:57:18 +02:00
{
2015-09-27 09:02:57 +02:00
int Equals = Line.indexOf('=');
2013-08-09 06:57:18 +02:00
2015-09-27 09:02:57 +02:00
if (Equals == -1)
2013-08-09 06:57:18 +02:00
continue;
2015-09-27 09:02:57 +02:00
QString Key = Line.left(Equals);
2013-08-09 06:57:18 +02:00
int CommandIdx;
for (CommandIdx = 0; CommandIdx < LC_NUM_COMMANDS; CommandIdx++)
2015-09-27 09:02:57 +02:00
if (gCommands[CommandIdx].ID == Key)
2013-08-09 06:57:18 +02:00
break;
if (CommandIdx == LC_NUM_COMMANDS)
continue;
2015-09-27 09:02:57 +02:00
mShortcuts[CommandIdx] = Line.mid(Equals + 1);
2013-08-09 06:57:18 +02:00
}
return true;
}