mirror of
https://github.com/leozide/leocad.git
synced 2026-07-27 19:56:49 +00:00
Keyboard shortcuts cleanup.
This commit is contained in:
@@ -1644,7 +1644,7 @@ void lcMainWindow::UpdateRecentFiles()
|
||||
void lcMainWindow::UpdateShortcuts()
|
||||
{
|
||||
for (int ActionIdx = 0; ActionIdx < LC_NUM_COMMANDS; ActionIdx++)
|
||||
mActions[ActionIdx]->setShortcut(QKeySequence(gKeyboardShortcuts.Shortcuts[ActionIdx]));
|
||||
mActions[ActionIdx]->setShortcut(QKeySequence(gKeyboardShortcuts.mShortcuts[ActionIdx]));
|
||||
}
|
||||
|
||||
void lcMainWindow::NewProject()
|
||||
|
||||
+22
-2
@@ -157,7 +157,17 @@ QString lcGetProfileString(LC_PROFILE_KEY Key)
|
||||
return Settings.value(QString("%1/%2").arg(Entry.mSection, Entry.mKey), Entry.mDefault.StringValue).toString();
|
||||
}
|
||||
|
||||
void lcGetProfileBuffer(LC_PROFILE_KEY Key, lcMemFile& Buffer)
|
||||
QByteArray lcGetProfileBuffer(LC_PROFILE_KEY Key)
|
||||
{
|
||||
lcProfileEntry& Entry = gProfileEntries[Key];
|
||||
QSettings Settings;
|
||||
|
||||
LC_ASSERT(Entry.mType == LC_PROFILE_ENTRY_BUFFER);
|
||||
|
||||
return Settings.value(QString("%1/%2").arg(Entry.mSection, Entry.mKey)).toByteArray();
|
||||
}
|
||||
|
||||
void lcGetProfileBuffer(LC_PROFILE_KEY Key, lcMemFile& Buffer) // todo: deprecated
|
||||
{
|
||||
lcProfileEntry& Entry = gProfileEntries[Key];
|
||||
QSettings Settings;
|
||||
@@ -203,7 +213,17 @@ void lcSetProfileString(LC_PROFILE_KEY Key, const QString& Value)
|
||||
Settings.setValue(QString("%1/%2").arg(Entry.mSection, Entry.mKey), Value);
|
||||
}
|
||||
|
||||
void lcSetProfileBuffer(LC_PROFILE_KEY Key, const lcMemFile& Buffer)
|
||||
void lcSetProfileBuffer(LC_PROFILE_KEY Key, const QByteArray& Buffer)
|
||||
{
|
||||
lcProfileEntry& Entry = gProfileEntries[Key];
|
||||
QSettings Settings;
|
||||
|
||||
LC_ASSERT(Entry.mType == LC_PROFILE_ENTRY_BUFFER);
|
||||
|
||||
Settings.setValue(QString("%1/%2").arg(Entry.mSection, Entry.mKey), Buffer);
|
||||
}
|
||||
|
||||
void lcSetProfileBuffer(LC_PROFILE_KEY Key, const lcMemFile& Buffer) // todo: deprecated
|
||||
{
|
||||
lcProfileEntry& Entry = gProfileEntries[Key];
|
||||
QSettings Settings;
|
||||
|
||||
@@ -104,11 +104,13 @@ QString lcGetDefaultProfileString(LC_PROFILE_KEY Key);
|
||||
int lcGetProfileInt(LC_PROFILE_KEY Key);
|
||||
float lcGetProfileFloat(LC_PROFILE_KEY Key);
|
||||
QString lcGetProfileString(LC_PROFILE_KEY Key);
|
||||
QByteArray lcGetProfileBuffer(LC_PROFILE_KEY Key);
|
||||
void lcGetProfileBuffer(LC_PROFILE_KEY Key, lcMemFile& Buffer);
|
||||
|
||||
void lcSetProfileInt(LC_PROFILE_KEY Key, int Value);
|
||||
void lcSetProfileFloat(LC_PROFILE_KEY Key, float Value);
|
||||
void lcSetProfileString(LC_PROFILE_KEY Key, const QString& Value);
|
||||
void lcSetProfileBuffer(LC_PROFILE_KEY Key, const QByteArray& Buffer);
|
||||
void lcSetProfileBuffer(LC_PROFILE_KEY Key, const lcMemFile& Buffer);
|
||||
|
||||
#endif // LC_PROFILE_H
|
||||
|
||||
+28
-42
@@ -1,109 +1,95 @@
|
||||
#include "lc_global.h"
|
||||
#include "lc_shortcuts.h"
|
||||
#include "lc_profile.h"
|
||||
#include "lc_file.h"
|
||||
|
||||
lcKeyboardShortcuts gKeyboardShortcuts;
|
||||
|
||||
void lcLoadDefaultKeyboardShortcuts()
|
||||
{
|
||||
lcMemFile File;
|
||||
QByteArray Buffer = lcGetProfileBuffer(LC_PROFILE_SHORTCUTS);
|
||||
|
||||
lcGetProfileBuffer(LC_PROFILE_SHORTCUTS, File);
|
||||
|
||||
if (!File.GetLength() || !lcLoadKeyboardShortcuts(File, gKeyboardShortcuts))
|
||||
lcResetKeyboardShortcuts(gKeyboardShortcuts);
|
||||
if (Buffer.isEmpty() || !gKeyboardShortcuts.Load(QTextStream(Buffer, QIODevice::ReadOnly)))
|
||||
gKeyboardShortcuts.Reset();
|
||||
}
|
||||
|
||||
void lcSaveDefaultKeyboardShortcuts()
|
||||
{
|
||||
lcMemFile File;
|
||||
QByteArray Buffer;
|
||||
|
||||
lcSaveKeyboardShortcuts(File, gKeyboardShortcuts);
|
||||
gKeyboardShortcuts.Save(QTextStream(&Buffer, QIODevice::WriteOnly));
|
||||
|
||||
lcSetProfileBuffer(LC_PROFILE_SHORTCUTS, File);
|
||||
lcSetProfileBuffer(LC_PROFILE_SHORTCUTS, Buffer);
|
||||
}
|
||||
|
||||
void lcResetDefaultKeyboardShortcuts()
|
||||
{
|
||||
lcResetKeyboardShortcuts(gKeyboardShortcuts);
|
||||
gKeyboardShortcuts.Reset();
|
||||
|
||||
lcRemoveProfileKey(LC_PROFILE_SHORTCUTS);
|
||||
}
|
||||
|
||||
void lcResetKeyboardShortcuts(lcKeyboardShortcuts& Shortcuts)
|
||||
void lcKeyboardShortcuts::Reset()
|
||||
{
|
||||
for (int CommandIdx = 0; CommandIdx < LC_NUM_COMMANDS; CommandIdx++)
|
||||
Shortcuts.Shortcuts[CommandIdx] = qApp->translate("Shortcut", gCommands[CommandIdx].DefaultShortcut);
|
||||
mShortcuts[CommandIdx] = qApp->translate("Shortcut", gCommands[CommandIdx].DefaultShortcut);
|
||||
}
|
||||
|
||||
bool lcSaveKeyboardShortcuts(const QString& FileName, const lcKeyboardShortcuts& Shortcuts)
|
||||
bool lcKeyboardShortcuts::Save(const QString& FileName)
|
||||
{
|
||||
lcDiskFile File;
|
||||
QFile File(FileName);
|
||||
|
||||
if (!File.Open(FileName, "wt"))
|
||||
if (!File.open(QIODevice::WriteOnly))
|
||||
return false;
|
||||
|
||||
return lcSaveKeyboardShortcuts(File, Shortcuts);
|
||||
return Save(QTextStream(&File));
|
||||
}
|
||||
|
||||
bool lcSaveKeyboardShortcuts(lcFile& File, const lcKeyboardShortcuts& Shortcuts)
|
||||
bool lcKeyboardShortcuts::Save(QTextStream& Stream)
|
||||
{
|
||||
char Line[1024];
|
||||
|
||||
for (int CommandIdx = 0; CommandIdx < LC_NUM_COMMANDS; CommandIdx++)
|
||||
{
|
||||
if (Shortcuts.Shortcuts[CommandIdx].isEmpty())
|
||||
if (mShortcuts[CommandIdx].isEmpty())
|
||||
continue;
|
||||
|
||||
sprintf(Line, "%s=%s\n", gCommands[CommandIdx].ID, Shortcuts.Shortcuts[CommandIdx].toUtf8().constData()); // todo: qstring
|
||||
|
||||
File.WriteLine(Line);
|
||||
Stream << gCommands[CommandIdx].ID << QLatin1String("=") << mShortcuts[CommandIdx] << QLatin1String("\n");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lcLoadKeyboardShortcuts(const QString& FileName, lcKeyboardShortcuts& Shortcuts)
|
||||
bool lcKeyboardShortcuts::Load(const QString& FileName)
|
||||
{
|
||||
lcDiskFile File;
|
||||
QFile File(FileName);
|
||||
|
||||
if (!File.Open(FileName, "rt"))
|
||||
if (!File.open(QIODevice::ReadOnly))
|
||||
return false;
|
||||
|
||||
return lcLoadKeyboardShortcuts(File, Shortcuts);
|
||||
return Load(QTextStream(&File));
|
||||
}
|
||||
|
||||
bool lcLoadKeyboardShortcuts(lcFile& File, lcKeyboardShortcuts& Shortcuts)
|
||||
bool lcKeyboardShortcuts::Load(QTextStream& Stream)
|
||||
{
|
||||
for (int CommandIdx = 0; CommandIdx < LC_NUM_COMMANDS; CommandIdx++)
|
||||
Shortcuts.Shortcuts[CommandIdx][0] = 0;
|
||||
mShortcuts[CommandIdx].clear();
|
||||
|
||||
char Line[1024];
|
||||
|
||||
while (File.ReadLine(Line, sizeof(Line)))
|
||||
for (QString Line = Stream.readLine(); !Line.isNull(); Line = Stream.readLine())
|
||||
{
|
||||
char* Key = strchr(Line, '=');
|
||||
int Equals = Line.indexOf('=');
|
||||
|
||||
if (!Key)
|
||||
if (Equals == -1)
|
||||
continue;
|
||||
|
||||
*Key = 0;
|
||||
Key++;
|
||||
QString Key = Line.left(Equals);
|
||||
|
||||
int CommandIdx;
|
||||
for (CommandIdx = 0; CommandIdx < LC_NUM_COMMANDS; CommandIdx++)
|
||||
if (!strcmp(gCommands[CommandIdx].ID, Line))
|
||||
if (gCommands[CommandIdx].ID == Key)
|
||||
break;
|
||||
|
||||
if (CommandIdx == LC_NUM_COMMANDS)
|
||||
continue;
|
||||
|
||||
char* NewLine = strchr(Key, '\n');
|
||||
if (NewLine)
|
||||
*NewLine = 0;
|
||||
|
||||
Shortcuts.Shortcuts[CommandIdx] = QString::fromUtf8(Key); // todo: qstring
|
||||
mShortcuts[CommandIdx] = Line.mid(Equals + 1);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -3,9 +3,16 @@
|
||||
|
||||
#include "lc_commands.h"
|
||||
|
||||
struct lcKeyboardShortcuts
|
||||
class lcKeyboardShortcuts
|
||||
{
|
||||
QString Shortcuts[LC_NUM_COMMANDS];
|
||||
public:
|
||||
void Reset();
|
||||
bool Save(const QString& FileName);
|
||||
bool Save(QTextStream& Stream);
|
||||
bool Load(const QString& FileName);
|
||||
bool Load(QTextStream& Stream);
|
||||
|
||||
QString mShortcuts[LC_NUM_COMMANDS];
|
||||
};
|
||||
|
||||
extern lcKeyboardShortcuts gKeyboardShortcuts;
|
||||
@@ -14,10 +21,4 @@ void lcLoadDefaultKeyboardShortcuts();
|
||||
void lcSaveDefaultKeyboardShortcuts();
|
||||
void lcResetDefaultKeyboardShortcuts();
|
||||
|
||||
void lcResetKeyboardShortcuts(lcKeyboardShortcuts& Shortcuts);
|
||||
bool lcSaveKeyboardShortcuts(const QString& FileName, const lcKeyboardShortcuts& Shortcuts);
|
||||
bool lcSaveKeyboardShortcuts(lcFile& File, const lcKeyboardShortcuts& Shortcuts);
|
||||
bool lcLoadKeyboardShortcuts(const QString& FileName, lcKeyboardShortcuts& Shortcuts);
|
||||
bool lcLoadKeyboardShortcuts(lcFile& File, lcKeyboardShortcuts& Shortcuts);
|
||||
|
||||
#endif // _LC_SHORTCUTS_H_
|
||||
|
||||
@@ -483,12 +483,12 @@ void lcQPreferencesDialog::updateCommandList()
|
||||
}
|
||||
|
||||
QTreeWidgetItem *item = new QTreeWidgetItem;
|
||||
QKeySequence sequence(options->KeyboardShortcuts.Shortcuts[actionIdx]);
|
||||
QKeySequence sequence(options->KeyboardShortcuts.mShortcuts[actionIdx]);
|
||||
item->setText(0, subId);
|
||||
item->setText(1, sequence.toString(QKeySequence::NativeText));
|
||||
item->setData(0, Qt::UserRole, qVariantFromValue(actionIdx));
|
||||
|
||||
if (options->KeyboardShortcuts.Shortcuts[actionIdx] != gCommands[actionIdx].DefaultShortcut)
|
||||
if (options->KeyboardShortcuts.mShortcuts[actionIdx] != gCommands[actionIdx].DefaultShortcut)
|
||||
setShortcutModified(item, true);
|
||||
|
||||
sections[section]->addChild(item);
|
||||
@@ -516,7 +516,7 @@ void lcQPreferencesDialog::commandChanged(QTreeWidgetItem *current)
|
||||
ui->shortcutGroup->setEnabled(true);
|
||||
|
||||
int shortcutIndex = qvariant_cast<int>(current->data(0, Qt::UserRole));
|
||||
QKeySequence key(options->KeyboardShortcuts.Shortcuts[shortcutIndex]);
|
||||
QKeySequence key(options->KeyboardShortcuts.mShortcuts[shortcutIndex]);
|
||||
ui->shortcutEdit->setText(key.toString(QKeySequence::NativeText));
|
||||
}
|
||||
|
||||
@@ -528,11 +528,11 @@ void lcQPreferencesDialog::on_shortcutAssign_clicked()
|
||||
return;
|
||||
|
||||
int shortcutIndex = qvariant_cast<int>(current->data(0, Qt::UserRole));
|
||||
options->KeyboardShortcuts.Shortcuts[shortcutIndex] = ui->shortcutEdit->text();
|
||||
options->KeyboardShortcuts.mShortcuts[shortcutIndex] = ui->shortcutEdit->text();
|
||||
|
||||
current->setText(1, ui->shortcutEdit->text());
|
||||
|
||||
setShortcutModified(current, options->KeyboardShortcuts.Shortcuts[shortcutIndex] != gCommands[shortcutIndex].DefaultShortcut);
|
||||
setShortcutModified(current, options->KeyboardShortcuts.mShortcuts[shortcutIndex] != gCommands[shortcutIndex].DefaultShortcut);
|
||||
|
||||
options->ShortcutsModified = true;
|
||||
options->ShortcutsDefault = false;
|
||||
@@ -553,7 +553,7 @@ void lcQPreferencesDialog::on_shortcutsImport_clicked()
|
||||
return;
|
||||
|
||||
lcKeyboardShortcuts Shortcuts;
|
||||
if (!lcLoadKeyboardShortcuts(FileName, Shortcuts))
|
||||
if (!Shortcuts.Load(FileName))
|
||||
{
|
||||
QMessageBox::warning(this, "LeoCAD", tr("Error loading keyboard shortcuts file."));
|
||||
return;
|
||||
@@ -572,7 +572,7 @@ void lcQPreferencesDialog::on_shortcutsExport_clicked()
|
||||
if (FileName.isEmpty())
|
||||
return;
|
||||
|
||||
if (!lcSaveKeyboardShortcuts(FileName, options->KeyboardShortcuts))
|
||||
if (!options->KeyboardShortcuts.Save(FileName))
|
||||
{
|
||||
QMessageBox::warning(this, "LeoCAD", tr("Error saving keyboard shortcuts file."));
|
||||
return;
|
||||
@@ -584,7 +584,7 @@ void lcQPreferencesDialog::on_shortcutsReset_clicked()
|
||||
if (QMessageBox::question(this, "LeoCAD", tr("Are you sure you want to load the default keyboard shortcuts?"), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
|
||||
return;
|
||||
|
||||
lcResetKeyboardShortcuts(options->KeyboardShortcuts);
|
||||
options->KeyboardShortcuts.Reset();
|
||||
updateCommandList();
|
||||
|
||||
options->ShortcutsModified = true;
|
||||
|
||||
Reference in New Issue
Block a user