leocad/common/lc_shortcuts.cpp

242 lines
5.8 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;
2016-04-23 02:17:33 +02:00
lcMouseShortcuts gMouseShortcuts;
2013-08-09 06:57:18 +02:00
void lcLoadDefaultKeyboardShortcuts()
{
2016-04-23 02:17:33 +02:00
QByteArray Buffer = lcGetProfileBuffer(LC_PROFILE_KEYBOARD_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
2016-04-23 02:17:33 +02:00
lcSetProfileBuffer(LC_PROFILE_KEYBOARD_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
2016-04-23 02:17:33 +02:00
lcRemoveProfileKey(LC_PROFILE_KEYBOARD_SHORTCUTS);
}
void lcLoadDefaultMouseShortcuts()
{
QStringList Shortcuts = lcGetProfileStringList(LC_PROFILE_MOUSE_SHORTCUTS);
2016-04-23 02:17:33 +02:00
if (Shortcuts.isEmpty() || !gMouseShortcuts.Load(Shortcuts))
2016-04-23 02:17:33 +02:00
gMouseShortcuts.Reset();
2013-08-09 06:57:18 +02:00
}
2016-04-25 07:26:34 +02:00
void lcSaveDefaultMouseShortcuts()
{
QStringList Shortcuts;
2016-04-25 07:26:34 +02:00
gMouseShortcuts.Save(Shortcuts);
2016-04-25 07:26:34 +02:00
lcSetProfileStringList(LC_PROFILE_MOUSE_SHORTCUTS, Shortcuts);
2016-04-25 07:26:34 +02:00
}
void lcResetDefaultMouseShortcuts()
{
gMouseShortcuts.Reset();
lcRemoveProfileKey(LC_PROFILE_MOUSE_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
}
2016-02-17 00:11:52 +01:00
Stream.flush();
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;
}
2016-04-23 02:17:33 +02:00
void lcMouseShortcuts::Reset()
{
memset(mShortcuts, 0, sizeof(mShortcuts));
mShortcuts[LC_TOOL_ROTATE_VIEW].Modifiers1 = Qt::AltModifier;
mShortcuts[LC_TOOL_ROTATE_VIEW].Button1 = Qt::LeftButton;
mShortcuts[LC_TOOL_ROTATE_VIEW].Modifiers2 = Qt::NoModifier;
mShortcuts[LC_TOOL_ROTATE_VIEW].Button2 = Qt::RightButton;
2016-09-22 17:04:51 +02:00
#if (QT_VERSION >= QT_VERSION_CHECK(4, 7, 0))
mShortcuts[LC_TOOL_PAN].Modifiers1 = Qt::AltModifier;
mShortcuts[LC_TOOL_PAN].Button1 = Qt::MiddleButton;
mShortcuts[LC_TOOL_PAN].Modifiers2 = Qt::ShiftModifier;
mShortcuts[LC_TOOL_PAN].Button2 = Qt::RightButton;
#else
mShortcuts[LC_TOOL_PAN].Modifiers1 = Qt::ShiftModifier;
mShortcuts[LC_TOOL_PAN].Button1 = Qt::RightButton;
2016-09-22 17:04:51 +02:00
#endif
mShortcuts[LC_TOOL_ZOOM].Modifiers1 = Qt::AltModifier;
mShortcuts[LC_TOOL_ZOOM].Button1 = Qt::RightButton;
2016-04-23 02:17:33 +02:00
}
bool lcMouseShortcuts::Save(QStringList& Shortcuts)
2016-04-23 02:17:33 +02:00
{
Shortcuts.clear();
2016-04-23 02:17:33 +02:00
for (int ToolIdx = 0; ToolIdx < LC_NUM_TOOLS; ToolIdx++)
{
int ButtonIndex1 = 0;
for (int Button1 = mShortcuts[ToolIdx].Button1; Button1; Button1 >>= 1)
ButtonIndex1++;
2016-04-23 02:17:33 +02:00
if (!ButtonIndex1)
2016-04-23 02:17:33 +02:00
continue;
QString Shortcut = QKeySequence(mShortcuts[ToolIdx].Modifiers1 | (Qt::Key_0 + ButtonIndex1)).toString(QKeySequence::PortableText);
int ButtonIndex2 = 0;
for (int Button2 = mShortcuts[ToolIdx].Button2; Button2; Button2 >>= 1)
ButtonIndex2++;
if (ButtonIndex2)
Shortcut += ',' + QKeySequence(mShortcuts[ToolIdx].Modifiers2 | (Qt::Key_0 + ButtonIndex2)).toString(QKeySequence::PortableText);
2016-06-13 01:05:26 +02:00
Shortcuts << QString::fromLatin1(gToolNames[ToolIdx]) + QLatin1String("=") + Shortcut;
2016-04-23 02:17:33 +02:00
}
return true;
}
bool lcMouseShortcuts::Load(const QStringList& Shortcuts)
2016-04-23 02:17:33 +02:00
{
memset(mShortcuts, 0, sizeof(mShortcuts));
foreach (const QString& Shortcut, Shortcuts)
2016-04-23 02:17:33 +02:00
{
int Equals = Shortcut.indexOf('=');
2016-04-23 02:17:33 +02:00
if (Equals == -1)
continue;
QString Key = Shortcut.left(Equals);
2016-04-23 02:17:33 +02:00
int ToolIdx;
for (ToolIdx = 0; ToolIdx < LC_NUM_TOOLS; ToolIdx++)
2016-06-13 01:05:26 +02:00
if (Key == gToolNames[ToolIdx])
2016-04-23 02:17:33 +02:00
break;
if (ToolIdx == LC_NUM_TOOLS)
continue;
QStringList Shortcuts = Shortcut.mid(Equals + 1).split(',');
bool AddedShortcut = false;
for (const QString& Shortcut : Shortcuts)
{
QKeySequence KeySequence(Shortcut.mid(Equals + 1));
if (KeySequence.isEmpty())
continue;
int ShortcutKey = KeySequence[0];
Qt::KeyboardModifiers Modifiers = (Qt::KeyboardModifier)(ShortcutKey & Qt::KeyboardModifierMask);
Qt::MouseButton Button = (Qt::MouseButton)(1 << ((ShortcutKey & ~Qt::KeyboardModifierMask) - Qt::Key_0 - 1));
if (!AddedShortcut)
{
mShortcuts[ToolIdx].Modifiers1 = Modifiers;
mShortcuts[ToolIdx].Button1 = Button;
AddedShortcut = true;
}
else
{
mShortcuts[ToolIdx].Modifiers2 = Modifiers;
mShortcuts[ToolIdx].Button2 = Button;
}
}
2016-04-23 02:17:33 +02:00
}
return true;
}
lcTool lcMouseShortcuts::GetTool(Qt::MouseButton Button, Qt::KeyboardModifiers Modifiers) const
{
for (int ToolIdx = 0; ToolIdx < LC_NUM_TOOLS; ToolIdx++)
if ((mShortcuts[ToolIdx].Button1 == Button && mShortcuts[ToolIdx].Modifiers1 == Modifiers) || (mShortcuts[ToolIdx].Button2 == Button && mShortcuts[ToolIdx].Modifiers2 == Modifiers))
2016-04-23 02:17:33 +02:00
return (lcTool)ToolIdx;
return LC_NUM_TOOLS;
}