Repository: nuttyartist/notes
File: src/notelistview.cpp (C++)

Code before:
{
    Q_D(NoteListView);
    m_isMousePressed = true;
    auto index = indexAt(e->pos());
    if (!index.isValid()) {
        emit noteListViewClicked();
        return;
    }
    auto model = dynamic_cast<NoteListModel *>(this->model());
    if (model && model->isFirstPinnedNote(index)) {
        auto rect = visualRect(index);
        auto iconRect = QRect(rect.right() - 25, rect.y() + 2, 20, 20);
        if (iconRect.contains(e->pos())) {
            setIsPinnedNotesCollapsed(!isPinnedNotesCollapsed());
            m_mousePressHandled = true;
            return;
        }
    }

    if (e->button() == Qt::LeftButton) {
        m_dragStartPosition = e->pos();
        auto oldIndexes = selectionModel()->selectedIndexes();
        if (!oldIndexes.contains(index)) {
            if (e->modifiers() == Qt::ControlModifier) {
                setSelectionMode(QAbstractItemView::MultiSelection);
                if (oldIndexes.contains(index) && oldIndexes.size() > 1) {
                    selectionModel()->select(index, QItemSelectionModel::Deselect);
                } else {
                    setCurrentIndex(index);
                    selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);
                }
                auto selectedIndexes = selectionModel()->selectedIndexes();
                emit notePressed(selectedIndexes);
            } else {
                setCurrentIndexC(index);
                emit notePressed({ index });
            }
            m_mousePressHandled = true;
        }
    } else if (e->button() == Qt::RightButton) {
        auto oldIndexes = selectionModel()->selectedIndexes();
        if (!oldIndexes.contains(index)) {
            setCurrentIndexC(index);
            emit notePressed({ index });
        }
    }
    QPoint offset = d->offset();
    d->pressedPosition = e->pos() + offset;
}

void NoteListView::mouseReleaseEvent(QMouseEvent *e)

Review comment (bug):
This `oldIndexes.contains(index)` check makes no sense, btw. If we get here, we already know that `!oldIndexes.contains(index)` was `true` after all. So either this code can be simplified or there is a bug.

Code after:
{
    Q_D(NoteListView);
    m_isMousePressed = true;
    auto index = indexAt(e->pos());
    if (!index.isValid()) {
        emit noteListViewClicked();
        return;
    }
    auto model = dynamic_cast<NoteListModel *>(this->model());
    if (model && model->isFirstPinnedNote(index)) {
        auto rect = visualRect(index);
        auto iconRect = QRect(rect.right() - 25, rect.y() + 2, 20, 20);
        if (iconRect.contains(e->pos())) {
            setIsPinnedNotesCollapsed(!isPinnedNotesCollapsed());
            m_mousePressHandled = true;
            return;
        }
    }

    if (e->button() == Qt::LeftButton) {
        m_dragStartPosition = e->pos();
        auto oldIndexes = selectionModel()->selectedIndexes();
        if (!oldIndexes.contains(index)) {
            if (e->modifiers() == Qt::ControlModifier) {
                setSelectionMode(QAbstractItemView::MultiSelection);
                setCurrentIndex(index);
                selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);
                auto selectedIndexes = selectionModel()->selectedIndexes();
                emit notePressed(selectedIndexes);
            } else {
                setCurrentIndexC(index);
                emit notePressed({ index });
            }
            m_mousePressHandled = true;
        }
    } else if (e->button() == Qt::RightButton) {
        auto oldIndexes = selectionModel()->selectedIndexes();
        if (!oldIndexes.contains(index)) {
            setCurrentIndexC(index);
            emit notePressed({ index });
        }
    }
    QPoint offset = d->offset();
    d->pressedPosition = e->pos() + offset;
}

void NoteListView::mouseReleaseEvent(QMouseEvent *e)
{
    m_isMousePressed = false;
    auto index = indexAt(e->pos());
    if (!index.isValid()) {