Browse Source

keyboard selection for listbox

Sonnix 6 years ago
parent
commit
ac8c7aacd8
6 changed files with 86 additions and 34 deletions
  1. 49 5
      mainwindow.cpp
  2. 2 0
      mainwindow.h
  3. 2 2
      qspinputbox.cpp
  4. 24 24
      qsplistbox.cpp
  5. 6 0
      qsplistbox.h
  6. 3 3
      qsptextbox.cpp

+ 49 - 5
mainwindow.cpp

@@ -25,6 +25,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
     setFocusPolicy(Qt::StrongFocus);
     setObjectName(QStringLiteral("MainWindow"));
 
+    m_palette = palette();
+
     mainMenuBar = new QMenuBar(this);
     setMenuBar(mainMenuBar);
     mainMenuBar->setObjectName(QStringLiteral("mainMenuBar"));
@@ -109,6 +111,7 @@ void MainWindow::ApplyParams()
     QSP_CHAR *strVal;
     QColor setBackColor, setFontColor, setLinkColor;
     int col;
+    setPalette(m_palette);
     // --------------
     if(QSPGetVarValues(QSP_FMT("BCOLOR"), 0, &numVal, &strVal))
     {
@@ -564,7 +567,7 @@ void MainWindow::CreateDockWindows()
     connect(_actionsListBox, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(OnActionsListBoxItemClicked(QListWidgetItem *)));
     connect(_actionsListBox, SIGNAL(itemPressed(QListWidgetItem *)), this, SLOT(OnActionsListBoxItemClicked(QListWidgetItem *)));
     connect(_actionsListBox, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(OnActionsListBoxItemClicked(QListWidgetItem *)));
-    //connect(_actionsListBox, SIGNAL(currentRowChanged(int)), this, SLOT(OnActionChange(int)));
+    connect(_actionsListBox, SIGNAL(SelectionChange(int)), this, SLOT(OnActionChange(int)));
     _actionsWidget->setWidget(_actionsListBox);
 
     // "Additional desc" widget
@@ -644,6 +647,39 @@ void MainWindow::keyPressEvent(QKeyEvent *event)
             ShowError();
         return;
     }
+
+    if(event->key() == Qt::Key_Up)
+    {
+        if(_actionsListBox->count()!=0)
+        {
+            int newSel = _actionsListBox->GetSelection() - 1;
+            if(newSel < 0)
+                _actionsListBox->SetSelection(_actionsListBox->count()-1);
+            else
+                _actionsListBox->SetSelection(newSel);
+        }
+        return;
+    }
+    if(event->key() == Qt::Key_Down)
+    {
+        if(_actionsListBox->count()!=0)
+        {
+            int newSel = _actionsListBox->GetSelection() + 1;
+            if(newSel <= 0 || newSel >= _actionsListBox->count())
+                _actionsListBox->SetSelection(0);
+            else
+                _actionsListBox->SetSelection(newSel);
+        }
+        return;
+    }
+    if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
+        if(_actionsListBox->GetSelection() != -1)
+        {
+            ActionsListBoxDoAction(_actionsListBox->GetSelection());
+            return;
+        }
+
+
     if(event->key() == Qt::Key_Escape)
         if(isFullScreen())
             showNormal();
@@ -679,6 +715,17 @@ void MainWindow::OpenGameFile(const QString &path)
         ShowError();
 }
 
