Krita Source Code Documentation
Loading...
Searching...
No Matches
KoConfigAuthorPage.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 SPDX-FileCopyrightText: 2000 Simon Hausmann <hausmann@kde.org>
3 SPDX-FileCopyrightText: 2006 Martin Pfeiffer <hubipete@gmx.net>
4 SPDX-FileCopyrightText: 2012 C. Boemann <cbo@boemann.dk>
5 SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#include "KoConfigAuthorPage.h"
11
12#include "ui_KoConfigAuthorPage.h"
13
14#include <KoIcon.h>
15#include <QDebug>
16
17#include <klocalizedstring.h>
18#include <kuser.h>
19#include <kemailsettings.h>
20#include <kconfiggroup.h>
21#include <ksharedconfig.h>
22#include <KoResourcePaths.h>
23
24#include <QLineEdit>
25#include <QCompleter>
26#include <QStackedWidget>
27#include <QList>
28#include <QComboBox>
29#include <QGridLayout>
30#include <QString>
31#include <QStringList>
32#include <QToolButton>
33#include <QInputDialog>
34#include <QTableView>
35#include <QStandardItem>
36#include <QDomDocument>
37#include <QDomElement>
38#include <QFile>
39#include <QDir>
40#include <QByteArray>
41
54
55
57 : d(new Private)
58{
59 QGridLayout *layout = new QGridLayout(this);
60
61 d->cmbAuthorProfiles = new QComboBox();
62 layout->addWidget(d->cmbAuthorProfiles, 0, 0);
63 QToolButton *newUser = new QToolButton();
64 newUser->setIcon(koIcon("list-add"));
65 newUser->setToolTip(i18n("Add new author profile (starts out as a copy of current)"));
66 layout->addWidget(newUser, 0, 1);
67 d->bnDeleteUser = new QToolButton();
68 d->bnDeleteUser->setIcon(koIcon("edit-delete"));
69 d->bnDeleteUser->setToolTip(i18n("Delete the author profile"));
70 layout->addWidget(d->bnDeleteUser, 0, 2);
71 QFrame *f = new QFrame();
72 f->setFrameStyle(QFrame::HLine | QFrame::Sunken);
73 layout->addWidget(f, 1, 0);
74 d->stack = new QStackedWidget();
75 layout->addWidget(d->stack, 2, 0, 1, 3);
76
77 //list of positions that we can use to provide useful autocompletion.
78 d->positions << QString(i18nc("This is a list of suggestions for positions an artist can take, comma-separated","Adapter,Animator,Artist,Art Director,Author,Assistant,"
79 "Editor,Background,Cartoonist,Colorist,Concept Artist,"
80 "Corrector,Cover Artist,Creator,Designer,Inker,"
81 "Letterer,Matte Painter,Painter,Penciller,Proofreader,"
82 "Pixel Artist,Redliner,Sprite Artist,Typographer,Texture Artist,"
83 "Translator,Writer,Other")).split(",");
84
85 //Keep these two in sync!
86 d->contactModes << i18n("Homepage") << i18n("Email") << i18n("Post Address") << i18n("Telephone") << i18n("Fax");
87 d->contactKeys << "homepage" << "email" << "address" << "telephone" << "fax";
88 QStringList headerlabels;
89 headerlabels<< i18n("Type") << i18n("Entry");
90
91 Ui::KoConfigAuthorPage *aUi = 0;
92 QWidget *w = new QWidget;
93 d->defaultAuthor = i18n("Anonymous");
94
95 QStringList profilesNew;
96 QDir dir(KoResourcePaths::getAppDataLocation() + "/authorinfo/");
97 QStringList filters = QStringList() << "*.authorinfo";
98 Q_FOREACH(const QString &entry, dir.entryList(filters)) {
99 QFile file(dir.absoluteFilePath(entry));
100 if (file.open(QFile::ReadOnly)) {
101 QByteArray ba = file.readAll();
102 file.close();
103 QDomDocument doc = QDomDocument();
104 doc.setContent(ba);
105 QDomElement root = doc.firstChildElement();
106 aUi = new Ui::KoConfigAuthorPage();
107 w = new QWidget;
108 aUi->setupUi(w);
109 QString profile = root.attribute("name");
110
111 QDomElement el = root.firstChildElement("nickname");
112 if (!el.isNull()) {
113 aUi->leNickName->setText(el.text());
114 }
115 el = root.firstChildElement("givenname");
116 if (!el.isNull()) {
117 aUi->leFirstName->setText(el.text());
118 }
119 el = root.firstChildElement("middlename");
120 if (!el.isNull()) {
121 aUi->leInitials->setText(el.text());
122 }
123 el = root.firstChildElement("familyname");
124 if (!el.isNull()) {
125 aUi->leLastName->setText(el.text());
126 }
127 el = root.firstChildElement("title");
128 if (!el.isNull()) {
129 aUi->leTitle->setText(el.text());
130 }
131 el = root.firstChildElement("position");
132 if (!el.isNull()) {
133 aUi->lePosition->setText(el.text());
134 }
135 el = root.firstChildElement("company");
136 if (!el.isNull()) {
137 aUi->leCompany->setText(el.text());
138 }
139
140 aUi->tblContactInfo->setItemDelegate(new KoContactInfoDelegate(this, d->contactModes));
141 QStandardItemModel *modes = new QStandardItemModel();
142 aUi->tblContactInfo->setModel(modes);
143 el = root.firstChildElement("contact");
144 while (!el.isNull()) {
146 QString type = d->contactModes.at(d->contactKeys.indexOf(el.attribute("type")));
147 list.append(new QStandardItem(type));
148 list.append(new QStandardItem(el.text()));
149 modes->appendRow(list);
150 el = el.nextSiblingElement("contact");
151 }
152 modes->setHorizontalHeaderLabels(headerlabels);
153 QCompleter *positionSuggestions = new QCompleter(d->positions);
154 positionSuggestions->setCaseSensitivity(Qt::CaseInsensitive);
155 aUi->lePosition->setCompleter(positionSuggestions);
156
157 connect(aUi->btnAdd, SIGNAL(clicked()), this, SLOT(addContactEntry()));
158 connect(aUi->btnRemove, SIGNAL(clicked()), this, SLOT(removeContactEntry()));
159
160 d->cmbAuthorProfiles->addItem(profile);
161 profilesNew.append(profile);
162 d->profileUiList.append(aUi);
163 d->stack->addWidget(w);
164 }
165 }
166
167 // Add all the user defined profiles (old type)
168 KConfigGroup authorGroup(KSharedConfig::openConfig(), "Author");
169 QStringList profiles = authorGroup.readEntry("profile-names", QStringList());
170
171
172 foreach (const QString &profile , profiles) {
173 if (!profilesNew.contains(profile)) {
174 KConfigGroup cgs(&authorGroup, "Author-" + profile);
175 aUi = new Ui::KoConfigAuthorPage();
176 w = new QWidget;
177 aUi->setupUi(w);
178 aUi->leNickName->setText(cgs.readEntry("creator"));
179 aUi->leFirstName->setText(cgs.readEntry("creator-first-name"));
180 aUi->leLastName->setText(cgs.readEntry("creator-last-name"));
181 aUi->leInitials->setText(cgs.readEntry("initial"));
182 aUi->leTitle->setText(cgs.readEntry("author-title"));
183 aUi->lePosition->setText(cgs.readEntry("position"));
184 QCompleter *positionSuggestions = new QCompleter(d->positions);
185 positionSuggestions->setCaseSensitivity(Qt::CaseInsensitive);
186 aUi->lePosition->setCompleter(positionSuggestions);
187 aUi->leCompany->setText(cgs.readEntry("company"));
188
189 aUi->tblContactInfo->setItemDelegate(new KoContactInfoDelegate(this, d->contactModes));
190 QStandardItemModel *modes = new QStandardItemModel();
191 aUi->tblContactInfo->setModel(modes);
192 if (cgs.hasKey("email")) {
194 QString email = d->contactModes.at(d->contactKeys.indexOf("email"));
195 list.append(new QStandardItem(email));
196 list.append(new QStandardItem(cgs.readEntry("email")));
197 modes->appendRow(list);
198 }
199 if (cgs.hasKey("telephone-work")) {
201 QString tel = d->contactModes.at(d->contactKeys.indexOf("telephone"));
202 list.append(new QStandardItem(tel));
203 list.append(new QStandardItem(cgs.readEntry("telephone-work")));
204 modes->appendRow(list);
205 }
206 if (cgs.hasKey("fax")) {
208 QString fax = d->contactModes.at(d->contactKeys.indexOf("fax"));
209 list.append(new QStandardItem(fax));
210 list.append(new QStandardItem(cgs.readEntry("fax")));
211 modes->appendRow(list);
212 }
213 QStringList postal;
214 postal << cgs.readEntry("street") << cgs.readEntry("postal-code") << cgs.readEntry("city") << cgs.readEntry("country");
215 QString address;
216 Q_FOREACH(QString part, postal) {
217 if (!part.isEmpty()) {
218 address+= part + "\n";
219 }
220 }
221 if (!address.isEmpty()) {
223 QString add = d->contactModes.at(d->contactKeys.indexOf("address"));
224 list.append(new QStandardItem(add));
225 list.append(new QStandardItem(address));
226 modes->appendRow(list);
227 }
228 modes->setHorizontalHeaderLabels(headerlabels);
229 connect(aUi->btnAdd, SIGNAL(clicked()), this, SLOT(addContactEntry()));
230 connect(aUi->btnRemove, SIGNAL(clicked()), this, SLOT(removeContactEntry()));
231
232 d->cmbAuthorProfiles->addItem(profile);
233 d->profileUiList.append(aUi);
234 d->stack->addWidget(w);
235 }
236 }
237
238
239 // Add a default profile
240 aUi = new Ui::KoConfigAuthorPage();
241 w = new QWidget;
242 if (!profiles.contains(d->defaultAuthor) || profilesNew.contains(d->defaultAuthor)) {
243 //w->setEnabled(false);
244 aUi->setupUi(w);
245 w->setEnabled(false);
246 d->cmbAuthorProfiles->insertItem(0, d->defaultAuthor);
247 d->stack->insertWidget(0, w);
248 d->profileUiList.insert(0, aUi);
249 } else {
250 delete aUi;
251 delete w;
252 }
253
254
255 // Connect slots
256 connect(d->cmbAuthorProfiles, SIGNAL(currentIndexChanged(int)), this, SLOT(profileChanged(int)));
257 connect(newUser, SIGNAL(clicked(bool)), this, SLOT(addUser()));
258 connect(d->bnDeleteUser, SIGNAL(clicked(bool)), this, SLOT(deleteUser()));
259
260 d->cmbAuthorProfiles->setCurrentIndex(0);
262}
263
268
270{
271 d->stack->setCurrentIndex(i);
272 // Profile 0 should never be deleted: it's the anonymous profile.
273 d->bnDeleteUser->setEnabled(i > 0);
274}
275
277{
278 bool ok;
279 QString profileName = QInputDialog::getText(this, i18n("Name of Profile"), i18n("Name (not duplicate or blank name):"), QLineEdit::Normal, "", &ok);
280
281 if (!ok) {
282 return;
283 }
284
285 Ui::KoConfigAuthorPage *curUi = d->profileUiList[d->cmbAuthorProfiles->currentIndex()];
286 Ui::KoConfigAuthorPage *aUi = new Ui::KoConfigAuthorPage();
287 QWidget *w = new QWidget;
288 aUi->setupUi(w);
289
290 aUi->leNickName->setText(curUi->leNickName->text());
291 aUi->leInitials->setText(curUi->leInitials->text());
292 aUi->leTitle->setText(curUi->leTitle->text());
293 aUi->leCompany->setText(curUi->leCompany->text());
294 aUi->leFirstName->setText(curUi->leFirstName->text());
295 aUi->leLastName->setText(curUi->leLastName->text());
296 aUi->lePosition->setText(curUi->lePosition->text());
297 QCompleter *positionSuggestions = new QCompleter(d->positions);
298 positionSuggestions->setCaseSensitivity(Qt::CaseInsensitive);
299 aUi->lePosition->setCompleter(positionSuggestions);
300 aUi->tblContactInfo->setItemDelegate(new KoContactInfoDelegate(this, d->contactModes));
301 QStandardItemModel *modes = new QStandardItemModel();
302 aUi->tblContactInfo->setModel(modes);
303
304 connect(aUi->btnAdd, SIGNAL(clicked()), this, SLOT(addContactEntry()));
305 connect(aUi->btnRemove, SIGNAL(clicked()), this, SLOT(removeContactEntry()));
306
307 int index = d->cmbAuthorProfiles->currentIndex() + 1;
308 d->cmbAuthorProfiles->insertItem(index, profileName);
309 d->profileUiList.insert(index, aUi);
310 d->stack->insertWidget(index, w);
311 d->cmbAuthorProfiles->setCurrentIndex(index);
312}
313
315{
316 int index = d->cmbAuthorProfiles->currentIndex();
317 QWidget *w = d->stack->currentWidget();
318
319 d->stack->removeWidget(w);
320 d->profileUiList.removeAt(index);
321 d->cmbAuthorProfiles->removeItem(index);
322 delete w;
323}
324
326{
327 int i = d->cmbAuthorProfiles->currentIndex();
328 Ui::KoConfigAuthorPage *aUi = d->profileUiList[i];
329 QStandardItemModel *contact = static_cast<QStandardItemModel*>(aUi->tblContactInfo->model());
331 list.append(new QStandardItem(d->contactModes.at(0)));
332 list.append(new QStandardItem(i18n("New Contact Info")));
333 contact->appendRow(list);
334 aUi->tblContactInfo->setModel(contact);
335}
336
338{
339 int i = d->cmbAuthorProfiles->currentIndex();
340 Ui::KoConfigAuthorPage *aUi = d->profileUiList[i];
341 QModelIndex index = aUi->tblContactInfo->selectionModel()->currentIndex();
342 aUi->tblContactInfo->model()->removeRow(index.row());
343}
344
346{
347 QString authorInfo = KoResourcePaths::getAppDataLocation() + "/authorinfo/";
348 QDir dir(authorInfo);
349 if (!dir.mkpath(authorInfo)) {
350 qWarning()<<"We can't make an author info directory, and therefore not save!";
351 return;
352 }
353 for (int i = 0; i < d->profileUiList.size(); i++) {
354 if (d->cmbAuthorProfiles->itemText(i)!= d->defaultAuthor) {
355 QByteArray ba;
356 QDomDocument doc = QDomDocument();
357 Ui::KoConfigAuthorPage *aUi = d->profileUiList[i];
358
359 QDomElement root = doc.createElement("author");
360 root.setAttribute("name", d->cmbAuthorProfiles->itemText(i));
361
362 QDomElement nickname = doc.createElement("nickname");
363 nickname.appendChild(doc.createTextNode(aUi->leNickName->text()));
364 root.appendChild(nickname);
365 QDomElement givenname = doc.createElement("givenname");
366 givenname.appendChild(doc.createTextNode(aUi->leFirstName->text()));
367 root.appendChild(givenname);
368 QDomElement familyname = doc.createElement("familyname");
369 familyname.appendChild(doc.createTextNode(aUi->leLastName->text()));
370 root.appendChild(familyname);
371 QDomElement middlename = doc.createElement("middlename");
372 middlename.appendChild(doc.createTextNode(aUi->leInitials->text()));
373 root.appendChild(middlename);
374 QDomElement title = doc.createElement("title");
375 title.appendChild(doc.createTextNode(aUi->leTitle->text()));
376 root.appendChild(title);
377 QDomElement company = doc.createElement("company");
378 company.appendChild(doc.createTextNode(aUi->leCompany->text()));
379 root.appendChild(company);
380 QDomElement position = doc.createElement("position");
381 position.appendChild(doc.createTextNode(aUi->lePosition->text()));
382 root.appendChild(position);
383 if (aUi->tblContactInfo) {
384 if (aUi->tblContactInfo->model()) {
385 for (int i=0; i<aUi->tblContactInfo->model()->rowCount(); i++) {
386 QModelIndex index = aUi->tblContactInfo->model()->index(i, 1);
387 QModelIndex typeIndex = aUi->tblContactInfo->model()->index(i, 0);
388 QDomElement contactEl = doc.createElement("contact");
389 QString content = QVariant(aUi->tblContactInfo->model()->data(index)).toString();
390 contactEl.appendChild(doc.createTextNode(content));
391 QString type = QVariant(aUi->tblContactInfo->model()->data(typeIndex)).toString();
392 contactEl.setAttribute("type", d->contactKeys.at(d->contactModes.indexOf(type)));
393 root.appendChild(contactEl);
394 }
395 }
396 }
397 doc.appendChild(root);
398 ba = doc.toByteArray();
399
400 QFile f(authorInfo + d->cmbAuthorProfiles->itemText(i) +".authorinfo");
401 if(f.open(QFile::WriteOnly)) {
402 if (f.write(ba) < 0) {
403 qWarning() << "Writing author info went wrong:" << f.errorString();
404 }
405 f.close();
406 } else {
407 qWarning() << "Writing author info went wrong:" << f.errorString();
408 }
409 }
410 }
411}
412
413KoContactInfoDelegate::KoContactInfoDelegate(QWidget *parent, QStringList contactModes): QStyledItemDelegate(parent), m_contactModes(contactModes)
414{
415}
416
421
422QWidget* KoContactInfoDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &index) const
423{
424
425 if (index.column() > 0) {
426 return new QLineEdit(parent);
427 } else {
428 QComboBox *box = new QComboBox(parent);
429 box->addItems(m_contactModes);
430 return box;
431 }
432}
QList< QString > QStringList
quint64 part(quint64 n1, quint64 n2, int p)
QList< Ui::KoConfigAuthorPage * > profileUiList
The KoContactInfoDelegate class.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
KoContactInfoDelegate(QWidget *parent, QStringList contactModes)
static QString getAppDataLocation()
#define koIcon(name)
Use these macros for icons without any issues.
Definition kis_icon.h:25