BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
formsimulator.cpp
1 #include "gui/formsimulator.h"
2 
3 #include "gui/formmain.h"
4 #include "definitions/definitions.h"
5 #include "miscellaneous/iconfactory.h"
6 #include "miscellaneous/application.h"
7 #include "miscellaneous/skinfactory.h"
8 #include "core/templatesimulator.h"
9 #include "core/templatecore.h"
10 #include "core/templateeditor.h"
11 
12 #include <QWidget>
13 #include <QCloseEvent>
14 
15 
16 FormSimulator::FormSimulator(FormMain* parent)
17  : QDialog(parent), m_ui(new Ui::FormSimulator), m_mainWindow(parent), m_isActive(false), m_activeSimulation(NULL) {
18  m_ui->setupUi(this);
19 
20  // Load some needed settings.
21  m_isVisibleOnStartup = qApp->settings()->value(APP_CFG_SIMULATOR, "visible_on_startup", false).toBool();
22  m_isSticked = qApp->settings()->value(APP_CFG_SIMULATOR, "is_sticked", false).toBool();
23 
24  // Do necessary initializations.
25  setupIcons();
26  setupPhoneWidget();
27 
28  connect(parent, SIGNAL(moved()), this, SLOT(conditionallyAttachToParent()));
29  connect(parent, SIGNAL(resized()), this, SLOT(conditionallyAttachToParent()));
30  connect(m_ui->m_btnRunSimulation, SIGNAL(clicked()), this, SLOT(startSimulation()));
31  connect(m_ui->m_btnStopSimulation, SIGNAL(clicked()), this, SLOT(stopSimulation()));
32  connect(m_ui->m_btnGoBack, SIGNAL(clicked()), this, SLOT(goBack()));
33  connect(this, SIGNAL(stopEnableChanged(bool)), m_ui->m_btnStopSimulation, SLOT(setEnabled(bool)));
34 
35  // This window mustn't be deleted when closed by user.
36  setAttribute(Qt::WA_DeleteOnClose, false);
37  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
38 }
39 
40 FormSimulator::~FormSimulator() {
41  delete m_ui;
42 }
43 
45  qApp->settings()->setValue(APP_CFG_SIMULATOR, "visible_on_startup", m_isActive);
46 
47  if (!m_isSticked) {
48  qApp->settings()->setValue(APP_CFG_SIMULATOR, "position", pos());
49  qApp->settings()->setValue(APP_CFG_SIMULATOR, "height", height());
50  }
51 }
52 
54  if (!m_isSticked) {
55  move(qApp->settings()->value(APP_CFG_SIMULATOR, "position", pos()).toPoint());
56  resize(width(), qApp->settings()->value(APP_CFG_SIMULATOR, "height", height()).toInt());
57  }
58 }
59 
61  if (m_activeSimulation != NULL) {
62  // There is existing simulator widget, remove it.
63  m_activeSimulation->close();
64  m_activeSimulation->deleteLater();
65  m_activeSimulation = NULL;
66  }
67 
68  // Assign new simulation and display its initial state.
69  m_activeSimulation = simulation;
70  m_activeSimulation->setParent(m_ui->m_phoneWidget);
71  m_activeSimulation->setGeometry(SIMULATOR_CONTENTS_OFFSET_X, SIMULATOR_CONTENTS_OFFSET_Y,
72  SIMULATOR_CONTENTS_WIDTH, SIMULATOR_CONTENTS_HEIGHT);
73  m_activeSimulation->show();
74 
75  // Make necessary connections which are meant for simulator window.
76  connect(m_activeSimulation, SIGNAL(canGoBackChanged(bool)), m_ui->m_btnGoBack, SLOT(setEnabled(bool)));
77  connect(m_activeSimulation, SIGNAL(simulationStopRequested()), m_ui->m_btnStopSimulation, SLOT(click()));
78  connect(m_activeSimulation->core()->editor(), SIGNAL(canGenerateChanged(bool)), m_ui->m_btnRunSimulation, SLOT(setEnabled(bool)));
79 
80  emit stopEnableChanged(false);
81 }
82 
84  if (m_activeSimulation != NULL) {
85  m_activeSimulation->goBack();
86  }
87 }
88 
90  if (m_activeSimulation != NULL) {
91  if (m_activeSimulation->startSimulation()) {
92  emit stopEnableChanged(true);
93  }
94  }
95 }
96 
98  if (m_activeSimulation != NULL) {
99  if (m_activeSimulation->stopSimulation()) {
100  emit stopEnableChanged(false);
101  }
102  }
103 }
104 
106  if (m_isSticked) {
107  attachToParent();
108  }
109 }
110 
111 void FormSimulator::setIsSticked(bool is_sticked) {
112  if (is_sticked && !m_isSticked) {
113  attachToParent();
114  }
115  else if (!is_sticked && m_isSticked) {
117  }
118 
119  m_isSticked = is_sticked;
120 
121  qApp->settings()->setValue(APP_CFG_SIMULATOR, "is_sticked", is_sticked);
122 }
123 
125  m_isActive = true;
126 
128  QDialog::show();
129 }
130 
132  QPoint main_window_position = m_mainWindow->pos();
133  QSize main_window_size = m_mainWindow->size();
134 
135  setFixedHeight(main_window_size.height());
136  //resize(size().width(), main_window_size.height());
137 
138  move(main_window_position.x() + main_window_size.width() + SIMULATOR_OFFSET,
139  main_window_position.y());
140 }
141 
143  QPoint main_window_position = m_mainWindow->pos();
144  QSize main_window_size = m_mainWindow->size();
145 
146  setMinimumHeight(SIMULATOR_HEIGHT_MIN);
147  setMaximumHeight(SIMULATOR_HEIGHT_MAX);
148 
149  move(main_window_position.x() + main_window_size.width() + SIMULATOR_WIDTH_OFFSET,
150  main_window_position.y());
151 }
152 
153 void FormSimulator::setupPhoneWidget() {
154  m_ui->m_phoneWidget->setFixedSize(SIMULATOR_WIDTH, SIMULATOR_HEIGHT_DEFAULT);
155  m_ui->m_phoneWidget->setPixmap(QPixmap(qApp->skinFactory()->currentSkin().m_simulatorBackgroundMain));
156 
157  setFixedWidth(SIMULATOR_WIDTH + SIMULATOR_WIDTH_OFFSET);
158 }
159 
160 void FormSimulator::closeEvent(QCloseEvent *e) {
161  m_isActive = false;
162 
163  emit closed();
164  e->accept();
165 }
166 
167 void FormSimulator::setupIcons() {
168  IconFactory *factory = IconFactory::instance();
169 
170  setWindowIcon(factory->fromTheme("view-simulator"));
171  m_ui->m_btnStopSimulation->setIcon(factory->fromTheme("simulation-stop"));
172  m_ui->m_btnGoBack->setIcon(factory->fromTheme("simulation-back"));
173  m_ui->m_btnRunSimulation->setIcon(factory->fromTheme("simulation-run"));
174 }
void setActiveSimulation(TemplateSimulator *simulation)
Sets new active simulation widget.
void attachToParent()
Forces this window to align itself next to its parent window.
virtual bool stopSimulation()=0
Stops simulation and resets its to initial state, all user data from simulation are cleared...
void stopSimulation()
Stops active simulation.
Separate window which contains active simulator.
Definition: formsimulator.h:19
virtual TemplateEditor * editor() const
Access to editor widget of the template.
Definition: templatecore.h:84
Main application window.
Definition: formmain.h:53
void conditionallyAttachToParent()
Conditionally sticks simulator window to its parent.
virtual bool goBack()=0
Gets simulation one step back.
void show()
Displays simulator window.
void stopEnableChanged(bool enabled)
Emitted if "stop" feature for active simulation is changed, in other words, if simulation is started/...
Icon theme manipulator and provider.
Definition: iconfactory.h:47
TemplateCore * core() const
Access to associated template core.
void goBack()
Takes active simulation one step back.
void closed()
Emitted when simulator window is closed.
void closeEvent(QCloseEvent *e)
Executed when simulator window is closed.
void startSimulation()
Starts active simulation.
void setIsSticked(bool is_sticked)
Sticks or unsticks window.
void loadState()
Loads size & position for the window from settings.
void unAttachFromParent()
Allows window to be unattached and allows vertical resize policy.
void saveState()
Saves size & position for the window into settings.
QIcon fromTheme(const QString &name)
Returns icon from active theme.
Definition: iconfactory.h:58
virtual bool startSimulation()=0
(Re)starts the simulation.
static IconFactory * instance()
Singleton getter.
Definition: iconfactory.cpp:48
Base widget which represents simulator of the template.