+void MainWindow::ActionsListBoxDoAction(int action)
+{
+    if(action != -1)
+    {
+        if (!QSPSetSelActionIndex(action, QSP_TRUE))
+            ShowError();
+        if (!QSPExecuteSelActionCode(QSP_TRUE))
+            ShowError();
+    }
+}
+
 void MainWindow::OnOpenGame()
 {
     QString path = QFileDialog::getOpenFileName(this, tr("Select game file"), GetLastPath(), tr("QSP games (*.qsp *.gam)"));
@@ -847,10 +894,7 @@ void MainWindow::OnObjectListBoxItemClicked(QListWidgetItem *itemClicked)
 void MainWindow::OnActionsListBoxItemClicked(QListWidgetItem *itemClicked)
 {
     int action = _actionsListBox->row(itemClicked);
-    if (!QSPSetSelActionIndex(action, QSP_TRUE))
-        ShowError();
-    if (!QSPExecuteSelActionCode(QSP_TRUE))
-        ShowError();
+    ActionsListBoxDoAction(action);
 }
 
 void MainWindow::OnObjectChange(int currentRow)

+ 2 - 0
mainwindow.h

@@ -86,6 +86,7 @@ private:
     void closeEvent(QCloseEvent *event);
     void keyPressEvent(QKeyEvent *event);
     void OpenGameFile(const QString& path);
+    void ActionsListBoxDoAction(int action);
 
     // Internal methods
     void UpdateTitle();
@@ -127,6 +128,7 @@ private:
     QColor m_backColor;
     QColor m_linkColor;
     QColor m_fontColor;
+    QPalette m_palette;
     int m_fontSize;
     QString m_fontName;
     QFont m_font;

+ 2 - 2
qspinputbox.cpp

@@ -24,12 +24,12 @@ QString QspInputBox::GetText()
 
 void QspInputBox::keyPressEvent(QKeyEvent *event)
 {
-    if ((event->key()==Qt::Key_Return) && (event->modifiers()==Qt::ControlModifier))
+    if ((event->key()==Qt::Key_Return || event->key() == Qt::Key_Enter) && (event->modifiers()==Qt::ControlModifier))
     {
         appendPlainText("\n");
         return;
     }
-    else if (event->key()==Qt::Key_Return)
+    else if (event->key()==Qt::Key_Return || event->key() == Qt::Key_Enter)
     {
         emit InputTextEnter();
         return;

+ 24 - 24
qsplistbox.cpp

@@ -13,14 +13,16 @@
 
 QspListBox::QspListBox(QWidget *parent) : QListWidget(parent)
 {
-    setSelectionMode(QAbstractItemView::SingleSelection);
+    setSelectionMode(QAbstractItemView::NoSelection);
     m_isUseHtml = false;
     m_isShowNums = false;
     showPlainText = false;
     m_linkColor = palette().color(QPalette::Link);
     m_textColor = palette().color(QPalette::Text);
     m_backgroundColor = palette().color(QPalette::Window);
+    m_selectionColor = palette().color(QPalette::Highlight);
     m_font = font();
+    oldSelection = -1;
 }
 
 QspListBox::~QspListBox()
@@ -141,27 +143,25 @@ bool QspListBox::SetForegroundColor(const QColor &color)
 
 void QspListBox::SetSelection(int selection)
 {
-    if(selection == -1)
-        clearSelection();
-    else
+    if(selection != oldSelection)
     {
-        return;
-        if(selection < count())
-        {
+        if(selection != -1 && selection < count())
             if(item(selection) != 0)
-            {
-                clearSelection();
-                setCurrentItem(item(selection), QItemSelectionModel::SelectCurrent);
-                item(selection)->setSelected(true);
                 scrollToItem(item(selection));
-                //setFocus();//TODO: check if required
-            }
+        if(selection != -1)
+        {
+            QListWidgetItem *curItem =item(selection);
+            if (curItem != 0)
+                qobject_cast<QspTextBox*>(itemWidget(curItem))->SetBackgroundColor(m_selectionColor);
         }
-        else
+        if(oldSelection != -1)
         {
-            clearSelection();
-            //clearFocus();
+            QListWidgetItem *curItem =item(oldSelection);
+            if (curItem != 0)
+                qobject_cast<QspTextBox*>(itemWidget(curItem))->SetBackgroundColor(m_backgroundColor);
         }
+        oldSelection = selection;
+        emit SelectionChange(selection);
     }
 }
 
@@ -175,11 +175,7 @@ void QspListBox::SetShowPlainText(bool isPlain)
 void QspListBox::createList()
 {
     //clear(); //NOTE: clear() only deletes items but does not delete the widgets belonging to it. The widgets will be deleted if the QListWidget is deleted.
-    bool oldState = blockSignals(true);
-    int selection = -1;
-    QList<QListWidgetItem *> selList = selectedItems();
-    if(selList.size() != 0)
-        selection = row(selList.at(0));
+    //bool oldState = blockSignals(true);
     for(int i = 0; i<count(); i++)
     {
         removeItemWidget(item(i));
@@ -231,9 +227,13 @@ void QspListBox::createList()
         item->setSizeHint(sizehint);
         setItemWidget(item, item_widget);
     }
-    if(selection != -1)
-        SetSelection(selection);
-    blockSignals(oldState);
+    if(oldSelection != -1)
+    {
+        QListWidgetItem *curItem =item(oldSelection);
+        if (curItem != 0)
+            qobject_cast<QspTextBox*>(itemWidget(curItem))->SetBackgroundColor(m_selectionColor);
+    }
+    //blockSignals(oldState);
 }
 
 QString QspListBox::formatItem(int itemIndex)

+ 6 - 0
qsplistbox.h

@@ -16,6 +16,9 @@ class QspListBox : public QListWidget
 {
     Q_OBJECT
 
+signals:
+   void SelectionChange(int selection);
+
 public:
     explicit QspListBox(QWidget *parent = 0);
     ~QspListBox();
@@ -39,6 +42,7 @@ public:
     bool SetForegroundColor(const QColor& color);
     void SetGamePath(const QString& path) { m_path = path; }
     void SetSelection(int selection);
+    int GetSelection() { return oldSelection; }
     void SetShowPlainText(bool isPlain);
 
 private:
@@ -62,7 +66,9 @@ private:
     QColor m_linkColor;
     QColor m_textColor;
     QColor m_backgroundColor;
+    QColor m_selectionColor;
     bool showPlainText;
+    int oldSelection;
 };
 
 #endif // QSPLISTBOX_H

+ 3 - 3
qsptextbox.cpp

@@ -156,9 +156,9 @@ bool QspTextBox::SetBackgroundColor(const QColor &color)
 {
     if(m_backColor != color)
     {
-        //QPalette p = palette();
-        //p.setColor(QPalette::Base, color);
-        //setPalette(p);
+        QPalette p = palette();
+        p.setColor(QPalette::Base, color);
+        setPalette(p);
         m_backColor = color;
         setTextBackgroundColor(color);
         RefreshUI();