Changeset View
Changeset View
Standalone View
Standalone View
src/settings/dictionaryeditor.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | | ||||
3 | web dictionary options for Parley | ||||
4 | | ||||
5 | ----------------------------------------------------------------------- | ||||
6 | | ||||
7 | begin : Mon Nov 13 2017 | ||||
8 | | ||||
9 | copyright :(C) 2017 Dimitris Kardarakos <dimkard@gmail.com> | ||||
10 | | ||||
11 | ----------------------------------------------------------------------- | ||||
12 | | ||||
13 | ***************************************************************************/ | ||||
14 | | ||||
15 | /*************************************************************************** | ||||
16 | * * | ||||
17 | * This program is free software; you can redistribute it and/or modify * | ||||
18 | * it under the terms of the GNU General Public License as published by * | ||||
19 | * the Free Software Foundation; either version 2 of the License, or * | ||||
20 | * (at your option) any later version. * | ||||
21 | * * | ||||
22 | ***************************************************************************/ | ||||
23 | | ||||
24 | #include <KColorScheme> | ||||
25 | #include "dictionaryeditor.h" | ||||
26 | #include "prefs.h" | ||||
27 | #include "webdictoptions.h" | ||||
28 | | ||||
29 | DictionaryEditor::DictionaryEditor(const QString & dictName, const EntryMode & dictEntryMode, const QList<QJsonObject> & currentDictionaries, QWidget *parent) : QDialog(parent), | ||||
30 | entryMode(dictEntryMode), dictionariesJsonObjList(currentDictionaries) | ||||
31 | { | ||||
32 | setupUi(this); | ||||
33 | | ||||
34 | if (entryMode == EntryMode::EditOnly) { | ||||
35 | QJsonObject cfgictionary = dictionaryJsonObj("name", dictName); | ||||
36 | | ||||
37 | entryId = cfgictionary["id"].toString(); | ||||
38 | name->setText(cfgictionary["name"].toString()); | ||||
39 | url->setText(cfgictionary["url"].toString()); | ||||
40 | source->setText(cfgictionary["source"].toString()); | ||||
41 | target->setText(cfgictionary["target"].toString()); | ||||
42 | } | ||||
43 | else if (entryMode == EntryMode::AddOnly) { | ||||
44 | entryId = nextId(); | ||||
45 | name->setText(dictName); | ||||
46 | } | ||||
47 | | ||||
48 | canSave(); //Enable/disable buttons | ||||
49 | | ||||
50 | //Initialize label that explains why save cannot be performed | ||||
51 | QPalette p = QApplication::palette(); | ||||
52 | KColorScheme scheme(QPalette::Active); | ||||
53 | p.setColor(QPalette::WindowText, scheme.foreground(KColorScheme::NegativeText).color()); | ||||
54 | displayError->setPalette(p); | ||||
55 | | ||||
56 | connect(buttonBox, &QDialogButtonBox::accepted, this, &DictionaryEditor::userAccepted); | ||||
57 | | ||||
58 | connect(name, &QLineEdit::textChanged, this, &DictionaryEditor::canSave); | ||||
59 | connect(url, &QLineEdit::textChanged, this, &DictionaryEditor::canSave); | ||||
60 | connect(source, &QLineEdit::textChanged, this, &DictionaryEditor::canSave); | ||||
61 | connect(target, &QLineEdit::textChanged, this, &DictionaryEditor::canSave); | ||||
62 | | ||||
63 | connect(this, &DictionaryEditor::updateDictionaryList, (WebDictOptions *) parent, &WebDictOptions::updateDictionaryList); | ||||
64 | } | ||||
65 | | ||||
66 | void DictionaryEditor::userAccepted() | ||||
67 | { | ||||
68 | QList<QJsonObject>::iterator itr; | ||||
69 | QJsonObject jsonEntry; | ||||
70 | | ||||
71 | jsonEntry["name"] = name->text(); | ||||
72 | jsonEntry["url"] = url->text(); | ||||
73 | jsonEntry["source"] = source->text(); | ||||
74 | jsonEntry["target"] = target->text(); | ||||
75 | jsonEntry["id"] = entryId; | ||||
76 | | ||||
77 | if (entryMode == EntryMode::AddOnly) { | ||||
78 | dictionariesJsonObjList.append(jsonEntry); | ||||
79 | emit updateDictionaryList(dictionariesJsonObjList); | ||||
80 | } | ||||
81 | else if (entryMode == EntryMode::EditOnly) { | ||||
82 | for (itr = dictionariesJsonObjList.begin(); itr != dictionariesJsonObjList.end(); ++itr ) { | ||||
83 | if ( (*itr)["id"] == entryId) { | ||||
84 | (*itr) = jsonEntry; | ||||
85 | } | ||||
86 | } | ||||
87 | emit updateDictionaryList(dictionariesJsonObjList); | ||||
88 | } | ||||
89 | else { | ||||
90 | qDebug() << "No valid action provided"; | ||||
91 | } | ||||
92 | } | ||||
93 | | ||||
94 | QJsonObject DictionaryEditor::dictionaryJsonObj(const QString & keyName, const QString & keyValue) { | ||||
95 | QList<QJsonObject>::const_iterator itr; | ||||
96 | for (itr = dictionariesJsonObjList.constBegin(); itr != dictionariesJsonObjList.constEnd(); ++itr ) { | ||||
97 | if ( (*itr)[keyName] == keyValue ) { | ||||
98 | return *itr; | ||||
99 | } | ||||
100 | } | ||||
101 | | ||||
102 | return QJsonObject(); | ||||
103 | } | ||||
104 | | ||||
105 | | ||||
106 | void DictionaryEditor::canSave() { | ||||
107 | QJsonObject cfgDictionary = QJsonObject(); | ||||
108 | cfgDictionary = dictionaryJsonObj("name", name->text()); //Dictionary with the same name | ||||
109 | | ||||
110 | if(!(cfgDictionary.empty()) && (entryMode == EntryMode::AddOnly || (entryMode == EntryMode::EditOnly && cfgDictionary["id"].toString() != entryId ))) { | ||||
111 | buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); | ||||
112 | displayError->setText(i18n("Dictionary with the same name already exists.")); | ||||
113 | } | ||||
114 | else if ((name->text()).isEmpty() || (url->text()).isEmpty()) { | ||||
115 | buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); | ||||
116 | displayError->setText(i18n("Name and URL cannot be empty.")); | ||||
117 | } | ||||
118 | else if (name->text().contains("<<") || url->text().contains("<<") || source->text().contains("<<") || target->text().contains("<<")) { | ||||
119 | buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); | ||||
120 | displayError->setText(i18n("Please do not use string '<<'.")); | ||||
121 | } | ||||
122 | else { | ||||
123 | buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true); | ||||
124 | displayError->setText(QString()); | ||||
125 | } | ||||
126 | } | ||||
127 | | ||||
128 | const QString DictionaryEditor::nextId() { | ||||
129 | const QList<QJsonObject> listOfJsonObj = dictionariesJsonObjList; | ||||
130 | | ||||
131 | int maxId = 0; | ||||
132 | | ||||
133 | QList<QJsonObject>::const_iterator itr; | ||||
134 | for (itr = listOfJsonObj.constBegin(); itr != listOfJsonObj.constEnd(); ++itr ) { | ||||
135 | if ( ((*itr)["id"]).toString().toInt() > maxId ) { | ||||
136 | maxId = (*itr)["id"].toString().toInt(); | ||||
137 | } | ||||
138 | } | ||||
139 | | ||||
140 | return QString::number(++maxId); | ||||
141 | } |