give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
authorsheepluva
Wed, 19 Oct 2011 16:10:18 +0200
changeset 6151 9fd5b70acb1a
parent 6150 1d98752c1fba
child 6152 aa9f9b034e00
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
QTfrontend/hwform.cpp
QTfrontend/ui/page/pagenetgame.cpp
QTfrontend/ui/page/pagenetgame.h
QTfrontend/ui/widget/HistoryLineEdit.cpp
QTfrontend/ui/widget/HistoryLineEdit.h
QTfrontend/ui/widget/SmartLineEdit.cpp
QTfrontend/ui/widget/SmartLineEdit.h
QTfrontend/ui/widget/chatwidget.cpp
project_files/hedgewars.pro
--- a/QTfrontend/hwform.cpp	Wed Oct 19 02:10:27 2011 +0200
+++ b/QTfrontend/hwform.cpp	Wed Oct 19 16:10:18 2011 +0200
@@ -1204,7 +1204,7 @@
         // disconnect connections first to ensure their inexistance and not to connect twice
         ui.pageNetGame->BtnStart->disconnect(hwnet);
         ui.pageNetGame->BtnUpdate->disconnect(hwnet);
-        ui.pageNetGame->leRoomName->setText(hwnet->getRoom());
+        ui.pageNetGame->setRoomName(hwnet->getRoom());
         ui.pageNetGame->restrictJoins->disconnect(hwnet);
         ui.pageNetGame->restrictTeamAdds->disconnect(hwnet);
         connect(ui.pageNetGame->BtnStart, SIGNAL(clicked()), hwnet, SLOT(startGame()));
--- a/QTfrontend/ui/page/pagenetgame.cpp	Wed Oct 19 02:10:27 2011 +0200
+++ b/QTfrontend/ui/page/pagenetgame.cpp	Wed Oct 19 16:10:18 2011 +0200
@@ -61,7 +61,7 @@
 {
     QHBoxLayout * bottomLayout = new QHBoxLayout;
 
-    leRoomName = new QLineEdit(this);
+    leRoomName = new HistoryLineEdit(this,10);
     leRoomName->setMaxLength(60);
     leRoomName->setMinimumWidth(200);
     leRoomName->setMaximumWidth(400);
@@ -124,13 +124,26 @@
 
 void PageNetGame::onUpdateClick()
 {
-    if (leRoomName->text().size())
+    if (!leRoomName->text().trimmed().isEmpty())
+    {
         emit askForUpdateRoomName(leRoomName->text());
+        leRoomName->rememberCurrentText();
+    }
     else
+    {
+        leRoomName->clear();
         QMessageBox::critical(this,
                 tr("Error"),
                 tr("Please enter room name"),
                 tr("OK"));
+    }
+}
+
+
+void PageNetGame::setRoomName(const QString & roomName)
+{
+    leRoomName->setText(roomName);
+    leRoomName->rememberCurrentText();
 }
 
 void PageNetGame::setMasterMode(bool isMaster)
@@ -140,3 +153,4 @@
     BtnUpdate->setVisible(isMaster);
     leRoomName->setVisible(isMaster);
 }
+
--- a/QTfrontend/ui/page/pagenetgame.h	Wed Oct 19 02:10:27 2011 +0200
+++ b/QTfrontend/ui/page/pagenetgame.h	Wed Oct 19 16:10:18 2011 +0200
@@ -19,6 +19,8 @@
 #ifndef PAGE_NETGAME_H
 #define PAGE_NETGAME_H
 
+#include "HistoryLineEdit.h"
+
 #include "AbstractPage.h"
 #include "SDLs.h"
 
@@ -33,13 +35,17 @@
 public:
     PageNetGame(QWidget* parent, QSettings * gameSettings, SDLInteraction * sdli);
 
+    /**
+     * Sets the room name to display.
+     * @roomName room name to be displayed.
+     */
+    void setRoomName(const QString & roomName);
+
     QPushButton *BtnGo;
     QPushButton *BtnMaster;
     QPushButton *BtnStart;
     QPushButton *BtnUpdate;
 
-    QLineEdit * leRoomName;
-
     QAction * restrictJoins;
     QAction * restrictTeamAdds;
 
@@ -65,6 +71,7 @@
     QSettings * m_gameSettings;
     SDLInteraction * m_sdli;
 
