summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDmitry Savchenko <dmitry.a.sav@gmail.com>2012-03-15 18:51:52 +0300
committerEike Ziller <eike.ziller@nokia.com>2012-04-26 08:06:18 +0200
commit8563d032f7a38d14f2337ab3ccc3a1e1aa33d4b6 (patch)
tree64271f91304eb239949d195200896e31686093d0 /src
parent71d9f33bef895a4ba030ffb0cdadc1bd1d36fdd5 (diff)
downloadqt-creator-8563d032f7a38d14f2337ab3ccc3a1e1aa33d4b6.tar.gz
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>
Diffstat (limited to 'src')
-rw-r--r--src/plugins/todo/keyworddialog.cpp65
-rw-r--r--src/plugins/todo/keyworddialog.h22
-rw-r--r--src/plugins/todo/keyworddialog.ui32
-rw-r--r--src/plugins/todo/optionsdialog.cpp26
-rw-r--r--src/plugins/todo/optionsdialog.h1
5 files changed, 112 insertions, 34 deletions
diff --git a/src/plugins/todo/keyworddialog.cpp b/src/plugins/todo/keyworddialog.cpp
index 898ccdd403..6ec25477f0 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 d4226751e2..7db252613c 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 8d73589f12..13516dc99e 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>
@@ -80,6 +80,16 @@
</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">
<enum>Qt::Horizontal</enum>
@@ -102,24 +112,8 @@
<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 e6432967eb..5c9d643e79 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 5a876e8e62..6867de7c07 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;
};