1 | /* |
---|
2 | * BCValidationResult.cpp |
---|
3 | * |
---|
4 | * Created on: Dec 18, 2009 |
---|
5 | * Author: saemy |
---|
6 | */ |
---|
7 | |
---|
8 | #include "svalidationresult.h" |
---|
9 | |
---|
10 | #include <QListIterator> |
---|
11 | |
---|
12 | /** |
---|
13 | * If the given message is not empty, it is added as an error |
---|
14 | * otherwise the result will be initialized as valid. |
---|
15 | */ |
---|
16 | SValidationResult::SValidationResult(const QString& message /* = "" */) { |
---|
17 | if (message.trimmed() != "") { |
---|
18 | addError(message); |
---|
19 | } |
---|
20 | } |
---|
21 | |
---|
22 | void SValidationResult::addError(const QString& message) { |
---|
23 | if (message.trimmed() != "") { |
---|
24 | errors_.append(message); |
---|
25 | } |
---|
26 | } |
---|
27 | void SValidationResult::addWarning(const QString& message) { |
---|
28 | if (message.trimmed() != "") { |
---|
29 | warnings_.append(message); |
---|
30 | } |
---|
31 | } |
---|
32 | |
---|
33 | ValidationType SValidationResult::validationType() const { |
---|
34 | if (hasWarnings()) { |
---|
35 | return Warning; |
---|
36 | } |
---|
37 | if (hasErrors()) { |
---|
38 | return Error; |
---|
39 | } |
---|
40 | |
---|
41 | return Valid; |
---|
42 | } |
---|
43 | |
---|
44 | QList<QString> SValidationResult::errors() const { |
---|
45 | return errors_; |
---|
46 | } |
---|
47 | QList<QString> SValidationResult::warnings() const { |
---|
48 | return warnings_; |
---|
49 | } |
---|
50 | |
---|
51 | bool SValidationResult::hasErrors() const { |
---|
52 | return !errors().isEmpty(); |
---|
53 | } |
---|
54 | bool SValidationResult::hasWarnings() const { |
---|
55 | return !warnings().isEmpty(); |
---|
56 | } |
---|
57 | bool SValidationResult::isValid() const { |
---|
58 | return !hasErrors() && !hasWarnings(); |
---|
59 | } |
---|
60 | |
---|
61 | QString SValidationResult::getErrorString(const QString& delimiter /* = "\n" */) const { |
---|
62 | return getStringFromList(errors(), delimiter); |
---|
63 | } |
---|
64 | QString SValidationResult::getWarningString(const QString& delimiter /* = "\n" */) const { |
---|
65 | return getStringFromList(errors(), delimiter); |
---|
66 | } |
---|
67 | QString SValidationResult::getStringFromList(const QList<QString> list, const QString& delimiter) const { |
---|
68 | QString messages = ""; |
---|
69 | |
---|
70 | QListIterator<QString> i(list); |
---|
71 | while (i.hasNext()) { |
---|
72 | messages += i.next() + delimiter; |
---|
73 | } |
---|
74 | |
---|
75 | return messages.left(messages.length() - delimiter.length()); |
---|
76 | } |
---|
77 | |
---|