31 #include "templates/mlearning/basicmlearningeditor.h"
33 #include "miscellaneous/application.h"
34 #include "miscellaneous/iconfactory.h"
35 #include "core/templatefactory.h"
36 #include "core/templatecore.h"
37 #include "core/templateentrypoint.h"
42 BasicmLearningEditor::BasicmLearningEditor(
TemplateCore *core, QWidget *parent)
47 QRegExpValidator *author_validator =
new QRegExpValidator(
this);
48 QRegExpValidator *title_validator =
new QRegExpValidator(
this);
50 author_validator->setRegExp(QRegExp(
".{,50}"));
51 title_validator->setRegExp(QRegExp(
".{,100}"));
53 m_ui->m_txtAuthor->lineEdit()->setValidator(author_validator);
54 m_ui->m_txtName->lineEdit()->setValidator(title_validator);
57 QList<QWidget*> tab_order_widgets;
58 tab_order_widgets << m_ui->m_txtTitle->lineEdit() << m_ui->m_txtDescription <<
59 m_ui->m_txtAuthor->lineEdit() << m_ui->m_txtName->lineEdit() <<
60 m_ui->m_listItems << m_ui->m_btnItemAdd << m_ui->m_btnItemRemove <<
61 m_ui->m_btnItemUp << m_ui->m_btnItemDown;
63 for (
int i = 1; i < tab_order_widgets.size(); i++) {
64 setTabOrder(tab_order_widgets.at(i - 1), tab_order_widgets.at(i));
68 m_ui->m_txtTitle->lineEdit()->setPlaceholderText(tr(
"Title of the item"));
69 m_ui->m_txtNumberOfItems->lineEdit()->setEnabled(
false);
70 m_ui->m_txtAuthor->lineEdit()->setPlaceholderText(tr(
"Author of this collection"));
71 m_ui->m_txtName->lineEdit()->setPlaceholderText(tr(
"Name of this collection"));
75 m_ui->m_btnItemAdd->setIcon(factory->
fromTheme(
"item-add"));
76 m_ui->m_btnItemRemove->setIcon(factory->
fromTheme(
"item-remove"));
77 m_ui->m_btnItemUp->setIcon(factory->
fromTheme(
"move-up"));
78 m_ui->m_btnItemDown->setIcon(factory->
fromTheme(
"move-down"));
80 connect(m_ui->m_txtTitle->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(checkTitle(QString)));
81 connect(m_ui->m_btnItemAdd, SIGNAL(clicked()),
this, SLOT(addNewItem()));
82 connect(m_ui->m_btnItemRemove, SIGNAL(clicked()),
this, SLOT(removeSelectedItem()));
83 connect(m_ui->m_txtDescription, SIGNAL(textChanged()),
this, SLOT(saveItem()));
84 connect(m_ui->m_txtTitle->lineEdit(), SIGNAL(textEdited(QString)),
this, SLOT(saveItem()));
85 connect(m_ui->m_listItems, SIGNAL(currentRowChanged(
int)),
this, SLOT(displayItem(
int)));
86 connect(m_ui->m_btnItemUp, SIGNAL(clicked()),
this, SLOT(moveItemUp()));
87 connect(m_ui->m_btnItemDown, SIGNAL(clicked()),
this, SLOT(moveItemDown()));
88 connect(m_ui->m_txtAuthor->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(onAuthorChanged(QString)));
89 connect(m_ui->m_txtName->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(onNameChanged(QString)));
91 checkTitle(m_ui->m_txtTitle->lineEdit()->text());
94 setEditorsEnabled(
false);
98 BasicmLearningEditor::~BasicmLearningEditor() {
103 QDomDocument bundle_document;
104 bundle_document.setContent(bundle_data);
106 QDomNodeList items = bundle_document.documentElement().elementsByTagName(
"item");
108 for (
int i = 0; i < items.size(); i++) {
109 QDomNode item = items.at(i);
111 if (item.isElement()) {
112 QString title = item.namedItem(
"item_title").toElement().text();
113 QString description = item.namedItem(
"item_description").toElement().text();
115 if (title.isEmpty() || description.isEmpty()) {
120 addNewItem(title, description);
129 m_ui->m_txtAuthor->lineEdit()->setText(bundle_document.documentElement().namedItem(
"author").namedItem(
"name").toElement().text());
130 m_ui->m_txtName->lineEdit()->setText(bundle_document.documentElement().namedItem(
"title").toElement().text());
136 return m_ui->m_txtName->lineEdit()->text();
140 return m_ui->m_txtAuthor->lineEdit()->text();
143 void BasicmLearningEditor::addNewItem(
const QString &title,
const QString &description) {
144 int marked_item = m_ui->m_listItems->currentRow();
146 QListWidgetItem *new_item_view =
new QListWidgetItem();
148 new_item.setTitle(title);
149 new_item.setDescription(description);
151 new_item_view->setText(new_item.title());
152 new_item_view->setData(Qt::UserRole, QVariant::fromValue(new_item));
154 if (m_ui->m_listItems->count() == 0) {
156 setEditorsEnabled(
true);
158 m_ui->m_btnItemRemove->setEnabled(
true);
160 m_ui->m_listItems->insertItem(0, new_item_view);
161 m_ui->m_listItems->setCurrentRow(0);
164 m_ui->m_listItems->insertItem(marked_item + 1, new_item_view);
165 m_ui->m_listItems->setCurrentRow(marked_item + 1);
171 void BasicmLearningEditor::addNewItem() {
172 addNewItem(tr(
"Prague"), tr(
"Prague is the city which lies in the heart of Europe."));
177 void BasicmLearningEditor::checkAuthor() {
178 if (m_ui->m_txtAuthor->lineEdit()->text().isEmpty()) {
179 m_ui->m_txtAuthor->setStatus(WidgetWithStatus::Error,
180 tr(
"No author is specified."));
183 m_ui->m_txtAuthor->setStatus(WidgetWithStatus::Ok,
184 tr(
"Author is specified."));
188 void BasicmLearningEditor::checkName() {
189 if (m_ui->m_txtName->lineEdit()->text().isEmpty()) {
190 m_ui->m_txtName->setStatus(WidgetWithStatus::Error,
191 tr(
"No collection title is specified."));
194 m_ui->m_txtName->setStatus(WidgetWithStatus::Ok,
195 tr(
"Collection title is specified."));
199 void BasicmLearningEditor::onAuthorChanged(
const QString& new_author) {
218 if (m_ui->m_listItems->count() > 1) {
219 int index = m_ui->m_listItems->currentRow();
222 m_ui->m_btnItemUp->setEnabled(
false);
223 m_ui->m_btnItemDown->setEnabled(
true);
225 else if (index == m_ui->m_listItems->count() - 1) {
226 m_ui->m_btnItemUp->setEnabled(
true);
227 m_ui->m_btnItemDown->setEnabled(
false);
230 m_ui->m_btnItemUp->setEnabled(
true);
231 m_ui->m_btnItemDown->setEnabled(
true);
235 m_ui->m_btnItemUp->setEnabled(
false);
236 m_ui->m_btnItemDown->setEnabled(
false);
240 QList<BasicmLearningItem> BasicmLearningEditor::activeItems()
const {
241 QList<BasicmLearningItem> questions;
243 for (
int i = 0; i < m_ui->m_listItems->count(); i++) {
244 questions.append(m_ui->m_listItems->item(i)->data(Qt::UserRole).value<
BasicmLearningItem>());
251 return !activeItems().isEmpty();
259 QDomDocument source_document = qApp->templateManager()->generateBundleHeader(
core()->entryPoint()->typeIndentifier(),
260 m_ui->m_txtAuthor->lineEdit()->text(),
262 m_ui->m_txtName->lineEdit()->text(),
265 FIND_DATA_ELEMENT(data_element, source_document);
268 QDomElement item_element = source_document.createElement(
"item");
271 QDomElement title_element = source_document.createElement(
"item_title");
272 QDomElement description_element = source_document.createElement(
"item_description");
274 title_element.appendChild(source_document.createTextNode(item.title()));
275 description_element.appendChild(source_document.createTextNode(item.description()));
277 item_element.appendChild(title_element);
278 item_element.appendChild(description_element);
280 data_element.appendChild(item_element);
283 return source_document.toString(XML_BUNDLE_INDENTATION);
286 void BasicmLearningEditor::updateItemCount() {
287 m_ui->m_txtNumberOfItems->lineEdit()->setText(QString::number(m_ui->m_listItems->count()));
289 if (m_ui->m_listItems->count() > 0) {
290 m_ui->m_txtNumberOfItems->setStatus(WidgetWithStatus::Ok, tr(
"Collection contains at least one item."));
293 m_ui->m_txtNumberOfItems->setStatus(WidgetWithStatus::Error, tr(
"Collection does not contain any items."));
297 void BasicmLearningEditor::removeSelectedItem() {
298 int current_row = m_ui->m_listItems->currentRow();
300 if (current_row >= 0) {
301 if (m_ui->m_listItems->count() == 1) {
303 setEditorsEnabled(
false);
305 m_ui->m_btnItemRemove->setEnabled(
false);
308 delete m_ui->m_listItems->takeItem(current_row);
316 void BasicmLearningEditor::saveItem() {
317 m_activeItem.setTitle(m_ui->m_txtTitle->lineEdit()->text());
318 m_activeItem.setDescription(m_ui->m_txtDescription->toPlainText());
320 m_ui->m_listItems->currentItem()->setData(Qt::UserRole, QVariant::fromValue(m_activeItem));
321 m_ui->m_listItems->currentItem()->setText(m_activeItem.title());
326 void BasicmLearningEditor::displayItem(
int index) {
330 m_ui->m_txtTitle->lineEdit()->setText(item.title());
331 m_ui->m_txtDescription->setText(item.description());
335 m_ui->m_txtTitle->lineEdit()->clear();
336 m_ui->m_txtDescription->clear();
339 QTimer::singleShot(0,
this, SLOT(configureUpDown()));
342 void BasicmLearningEditor::checkTitle(
const QString &title) {
343 if (title.simplified().isEmpty()) {
344 m_ui->m_txtTitle->setStatus(WidgetWithStatus::Error, tr(
"Please, enter some title."));
347 m_ui->m_txtTitle->setStatus(WidgetWithStatus::Ok, tr(
"Title seems to be okay."));
351 void BasicmLearningEditor::moveItemUp() {
352 int index = m_ui->m_listItems->currentRow();
354 m_ui->m_listItems->insertItem(index - 1, m_ui->m_listItems->takeItem(index));
355 m_ui->m_listItems->setCurrentRow(index - 1);
360 void BasicmLearningEditor::moveItemDown() {
361 int index = m_ui->m_listItems->currentRow();
363 m_ui->m_listItems->insertItem(index + 1, m_ui->m_listItems->takeItem(index));
364 m_ui->m_listItems->setCurrentRow(index + 1);
369 void BasicmLearningEditor::setEditorsEnabled(
bool enabled) {
370 m_ui->m_groupItemEditor->setEnabled(enabled);
TemplateCore * core() const
Access to associated template core.
void changed()
Emitted everytime any child widget of editor changes its contents.
Represents the editor of the template.
QString authorName()
Access to author name of current editor.
bool canGenerateApplications()
Specifies if template can generate applications or not.
QString generateBundleData()
Generates RAW data which represent data of this template.
bool loadBundleData(const QString &bundle_data)
Loads editor state from XML bundle.
Icon theme manipulator and provider.
The core class container for single template.
QString projectName()
Access to project name of current editor.
QIcon fromTheme(const QString &name)
Returns icon from active theme.
virtual void launch()
Executed when given template with this editor is launched.
static IconFactory * instance()
Singleton getter.