2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
11 #include "pref_engines.h"
13 #include <KListWidget>
14 #include <QPushButton>
16 #include <KUrlRequester>
18 #include <KMessageBox>
20 #include "mastersettings.h"
22 class EditEngine
: public KDialog
{
25 KUrlRequester
* m_path
;
26 KUrlRequester
* m_work_dir
;
28 EditEngine(const QString
& caption
, QWidget
* parent
)
31 setButtons(Ok
| Cancel
);
33 QFrame
* page
= new QFrame(this);
34 QVBoxLayout
* layout
= new QVBoxLayout
;
38 tmp
= new QHBoxLayout
;
39 lbl
= new QLabel(i18n("&Name:"), this);
42 m_name
= new KLineEdit(this);
43 lbl
->setBuddy(m_name
);
44 tmp
->addWidget(m_name
);
47 lbl
= new QLabel(i18n("&Type:"), this);
50 m_type
= new KComboBox(this);
51 lbl
->setBuddy(m_type
);
52 m_type
->addItem(i18n("XBoard"), "xboard");
53 m_type
->addItem(i18n("UCI"), "UCI");
54 m_type
->addItem(i18n("Tagua (textual)"), "tagua-text");
55 m_type
->addItem(i18n("Tagua (DBUS)"), "tagua-dbus");
56 m_type
->addItem(i18n("GNU Shogi"), "gnushogi");
57 tmp
->addWidget(m_type
);
58 layout
->addLayout(tmp
);
60 tmp
= new QHBoxLayout
;
61 lbl
= new QLabel(i18n("&Path:"), this);
64 m_path
= new KUrlRequester(this);
65 lbl
->setBuddy(m_path
);
66 tmp
->addWidget(m_path
);
67 layout
->addLayout(tmp
);
69 tmp
= new QHBoxLayout
;
70 lbl
= new QLabel(i18n("&Work directory:"), this);
73 m_work_dir
= new KUrlRequester(this);
74 lbl
->setBuddy(m_work_dir
);
75 tmp
->addWidget(m_work_dir
);
76 layout
->addLayout(tmp
);
78 page
->setLayout(layout
);
84 void load(const EngineDetails
& e
) {
85 m_name
->setText(e
.name
);
86 m_type
->setCurrentIndex(e
.type
);
87 m_path
->setPath(e
.path
);
88 m_work_dir
->setPath(e
.workPath
);
92 if (m_name
->text().isEmpty()) {
93 KMessageBox::error(this, i18n("Choose a name for the engine"), i18n("No engine name"));
99 void save(EngineDetails
& e
) {
101 e
.name
= m_name
->text();
102 e
.type
= static_cast<EngineDetails::EngineType
>(m_type
->currentIndex());
103 e
.path
= m_path
->url().path();
104 e
.workPath
= m_work_dir
->url().path();
109 PrefEngines::PrefEngines(const QString
&, QWidget
* parent
)
111 QHBoxLayout
* layout
= new QHBoxLayout
;
113 m_list
= new QListWidget(this);
114 layout
->addWidget(m_list
);
116 QVBoxLayout
* buttons
= new QVBoxLayout
;
117 m_new
= new QPushButton(i18n("&New..."), this);
118 buttons
->addWidget(m_new
);
119 m_edit
= new QPushButton(i18n("&Edit..."), this);
120 buttons
->addWidget(m_edit
);
121 m_delete
= new QPushButton(i18n("&Delete"), this);
122 buttons
->addWidget(m_delete
);
123 buttons
->addStretch();
124 layout
->addLayout(buttons
);
128 connect(m_new
, SIGNAL(pressed()), this, SLOT(new_engine()));
129 connect(m_edit
, SIGNAL(pressed()), this, SLOT(edit_engine()));
130 connect(m_delete
, SIGNAL(pressed()), this, SLOT(delete_engine()));
131 connect(m_list
, SIGNAL(itemDoubleClicked(QListWidgetItem
*)), this, SLOT(edit_engine()));
134 foreach (Settings s
, settings().group("engines").array("engine")) {
141 connect(m_list
, SIGNAL(currentRowChanged(int)), this, SLOT(update_buttons()));
145 PrefEngines::~PrefEngines() { }
147 void PrefEngines::apply() {
148 SettingArray engines
= settings().group("engines").newArray("engine");
149 foreach (EngineDetails e
, m_engines
) {
150 e
.save(engines
.append());
154 void PrefEngines::reload_engines() {
156 foreach (EngineDetails e
, m_engines
) {
157 m_list
->addItem(e
.name
);
161 void PrefEngines::new_engine() {
162 EditEngine
dialog("New engine", this);
172 void PrefEngines::edit_engine() {
173 int row
= m_list
->currentRow();
175 EditEngine
dialog("Edit engine", this);
177 EngineDetails
& e
= m_engines
[m_list
->currentRow()];
185 void PrefEngines::delete_engine() {
186 int row
= m_list
->currentRow();
189 m_engines
.removeAt(row
);
193 void PrefEngines::update_buttons() {
194 bool sel
= m_list
->currentRow() != -1;
195 m_edit
->setEnabled(sel
);
196 m_delete
->setEnabled(sel
);