1 /*******************************************************************
2 
3 Part of the Fritzing project - http://fritzing.org
4 Copyright (c) 2007-2014 Fachhochschule Potsdam - http://fh-potsdam.de
5 
6 Fritzing 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 Fritzing 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 Fritzing.  If not, see <http://www.gnu.org/licenses/>.
18 
19 ********************************************************************
20 
21 $Revision: 6904 $:
22 $Author: irascibl@gmail.com $:
23 $Date: 2013-02-26 16:26:03 +0100 (Di, 26. Feb 2013) $
24 
25 ********************************************************************/
26 
27 
28 #include <QHBoxLayout>
29 #include <QVBoxLayout>
30 #include <QFormLayout>
31 #include <QLineEdit>
32 #include <QTextEdit>
33 #include <QMessageBox>
34 #include <QtDebug>
35 #include <QApplication>
36 
37 #include "pemetadataview.h"
38 #include "hashpopulatewidget.h"
39 
40 //////////////////////////////////////
41 
FocusOutTextEdit(QWidget * parent)42 FocusOutTextEdit::FocusOutTextEdit(QWidget * parent) : QTextEdit(parent)
43 {
44 
45 }
46 
~FocusOutTextEdit()47 FocusOutTextEdit::~FocusOutTextEdit()
48 {
49 }
50 
focusOutEvent(QFocusEvent * e)51 void FocusOutTextEdit::focusOutEvent(QFocusEvent * e) {
52 	QTextEdit::focusOutEvent(e);
53     if (document()->isModified()) {
54         emit focusOut();
55         document()->setModified(false);
56     }
57 }
58 
59 //////////////////////////////////////
60 
PEMetadataView(QWidget * parent)61 PEMetadataView::PEMetadataView(QWidget * parent) : QScrollArea(parent)
62 {
63     m_mainFrame = NULL;
64 	this->setWidgetResizable(true);
65 	this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66 }
67 
~PEMetadataView()68 PEMetadataView::~PEMetadataView() {
69 
70 }
71 
titleEntry()72 void PEMetadataView::titleEntry() {
73 	if (m_titleEdit->isModified()) {
74 		emit metadataChanged("title", m_titleEdit->text());
75         m_titleEdit->setModified(false);
76 	}
77 }
78 
authorEntry()79 void PEMetadataView::authorEntry() {
80 	if (m_authorEdit->isModified()) {
81 		emit metadataChanged("author", m_authorEdit->text());
82         m_authorEdit->setModified(false);
83 	}
84 }
85 
descriptionEntry()86 void PEMetadataView::descriptionEntry() {
87 	if (m_descriptionEdit->document()->isModified()) {
88 		emit metadataChanged("description", m_descriptionEdit->toHtml());
89         m_descriptionEdit->document()->setModified(false);
90 	}
91 }
92 
urlEntry()93 void PEMetadataView::urlEntry() {
94 	if (m_urlEdit->isModified()) {
95 		emit metadataChanged("url", m_urlEdit->text());
96         m_urlEdit->setModified(false);
97 	}
98 }
99 
labelEntry()100 void PEMetadataView::labelEntry() {
101 	if (m_labelEdit->isModified()) {
102 		emit metadataChanged("label", m_labelEdit->text());
103         m_labelEdit->setModified(false);
104 	}
105 }
106 
familyEntry()107 void PEMetadataView::familyEntry() {
108 	if (m_familyEdit->isModified()) {
109 		emit metadataChanged("family", m_familyEdit->text());
110         m_familyEdit->setModified(false);
111 	}
112 }
113 
variantEntry()114 void PEMetadataView::variantEntry() {
115 	if (m_variantEdit->isModified()) {
116 		emit metadataChanged("variant", m_variantEdit->text());
117         m_variantEdit->setModified(false);
118 	}
119 }
120 
dateEntry()121 void PEMetadataView::dateEntry() {
122 }
123 
propertiesEntry()124 void PEMetadataView::propertiesEntry() {
125     emit propertiesChanged(m_propertiesEdit->hash());
126 }
127 
properties()128 const QHash<QString, QString> & PEMetadataView::properties() {
129     return m_propertiesEdit->hash();
130 }
131 
tagsEntry()132 void PEMetadataView::tagsEntry() {
133     static QStringList keys;
134     keys = m_tagsEdit->hash().keys();
135     emit tagsChanged(keys);
136 }
137 
initMetadata(const QDomDocument & doc)138 void PEMetadataView::initMetadata(const QDomDocument & doc)
139 {
140     QWidget * widget = QApplication::focusWidget();
141     if (widget) {
142         QList<QWidget *> children = m_mainFrame->findChildren<QWidget *>();
143         if (children.contains(widget)) {
144             widget->blockSignals(true);
145         }
146     }
147 
148     if (m_mainFrame) {
149         this->setWidget(NULL);
150         delete m_mainFrame;
151         m_mainFrame = NULL;
152     }
153 
154     QDomElement root = doc.documentElement();
155     QDomElement label = root.firstChildElement("label");
156     QDomElement author = root.firstChildElement("author");
157     QDomElement descr = root.firstChildElement("description");
158     QDomElement title = root.firstChildElement("title");
159     QDomElement date = root.firstChildElement("date");
160     QDomElement url = root.firstChildElement("url");
161 
162 	QStringList readOnlyKeys;
163     QHash<QString, QString> tagHash;
164     QDomElement tags = root.firstChildElement("tags");
165     QDomElement tag = tags.firstChildElement("tag");
166     while (!tag.isNull()) {
167         tagHash.insert(tag.text(), "");
168         tag = tag.nextSiblingElement("tag");
169     }
170 
171     QString family;
172     QString variant;
173 
174     QHash<QString, QString> propertyHash;
175     QDomElement properties = root.firstChildElement("properties");
176     QDomElement prop = properties.firstChildElement("property");
177     while (!prop.isNull()) {
178         QString name = prop.attribute("name");
179         QString value = prop.text();
180         if (name.compare("family", Qt::CaseInsensitive) == 0) {
181             family = value;
182         }
183         else if (name.compare("variant", Qt::CaseInsensitive) == 0) {
184             variant = value;
185         }
186         else {
187             propertyHash.insert(name, value);
188         }
189 
190         prop = prop.nextSiblingElement("property");
191     }
192 
193 
194 	m_mainFrame = new QFrame(this);
195 	m_mainFrame->setObjectName("metadataMainFrame");
196 	QVBoxLayout *mainLayout = new QVBoxLayout(m_mainFrame);
197     mainLayout->setSizeConstraint( QLayout::SetMinAndMaxSize );
198 
199     QLabel *explanation = new QLabel(tr("This is where you edit the metadata for the part ..."));
200     mainLayout->addWidget(explanation);
201 
202     QFormLayout * formLayout = new QFormLayout();
203     QFrame * formFrame = new QFrame;
204     mainLayout->addWidget(formFrame);
205 
206     m_titleEdit = new QLineEdit();
207     m_titleEdit->setText(title.text());
208 	connect(m_titleEdit, SIGNAL(editingFinished()), this, SLOT(titleEntry()));
209 	m_titleEdit->setObjectName("PartsEditorLineEdit");
210     m_titleEdit->setStatusTip(tr("Set the part's title"));
211     formLayout->addRow(tr("Title"), m_titleEdit);
212 
213     m_dateEdit = new QLineEdit();
214     m_dateEdit->setText(date.text());
215 	connect(m_dateEdit, SIGNAL(editingFinished()), this, SLOT(dateEntry()));
216 	m_dateEdit->setObjectName("PartsEditorLineEdit");
217     m_dateEdit->setStatusTip(tr("Set the part's date"));
218     m_dateEdit->setEnabled(false);
219     formLayout->addRow(tr("Date"), m_dateEdit);
220 
221     m_authorEdit = new QLineEdit();
222     m_authorEdit->setText(author.text());
223 	connect(m_authorEdit, SIGNAL(editingFinished()), this, SLOT(authorEntry()));
224 	m_authorEdit->setObjectName("PartsEditorLineEdit");
225     m_authorEdit->setStatusTip(tr("Set the part's author"));
226     formLayout->addRow(tr("Author"), m_authorEdit);
227 
228     m_descriptionEdit = new FocusOutTextEdit();
229     m_descriptionEdit->setText(descr.text());
230 	m_descriptionEdit->document()->setModified(false);
231 	connect(m_descriptionEdit, SIGNAL(focusOut()), this, SLOT(descriptionEntry()));
232 	m_descriptionEdit->setObjectName("PartsEditorTextEdit");
233     m_descriptionEdit->setStatusTip(tr("Set the part's description--you can use simple html (as defined by Qt's Rich Text)"));
234     formLayout->addRow(tr("Description"), m_descriptionEdit);
235 
236     m_labelEdit = new QLineEdit();
237     m_labelEdit->setText(label.text());
238 	connect(m_labelEdit, SIGNAL(editingFinished()), this, SLOT(labelEntry()));
239 	m_labelEdit->setObjectName("PartsEditorLineEdit");
240     m_labelEdit->setStatusTip(tr("Set the default part label prefix"));
241     formLayout->addRow(tr("Label"), m_labelEdit);
242 
243     m_urlEdit = new QLineEdit();
244     m_urlEdit->setText(url.text());
245 	connect(m_urlEdit, SIGNAL(editingFinished()), this, SLOT(urlEntry()));
246 	m_urlEdit->setObjectName("PartsEditorLineEdit");
247     m_urlEdit->setStatusTip(tr("Set the part's url if it is described on a web page"));
248     formLayout->addRow(tr("URL"), m_urlEdit);
249 
250     m_familyEdit = new QLineEdit();
251     m_familyEdit->setText(family);
252 	connect(m_familyEdit, SIGNAL(editingFinished()), this, SLOT(familyEntry()));
253 	m_familyEdit->setObjectName("PartsEditorLineEdit");
254     m_familyEdit->setStatusTip(tr("Set the part's family--what other parts is this part related to"));
255     formLayout->addRow(tr("Family"), m_familyEdit);
256 
257     m_variantEdit = new QLineEdit();
258     m_variantEdit->setText(variant);
259 	connect(m_variantEdit, SIGNAL(editingFinished()), this, SLOT(variantEntry()));
260 	m_variantEdit->setObjectName("PartsEditorLineEdit");
261     m_variantEdit->setStatusTip(tr("Set the part's variant--this makes it unique from all other parts in the same family"));
262     formLayout->addRow(tr("Variant"), m_variantEdit);
263 
264     m_propertiesEdit = new HashPopulateWidget("", propertyHash, readOnlyKeys, false, this);
265 	m_propertiesEdit->setObjectName("PartsEditorPropertiesEdit");
266     m_propertiesEdit->setStatusTip(tr("Set the part's properties"));
267     connect(m_propertiesEdit, SIGNAL(changed()), this, SLOT(propertiesEntry()));
268     formLayout->addRow(tr("Properties"), m_propertiesEdit);
269 
270     m_tagsEdit = new HashPopulateWidget("", tagHash, readOnlyKeys, true, this);
271 	m_tagsEdit->setObjectName("PartsEditorPropertiesEdit");
272     m_tagsEdit->setStatusTip(tr("Set the part's tags"));
273     connect(m_tagsEdit, SIGNAL(changed()), this, SLOT(tagsEntry()));
274     formLayout->addRow(tr("Tags"), m_tagsEdit);
275 
276     formFrame->setLayout(formLayout);
277     m_mainFrame->setLayout(mainLayout);
278 
279     this->setWidget(m_mainFrame);
280 }
281 
resetProperty(const QString & name,const QString & value)282 void PEMetadataView::resetProperty(const QString & name, const QString & value)
283 {
284 	if (name == "family") m_familyEdit->setText(value);
285 	else if (name == "variant") m_variantEdit->setText(value);
286 }
287 
family()288 QString PEMetadataView::family() {
289 	return m_familyEdit->text();
290 }
291 
variant()292 QString PEMetadataView::variant() {
293 	return m_variantEdit->text();
294 }
295 
296