1 /*
2     SPDX-License-Identifier: GPL-2.0-or-later
3     SPDX-FileCopyrightText: 2002-2020 Umbrello UML Modeller Authors <umbrello-devel@kde.org>
4 */
5 
6 // own header
7 #include "statedialog.h"
8 
9 // local includes
10 #include "activitypage.h"
11 #include "documentationwidget.h"
12 #include "umlview.h"
13 #include "umlscene.h"
14 #include "umlviewlist.h"
15 #include "umldoc.h"
16 #include "uml.h"
17 #include "selectdiagramwidget.h"
18 #include "statewidget.h"
19 #include "dialog_utils.h"
20 #include "icon_utils.h"
21 
22 // kde includes
23 #include <KComboBox>
24 #include <klineedit.h>
25 #include <KLocalizedString>
26 
27 // qt includes
28 #include <QFrame>
29 #include <QGridLayout>
30 #include <QGroupBox>
31 #include <QHBoxLayout>
32 #include <QLabel>
33 
34 /**
35  * Constructor.
36  */
StateDialog(QWidget * parent,StateWidget * pWidget)37 StateDialog::StateDialog(QWidget * parent, StateWidget * pWidget)
38   : MultiPageDialogBase(parent),
39     m_pActivityPage(0),
40     m_pStateWidget(pWidget),
41     m_bChangesMade(false),
42     pageActivity(0)
43 {
44     setCaption(i18n("Properties"));
45     setupPages();
46     connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
47     connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
48 }
49 
50 /**
51  * Entered when OK button pressed.
52  */
slotOk()53 void StateDialog::slotOk()
54 {
55     applyPage(pageGeneral);
56     applyPage(pageFont);
57     applyPage(pageActivity);
58     applyPage(pageStyle);
59     accept();
60 }
61 
62 /**
63  * Entered when Apply button pressed.
64  */
slotApply()65 void StateDialog::slotApply()
66 {
67     applyPage(currentPage());
68 }
69 
70 /**
71  * Sets up the pages of the dialog.
72  */
setupPages()73 void StateDialog::setupPages()
74 {
75     setupGeneralPage();
76     if (m_pStateWidget->stateType() == StateWidget::Normal) {
77         setupActivityPage();
78     }
79     pageStyle = setupStylePage(m_pStateWidget);
80     pageFont = setupFontPage(m_pStateWidget);
81 }
82 
83 /**
84  * Applies changes to the given page.
85  */
applyPage(KPageWidgetItem * item)86 void StateDialog::applyPage(KPageWidgetItem*item)
87 {
88     m_bChangesMade = true;
89     if (item == pageGeneral) {
90         if (m_pStateWidget->stateType() == StateWidget::Combined) {
91             m_pStateWidget->setDiagramLink(m_GenPageWidgets.diagramLinkWidget->currentID());
92         }
93         m_pStateWidget->setName(m_GenPageWidgets.nameLE->text());
94         m_GenPageWidgets.docWidget->apply();
95     }
96     else if (item == pageActivity) {
97         if (m_pActivityPage) {
98             m_pActivityPage->updateActivities();
99         }
100     }
101     else if (item == pageStyle) {
102         applyStylePage();
103     }
104     else if (item == pageFont) {
105         applyFontPage(m_pStateWidget);
106     }
107 }
108 
109 /**
110  * Sets up the general page of the dialog.
111  */
setupGeneralPage()112 void StateDialog::setupGeneralPage()
113 {
114     StateWidget::StateType type = m_pStateWidget->stateType();
115 
116     QWidget* page = new QWidget();
117     QVBoxLayout* topLayout = new QVBoxLayout();
118     page->setLayout(topLayout);
119 
120     pageGeneral = createPage(i18nc("general page", "General"), i18n("General Properties"),
121                              Icon_Utils::it_Properties_General, page);
122 
123     m_GenPageWidgets.generalGB = new QGroupBox(i18n("Properties"));
124     topLayout->addWidget(m_GenPageWidgets.generalGB);
125 
126     QGridLayout * generalLayout = new QGridLayout(m_GenPageWidgets.generalGB);
127     generalLayout->setSpacing(spacingHint());
128     generalLayout->setMargin(fontMetrics().height());
129 
130     QString typeStr;
131     switch (type) {
132     case StateWidget::Initial:
133         typeStr = i18nc("initial state in statechart", "Initial state");
134         break;
135     case StateWidget::Normal:
136         typeStr = i18nc("state in statechart", "State");
137         break;
138     case StateWidget::End:
139         typeStr = i18nc("end state in statechart", "End state");
140         break;
141     case StateWidget::Fork:
142         typeStr = i18nc("fork state in statechart", "Fork");
143         break;
144     case StateWidget::Join:
145         typeStr = i18nc("join state in statechart", "Join");
146         break;
147     case StateWidget::Junction:
148         typeStr = i18nc("junction state in statechart", "Junction");
149         break;
150     case StateWidget::DeepHistory:
151         typeStr = i18nc("deep history state in statechart", "DeepHistory");
152         break;
153     case StateWidget::ShallowHistory:
154         typeStr = i18nc("shallow history state in statechart", "ShallowHistory");
155         break;
156     case StateWidget::Choice:
157         typeStr = i18nc("choice state in statechart", "Choice");
158         break;
159     case StateWidget::Combined:
160         typeStr = i18nc("combined state in statechart", "Combined");
161         break;
162     default:
163         typeStr = QString::fromLatin1("???");
164         break;
165     }
166     int row = 0;
167     Dialog_Utils::makeLabeledEditField(generalLayout, row++,
168                                        m_GenPageWidgets.typeL, i18n("State type:"),
169                                        m_GenPageWidgets.typeLE, typeStr);
170     m_GenPageWidgets.typeLE->setEnabled(false);
171 
172     Dialog_Utils::makeLabeledEditField(generalLayout, row++,
173                                        m_GenPageWidgets.nameL, i18n("State name:"),
174                                        m_GenPageWidgets.nameLE);
175 
176     if (type != StateWidget::Normal && type != StateWidget::Combined) {
177         m_GenPageWidgets.nameLE->setEnabled(false);
178         m_GenPageWidgets.nameLE->setText(QString());
179     } else
180         m_GenPageWidgets.nameLE->setText(m_pStateWidget->name());
181 
182     if (type == StateWidget::Combined) {
183         m_GenPageWidgets.diagramLinkWidget = new SelectDiagramWidget(i18n("Linked diagram:"), this);
184         m_GenPageWidgets.diagramLinkWidget->setupWidget(Uml::DiagramType::State,
185             m_pStateWidget->linkedDiagram() ? m_pStateWidget->linkedDiagram()->name() : QString(),
186             m_pStateWidget->umlScene()->name(), false);
187         m_GenPageWidgets.diagramLinkWidget->addToLayout(generalLayout, row++);
188     }
189 
190     m_GenPageWidgets.docWidget = new DocumentationWidget(m_pStateWidget);
191     generalLayout->addWidget(m_GenPageWidgets.docWidget, row, 0, 1, 2);
192     m_GenPageWidgets.nameLE->setFocus();
193 }
194 
195 /**
196  * Sets up the activity page.
197  */
setupActivityPage()198 void StateDialog::setupActivityPage()
199 {
200     m_pActivityPage = new ActivityPage(0, m_pStateWidget);
201     pageActivity = createPage(i18n("Activities"), i18n("Activities"),
202                               Icon_Utils::it_Properties_Activities, m_pActivityPage);
203 }
204 
205