leocad/common/lc_shortcuts.cpp

277 lines
6.4 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++)
mShortcuts[CommandIdx] = 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));
2020-12-05 00:38:49 +01:00
lcToolShortcut& RotateViewShortcut = mShortcuts[static_cast<int>(lcTool::RotateView)];
RotateViewShortcut.Modifiers1 = Qt::AltModifier;
RotateViewShortcut.Button1 = Qt::LeftButton;
RotateViewShortcut.Modifiers2 = Qt::NoModifier;
RotateViewShortcut.Button2 = Qt::RightButton;
2020-12-05 00:38:49 +01:00
lcToolShortcut& PanShortcut = mShortcuts[static_cast<int>(lcTool::Pan)];
PanShortcut.Modifiers1 = Qt::AltModifier;
PanShortcut.Button1 = Qt::MiddleButton;
PanShortcut.Modifiers2 = Qt::ShiftModifier;
PanShortcut.Button2 = Qt::RightButton;
2020-12-05 00:38:49 +01:00
lcToolShortcut& ZoomShortcut = mShortcuts[static_cast<int>(lcTool::Zoom)];
ZoomShortcut.Modifiers1 = Qt::AltModifier;
ZoomShortcut.Button1 = Qt::RightButton;
2016-04-23 02:17:33 +02:00
}
bool lcMouseShortcuts::Save(const QString& FileName)
{
QStringList Shortcuts;
if (!Save(Shortcuts))
return false;
QFile File(FileName);
if (!File.open(QIODevice::WriteOnly))
return false;
QTextStream Stream(&File);
for (const QString& Shortcut : Shortcuts)
Stream << Shortcut << QLatin1String("\n");
Stream.flush();
return true;
}
bool lcMouseShortcuts::Save(QStringList& Shortcuts)
2016-04-23 02:17:33 +02:00
{
Shortcuts.clear();
2020-12-05 00:38:49 +01:00
for (int ToolIdx = 0; ToolIdx < static_cast<int>(lcTool::Count); ToolIdx++)
2016-04-23 02:17:33 +02:00
{
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 QString& FileName)
{
QFile File(FileName);
if (!File.open(QIODevice::ReadOnly))
return false;
QTextStream Stream(&File);
QStringList Lines;
while (!Stream.atEnd())
Lines += Stream.readLine();
return Load(Lines);
}
2019-05-18 20:33:27 +02:00
bool lcMouseShortcuts::Load(const QStringList& FullShortcuts)
2016-04-23 02:17:33 +02:00
{
memset(mShortcuts, 0, sizeof(mShortcuts));
2019-05-18 20:33:27 +02:00
for (const QString& FullShortcut : FullShortcuts)
2016-04-23 02:17:33 +02:00
{
2019-05-18 20:33:27 +02:00
int Equals = FullShortcut.indexOf('=');
2016-04-23 02:17:33 +02:00
if (Equals == -1)
continue;
2019-05-18 20:33:27 +02:00
QString Key = FullShortcut.left(Equals);
2016-04-23 02:17:33 +02:00
int ToolIdx;
2020-12-05 00:38:49 +01:00
for (ToolIdx = 0; ToolIdx < static_cast<int>(lcTool::Count); ToolIdx++)
2016-06-13 01:05:26 +02:00
if (Key == gToolNames[ToolIdx])
2016-04-23 02:17:33 +02:00
break;
2020-12-05 00:38:49 +01:00
if (ToolIdx == static_cast<int>(lcTool::Count))
2016-04-23 02:17:33 +02:00
continue;
2019-05-18 20:33:27 +02:00
QStringList Shortcuts = FullShortcut.mid(Equals + 1).split(',');
bool AddedShortcut = false;
for (const QString& Shortcut : Shortcuts)
{
QKeySequence KeySequence(Shortcut);
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
{
2020-12-05 00:38:49 +01:00
for (int ToolIdx = 0; ToolIdx < static_cast<int>(lcTool::Count); 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;
2020-12-05 00:38:49 +01:00
return lcTool::Count;
2016-04-23 02:17:33 +02:00
}