1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /*******************************************************************************
21  *  Includes
22  ******************************************************************************/
23 #include "projectpropertieseditordialog.h"
24 
25 #include "ui_projectpropertieseditordialog.h"
26 
27 #include <librepcb/common/undostack.h>
28 #include <librepcb/project/metadata/cmd/cmdprojectmetadataedit.h>
29 #include <librepcb/project/metadata/projectmetadata.h>
30 
31 #include <QtCore>
32 #include <QtWidgets>
33 
34 /*******************************************************************************
35  *  Namespace
36  ******************************************************************************/
37 namespace librepcb {
38 namespace project {
39 namespace editor {
40 
41 /*******************************************************************************
42  *  Constructors / Destructor
43  ******************************************************************************/
44 
ProjectPropertiesEditorDialog(ProjectMetadata & metadata,UndoStack & undoStack,QWidget * parent)45 ProjectPropertiesEditorDialog::ProjectPropertiesEditorDialog(
46     ProjectMetadata& metadata, UndoStack& undoStack, QWidget* parent) noexcept
47   : QDialog(parent),
48     mMetadata(metadata),
49     mUndoStack(undoStack),
50     mAttributes(mMetadata.getAttributes()),
51     mUi(new Ui::ProjectPropertiesEditorDialog) {
52   mUi->setupUi(this);
53 
54   mUi->edtName->setText(*mMetadata.getName());
55   mUi->edtAuthor->setText(mMetadata.getAuthor());
56   mUi->edtVersion->setText(mMetadata.getVersion());
57   mUi->lblCreatedDateTime->setText(
58       mMetadata.getCreated().toString(Qt::DefaultLocaleLongDate));
59   mUi->lblLastModifiedDateTime->setText(
60       mMetadata.getLastModified().toString(Qt::DefaultLocaleLongDate));
61   mUi->attributeListEditorWidget->setReferences(nullptr, &mAttributes);
62 
63   // set focus to name so the user can immediately start typing to change it
64   mUi->edtName->setFocus();
65 }
66 
~ProjectPropertiesEditorDialog()67 ProjectPropertiesEditorDialog::~ProjectPropertiesEditorDialog() noexcept {
68   mUi->attributeListEditorWidget->setReferences(nullptr, nullptr);
69 }
70 
71 /*******************************************************************************
72  *  Private Methods
73  ******************************************************************************/
74 
keyPressEvent(QKeyEvent * e)75 void ProjectPropertiesEditorDialog::keyPressEvent(QKeyEvent* e) {
76   switch (e->key()) {
77     case Qt::Key_Return:
78       accept();
79       break;
80     case Qt::Key_Escape:
81       reject();
82       break;
83     default:
84       QDialog::keyPressEvent(e);
85       break;
86   }
87 }
88 
accept()89 void ProjectPropertiesEditorDialog::accept() {
90   if (applyChanges()) {
91     QDialog::accept();
92   }
93 }
94 
applyChanges()95 bool ProjectPropertiesEditorDialog::applyChanges() noexcept {
96   try {
97     CmdProjectMetadataEdit* cmd = new CmdProjectMetadataEdit(mMetadata);
98     cmd->setName(ElementName(mUi->edtName->text().trimmed()));  // can throw
99     cmd->setAuthor(mUi->edtAuthor->text().trimmed());
100     cmd->setVersion(mUi->edtVersion->text().trimmed());
101     cmd->setAttributes(mAttributes);
102     mUndoStack.execCmd(cmd);
103     return true;
104   } catch (const Exception& e) {
105     QMessageBox::critical(this, tr("Error"), e.getMsg());
106     return false;
107   }
108 }
109 
110 /*******************************************************************************
111  *  End of File
112  ******************************************************************************/
113 
114 }  // namespace editor
115 }  // namespace project
116 }  // namespace librepcb
117