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.exists()) {
101 file.open(QFile::ReadOnly);
102 QByteArray ba = file.readAll();
103 file.close();
104 QDomDocument doc = QDomDocument();
105 doc.setContent(ba);
106 QDomElement root = doc.firstChildElement();
107 aUi = new Ui::KoConfigAuthorPage();
108 w = new QWidget;
109 aUi->setupUi(w);
110 QString profile = root.attribute("name");
111
112 QDomElement el = root.firstChildElement("nickname");
113 if (!el.isNull()) {
114 aUi->leNickName->setText(el.text());
115 }
116 el = root.firstChildElement("givenname");
117 if (!el.isNull()) {
118 aUi->leFirstName->setText(el.text());
119 }
120 el = root.firstChildElement("middlename");
121 if (!el.isNull()) {
122 aUi->leInitials->setText(el.text());
123 }
124 el = root.firstChildElement("familyname");
125 if (!el.isNull()) {
126 aUi->leLastName->setText(el.text());
127 }
128 el = root.firstChildElement("title");
129 if (!el.isNull()) {
130 aUi->leTitle->setText(el.text());
131 }
132 el = root.firstChildElement("position");
133 if (!el.isNull()) {
134 aUi->lePosition->setText(el.text());
135 }
136 el = root.firstChildElement("company");
137 if (!el.isNull()) {
138 aUi->leCompany->setText(el.text());
139 }
140
141 aUi->tblContactInfo->setItemDelegate(new KoContactInfoDelegate(this, d->contactModes));
142 QStandardItemModel *modes = new QStandardItemModel();
143 aUi->tblContactInfo->setModel(modes);
144 el = root.firstChildElement("contact");
145 while (!el.isNull()) {
147 QString type = d->contactModes.at(d->contactKeys.indexOf(el.attribute("type")));
148 list.append(new QStandardItem(type));
149 list.append(new QStandardItem(el.text()));
150 modes->appendRow(list);
151 el = el.nextSiblingElement("contact");
152 }
153 modes->setHorizontalHeaderLabels(headerlabels);
154 QCompleter *positionSuggestions = new QCompleter(d->positions);
155 positionSuggestions->setCaseSensitivity(Qt::CaseInsensitive);
156 aUi->lePosition->setCompleter(positionSuggestions);
157
158 connect(aUi->btnAdd, SIGNAL(clicked()), this, SLOT(addContactEntry()));
159 connect(aUi->btnRemove, SIGNAL(clicked()), this, SLOT(removeContactEntry()));
160
161 d->cmbAuthorProfiles->addItem(profile);
162 profilesNew.append(profile);
163 d->profileUiList.append(aUi);
164 d->stack->addWidget(w);
165 }
166 }
167
168 // Add all the user defined profiles (old type)
169 KConfigGroup authorGroup(KSharedConfig::openConfig(), "Author");
170 QStringList profiles = authorGroup.readEntry("profile-names", QStringList());
171
172
173 foreach (const QString &profile , profiles) {
174 if (!profilesNew.contains(profile)) {
175 KConfigGroup cgs(&authorGroup, "Author-" + profile);
176 aUi = new Ui::KoConfigAuthorPage();
177 w = new QWidget;
178 aUi->setupUi(w);
179 aUi->leNickName->setText(cgs.readEntry("creator"));
180 aUi->leFirstName->setText(cgs.readEntry("creator-first-name"));
181 aUi->leLastName->setText(cgs.readEntry("creator-last-name"));
182 aUi->leInitials->setText(cgs.readEntry("initial"));
183 aUi->leTitle->setText(cgs.readEntry("author-title"));
184 aUi->lePosition->setText(cgs.readEntry("position"));
185 QCompleter *positionSuggestions = new QCompleter(d->positions);
186 positionSuggestions->setCaseSensitivity(Qt::CaseInsensitive);
187 aUi->lePosition->setCompleter(positionSuggestions);
188 aUi->leCompany->setText(cgs.readEntry("company"));
189
190 aUi->tblContactInfo->setItemDelegate(new KoContactInfoDelegate(this, d->contactModes));
191 QStandardItemModel *modes = new QStandardItemModel();
192 aUi->tblContactInfo->setModel(modes);
193 if (cgs.hasKey("email")) {
195 QString email = d->contactModes.at(d->contactKeys.indexOf("email"));
196 list.append(new QStandardItem(email));
197 list.append(new QStandardItem(cgs.readEntry("email")));
198 modes->appendRow(list);
199 }
200 if (cgs.hasKey("telephone-work")) {
202 QString tel = d->contactModes.at(d->contactKeys.indexOf("telephone"));
203 list.append(new QStandardItem(tel));
204 list.append(new QStandardItem(cgs.readEntry("telephone-work")));
205 modes->appendRow(list);
206 }
207 if (cgs.hasKey("fax")) {
209 QString fax = d->contactModes.at(d->contactKeys.indexOf("fax"));
210 list.append(new QStandardItem(fax));
211 list.append(new QStandardItem(cgs.readEntry("fax")));
212 modes->appendRow(list);
213 }
214 QStringList postal;
215 postal << cgs.readEntry("street") << cgs.readEntry("postal-code") << cgs.readEntry("city") << cgs.readEntry("country");
216 QString address;
217 Q_FOREACH(QString part, postal) {
218 if (!part.isEmpty()) {
219 address+= part + "\n";
220 }
221 }
222 if (!address.isEmpty()) {
224 QString add = d->contactModes.at(d->contactKeys.indexOf("address"));
225 list.append(new QStandardItem(add));
226 list.append(new QStandardItem(address));
227 modes->appendRow(list);
228 }
229 modes->setHorizontalHeaderLabels(headerlabels);
230 connect(aUi->btnAdd, SIGNAL(clicked()), this, SLOT(addContactEntry()));
231 connect(aUi->btnRemove, SIGNAL(clicked()), this, SLOT(removeContactEntry()));
232
233 d->cmbAuthorProfiles->addItem(profile);
234 d->profileUiList.append(aUi);
235 d->stack->addWidget(w);
236 }
237 }
238
239
240 // Add a default profile
241 aUi = new Ui::KoConfigAuthorPage();
242 w = new QWidget;
243 if (!profiles.contains(d->defaultAuthor) || profilesNew.contains(d->defaultAuthor)) {
244 //w->setEnabled(false);
245 aUi->setupUi(w);
246 w->setEnabled(false);
247 d->cmbAuthorProfiles->insertItem(0, d->defaultAuthor);
248 d->stack->insertWidget(0, w);
249 d->profileUiList.insert(0, aUi);
250 } else {
251 delete aUi;
252 delete w;
253 }
254
255
256 // Connect slots
257 connect(d->cmbAuthorProfiles, SIGNAL(currentIndexChanged(int)), this, SLOT(profileChanged(int)));
258 connect(newUser, SIGNAL(clicked(bool)), this, SLOT(addUser()));
259 connect(d->bnDeleteUser, SIGNAL(clicked(bool)), this, SLOT(deleteUser()));
260
261 d->cmbAuthorProfiles->setCurrentIndex(0);
263}
264
269
271{
272 d->stack->setCurrentIndex(i);
273 // Profile 0 should never be deleted: it's the anonymous profile.
274 d->bnDeleteUser->setEnabled(i > 0);
275}
276
278{
279 bool ok;
280 QString profileName = QInputDialog::getText(this, i18n("Name of Profile"), i18n("Name (not duplicate or blank name):"), QLineEdit::Normal, "", &ok);
281
282 if (!ok) {
283 return;
284 }
285
286 Ui::KoConfigAuthorPage *curUi = d->profileUiList[d->cmbAuthorProfiles->currentIndex()];
287 Ui::KoConfigAuthorPage *aUi = new Ui::KoConfigAuthorPage();
288 QWidget *w = new QWidget;
289 aUi->setupUi(w);
290
291 aUi->leNickName->setText(curUi->leNickName->text());
292 aUi->leInitials->setText(curUi->leInitials->text());
293 aUi->leTitle->setText(curUi->leTitle->text());
294 aUi->leCompany->setText(curUi->leCompany->text());
295 aUi->leFirstName->setText(curUi->leFirstName->text());
296 aUi->leLastName->setText(curUi->leLastName->text());
297 aUi->lePosition->setText(curUi->lePosition->text());
298 QCompleter *positionSuggestions = new QCompleter(d->positions);
299 positionSuggestions->setCaseSensitivity(Qt::CaseInsensitive);
300 aUi->lePosition->setCompleter(positionSuggestions);
301 aUi->tblContactInfo->setItemDelegate(new KoContactInfoDelegate(this, d->contactModes));
302 QStandardItemModel *modes = new QStandardItemModel();
303 aUi->tblContactInfo->setModel(modes);
304
305 connect(aUi->btnAdd, SIGNAL(clicked()), this, SLOT(addContactEntry()));
306 connect(aUi->btnRemove, SIGNAL(clicked()), this, SLOT(removeContactEntry()));
307
308 int index = d->cmbAuthorProfiles->currentIndex() + 1;
309 d->cmbAuthorProfiles->insertItem(index, profileName);
310 d->profileUiList.insert(index, aUi);
311 d->stack->insertWidget(index, w);
312 d->cmbAuthorProfiles->setCurrentIndex(index);
313}
314
316{
317 int index = d->cmbAuthorProfiles->currentIndex();
318 QWidget *w = d->stack->currentWidget();
319
320 d->stack->removeWidget(w);
321 d->profileUiList.removeAt(index);
322 d->cmbAuthorProfiles->removeItem(index);
323 delete w;
324}
325
327{
328 int i = d->cmbAuthorProfiles->currentIndex();
329 Ui::KoConfigAuthorPage *aUi = d->profileUiList[i];
330 QStandardItemModel *contact = static_cast<QStandardItemModel*>(aUi->tblContactInfo->model());
332 list.append(new QStandardItem(d->contactModes.at(0)));
333 list.append(new QStandardItem(i18n("New Contact Info")));
334 contact->appendRow(list);
335 aUi->tblContactInfo->setModel(contact);
336}
337
339{
340 int i = d->cmbAuthorProfiles->currentIndex();
341 Ui::KoConfigAuthorPage *aUi = d->profileUiList[i];
342 QModelIndex index = aUi->tblContactInfo->selectionModel()->currentIndex();
343 aUi->tblContactInfo->model()->removeRow(index.row());
344}
345
347{
348 QString authorInfo = KoResourcePaths::getAppDataLocation() + "/authorinfo/";
349 QDir dir(authorInfo);
350 if (!dir.mkpath(authorInfo)) {
351 qWarning()<<"We can't make an author info directory, and therefore not save!";
352 return;
353 }
354 for (int i = 0; i < d->profileUiList.size(); i++) {
355 if (d->cmbAuthorProfiles->itemText(i)!= d->defaultAuthor) {
356 QByteArray ba;
357 QDomDocument doc = QDomDocument();
358 Ui::KoConfigAuthorPage *aUi = d->profileUiList[i];
359
360 QDomElement root = doc.createElement("author");
361 root.setAttribute("name", d->cmbAuthorProfiles->itemText(i));
362
363 QDomElement nickname = doc.createElement("nickname");
364 nickname.appendChild(doc.createTextNode(aUi->leNickName->text()));
365 root.appendChild(nickname);
366 QDomElement givenname = doc.createElement("givenname");
367 givenname.appendChild(doc.createTextNode(aUi->leFirstName->text()));
368 root.appendChild(givenname);
369 QDomElement familyname = doc.createElement("familyname");
370 familyname.appendChild(doc.createTextNode(aUi->leLastName->text()));
371 root.appendChild(familyname);
372 QDomElement middlename = doc.createElement("middlename");
373 middlename.appendChild(doc.createTextNode(aUi->leInitials->text()));
374 root.appendChild(middlename);
375 QDomElement title = doc.createElement("title");
376 title.appendChild(doc.createTextNode(aUi->leTitle->text()));
377 root.appendChild(title);
378 QDomElement company = doc.createElement("company");
379 company.appendChild(doc.createTextNode(aUi->leCompany->text()));
380 root.appendChild(company);
381 QDomElement position = doc.createElement("position");
382 position.appendChild(doc.createTextNode(aUi->lePosition->text()));
383 root.appendChild(position);
384 if (aUi->tblContactInfo) {
385 if (aUi->tblContactInfo->model()) {
386 for (int i=0; i<aUi->tblContactInfo->model()->rowCount(); i++) {
387 QModelIndex index = aUi->tblContactInfo->model()->index(i, 1);
388 QModelIndex typeIndex = aUi->tblContactInfo->model()->index(i, 0);
389 QDomElement contactEl = doc.createElement("contact");
390 QString content = QVariant(aUi->tblContactInfo->model()->data(index)).toString();
391 contactEl.appendChild(doc.createTextNode(content));
392 QString type = QVariant(aUi->tblContactInfo->model()->data(typeIndex)).toString();
393 contactEl.setAttribute("type", d->contactKeys.at(d->contactModes.indexOf(type)));
394 root.appendChild(contactEl);
395 }
396 }
397 }
398 doc.appendChild(root);
399 ba = doc.toByteArray();
400
401 QFile f(authorInfo + d->cmbAuthorProfiles->itemText(i) +".authorinfo");
402 f.open(QFile::WriteOnly);
403 if (f.write(ba) < 0) {
404 qWarning()<<"Writing author info went wrong:"<<f.errorString();
405 }
406 f.close();
407 }
408 }
409}
410
411KoContactInfoDelegate::KoContactInfoDelegate(QWidget *parent, QStringList contactModes): QStyledItemDelegate(parent), m_contactModes(contactModes)
412{
413}
414
419
420QWidget* KoContactInfoDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &index) const
421{
422
423 if (index.column() > 0) {
424 return new QLineEdit(parent);
425 } else {
426 QComboBox *box = new QComboBox(parent);
427 box->addItems(m_contactModes);
428 return box;
429 }
430}
QList< QString > QStringList
quint64 part(quint64 n1, quint64 n2, int p)
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
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