File indexing completed on 2024-05-19 04:56:02

0001 /**
0002  * \file fileproxymodeliterator.h
0003  * Iterator for FileProxyModel.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 03-Feb-2013
0008  *
0009  * Copyright (C) 2013-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #pragma once
0028 
0029 #include <QObject>
0030 #include <QStack>
0031 #include <QPersistentModelIndex>
0032 #include "iabortable.h"
0033 #include "kid3api.h"
0034 
0035 class FileProxyModel;
0036 
0037 /**
0038  * Iterator for FileProxyModel.
0039  * This iterator behaves differently than other iterators, e.g. ModelIterator.
0040  * The file system model is not completely loaded, subdirectories can be
0041  * fetched later using fetchMore(). This class fetches directories
0042  * continuously and waits for them to be fetched. Therefore the routine
0043  * doing the actual work has to be connected with a slot and will be called
0044  * when file nodes are available. The iteration will also be suspended after
0045  * some files so that other slots can be processed and the GUI remains
0046  * responsive. If the iteration shall stop before all files are processed,
0047  * abort() shall be called.
0048  */
0049 class KID3_CORE_EXPORT FileProxyModelIterator : public QObject, public IAbortable {
0050   Q_OBJECT
0051 public:
0052   /**
0053    * Constructor.
0054    *
0055    * @param model file proxy model
0056    */
0057   explicit FileProxyModelIterator(FileProxyModel* model);
0058 
0059   /**
0060    * Destructor.
0061    */
0062   ~FileProxyModelIterator() override = default;
0063 
0064   /**
0065    * Abort operation.
0066    */
0067   void abort() override;
0068 
0069   /**
0070    * Check if operation is aborted.
0071    *
0072    * @return true if aborted.
0073    */
0074   bool isAborted() const override;
0075 
0076   /**
0077    * Clear state which is reported by isAborted().
0078    */
0079   void clearAborted() override;
0080 
0081   /**
0082    * Start iteration.
0083    *
0084    * @param rootIdx index of root element
0085    */
0086   void start(const QPersistentModelIndex& rootIdx);
0087 
0088   /**
0089    * Start iteration.
0090    *
0091    * @param indexes indexes of root directories
0092    */
0093   void start(const QList<QPersistentModelIndex>& indexes);
0094 
0095   /**
0096    * Get amount of work to do.
0097    * @return number of nodes which have to be processed.
0098    */
0099   int getWorkToDo() const { return m_nodes.size() + m_rootIndexes.size(); }
0100 
0101   /**
0102    * Get amount of work done.
0103    * @return number of nodes which have been processed.
0104    */
0105   int getWorkDone() const { return m_numDone; }
0106 
0107 signals:
0108   /**
0109    * Signaled when the next file node is ready to be processed.
0110    *
0111    * @param idx file model index
0112    */
0113   void nextReady(const QPersistentModelIndex& idx);
0114 
0115 private slots:
0116   /**
0117    * Called when the gatherer thread has finished to load.
0118    */
0119   void onDirectoryLoaded();
0120 
0121   /**
0122    * Fetch next index.
0123    */
0124   void fetchNext();
0125 
0126 private:
0127   QList<QPersistentModelIndex> m_rootIndexes;
0128   QStack<QPersistentModelIndex> m_nodes;
0129   FileProxyModel* m_model;
0130   QPersistentModelIndex m_nextIdx;
0131   int m_numDone;
0132   bool m_aborted;
0133 };