File indexing completed on 2024-12-08 13:34:32

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