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

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Dan Meltzer <parallelgrapefruit@gmail.com>                        *
0003  * Copyright (c) 2011 Sven Krohlas <sven@asbest-online.de>                              *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #ifndef SEARCHWIDGET_H
0019 #define SEARCHWIDGET_H
0020 
0021 #include "amarok_export.h"
0022 #include "ComboBox.h"
0023 
0024 #include <QWidget>
0025 #include <QTimer>
0026 
0027 class QToolBar;
0028 // A Custom Widget that can be used globally to implement
0029 // searching a treeview.
0030 
0031 class AMAROK_EXPORT SearchWidget : public QWidget
0032 {
0033     Q_OBJECT
0034     public:
0035         /** Creates a search widget.
0036          * @param parent The parent widget
0037          * @param advanced If true generates a button that opens a edit filter dialog.
0038         */
0039         explicit SearchWidget( QWidget *parent = nullptr, bool advanced = true );
0040 
0041         QString currentText() const { return m_sw->currentText(); }
0042         Amarok::ComboBox *comboBox() { return m_sw; }
0043 
0044         /**
0045          * Sets the timout length after which the filterChanged() signal will be fired automatically.
0046          * @param newTimeout timeout in milliseconds.
0047          */
0048         void setTimeout( quint16 newTimeout );
0049 
0050         QToolBar* toolBar();
0051 
0052         void showAdvancedButton( bool show );
0053 
0054         /**
0055          * Sets the string that will be visible when the ComboBox's edit text is empty.
0056          * @param message the string that will be visible when the ComboBox's edit text is empty.
0057          */
0058         void setClickMessage( const QString &message );
0059 
0060     public Q_SLOTS:
0061         void setSearchString( const QString &searchString = QString() );
0062         void emptySearchString() { setSearchString( QString() ); }
0063 
0064         /**
0065          * Tells the widget that a search operation has started. As a consequence the
0066          * "search" icon changes to a progress animation.
0067          *
0068          * Note: You can call this slot several times if you have several search operations
0069          * simultaneously. The widget has an internal counter to track them.
0070          */
0071         void searchStarted();
0072 
0073         /**
0074          * Tells the widget that a search operation has ended. As a consequence the
0075          * progress animation will be changed back to a search icon iff no other search
0076          * operation is in progress.
0077          */
0078         void searchEnded();
0079 
0080     Q_SIGNALS:
0081         /**
0082          * Emitted when the filter value was changed.
0083          * Note: This signal might be delayed while the user is typing
0084          */
0085         void filterChanged( const QString &filter );
0086 
0087         /**
0088          * Emitted when the user hits enter after after typing in the filter. It is
0089          * guaranteed that filterChanged() with the current text was emitted previously.
0090          */
0091         void returnPressed();
0092 
0093     private Q_SLOTS:
0094         void resetFilterTimeout();
0095         void filterNow();
0096         void advanceFocus();
0097 
0098         void addCompletion( const QString &text );
0099         void nextAnimationTick();
0100         void onComboItemActivated( int index );
0101         void slotShowFilterEditor();
0102         void slotFilterEditorFinished( int result );
0103 
0104     private:
0105         Amarok::ComboBox *m_sw;
0106         QAction          *m_filterAction;
0107         QToolBar         *m_toolBar;
0108         QTimer            m_animationTimer;
0109         QTimer            m_filterTimer;
0110         quint16           m_timeout;
0111         bool              m_currentFrame;
0112         unsigned int      m_runningSearches;
0113 
0114         // required to save/restore line edit status
0115         QString m_text;
0116         int     m_cursorPosition;
0117         bool    m_hasSelectedText;
0118         int     m_selectionStart;
0119         int     m_selectionLength;
0120 
0121         /**
0122          * Restore the status of the internal line edit (text, selection, cursor position).
0123          * Crete a snapshot with saveLineEditStatus() before using this method.
0124          * Required to keep user changes during animations.
0125          */
0126         void restoreLineEditStatus();
0127 
0128         /**
0129          * Save the status of the internal line edit (text, selection, cursor position) to
0130          * restore it later with restoreLineEditStatus().
0131          * Required to keep user changes during animations.
0132          */
0133         void saveLineEditStatus();
0134 };
0135 
0136 #endif