Skip to content
Snippets Groups Projects
Commit 8563d032 authored by Dmitry Savchenko's avatar Dmitry Savchenko Committed by Eike Ziller
Browse files

Check keyword name before accepting in KeywordDialog in Todo plugin.


* Make sure keyword name is not already used in existing keywords.
* Make sure keyword name doesn't contain spaces or a colon.

Change-Id: I2856d122ba2fc6a7a60dc760dcd3d536523db5bc
Reviewed-by: default avatarEike Ziller <eike.ziller@nokia.com>
parent 71d9f33b
No related branches found
No related tags found
No related merge requests found
......@@ -41,14 +41,20 @@
namespace Todo {
namespace Internal {
KeywordDialog::KeywordDialog(const Keyword &keyword, QWidget *parent) :
KeywordDialog::KeywordDialog(const Keyword &keyword, const QSet<QString> &alreadyUsedKeywordNames,
QWidget *parent) :
QDialog(parent),
ui(new Ui::AddKeywordDialog)
ui(new Ui::KeywordDialog),
m_alreadyUsedKeywordNames(alreadyUsedKeywordNames)
{
ui->setupUi(this);
setupListWidget(keyword.iconResource);
setupColorWidgets(keyword.color);
ui->keywordNameEdit->setText(keyword.name);
ui->errorLabel->hide();
connect(ui->buttonBox, SIGNAL(accepted()), SLOT(acceptButtonClicked()));
connect(ui->keywordNameEdit, SIGNAL(textChanged(QString)), ui->errorLabel, SLOT(hide()));
}
KeywordDialog::~KeywordDialog()
......@@ -59,7 +65,7 @@ KeywordDialog::~KeywordDialog()
Keyword KeywordDialog::keyword()
{
Keyword result;
result.name = ui->keywordNameEdit->text();
result.name = keywordName();
result.iconResource = ui->listWidget->currentItem()->data(Qt::UserRole).toString();
result.color = ui->colorEdit->text();
......@@ -71,6 +77,12 @@ void KeywordDialog::colorSelected(const QColor &color)
ui->colorEdit->setText(color.name());
}
void KeywordDialog::acceptButtonClicked()
{
if (canAccept())
accept();
}
void KeywordDialog::setupListWidget(const QString &selectedIcon)
{
ui->listWidget->setViewMode(QListWidget::IconMode);
......@@ -109,5 +121,52 @@ void KeywordDialog::setupColorWidgets(const QColor &color)
connect(ui->colorButton, SIGNAL(colorChanged(QColor)), SLOT(colorSelected(QColor)));
}
bool KeywordDialog::canAccept()
{
if (!isKeywordNameCorrect()) {
showError(tr("Keyword cannot be empty, contain spaces or colons."));
return false;
}
if (isKeywordNameAlreadyUsed()) {
showError(tr("There is already a keyword with this name."));
return false;
}
return true;
}
bool KeywordDialog::isKeywordNameCorrect()
{
// Make sure keyword is not empty and contains no spaces or colons
QString name = keywordName();
if (name.isEmpty())
return false;
for (int i = 0; i < name.size(); ++i)
if (name.at(i).isSpace() || name.at(i) == QLatin1Char(':'))
return false;
return true;
}
bool KeywordDialog::isKeywordNameAlreadyUsed()
{
return m_alreadyUsedKeywordNames.contains(keywordName());
}
void KeywordDialog::showError(const QString &text)
{
ui->errorLabel->setText(text);
ui->errorLabel->show();
}
QString KeywordDialog::keywordName()
{
return ui->keywordNameEdit->text().trimmed();
}
} // namespace Internal
} // namespace Todo
......@@ -31,16 +31,17 @@
**
**************************************************************************/
#ifndef ADDKEYWORDDIALOG_H
#define ADDKEYWORDDIALOG_H
#ifndef KEYWORDDIALOG_H
#define KEYWORDDIALOG_H
#include <QDialog>
#include <QSet>
namespace Todo {
namespace Internal {
namespace Ui {
class AddKeywordDialog;
class KeywordDialog;
}
class Keyword;
......@@ -49,21 +50,30 @@ class KeywordDialog : public QDialog
{
Q_OBJECT
public:
KeywordDialog(const Keyword &keyword, QWidget *parent = 0);
KeywordDialog(const Keyword &keyword, const QSet<QString> &alreadyUsedKeywordNames,
QWidget *parent = 0);
~KeywordDialog();
Keyword keyword();
private slots:
void colorSelected(const QColor &color);
void acceptButtonClicked();
private:
void setupListWidget(const QString &selectedIcon);
void setupColorWidgets(const QColor &color);
Ui::AddKeywordDialog *ui;
bool canAccept();
bool isKeywordNameCorrect();
bool isKeywordNameAlreadyUsed();
void showError(const QString &text);
QString keywordName();
Ui::KeywordDialog *ui;
QSet<QString> m_alreadyUsedKeywordNames;
};
} // namespace Internal
} // namespace Todo
#endif // ADDKEYWORDDIALOG_H
#endif // KEYWORDDIALOG_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Todo::Internal::AddKeywordDialog</class>
<widget class="QDialog" name="Todo::Internal::AddKeywordDialog">
<class>Todo::Internal::KeywordDialog</class>
<widget class="QDialog" name="Todo::Internal::KeywordDialog">
<property name="geometry">
<rect>
<x>0</x>
......@@ -79,6 +79,16 @@
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="errorLabel">
<property name="styleSheet">
<string notr="true">color: red;</string>
</property>
<property name="text">
<string>errorLabel</string>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
......@@ -100,26 +110,10 @@
</customwidgets>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Todo::Internal::AddKeywordDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Todo::Internal::AddKeywordDialog</receiver>
<receiver>Todo::Internal::KeywordDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
......
......@@ -71,6 +71,17 @@ void OptionsDialog::addToKeywordsList(const Keyword &keyword)
ui->keywordsList->addItem(item);
}
QSet<QString> OptionsDialog::keywordNames()
{
KeywordList keywords = settingsFromUi().keywords;
QSet<QString> result;
foreach (const Keyword &keyword, keywords)
result << keyword.name;
return result;
}
Settings OptionsDialog::settings()
{
return settingsFromUi();
......@@ -79,9 +90,9 @@ Settings OptionsDialog::settings()
void OptionsDialog::addButtonClicked()
{
Keyword keyword;
KeywordDialog *addKeywordDialog = new KeywordDialog(keyword, this);
if (addKeywordDialog->exec() == QDialog::Accepted) {
keyword = addKeywordDialog->keyword();
KeywordDialog *keywordDialog = new KeywordDialog(keyword, keywordNames(), this);
if (keywordDialog->exec() == QDialog::Accepted) {
keyword = keywordDialog->keyword();
addToKeywordsList(keyword);
}
}
......@@ -95,9 +106,12 @@ void OptionsDialog::editButtonClicked()
keyword.iconResource = item->data(Qt::UserRole).toString();
keyword.color = item->backgroundColor();
KeywordDialog *addKeywordDialog = new KeywordDialog(keyword, this);
if (addKeywordDialog->exec() == QDialog::Accepted) {
keyword = addKeywordDialog->keyword();
QSet<QString> keywordNamesButThis = keywordNames();
keywordNamesButThis.remove(keyword.name);
KeywordDialog *keywordDialog = new KeywordDialog(keyword, keywordNamesButThis, this);
if (keywordDialog->exec() == QDialog::Accepted) {
keyword = keywordDialog->keyword();
item->setIcon(QIcon(keyword.iconResource));
item->setText(keyword.name);
item->setData(Qt::UserRole, keyword.iconResource);
......
......@@ -67,6 +67,7 @@ private:
void uiFromSettings(const Settings &settings);
Settings settingsFromUi();
void addToKeywordsList(const Keyword &keyword);
QSet<QString> keywordNames();
Ui::OptionsDialog *ui;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment