File indexing completed on 2024-05-12 05:09:52

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 "../images/imagefactory.h"
0029 #include "../images/image.h"
0030 
0031 #include <QDateTime>
0032 
0033 using namespace Tellico;
0034 
0035 Tellico::FieldComparison* Tellico::FieldComparison::create(Data::FieldPtr field_) {
0036   if(!field_) {
0037     return nullptr;
0038   }
0039   if(field_->type() == Data::Field::Image) {
0040     return new ImageComparison(field_);
0041   } else if(field_->type() == Data::Field::Choice) {
0042     return new ChoiceComparison(field_);
0043   }
0044   return new ValueComparison(field_, StringComparison::create(field_));
0045 }
0046 
0047 Tellico::FieldComparison::FieldComparison(Data::FieldPtr field_) : m_field(field_) {
0048 }
0049 
0050 int Tellico::FieldComparison::compare(Data::EntryPtr entry1_, Data::EntryPtr entry2_) {
0051   return compare(entry1_->formattedField(m_field), entry2_->formattedField(m_field));
0052 }
0053 
0054 Tellico::ValueComparison::ValueComparison(Data::FieldPtr field, StringComparison* comp)
0055     : FieldComparison(field)
0056     , m_stringComparison(comp) {
0057   Q_ASSERT(comp);
0058 }
0059 
0060 Tellico::ValueComparison::~ValueComparison() {
0061   delete m_stringComparison;
0062 }
0063 
0064 int Tellico::ValueComparison::compare(const QString& str1_, const QString& str2_) {
0065   return m_stringComparison->compare(str1_, str2_);
0066 }
0067 
0068 Tellico::ImageComparison::ImageComparison(Data::FieldPtr field) : FieldComparison(field) {
0069 }
0070 
0071 int Tellico::ImageComparison::compare(const QString& str1_, const QString& str2_) {
0072   if(str1_.isEmpty()) {
0073     if(str2_.isEmpty()) {
0074       return 0;
0075     }
0076     return -1;
0077   }
0078   if(str2_.isEmpty()) {
0079     return 1;
0080   }
0081 
0082   const Data::Image& image1 = ImageFactory::imageById(str1_);
0083   const Data::Image& image2 = ImageFactory::imageById(str2_);
0084   if(image1.isNull()) {
0085     if(image2.isNull()) {
0086       return 0;
0087     }
0088     return -1;
0089   }
0090   if(image2.isNull()) {
0091     return 1;
0092   }
0093   // large images come first
0094   return image1.width() - image2.width();
0095 }
0096 
0097 Tellico::ChoiceComparison::ChoiceComparison(Data::FieldPtr field) : FieldComparison(field) {
0098   m_values = field->allowed();
0099 }
0100 
0101 int Tellico::ChoiceComparison::compare(const QString& str1, const QString& str2) {
0102   return m_values.indexOf(str1) - m_values.indexOf(str2);
0103 }