+    HistoryLineEdit * leRoomName;
     QPushButton * btnSetup;
 };
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/ui/widget/HistoryLineEdit.cpp	Wed Oct 19 16:10:18 2011 +0200
@@ -0,0 +1,153 @@
+/*
+ * Hedgewars, a free turn based strategy game
+ * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
+ * Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include <QStringList>
+
+#include "SmartLineEdit.h"
+
+HistoryLineEdit::HistoryLineEdit(QWidget * parent, int maxHistorySize)
+: QLineEdit(parent)
+{
+    m_curHistEntryIdx = 0;
+    m_maxHistorySize = maxHistorySize;
+    m_history = new QStringList();
+}
+
+
+void HistoryLineEdit::rememberCurrentText()
+{
+    m_historyMutex.lock();
+
+    rememberCurrentTextUnsynced();
+
+    m_historyMutex.unlock();
+}
+
+
+void HistoryLineEdit::rememberCurrentTextUnsynced()
+{
+    QString newEntry = text();
+
+    // don't store whitespace-only/empty text
+    if (newEntry.trimmed().isEmpty())
+        return;
+
+    m_history->removeOne(newEntry); // no duplicates please
+    m_history->append(newEntry);
+
+    // do not keep more entries than allowed
+    if (m_history->size() > m_maxHistorySize)
+        m_history->removeFirst();
+
+    // we're looking at the latest entry
+    m_curHistEntryIdx = m_history->size() - 1;
+}
+
+
+void HistoryLineEdit::clear()
+{
+    m_historyMutex.lock();
+
+    QLineEdit::clear();
+    m_curHistEntryIdx = m_history->size();
+
+    m_historyMutex.unlock();
+}
+
+
+void HistoryLineEdit::reset()
+{
+    // forget history
+    m_historyMutex.lock();
+
+    m_history->clear();
+    m_curHistEntryIdx = 0;
+
+    m_historyMutex.unlock();
+}
+
+
+void HistoryLineEdit::navigateHistory(bool isGoingUp)
+{
+    m_historyMutex.lock();
+
+    // save possible changes to new entry
+    if ((m_curHistEntryIdx >= m_history->size() ||
+        (text() != m_history->at(m_curHistEntryIdx))))
+        {
+            rememberCurrentTextUnsynced();
+        }
+
+    if (isGoingUp)
+        m_curHistEntryIdx--;
+    else
+        m_curHistEntryIdx++;
+
+    // if Idx higher than valid range
+    if (m_curHistEntryIdx >= m_history->size())
+    {
+        QLineEdit::clear();
+        m_curHistEntryIdx = m_history->size();
+    }
+    // if Idx in valid range
+    else if (m_curHistEntryIdx >= 0)
+    {
+        setText(m_history->at(m_curHistEntryIdx));
+    }
+    // if Idx below 0
+    else
+        m_curHistEntryIdx = 0;
+
+
+    m_historyMutex.unlock();
+}
+
+
+void HistoryLineEdit::keyPressEvent(QKeyEvent * event)
+{
+    int key = event->key(); // retrieve pressed key
+
+    // navigate history with arrow keys
+    if (event->modifiers() == Qt::NoModifier)
+        switch (key)
+        {
+            case Qt::Key_Escape:
+                clear();
+                event->accept();
+                break;
+
+            case Qt::Key_Up:
+                navigateHistory(true);
+                event->accept();
+                break;
+
+            case Qt::Key_Down:
+                navigateHistory(false);
+                event->accept();
+                break;
+
+            default:
+                QLineEdit::keyPressEvent(event);
+                break;
+        }
+    // otherwise forward keys to parent
+    else
+        QLineEdit::keyPressEvent(event);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/ui/widget/HistoryLineEdit.h	Wed Oct 19 16:10:18 2011 +0200
@@ -0,0 +1,105 @@
+/*
+ * Hedgewars, a free turn based strategy game
+ * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
+ * Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#ifndef HEDGEWARS_HISTORYLINEEDIT
+#define HEDGEWARS_HISTORYLINEEDIT
+
+#include <QLineEdit>
+#include <QString>
+
+#include <QKeyEvent>
+
+#include <QMutex>
+
+class QLineEdit;
+
+/**
+ * A modification of QLineEdit that features:
+ * + History of previous contents, re-selectable using the arrow keys.
+ *
+ * Note:
+ *   * Public methods for accessing history are thread-safe.
+ * @author sheepluva
+ * @since 0.9.17
+ */
+class HistoryLineEdit : public QLineEdit
+{
+ Q_OBJECT
+
+public:
+    /**
+    * Class constructor.
+    * @param parent parent QWidget.
+    * @param maxHistorySize maximum amount of history entries kept.
+    */
+    HistoryLineEdit(QWidget * parent = 0, int maxHistorySize = 64);
+
+    /**
+     * Appends current text to history (if not only whitespaces);
+     */
+    void rememberCurrentText();
+
+    /**
+     * Forget all history.
+     */
+    void reset();
+
+
+public slots:
+    /**
+     * Clears the contents.
+     */
+    void clear();
+
+
+protected:
+    /**
+     * Overrides method of parent class.
+     * Arrow keys are used for navigating the history.
+     * All other keys are forwarded to the parent's method.
+     * @param event the key event.
+     */
+    virtual void keyPressEvent(QKeyEvent * event);
+
+
+private:
+    int m_maxHistorySize; // the maximum allowed size for the history
+    int m_curHistEntryIdx; // the index of the displayed used entry
+
+    QStringList * m_history; // history of previous inputs
+
+    QMutex m_historyMutex; // make history QStringList action thread-safe
+
+    /**
+     * Navigates content history in the desired direction.
+     * Note: no wrap-around on purpose (so that holding down/up will get the
+     * the user to the respective end rather than into an endless cycle :P)
+     * @param isGoingUp true: next older entry, false: next more recent entry.
+     */
+    void navigateHistory(bool isGoingUp);
+
+    /**
+     * Appends current text to history, without Mutex.
+     */
+    void rememberCurrentTextUnsynced();
+};
+
+
+
+#endif // HEDGEWARS_HISTORYLINEEDIT
--- a/QTfrontend/ui/widget/SmartLineEdit.cpp	Wed Oct 19 02:10:27 2011 +0200
+++ b/QTfrontend/ui/widget/SmartLineEdit.cpp	Wed Oct 19 16:10:18 2011 +0200
@@ -17,23 +17,16 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  */
 
