diff --git a/src/plugins/todo/keyworddialog.cpp b/src/plugins/todo/keyworddialog.cpp index 898ccdd403e..6ec25477f0f 100644 --- a/src/plugins/todo/keyworddialog.cpp +++ b/src/plugins/todo/keyworddialog.cpp @@ -41,14 +41,20 @@ namespace Todo { namespace Internal { -KeywordDialog::KeywordDialog(const Keyword &keyword, QWidget *parent) : +KeywordDialog::KeywordDialog(const Keyword &keyword, const QSet &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 diff --git a/src/plugins/todo/keyworddialog.h b/src/plugins/todo/keyworddialog.h index d4226751e28..7db252613cc 100644 --- a/src/plugins/todo/keyworddialog.h +++ b/src/plugins/todo/keyworddialog.h @@ -31,16 +31,17 @@ ** **************************************************************************/ -#ifndef ADDKEYWORDDIALOG_H -#define ADDKEYWORDDIALOG_H +#ifndef KEYWORDDIALOG_H +#define KEYWORDDIALOG_H #include +#include 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 &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 m_alreadyUsedKeywordNames; }; } // namespace Internal } // namespace Todo -#endif // ADDKEYWORDDIALOG_H +#endif // KEYWORDDIALOG_H diff --git a/src/plugins/todo/keyworddialog.ui b/src/plugins/todo/keyworddialog.ui index 8d73589f128..13516dc99e0 100644 --- a/src/plugins/todo/keyworddialog.ui +++ b/src/plugins/todo/keyworddialog.ui @@ -1,7 +1,7 @@ - Todo::Internal::AddKeywordDialog - + Todo::Internal::KeywordDialog + 0 @@ -79,6 +79,16 @@ + + + + color: red; + + + errorLabel + + + @@ -100,26 +110,10 @@ - - buttonBox - accepted() - Todo::Internal::AddKeywordDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - buttonBox rejected() - Todo::Internal::AddKeywordDialog + Todo::Internal::KeywordDialog reject() diff --git a/src/plugins/todo/optionsdialog.cpp b/src/plugins/todo/optionsdialog.cpp index e6432967eb4..5c9d643e790 100644 --- a/src/plugins/todo/optionsdialog.cpp +++ b/src/plugins/todo/optionsdialog.cpp @@ -71,6 +71,17 @@ void OptionsDialog::addToKeywordsList(const Keyword &keyword) ui->keywordsList->addItem(item); } +QSet OptionsDialog::keywordNames() +{ + KeywordList keywords = settingsFromUi().keywords; + + QSet 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 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); diff --git a/src/plugins/todo/optionsdialog.h b/src/plugins/todo/optionsdialog.h index 5a876e8e626..6867de7c07b 100644 --- a/src/plugins/todo/optionsdialog.h +++ b/src/plugins/todo/optionsdialog.h @@ -67,6 +67,7 @@ private: void uiFromSettings(const Settings &settings); Settings settingsFromUi(); void addToKeywordsList(const Keyword &keyword); + QSet keywordNames(); Ui::OptionsDialog *ui; };