Debian Codegraph @

src/Gui/Dialogs/DlgProjectInformationImp.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <QApplication>
24 #include <QByteArray>
25 #include <QDateTime>
26 #include <QDesktopServices>
27 #include <QLocale>
28 #include <QUrl>
29
30 #include <App/Document.h>
31 #include <App/License.h>
32 #include <Base/UnitsApi.h>
33
34 #include "Dialogs/DlgProjectInformationImp.h"
35 #include "ui_DlgProjectInformation.h"
36
37 #include "MainWindow.h"
38
39 #if 0 // needed for Qt's lupdate utility
40 qApp->translate("Gui::Dialog::DlgSettingsDocument", "All rights reserved");
41 qApp->translate("Gui::Dialog::DlgSettingsDocument", "Creative Commons Attribution");
42 qApp->translate("Gui::Dialog::DlgSettingsDocument", "Creative Commons Attribution-ShareAlike");
43 qApp->translate("Gui::Dialog::DlgSettingsDocument", "Creative Commons Attribution-NoDerivatives");
44 qApp->translate("Gui::Dialog::DlgSettingsDocument", "Creative Commons Attribution-NonCommercial");
45 qApp->translate("Gui::Dialog::DlgSettingsDocument", "Creative Commons Attribution-NonCommercial-ShareAlike");
46 qApp->translate("Gui::Dialog::DlgSettingsDocument", "Creative Commons Attribution-NonCommercial-NoDerivatives");
47 qApp->translate("Gui::Dialog::DlgSettingsDocument", "Public Domain");
48 qApp->translate("Gui::Dialog::DlgSettingsDocument", "FreeArt");
49 qApp->translate("Gui::Dialog::DlgSettingsDocument", "CERN Open Hardware Licence strongly-reciprocal");
50 qApp->translate("Gui::Dialog::DlgSettingsDocument", "CERN Open Hardware Licence weakly-reciprocal");
51 qApp->translate("Gui::Dialog::DlgSettingsDocument", "CERN Open Hardware Licence permissive");
52 qApp->translate("Gui::Dialog::DlgSettingsDocument", "Other");
53 #endif
54
55 using namespace Gui::Dialog;
56
57 /* TRANSLATOR Gui::Dialog::DlgProjectInformationImp */
58
59 /**
60 * Constructs a Gui::Dialog::DlgProjectInformationImp as a child of 'parent', with the
61 * name 'name' and widget flags set to 'fl'.
62 *
63 * The dialog will by default be modeless, unless you set 'modal' to
64 * true to construct a modal dialog.
65 */
66 DlgProjectInformationImp::DlgProjectInformationImp(App::Document* doc, QWidget* parent, Qt::WindowFlags fl)
67 : QDialog(parent, fl)
68 , _doc(doc)
69 , ui(new Ui_DlgProjectInformation)
70 {
71 auto convertISODate = [](const char* isoDate) {
72 auto str = QString::fromUtf8(isoDate);
73 QDateTime dt = QDateTime::fromString(str, Qt::DateFormat::ISODate);
74 if (dt.isNull()) {
75 return str;
76 }
77
78 QLocale loc = QLocale::system();
79 return loc.toString(dt);
80 };
81 ui->setupUi(this);
82 ui->lineEditName->setText(QString::fromUtf8(doc->Label.getValue()));
83 ui->lineEditPath->setText(QString::fromUtf8(doc->FileName.getValue()));
84 ui->lineEditUuid->setText(QString::fromUtf8(doc->Uid.getValueStr().c_str()));
85 ui->lineEditProgramVersion->setText(QString::fromUtf8(doc->getProgramVersion()));
86 ui->lineEditCreator->setText(QString::fromUtf8(doc->CreatedBy.getValue()));
87 ui->lineEditDate->setText(convertISODate(doc->CreationDate.getValue()));
88 ui->lineEditLastMod->setText(QString::fromUtf8(doc->LastModifiedBy.getValue()));
89 ui->lineEditLastModDate->setText(convertISODate(doc->LastModifiedDate.getValue()));
90 ui->lineEditCompany->setText(QString::fromUtf8(doc->Company.getValue()));
91
92 // Load comboBox with unit systems
93 auto addDesc = [&, index {0}](const std::string& item) mutable {
94 ui->comboBox_unitSystem->addItem(QString::fromStdString(item), index++);
95 };
96 const auto descriptions = Base::UnitsApi::getDescriptions();
97 std::for_each(descriptions.begin(), descriptions.end(), addDesc);
98
99 ui->comboBox_unitSystem->setCurrentIndex(doc->UnitSystem.getValue());
100
101 // load comboBox with license names
102 for (const auto& item : App::licenseItems) {
103 const char* name {item.at(App::posnOfFullName)};
104 QString translated = QApplication::translate("Gui::Dialog::DlgSettingsDocument", name);
105 ui->comboLicense->addItem(translated, QByteArray(name));
106 }
107
108 // set default position to match document
109 int index = ui->comboLicense->findData(QByteArray(doc->License.getValue()));
110 if (index >= 0) {
111 ui->comboLicense->setCurrentIndex(index);
112 }
113 else {
114 index = ui->comboLicense->count();
115 QString text = QString::fromUtf8(doc->License.getValue());
116 ui->comboLicense->addItem(text);
117 ui->comboLicense->setCurrentIndex(index);
118 }
119
120 ui->lineEditLicenseURL->setText(QString::fromUtf8(doc->LicenseURL.getValue()));
121
122 // When saving the text to XML the newlines get lost. So we store also the newlines as '\n'.
123 // See also accept().
124 QString comment = QString::fromUtf8(doc->Comment.getValue());
125
126 QStringList lines = comment.split(QLatin1String("\\n"), Qt::KeepEmptyParts);
127
128 QString text = lines.join(QLatin1String("\n"));
129 ui->textEditComment->setPlainText(text);
130 connect(ui->pushButtonOpenURL, &QPushButton::clicked, this, &DlgProjectInformationImp::open_url);
131 connect(
132 ui->comboLicense,
133 qOverload<int>(&QComboBox::currentIndexChanged),
134 this,
135 &DlgProjectInformationImp::onLicenseTypeChanged
136 );
137 }
138
139 /**
140 * Destroys the object and frees any allocated resources
141 */
142 DlgProjectInformationImp::~DlgProjectInformationImp()
143 {
144 // no need to delete child widgets, Qt does it all for us
145 delete ui;
146 }
147
148 /**
149 * Applies the changes to the project information of the given document.
150 */
151 void DlgProjectInformationImp::accept()
152 {
153 _doc->CreatedBy.setValue(ui->lineEditCreator->text().toUtf8());
154 _doc->LastModifiedBy.setValue(ui->lineEditCreator->text().toUtf8());
155 _doc->Company.setValue(ui->lineEditCompany->text().toUtf8());
156 getMainWindow()->setUserSchema(ui->comboBox_unitSystem->currentIndex());
157 QByteArray licenseName {ui->comboLicense->currentData().toByteArray()};
158 // Is this really necessary?
159 if (licenseName.isEmpty()) {
160 licenseName = ui->comboLicense->currentText().toUtf8();
161 }
162 _doc->License.setValue(licenseName);
163 _doc->LicenseURL.setValue(ui->lineEditLicenseURL->text().toUtf8());
164
165 // Replace newline escape sequence through '\\n' string
166 QStringList lines
167 = ui->textEditComment->toPlainText().split(QLatin1String("\n"), Qt::KeepEmptyParts);
168
169 QString text = lines.join(QLatin1String("\\n"));
170 _doc->Comment.setValue(text.isEmpty() ? QByteArray() : text.toUtf8());
171
172 QDialog::accept();
173 }
174
175 void DlgProjectInformationImp::onLicenseTypeChanged(int index)
176 {
177 const char* url {
178 index >= 0 && index < App::countOfLicenses ? App::licenseItems.at(index).at(App::posnOfUrl)
179 : _doc->LicenseURL.getValue()
180 };
181
182 ui->lineEditLicenseURL->setText(QString::fromLatin1(url));
183 }
184
185 /**
186 * Opens the text in the LicenseURL property in external browser.
187 */
188 void DlgProjectInformationImp::open_url()
189 {
190 QString url = ui->lineEditLicenseURL->text();
191 QDesktopServices::openUrl(QUrl(url, QUrl::TolerantMode));
192 }
193
194 #include "moc_DlgProjectInformationImp.cpp"