2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
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"
12 #include "mastersettings.h"
17 PrefEngines::PrefEngines(QWidget
* parent
)
21 connect(m_engine_list
, SIGNAL(itemChanged(QListWidgetItem
*)), this, SLOT(setName(QListWidgetItem
*)));
22 connect(m_engine_list
, SIGNAL(currentItemChanged(QListWidgetItem
*, QListWidgetItem
*)),
23 this, SLOT(changeEngine(QListWidgetItem
*)));
24 connect(m_add_engine
, SIGNAL(clicked()), this, SLOT(add()));
25 connect(m_remove_engine
, SIGNAL(clicked()), this, SLOT(remove()));
26 connect(m_path
, SIGNAL(textChanged(const QString
&)), this, SLOT(setPath()));
27 connect(m_type
, SIGNAL(currentIndexChanged(int)), this, SLOT(setType()));
28 connect(m_workdir
, SIGNAL(textChanged(const QString
&)), this, SLOT(setWorkDir()));
30 m_add_engine
->setIcon(KIcon("add"));
31 m_remove_engine
->setIcon(KIcon("remove.png"));
33 SettingArray s_engines
= settings
.group("engines").array("engine");
34 foreach (Settings s
, s_engines
) {
35 EngineDetails details
;
36 s
["name"] >> details
.name
;
37 s
["path"] >> details
.path
;
38 details
.type
= EngineDetails::typeFromName(s
["type"].value
<QString
>());
39 if (s
["workPath"]) s
["workPath"] >> details
.workPath
;
45 EngineDetails
& PrefEngines::currentEngine() {
46 QListWidgetItem
* item
= m_engine_list
->currentItem();
47 if (!item
) throw no_engine();
48 return m_engines
[item
];
51 PrefEngines::~PrefEngines() { }
53 QListWidgetItem
* PrefEngines::addEngine(const EngineDetails
& details
) {
54 QListWidgetItem
* item
= new QListWidgetItem(details
.name
, m_engine_list
);
55 m_engines
[item
] = details
;
56 item
->setFlags(item
->flags() | Qt::ItemIsEditable
);
57 m_type
->setCurrentIndex(0);
61 void PrefEngines::add() {
62 QListWidgetItem
* item
= addEngine(EngineDetails());
63 m_engine_list
->editItem(item
);
66 void PrefEngines::remove() {
67 QListWidgetItem
* item
= m_engine_list
->currentItem();
69 m_engines
.erase(item
);
73 void PrefEngines::setName(QListWidgetItem
* item
) {
75 if (item
) currentEngine().name
= item
->text();
77 catch (no_engine
&) { }
80 void PrefEngines::setPath() {
82 currentEngine().path
= m_path
->url().prettyUrl();
84 catch (no_engine
&) { }
87 void PrefEngines::setType() {
89 currentEngine().type
= static_cast<EngineDetails::EngineType
>(m_type
->currentIndex());
91 catch (no_engine
&) { }
94 void PrefEngines::setWorkDir() {
96 currentEngine().workPath
= m_workdir
->url().prettyUrl();
98 catch (no_engine
&) { }
101 void PrefEngines::changeEngine(QListWidgetItem
*) {
103 m_path
->setUrl(currentEngine().path
);
104 m_workdir
->setUrl(currentEngine().workPath
);
105 m_type
->setCurrentIndex(currentEngine().type
);
107 catch (no_engine
&) { }
111 void PrefEngines::apply() {
112 SettingArray s_engines
= settings
.group("engines").newArray("engine");
113 std::pair
<QListWidgetItem
*, EngineDetails
> p
;
115 for (int i
= 0; i
< m_engine_list
->count(); i
++) {
116 EngineDetails
& details
= m_engines
[m_engine_list
->item(i
)];
117 Settings s
= s_engines
.append();
118 s
["name"] = details
.name
;
119 s
["path"] = details
.path
;
120 s
["type"] = EngineDetails::typeName(details
.type
);
121 if (!details
.workPath
.isEmpty())
122 s
["workPath"] = details
.workPath
;