-#include <QStringList>
-
 #include "SmartLineEdit.h"
 
 SmartLineEdit::SmartLineEdit(QWidget * parent, int maxHistorySize)
-: QLineEdit(parent)
+: HistoryLineEdit(parent, maxHistorySize)
 {
-    m_curHistEntryIdx = 0;
-    m_maxHistorySize = maxHistorySize;
-
     m_whitespace = QRegExp("\\s");
 
     m_cmds  = new QStringList();
     m_nicks = new QStringList();
 
-    m_history = new QStringList();
-
     resetAutoCompletionStatus();
 
     // reset autocompletion status when cursor is moved or content is changed
@@ -86,99 +79,22 @@
     m_keywordMutex.unlock();
 }
 
-void SmartLineEdit::rememberCurrentText()
-{
-    m_historyMutex.lock();
 
-    rememberCurrentTextUnsynced();
-
-    m_historyMutex.unlock();
-}
-
-void SmartLineEdit::rememberCurrentTextUnsynced()
-{
-    QString newEntry = text();
-
-    // don't store whitespace-only/empty text
-    if (newEntry.trimmed().isEmpty())
-        return;
-
-    m_history->removeOne(newEntry); // no duplicates please
-    m_history->append(newEntry);
-
-    // do not keep more entries than allowed
-    if (m_history->size() > m_maxHistorySize)
-        m_history->removeFirst();
-
-    // we're looking at the latest entry
-    m_curHistEntryIdx = m_history->size() - 1;
-}
-
-void SmartLineEdit::clear()
-{
-    m_historyMutex.lock();
-
-    QLineEdit::clear();
-    m_curHistEntryIdx = m_history->size();
-
-    m_historyMutex.unlock();
-}
-
-void SmartLineEdit::forgetEverything()
+void SmartLineEdit::reset()
 {
     // forget keywords
     m_keywordMutex.lock();
 
     m_cmds->clear();
     m_nicks->clear();
+    resetAutoCompletionStatus();
 
     m_keywordMutex.unlock();
 
     // forget history
-    m_historyMutex.lock();
-
-    m_history->clear();
-    m_curHistEntryIdx = 0;
-
-    m_historyMutex.unlock();
-
-    resetAutoCompletionStatus();
+    HistoryLineEdit::reset();
 }
 
