1 /***************************************************************************
2                           krenamemodel.h  -  description
3                              -------------------
4     begin                : Sun Apr 25 2007
5     copyright            : (C) 2007 by Dominik Seichter
6     email                : domseichter@web.de
7 ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef KRENAMEMODEL_H
19 #define KRENAMEMODEL_H
20 
21 #include "krenamefile.h"
22 #include "krenametokensorter.h"
23 
24 #include <QAbstractListModel>
25 
26 class BatchRenamer;
27 class ThreadedLister;
28 class KFileItem;
29 class QPixmap;
30 
31 /** This enum is used to specify a sorting mode
32  */
33 enum ESortMode {
34     eSortMode_Unsorted,
35     eSortMode_Ascending,
36     eSortMode_Descending,
37     eSortMode_Numeric,
38     eSortMode_Random,
39     eSortMode_AscendingDate,
40     eSortMode_DescendingDate,
41     eSortMode_Token
42 };
43 
44 class KRenameModel : public QAbstractListModel
45 {
46     Q_OBJECT
47 public:
48     explicit KRenameModel(KRenameFile::List *vector);
49     ~KRenameModel();
50 
51     /**
52      * Set the batchrenamer instance.
53      */
54     inline void setRenamer(BatchRenamer *renamer);
55 
56     /** Add a KRenameFile to the model
57      *
58      *  @param files a KRenameFile::List which is added to the internal list
59      */
60     void addFiles(const KRenameFile::List &files);
61 
62     /** Remove items in the model
63      *
64      *  @param remove list of indexes to remove
65      */
66     void removeFiles(const QList<int> &remove);
67 
68     /** Sort the data in the model
69      *  using the selected sort mode.
70      *
71      *  @param mode the sort mode to use
72      *  @param customSortToken customSortToken if mode is eSortMode_Token
73      *  @param customSortMode mode for sorting if mode is eSortMode_Token
74      */
75     void sortFiles(ESortMode mode, const QString &customSortToken,
76                    KRenameTokenSorter::ESimpleSortMode customSortMode);
77 
78     /** Get the current sort mode.
79      *  @returns the current sort mode
80      */
81     inline ESortMode getSortMode() const;
82 
83     inline QString getSortModeCustomToken() const;
84     inline KRenameTokenSorter::ESimpleSortMode getSortModeCustomMode() const;
85 
86     /** Move each file in a list of indices upwards
87      *  @param files list of file indices. Each file is moved up one position
88      */
89     void moveFilesUp(const QList<int> &files);
90 
91     /** Move each file in a list of indices downwards
92      *  @param files list of file indices. Each file is moved down one position
93      */
94     void moveFilesDown(const QList<int> &files);
95 
96     /** Creates a new model index
97      *
98      *  @param row the index of the requested file
99      *  @returns the model index for a certain row
100      */
101     const QModelIndex createIndex(int row) const;
102 
103     /** Get the file at position index.
104      *
105      *  @param a valid index in the internal vector
106      *
107      *  @returns a KRenameFile object
108      */
109     inline const KRenameFile &file(int index) const;
110 
111     /** Get the file at position index.
112      *
113      *  @param a valid index in the internal vector
114      *
115      *  @returns a KRenameFile object
116      */
117     inline KRenameFile &file(int index);
118 
119     virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
120     virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
121 
122     Qt::DropActions supportedDropActions() const;
123     QStringList mimeTypes() const;
124     Qt::ItemFlags flags(const QModelIndex &index) const;
125     bool dropMimeData(const QMimeData *data, Qt::DropAction action,
126                       int row, int column, const QModelIndex &parent);
127     bool setData(const QModelIndex &index, const QVariant &value, int role);
128 
129     /** Enable the preview of KRenameFile objects.
130      *
131      *  @param preview enable/disable preview
132      *  @param filenames if preview is true this items decides
133      *                   if the filename text is displayed next to the preview
134      */
135     inline void setEnablePreview(bool preview, bool filenames);
136     /** Test if the preview of KRenameFile objects is enabled.
137      *
138      *  @return preview enabled?
139      */
140     inline bool isPreviewEnabled() const;
141 
142     /** Run/open the file which is pointed to by the passed modelinex
143      *
144      *  @param index a modelindex specifying a file to open
145      *  @param window a window used to cache authentication information
146      */
147     void run(const QModelIndex &index, QWidget *window) const;
148 
149     /**
150      * Specify the extension split mode.
151      * \param splitMode split mode
152      * \param dot dot t use for eSplitMode_CustomDot
153      */
154     inline void setExtensionSplitMode(ESplitMode splitMode, unsigned int dot);
155 
156     inline ESplitMode splitMode();
157     inline unsigned int splitDot();
158 
159 Q_SIGNALS:
160     /** This signal is emitted when the maximum number of
161      *  dots in a filename that can be used to separate
162      *  filename and extension has changed (by adding a new file).
163      *
164      *  @param dots the maximum number of dots in a filename
165      */
166     void maxDotsChanged(int dots);
167 
168     /** Emitted when files have been added using drag and drop
169      */
170     void filesDropped();
171 
172 private Q_SLOTS:
173     void slotListerDone(ThreadedLister *lister);
174     void gotPreview(const KFileItem &item, const QPixmap &preview);
175 
176 private:
177     BatchRenamer *m_renamer;
178     KRenameFile::List *m_vector;
179 
180     bool               m_preview;
181     bool               m_text;
182 
183     int                m_maxDots;  ///< The maximum number of dots in a filename which can be used to separate filename and extension
184     const char        *m_mimeType; ///< MIME type for drag and drop operations
185 
186     ESortMode          m_eSortMode; ///< Last used sort mode
187     QString            m_customSortToken; ///< if m_eSortMode = eSortMode_Token
188     KRenameTokenSorter::ESimpleSortMode m_eCustomSortMode;  ///< if m_eSortMode = eSortMode_Token
189 
190     ESplitMode        m_eSplitMode;
191     unsigned int      m_dot;
192 };
193 
setRenamer(BatchRenamer * renamer)194 void KRenameModel::setRenamer(BatchRenamer *renamer)
195 {
196     m_renamer = renamer;
197 }
198 
getSortMode()199 ESortMode KRenameModel::getSortMode() const
200 {
201     return m_eSortMode;
202 }
203 
getSortModeCustomToken()204 QString KRenameModel::getSortModeCustomToken() const
205 {
206     return m_customSortToken;
207 }
208 
getSortModeCustomMode()209 KRenameTokenSorter::ESimpleSortMode KRenameModel::getSortModeCustomMode() const
210 {
211     return m_eCustomSortMode;
212 }
213 
file(int index)214 const KRenameFile &KRenameModel::file(int index) const
215 {
216     return m_vector->at(index);
217 }
218 
file(int index)219 KRenameFile &KRenameModel::file(int index)
220 {
221     return (*m_vector)[index];
222 }
223 
isPreviewEnabled()224 bool KRenameModel::isPreviewEnabled() const
225 {
226     return m_preview;
227 }
228 
setEnablePreview(bool preview,bool filenames)229 void KRenameModel::setEnablePreview(bool preview, bool filenames)
230 {
231     bool update = false;
232     if (m_preview != preview || filenames != m_text) {
233         update = true;
234     }
235 
236     m_preview = preview;
237     m_text    = filenames;
238 
239     if (update) {
240         // TODO: update the model
241         ;
242     }
243 }
244 
setExtensionSplitMode(ESplitMode splitMode,unsigned int dot)245 void KRenameModel::setExtensionSplitMode(ESplitMode splitMode, unsigned int dot)
246 {
247     m_eSplitMode = splitMode;
248     m_dot = dot;
249 }
250 
splitMode()251 ESplitMode KRenameModel::splitMode()
252 {
253     return m_eSplitMode;
254 }
255 
splitDot()256 unsigned int KRenameModel::splitDot()
257 {
258     return m_dot;
259 }
260 
261 class KRenamePreviewModel : public QAbstractTableModel
262 {
263     Q_OBJECT
264 public:
265     explicit KRenamePreviewModel(KRenameFile::List *vector);
266     ~KRenamePreviewModel();
267 
268     void refresh();
269 
270     virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
271     virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
272     virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
273 
274     virtual QModelIndex parent(const QModelIndex &index) const;
275     virtual QModelIndex sibling(int row, int column, const QModelIndex &index) const;
276 
277     virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
278 private:
279     KRenameFile::List *m_vector;
280 };
281 
282 #endif // KRENAMEMODEL_H
283 
284