modernize: Use override instead of virtual
[quassel.git] / src / uisupport / bufferview.cpp
index 0b5dfa5..0925b50 100644 (file)
@@ -73,9 +73,9 @@ void BufferView::init()
 
     header()->hide(); // nobody seems to use this anyway
 
-    // breaks with Qt 4.8
-    if (QString("4.8.0") > qVersion()) // FIXME breaks with Qt versions >= 4.10!
-        setAnimated(true);
+    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+
+    setAnimated(true);
 
     // FIXME This is to workaround bug #663
     setUniformRowHeights(true);
@@ -147,8 +147,8 @@ void BufferView::setFilteredModel(QAbstractItemModel *model_, BufferViewConfig *
     }
 
     if (model()) {
-        disconnect(this, 0, model(), 0);
-        disconnect(model(), 0, this, 0);
+        disconnect(this, nullptr, model(), nullptr);
+        disconnect(model(), nullptr, this, nullptr);
     }
 
     if (!model_) {
@@ -169,7 +169,7 @@ void BufferView::setConfig(BufferViewConfig *config)
         return;
 
     if (_config) {
-        disconnect(_config, 0, this, 0);
+        disconnect(_config, nullptr, this, nullptr);
     }
 
     _config = config;
@@ -262,7 +262,7 @@ void BufferView::dropEvent(QDropEvent *event)
         return TreeViewTouch::dropEvent(event);
 
     // Confirm that the user really wants to merge the buffers before doing so
-    int res = QMessageBox::question(0, tr("Merge buffers permanently?"),
+    int res = QMessageBox::question(nullptr, tr("Merge buffers permanently?"),
         tr("Do you want to merge the buffer \"%1\" permanently into buffer \"%2\"?\n This cannot be reversed!").arg(Client::networkModel()->bufferName(bufferId2)).arg(Client::networkModel()->bufferName(bufferId1)),
         QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
     if (res == QMessageBox::Yes) {
@@ -392,15 +392,9 @@ void BufferView::setExpandedState(const QModelIndex &networkIdx)
     storeExpandedState(networkIdx); // this call is needed to keep track of the isActive state
 }
 
-#if QT_VERSION < 0x050000
-void BufferView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
-{
-    TreeViewTouch::dataChanged(topLeft, bottomRight);
-#else
 void BufferView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
 {
     TreeViewTouch::dataChanged(topLeft, bottomRight, roles);
-#endif
 
     // determine how many items have been changed and if any of them is a networkitem
     // which just swichted from active to inactive or vice versa
@@ -499,6 +493,8 @@ void BufferView::changeBuffer(Direction direction)
     QModelIndex currentIndex = selectionModel()->currentIndex();
     QModelIndex resultingIndex;
 
+    QModelIndex lastNetIndex = model()->index(model()->rowCount() - 1, 0, QModelIndex());
+
     if (currentIndex.parent().isValid()) {
         //If we are a child node just switch among siblings unless it's the first/last child
         resultingIndex = currentIndex.sibling(currentIndex.row() + direction, 0);
@@ -515,6 +511,8 @@ void BufferView::changeBuffer(Direction direction)
         //If we have a toplevel node, try and get an adjacent child
         if (direction == Backward) {
             QModelIndex newParent = currentIndex.sibling(currentIndex.row() - 1, 0);
+            if (currentIndex.row() == 0)
+                newParent = lastNetIndex;
             if (model()->hasChildren(newParent))
                 resultingIndex = newParent.child(model()->rowCount(newParent) - 1, 0);
             else
@@ -528,8 +526,12 @@ void BufferView::changeBuffer(Direction direction)
         }
     }
 
-    if (!resultingIndex.isValid())
-        return;
+    if (!resultingIndex.isValid()) {
+        if (direction == Forward)
+            resultingIndex = model()->index(0, 0, QModelIndex());
+        else
+            resultingIndex = lastNetIndex.child(model()->rowCount(lastNetIndex) - 1, 0);
+    }
 
     selectionModel()->setCurrentIndex(resultingIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
     selectionModel()->select(resultingIndex, QItemSelectionModel::ClearAndSelect);
@@ -584,7 +586,7 @@ void BufferView::hideCurrentBuffer()
     config()->requestRemoveBuffer(bufferId);
 }
 
-void BufferView::filterTextChanged(QString filterString)
+void BufferView::filterTextChanged(const QString& filterString)
 {
     BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());
     if (!filter) {
@@ -614,8 +616,63 @@ QSize BufferView::sizeHint() const
 }
 
 
+void BufferView::changeHighlight(BufferView::Direction direction)
+{
+    // If for some weird reason we get a new delegate
+    BufferViewDelegate *delegate = qobject_cast<BufferViewDelegate*>(itemDelegate(_currentHighlight));
+    if (delegate) {
+        delegate->currentHighlight = QModelIndex();
+    }
+
+    QModelIndex newIndex = _currentHighlight;
+    if (!newIndex.isValid()) {
+        newIndex = model()->index(0, 0);
+    }
+
+    if (direction == Backward) {
+        newIndex = indexBelow(newIndex);
+    } else {
+        newIndex = indexAbove(newIndex);
+    }
+
+    if (!newIndex.isValid()) {
+        return;
+    }
+
+    _currentHighlight = newIndex;
+
+    delegate = qobject_cast<BufferViewDelegate*>(itemDelegate(_currentHighlight));
+    if (delegate) {
+        delegate->currentHighlight = _currentHighlight;
+    }
+    viewport()->update();
+}
+
+void BufferView::selectHighlighted()
+{
+    if (_currentHighlight.isValid()) {
+        selectionModel()->setCurrentIndex(_currentHighlight, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
+        selectionModel()->select(_currentHighlight, QItemSelectionModel::ClearAndSelect);
+    } else {
+        selectFirstBuffer();
+    }
+
+    clearHighlight();
+}
+
+void BufferView::clearHighlight()
+{
+    // If for some weird reason we get a new delegate
+    BufferViewDelegate *delegate = qobject_cast<BufferViewDelegate*>(itemDelegate(_currentHighlight));
+    if (delegate) {
+        delegate->currentHighlight = QModelIndex();
+    }
+    _currentHighlight = QModelIndex();
+    viewport()->update();
+}
+
 // ****************************************
-//  BufferViewDelgate
+//  BufferViewDelegate
 // ****************************************
 class ColorsChangedEvent : public QEvent
 {
@@ -651,11 +708,7 @@ bool BufferViewDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, c
     if (!value.isValid())
         return QStyledItemDelegate::editorEvent(event, model, option, index);
 
-#if QT_VERSION < 0x050000
-    QStyleOptionViewItemV4 viewOpt(option);
-#else
     QStyleOptionViewItem viewOpt(option);
-#endif
     initStyleOption(&viewOpt, index);
 
     QRect checkRect = viewOpt.widget->style()->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &viewOpt, viewOpt.widget);
@@ -681,7 +734,7 @@ bool BufferViewDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, c
 // ==============================
 BufferViewDock::BufferViewDock(BufferViewConfig *config, QWidget *parent)
     : QDockWidget(parent),
-    _childWidget(0),
+    _childWidget(nullptr),
     _widget(new QWidget(parent)),
     _filterEdit(new QLineEdit(parent)),
     _active(false),
@@ -711,7 +764,7 @@ BufferViewDock::BufferViewDock(BufferViewConfig *config, QWidget *parent)
 
 void BufferViewDock::setLocked(bool locked) {
     if (locked) {
-        setFeatures(0);
+        setFeatures(nullptr);
     }
     else {
         setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
@@ -738,7 +791,7 @@ void BufferViewDock::onFilterReturnPressed()
 {
     if (_oldFocusItem) {
         _oldFocusItem->setFocus();
-        _oldFocusItem = 0;
+        _oldFocusItem = nullptr;
     }
 
     if (!config()->showSearch()) {
@@ -746,12 +799,16 @@ void BufferViewDock::onFilterReturnPressed()
     }
 
     BufferView *view = bufferView();
-    if (!view || _filterEdit->text().isEmpty()) {
+    if (!view) {
         return;
     }
 
-    view->selectFirstBuffer();
-    _filterEdit->clear();
+    if (!_filterEdit->text().isEmpty()) {
+        view->selectHighlighted();
+        _filterEdit->clear();
+    } else {
+        view->clearHighlight();
+    }
 }
 
 void BufferViewDock::setActive(bool active)
@@ -778,18 +835,35 @@ bool BufferViewDock::eventFilter(QObject *object, QEvent *event)
        }
    } else if (event->type() == QEvent::KeyRelease) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
-       if (keyEvent->key() != Qt::Key_Escape) {
+
+       BufferView *view = bufferView();
+       if (!view) {
            return false;
        }
 
-       _filterEdit->clear();
+       switch (keyEvent->key()) {
+       case Qt::Key_Escape: {
+           _filterEdit->clear();
+
+           if (!_oldFocusItem) {
+               return false;
+           }
 
-       if (_oldFocusItem) {
            _oldFocusItem->setFocus();
-           _oldFocusItem = 0;
+           _oldFocusItem = nullptr;
+           return true;
+       }
+       case Qt::Key_Down:
+           view->changeHighlight(BufferView::Backward);
+           return true;
+       case Qt::Key_Up:
+           view->changeHighlight(BufferView::Forward);
+           return true;
+       default:
+           break;
        }
 
-       return true;
+       return false;
    }
 
    return false;
@@ -820,7 +894,7 @@ BufferViewConfig *BufferViewDock::config() const
 {
     BufferView *view = bufferView();
     if (!view)
-        return 0;
+        return nullptr;
     else
         return view->config();
 }
@@ -843,3 +917,13 @@ void BufferViewDock::activateFilter()
 
     _filterEdit->setFocus();
 }
+
+
+void BufferViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+    QStyleOptionViewItem newOption = option;
+    if (index == currentHighlight) {
+        newOption.state |= QStyle::State_HasFocus;
+    }
+    QStyledItemDelegate::paint(painter, newOption, index);
+}