diff --git a/src/plugins/todo/keyworddialog.cpp b/src/plugins/todo/keyworddialog.cpp index 898ccdd403e331966a633996aef131f1e3fb1fe7..6ec25477f0f7112daf55d7c17ce5c2f2acea62fd 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<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 diff --git a/src/plugins/todo/keyworddialog.h b/src/plugins/todo/keyworddialog.h index d4226751e28ebd2eb282da6ba9b5732680c6129a..7db252613cceca868c4d8a784097674d32c5f1ad 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 <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 diff --git a/src/plugins/todo/keyworddialog.ui b/src/plugins/todo/keyworddialog.ui index 8d73589f1282222aec4b7e2c6a33854b67938e88..13516dc99e085d886418e4dca45ee258d4be2847 100644 --- a/src/plugins/todo/keyworddialog.ui +++ b/src/plugins/todo/keyworddialog.ui @@ -1,7 +1,7 @@ <?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"> diff --git a/src/plugins/todo/optionsdialog.cpp b/src/plugins/todo/optionsdialog.cpp index e6432967eb401174d02db9d7773e407e0a55c339..5c9d643e79009875065f6f2f5914a0fbce33609d 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<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); diff --git a/src/plugins/todo/optionsdialog.h b/src/plugins/todo/optionsdialog.h index 5a876e8e626b4596b8d9403aad0d881b2bddb11a..6867de7c07b0ddfedeac4afd09a148f7d497fbbd 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<QString> keywordNames(); Ui::OptionsDialog *ui; };