aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/General/hotkey/settingsdialog.cpp
blob: bc4e897e5c3f97d742f255263092f239a9c07b53 (plain) (blame)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/***************************************************************************
 *   Copyright (C) 2009 by Ilya Kotov                                      *
 *   forkotov02@hotmail.ru                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QSettings>
#include <QHeaderView>

extern "C"
{
#include <X11/keysym.h>
}

#include <qmmp/qmmp.h>
#include "hotkeydialog.h"
#include "settingsdialog.h"

SettingsDialog::SettingsDialog(QWidget *parent)
        : QDialog(parent)
{
    ui.setupUi(this);
    ui.tableWidget->verticalHeader()->setDefaultSectionSize(fontMetrics().height());
    ui.tableWidget->verticalHeader()->setResizeMode(QHeaderView::Fixed);
    ui.tableWidget->verticalHeader()->hide();
    ui.tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    ui.tableWidget->setRowCount (9);
    ui.tableWidget->setItem(0,0, new QTableWidgetItem(tr("Play")));
    ui.tableWidget->setItem(1,0, new QTableWidgetItem(tr("Stop")));
    ui.tableWidget->setItem(2,0, new QTableWidgetItem(tr("Pause")));
    ui.tableWidget->setItem(3,0, new QTableWidgetItem(tr("Play/Pause")));
    ui.tableWidget->setItem(4,0, new QTableWidgetItem(tr("Next")));
    ui.tableWidget->setItem(5,0, new QTableWidgetItem(tr("Previous")));
    ui.tableWidget->setItem(6,0, new QTableWidgetItem(tr("Show/Hide")));
    ui.tableWidget->setItem(7,0, new QTableWidgetItem(tr("Volume +")));
    ui.tableWidget->setItem(8,0, new QTableWidgetItem(tr("Volume -")));

    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    settings.beginGroup("Hotkey");
    for (int i = Hotkey::PLAY, j = 0; i <= Hotkey::VOLUME_DOWN; ++i, ++j)
    {
        Hotkey *hotkey = new Hotkey;
        hotkey->action = i;
        hotkey->key = settings.value(QString("key_%1").arg(i), hotkey->defaultKey()).toUInt();
        hotkey->mod = settings.value(QString("modifiers_%1").arg(i), 0).toUInt();
        ui.tableWidget->setItem(j,1, new QTableWidgetItem(HotkeyManager::getKeyString(hotkey->key,
                                hotkey->mod), i));
        m_hotkeys << hotkey;
    }
    settings.endGroup();
}


SettingsDialog::~SettingsDialog()
{
    while (!m_hotkeys.isEmpty())
        delete m_hotkeys.takeFirst ();

}

void SettingsDialog::accept()
{
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    settings.beginGroup("Hotkey");
    foreach(Hotkey *k, m_hotkeys)
    {
        settings.setValue(QString("key_%1").arg(k->action), k->key);
        settings.setValue(QString("modifiers_%1").arg(k->action), k->mod);
    }
    settings.endGroup();
    QDialog::accept();
}

void SettingsDialog::on_tableWidget_itemDoubleClicked (QTableWidgetItem *item)
{
    Hotkey *k = 0;
    foreach(k, m_hotkeys)
    {
        if (k->action == item->type())
            break;
    }
    if (!k)
        return;

    HotkeyDialog *dialog = new HotkeyDialog(k->key, k->mod, this);
    if (item->type() >= QTableWidgetItem::UserType &&
            dialog->exec() == QDialog::Accepted)
    {
        item->setText(HotkeyManager::getKeyString(dialog->keySym (), dialog->nativeModifiers ()));
        k->key = dialog->keySym ();
        k->mod = dialog->nativeModifiers ();
    }
    delete dialog;
}

void SettingsDialog::on_resetButton_clicked ()
{
    for (int i = 0; i < m_hotkeys.size(); ++i)
    {
        m_hotkeys[i]->key = m_hotkeys[i]->defaultKey();
        m_hotkeys[i]->mod = 0;
        ui.tableWidget->item(i, 1)->setText(HotkeyManager::getKeyString(m_hotkeys[i]->key, m_hotkeys[i]->mod));
    }
}