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

0001 /**
0002  * \file bidirfileproxymodeliterator.h
0003  * Birdirectional iterator for FileProxyModel.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 21-Feb-2014
0008  *
0009  * Copyright (C) 2014-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 <QPersistentModelIndex>
0031 #include "iabortable.h"
0032 #include "kid3api.h"
0033 
0034 class FileProxyModel;
0035 
0036 /**
0037  * Iterator for FileProxyModel.
0038  * This iterator is like FileProxyModelIterator, but it can traverse the
0039  * FileProxyModel in both directions and the iteration can be suspended and
0040  * resumed.
0041  */
0042 class KID3_CORE_EXPORT BiDirFileProxyModelIterator
0043     : public QObject, public IAbortable {
0044   Q_OBJECT
0045 public:
0046   /**
0047    * Constructor.
0048    *
0049    * @param model file proxy model
0050    * @param parent parent object
0051    */
0052   explicit BiDirFileProxyModelIterator(FileProxyModel* model,
0053                                        QObject* parent = nullptr);
0054 
0055   /**
0056    * Destructor.
0057    */
0058   ~BiDirFileProxyModelIterator() override = default;
0059 
0060   /**
0061    * Abort operation.
0062    */
0063   void abort() override;
0064 
0065   /**
0066    * Check if operation is aborted.
0067    *
0068    * @return true if aborted.
0069    */
0070   bool isAborted() const override;
0071 
0072   /**
0073    * Clear state which is reported by isAborted().
0074    */
0075   void clearAborted() override;
0076 
0077   /**
0078    * Set root index of file proxy model.
0079    *
0080    * @param rootIdx index of root element
0081    */
0082   void setRootIndex(const QPersistentModelIndex& rootIdx);
0083 
0084   /**
0085    * Set index of current file.
0086    *
0087    * @param index index of current file
0088    */
0089   void setCurrentIndex(const QPersistentModelIndex& index);
0090 
0091   /**
0092    * Set direction of iteration.
0093    * @param backwards true to iterate backwards, false (default) to iterate
0094    * forwards
0095    */
0096   void setDirectionBackwards(bool backwards) {
0097     m_backwards = backwards;
0098   }
0099 
0100   /**
0101    * Start iteration.
0102    */
0103   void start();
0104 
0105   /**
0106    * Suspend iteration.
0107    * The iteration can be continued with resume().
0108    */
0109   void suspend();
0110 
0111   /**
0112    * Resume iteration which has been suspended with suspend().
0113    */
0114   void resume();
0115 
0116 signals:
0117   /**
0118    * Signaled when the next file node is ready to be processed.
0119    *
0120    * @param idx file model index
0121    */
0122   void nextReady(const QPersistentModelIndex& idx);
0123 
0124 private slots:
0125   /**
0126    * Called when the gatherer thread has finished to load.
0127    */
0128   void onDirectoryLoaded();
0129 
0130   /**
0131    * Fetch next index.
0132    */
0133   void fetchNext();
0134 
0135 private:
0136   FileProxyModel* m_model;
0137   QPersistentModelIndex m_rootIndex;
0138   QPersistentModelIndex m_currentIndex;
0139   bool m_backwards;
0140   bool m_aborted;
0141   bool m_suspended;
0142 };