31 #include "templates/learnspellings/learnspellingseditor.h"
33 #include "miscellaneous/iconfactory.h"
34 #include "miscellaneous/application.h"
35 #include "core/templatefactory.h"
36 #include "core/templatecore.h"
37 #include "core/templateentrypoint.h"
42 LearnSpellingsEditor::LearnSpellingsEditor(
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->lineEdit() <<
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));
67 m_ui->m_txtTitle->lineEdit()->setPlaceholderText(tr(
"Word to spell"));
68 m_ui->m_txtDescription->lineEdit()->setPlaceholderText(tr(
"Meaning of the word"));
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_txtDescription->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(checkDescription(QString)));
81 connect(m_ui->m_txtTitle->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(checkTitle(QString)));
82 connect(m_ui->m_btnItemAdd, SIGNAL(clicked()),
this, SLOT(addSampleWord()));
83 connect(m_ui->m_btnItemRemove, SIGNAL(clicked()),
this, SLOT(removeSelectedWord()));
84 connect(m_ui->m_txtDescription->lineEdit(), SIGNAL(textEdited(QString)),
this, SLOT(saveWord()));
85 connect(m_ui->m_txtTitle->lineEdit(), SIGNAL(textEdited(QString)),
this, SLOT(saveWord()));
86 connect(m_ui->m_listItems, SIGNAL(currentRowChanged(
int)),
this, SLOT(displayWord(
int)));
87 connect(m_ui->m_btnItemUp, SIGNAL(clicked()),
this, SLOT(moveWordUp()));
88 connect(m_ui->m_btnItemDown, SIGNAL(clicked()),
this, SLOT(moveWordDown()));
89 connect(m_ui->m_txtAuthor->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(onAuthorChanged(QString)));
90 connect(m_ui->m_txtName->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(onNameChanged(QString)));
92 checkDescription(m_ui->m_txtDescription->lineEdit()->text());
93 checkTitle(m_ui->m_txtTitle->lineEdit()->text());
96 setEditorsEnabled(
false);
100 LearnSpellingsEditor::~LearnSpellingsEditor() {
109 QDomDocument source_document = qApp->templateManager()->generateBundleHeader(
core()->entryPoint()->typeIndentifier(),
110 m_ui->m_txtAuthor->lineEdit()->text(),
112 m_ui->m_txtName->lineEdit()->text(),
115 FIND_DATA_ELEMENT(data_element, source_document);
118 QDomElement item_element = source_document.createElement(
"item");
121 QDomElement word_element = source_document.createElement(
"word");
122 QDomElement meaning_element = source_document.createElement(
"meaning");
124 word_element.appendChild(source_document.createTextNode(item.word()));
125 meaning_element.appendChild(source_document.createTextNode(item.meaning()));
127 item_element.appendChild(word_element);
128 item_element.appendChild(meaning_element);
130 data_element.appendChild(item_element);
133 return source_document.toString(XML_BUNDLE_INDENTATION);
137 QDomDocument bundle_document;
138 bundle_document.setContent(bundle_data);
140 QDomNodeList items = bundle_document.documentElement().elementsByTagName(
"item");
142 for (
int i = 0; i < items.size(); i++) {
143 QDomNode item = items.at(i);
145 if (item.isElement()) {
146 QString word = item.namedItem(
"word").toElement().text();
147 QString meaning = item.namedItem(
"meaning").toElement().text();
149 if (word.isEmpty()) {
154 addSampleWord(word, meaning);
163 m_ui->m_txtAuthor->lineEdit()->setText(bundle_document.documentElement().namedItem(
"author").namedItem(
"name").toElement().text());
164 m_ui->m_txtName->lineEdit()->setText(bundle_document.documentElement().namedItem(
"title").toElement().text());
169 void LearnSpellingsEditor::addSampleWord(
const QString &title,
const QString &description) {
170 int marked_item = m_ui->m_listItems->currentRow();
172 QListWidgetItem *new_item_view =
new QListWidgetItem();
174 new_item.setWord(title);
175 new_item.setMeaning(description);
177 new_item_view->setText(new_item.word());
178 new_item_view->setData(Qt::UserRole, QVariant::fromValue(new_item));
180 if (m_ui->m_listItems->count() == 0) {
182 setEditorsEnabled(
true);
184 m_ui->m_btnItemRemove->setEnabled(
true);
186 m_ui->m_listItems->insertItem(0, new_item_view);
187 m_ui->m_listItems->setCurrentRow(0);
190 m_ui->m_listItems->insertItem(marked_item + 1, new_item_view);
191 m_ui->m_listItems->setCurrentRow(marked_item + 1);
197 void LearnSpellingsEditor::addSampleWord() {
198 addSampleWord(tr(
"cat"), tr(
"Cats are animals which are hated by dogs."));
203 void LearnSpellingsEditor::checkAuthor() {
204 if (m_ui->m_txtAuthor->lineEdit()->text().isEmpty()) {
205 m_ui->m_txtAuthor->setStatus(WidgetWithStatus::Error,
206 tr(
"No author is specified."));
209 m_ui->m_txtAuthor->setStatus(WidgetWithStatus::Ok,
210 tr(
"Author is specified."));
214 void LearnSpellingsEditor::checkName() {
215 if (m_ui->m_txtName->lineEdit()->text().isEmpty()) {
216 m_ui->m_txtName->setStatus(WidgetWithStatus::Error,
217 tr(
"No collection title is specified."));
220 m_ui->m_txtName->setStatus(WidgetWithStatus::Ok,
221 tr(
"Collection title is specified."));
225 void LearnSpellingsEditor::onAuthorChanged(
const QString& new_author) {
244 if (m_ui->m_listItems->count() > 1) {
245 int index = m_ui->m_listItems->currentRow();
248 m_ui->m_btnItemUp->setEnabled(
false);
249 m_ui->m_btnItemDown->setEnabled(
true);
251 else if (index == m_ui->m_listItems->count() - 1) {
252 m_ui->m_btnItemUp->setEnabled(
true);
253 m_ui->m_btnItemDown->setEnabled(
false);
256 m_ui->m_btnItemUp->setEnabled(
true);
257 m_ui->m_btnItemDown->setEnabled(
true);
261 m_ui->m_btnItemUp->setEnabled(
false);
262 m_ui->m_btnItemDown->setEnabled(
false);
266 QList<LearnSpellingsItem> LearnSpellingsEditor::activeWords()
const {
267 QList<LearnSpellingsItem> questions;
269 for (
int i = 0; i < m_ui->m_listItems->count(); i++) {
270 questions.append(m_ui->m_listItems->item(i)->data(Qt::UserRole).value<
LearnSpellingsItem>());
278 !activeWords().isEmpty() &&
279 !m_ui->m_txtAuthor->lineEdit()->text().simplified().isEmpty() &&
280 !m_ui->m_txtName->lineEdit()->text().simplified().isEmpty();
284 return m_ui->m_txtName->lineEdit()->text();
288 return m_ui->m_txtAuthor->lineEdit()->text();
291 void LearnSpellingsEditor::updateItemCount() {
292 m_ui->m_txtNumberOfItems->lineEdit()->setText(QString::number(m_ui->m_listItems->count()));
294 if (m_ui->m_listItems->count() > 0) {
295 m_ui->m_txtNumberOfItems->setStatus(WidgetWithStatus::Ok, tr(
"Collection contains at least one word."));
298 m_ui->m_txtNumberOfItems->setStatus(WidgetWithStatus::Error, tr(
"Collection does not contain any words."));
302 void LearnSpellingsEditor::removeSelectedWord() {
303 int current_row = m_ui->m_listItems->currentRow();
305 if (current_row >= 0) {
306 if (m_ui->m_listItems->count() == 1) {
308 setEditorsEnabled(
false);
310 m_ui->m_btnItemRemove->setEnabled(
false);
313 delete m_ui->m_listItems->takeItem(current_row);
321 void LearnSpellingsEditor::saveWord() {
322 m_activeItem.setWord(m_ui->m_txtTitle->lineEdit()->text());
323 m_activeItem.setMeaning(m_ui->m_txtDescription->lineEdit()->text());
325 m_ui->m_listItems->currentItem()->setData(Qt::UserRole, QVariant::fromValue(m_activeItem));
326 m_ui->m_listItems->currentItem()->setText(m_activeItem.word());
331 void LearnSpellingsEditor::displayWord(
int index) {
335 m_ui->m_txtTitle->lineEdit()->setText(item.word());
336 m_ui->m_txtDescription->lineEdit()->setText(item.meaning());
340 m_ui->m_txtTitle->lineEdit()->clear();
341 m_ui->m_txtDescription->lineEdit()->clear();
344 QTimer::singleShot(0,
this, SLOT(configureUpDown()));
347 void LearnSpellingsEditor::checkTitle(
const QString &title) {
348 if (title.simplified().isEmpty()) {
349 m_ui->m_txtTitle->setStatus(WidgetWithStatus::Error, tr(
"Please, enter some word."));
352 m_ui->m_txtTitle->setStatus(WidgetWithStatus::Ok, tr(
"Word seems to be okay."));
356 void LearnSpellingsEditor::moveWordUp() {
357 int index = m_ui->m_listItems->currentRow();
359 m_ui->m_listItems->insertItem(index - 1, m_ui->m_listItems->takeItem(index));
360 m_ui->m_listItems->setCurrentRow(index - 1);
365 void LearnSpellingsEditor::moveWordDown() {
366 int index = m_ui->m_listItems->currentRow();
368 m_ui->m_listItems->insertItem(index + 1, m_ui->m_listItems->takeItem(index));
369 m_ui->m_listItems->setCurrentRow(index + 1);
374 void LearnSpellingsEditor::checkDescription(
const QString &description) {
375 if (description.simplified().isEmpty()) {
376 m_ui->m_txtDescription->setStatus(WidgetWithStatus::Error, tr(
"Please, enter some meaning."));
379 m_ui->m_txtDescription->setStatus(WidgetWithStatus::Ok, tr(
"Meaning seems to be okay."));
383 void LearnSpellingsEditor::setEditorsEnabled(
bool enabled) {
384 m_ui->m_groupItemEditor->setEnabled(enabled);
Editor widget of Learn Spellings template.
QString generateBundleData()
Generates RAW data which represent data of this template.
TemplateCore * core() const
Access to associated template core.
void changed()
Emitted everytime any child widget of editor changes its contents.
bool canGenerateApplications()
Specifies if template can generate applications or not.
Represents the editor of the template.
Icon theme manipulator and provider.
bool loadBundleData(const QString &bundle_data)
Loads editor state from XML bundle.
QString projectName()
Access to project name of current editor.
The core class container for single template.
QIcon fromTheme(const QString &name)
Returns icon from active theme.
Class which represents single word for Learn Spellings template.
virtual void launch()
Executed when given template with this editor is launched.
static IconFactory * instance()
Singleton getter.
QString authorName()
Access to author name of current editor.