QTfrontend/ui/widget/HistoryLineEdit.h
changeset 6151 9fd5b70acb1a
child 6160 863d3edf5690
equal deleted inserted replaced
6150:1d98752c1fba 6151:9fd5b70acb1a
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
       
     4  * Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com>
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License as published by
       
     8  * the Free Software Foundation; version 2 of the License
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    18  */
       
    19 
       
    20 #ifndef HEDGEWARS_HISTORYLINEEDIT
       
    21 #define HEDGEWARS_HISTORYLINEEDIT
       
    22 
       
    23 #include <QLineEdit>
       
    24 #include <QString>
       
    25 
       
    26 #include <QKeyEvent>
       
    27 
       
    28 #include <QMutex>
       
    29 
       
    30 class QLineEdit;
       
    31 
       
    32 /**
       
    33  * A modification of QLineEdit that features:
       
    34  * + History of previous contents, re-selectable using the arrow keys.
       
    35  *
       
    36  * Note:
       
    37  *   * Public methods for accessing history are thread-safe.
       
    38  * @author sheepluva
       
    39  * @since 0.9.17
       
    40  */
       
    41 class HistoryLineEdit : public QLineEdit
       
    42 {
       
    43  Q_OBJECT
       
    44 
       
    45 public:
       
    46     /**
       
    47     * Class constructor.
       
    48     * @param parent parent QWidget.
       
    49     * @param maxHistorySize maximum amount of history entries kept.
       
    50     */
       
    51     HistoryLineEdit(QWidget * parent = 0, int maxHistorySize = 64);
       
    52 
       
    53     /**
       
    54      * Appends current text to history (if not only whitespaces);
       
    55      */
       
    56     void rememberCurrentText();
       
    57 
       
    58     /**
       
    59      * Forget all history.
       
    60      */
       
    61     void reset();
       
    62 
       
    63 
       
    64 public slots:
       
    65     /**
       
    66      * Clears the contents.
       
    67      */
       
    68     void clear();
       
    69 
       
    70 
       
    71 protected:
       
    72     /**
       
    73      * Overrides method of parent class.
       
    74      * Arrow keys are used for navigating the history.
       
    75      * All other keys are forwarded to the parent's method.
       
    76      * @param event the key event.
       
    77      */
       
    78     virtual void keyPressEvent(QKeyEvent * event);
       
    79 
       
    80 
       
    81 private:
       
    82     int m_maxHistorySize; // the maximum allowed size for the history
       
    83     int m_curHistEntryIdx; // the index of the displayed used entry
       
    84 
       
    85     QStringList * m_history; // history of previous inputs
       
    86 
       
    87     QMutex m_historyMutex; // make history QStringList action thread-safe
       
    88 
       
    89     /**
       
    90      * Navigates content history in the desired direction.
       
    91      * Note: no wrap-around on purpose (so that holding down/up will get the
       
    92      * the user to the respective end rather than into an endless cycle :P)
       
    93      * @param isGoingUp true: next older entry, false: next more recent entry.
       
    94      */
       
    95     void navigateHistory(bool isGoingUp);
       
    96 
       
    97     /**
       
    98      * Appends current text to history, without Mutex.
       
    99      */
       
   100     void rememberCurrentTextUnsynced();
       
   101 };
       
   102 
       
   103 
       
   104 
       
   105 #endif // HEDGEWARS_HISTORYLINEEDIT