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: Eike Ziller <eike.ziller@nokia.com>
This commit is contained in:
Dmitry Savchenko
2012-03-15 18:51:52 +03:00
committed by Eike Ziller
parent 71d9f33bef
commit 8563d032f7
5 changed files with 112 additions and 34 deletions

View File

@@ -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