-void SmartLineEdit::navigateHistory(bool isGoingUp)
-{
-    m_historyMutex.lock();
-
-    // save possible changes to new entry
-    if ((m_curHistEntryIdx >= m_history->size() ||
-        (text() != m_history->at(m_curHistEntryIdx))))
-        {
-            rememberCurrentTextUnsynced();
-        }
-
-    if (isGoingUp)
-        m_curHistEntryIdx--;
-    else
-        m_curHistEntryIdx++;
-
-    // if Idx higher than valid range
-    if (m_curHistEntryIdx >= m_history->size())
-    {
-        QLineEdit::clear();
-        m_curHistEntryIdx = m_history->size();
-    }
-    // if Idx in valid range
-    else if (m_curHistEntryIdx >= 0)
-    {
-        setText(m_history->at(m_curHistEntryIdx));
-    }
-    // if Idx below 0
-    else
-        m_curHistEntryIdx = 0;
-
-
-    m_historyMutex.unlock();
-}
 
 bool SmartLineEdit::event(QEvent * event)
 {
@@ -195,7 +111,7 @@
                 return true;
         }
     }
-    return QLineEdit::event(event);
+    return HistoryLineEdit::event(event);
 }
 
 void SmartLineEdit::keyPressEvent(QKeyEvent * event)
@@ -208,32 +124,15 @@
         autoComplete();
         event->accept();
     }
-    // clear contents on pressed ESC, navigate history with arrow keys
-    else if (event->modifiers() == Qt::NoModifier)
-        switch (key)
-        {
-            case Qt::Key_Escape:
-                clear();
-                event->accept();
-                break;
-
-            case Qt::Key_Up:
-                navigateHistory(true);
-                event->accept();
-                break;
-
-            case Qt::Key_Down:
-                navigateHistory(false);
-                event->accept();
-                break;
-
-            default:
-                QLineEdit::keyPressEvent(event);
-                break;
-        }
+    // clear contents on pressed ESC
+    else if ((event->modifiers() == Qt::NoModifier) && (key == Qt::Key_Escape))
+    {
+        clear();
+        event->accept();
+    }
     // otherwise forward keys to parent
     else
-        QLineEdit::keyPressEvent(event);
+        HistoryLineEdit::keyPressEvent(event);
 }
 
 
@@ -363,3 +262,4 @@
     m_prefix = "";
     m_postfix = "";
 }
+
--- a/QTfrontend/ui/widget/SmartLineEdit.h	Wed Oct 19 02:10:27 2011 +0200
+++ b/QTfrontend/ui/widget/SmartLineEdit.h	Wed Oct 19 16:10:18 2011 +0200
@@ -17,35 +17,29 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  */
 
-#ifndef HEDGEWARS_SMARTLINEDIT
-#define HEDGEWARS_SMARTLINEDIT
+#ifndef HEDGEWARS_SMARTLINEEDIT
+#define HEDGEWARS_SMARTLINEEDIT
 
-#include <QLineEdit>
 #include <QStringList>
-#include <QString>
 
 #include <QEvent>
-#include <QKeyEvent>
-
-#include <QMutex>
 #include <QRegExp>
 
-class QLineEdit;
+#include "HistoryLineEdit.h"
 
 /**
- * A modification of QLineEdit that features:
+ * A {@link HistoryLineEdit} that additionally features:
  * + Auto-completion for word under cursor when the TAB key is pressed.
  * + ESC key clears text.
- * + History of previous contents, re-selectable using the arrow keys.
  *
  * Note:
  *   * A Keyword can either be a command (if first word) or
  *     a nickname (completed regardless of position in text).
- *   * Public methods for accessing keywords and history are thread-safe.
+ *   * Public methods for accessing keywords are thread-safe.
  * @author sheepluva
  * @since 0.9.17
  */
