File indexing completed on 2024-05-12 16:46:11
0001 /*************************************************************************** 0002 Copyright (C) 2007-2009 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #include "fieldcomparison.h" 0026 #include "stringcomparison.h" 0027 #include "../field.h" 0028 #include "../collection.h" 0029 #include "../images/imagefactory.h" 0030 #include "../images/image.h" 0031 #include "../tellico_debug.h" 0032 0033 #include <QDateTime> 0034 0035 using namespace Tellico; 0036 0037 Tellico::FieldComparison* Tellico::FieldComparison::create(Data::FieldPtr field_) { 0038 if(!field_) { 0039 // myWarning() << "No field for creating a field comparison"; 0040 return nullptr; 0041 } 0042 if(field_->type() == Data::Field::Image) { 0043 return new ImageComparison(field_); 0044 } else if(field_->type() == Data::Field::Choice) { 0045 return new ChoiceComparison(field_); 0046 } 0047 return new ValueComparison(field_, StringComparison::create(field_)); 0048 } 0049 0050 Tellico::FieldComparison::FieldComparison(Data::FieldPtr field_) : m_field(field_) { 0051 } 0052 0053 int Tellico::FieldComparison::compare(Data::EntryPtr entry1_, Data::EntryPtr entry2_) { 0054 return compare(entry1_->formattedField(m_field), entry2_->formattedField(m_field)); 0055 } 0056 0057 Tellico::ValueComparison::ValueComparison(Data::FieldPtr field, StringComparison* comp) 0058 : FieldComparison(field) 0059 , m_stringComparison(comp) { 0060 Q_ASSERT(comp); 0061 } 0062 0063 Tellico::ValueComparison::~ValueComparison() { 0064 delete m_stringComparison; 0065 } 0066 0067 int Tellico::ValueComparison::compare(const QString& str1_, const QString& str2_) { 0068 return m_stringComparison->compare(str1_, str2_); 0069 } 0070 0071 Tellico::ImageComparison::ImageComparison(Data::FieldPtr field) : FieldComparison(field) { 0072 } 0073 0074 int Tellico::ImageComparison::compare(const QString& str1_, const QString& str2_) { 0075 if(str1_.isEmpty()) { 0076 if(str2_.isEmpty()) { 0077 return 0; 0078 } 0079 return -1; 0080 } 0081 if(str2_.isEmpty()) { 0082 return 1; 0083 } 0084 0085 const Data::Image& image1 = ImageFactory::imageById(str1_); 0086 const Data::Image& image2 = ImageFactory::imageById(str2_); 0087 if(image1.isNull()) { 0088 if(image2.isNull()) { 0089 return 0; 0090 } 0091 return -1; 0092 } 0093 if(image2.isNull()) { 0094 return 1; 0095 } 0096 // large images come first 0097 return image1.width() - image2.width(); 0098 } 0099 0100 Tellico::ChoiceComparison::ChoiceComparison(Data::FieldPtr field) : FieldComparison(field) { 0101 m_values = field->allowed(); 0102 } 0103 0104 int Tellico::ChoiceComparison::compare(const QString& str1, const QString& str2) { 0105 return m_values.indexOf(str1) - m_values.indexOf(str2); 0106 }