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
29 #include <QUndoCommand>
30
31 #include "abstracteditablelabelwidget.h"
32 #include "../utils/misc.h"
33
AbstractEditableLabelWidget(QString text,WaitPushUndoStack * undoStack,QWidget * parent,QString title,bool edited,bool noSpacing)34 AbstractEditableLabelWidget::AbstractEditableLabelWidget(QString text, WaitPushUndoStack *undoStack, QWidget *parent, QString title, bool edited, bool noSpacing) : QFrame(parent) {
35 m_noSpacing = noSpacing;
36 m_edited = edited;
37 m_isInEditionMode = false;
38 m_undoStack = undoStack;
39
40 QGridLayout *layout = new QGridLayout;
41
42 if(!title.isNull() && !title.isEmpty()) {
43 m_title = new QLabel(title, this);
44 m_title->setObjectName("title");
45 layout->addWidget(m_title,0,0);
46 } else {
47 m_title = NULL;
48 }
49
50 setLayout(layout);
51
52 m_label = new EditableLabel(text, this);
53 connect(m_label,SIGNAL(editionStarted(QString)),this,SLOT(editionStarted(QString)));
54 connect(this,SIGNAL(editionCompleted(QString)),m_label,SLOT(editionCompleted(QString)));
55
56 m_acceptButton = new QPushButton(tr("Accept"), this);
57 connect(m_acceptButton,SIGNAL(clicked()),this,SLOT(informEditionCompleted()));
58
59 m_cancelButton = new QPushButton(tr("Cancel"), this);
60 connect(m_cancelButton,SIGNAL(clicked()),this,SLOT(editionCanceled()));
61 }
62
text()63 QString AbstractEditableLabelWidget::text() {
64 if(m_edited) {
65 return m_label->text();
66 } else {
67 return ___emptyString___;
68 }
69 }
70
editionStarted(QString text)71 void AbstractEditableLabelWidget::editionStarted(QString text) {
72 setEditionText(text);
73 toEditionMode();
74 }
75
informEditionCompleted()76 void AbstractEditableLabelWidget::informEditionCompleted() {
77 if(m_isInEditionMode) {
78 m_undoStack->push(new QUndoCommand("Dummy parts editor command"));
79 m_edited = true;
80 emit editionCompleted(editionText());
81 toStandardMode();
82 }
83 }
84
editionCanceled()85 void AbstractEditableLabelWidget::editionCanceled() {
86 toStandardMode();
87 }
88
toStandardMode()89 void AbstractEditableLabelWidget::toStandardMode() {
90 m_isInEditionMode = false;
91
92 hide();
93 QGridLayout *layout = (QGridLayout*)this->layout();
94
95 myEditionWidget()->hide();
96 layout->removeWidget(myEditionWidget());
97 m_acceptButton->hide();
98 layout->removeWidget(m_acceptButton);
99 m_cancelButton->hide();
100 layout->removeWidget(m_cancelButton);
101
102 m_label->show();
103 layout->addWidget(m_label,1,0);
104
105 setNoSpacing(layout);
106
107 updateGeometry();
108 show();
109
110 emit editionFinished();
111 }
112
toEditionMode()113 void AbstractEditableLabelWidget::toEditionMode() {
114 m_isInEditionMode = true;
115
116 hide();
117 QGridLayout *layout = (QGridLayout*)this->layout();
118
119 m_label->hide();
120 layout->removeWidget(m_label);
121
122 if(!m_edited) {
123 setEmptyTextToEdit();
124 }
125 myEditionWidget()->show();
126 layout->addWidget(myEditionWidget(),1,0,1,5);
127 m_acceptButton->show();
128 layout->addWidget(m_acceptButton,2,3);
129 m_cancelButton->show();
130 layout->addWidget(m_cancelButton,2,4);
131
132 setNoSpacing(layout);
133
134 updateGeometry();
135 show();
136
137 emit editionStarted();
138 }
139
setNoSpacing(QLayout * layout)140 void AbstractEditableLabelWidget::setNoSpacing(QLayout *layout) {
141 if(m_noSpacing) {
142 layout->setMargin(0);
143 layout->setSpacing(0);
144 }
145 }
146