File indexing completed on 2024-05-05 04:49:27

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Daniel Caleb Jones <danielcjones@gmail.com>                       *
0003  * Copyright (c) 2009 Mark Kretschmann <kretschmann@kde.org>                            *
0004  * Copyright (c) 2010 Ralf Engels <ralf-engels@gmx.de>                                  *
0005  *                                                                                      *
0006  * This program is free software; you can redistribute it and/or modify it under        *
0007  * the terms of the GNU General Public License as published by the Free Software        *
0008  * Foundation; either version 2 of the License, or (at your option) version 3 or        *
0009  * any later version accepted by the membership of KDE e.V. (or its successor approved  *
0010  * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of  *
0011  * version 3 of the license.                                                            *
0012  *                                                                                      *
0013  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0016  *                                                                                      *
0017  * You should have received a copy of the GNU General Public License along with         *
0018  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0019  ****************************************************************************************/
0020 
0021 #ifndef AMAROK_METAQUERYWIDGET_H
0022 #define AMAROK_METAQUERYWIDGET_H
0023 
0024 #include <QWidget>
0025 #include <QPointer>
0026 #include "core/meta/forward_declarations.h"
0027 #include "core/meta/support/MetaConstants.h"
0028 
0029 #include <KComboBox>
0030 #include <QSpinBox>
0031 
0032 class QHBoxLayout;
0033 class QLabel;
0034 class QVBoxLayout;
0035 
0036 namespace Collections
0037 {
0038     class QueryMaker;
0039 }
0040 
0041 /**
0042  *  A class that allows to select a time distance.
0043  */
0044 class TimeDistanceWidget : public QWidget
0045 {
0046     Q_OBJECT
0047 
0048 public:
0049     explicit TimeDistanceWidget( QWidget *parent = nullptr );
0050     qint64 timeDistance() const;
0051     void setTimeDistance( qint64 value );
0052 
0053     template<class Receiver, class Func>
0054     void connectChanged( Receiver receiver, Func slot )
0055     {
0056         connect( m_timeEdit, QOverload<int>::of(&QSpinBox::valueChanged),
0057                  receiver, slot );
0058         connect( m_unitSelection, QOverload<int>::of(&QComboBox::currentIndexChanged),
0059                  receiver, slot );
0060     }
0061 
0062 protected:
0063     QSpinBox *m_timeEdit;
0064     QComboBox *m_unitSelection;
0065 
0066 private Q_SLOTS:
0067     void slotUpdateComboBoxLabels( int value );
0068 };
0069 
0070 class MetaQueryWidget : public QWidget
0071 {
0072     Q_PROPERTY( bool hideFieldSelector READ isFieldSelectorHidden WRITE setFieldSelectorHidden )
0073 
0074     Q_OBJECT
0075 
0076     public:
0077         /** Creates a MetaQueryWidget which can be used to select one meta query filter.
0078          *  @param parent The parent widget
0079          *  @param onlyNumeric If set to true the widget will only display numeric fields.
0080          *  @param noCondition If set to true no condition can be selected.
0081          */
0082         explicit MetaQueryWidget( QWidget* parent = nullptr, bool onlyNumeric = false, bool noCondition = false );
0083         ~MetaQueryWidget() override;
0084 
0085         enum FilterCondition
0086         {
0087             Equals       =  0,
0088             GreaterThan  =  1,
0089             LessThan     =  2,
0090             Between      =  3,
0091             OlderThan    =  4,
0092             NewerThan    =  5,
0093             Contains     =  6
0094         };
0095 
0096         struct Filter
0097         {
0098             Filter()
0099                   : m_field( 0 )
0100                   , numValue( 0 )
0101                   , numValue2( 0 )
0102                   , condition( Contains )
0103             {}
0104 
0105             qint64 field() const { return m_field; }
0106             void setField( qint64 newField );
0107 
0108             /** Returns a textual representation of the field.
0109              */
0110             QString fieldToString() const;
0111 
0112             /** Returns a textual representation of the filter.
0113              *  Used for the edit filter dialog (or for debugging)
0114              */
0115             QString toString( bool invert = false ) const;
0116 
0117             bool isNumeric() const
0118             { return MetaQueryWidget::isNumeric( m_field ); }
0119 
0120             bool isDate() const
0121             { return MetaQueryWidget::isDate( m_field ); }
0122 
0123             /** Returns the minimum allowed value for the field type */
0124             static qint64 minimumValue( quint64 field );
0125             static qint64 maximumValue( quint64 field );
0126             static qint64 defaultValue( quint64 field );
0127 
0128         private:
0129             qint64 m_field;
0130 
0131         public:
0132             QString  value;
0133             qint64   numValue;
0134             qint64   numValue2;
0135             FilterCondition condition;
0136         };
0137 
0138         /** Returns the current filter value.
0139          */
0140         Filter filter() const;
0141 
0142         /** Returns true if the given field is a numeric field */
0143         static bool isNumeric( qint64 field );
0144 
0145         /** Returns true if the given field is a date field */
0146         static bool isDate( qint64 field );
0147 
0148         /** Returns a localized text of the condition.
0149          *  @param condition The condition
0150          *  @param field Needed in order to test whether the field is a date, numeric or a string since the texts differ slightly
0151         */
0152         static QString conditionToString( FilterCondition condition, qint64 field );
0153 
0154 
0155     public Q_SLOTS:
0156         void setFilter(const MetaQueryWidget::Filter &value);
0157 
0158         void setField( const qint64 field );
0159         /** Field Selector combo box visibility state
0160          */
0161         bool isFieldSelectorHidden() const;
0162         void setFieldSelectorHidden( const bool hidden );
0163 
0164     Q_SIGNALS:
0165         void changed(const MetaQueryWidget::Filter &value);
0166 
0167     private Q_SLOTS:
0168         void fieldChanged( int );
0169         void compareChanged( int );
0170         void valueChanged( const QString& );
0171         void numValueChanged( int );
0172         void numValue2Changed( int );
0173         void numValueChanged( qint64 );
0174         void numValue2Changed( qint64 );
0175         void numValueChanged( const QTime& );
0176         void numValue2Changed( const QTime& );
0177         void numValueDateChanged();
0178         void numValue2DateChanged();
0179         void numValueTimeDistanceChanged();
0180         void numValueFormatChanged( int );
0181 
0182         void populateComboBox(const QStringList & );
0183         void comboBoxPopulated();
0184 
0185     private:
0186         void makeFieldSelection();
0187 
0188         /** Adds the value selection widgets to the layout.
0189          *  Adds m_compareSelection, m_valueSelection1, m_valueSelection2 to the layout.
0190          */
0191         void setValueSelection();
0192 
0193         void makeCompareSelection();
0194         void makeValueSelection();
0195 
0196         void makeGenericComboSelection( bool editable, Collections::QueryMaker* populateQuery );
0197         void makeMetaComboSelection( qint64 field );
0198 
0199         void makeFormatComboSelection();
0200         void makeGenericNumberSelection( qint64 field, const QString& unit = QLatin1String("") );
0201         void makePlaycountSelection();
0202         void makeRatingSelection();
0203         void makeLengthSelection();
0204         void makeDateTimeSelection();
0205         void makeFilenameSelection();
0206 
0207         bool m_onlyNumeric;
0208         bool m_noCondition;
0209 
0210         bool m_settingFilter; // if set to true we are just setting the filter
0211 
0212         QVBoxLayout* m_layoutMain;
0213         QHBoxLayout* m_layoutValue;
0214         QVBoxLayout* m_layoutValueLabels;
0215         QVBoxLayout* m_layoutValueValues;
0216 
0217         QComboBox*   m_fieldSelection;
0218         QLabel*      m_andLabel;
0219         QComboBox*   m_compareSelection;
0220         QWidget*     m_valueSelection1;
0221         QWidget*     m_valueSelection2;
0222 
0223         Filter m_filter;
0224 
0225         QMap< QObject*, QPointer<KComboBox> > m_runningQueries;
0226 };
0227 
0228 #endif
0229