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 @@ ...@@ -41,14 +41,20 @@
namespace Todo { namespace Todo {
namespace Internal { namespace Internal {
KeywordDialog::KeywordDialog(const Keyword &keyword, QWidget *parent) : KeywordDialog::KeywordDialog(const Keyword &keyword, const QSet<QString> &alreadyUsedKeywordNames,
QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::AddKeywordDialog) ui(new Ui::KeywordDialog),
m_alreadyUsedKeywordNames(alreadyUsedKeywordNames)
{ {
ui->setupUi(this); ui->setupUi(this);
setupListWidget(keyword.iconResource); setupListWidget(keyword.iconResource);
setupColorWidgets(keyword.color); setupColorWidgets(keyword.color);
ui->keywordNameEdit->setText(keyword.name); 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() KeywordDialog::~KeywordDialog()
...@@ -59,7 +65,7 @@ KeywordDialog::~KeywordDialog() ...@@ -59,7 +65,7 @@ KeywordDialog::~KeywordDialog()
Keyword KeywordDialog::keyword() Keyword KeywordDialog::keyword()
{ {
Keyword result; Keyword result;
result.name = ui->keywordNameEdit->text(); result.name = keywordName();
result.iconResource = ui->listWidget->currentItem()->data(Qt::UserRole).toString(); result.iconResource = ui->listWidget->currentItem()->data(Qt::UserRole).toString();
result.color = ui->colorEdit->text(); result.color = ui->colorEdit->text();
...@@ -71,6 +77,12 @@ void KeywordDialog::colorSelected(const QColor &color) ...@@ -71,6 +77,12 @@ void KeywordDialog::colorSelected(const QColor &color)
ui->colorEdit->setText(color.name()); ui->colorEdit->setText(color.name());
} }
void KeywordDialog::acceptButtonClicked()
{
if (canAccept())
accept();
}
void KeywordDialog::setupListWidget(const QString &selectedIcon) void KeywordDialog::setupListWidget(const QString &selectedIcon)
{ {
ui->listWidget->setViewMode(QListWidget::IconMode); ui->listWidget->setViewMode(QListWidget::IconMode);
...@@ -109,5 +121,52 @@ void KeywordDialog::setupColorWidgets(const QColor &color) ...@@ -109,5 +121,52 @@ void KeywordDialog::setupColorWidgets(const QColor &color)
connect(ui->colorButton, SIGNAL(colorChanged(QColor)), SLOT(colorSelected(QColor))); 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 Internal
} // namespace Todo } // namespace Todo
...@@ -31,16 +31,17 @@ ...@@ -31,16 +31,17 @@
** **
**************************************************************************/ **************************************************************************/
#ifndef ADDKEYWORDDIALOG_H #ifndef KEYWORDDIALOG_H
#define ADDKEYWORDDIALOG_H #define KEYWORDDIALOG_H
#include <QDialog> #include <QDialog>
#include <QSet>
namespace Todo { namespace Todo {
namespace Internal { namespace Internal {
namespace Ui { namespace Ui {
class AddKeywordDialog; class KeywordDialog;
} }
class Keyword; class Keyword;
...@@ -49,21 +50,30 @@ class KeywordDialog : public QDialog ...@@ -49,21 +50,30 @@ class KeywordDialog : public QDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
KeywordDialog(const Keyword &keyword, QWidget *parent = 0); KeywordDialog(const Keyword &keyword, const QSet<QString> &alreadyUsedKeywordNames,
QWidget *parent = 0);
~KeywordDialog(); ~KeywordDialog();
Keyword keyword(); Keyword keyword();
private slots: private slots:
void colorSelected(const QColor &color); void colorSelected(const QColor &color);
void acceptButtonClicked();
private: private:
void setupListWidget(const QString &selectedIcon); void setupListWidget(const QString &selectedIcon);
void setupColorWidgets(const QColor &color); 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 Internal
} // namespace Todo } // namespace Todo
#endif // ADDKEYWORDDIALOG_H #endif // KEYWORDDIALOG_H
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>Todo::Internal::AddKeywordDialog</class> <class>Todo::Internal::KeywordDialog</class>
<widget class="QDialog" name="Todo::Internal::AddKeywordDialog"> <widget class="QDialog" name="Todo::Internal::KeywordDialog">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
...@@ -79,6 +79,16 @@ ...@@ -79,6 +79,16 @@
</item> </item>
</layout> </layout>
</item> </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> <item>
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation"> <property name="orientation">
...@@ -100,26 +110,10 @@ ...@@ -100,26 +110,10 @@
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections> <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> <connection>
<sender>buttonBox</sender> <sender>buttonBox</sender>
<signal>rejected()</signal> <signal>rejected()</signal>
<receiver>Todo::Internal::AddKeywordDialog</receiver> <receiver>Todo::Internal::KeywordDialog</receiver>
<slot>reject()</slot> <slot>reject()</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
......
...@@ -71,6 +71,17 @@ void OptionsDialog::addToKeywordsList(const Keyword &keyword) ...@@ -71,6 +71,17 @@ void OptionsDialog::addToKeywordsList(const Keyword &keyword)
ui->keywordsList->addItem(item); 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() Settings OptionsDialog::settings()
{ {
return settingsFromUi(); return settingsFromUi();
...@@ -79,9 +90,9 @@ Settings OptionsDialog::settings() ...@@ -79,9 +90,9 @@ Settings OptionsDialog::settings()
void OptionsDialog::addButtonClicked() void OptionsDialog::addButtonClicked()
{ {
Keyword keyword; Keyword keyword;
KeywordDialog *addKeywordDialog = new KeywordDialog(keyword, this); KeywordDialog *keywordDialog = new KeywordDialog(keyword, keywordNames(), this);
if (addKeywordDialog->exec() == QDialog::Accepted) { if (keywordDialog->exec() == QDialog::Accepted) {
keyword = addKeywordDialog->keyword(); keyword = keywordDialog->keyword();
addToKeywordsList(keyword); addToKeywordsList(keyword);
} }
} }
...@@ -95,9 +106,12 @@ void OptionsDialog::editButtonClicked() ...@@ -95,9 +106,12 @@ void OptionsDialog::editButtonClicked()
keyword.iconResource = item->data(Qt::UserRole).toString(); keyword.iconResource = item->data(Qt::UserRole).toString();
keyword.color = item->backgroundColor(); keyword.color = item->backgroundColor();
KeywordDialog *addKeywordDialog = new KeywordDialog(keyword, this); QSet<QString> keywordNamesButThis = keywordNames();
if (addKeywordDialog->exec() == QDialog::Accepted) { keywordNamesButThis.remove(keyword.name);
keyword = addKeywordDialog->keyword();
KeywordDialog *keywordDialog = new KeywordDialog(keyword, keywordNamesButThis, this);
if (keywordDialog->exec() == QDialog::Accepted) {
keyword = keywordDialog->keyword();
item->setIcon(QIcon(keyword.iconResource)); item->setIcon(QIcon(keyword.iconResource));
item->setText(keyword.name); item->setText(keyword.name);
item->setData(Qt::UserRole, keyword.iconResource); item->setData(Qt::UserRole, keyword.iconResource);
......
...@@ -67,6 +67,7 @@ private: ...@@ -67,6 +67,7 @@ private:
void uiFromSettings(const Settings &settings); void uiFromSettings(const Settings &settings);
Settings settingsFromUi(); Settings settingsFromUi();
void addToKeywordsList(const Keyword &keyword); void addToKeywordsList(const Keyword &keyword);
QSet<QString> keywordNames();
Ui::OptionsDialog *ui; 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