1 /****************************************************************************************
2  * Copyright (c) 2007 Dan Meltzer <parallelgrapefruit@gmail.com>                        *
3  * Copyright (c) 2011 Sven Krohlas <sven@asbest-online.de>                              *
4  *                                                                                      *
5  * This program is free software; you can redistribute it and/or modify it under        *
6  * the terms of the GNU General Public License as published by the Free Software        *
7  * Foundation; either version 2 of the License, or (at your option) any later           *
8  * version.                                                                             *
9  *                                                                                      *
10  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
12  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
13  *                                                                                      *
14  * You should have received a copy of the GNU General Public License along with         *
15  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
16  ****************************************************************************************/
17 
18 #ifndef SEARCHWIDGET_H
19 #define SEARCHWIDGET_H
20 
21 #include "amarok_export.h"
22 #include "ComboBox.h"
23 
24 #include <QWidget>
25 #include <QTimer>
26 
27 class QToolBar;
28 // A Custom Widget that can be used globally to implement
29 // searching a treeview.
30 
31 class AMAROK_EXPORT SearchWidget : public QWidget
32 {
33     Q_OBJECT
34     public:
35         /** Creates a search widget.
36          * @param parent The parent widget
37          * @param advanced If true generates a button that opens a edit filter dialog.
38         */
39         explicit SearchWidget( QWidget *parent = nullptr, bool advanced = true );
40 
currentText()41         QString currentText() const { return m_sw->currentText(); }
comboBox()42         Amarok::ComboBox *comboBox() { return m_sw; }
43 
44         /**
45          * Sets the timout length after which the filterChanged() signal will be fired automatically.
46          * @param newTimeout timeout in milliseconds.
47          */
48         void setTimeout( quint16 newTimeout );
49 
50         QToolBar* toolBar();
51 
52         void showAdvancedButton( bool show );
53 
54         /**
55          * Sets the string that will be visible when the ComboBox's edit text is empty.
56          * @param message the string that will be visible when the ComboBox's edit text is empty.
57          */
58         void setClickMessage( const QString &message );
59 
60     public Q_SLOTS:
61         void setSearchString( const QString &searchString = QString() );
emptySearchString()62         void emptySearchString() { setSearchString( QString() ); }
63 
64         /**
65          * Tells the widget that a search operation has started. As a consequence the
66          * "search" icon changes to a progress animation.
67          *
68          * Note: You can call this slot several times if you have several search operations
69          * simultaneously. The widget has an internal counter to track them.
70          */
71         void searchStarted();
72 
73         /**
74          * Tells the widget that a search operation has ended. As a consequence the
75          * progress animation will be changed back to a search icon iff no other search
76          * operation is in progress.
77          */
78         void searchEnded();
79 
80     Q_SIGNALS:
81         /**
82          * Emitted when the filter value was changed.
83          * Note: This signal might be delayed while the user is typing
84          */
85         void filterChanged( const QString &filter );
86 
87         /**
88          * Emitted when the user hits enter after after typing in the filter. It is
89          * guaranteed that filterChanged() with the current text was emitted previously.
90          */
91         void returnPressed();
92 
93     private Q_SLOTS:
94         void resetFilterTimeout();
95         void filterNow();
96         void advanceFocus();
97 
98         void addCompletion( const QString &text );
99         void nextAnimationTick();
100         void onComboItemActivated( int index );
101         void slotShowFilterEditor();
102         void slotFilterEditorFinished( int result );
103 
104     private:
105         Amarok::ComboBox *m_sw;
106         QAction          *m_filterAction;
107         QToolBar         *m_toolBar;
108         QTimer            m_animationTimer;
109         QTimer            m_filterTimer;
110         quint16           m_timeout;
111         bool              m_currentFrame;
112         unsigned int      m_runningSearches;
113 
114         // required to save/restore line edit status
115         QString m_text;
116         int     m_cursorPosition;
117         bool    m_hasSelectedText;
118         int     m_selectionStart;
119         int     m_selectionLength;
120 
121         /**
122          * Restore the status of the internal line edit (text, selection, cursor position).
123          * Crete a snapshot with saveLineEditStatus() before using this method.
124          * Required to keep user changes during animations.
125          */
126         void restoreLineEditStatus();
127 
128         /**
129          * Save the status of the internal line edit (text, selection, cursor position) to
130          * restore it later with restoreLineEditStatus().
131          * Required to keep user changes during animations.
132          */
133         void saveLineEditStatus();
134 };
135 
136 #endif
137