File: c2bShortcutWidget.cpp

package info (click to toggle)
cb2bib 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,368 kB
  • sloc: cpp: 24,179; sh: 481; makefile: 16
file content (102 lines) | stat: -rw-r--r-- 4,652 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/***************************************************************************
 *   Copyright (C) 2004-2015 by Pere Constans
 *   constans@molspaces.com
 *   cb2Bib version 1.9.2. Licensed under the GNU GPL version 3.
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "c2bShortcutWidget.h"

#include "c2bSettings.h"


c2bShortcutWidget::c2bShortcutWidget(QWidget* parentw) : QWidget(parentw)
{
    ui.setupUi(this);
    _settingsP = c2bSettingsP;
    _standard_keysequences.append(QKeySequence::Close);
    _standard_keysequences.append(QKeySequence::Copy);
    _standard_keysequences.append(QKeySequence::Cut);
    _standard_keysequences.append(QKeySequence::Find);
    _standard_keysequences.append(QKeySequence::FindNext);
    _standard_keysequences.append(QKeySequence::FindPrevious);
    _standard_keysequences.append(QKeySequence::HelpContents);
    _standard_keysequences.append(QKeySequence::Open);
    _standard_keysequences.append(QKeySequence::Paste);
    _standard_keysequences.append(QKeySequence::Redo);
    _standard_keysequences.append(QKeySequence::Refresh);
    _standard_keysequences.append(QKeySequence::Save);
    _standard_keysequences.append(QKeySequence::SelectAll);
    _standard_keysequences.append(QKeySequence::Undo);
    _standard_keysequences.append(QKeySequence::ZoomIn);
    _standard_keysequences.append(QKeySequence::ZoomOut);
    _standard_keysequences.append(Qt::Key_F2);
    _standard_keysequences.append(QKeySequence::Quit);
    _standard_keysequences.append(QKeySequence::SaveAs);

    connect(ui.Clear, SIGNAL(clicked()), ui.Shortcut, SLOT(clear()));
    connect(ui.Reset, SIGNAL(clicked()), ui.Shortcut, SLOT(reset()));
}


void c2bShortcutWidget::setShortcutNames(const QString& path, const QStringList& fullnames)
{
    disconnect(ui.Shortcut, SIGNAL(shortcutChanged(const QKeySequence&, const QKeySequence&)),
               this, SLOT(shortcutChanged(const QKeySequence&, const QKeySequence&)));
    disconnect(ui.Description, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(currentIndexChanged(const QString&)));
    _modified_keysequences.clear();

    _shortcut_path = path;
    _fullnames = fullnames.filter(path);
    const int n(_fullnames.count());
    const int p(path.length());
    for (int i = 0; i < n; ++i)
        if (_fullnames.at(i).startsWith(path))
            ui.Description->addItem(_fullnames.at(i).mid(p));

    ui.Shortcut->setKeysequence(_settingsP->keysequence(_shortcut_path + ui.Description->currentText()));
    ui.Shortcut->setDefaultKeysequence(_settingsP->defaultKeysequence(_shortcut_path + ui.Description->currentText()));

    connect(ui.Shortcut, SIGNAL(shortcutChanged(const QKeySequence&, const QKeySequence&)),
            this, SLOT(shortcutChanged(const QKeySequence&, const QKeySequence&)));
    connect(ui.Description, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(currentIndexChanged(const QString&)));
}

void c2bShortcutWidget::currentIndexChanged(const QString& name)
{
    const QString fullname(_shortcut_path + name);
    if (_modified_keysequences.contains(fullname))
        ui.Shortcut->setKeysequence(_modified_keysequences.value(fullname));
    else
        ui.Shortcut->setKeysequence(_settingsP->keysequence(fullname));
    ui.Shortcut->setDefaultKeysequence(_settingsP->defaultKeysequence(fullname));
}

void c2bShortcutWidget::shortcutChanged(const QKeySequence& oldks, const QKeySequence& newks)
{
    const QString fullname(_shortcut_path + ui.Description->currentText());
    if (!newks.isEmpty())
    {
        if (_standard_keysequences.contains(newks))
        {
            ui.Shortcut->setKeysequence(oldks);
            ui.Shortcut->setChecked(true);
            emit statusMessage(tr("Key sequence %1 is used by standard shortcuts").arg(newks.toString(QKeySequence::NativeText)));
            return;
        }
        if (_modified_keysequences.size() == 0)
        {
            const int n(_fullnames.count());
            for (int i = 0; i < n; ++i)
                _modified_keysequences.insert(_fullnames.at(i), _settingsP->keysequence(_fullnames.at(i)));
        }
        if (_modified_keysequences.values().contains(newks) && _modified_keysequences.value(fullname) != newks)
        {
            ui.Shortcut->setKeysequence(oldks);
            ui.Shortcut->setChecked(true);
            emit statusMessage(tr("Key sequence %1 is already assigned").arg(newks.toString(QKeySequence::NativeText)));
            return;
        }
    }
    _modified_keysequences[fullname] = newks;
    emit statusMessage(QString());
}