Removed AlgebraicNotation from the variant API.
[tagua/yd.git] / src / pref_engines.cpp
blob7f9313c7d4e24a14f68a64dc09a8d10eb1e2130d
1 /*
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.
9 */
11 #include "pref_engines.h"
13 #include <KIcon>
15 #include "foreach.h"
16 #include "mastersettings.h"
19 class no_engine { };
21 PrefEngines::PrefEngines(const QString&, QWidget* parent)
22 : QWidget(parent) {
23 setupUi(this);
25 connect(m_engine_list, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(setName(QListWidgetItem*)));
26 connect(m_engine_list, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
27 this, SLOT(changeEngine(QListWidgetItem*)));
28 connect(m_add_engine, SIGNAL(clicked()), this, SLOT(add()));
29 connect(m_remove_engine, SIGNAL(clicked()), this, SLOT(remove()));
30 connect(m_path, SIGNAL(textChanged(const QString&)), this, SLOT(setPath()));
31 connect(m_type, SIGNAL(currentIndexChanged(int)), this, SLOT(setType()));
32 connect(m_workdir, SIGNAL(textChanged(const QString&)), this, SLOT(setWorkDir()));
34 m_add_engine->setIcon(KIcon("add"));
35 m_remove_engine->setIcon(KIcon("edit-delete"));
37 SettingArray s_engines = settings().group("engines").array("engine");
38 foreach (Settings s, s_engines) {
39 EngineDetails details;
40 s["name"] >> details.name;
41 s["path"] >> details.path;
42 details.type = EngineDetails::typeFromName(s["type"].value<QString>());
43 if (s["workPath"]) s["workPath"] >> details.workPath;
45 addEngine(details);
49 EngineDetails& PrefEngines::currentEngine() {
50 QListWidgetItem* item = m_engine_list->currentItem();
51 if (!item) throw no_engine();
52 return m_engines[item];
55 PrefEngines::~PrefEngines() { }
57 QListWidgetItem* PrefEngines::addEngine(const EngineDetails& details) {
58 QListWidgetItem* item = new QListWidgetItem(details.name, m_engine_list);
59 m_engines[item] = details;
60 item->setFlags(item->flags() | Qt::ItemIsEditable);
61 m_type->setCurrentIndex(0);
62 return item;
65 void PrefEngines::add() {
66 QListWidgetItem* item = addEngine(EngineDetails());
67 m_engine_list->editItem(item);
70 void PrefEngines::remove() {
71 QListWidgetItem* item = m_engine_list->currentItem();
72 Q_ASSERT(item);
73 m_engines.erase(item);
74 delete item;
77 void PrefEngines::setName(QListWidgetItem* item) {
78 try {
79 if (item) currentEngine().name = item->text();
81 catch (no_engine&) { }
84 void PrefEngines::setPath() {
85 try {
86 currentEngine().path = m_path->url().prettyUrl();
88 catch (no_engine&) { }
91 void PrefEngines::setType() {
92 try {
93 currentEngine().type = static_cast<EngineDetails::EngineType>(m_type->currentIndex());
95 catch (no_engine&) { }
98 void PrefEngines::setWorkDir() {
99 try {
100 currentEngine().workPath = m_workdir->url().prettyUrl();
102 catch (no_engine&) { }
105 void PrefEngines::changeEngine(QListWidgetItem*) {
106 try {
107 m_path->setUrl(currentEngine().path);
108 m_workdir->setUrl(currentEngine().workPath);
109 m_type->setCurrentIndex(currentEngine().type);
111 catch (no_engine&) { }
115 void PrefEngines::apply() {
116 SettingArray s_engines = settings().group("engines").newArray("engine");
117 std::pair<QListWidgetItem*, EngineDetails> p;
119 for (int i = 0; i < m_engine_list->count(); i++) {
120 EngineDetails& details = m_engines[m_engine_list->item(i)];
121 Settings s = s_engines.append();
122 s["name"] = details.name;
123 s["path"] = details.path;
124 s["type"] = EngineDetails::typeName(details.type);
125 if (!details.workPath.isEmpty())
126 s["workPath"] = details.workPath;