QtPass Developer documentation
QtPass test coverage
Current view: top level - src/storetree.cpp (source / functions) Coverage Total Hit
Test: QtPass Lines: 96.6 % 59 57
Test Date: 2026-11-14 13:19:18 Functions: 91.7 % 12 11

            Line data    Source code
       1              : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
       2              : // SPDX-License-Identifier: GPL-3.0-or-later
       3              : /**
       4              :  * @file
       5              :  * @brief Implements StoreTree; setStore() is where the file system model,
       6              :  *        the proxy and the view are re-rooted together.
       7              :  */
       8              : #include "storetree.h"
       9              : #include "util.h"
      10              : #include <QDir>
      11              : #include <QHeaderView>
      12              : #include <QTreeView>
      13              : 
      14          136 : StoreTree::StoreTree(QTreeView *view, QObject *parent)
      15          136 :     : QObject(parent), m_view(view) {
      16          272 :   m_fs.setNameFilters(QStringList() << QStringLiteral("*.gpg"));
      17          136 :   m_fs.setNameFilterDisables(false);
      18              : 
      19          136 :   m_view->setModel(&m_proxy);
      20          136 :   m_view->setHeaderHidden(true);
      21          136 :   m_view->setIndentation(15);
      22          136 :   m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
      23          136 :   m_view->setContextMenuPolicy(Qt::CustomContextMenu);
      24          136 : }
      25              : 
      26          145 : void StoreTree::setStore(const QString &path) {
      27          145 :   m_store = path;
      28          145 :   const QModelIndex rootDir = m_fs.setRootPath(path);
      29          145 :   m_fs.fetchMore(rootDir);
      30          145 :   if (m_proxy.sourceModel() == nullptr) {
      31          136 :     m_proxy.setModelAndStore(&m_fs, path);
      32              :     // Columns exist only once a source model is attached: hiding the size,
      33              :     // type and date ones is a no-op before that point, and addressing header
      34              :     // section 0 without any sections crashes on Qt 6.8.
      35          136 :     m_view->setColumnHidden(1, true);
      36          136 :     m_view->setColumnHidden(2, true);
      37          136 :     m_view->setColumnHidden(3, true);
      38          136 :     m_view->header()->setSectionResizeMode(0, QHeaderView::Stretch);
      39          136 :     m_view->sortByColumn(0, Qt::AscendingOrder);
      40              :   } else {
      41            9 :     m_proxy.setStore(path);
      42              :   }
      43          145 :   m_proxy.setFilterRegularExpression(QRegularExpression());
      44          145 :   m_view->setRootIndex(m_proxy.rootIndexFor(path));
      45          145 : }
      46              : 
      47          133 : void StoreTree::setPass(Pass *pass) { m_proxy.setPass(pass); }
      48              : 
      49           30 : void StoreTree::setFilter(const QRegularExpression &pattern) {
      50           30 :   m_proxy.setFilterRegularExpression(pattern);
      51              :   // Filtering re-derives the mapping; the root must be looked up again.
      52           30 :   m_view->setRootIndex(m_proxy.rootIndexFor(m_store));
      53           30 : }
      54              : 
      55           39 : auto StoreTree::dirFor(const QModelIndex &index, bool forPass) const
      56              :     -> QString {
      57           78 :   const QString abspath = QDir(m_store).absolutePath() + QDir::separator();
      58              :   if (!index.isValid()) {
      59           17 :     return forPass ? QString() : abspath;
      60              :   }
      61           22 :   const QFileInfo info = m_fs.fileInfo(m_proxy.mapToSource(index));
      62              :   QString filePath =
      63           22 :       info.isFile() ? info.absolutePath() : info.absoluteFilePath();
      64           22 :   if (forPass) {
      65           14 :     filePath = QDir(abspath).relativeFilePath(filePath);
      66              :   }
      67           22 :   filePath += QDir::separator();
      68              :   return filePath;
      69           22 : }
      70              : 
      71           32 : auto StoreTree::currentDir(bool forPass) const -> QString {
      72           32 :   return dirFor(m_view->currentIndex(), forPass);
      73              : }
      74              : 
      75           42 : auto StoreTree::fileFor(const QModelIndex &index, bool forPass) const
      76              :     -> QString {
      77           80 :   if (!index.isValid() || !m_fs.fileInfo(m_proxy.mapToSource(index)).isFile()) {
      78            4 :     return {};
      79              :   }
      80           38 :   QString filePath = m_fs.filePath(m_proxy.mapToSource(index));
      81           38 :   if (forPass) {
      82           70 :     filePath = QDir(m_store).relativeFilePath(filePath);
      83           70 :     filePath.replace(Util::endsWithGpg(), "");
      84              :   }
      85              :   return filePath;
      86              : }
      87              : 
      88            1 : auto StoreTree::currentFile(bool forPass) const -> QString {
      89            1 :   return fileFor(m_view->currentIndex(), forPass);
      90              : }
      91              : 
      92           50 : auto StoreTree::fileInfo(const QModelIndex &index) const -> QFileInfo {
      93           50 :   return m_fs.fileInfo(m_proxy.mapToSource(index));
      94              : }
      95              : 
      96           23 : auto StoreTree::indexFor(const QString &path) const -> QModelIndex {
      97           23 :   const QModelIndex src = m_fs.index(path);
      98           23 :   return src.isValid() ? m_proxy.mapFromSource(src) : QModelIndex();
      99              : }
     100              : 
     101           19 : auto StoreTree::rootIndex() -> QModelIndex {
     102           19 :   return m_proxy.rootIndexFor(m_store);
     103              : }
     104              : 
     105            0 : auto StoreTree::currentIndex() const -> QModelIndex {
     106            0 :   return m_view->currentIndex();
     107              : }
        

Generated by: LCOV version 2.0-1