File indexing completed on 2025-01-05 05:14:51

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "difftreeview.h"
0008 #include "models/difftreemodel.h"
0009 #include "models/filesmodel.h"
0010 
0011 #include <QKeyEvent>
0012 #include <QSortFilterProxyModel>
0013 
0014 DiffTreeModel *DiffTreeView::diffModel() const
0015 {
0016     return mDiffModel;
0017 }
0018 
0019 void DiffTreeView::setModels(DiffTreeModel *newDiffModel, FilesModel *filesModel)
0020 {
0021     mDiffModel = newDiffModel;
0022     mFilesModel = filesModel;
0023 
0024     mFilterModel->setSourceModel(filesModel);
0025     listView->setModel(mFilterModel);
0026 
0027     treeView->setModel(mDiffModel);
0028 }
0029 
0030 DiffTreeView::DiffTreeView(QWidget *parent)
0031     : QWidget(parent)
0032     , mFilterModel(new QSortFilterProxyModel(this))
0033 {
0034     setupUi(this);
0035     mFilterModel->setFilterKeyColumn(0);
0036     mFilterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
0037     listView->setModel(mFilterModel);
0038 
0039     connect(checkBoxHideUnchangeds, &QAbstractButton::toggled, this, &DiffTreeView::hideUnchangedsChanged);
0040     checkBoxHideUnchangeds->hide();
0041 
0042     lineEditFilter->installEventFilter(this);
0043     listView->installEventFilter(this);
0044 
0045     connect(lineEditFilter, &QLineEdit::textEdited, this, &DiffTreeView::lineEditFilterTextChanged);
0046     connect(treeView, &QTreeView::clicked, this, &DiffTreeView::slotTreeViewClicked);
0047     connect(listView, &QListView::clicked, this, &DiffTreeView::slotListViewClicked);
0048 }
0049 
0050 void DiffTreeView::lineEditFilterTextChanged(const QString &text)
0051 {
0052     stackedWidget->setCurrentIndex(text.isEmpty() ? 0 : 1);
0053     mFilterModel->setFilterRegularExpression(QStringLiteral(".*") + text + QStringLiteral(".*"));
0054 }
0055 
0056 void DiffTreeView::slotTreeViewClicked(const QModelIndex &index)
0057 {
0058     const auto fileName = mDiffModel->fullPath(index);
0059     Q_EMIT fileSelected(fileName);
0060 }
0061 
0062 void DiffTreeView::slotListViewClicked(const QModelIndex &index)
0063 {
0064     const auto row = mFilterModel->mapToSource(index).row();
0065     const auto fileName = mFilesModel->data(mFilesModel->index(row, 1), Qt::DisplayRole);
0066     Q_EMIT fileSelected(fileName.toString());
0067 }
0068 
0069 bool DiffTreeView::hideUnchangeds() const
0070 {
0071     return checkBoxHideUnchangeds->isChecked();
0072 }
0073 
0074 void DiffTreeView::setHideUnchangeds(bool newHideUnchangeds)
0075 {
0076     if (checkBoxHideUnchangeds->isChecked() == newHideUnchangeds)
0077         return;
0078     checkBoxHideUnchangeds->setChecked(newHideUnchangeds);
0079     Q_EMIT hideUnchangedsChanged();
0080 }
0081 
0082 bool DiffTreeView::eventFilter(QObject *watched, QEvent *event)
0083 {
0084     if (watched == lineEditFilter && event->type() == QEvent::KeyPress) {
0085         auto ke = static_cast<QKeyEvent *>(event);
0086 
0087         if (ke->key() == Qt::Key_Down) {
0088             if (stackedWidget->currentIndex() == 0)
0089                 treeView->setFocus();
0090             else
0091                 listView->setFocus();
0092         }
0093     }
0094     if (watched == listView && event->type() == QEvent::KeyPress) {
0095         auto ke = static_cast<QKeyEvent *>(event);
0096 
0097         if (ke->key() == Qt::Key_Up && listView->currentIndex().row() == 0)
0098             lineEditFilter->setFocus();
0099     }
0100     return QWidget::eventFilter(watched, event);
0101 }
0102 
0103 #include "moc_difftreeview.cpp"