File indexing completed on 2022-10-04 17:30:17

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
0003 
0004 #ifndef KRENAMEMODEL_H
0005 #define KRENAMEMODEL_H
0006 
0007 #include "krenamefile.h"
0008 #include "krenametokensorter.h"
0009 
0010 #include <QAbstractListModel>
0011 
0012 class BatchRenamer;
0013 class ThreadedLister;
0014 class KFileItem;
0015 class QPixmap;
0016 
0017 /** This enum is used to specify a sorting mode
0018  */
0019 enum ESortMode {
0020     eSortMode_Unsorted,
0021     eSortMode_Ascending,
0022     eSortMode_Descending,
0023     eSortMode_Numeric,
0024     eSortMode_Random,
0025     eSortMode_AscendingDate,
0026     eSortMode_DescendingDate,
0027     eSortMode_Token
0028 };
0029 
0030 class KRenameModel : public QAbstractListModel
0031 {
0032     Q_OBJECT
0033 public:
0034     explicit KRenameModel(KRenameFile::List *vector);
0035     ~KRenameModel() override;
0036 
0037     /**
0038      * Set the batchrenamer instance.
0039      */
0040     inline void setRenamer(BatchRenamer *renamer);
0041 
0042     /** Add a KRenameFile to the model
0043      *
0044      *  @param files a KRenameFile::List which is added to the internal list
0045      */
0046     void addFiles(const KRenameFile::List &files);
0047 
0048     /** Remove items in the model
0049      *
0050      *  @param remove list of indexes to remove
0051      */
0052     void removeFiles(const QList<int> &remove);
0053 
0054     /** Sort the data in the model
0055      *  using the selected sort mode.
0056      *
0057      *  @param mode the sort mode to use
0058      *  @param customSortToken customSortToken if mode is eSortMode_Token
0059      *  @param customSortMode mode for sorting if mode is eSortMode_Token
0060      */
0061     void sortFiles(ESortMode mode, const QString &customSortToken,
0062                    KRenameTokenSorter::ESimpleSortMode customSortMode);
0063 
0064     /** Get the current sort mode.
0065      *  @returns the current sort mode
0066      */
0067     inline ESortMode getSortMode() const;
0068 
0069     inline QString getSortModeCustomToken() const;
0070     inline KRenameTokenSorter::ESimpleSortMode getSortModeCustomMode() const;
0071 
0072     /** Move each file in a list of indices upwards
0073      *  @param files list of file indices. Each file is moved up one position
0074      */
0075     void moveFilesUp(const QList<int> &files);
0076 
0077     /** Move each file in a list of indices downwards
0078      *  @param files list of file indices. Each file is moved down one position
0079      */
0080     void moveFilesDown(const QList<int> &files);
0081 
0082     /** Creates a new model index
0083      *
0084      *  @param row the index of the requested file
0085      *  @returns the model index for a certain row
0086      */
0087     const QModelIndex createIndex(int row) const;
0088 
0089     /** Get the file at position index.
0090      *
0091      *  @param a valid index in the internal vector
0092      *
0093      *  @returns a KRenameFile object
0094      */
0095     inline const KRenameFile &file(int index) const;
0096 
0097     /** Get the file at position index.
0098      *
0099      *  @param a valid index in the internal vector
0100      *
0101      *  @returns a KRenameFile object
0102      */
0103     inline KRenameFile &file(int index);
0104 
0105     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0106     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0107 
0108     Qt::DropActions supportedDropActions() const override;
0109     QStringList mimeTypes() const override;
0110     Qt::ItemFlags flags(const QModelIndex &index) const override;
0111     bool dropMimeData(const QMimeData *data, Qt::DropAction action,
0112                       int row, int column, const QModelIndex &parent) override;
0113     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
0114 
0115     /** Enable the preview of KRenameFile objects.
0116      *
0117      *  @param preview enable/disable preview
0118      *  @param filenames if preview is true this items decides
0119      *                   if the filename text is displayed next to the preview
0120      */
0121     inline void setEnablePreview(bool preview, bool filenames);
0122     /** Test if the preview of KRenameFile objects is enabled.
0123      *
0124      *  @return preview enabled?
0125      */
0126     inline bool isPreviewEnabled() const;
0127 
0128     /** Run/open the file which is pointed to by the passed modelinex
0129      *
0130      *  @param index a modelindex specifying a file to open
0131      *  @param window a window used to cache authentication information
0132      */
0133     void run(const QModelIndex &index, QWidget *window) const;
0134 
0135     /**
0136      * Specify the extension split mode.
0137      * \param splitMode split mode
0138      * \param dot dot t use for eSplitMode_CustomDot
0139      */
0140     inline void setExtensionSplitMode(ESplitMode splitMode, unsigned int dot);
0141 
0142     inline ESplitMode splitMode();
0143     inline unsigned int splitDot();
0144 
0145 Q_SIGNALS:
0146     /** This signal is emitted when the maximum number of
0147      *  dots in a filename that can be used to separate
0148      *  filename and extension has changed (by adding a new file).
0149      *
0150      *  @param dots the maximum number of dots in a filename
0151      */
0152     void maxDotsChanged(int dots);
0153 
0154     /** Emitted when files have been added using drag and drop
0155      */
0156     void filesDropped();
0157 
0158 private Q_SLOTS:
0159     void slotListerDone(ThreadedLister *lister);
0160     void gotPreview(const KFileItem &item, const QPixmap &preview);
0161 
0162 private:
0163     BatchRenamer *m_renamer;
0164     KRenameFile::List *m_vector;
0165 
0166     bool               m_preview;
0167     bool               m_text;
0168 
0169     int                m_maxDots;  ///< The maximum number of dots in a filename which can be used to separate filename and extension
0170     const char        *m_mimeType; ///< MIME type for drag and drop operations
0171 
0172     ESortMode          m_eSortMode; ///< Last used sort mode
0173     QString            m_customSortToken; ///< if m_eSortMode = eSortMode_Token
0174     KRenameTokenSorter::ESimpleSortMode m_eCustomSortMode;  ///< if m_eSortMode = eSortMode_Token
0175 
0176     ESplitMode        m_eSplitMode;
0177     unsigned int      m_dot;
0178 };
0179 
0180 void KRenameModel::setRenamer(BatchRenamer *renamer)
0181 {
0182     m_renamer = renamer;
0183 }
0184 
0185 ESortMode KRenameModel::getSortMode() const
0186 {
0187     return m_eSortMode;
0188 }
0189 
0190 QString KRenameModel::getSortModeCustomToken() const
0191 {
0192     return m_customSortToken;
0193 }
0194 
0195 KRenameTokenSorter::ESimpleSortMode KRenameModel::getSortModeCustomMode() const
0196 {
0197     return m_eCustomSortMode;
0198 }
0199 
0200 const KRenameFile &KRenameModel::file(int index) const
0201 {
0202     return m_vector->at(index);
0203 }
0204 
0205 KRenameFile &KRenameModel::file(int index)
0206 {
0207     return (*m_vector)[index];
0208 }
0209 
0210 bool KRenameModel::isPreviewEnabled() const
0211 {
0212     return m_preview;
0213 }
0214 
0215 void KRenameModel::setEnablePreview(bool preview, bool filenames)
0216 {
0217     bool update = false;
0218     if (m_preview != preview || filenames != m_text) {
0219         update = true;
0220     }
0221 
0222     m_preview = preview;
0223     m_text    = filenames;
0224 
0225     if (update) {
0226         // TODO: update the model
0227         ;
0228     }
0229 }
0230 
0231 void KRenameModel::setExtensionSplitMode(ESplitMode splitMode, unsigned int dot)
0232 {
0233     m_eSplitMode = splitMode;
0234     m_dot = dot;
0235 }
0236 
0237 ESplitMode KRenameModel::splitMode()
0238 {
0239     return m_eSplitMode;
0240 }
0241 
0242 unsigned int KRenameModel::splitDot()
0243 {
0244     return m_dot;
0245 }
0246 
0247 class KRenamePreviewModel : public QAbstractTableModel
0248 {
0249     Q_OBJECT
0250 public:
0251     explicit KRenamePreviewModel(KRenameFile::List *vector);
0252     ~KRenamePreviewModel() override;
0253 
0254     void refresh();
0255 
0256     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0257     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0258     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0259 
0260     QModelIndex parent(const QModelIndex &index) const override;
0261     QModelIndex sibling(int row, int column, const QModelIndex &index) const override;
0262 
0263     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0264 private:
0265     KRenameFile::List *m_vector;
0266 };
0267 
0268 #endif // KRENAMEMODEL_H
0269