-class SmartLineEdit : public QLineEdit
+class SmartLineEdit : public HistoryLineEdit
 {
  Q_OBJECT
 
@@ -70,11 +64,6 @@
     void addNickname(const QString & nickname);
 
     /**
-     * Appends current text to history.
-     */
-    void rememberCurrentText();
-
-    /**
      * Removes commands from the auto-completion feature.
      * @param commands list of commands to be removed.
      */
@@ -89,14 +78,7 @@
     /**
      * Forget all keywords and input history.
      */
-    void forgetEverything();
-
-
-public slots:
-    /**
-     * Clears the contents.
-     */
-    void clear();
+    void reset();
 
 
 protected:
@@ -112,10 +94,8 @@
     /**
      * Overrides method of parent class.
      * Autocompletes if TAB is reported as pressed key in the key event,
-     * otherwise keys except for ESC and Up/Down (with no modifiers)
-     * are forwarded to parent method.
      * ESC leads to the contents being cleared.
-     * Arrow keys are used for navigating the history.
+     * otherwise keys are forwarded to parent method.
      * @param event the key event.
      */
     virtual void keyPressEvent(QKeyEvent * event);
@@ -124,14 +104,9 @@
 private:
     QRegExp m_whitespace; // regexp that matches a whitespace
 
-    int m_maxHistorySize; // the maximum allowed size for the history
-    int m_curHistEntryIdx; // the index of the currently used entry or -1
-
     QStringList * m_cmds;  // list of recognized commands
     QStringList * m_nicks; // list of recognized nicknames
 
-    QStringList * m_history; // history of previous inputs
-
     // these variables contain information about the last replacement
     // they get reset whenever cursor is moved or text is changed
 
@@ -141,26 +116,12 @@
     QString m_postfix; // postfix of the text replacement this widget just did
 
     QMutex m_keywordMutex; // make keyword QStringList action thread-safe
-    QMutex m_historyMutex; // make history QStringList action thread-safe
 
     /**
      * Autocompletes the contents based on the known commands and/or names.
      */
     void autoComplete();
 
-    /**
-     * Navigates content history in the desired direction.
-     * Note: no wrap-around on purpose (so that holding down/up will get the
-     * the user to the respective end rather than into an endless cycle :P)
-     * @param isGoingUp true: next older entry, false: next more recent entry.
-     */
-    void navigateHistory(bool isGoingUp);
-
-    /**
-     * Appends current text to history, without Mutex.
-     */
-    void rememberCurrentTextUnsynced();
-
 
 private slots:
     /**
@@ -171,4 +132,4 @@
 
 
 
-#endif // HEDGEWARS_SMARTLINEDIT
+#endif // HEDGEWARS_SMARTLINEEDIT
--- a/QTfrontend/ui/widget/chatwidget.cpp	Wed Oct 19 02:10:27 2011 +0200
+++ b/QTfrontend/ui/widget/chatwidget.cpp	Wed Oct 19 16:10:18 2011 +0200
@@ -442,7 +442,7 @@
 
 void HWChatWidget::clear()
 {
-    chatEditLine->forgetEverything();
+    chatEditLine->reset();
     chatText->clear();
     chatStrings.clear();
     chatNicks->clear();
--- a/project_files/hedgewars.pro	Wed Oct 19 02:10:27 2011 +0200
+++ b/project_files/hedgewars.pro	Wed Oct 19 16:10:18 2011 +0200
@@ -69,6 +69,7 @@
     ../QTfrontend/ui/widget/selectWeapon.h \
     ../QTfrontend/ui/widget/weaponItem.h \
     ../QTfrontend/ui/widget/gamecfgwidget.h \
+    ../QTfrontend/ui/widget/HistoryLineEdit.h \
     ../QTfrontend/ui/widget/SmartLineEdit.h \
     ../QTfrontend/net/netregister.h \
     ../QTfrontend/net/netserver.h \
@@ -142,6 +143,7 @@
     ../QTfrontend/ui/widget/teamselhelper.cpp \
     ../QTfrontend/ui/widget/drawmapwidget.cpp \
     ../QTfrontend/ui/widget/weaponItem.cpp \
+    ../QTfrontend/ui/widget/HistoryLineEdit.cpp \
     ../QTfrontend/ui/widget/SmartLineEdit.cpp \
     ../QTfrontend/net/tcpBase.cpp \
     ../QTfrontend/net/netregister.cpp \